-
Notifications
You must be signed in to change notification settings - Fork 3
/
grammar.js
233 lines (201 loc) · 6.1 KB
/
grammar.js
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
module.exports = grammar({
name: "glimmer",
externals: ($) => [$.comment],
rules: {
// Entire file
template: ($) => repeat($._declaration),
// Each individual "thing" in the file
_declaration: ($) =>
choice(
alias($.comment, $.comment_statement),
$.mustache_statement,
$.block_statement,
$.element_node,
$.text_node,
),
//
// Text
//
// Match anything that doesn't start with
// - An open/close HTML delimiter (<, >)
// - An open/close Mustache delimiter ({, })
text_node: () => token(/[^<>{}]+/),
//
// Primitives
//
string_literal: ($) =>
choice($._single_quote_string_literal, $._double_quote_string_literal),
// https://github.com/tree-sitter/tree-sitter-javascript/blob/37af80d372ae9e2f5adc2c6321d5a34294dc348b/grammar.js#L826
_single_quote_string_literal: () => seq("'", /[^'\\]*/, "'"),
// https://github.com/tree-sitter/tree-sitter-javascript/blob/37af80d372ae9e2f5adc2c6321d5a34294dc348b/grammar.js#L818
_double_quote_string_literal: () => seq('"', /[^"\\]*/, '"'),
number_literal: () => /[0-9]+/,
boolean_literal: () => choice("true", "false"),
//
// HTML Elements
//
// Match a sequence of letters, plus
// - @ (for arguments, so they can only appear first)
// - Hyphens (for web components)
// - Period (for contextual Glimmer components)
// - Colon (for component namespacing and named blocks)
tag_name: () => /(@?[a-zA-Z0-9]|-|:|\.)+/,
// "Normal" elements with separate opening and closing tags
element_node_start: ($) =>
seq(
"<",
$.tag_name,
repeat(
choice(
$.attribute_node,
$.mustache_statement,
alias($.comment, $.comment_statement),
),
),
optional($.block_params),
">",
),
element_node_end: ($) => seq("</", $.tag_name, ">"),
// "Void" elements are self-closing
element_node_void: ($) =>
seq(
"<",
$.tag_name,
repeat(
choice(
$.attribute_node,
$.mustache_statement,
alias($.comment, $.comment_statement),
),
),
"/>",
),
// An "Element" is either a "normal" or "void" element
element_node: ($) =>
choice(
seq($.element_node_start, repeat($._declaration), $.element_node_end),
$.element_node_void,
),
attribute_name: () => /[^<>"'/={}()\s\.,!?|]+/,
_splattributes: () => "...attributes",
attribute_node: ($) =>
choice(
seq(
$.attribute_name,
optional(
seq(
"=",
choice(
$.concat_statement,
$.number_literal,
$.mustache_statement,
),
),
),
),
alias($._splattributes, $.attribute_name),
),
// Special attribute-value strings that can embed a mustache statement
concat_statement: ($) =>
choice(
$._single_quote_concat_statement,
$._double_quote_concat_statement,
),
_single_quote_concat_statement: ($) =>
seq(
"'",
repeat(
choice(
$._mustache_safe_single_quote_string_literal_content,
$.mustache_statement,
),
),
"'",
),
_double_quote_concat_statement: ($) =>
seq(
'"',
repeat(
choice(
$._mustache_safe_double_quote_string_literal_content,
$.mustache_statement,
),
),
'"',
),
_mustache_safe_string_literal: ($) =>
choice(
$._mustache_safe_single_quote_string_literal,
$._mustache_safe_double_quote_string_literal,
),
_mustache_safe_single_quote_string_literal_content: () => /[^'\\{]+/,
_mustache_safe_single_quote_string_literal: ($) =>
seq("'", $._mustache_safe_single_quote_string_literal_content, "'"),
_mustache_safe_double_quote_string_literal_content: () => /[^"\\{]+/,
_mustache_safe_double_quote_string_literal: ($) =>
seq('"', $._mustache_safe_double_quote_string_literal_content, '"'),
block_params: ($) => seq("as", "|", repeat($.identifier), "|"),
identifier: () => /[^<>"'/={}()\s\.,!|]+/,
path_expression: ($) => seq($.identifier, repeat1(seq(".", $.identifier))),
// Represents anything that can be a "value"; things like
// - Strings
// - Numbers
// - Variables
// - Handlebars sub-expressions
_expression: ($) =>
choice(
prec(1, $.number_literal),
$.string_literal,
$.boolean_literal,
$.sub_expression,
$.path_expression,
$.identifier,
),
hash_pair: ($) =>
seq(field("key", $.identifier), "=", field("value", $._expression)),
mustache_statement: ($) =>
seq(
choice("{{", "{{~"),
choice($._expression, $.helper_invocation),
choice("}}", "~}}"),
),
sub_expression: ($) =>
seq("(", choice($._expression, $.helper_invocation), ")"),
// There *must* be either:
// - 1 or more positional argument and 0 or more hash pairs
// - 0 or more positional arguments and 1 or more hash pairs
_arguments: ($) =>
choice(
seq(repeat1(field("argument", $._expression)), repeat($.hash_pair)),
seq(repeat(field("argument", $._expression)), repeat1($.hash_pair)),
),
helper_invocation: ($) =>
seq(
field("helper", choice($.identifier, $.path_expression)),
$._arguments,
),
//
// Block Expression
//
block_statement_start: ($) =>
seq(
choice("{{#", "{{~#"),
field("path", $.identifier),
$._arguments,
optional($.block_params),
choice("}}", "~}}"),
),
block_statement_end: ($) =>
seq(
choice("{{/", "{{~/"),
field("path", $.identifier),
choice("}}", "~}}"),
),
block_statement: ($) =>
seq(
$.block_statement_start,
field("program", repeat($._declaration)),
$.block_statement_end,
),
},
});