Repository navigation
Expand file tree
/
Copy pathgrammar.js
More file actions
127 lines (107 loc) · 3.78 KB
/
Copy pathgrammar.js
File metadata and controls
127 lines (107 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
module.exports = grammar({
name: 'mdn',
word: ($) => $.ident,
rules: {
doc: ($) => repeat(choice($.macro_tag, $.text)),
macro_tag: ($) =>
seq(token.immediate('{{'), $.ident, optional($.args), '}}'),
text: (_$) =>
token(
repeat1(
choice(
/[^{]/, // any non-brace
/\\\{/, // escaped open brace
/\{\{ *\}\}/, // {{ }}
/\{[^{]/, // single {
),
),
),
// ---------------------------------------------------------------------------
// Function calls and arguments
// ---------------------------------------------------------------------------
// This could be straight forward but in order to yield none for empty arguments
// we need to split this up.
args: ($) =>
choice(
seq('(', optional($._arg), ')'), // one explicit or zero arguments.
seq(
choice(seq('(', $.none), seq('(', $._arg, ',')), // first explicit or implicit argument.
optional(repeat(choice(seq($._arg, ','), $.none))), // all up to the last.
choice(
// the last explitict or implitit or both.
seq($._arg, $.none, ')'),
seq($._arg, ')'),
seq($.none, ')', alias(')', $.none)),
),
),
),
_arg: ($) =>
choice(
alias(choice('""', '\'\''), $.none),
$.string,
$.float,
$.int,
$.boolean,
),
boolean: (_$) => choice('true', 'false'),
none: (_$) => prec(1, ','),
// ---------------------------------------------------------------------------
// Identifiers
// ---------------------------------------------------------------------------
ident: (_$) => /[a-zA-Z_][a-zA-Z0-9_\-]*/,
// ---------------------------------------------------------------------------
// Numbers
// ---------------------------------------------------------------------------
int: (_$) => token(choice(/-?0/, /-?[1-9]\d*/)),
float: (_$) => token(choice(/-?0\.\d+/, /-?[1-9]\d*\.\d+/)),
// ---------------------------------------------------------------------------
// Strings
// ---------------------------------------------------------------------------
string: ($) =>
choice(
$.double_quoted_string,
$.single_quoted_string,
$.backquoted_quoted_string,
),
double_quoted_string: ($) => seq('"', repeat($._dq_char), '"'),
single_quoted_string: ($) => seq('\'', repeat($._sq_char), '\''),
backquoted_quoted_string: ($) => seq('`', repeat($._bq_char), '`'),
// ---------------------------------------------------------------------------
// String content chars
// ---------------------------------------------------------------------------
// dq_char = {
// !("\"" | "\\") ~ ANY
// | "\\" ~ ("\"" | "\\" | "/" | "b" | "f" | "n" | "r" | "t")
// | "\\" ~ ("u" ~ ASCII_HEX_DIGIT{4})
// }
//
// In Tree-sitter, it’s typical to make these immediate tokens
// so that once we've started a string, we parse char-by-char.
_dq_char: (_$) =>
token.immediate(
choice(
/[^"\\\n]/,
seq('\\', choice('"', '\'', '`', '\\', '/', 'b', 'f', 'n', 'r', 't')),
seq('\\u', /[0-9A-Fa-f]{4}/),
),
),
// sq_char analogous
_sq_char: (_$) =>
token.immediate(
choice(
/[^'\\\n]/,
seq('\\', choice('"', '\'', '`', '\\', '/', 'b', 'f', 'n', 'r', 't')),
seq('\\u', /[0-9A-Fa-f]{4}/),
),
),
// bq_char analogous
_bq_char: (_$) =>
token.immediate(
choice(
/[^`\\\n]/,
seq('\\', choice('"', '\'', '`', '\\', '/', 'b', 'f', 'n', 'r', 't')),
seq('\\u', /[0-9A-Fa-f]{4}/),
),
),
},
});