-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrammar.js
More file actions
585 lines (510 loc) · 16.1 KB
/
grammar.js
File metadata and controls
585 lines (510 loc) · 16.1 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
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
module.exports = grammar({
name: 'leaf',
extras: $ => [
/\s/,
],
rules: {
template: $ => repeat(choice(
$.doctype,
$.html_comment,
$.leaf_directive,
$.leaf_variable,
$.leaf_tag,
$.html_element,
$.html_self_closing_tag,
$.text,
)),
// HTML Structure - FIXED: Proper void element handling
html_element: $ => choice(
// Void elements (no closing tag) - HIGHEST precedence
prec(3, seq(
'<',
field('name', $.void_tag_name),
repeat($.attribute),
'>',
)),
// Regular elements (with closing tag) - HIGH precedence
prec(2, seq(
$.start_tag,
optional($.html_content),
$.end_tag,
)),
),
html_self_closing_tag: $ => seq(
'<',
$.tag_name,
repeat($.attribute),
'/>',
),
// Void tag names - tokenize to avoid conflicts
void_tag_name: $ => token(choice(
'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input',
'link', 'meta', 'param', 'source', 'track', 'wbr'
)),
start_tag: $ => seq(
'<',
$.tag_name,
repeat($.attribute),
'>',
),
end_tag: $ => seq(
'</',
$.tag_name,
'>',
),
// Regular tag names - LOWER precedence than void tag names
tag_name: $ => token(prec(-2, /[a-zA-Z][a-zA-Z0-9-]*/)),
// FIXED: More robust attribute handling
attribute: $ => choice(
// Boolean attribute (no value)
$.attribute_name,
// Regular attribute with value
seq(
$.attribute_name,
'=',
choice(
$.quoted_attribute_value,
$.unquoted_attribute_value,
$.leaf_variable,
$.leaf_tag,
$.leaf_directive
),
),
),
// Define leaf directives inside attribute values
leaf_directive_in_attribute: $ => seq(
optional($.attribute_value),
$.leaf_directive,
optional($.attribute_value)
),
attribute_name: $ => /[a-zA-Z_:][a-zA-Z0-9_:.-]*/,
// FIXED: Simplified and more robust quoted attribute value with higher precedence
quoted_attribute_value: $ => prec(3, choice(
seq('"', choice($.attribute_value, $.leaf_directive_in_attribute), '"'),
seq("'", choice($.attribute_value, $.leaf_directive_in_attribute), "'"),
)),
unquoted_attribute_value: $ => /[^\s<>"'=`{}#]+/,
// Attribute value should recognize text mixed with import directives
attribute_value: $ => choice(
// Plain text without directives (for compatibility with most tests)
token(prec(-1, /[^"'#]*/)),
// Legacy format for compatibility - this is what we'll target with highlighting
token(prec(5, /#import\([^)]*\)/)),
),
// HTML content
html_content: $ => repeat1(choice(
$.leaf_directive,
$.leaf_variable,
$.leaf_tag,
$.html_element,
$.html_self_closing_tag,
$.html_comment,
$.text,
)),
// FIXED: More robust HTML comment handling with higher precedence
html_comment: $ => token(prec(10, seq(
'<!--',
repeat(choice(
/[^-]+/,
/-[^-]/,
/--[^>]/,
)),
'-->'
))),
// Leaf Specific Rules - UPDATED: Clear distinction using colon
leaf_directive: $ => choice(
$.if_directive,
$.unless_directive,
$.for_directive,
$.while_directive,
$.extend_directive,
$.simple_extend_directive,
$.export_directive,
$.simple_export_directive, // Keep this as a separate directive
$.import_directive,
$.evaluate_directive,
),
// Leaf tags (functions that can be used inline)
leaf_tag: $ => choice(
$.count_tag,
$.lowercased_tag,
$.uppercased_tag,
$.capitalized_tag,
$.contains_tag,
$.date_tag,
$.unsafe_html_tag,
$.dump_context_tag,
),
count_tag: $ => seq(
'#count',
'(',
$.expression,
')',
),
lowercased_tag: $ => seq(
'#lowercased',
'(',
$.expression,
')',
),
uppercased_tag: $ => seq(
'#uppercased',
'(',
$.expression,
')',
),
capitalized_tag: $ => seq(
'#capitalized',
'(',
$.expression,
')',
),
contains_tag: $ => seq(
'#contains',
'(',
$.expression,
',',
$.expression,
')',
),
date_tag: $ => seq(
'#date',
'(',
$.expression,
optional(seq(',', $.expression)),
optional(seq(',', $.expression)),
')',
),
unsafe_html_tag: $ => seq(
'#unsafeHTML',
'(',
$.expression,
')',
),
dump_context_tag: $ => '#dumpContext',
// Conditional Directives
if_directive: $ => prec(1, seq(
$.if_header,
optional('{'),
optional($.html_content),
optional('}'),
repeat(seq(
$.elseif_header,
optional('{'),
optional($.html_content),
optional('}'),
)),
optional(seq(
$.else_directive,
optional('{'),
optional($.html_content),
optional('}'),
)),
$.end_if_directive,
)),
unless_directive: $ => prec(1, seq(
$.unless_header,
optional('{'),
optional($.html_content),
optional('}'),
optional(seq(
$.else_directive,
optional('{'),
optional($.html_content),
optional('}'),
)),
$.end_unless_directive,
)),
// Loop Directives
for_directive: $ => prec(1, seq(
$.for_header,
optional('{'),
optional($.html_content),
optional('}'),
$.end_for_directive,
)),
while_directive: $ => prec(1, seq(
$.while_header,
optional('{'),
optional($.html_content),
optional('}'),
$.end_while_directive,
)),
// UPDATED: Block extend (with colon and content)
extend_directive: $ => seq(
$.extend_header_with_colon,
optional($.html_content),
$.end_extend_directive,
),
// UPDATED: Simple extend (no colon, no content)
simple_extend_directive: $ => $.extend_header,
// Export directive - supports both block and simple forms
export_directive: $ => choice(
// Block form: #export("key"): content #endexport
prec(2, seq(
$.export_header_block,
optional($.html_content),
$.end_export_directive,
)),
// Legacy format without colon for compatibility with tests
prec(1, seq(
$.export_header,
optional($.html_content),
$.end_export_directive,
)),
),
// Simple export with key and value parameters
simple_export_directive: $ => seq(
'#export',
'(',
$.string_literal,
',',
$.expression,
')',
),
// Export header without colon (legacy format for test compatibility)
export_header: $ => prec(1, seq(
'#export',
'(',
$.string_literal,
')',
)),
import_directive: $ => $.import_header,
evaluate_directive: $ => $.evaluate_header,
// Directive Headers - UPDATED: Separate headers with and without colon
if_header: $ => seq(
'#if',
'(',
$.expression,
')',
optional(':'),
),
elseif_header: $ => seq(
'#elseif',
'(',
$.expression,
')',
optional(':'),
),
else_directive: $ => choice('#else', '#else:'),
unless_header: $ => seq(
'#unless',
'(',
$.expression,
')',
optional(':'),
),
for_header: $ => seq(
'#for',
'(',
$.identifier,
'in',
$.expression,
')',
optional(':'),
),
while_header: $ => seq(
'#while',
'(',
$.expression,
')',
optional(':'),
),
// UPDATED: Header without colon for simple extend
extend_header: $ => seq(
'#extend',
'(',
$.string_literal,
')',
),
// NEW: Header with colon for block extend
extend_header_with_colon: $ => seq(
'#extend',
'(',
$.string_literal,
')',
':',
),
// Block export header (with colon)
export_header_block: $ => prec(2, seq(
'#export',
'(',
$.string_literal,
')',
optional(':'),
)),
import_header: $ => seq(
'#import',
'(',
$.string_literal,
')',
),
// Import directive parts will be handled via post-processing highlighting
evaluate_header: $ => seq(
'#evaluate',
'(',
$.expression,
')',
),
// End Directives
end_if_directive: $ => '#endif',
end_unless_directive: $ => '#endunless',
end_for_directive: $ => '#endfor',
end_while_directive: $ => '#endwhile',
end_extend_directive: $ => '#endextend',
end_export_directive: $ => '#endexport',
// Leaf Variables - UPDATED to handle simple identifiers specially
leaf_variable: $ => seq(
'#(',
choice(
$.simple_variable,
$.expression,
),
')',
),
// NEW: Simple variable rule for standalone identifiers
simple_variable: $ => prec(2, $.identifier),
// Expression system with proper precedence
expression: $ => choice(
$.ternary_expression,
$.binary_expression,
$.unary_expression,
$.postfix_expression,
),
// Postfix expressions handle member access, function calls, and array access
postfix_expression: $ => prec.left(12, choice(
$.primary_expression,
$.member_expression,
$.call_expression,
$.subscript_expression,
)),
// Member expressions (property access)
member_expression: $ => prec.left(12, seq(
$.postfix_expression,
'.',
$.identifier,
)),
// Call expressions (function/method calls)
call_expression: $ => prec.left(12, seq(
$.postfix_expression,
'(',
optional($.argument_list),
')',
)),
// Subscript expressions (array access)
subscript_expression: $ => prec.left(12, seq(
$.postfix_expression,
'[',
$.expression,
']',
)),
// Primary expressions - Updated to include tag function calls
primary_expression: $ => choice(
$.identifier,
$.string_literal,
$.number_literal,
$.boolean_literal,
$.null_literal,
$.array_literal,
$.dictionary_literal,
$.parenthesized_expression,
$.tag_function_call,
),
// NEW: Tag functions used in expressions (without #)
tag_function_call: $ => choice(
seq('count', '(', $.expression, ')'),
seq('lowercased', '(', $.expression, ')'),
seq('uppercased', '(', $.expression, ')'),
seq('capitalized', '(', $.expression, ')'),
seq('contains', '(', $.expression, ',', $.expression, ')'),
seq('date', '(', $.expression, optional(seq(',', $.expression)), optional(seq(',', $.expression)), ')'),
seq('unsafeHTML', '(', $.expression, ')'),
),
parenthesized_expression: $ => seq(
'(',
$.expression,
')',
),
binary_expression: $ => choice(
prec.left(10, seq($.expression, '*', $.expression)),
prec.left(10, seq($.expression, '/', $.expression)),
prec.left(10, seq($.expression, '%', $.expression)),
prec.left(9, seq($.expression, '+', $.expression)),
prec.left(9, seq($.expression, '-', $.expression)),
prec.left(8, seq($.expression, '<', $.expression)),
prec.left(8, seq($.expression, '<=', $.expression)),
prec.left(8, seq($.expression, '>', $.expression)),
prec.left(8, seq($.expression, '>=', $.expression)),
prec.left(7, seq($.expression, '==', $.expression)),
prec.left(7, seq($.expression, '!=', $.expression)),
prec.left(6, seq($.expression, choice('&&', 'and'), $.expression)),
prec.left(5, seq($.expression, choice('||', 'or'), $.expression)),
prec.left(4, seq($.expression, '??', $.expression)),
),
unary_expression: $ => choice(
prec(11, seq('!', $.expression)),
prec(11, seq('not', $.expression)),
prec(11, seq('-', $.expression)),
prec(11, seq('+', $.expression)),
),
ternary_expression: $ => prec.right(3, seq(
$.expression,
'?',
$.expression,
':',
$.expression,
)),
argument_list: $ => seq(
$.expression,
repeat(seq(',', $.expression)),
optional(','),
),
// Literals
identifier: $ => /[a-zA-Z_][a-zA-Z0-9_]*/,
string_literal: $ => choice(
seq('"', repeat(choice(/[^"\\]/, /\\./)), '"'),
seq("'", repeat(choice(/[^'\\]/, /\\./)), "'"),
),
number_literal: $ => choice(
/\d+\.\d+/,
/\d+/,
),
boolean_literal: $ => choice('true', 'false'),
null_literal: $ => 'nil',
array_literal: $ => seq(
'[',
optional(seq(
$.expression,
repeat(seq(',', $.expression)),
optional(','),
)),
']',
),
// Dictionary literals use curly braces to avoid conflict with arrays
dictionary_literal: $ => seq(
'{',
optional(seq(
$.dictionary_pair,
repeat(seq(',', $.dictionary_pair)),
optional(','),
)),
'}',
),
dictionary_pair: $ => seq(
choice($.string_literal, $.identifier),
':',
$.expression,
),
// FIXED: Text should not capture sequences that contain only whitespace
// Lower precedence to ensure other tokens take priority
text: $ => token(prec(-2, /[^\s<#][^<#]*/)),
// DOCTYPE
doctype: $ => seq(
'<',
token(prec(1, '!')),
/[Dd][Oo][Cc][Tt][Yy][Pp][Ee]/,
/[^>]+/,
'>',
),
},
});