-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar.js
526 lines (469 loc) · 13.8 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
const html = require("tree-sitter-html/grammar");
// taken from tree-sitter-php
const PREC = {
COMMA: -1,
CAST: -1,
LOGICAL_OR_2: 1,
LOGICAL_XOR: 2,
LOGICAL_AND_2: 3,
ASSIGNMENT: 4,
TERNARY: 5,
NULL_COALESCE: 6,
LOGICAL_OR_1: 7,
LOGICAL_AND_1: 8,
BITWISE_OR: 9,
BITWISE_XOR: 10,
BITWISE_AND: 11,
EQUALITY: 12,
INEQUALITY: 13,
SHIFT: 14,
PLUS: 15,
TIMES: 16,
NEG: 17,
INSTANCEOF: 18,
INC: 19,
SCOPE: 20,
NEW: 21,
CALL: 22,
MEMBER: 23,
DEREF: 24
};
module.exports = grammar(html, {
name: "htmlsmarty",
extras: $ => [
$.comment,
// TODO: technically this would be possible too, and a whole lot easier (only needs adjusting text, nothing else)
// but i dont know if this would then properly work with indentation for eg. smarty_if
//$.smarty_comment,
/\s+/,
],
rules: {
smarty_comment: ($) => seq(
"{*",
/[^*]*\*+([^}*][^*]*\*+)*/,
"}"
),
_smarty_expression: ($) => choice(
$._smarty_interpolation_expression,
$.smarty_function_call_expression,
),
// these are statements allowed in expression position
// eg. {$x = {inline_interpolation_function_call x=$y}}
_smarty_inline_interpolation: ($) => choice(
$.smarty_function_call,
$.smarty_interpolation, // on one hand, why would you do that - on the other hand, this is being used
),
// these are expressions directly allowed inside a smarty_interpolation
_smarty_interpolation_expression: ($) => choice(
$._smarty_variable,
$._smarty_literal,
$.smarty_binary_expression,
$.smarty_unary_op_expression,
$.smarty_parenthesized_expression,
$.smarty_filter_expression,
$._smarty_inline_interpolation,
),
// taken from tree-sitter-php
smarty_binary_expression: $ => choice(
...[
[alias(/and|AND/, 'and'), PREC.LOGICAL_AND_2],
[alias(/or|OR/, 'or'), PREC.LOGICAL_OR_2],
[alias(/xor|XOR/, 'xor'), PREC.LOGICAL_XOR],
['||', PREC.LOGICAL_OR_1],
['&&', PREC.LOGICAL_AND_1],
//['|', PREC.BITWISE_OR], // no bitwise or, used for pipes
['^', PREC.BITWISE_XOR],
['&', PREC.BITWISE_AND],
['==', PREC.EQUALITY],
['!=', PREC.EQUALITY],
['<>', PREC.EQUALITY],
['===', PREC.EQUALITY],
['!==', PREC.EQUALITY],
['<', PREC.INEQUALITY],
['>', PREC.INEQUALITY],
['<=', PREC.INEQUALITY],
['>=', PREC.INEQUALITY],
//['<=>', PREC.EQUALITY], // no spaceship operator
//['<<', PREC.SHIFT], // no shift
//['>>', PREC.SHIFT], // no shift
['+', PREC.PLUS],
['-', PREC.PLUS],
//['.', PREC.PLUS], // no concatenation, used for member access
['*', PREC.TIMES],
['/', PREC.TIMES],
['%', PREC.TIMES],
].map(([op, p]) => prec.left(p, seq(
field('left', $._smarty_expression),
field('operator', op),
field('right', $._smarty_expression)
)))
),
smarty_unary_op_expression: $ => prec.left(PREC.NEG, seq(
choice(
'+',
'-',
//'~', // no bitwise negation
'!'
),
$._smarty_expression
)),
smarty_parenthesized_expression: $ => seq(
'(',
$._smarty_expression,
')',
),
_smarty_variable: ($) => choice(
$.smarty_variable_name,
$.smarty_member_access_expression,
),
_smarty_literal: ($) => choice(
$.smarty_string,
$.smarty_integer,
$.smarty_float,
$.smarty_boolean,
$.smarty_null,
$.smarty_array_literal,
),
smarty_member_access_expression: ($) => seq(
$._smarty_variable,
choice(
seq(
'.',
$._smarty_member_name,
),
seq(
'[',
$._smarty_expression,
']'
)
)
),
_smarty_member_name: ($) => field('name', $.smarty_name),
smarty_interpolation: ($) => seq(
'{',
$._smarty_interpolation_expression,
'}'
),
smarty_assignment: ($) => seq(
'{',
field('left', $._smarty_variable),
"=",
field('right', $._smarty_expression),
'}'
),
// TODO: smarty allows both string interpolation "string $var string"
// (also with a backtick syntax "string `$var.member` string")
// as well as embedded smarty tags "string {smarty_function_call} string"
// see https://www.smarty.net/docsv2/en/language.syntax.quotes.tpl and https://smarty-php.github.io/smarty/5.x/designers/language-basic-syntax/language-syntax-quotes/
// both php and js need external scanners for that...
smarty_string: $ => token(choice(
seq(
"'",
repeat(/\\'|\\\\|\\?[^'\\]/),
"'"
),
seq(
'"',
repeat(/\\"|\\\\|\\?[^"\\]/),
'"'
)
)),
smarty_boolean: $ => /[Tt][Rr][Uu][Ee]|[Ff][Aa][Ll][Ss][Ee]/,
smarty_null: $ => keyword('null', false),
smarty_float: $ => /\d*((\.\d*)?([eE][\+-]?\d+)|(\.\d*)([eE][\+-]?\d+)?)/,
smarty_integer: $ => {
const decimal = /[1-9]\d*/
const octal = /0[0-7]*/
const hex = /0[xX][0-9a-fA-F]+/
const binary = /0[bB][01]+/
return token(choice(
decimal,
octal,
hex,
binary
))
},
smarty_array_literal: $ => choice(
seq('array', '(', optional($._smarty_array_elements), ')'),
seq('[', optional($._smarty_array_elements), ']')
),
_smarty_array_elements: $ => seq(
$.smarty_array_element,
repeat(seq(',', $.smarty_array_element)),
//optional(','), // smarty does not allow trailing commas currently
),
smarty_array_element: $ => prec.right(choice(
$._smarty_expression,
seq($._smarty_expression, '=>', $._smarty_expression)
)),
smarty_variable_name: $ => seq('$', $.smarty_name),
smarty_name: $ => smarty_name(),
_smarty_name_immediate: $ => alias(token.immediate(smarty_name()), $.smarty_name),
_smarty_if_condition: ($) => choice(
$._smarty_expression,
),
smarty_if_nodes: ($) => if_with_body(
$,
repeat($._node),
$.smarty_elseif_nodes,
$.smarty_else_nodes
),
smarty_elseif_nodes: ($) => elseif_with_body($, repeat($._node)),
smarty_else_nodes: ($) => else_with_body($, repeat($._node)),
smarty_if_attributes: ($) => if_with_body(
$,
repeat($._attribute),
$.smarty_elseif_attributes,
$.smarty_else_attributes
),
smarty_elseif_attributes: ($) => elseif_with_body($, repeat($._attribute)),
smarty_else_attributes: ($) => else_with_body($, repeat($._attribute)),
smarty_if_attrval_sq: ($) => if_with_body(
$,
repeat($._sq_attribute_value_fragment),
$.smarty_elseif_attrval_sq,
$.smarty_else_attrval_sq
),
smarty_elseif_attrval_sq: ($) => elseif_with_body($, repeat($._sq_attribute_value_fragment)),
smarty_else_attrval_sq: ($) => else_with_body($, repeat($._sq_attribute_value_fragment)),
smarty_if_attrval_dq: ($) => if_with_body(
$,
repeat($._dq_attribute_value_fragment),
$.smarty_elseif_attrval_dq,
$.smarty_else_attrval_dq
),
smarty_elseif_attrval_dq: ($) => elseif_with_body($, repeat($._dq_attribute_value_fragment)),
smarty_else_attrval_dq: ($) => else_with_body($, repeat($._dq_attribute_value_fragment)),
smarty_foreach_nodes: ($) => seq(
prefixedKeyword("{", "foreach"),
choice(
// new header
seq(
field('from', $._smarty_expression),
keyword("as"),
optional(seq(
field('key', $.smarty_variable_name),
'=>',
)),
field('item', $.smarty_variable_name)
),
// legacy header
repeat(choice(
seq(
"from",
"=",
field('from', $._smarty_expression),
),
seq(
"name",
"=",
$.smarty_string,
),
seq(
"item",
"=",
field('item', $.smarty_string),
),
seq(
"key",
"=",
field('key', $.smarty_string),
),
)),
),
"}",
repeat($._node),
field('alternative', optional($.smarty_foreachelse_nodes)),
"{/",
keyword("foreach"),
"}"
),
smarty_foreachelse_nodes: ($) => seq(
prefixedKeyword("{", "foreachelse"),
"}",
field('body', repeat($._node)),
),
smarty_function_definition: $ => seq(
prefixedKeyword("{", "function"),
field("function_name", $.smarty_name),
field("arguments", repeat(seq(
field("argument_name", $.smarty_name),
"=",
field("argument_value", $._smarty_literal),
))),
"}",
field("body", repeat($._node)),
"{/",
keyword("function"),
"}"
),
smarty_function_call: $ => seq(
"{",
field("function_name", $._smarty_name_immediate),
field("arguments", choice(
repeat(seq(
field("argument_name", $.smarty_name),
"=",
field("argument_value", $._smarty_expression),
)),
// {function_name($argument)}
// we do not support {function_name($argument) + 1}
// in theory, this could (should?) be smarty_interpolation + smarty_function_call_expression
// however, then we get errors, because it conflicts with this...
// so i guess ill live with the slight inconsistency
seq(
'(',
optional(field('arguments', seq(
$._smarty_expression,
repeat(seq(',', $._smarty_expression)),
))),
')'
),
)),
"}"
),
smarty_filter_expression: $ => prec.left(seq(
field("first_argument", $._smarty_expression),
"|",
field("filter_name", $.smarty_name),
field("arguments", repeat(prec.left(seq(
":",
field("argument_value", $._smarty_expression),
)))),
)),
smarty_function_call_expression: $ => seq(
field("function_name", $.smarty_name),
'(',
optional(field('arguments', seq(
$._smarty_expression,
repeat(seq(',', $._smarty_expression)),
))),
')'
),
// in text
// disallow { followed by non-space char
// FIXME: see comment in test/corpus/unpaired.txt for why this is broken in the edge case of
// `text {<valid-tag/>`
text: $ => /([^<>{]|\{\s)+/,
_node: $ => choice(
$.doctype,
$.text,
$.smarty_comment,
$.smarty_if_nodes,
$.smarty_foreach_nodes,
$.smarty_interpolation,
$.smarty_assignment,
$.smarty_function_definition,
$.smarty_function_call,
$.element,
$.script_element,
$.style_element,
$.erroneous_end_tag,
),
// in attribute lists
_attribute: $ => choice(
$.attribute,
$.smarty_comment,
$.smarty_if_attributes,
$.smarty_function_call,
),
attribute_name: $ => choice(/([^<>"'/=\s{]|\{\s)+/), // disallow { followed by non-space char
// this is necessary as otherwise we get errors
attribute_value: $ => choice(/([^<>"'=\s{]|\{\s)+/, "{"), // disallow { followed by non-space char
_sq_attribute_value_fragment: $ => choice(
$.smarty_if_attrval_sq,
$.smarty_comment,
$.smarty_interpolation,
$.smarty_function_call,
alias(/([^'{]|\{ )+/, $.attribute_value) // smarty ignores brackets followed by space
),
_dq_attribute_value_fragment: $ => choice(
$.smarty_if_attrval_dq,
$.smarty_comment,
$.smarty_interpolation,
$.smarty_function_call,
alias(/([^"{]|\{ )+/, $.attribute_value),
),
quoted_attribute_value: $ => choice(
seq("'", repeat($._sq_attribute_value_fragment), optional("{"), "'"),
seq('"', repeat($._dq_attribute_value_fragment), optional("{"), '"'),
),
start_tag: $ => seq(
'<',
alias($._start_tag_name, $.tag_name),
repeat($._attribute),
'>'
),
script_start_tag: $ => seq(
'<',
alias($._script_start_tag_name, $.tag_name),
repeat($._attribute),
'>'
),
style_start_tag: $ => seq(
'<',
alias($._style_start_tag_name, $.tag_name),
repeat($._attribute),
'>'
),
self_closing_tag: $ => seq(
'<',
alias($._start_tag_name, $.tag_name),
repeat($._attribute),
'/>'
),
}
});
function smarty_name() {
return /[_a-zA-Z\u00A1-\u00ff][_a-zA-Z\u00A1-\u00ff\d]*/;
}
function if_with_body($, body, elseif, else_) {
return seq(
prefixedKeyword("{", "if"),
field('condition', $._smarty_if_condition),
"}",
field('body', body),
repeat(field('alternative_condition', elseif)),
optional(field('alternative', else_)),
"{/",
keyword("if"),
"}"
);
}
function elseif_with_body($, body) {
return seq(
prefixedKeyword("{", "elseif"),
field('condition', $._smarty_if_condition),
"}",
field('body', body),
);
}
function else_with_body($, body) {
return seq(
prefixedKeyword("{", "else"),
"}",
field('body', body),
);
}
function prefixedKeyword(prefix, word, aliasAsWord = true) {
// TODO: this currently highlights the entire "{else", not just the "else"
// I would need eg. https://github.com/tree-sitter/tree-sitter/issues/1944 to fix this properly
// now I'm just using the nvim specific workaround
let pattern = ''
for (const letter of word) {
pattern += `[${letter}${letter.toLocaleUpperCase()}]`
}
let result = new RegExp(pattern)
if (aliasAsWord) result = alias(result, word)
return alias(token(seq(prefix, token.immediate(result))), word)
}
// taken from tree-sitter-php
function keyword(word, aliasAsWord = true) {
let pattern = ''
for (const letter of word) {
pattern += `[${letter}${letter.toLocaleUpperCase()}]`
}
let result = new RegExp(pattern)
if (aliasAsWord) result = alias(result, word)
return result
}