@konomanoasa/tree-sitter-yaml 0.3.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/grammar.js CHANGED
@@ -91,45 +91,53 @@ const structuredProblems = [
91
91
  {
92
92
  name: "unexpected_document_content",
93
93
  reason: "unexpected_document_content",
94
- start: "_unexpected_content_start",
94
+ start: [
95
+ "_unexpected_document_content_start",
96
+ "_unfinished_unexpected_document_content_start",
97
+ ],
95
98
  body: ($) => seq(field("content", $._node), $._document_content_end),
96
99
  },
100
+ extraContent("unexpected_collection_content", "_node"),
101
+ extraContent("unexpected_sequence_content", "_sequence_node"),
102
+ extraContent("unexpected_shared_content", "_shared_node"),
97
103
  {
98
104
  name: "invalid_document_collection",
99
105
  reason: "invalid_compact_collection",
100
- start: "_document_compact_start",
106
+ start: [
107
+ "_document_compact_start",
108
+ "_unfinished_invalid_document_collection_start",
109
+ ],
101
110
  body: ($) => field("content", $._node),
102
111
  },
103
112
  {
104
113
  name: "duplicate_anchor",
105
114
  reason: "duplicate_anchor",
106
- start: "_duplicate_anchor_start",
115
+ start: ["_duplicate_anchor_start", "_unfinished_duplicate_anchor_start"],
107
116
  body: ($) => field("property", $.anchor),
108
117
  },
109
118
  {
110
119
  name: "duplicate_tag",
111
120
  reason: "duplicate_tag",
112
- start: "_duplicate_tag_start",
121
+ start: ["_duplicate_tag_start", "_unfinished_duplicate_tag_start"],
113
122
  body: ($) => field("property", $._tag),
114
123
  },
115
124
  {
116
125
  name: "properties_on_alias",
117
126
  reason: "properties_on_alias",
118
- start: "_property_alias_start",
127
+ start: ["_property_alias_start", "_unfinished_properties_on_alias_start"],
119
128
  body: ($) => field("content", $.alias),
120
129
  },
121
- {
122
- name: "invalid_property_collection",
123
- reason: "invalid_compact_collection",
124
- start: "_property_compact_start",
125
- body: ($) => field("content", $._node_content),
126
- },
127
- {
128
- name: "invalid_compact_collection",
129
- reason: "invalid_compact_collection",
130
- start: "_invalid_compact_start",
131
- body: ($) => field("value", $._node),
132
- },
130
+ propertyCollection("invalid_property_collection", "_node_content"),
131
+ propertyCollection(
132
+ "invalid_sequence_property_collection",
133
+ "_sequence_node_content",
134
+ ),
135
+ propertyCollection(
136
+ "invalid_shared_property_collection",
137
+ "_shared_node_content",
138
+ ),
139
+ compactCollection("invalid_compact_collection", "_node"),
140
+ compactCollection("invalid_shared_compact_collection", "_shared_node"),
133
141
  {
134
142
  name: "invalid_implicit_key",
135
143
  reason: "invalid_implicit_key",
@@ -138,56 +146,9 @@ const structuredProblems = [
138
146
  },
139
147
  ];
140
148
 
141
- const problemRules = Object.fromEntries(
142
- problems.flatMap(([name, outcome, reason, leaf]) => [
143
- ...(leaf
144
- ? [[`_${name}_reason`, ($) => alias($[`_${name}`], $[leaf])]]
145
- : []),
146
- [
147
- `_${name}_outcome`,
148
- ($) => alias($[leaf ? `_${name}_reason` : `_${name}`], $[reason]),
149
- ],
150
- [`_${name}_issue`, ($) => alias($[`_${name}_outcome`], $[outcome])],
151
- ]),
152
- );
153
-
154
- for (const { name, reason, start, end, body } of structuredProblems) {
155
- for (const unfinished of [false, true]) {
156
- const id = unfinished ? `unfinished_${name}` : name;
157
- problemRules[`_${id}_reason`] = ($) =>
158
- end
159
- ? seq(body($), $[end[Number(unfinished)]])
160
- : seq($[unfinished ? `_${id}_start` : start], body($));
161
- problemRules[`_${id}_outcome`] = ($) =>
162
- alias($[`_${id}_reason`], $[reason]);
163
- problemRules[`_${id}_issue`] = ($) =>
164
- alias(
165
- $[`_${id}_outcome`],
166
- unfinished ? $.incomplete_syntax : $.invalid_syntax,
167
- );
168
- }
169
- }
170
-
171
- const issue = ($, name) =>
172
- field("issue", alias($[`_${name}_issue`], $.syntax_issue));
173
-
174
- const missing = ($, name) =>
175
- choice(issue($, `missing_${name}`), issue($, `incomplete_${name}`));
176
-
177
- const closing = ($, name) =>
178
- choice(field("closing", $[name]), missing($, name));
179
-
180
- const invalid = ($) =>
181
- choice(issue($, "invalid_encoding"), issue($, "invalid_character"));
182
-
183
- const unfinishedIssue = ($, name) =>
184
- choice(issue($, name), issue($, `unfinished_${name}`));
185
-
186
- const indentationIssue = ($) =>
187
- choice(
188
- unfinishedIssue($, "invalid_indentation"),
189
- unfinishedIssue($, "tab_in_indentation"),
190
- );
149
+ const unfinishedStarts = [
150
+ ...new Set(structuredProblems.flatMap(({ start = [] }) => start.slice(1))),
151
+ ];
191
152
 
192
153
  export default grammar({
193
154
  name: "yaml",
@@ -205,12 +166,18 @@ export default grammar({
205
166
  $._restore_indent,
206
167
  $._mapping_start,
207
168
  $._mapping_end,
169
+ $._mapping_in_mapping_end,
170
+ $._mapping_in_sequence_end,
171
+ $._mapping_in_shared_end,
208
172
  $._pair_start,
209
173
  $._sequence_start,
210
174
  $._sequence_end,
175
+ $._sequence_in_mapping_end,
176
+ $._sequence_in_sequence_end,
177
+ $._sequence_in_shared_end,
211
178
  $._entry_start,
212
- $._value_start,
213
- $._sequence_value_start,
179
+ $._block_out_value_start,
180
+ $._block_in_value_start,
214
181
  $._empty_block_node,
215
182
  $._explicit_value_start,
216
183
  $._invalid_compact_start,
@@ -272,7 +239,9 @@ export default grammar({
272
239
  $._boundary_check,
273
240
  $._document_content_end,
274
241
  $._document_compact_start,
275
- $._unexpected_content_start,
242
+ $._unexpected_document_content_start,
243
+ $._unexpected_collection_content_start,
244
+ $._collection_content_end,
276
245
  $._properties_start,
277
246
  $._properties_end,
278
247
  $._block_properties_end,
@@ -320,9 +289,7 @@ export default grammar({
320
289
  $._tag_prefix_start,
321
290
  $._tag_prefix_end,
322
291
  $.directive_parameter,
323
- ...structuredProblems
324
- .filter(({ start }) => start)
325
- .map(({ name }) => $[`_unfinished_${name}_start`]),
292
+ ...unfinishedStarts.map((name) => $[name]),
326
293
  ...problems.map(([name]) => $[`_${name}`]),
327
294
  $._undecodable_escape_prefix,
328
295
  $._error_sentinel,
@@ -359,82 +326,111 @@ export default grammar({
359
326
  repeat1(
360
327
  choice($.yaml_directive, $.tag_directive, $.reserved_directive),
361
328
  ),
362
- choice($.document_start, missing($, "document_start")),
363
- ),
364
- optional(choice($.document_start, missing($, "document_start"))),
365
- ),
366
- optional(
367
- seq(
368
- choice(
369
- field("content", $._node),
370
- unfinishedIssue($, "invalid_document_collection"),
371
- ),
372
- $._document_content_end,
329
+ $._document_start,
373
330
  ),
331
+ optional($._document_start),
374
332
  ),
333
+ optional($._document_content),
375
334
  repeat(unfinishedIssue($, "unexpected_document_content")),
376
335
  $._document_close,
377
336
  ),
378
- yaml_directive: ($) =>
337
+ _document_start: ($) =>
338
+ choice($.document_start, missing($, "document_start")),
339
+ _document_content: ($) =>
379
340
  seq(
380
- directiveHead($, $._yaml_directive_start),
381
- repeat(invalid($)),
382
341
  choice(
383
- field("version", $.yaml_version),
384
- missing($, "yaml_version"),
385
- issue($, "invalid_yaml_version"),
386
- issue($, "unfinished_yaml_version"),
342
+ field("content", $._node),
343
+ unfinishedIssue($, "invalid_document_collection"),
387
344
  ),
388
- directiveTail($),
345
+ $._document_content_end,
346
+ ),
347
+ yaml_directive: ($) =>
348
+ seq(
349
+ $._yaml_directive_start,
350
+ $._directive_head,
351
+ repeat(invalid($)),
352
+ $._yaml_directive_version,
353
+ $._directive_tail,
354
+ ),
355
+ _yaml_directive_version: ($) =>
356
+ choice(
357
+ field("version", $.yaml_version),
358
+ missing($, "yaml_version"),
359
+ issue($, "invalid_yaml_version"),
360
+ issue($, "unfinished_yaml_version"),
389
361
  ),
390
362
  tag_directive: ($) =>
391
363
  seq(
392
- directiveHead($, $._tag_directive_start),
364
+ $._tag_directive_start,
365
+ $._directive_head,
393
366
  repeat(invalid($)),
394
- choice(
395
- seq(
396
- $._tag_handle_start,
397
- field(
398
- "handle",
399
- choice(
400
- $.primary_tag_handle,
401
- $.secondary_tag_handle,
402
- alias($._directive_named_handle, $.named_tag_handle),
403
- ),
367
+ $._tag_directive_handle,
368
+ optional(issue($, "missing_separation")),
369
+ $._tag_directive_prefix,
370
+ $._directive_tail,
371
+ ),
372
+ _tag_directive_handle: ($) =>
373
+ choice(
374
+ seq(
375
+ $._tag_handle_start,
376
+ field(
377
+ "handle",
378
+ choice(
379
+ $.primary_tag_handle,
380
+ $.secondary_tag_handle,
381
+ alias($._directive_named_handle, $.named_tag_handle),
404
382
  ),
405
383
  ),
406
- missing($, "tag_handle"),
407
- issue($, "invalid_tag_handle"),
408
384
  ),
409
- optional(issue($, "missing_separation")),
410
- choice(field("prefix", $.tag_prefix), missing($, "tag_prefix")),
411
- directiveTail($),
385
+ missing($, "tag_handle"),
386
+ issue($, "invalid_tag_handle"),
412
387
  ),
388
+ _tag_directive_prefix: ($) =>
389
+ choice(field("prefix", $.tag_prefix), missing($, "tag_prefix")),
413
390
  reserved_directive: ($) =>
414
391
  seq(
415
- directiveHead($, $._reserved_directive_start),
392
+ $._reserved_directive_start,
393
+ $._directive_head,
416
394
  repeat(choice(field("parameter", $.directive_parameter), invalid($))),
417
395
  $._directive_end,
418
396
  ),
397
+ _directive_head: ($) =>
398
+ seq(
399
+ field("indicator", $.directive_indicator),
400
+ names($, "directive_name", $._directive_name_end),
401
+ ),
402
+ _directive_tail: ($) =>
403
+ seq(
404
+ repeat(choice(issue($, "unexpected_directive_parameter"), invalid($))),
405
+ $._directive_end,
406
+ ),
419
407
  _directive_named_handle: ($) =>
420
408
  namedTagHandle($, closing($, "tag_handle_close")),
421
409
  tag_prefix: ($) =>
422
410
  seq(
423
411
  $._tag_prefix_start,
424
- repeat1(uriPart($, $.uri_text, true)),
412
+ repeat1(uriPart($, $.uri_text, issue($, "invalid_tag_prefix_start"))),
425
413
  $._tag_prefix_end,
426
414
  ),
427
415
  _node: ($) => choice($._node_content, $.alias, $.node_with_properties),
428
416
  _node_content: ($) =>
417
+ nodeContent($, $._mapping_in_mapping, $._sequence_in_mapping),
418
+ _sequence_node: ($) =>
429
419
  choice(
430
- $._flow_content,
431
- $._indented_node,
432
- $._dedented_node,
433
- $.block_mapping,
434
- $.block_sequence,
435
- $.literal_scalar,
436
- $.folded_scalar,
420
+ $._sequence_node_content,
421
+ $.alias,
422
+ alias($._sequence_properties, $.node_with_properties),
423
+ ),
424
+ _sequence_node_content: ($) =>
425
+ nodeContent($, $._mapping_in_sequence, $._sequence_in_sequence),
426
+ _shared_node: ($) =>
427
+ choice(
428
+ $._shared_node_content,
429
+ $.alias,
430
+ alias($._shared_properties, $.node_with_properties),
437
431
  ),
432
+ _shared_node_content: ($) =>
433
+ nodeContent($, $._mapping_in_shared, $._sequence_in_shared),
438
434
  _flow_node: ($) =>
439
435
  choice(
440
436
  $._flow_content,
@@ -450,38 +446,41 @@ export default grammar({
450
446
  $.flow_mapping,
451
447
  ),
452
448
  node_with_properties: ($) =>
453
- propertyNode(
449
+ blockProperties($, $._node_content, "invalid_property_collection"),
450
+ _sequence_properties: ($) =>
451
+ blockProperties(
452
+ $,
453
+ $._sequence_node_content,
454
+ "invalid_sequence_property_collection",
455
+ ),
456
+ _shared_properties: ($) =>
457
+ blockProperties(
454
458
  $,
455
- seq($._block_property_content_start, field("content", $._node_content)),
456
- true,
459
+ $._shared_node_content,
460
+ "invalid_shared_property_collection",
457
461
  ),
458
462
  _flow_properties: ($) =>
459
463
  propertyNode(
460
464
  $,
465
+ $._properties_end,
461
466
  choice(
462
467
  seq($._property_content_start, field("content", $._flow_content)),
463
468
  seq($._block_key_content_start, field("content", $._node_content)),
464
469
  ),
470
+ "invalid_property_collection",
465
471
  ),
472
+ _property_list: ($) => propertyList($),
466
473
  _tag: ($) => choice($.non_specific_tag, $.shorthand_tag, $.verbatim_tag),
467
474
  anchor: ($) =>
468
475
  seq(
469
476
  field("indicator", $.anchor_indicator),
470
- choice(
471
- repeat1(choice(field("name", $.anchor_name), invalid($))),
472
- missing($, "anchor_name"),
473
- ),
474
- $._name_end,
477
+ names($, "anchor_name", $._name_end),
475
478
  ),
476
479
  alias: ($) =>
477
480
  seq(
478
481
  $._alias_start,
479
482
  field("indicator", $.alias_indicator),
480
- choice(
481
- repeat1(choice(field("name", $.alias_name), invalid($))),
482
- missing($, "alias_name"),
483
- ),
484
- $._name_end,
483
+ names($, "alias_name", $._name_end),
485
484
  ),
486
485
  shorthand_tag: ($) =>
487
486
  seq(
@@ -523,57 +522,76 @@ export default grammar({
523
522
  choice($._dedented_node, $.block_mapping, $.block_sequence),
524
523
  $._restore_indent,
525
524
  ),
526
- block_mapping: ($) =>
527
- seq($._mapping_start, repeat1($.block_mapping_pair), $._mapping_end),
525
+ block_mapping: ($) => blockMapping($, $._mapping_pairs, $._mapping_end),
526
+ _mapping_in_mapping: ($) =>
527
+ blockMapping($, $._mapping_pairs, $._mapping_in_mapping_end),
528
+ _mapping_in_sequence: ($) =>
529
+ blockMapping($, $._shared_pairs, $._mapping_in_sequence_end),
530
+ _mapping_in_shared: ($) =>
531
+ blockMapping($, $._shared_pairs, $._mapping_in_shared_end),
532
+ _mapping_layout: ($) =>
533
+ choice(
534
+ alias($._mapping_prefix, $.line_prefix),
535
+ alias($._mapping_comment, $.comment),
536
+ ),
537
+ _mapping_pairs: ($) =>
538
+ mappingPairs(
539
+ $,
540
+ $._mapping_pairs,
541
+ $.block_mapping_pair,
542
+ "unexpected_collection_content",
543
+ ),
544
+ _shared_pairs: ($) =>
545
+ mappingPairs(
546
+ $,
547
+ $._shared_pairs,
548
+ alias($._shared_pair, $.block_mapping_pair),
549
+ "unexpected_shared_content",
550
+ ),
528
551
  block_mapping_pair: ($) =>
529
- seq(
530
- $._pair_start,
531
- choice(
532
- seq(
533
- field("key_indicator", $.key_indicator),
534
- choice(
535
- seq($._value_start, field("key", $._node)),
536
- $._empty_block_node,
537
- ),
538
- choice(
539
- seq(
540
- $._explicit_value_start,
541
- field("indicator", $.value_indicator),
542
- choice(
543
- seq($._value_start, field("value", $._node)),
544
- $._empty_block_node,
545
- ),
546
- ),
547
- $._empty_block_node,
548
- ),
549
- ),
550
- seq(
551
- optional($._implicit_key),
552
- choice(
553
- seq(
554
- field("indicator", $.value_indicator),
555
- choice(
556
- seq($._value_start, field("value", $._node)),
557
- unfinishedIssue($, "invalid_compact_collection"),
558
- $._empty_block_node,
559
- ),
560
- ),
561
- missing($, "value_indicator"),
562
- ),
563
- ),
564
- ),
552
+ mappingPair(
553
+ $,
554
+ $._block_out_value_start,
555
+ $._node,
556
+ "invalid_compact_collection",
557
+ ),
558
+ _shared_pair: ($) =>
559
+ mappingPair(
560
+ $,
561
+ $._block_in_value_start,
562
+ $._shared_node,
563
+ "invalid_shared_compact_collection",
565
564
  ),
566
565
  block_sequence: ($) =>
567
- seq($._sequence_start, repeat1($.block_sequence_entry), $._sequence_end),
568
- block_sequence_entry: ($) =>
569
- seq(
570
- $._entry_start,
571
- field("indicator", $.sequence_indicator),
572
- choice(
573
- seq($._sequence_value_start, field("value", $._node)),
574
- $._empty_block_node,
575
- ),
566
+ blockSequence(
567
+ $,
568
+ $.block_sequence_entry,
569
+ "unexpected_sequence_content",
570
+ $._sequence_end,
571
+ ),
572
+ _sequence_in_mapping: ($) =>
573
+ blockSequence(
574
+ $,
575
+ alias($._shared_entry, $.block_sequence_entry),
576
+ "unexpected_shared_content",
577
+ $._sequence_in_mapping_end,
578
+ ),
579
+ _sequence_in_sequence: ($) =>
580
+ blockSequence(
581
+ $,
582
+ $.block_sequence_entry,
583
+ "unexpected_sequence_content",
584
+ $._sequence_in_sequence_end,
585
+ ),
586
+ _sequence_in_shared: ($) =>
587
+ blockSequence(
588
+ $,
589
+ alias($._shared_entry, $.block_sequence_entry),
590
+ "unexpected_shared_content",
591
+ $._sequence_in_shared_end,
576
592
  ),
593
+ block_sequence_entry: ($) => blockSequenceEntry($, $._sequence_node),
594
+ _shared_entry: ($) => blockSequenceEntry($, $._shared_node),
577
595
  literal_scalar: ($) => blockScalar($, $._literal_start),
578
596
  folded_scalar: ($) => blockScalar($, $._folded_start),
579
597
  block_scalar_header: ($) =>
@@ -626,7 +644,6 @@ export default grammar({
626
644
  $.scalar_line_prefix,
627
645
  $.scalar_line_suffix,
628
646
  indentationIssue($),
629
- unfinishedIssue($, "missing_indentation"),
630
647
  invalid($),
631
648
  issue($, "invalid_scalar_start"),
632
649
  issue($, "incomplete_scalar_start"),
@@ -653,25 +670,28 @@ export default grammar({
653
670
  ),
654
671
  ),
655
672
  flow_sequence: ($) =>
656
- seq(
673
+ flowCollection(
674
+ $,
657
675
  $._flow_sequence_start,
658
- field("opening", $.flow_sequence_open),
659
- flowContent(
660
- $,
661
- choice(
662
- seq($._implicit_key_start, $._flow_node, $._flow_node_end),
663
- alias($._flow_sequence_pair, $.flow_mapping_pair),
664
- ),
665
- ),
666
- closing($, "flow_sequence_close"),
676
+ $.flow_sequence_open,
677
+ $._flow_sequence_entries,
678
+ "flow_sequence_close",
679
+ ),
680
+ _flow_sequence_entries: ($) => flowEntries($, $._flow_sequence_entry),
681
+ _flow_sequence_entry: ($) =>
682
+ choice(
683
+ seq($._implicit_key_start, $._flow_node, $._flow_node_end),
684
+ alias($._flow_sequence_pair, $.flow_mapping_pair),
667
685
  ),
668
686
  flow_mapping: ($) =>
669
- seq(
687
+ flowCollection(
688
+ $,
670
689
  $._flow_mapping_start,
671
- field("opening", $.flow_mapping_open),
672
- flowContent($, $.flow_mapping_pair),
673
- closing($, "flow_mapping_close"),
690
+ $.flow_mapping_open,
691
+ $._flow_mapping_entries,
692
+ "flow_mapping_close",
674
693
  ),
694
+ _flow_mapping_entries: ($) => flowEntries($, $.flow_mapping_pair),
675
695
  flow_mapping_pair: ($) => seq($._flow_map_pair_start, $._flow_pair_body),
676
696
  _flow_sequence_pair: ($) =>
677
697
  choice(
@@ -689,52 +709,138 @@ export default grammar({
689
709
  seq(field("key", $._flow_node), optional(flowValue($))),
690
710
  flowValue($),
691
711
  ),
692
- comment: ($) =>
693
- seq(
694
- $._comment_start,
695
- optional(issue($, "missing_separation")),
696
- $.comment_marker,
697
- repeat(choice($.comment_text, invalid($))),
698
- $._comment_end,
699
- ),
700
- line_prefix: ($) =>
701
- seq(
702
- $._line_prefix_start,
703
- repeat1(
704
- choice(
705
- $.indentation,
706
- $.separation,
707
- indentationIssue($),
708
- unfinishedIssue($, "missing_indentation"),
709
- ),
710
- ),
711
- $._line_prefix_end,
712
- ),
712
+ comment,
713
+ line_prefix: linePrefix,
714
+ _mapping_comment: comment,
715
+ _mapping_prefix: linePrefix,
713
716
  // Prevents lexer fallback from accepting EOF mid-input.
714
717
  _unmatchable: () => token(seq(/[\s\S]/, /[^\s\S]/)),
715
- ...problemRules,
718
+ ...issueRules(),
716
719
  },
717
720
  });
718
721
 
719
- function flowContent($, entry) {
720
- const unexpectedSeparators = repeat(issue($, "unexpected_flow_separator"));
722
+ function issueRules() {
723
+ const rules = Object.fromEntries(
724
+ problems.flatMap(([name, outcome, reason, leaf]) => [
725
+ ...(leaf
726
+ ? [[`_${name}_reason`, ($) => alias($[`_${name}`], $[leaf])]]
727
+ : []),
728
+ [
729
+ `_${name}_outcome`,
730
+ ($) => alias($[leaf ? `_${name}_reason` : `_${name}`], $[reason]),
731
+ ],
732
+ [`_${name}_issue`, ($) => alias($[`_${name}_outcome`], $[outcome])],
733
+ ]),
734
+ );
735
+ for (const { name, reason, start, end, body } of structuredProblems) {
736
+ for (const unfinished of [false, true]) {
737
+ const id = unfinished ? `unfinished_${name}` : name;
738
+ rules[`_${id}_reason`] = ($) =>
739
+ end
740
+ ? seq(body($), $[end[Number(unfinished)]])
741
+ : seq($[start[Number(unfinished)]], body($));
742
+ rules[`_${id}_outcome`] = ($) => alias($[`_${id}_reason`], $[reason]);
743
+ rules[`_${id}_issue`] = ($) =>
744
+ alias(
745
+ $[`_${id}_outcome`],
746
+ unfinished ? $.incomplete_syntax : $.invalid_syntax,
747
+ );
748
+ }
749
+ }
750
+ return rules;
751
+ }
752
+
753
+ function extraContent(name, node) {
754
+ return {
755
+ name,
756
+ reason: "unexpected_collection_content",
757
+ start: [
758
+ "_unexpected_collection_content_start",
759
+ "_unfinished_unexpected_collection_content_start",
760
+ ],
761
+ body: ($) => seq(field("content", $[node]), $._collection_content_end),
762
+ };
763
+ }
764
+
765
+ function propertyCollection(name, content) {
766
+ return {
767
+ name,
768
+ reason: "invalid_compact_collection",
769
+ start: [
770
+ "_property_compact_start",
771
+ "_unfinished_invalid_property_collection_start",
772
+ ],
773
+ body: ($) => field("content", $[content]),
774
+ };
775
+ }
776
+
777
+ function compactCollection(name, node) {
778
+ return {
779
+ name,
780
+ reason: "invalid_compact_collection",
781
+ start: [
782
+ "_invalid_compact_start",
783
+ "_unfinished_invalid_compact_collection_start",
784
+ ],
785
+ body: ($) => field("value", $[node]),
786
+ };
787
+ }
788
+
789
+ function issue($, name) {
790
+ return field("issue", alias($[`_${name}_issue`], $.syntax_issue));
791
+ }
792
+
793
+ function missing($, name) {
794
+ return choice(issue($, `missing_${name}`), issue($, `incomplete_${name}`));
795
+ }
796
+
797
+ function closing($, name) {
798
+ return choice(field("closing", $[name]), missing($, name));
799
+ }
800
+
801
+ function invalid($) {
802
+ return choice(issue($, "invalid_encoding"), issue($, "invalid_character"));
803
+ }
804
+
805
+ function unfinishedIssue($, name) {
806
+ return choice(issue($, name), issue($, `unfinished_${name}`));
807
+ }
808
+
809
+ function indentationIssue($) {
810
+ return choice(
811
+ unfinishedIssue($, "invalid_indentation"),
812
+ unfinishedIssue($, "tab_in_indentation"),
813
+ unfinishedIssue($, "missing_indentation"),
814
+ );
815
+ }
816
+
817
+ function flowCollection($, start, opening, entries, closingName) {
721
818
  return seq(
722
- unexpectedSeparators,
723
- optional(
724
- seq(
725
- entry,
726
- repeat(
727
- seq(
728
- choice(
729
- $.flow_separator,
730
- unfinishedIssue($, "missing_flow_separator"),
731
- ),
732
- unexpectedSeparators,
733
- entry,
819
+ start,
820
+ field("opening", opening),
821
+ optional(entries),
822
+ closing($, closingName),
823
+ );
824
+ }
825
+
826
+ function flowEntries($, entry) {
827
+ const unexpectedSeparator = issue($, "unexpected_flow_separator");
828
+ return choice(
829
+ repeat1(unexpectedSeparator),
830
+ seq(
831
+ repeat(unexpectedSeparator),
832
+ entry,
833
+ repeat(
834
+ seq(
835
+ choice(
836
+ $.flow_separator,
837
+ unfinishedIssue($, "missing_flow_separator"),
734
838
  ),
839
+ repeat(unexpectedSeparator),
840
+ entry,
735
841
  ),
736
- optional(seq($.flow_separator, unexpectedSeparators)),
737
842
  ),
843
+ optional(seq($.flow_separator, repeat(unexpectedSeparator))),
738
844
  ),
739
845
  );
740
846
  }
@@ -746,6 +852,7 @@ function explicitFlowPair($) {
746
852
  optional(flowValue($)),
747
853
  );
748
854
  }
855
+
749
856
  function flowValue($) {
750
857
  return seq(
751
858
  field("indicator", $.value_indicator),
@@ -754,6 +861,90 @@ function flowValue($) {
754
861
  );
755
862
  }
756
863
 
864
+ function blockMapping($, pairs, end) {
865
+ return seq($._mapping_start, pairs, repeat($._mapping_layout), end);
866
+ }
867
+
868
+ function mappingPairs($, pairs, pair, content) {
869
+ return choice(
870
+ pair,
871
+ seq(
872
+ pairs,
873
+ repeat($._mapping_layout),
874
+ choice(pair, unfinishedIssue($, content)),
875
+ ),
876
+ );
877
+ }
878
+
879
+ function mappingPair($, valueStart, node, compact) {
880
+ return seq(
881
+ $._pair_start,
882
+ choice(
883
+ seq(
884
+ field("key_indicator", $.key_indicator),
885
+ blockNode("key", valueStart, node, $._empty_block_node),
886
+ choice(
887
+ seq(
888
+ $._explicit_value_start,
889
+ field("indicator", $.value_indicator),
890
+ blockNode("value", valueStart, node, $._empty_block_node),
891
+ ),
892
+ $._empty_block_node,
893
+ ),
894
+ ),
895
+ seq(
896
+ optional($._implicit_key),
897
+ choice(
898
+ seq(
899
+ field("indicator", $.value_indicator),
900
+ blockNode(
901
+ "value",
902
+ valueStart,
903
+ node,
904
+ $._empty_block_node,
905
+ unfinishedIssue($, compact),
906
+ ),
907
+ ),
908
+ missing($, "value_indicator"),
909
+ ),
910
+ ),
911
+ ),
912
+ );
913
+ }
914
+
915
+ function blockSequence($, entry, content, end) {
916
+ return seq(
917
+ $._sequence_start,
918
+ entry,
919
+ repeat(choice(entry, unfinishedIssue($, content))),
920
+ end,
921
+ );
922
+ }
923
+
924
+ function nodeContent($, sameIndentMapping, sameIndentSequence) {
925
+ return choice(
926
+ $._flow_content,
927
+ $._indented_node,
928
+ $._dedented_node,
929
+ alias(sameIndentMapping, $.block_mapping),
930
+ alias(sameIndentSequence, $.block_sequence),
931
+ $.literal_scalar,
932
+ $.folded_scalar,
933
+ );
934
+ }
935
+
936
+ function blockSequenceEntry($, node) {
937
+ return seq(
938
+ $._entry_start,
939
+ field("indicator", $.sequence_indicator),
940
+ blockNode("value", $._block_in_value_start, node, $._empty_block_node),
941
+ );
942
+ }
943
+
944
+ function blockNode(name, start, node, empty, ...issues) {
945
+ return choice(seq(start, choice(field(name, node), ...issues)), empty);
946
+ }
947
+
757
948
  function blockScalar($, start) {
758
949
  return seq(
759
950
  start,
@@ -769,27 +960,48 @@ function blockScalar($, start) {
769
960
  $._block_scalar_end,
770
961
  );
771
962
  }
963
+
772
964
  function blockLine($, start) {
773
965
  return seq(
774
966
  start,
775
967
  optional(field("prefix", $.scalar_line_prefix)),
776
- repeat(
777
- choice(
778
- $.scalar_text,
779
- unfinishedIssue($, "missing_indentation"),
780
- indentationIssue($),
781
- invalid($),
782
- ),
783
- ),
968
+ repeat(choice($.scalar_text, indentationIssue($), invalid($))),
784
969
  optional($.scalar_line_break),
785
970
  $._block_line_end,
786
971
  );
787
972
  }
788
973
 
789
- function propertyNode($, content, block = false) {
974
+ function blockProperties($, content, compact) {
975
+ return propertyNode(
976
+ $,
977
+ $._block_properties_end,
978
+ seq($._block_property_content_start, field("content", content)),
979
+ compact,
980
+ );
981
+ }
982
+
983
+ function propertyNode($, end, content, compact) {
984
+ return seq(
985
+ $._property_list,
986
+ choice(
987
+ $._empty_properties_end,
988
+ seq(
989
+ end,
990
+ optional(issue($, "missing_separation")),
991
+ choice(
992
+ content,
993
+ unfinishedIssue($, "properties_on_alias"),
994
+ unfinishedIssue($, compact),
995
+ ),
996
+ ),
997
+ ),
998
+ );
999
+ }
1000
+
1001
+ function propertyList($) {
790
1002
  const duplicate = (name) => unfinishedIssue($, `duplicate_${name}`);
791
- const separation = issue($, "missing_separation");
792
- const next = (rule) => seq($._property_continue, optional(separation), rule);
1003
+ const next = (rule) =>
1004
+ seq($._property_continue, optional(issue($, "missing_separation")), rule);
793
1005
  const both = repeat(next(choice(duplicate("anchor"), duplicate("tag"))));
794
1006
  const ordered = (first, second, rule) =>
795
1007
  seq(
@@ -808,38 +1020,20 @@ function propertyNode($, content, block = false) {
808
1020
  ordered("anchor", "tag", $.anchor),
809
1021
  ordered("tag", "anchor", $._tag),
810
1022
  ),
811
- choice(
812
- $._empty_properties_end,
813
- seq(
814
- block ? $._block_properties_end : $._properties_end,
815
- optional(separation),
816
- choice(
817
- content,
818
- unfinishedIssue($, "properties_on_alias"),
819
- unfinishedIssue($, "invalid_property_collection"),
820
- ),
821
- ),
822
- ),
823
1023
  );
824
1024
  }
825
- function directiveHead($, start) {
1025
+
1026
+ function names($, name, end) {
826
1027
  return seq(
827
- start,
828
- field("indicator", $.directive_indicator),
829
1028
  choice(
830
- repeat1(choice(field("name", $.directive_name), invalid($))),
831
- missing($, "directive_name"),
1029
+ repeat1(choice(field("name", $[name]), invalid($))),
1030
+ missing($, name),
832
1031
  ),
833
- $._directive_name_end,
834
- );
835
- }
836
- function directiveTail($) {
837
- return seq(
838
- repeat(choice(issue($, "unexpected_directive_parameter"), invalid($))),
839
- $._directive_end,
1032
+ end,
840
1033
  );
841
1034
  }
842
- function uriPart($, text, prefix = false) {
1035
+
1036
+ function uriPart($, text, ...issues) {
843
1037
  return choice(
844
1038
  text,
845
1039
  $.uri_escape,
@@ -847,7 +1041,7 @@ function uriPart($, text, prefix = false) {
847
1041
  issue($, "incomplete_uri_escape"),
848
1042
  seq($._undecodable_escape_prefix, issue($, "invalid_encoding")),
849
1043
  issue($, "invalid_tag_character"),
850
- ...(prefix ? [issue($, "invalid_tag_prefix_start")] : []),
1044
+ ...issues,
851
1045
  invalid($),
852
1046
  );
853
1047
  }
@@ -877,7 +1071,6 @@ function quotedScalar($, start, opening, escapes) {
877
1071
  $.scalar_line_prefix,
878
1072
  $.scalar_line_suffix,
879
1073
  indentationIssue($),
880
- unfinishedIssue($, "missing_indentation"),
881
1074
  invalid($),
882
1075
  ...escapes,
883
1076
  ),
@@ -885,3 +1078,21 @@ function quotedScalar($, start, opening, escapes) {
885
1078
  closing($, "quote_close"),
886
1079
  );
887
1080
  }
1081
+
1082
+ function comment($) {
1083
+ return seq(
1084
+ $._comment_start,
1085
+ optional(issue($, "missing_separation")),
1086
+ $.comment_marker,
1087
+ repeat(choice($.comment_text, invalid($))),
1088
+ $._comment_end,
1089
+ );
1090
+ }
1091
+
1092
+ function linePrefix($) {
1093
+ return seq(
1094
+ $._line_prefix_start,
1095
+ repeat1(choice($.indentation, $.separation, indentationIssue($))),
1096
+ $._line_prefix_end,
1097
+ );
1098
+ }