-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgrammar.js
1185 lines (1107 loc) · 38.3 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
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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const ELEMENT_PRECEDENCE = 100;
module.exports = grammar({
name: "djot",
extras: (_) => ["\r"],
conflicts: ($) => [
[$.emphasis_begin, $._symbol_fallback],
[$.strong_begin, $._symbol_fallback],
[$.superscript_begin, $._symbol_fallback],
[$.subscript_begin, $._symbol_fallback],
[$.highlighted_begin, $._symbol_fallback],
[$.insert_begin, $._symbol_fallback],
[$.delete_begin, $._symbol_fallback],
[$._bracketed_text_begin, $._symbol_fallback],
[$._image_description_begin, $._symbol_fallback],
[$.footnote_marker_begin, $._symbol_fallback],
[$.math, $._symbol_fallback],
[$.link_text, $._symbol_fallback],
[$._curly_bracket_span_begin, $._curly_bracket_span_fallback],
],
rules: {
document: ($) =>
seq(optional($.frontmatter), repeat($._block_with_section)),
frontmatter: ($) =>
seq(
$.frontmatter_marker,
$._whitespace,
optional(field("language", $.language)),
$._newline,
field("content", $.frontmatter_content),
$.frontmatter_marker,
$._newline,
),
frontmatter_content: ($) => repeat1($._line),
// A section is only valid on the top level, or nested inside other sections.
// Otherwise standalone headings are used (inside divs for example).
_block_with_section: ($) => choice($.section, $._block_element, $._newline),
_block_with_heading: ($) =>
seq(
optional($._block_quote_continuation),
choice($.heading, $._block_element, $._newline),
),
_block_element: ($) =>
choice(
$.list,
$.table,
$.footnote,
$.div,
$.raw_block,
$.code_block,
$.thematic_break,
$.block_quote,
$.link_reference_definition,
$.block_attribute,
$._paragraph,
),
// Section should end by a new header with the same or fewer amount of '#'.
section: ($) =>
seq(
field("heading", $.heading),
field(
"content",
alias(repeat($._block_with_section), $.section_content),
),
$._block_close,
),
// The external scanner allows for an arbitrary number of `#`
// that can be continued on the next line.
heading: ($) =>
seq(
field("marker", alias($._heading_begin, $.marker)),
field("content", alias($._heading_content, $.content)),
$._block_close,
optional($._eof_or_newline),
),
_heading_content: ($) =>
seq(
$._inline_line,
repeat(seq(alias($._heading_continuation, $.marker), $._inline_line)),
),
// Djot has a crazy number of different list types
// that we need to keep separate from each other.
list: ($) =>
prec.left(
choice(
$._list_dash,
$._list_plus,
$._list_star,
$._list_task,
$._list_definition,
$._list_decimal_period,
$._list_decimal_paren,
$._list_decimal_parens,
$._list_lower_alpha_period,
$._list_lower_alpha_paren,
$._list_lower_alpha_parens,
$._list_upper_alpha_period,
$._list_upper_alpha_paren,
$._list_upper_alpha_parens,
$._list_lower_roman_period,
$._list_lower_roman_paren,
$._list_lower_roman_parens,
$._list_upper_roman_period,
$._list_upper_roman_paren,
$._list_upper_roman_parens,
),
),
_list_dash: ($) =>
seq(repeat1(alias($._list_item_dash, $.list_item)), $._block_close),
_list_item_dash: ($) =>
seq(
optional($._block_quote_prefix),
field("marker", $.list_marker_dash),
field("content", $.list_item_content),
),
_list_plus: ($) =>
seq(repeat1(alias($._list_item_plus, $.list_item)), $._block_close),
_list_item_plus: ($) =>
seq(
optional($._block_quote_prefix),
field("marker", $.list_marker_plus),
field("content", $.list_item_content),
),
_list_star: ($) =>
seq(repeat1(alias($._list_item_star, $.list_item)), $._block_close),
_list_item_star: ($) =>
seq(
optional($._block_quote_prefix),
field("marker", $.list_marker_star),
field("content", $.list_item_content),
),
_list_task: ($) =>
seq(repeat1(alias($._list_item_task, $.list_item)), $._block_close),
_list_item_task: ($) =>
seq(
optional($._block_quote_prefix),
field("marker", $.list_marker_task),
field("content", $.list_item_content),
),
list_marker_task: ($) =>
seq(
$._list_marker_task_begin,
field("checkmark", choice($.checked, $.unchecked)),
$._whitespace1,
),
checked: (_) => seq("[", choice("x", "X"), "]"),
unchecked: (_) => seq("[", " ", "]"),
_list_definition: ($) =>
seq(repeat1(alias($._list_item_definition, $.list_item)), $._block_close),
_list_item_definition: ($) =>
seq(
field("marker", $.list_marker_definition),
field("term", alias($._paragraph_content, $.term)),
choice($._eof_or_newline, $._close_paragraph),
field(
"definition",
alias(
optional(
repeat(
seq(
optional($._block_quote_prefix),
$._list_item_continuation,
$._block_with_heading,
),
),
),
$.definition,
),
),
$._list_item_end,
),
_list_decimal_period: ($) =>
seq(
repeat1(alias($._list_item_decimal_period, $.list_item)),
$._block_close,
),
_list_item_decimal_period: ($) =>
seq(
optional($._block_quote_prefix),
field("marker", $.list_marker_decimal_period),
field("content", $.list_item_content),
),
_list_decimal_paren: ($) =>
seq(
repeat1(alias($._list_item_decimal_paren, $.list_item)),
$._block_close,
),
_list_item_decimal_paren: ($) =>
seq(
optional($._block_quote_prefix),
field("marker", $.list_marker_decimal_paren),
field("content", $.list_item_content),
),
_list_decimal_parens: ($) =>
seq(
repeat1(alias($._list_item_decimal_parens, $.list_item)),
$._block_close,
),
_list_item_decimal_parens: ($) =>
seq(
optional($._block_quote_prefix),
field("marker", $.list_marker_decimal_parens),
field("content", $.list_item_content),
),
_list_lower_alpha_period: ($) =>
seq(
repeat1(alias($._list_item_lower_alpha_period, $.list_item)),
$._block_close,
),
_list_item_lower_alpha_period: ($) =>
seq(
optional($._block_quote_prefix),
field("marker", $.list_marker_lower_alpha_period),
field("content", $.list_item_content),
),
_list_lower_alpha_paren: ($) =>
seq(
repeat1(alias($._list_item_lower_alpha_paren, $.list_item)),
$._block_close,
),
_list_item_lower_alpha_paren: ($) =>
seq(
optional($._block_quote_prefix),
field("marker", $.list_marker_lower_alpha_paren),
field("content", $.list_item_content),
),
_list_lower_alpha_parens: ($) =>
seq(
repeat1(alias($._list_item_lower_alpha_parens, $.list_item)),
$._block_close,
),
_list_item_lower_alpha_parens: ($) =>
seq(
optional($._block_quote_prefix),
field("marker", $.list_marker_lower_alpha_parens),
field("content", $.list_item_content),
),
_list_upper_alpha_period: ($) =>
seq(
repeat1(alias($._list_item_upper_alpha_period, $.list_item)),
$._block_close,
),
_list_item_upper_alpha_period: ($) =>
seq(
optional($._block_quote_prefix),
field("marker", $.list_marker_upper_alpha_period),
field("content", $.list_item_content),
),
_list_upper_alpha_paren: ($) =>
seq(
repeat1(alias($._list_item_upper_alpha_paren, $.list_item)),
$._block_close,
),
_list_item_upper_alpha_paren: ($) =>
seq(
optional($._block_quote_prefix),
field("marker", $.list_marker_upper_alpha_paren),
field("content", $.list_item_content),
),
_list_upper_alpha_parens: ($) =>
seq(
repeat1(alias($._list_item_upper_alpha_parens, $.list_item)),
$._block_close,
),
_list_item_upper_alpha_parens: ($) =>
seq(
optional($._block_quote_prefix),
field("marker", $.list_marker_upper_alpha_parens),
field("content", $.list_item_content),
),
_list_lower_roman_period: ($) =>
seq(
repeat1(alias($._list_item_lower_roman_period, $.list_item)),
$._block_close,
),
_list_item_lower_roman_period: ($) =>
seq(
optional($._block_quote_prefix),
field("marker", $.list_marker_lower_roman_period),
field("content", $.list_item_content),
),
_list_lower_roman_paren: ($) =>
seq(
repeat1(alias($._list_item_lower_roman_paren, $.list_item)),
$._block_close,
),
_list_item_lower_roman_paren: ($) =>
seq(
optional($._block_quote_prefix),
field("marker", $.list_marker_lower_roman_paren),
field("content", $.list_item_content),
),
_list_lower_roman_parens: ($) =>
seq(
repeat1(alias($._list_item_lower_roman_parens, $.list_item)),
$._block_close,
),
_list_item_lower_roman_parens: ($) =>
seq(
optional($._block_quote_prefix),
field("marker", $.list_marker_lower_roman_parens),
field("content", $.list_item_content),
),
_list_upper_roman_period: ($) =>
seq(
repeat1(alias($._list_item_upper_roman_period, $.list_item)),
$._block_close,
),
_list_item_upper_roman_period: ($) =>
seq(
optional($._block_quote_prefix),
field("marker", $.list_marker_upper_roman_period),
field("content", $.list_item_content),
),
_list_upper_roman_paren: ($) =>
seq(
repeat1(alias($._list_item_upper_roman_paren, $.list_item)),
$._block_close,
),
_list_item_upper_roman_paren: ($) =>
seq(
optional($._block_quote_prefix),
field("marker", $.list_marker_upper_roman_paren),
field("content", $.list_item_content),
),
_list_upper_roman_parens: ($) =>
seq(
repeat1(alias($._list_item_upper_roman_parens, $.list_item)),
$._block_close,
),
_list_item_upper_roman_parens: ($) =>
seq(
optional($._block_quote_prefix),
field("marker", $.list_marker_upper_roman_parens),
field("content", $.list_item_content),
),
list_item_content: ($) =>
seq(
$._block_with_heading,
$._indented_content_spacer,
optional(
repeat(
seq(
optional($._block_quote_prefix),
$._list_item_continuation,
$._block_with_heading,
$._indented_content_spacer,
),
),
),
$._list_item_end,
),
table: ($) =>
prec.right(
seq(
repeat1($._table_row),
optional($._newline),
optional($.table_caption),
),
),
_table_row: ($) =>
seq(
optional($._block_quote_prefix),
choice($.table_header, $.table_separator, $.table_row),
),
table_header: ($) =>
seq(
alias($._table_header_begin, "|"),
repeat($._table_cell),
$._table_row_end_newline,
),
table_separator: ($) =>
seq(
alias($._table_separator_begin, "|"),
repeat($._table_cell_alignment),
$._table_row_end_newline,
),
table_row: ($) =>
seq(
alias($._table_row_begin, "|"),
repeat($._table_cell),
$._table_row_end_newline,
),
_table_cell: ($) =>
seq(alias($._inline, $.table_cell), alias($._table_cell_end, "|")),
_table_cell_alignment: ($) =>
seq(
// Note that alignment appearance is already checked in the external
// scanner when `_table_separator_begin` is output.
// Therefore this regex can be simplified.
alias(token.immediate(/[^|]+/), $.table_cell_alignment),
alias($._table_cell_end, "|"),
),
table_caption: ($) =>
seq(
field("marker", alias($._table_caption_begin, $.marker)),
field("content", alias(repeat1($._inline_line), $.content)),
choice($._table_caption_end, "\0"),
),
footnote: ($) =>
seq(
$._footnote_mark_begin,
$.footnote_marker_begin,
field("label", $.reference_label),
alias("]:", $.footnote_marker_end),
$._whitespace1,
field("content", $.footnote_content),
),
footnote_content: ($) =>
seq(
$._block_with_heading,
$._indented_content_spacer,
optional(
repeat(
seq(
optional($._block_quote_prefix),
$._footnote_continuation,
$._block_with_heading,
$._indented_content_spacer,
),
),
),
$._footnote_end,
),
div: ($) =>
seq(
$._div_marker_begin,
$._newline,
field("content", alias(repeat($._block_with_heading), $.content)),
optional($._block_quote_prefix),
$._block_close,
optional(seq(alias($._div_end, $.div_marker_end), $._newline)),
),
_div_marker_begin: ($) =>
seq(
alias($._div_begin, $.div_marker_begin),
optional(seq($._whitespace1, field("class", $.class_name))),
),
class_name: ($) => $._id,
code_block: ($) =>
seq(
alias($._code_block_begin, $.code_block_marker_begin),
$._whitespace,
optional(field("language", $.language)),
$._newline,
optional(field("code", $.code)),
$._block_close,
optional(
seq(alias($._code_block_end, $.code_block_marker_end), $._newline),
),
),
raw_block: ($) =>
seq(
alias($._code_block_begin, $.raw_block_marker_begin),
$._whitespace,
field("info", $.raw_block_info),
$._newline,
field("content", optional(alias($.code, $.content))),
$._block_close,
optional(
seq(alias($._code_block_end, $.raw_block_marker_end), $._newline),
),
),
raw_block_info: ($) =>
seq(
field("marker", alias("=", $.language_marker)),
field("language", $.language),
),
language: (_) => /[^\n\t \{\}=]+/,
code: ($) =>
prec.left(repeat1(seq(optional($._block_quote_prefix), $._line))),
_line: ($) => seq(/[^\n]*/, $._newline),
thematic_break: ($) =>
seq(choice($._thematic_break_dash, $._thematic_break_star), $._newline),
block_quote: ($) =>
seq(
alias($._block_quote_begin, $.block_quote_marker),
field("content", alias($._block_quote_content, $.content)),
$._block_close,
),
_block_quote_content: ($) =>
seq(
choice($.heading, $._block_element),
repeat(seq($._block_quote_prefix, optional($._block_element))),
),
_block_quote_prefix: ($) =>
prec.left(
repeat1(
prec.left(alias($._block_quote_continuation, $.block_quote_marker)),
),
),
link_reference_definition: ($) =>
seq(
$._link_ref_def_mark_begin,
"[",
field("label", alias($._inline, $.link_label)),
$._link_ref_def_label_end,
"]",
":",
optional(seq($._whitespace1, field("destination", $.link_destination))),
$._newline,
),
link_destination: (_) => /\S+/,
block_attribute: ($) =>
seq(
alias($._block_attribute_begin, "{"),
field(
"args",
alias(
repeat(
choice(
$.class,
$.identifier,
$.key_value,
alias($._comment, $.comment),
$._whitespace1,
$._newline,
),
),
$.args,
),
),
"}",
$._newline,
),
class: ($) => seq(".", alias($.class_name, "class")),
identifier: (_) => token(seq("#", token.immediate(/[^\s\}]+/))),
key_value: ($) => seq(field("key", $.key), "=", field("value", $.value)),
key: ($) => $._id,
value: (_) => choice(seq('"', /[^"\n]+/, '"'), /\w+/),
// Paragraphs are a bit special parsing wise as it's the "fallback"
// block, where everything that doesn't fit will go.
// There's no "start" token and they're not tracked by the external scanner.
//
// Instead they're ended by either a blankline or by an explicit
// `_close_paragraph` token (by for instance div markers).
//
// Lines inside paragraphs are handled by the `_newline_inline` token
// that's a newline character only valid inside an `_inline` context.
// When the `newline_inline` token is no longer valid, the `_newline`
// token can be emitted which closes the paragraph content.
_paragraph: ($) =>
seq(
alias($._paragraph_content, $.paragraph),
// Blankline is split out from paragraph to enable textobject
// to not select newline up to following text.
choice($._eof_or_newline, $._close_paragraph),
),
_paragraph_content: ($) =>
// Newlines inside inline blocks should be of the `_newline_inline` type.
seq(
optional($._block_quote_prefix),
$._inline,
repeat(
seq($._newline_inline, optional($._block_quote_prefix), $._inline),
),
// Last newline can be of the normal variant to signal the end of the paragraph.
$._eof_or_newline,
),
_whitespace: (_) => token.immediate(/[ \t]*/),
_whitespace1: (_) => token.immediate(/[ \t]+/),
_inline: ($) =>
prec.left(
repeat1(choice($._inline_element, $._newline_inline, $._whitespace1)),
),
_inline_without_trailing_space: ($) =>
seq(
prec.left(
repeat(choice($._inline_element, $._newline_inline, $._whitespace1)),
),
$._inline_element,
),
_inline_element: ($) =>
prec.left(
choice(
// Span is declared separately because it always parses an `inline_attribute`,
// while the attribute is optional for everything else.
$.span,
seq(
choice(
$._smart_punctuation,
$.backslash_escape,
$.hard_line_break,
// Elements containing other inline elements needs to have the same precedence level
// so we can choose the element that's closed first.
//
// For example:
//
// *[x](y*)
//
// Should parse a strong element instead of a link because it's closed before the link.
//
// They also need a higher precedence than the fallback tokens so that:
//
// _a_
//
// Is parsed as emphasis instead of just text with `_symbol_fallback` tokens.
prec.dynamic(ELEMENT_PRECEDENCE, $.emphasis),
prec.dynamic(ELEMENT_PRECEDENCE, $.strong),
prec.dynamic(ELEMENT_PRECEDENCE, $.highlighted),
prec.dynamic(ELEMENT_PRECEDENCE, $.superscript),
prec.dynamic(ELEMENT_PRECEDENCE, $.subscript),
prec.dynamic(ELEMENT_PRECEDENCE, $.insert),
prec.dynamic(ELEMENT_PRECEDENCE, $.delete),
prec.dynamic(ELEMENT_PRECEDENCE, $.footnote_reference),
prec.dynamic(ELEMENT_PRECEDENCE, $._image),
prec.dynamic(ELEMENT_PRECEDENCE, $._link),
$.autolink,
$.verbatim,
$.math,
$.raw_inline,
$.symbol,
$.inline_comment,
$._todo_highlights,
// Text and the symbol fallback matches everything not matched elsewhere.
$._symbol_fallback,
$._text,
),
optional(
// We need a separate fallback token for the opening `{`
// for the parser to recognize the conflict.
choice(
// Use precedence for inline attribute as well to allow
// closure before other elements.
prec.dynamic(
2 * ELEMENT_PRECEDENCE,
field("attribute", $.inline_attribute),
),
$._curly_bracket_span_fallback,
),
),
),
),
),
_inline_line: ($) => seq($._inline, $._eof_or_newline),
_smart_punctuation: ($) =>
choice($.quotation_marks, $.ellipsis, $.em_dash, $.en_dash),
// It would be nice to be able to mark bare " and ', but then we'd have to be smarter
// so we don't mark the ' in `it's`. Not sure if we can do that in a correct way.
quotation_marks: (_) => token(choice('{"', '"}', "{'", "'}", '\\"', "\\'")),
ellipsis: (_) => "...",
em_dash: (_) => "---",
en_dash: (_) => "--",
backslash_escape: (_) => /\\[^\r\n]/,
autolink: (_) => seq("<", /[^>\s]+/, ">"),
symbol: (_) => token(seq(":", /[\w\d_-]+/, ":")),
// Emphasis and strong are a little special as they don't allow spaces next
// to begin and end markers unless using the bracketed variant.
// The strategy to solve this:
//
// Begin: Use the zero-width `$._non_whitespace_check` token to avoid the `_ ` case.
// End: Use `$._inline_without_trailing_space` to match inline without a trailing space
// and let the end token in the external scanner consume space for the `_}` case
// and not for the `_` case.
emphasis: ($) =>
seq(
field("begin_marker", $.emphasis_begin),
$._emphasis_mark_begin,
field("content", alias($._inline_without_trailing_space, $.content)),
field("end_marker", $.emphasis_end),
),
emphasis_begin: ($) => choice("{_", seq("_", $._non_whitespace_check)),
strong: ($) =>
seq(
field("begin_marker", $.strong_begin),
$._strong_mark_begin,
field("content", alias($._inline_without_trailing_space, $.content)),
field("end_marker", $.strong_end),
),
strong_begin: ($) => choice("{*", seq("*", $._non_whitespace_check)),
// The syntax description isn't clear about if non-bracket can contain surrounding spaces.
// The live playground suggests that yes they can, although it's a bit inconsistent.
superscript: ($) =>
seq(
field("begin_marker", $.superscript_begin),
$._superscript_mark_begin,
field("content", alias($._inline, $.content)),
field("end_marker", $.superscript_end),
),
superscript_begin: (_) => choice("{^", "^"),
subscript: ($) =>
seq(
field("begin_marker", $.subscript_begin),
$._subscript_mark_begin,
field("content", alias($._inline, $.content)),
field("end_marker", $.subscript_end),
),
subscript_begin: (_) => choice("{~", "~"),
highlighted: ($) =>
seq(
field("begin_marker", $.highlighted_begin),
$._highlighted_mark_begin,
field("content", alias($._inline, $.content)),
field("end_marker", $.highlighted_end),
),
highlighted_begin: (_) => "{=",
insert: ($) =>
seq(
field("begin_marker", $.insert_begin),
$._insert_mark_begin,
field("content", alias($._inline, $.content)),
field("end_marker", $.insert_end),
),
insert_begin: (_) => "{+",
delete: ($) =>
seq(
field("begin_marker", $.delete_begin),
$._delete_mark_begin,
field("content", alias($._inline, $.content)),
field("end_marker", $.delete_end),
),
delete_begin: (_) => "{-",
footnote_reference: ($) =>
seq(
$.footnote_marker_begin,
$._square_bracket_span_mark_begin,
$.reference_label,
alias($._square_bracket_span_end, $.footnote_marker_end),
),
footnote_marker_begin: (_) => "[^",
reference_label: ($) => $._id,
_id: (_) => /[\w_-]+/,
_image: ($) =>
choice(
$.full_reference_image,
$.collapsed_reference_image,
$.inline_image,
),
full_reference_image: ($) =>
seq(field("description", $.image_description), $._link_label),
collapsed_reference_image: ($) =>
seq(field("description", $.image_description), token.immediate("[]")),
inline_image: ($) =>
seq(
field("description", $.image_description),
field("destination", $.inline_link_destination),
),
image_description: ($) =>
seq(
$._image_description_begin,
$._square_bracket_span_mark_begin,
optional($._inline),
alias($._square_bracket_span_end, "]"),
),
_image_description_begin: (_) => "![",
_link: ($) =>
choice($.full_reference_link, $.collapsed_reference_link, $.inline_link),
full_reference_link: ($) => seq(field("text", $.link_text), $._link_label),
collapsed_reference_link: ($) =>
seq(field("text", $.link_text), token.immediate("[]")),
inline_link: ($) =>
seq(
field("text", $.link_text),
field("destination", $.inline_link_destination),
),
link_text: ($) =>
choice(
seq(
$._bracketed_text_begin,
$._square_bracket_span_mark_begin,
$._inline,
// Alias to "]" to allow us to highlight it in Neovim.
// Maybe some bug, or some undocumented behavior?
alias($._square_bracket_span_end, "]"),
),
// Required as we track fallback characters between bracketed begin and end,
// but when it's empty it skips blocks the inline link destination.
// This is an easy workaround for that special case.
"[]",
),
span: ($) =>
seq(
$._bracketed_text_begin,
$._square_bracket_span_mark_begin,
field("content", alias($._inline, $.content)),
// Prefer span over regular text + inline attribute.
prec.dynamic(
ELEMENT_PRECEDENCE,
alias($._square_bracket_span_end, "]"),
),
field("attribute", $.inline_attribute),
),
_bracketed_text_begin: (_) => "[",
inline_attribute: ($) =>
seq(
$._curly_bracket_span_begin,
$._curly_bracket_span_mark_begin,
alias(
repeat(
choice(
$.class,
$.identifier,
$.key_value,
alias($._comment, $.comment),
$._whitespace1,
$._newline_inline,
),
),
$.args,
),
alias($._curly_bracket_span_end, "}"),
),
_curly_bracket_span_begin: (_) => "{",
_bracketed_text: ($) =>
seq(
$._bracketed_text_begin,
$._square_bracket_span_mark_begin,
$._inline,
$._square_bracket_span_end,
),
_link_label: ($) =>
seq(
"[",
field("label", alias($._inline, $.link_label)),
token.immediate("]"),
),
inline_link_destination: ($) =>
seq(
$._parens_span_begin,
$._parens_span_mark_begin,
$._inline_link_url,
alias($._parens_span_end, ")"),
),
_inline_link_url: ($) =>
// Can escape `)`, but shouldn't capture it.
repeat1(choice(/([^\)\n]|\\\))+/, $._newline_inline)),
_parens_span_begin: (_) => "(",
_comment: ($) =>
seq(
"%",
field(
"content",
alias(repeat(choice($.backslash_escape, /[^%}]/)), $.content),
),
choice(alias($._comment_end_marker, "%"), $._comment_close),
),
inline_comment: ($) =>
seq(
$._whitespace1,
$._inline_comment_begin,
$._curly_bracket_span_mark_begin,
$._comment,
alias($._curly_bracket_span_end, "}"),
),
raw_inline: ($) =>
seq(
field(
"begin_marker",
alias($._verbatim_begin, $.raw_inline_marker_begin),
),
field("content", alias($._verbatim_content, $.content)),
field("end_marker", alias($._verbatim_end, $.raw_inline_marker_end)),
field("attribute", $.raw_inline_attribute),
),
raw_inline_attribute: ($) =>
seq(token.immediate("{="), field("language", $.language), "}"),
math: ($) =>
seq(
field("math_marker", alias("$", $.math_marker)),
field("begin_marker", alias($._verbatim_begin, $.math_marker_begin)),
field("content", alias($._verbatim_content, $.content)),
field("end_marker", alias($._verbatim_end, $.math_marker_end)),
),
verbatim: ($) =>
seq(
field(
"begin_marker",
alias($._verbatim_begin, $.verbatim_marker_begin),
),
field("content", alias($._verbatim_content, $.content)),
field("end_marker", alias($._verbatim_end, $.verbatim_marker_end)),
),
_todo_highlights: ($) => choice($.todo, $.note, $.fixme),
todo: (_) => choice("TODO", "WIP"),
note: (_) => choice("NOTE", "INFO", "XXX"),
fixme: (_) => "FIXME",
// These exists to explicit trigger an LR collision with existing
// prefixes. A collision isn't detected with a string and the
// catch-all `_text` regex.
//
// Don't use dynamic precedence on the fallback, instead use it
// on span end tokens to prevent these branches from getting pruned
// when the tree grows large.
//
// Block level collisions handled by the scanner scanning ahead.
_symbol_fallback: ($) =>
choice(
// Standalone emphasis and strong markers are required for backtracking
"_",
"*",
// Whitespace sensitive
seq(
choice("{_", seq("_", $._non_whitespace_check)),
choice($._emphasis_mark_begin, $._in_fallback),
),
seq(
choice("{*", seq("*", $._non_whitespace_check)),
choice($._strong_mark_begin, $._in_fallback),
),
// Not sensitive to whitespace
seq(
choice("{^", "^"),
choice($._superscript_mark_begin, $._in_fallback),
),
seq(choice("{~", "~"), choice($._subscript_mark_begin, $._in_fallback)),
// Only bracketed versions
seq("{=", choice($._highlighted_mark_begin, $._in_fallback)),
seq("{+", choice($._insert_mark_begin, $._in_fallback)),
seq("{-", choice($._delete_mark_begin, $._in_fallback)),
// Bracketed spans
seq("[^", choice($._square_bracket_span_mark_begin, $._in_fallback)),
seq("![", choice($._square_bracket_span_mark_begin, $._in_fallback)),
seq("[", choice($._square_bracket_span_mark_begin, $._in_fallback)),
seq("(", choice($._parens_span_mark_begin, $._in_fallback)),
// Autolink
"<",
seq("<", /[^>\s]+/),
// Math
"$",
// Empty link text
"[]",
),
// Used to branch on inline attributes that may follow any element.
_curly_bracket_span_fallback: ($) =>
seq("{", choice($._curly_bracket_span_mark_begin, $._in_fallback)),
// It's a bit faster with repeat1 here.
_text: (_) => repeat1(/\S/),
},
externals: ($) => [
// Used as default value in scanner, should never be referenced.
$._ignored,
// Token to implicitly terminate open blocks,
// for instance in this case:
//