-
Notifications
You must be signed in to change notification settings - Fork 12
/
yaml-spec-1.2.yaml
3357 lines (2401 loc) · 60.5 KB
/
yaml-spec-1.2.yaml
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
#===============================================================================
#
# This YAML grammar was generated from https://yaml.org/spec/1.2/spec.html by:
# https://github.com/yaml/yaml-grammar/tree/master/bin/yaml-grammar-html-to-yaml
#
# This grammar is a YAML file that should be loadable by any decent existing
# YAML loader. It has also been converted to JSON in a file next to this one.
# The YAML file has lots of helpful comments. The JSON (of course) does not.
#
# The purpose of this grammar-as-yaml file is to make the spec clearer and more
# precise to people implementing YAML parsers. The grammar in the YAML 1.2
# specification is difficult to understand and in a few cases ambiguous or
# incorrect.
#
# This grammar mirrors the 1.2 spec faithfully, but tries to express the rules
# in a more concrete and programatic fashion. Some of the rules that are more
# difficult to understand, ambiguous or incorrect, have a comment section
# before them. Each rule definition is preceded by the original YAML 1.2 spec
# rule text, formatted as a comment.
#
# The YAML Reference Parser project ( which can be found at
# https://github.com/yaml/yaml-reference-parser/ ) uses this file to generate
# 100% correct YAML parsers in many programming languages.
#
#===============================================================================
#===============================================================================
#
# = Syntax Guide =
#
# The following is an explanation of the various EBNF/DSL syntax forms used in
# this grammar. The original YAML 1.2 spec uses an EBNF form that has more than
# a couple oddities. This file tries to make as much sense of them as possible.
# Each rule that introduces a new syntax is preceded by an explanation.
#
# There are 211 rules in the YAML 1.2 spec. This grammar file has 211 sections,
# one for each rule. The overall structure of this file is a big mapping with
# 2 mapping pairs for each grammar rule. Each rule group looks like this:
#
# :rule-number: rule-name
# # original-spec-rule-as-a-comment
#
# rule-name: rule-definition
#
# The first pair is just for indexing the rule (by rule number) back to the 1.2
# spec. The rule definition is the important part. Complex definitions are data
# structures mostly comprised of other rule-names. The simplest rules point to
# single characters. This forms a top-down grammar where the top is the last
# rule, #211.
#
#
# == Rule Names ==
#
# A rule name consists of groups of lower case characters and numbers separated
# by dashes (`-`).
#
# Each rule name has a 1 or 2 character prefix indicating the rule type.
#
# Example rule names:
#
# * nb-foo-bar
# * nb+foo-bar
# * nb-foo-bar(n)
# * nb-foo-bar(n,c)
#
# === Rule Name Prefixes ===
#
# The rule type prefixes are:
#
# e -- Empty (matches no characters)
# c -- Character (match one single character)
# b -- Break (match a single line break character)
# nb -- Non-break (match a single non-break character)
# s -- Space (match a single whitespace character)
# ns -- Non-space (match a single non-space character)
# l -- Line (match a complete line)
#
# And also (where X and Y are each one of the above prefixes):
#
# X-Y -- Match starting with an X- match and ending with a Y- match
# X+ -- Match X where indentation is greater than n
# X-Y+ -- Match X-Y where indentation is greater than n
#
#
# == Rules Variables ==
#
# Some rules are defined with variable arguments indicating that they should be
# passed values when being used. There are 4 distinct variables used in the
# YAML 1.2 grammar.
#
# n -- Current indentation level. An integer indicating the number of spaces.
#
# m -- Additional indentation level. An integer indicating number of spaces.
#
# c -- Current context. String that is one of:
# * "block-in"
# * "block-out"
# * "block-key"
# * "flow-in"
# * "flow-out"
# * "flow-key"
#
# t -- How to treat whitespace after a literal scalar. One of:
# * "strip"
# * "clip"
# * "keep"
#
# Where and how these variables are set is a bit inconsistent. Some are set by
# explicitly in rules that act like function calls. For example, The starting
# values of n (-1) and c ("block-in") are set in rule 207.
#
# The m variable is set explicitly in rules 183 and 187, albeit by an undefined
# special rule called <auto-detect-indent>. In rules like 185 it assumes that m
# is stored as a state/stack variable and has been set somewhere else.
#
# The t variable is set using functional constructs in rule 164, depending on
# the value of a c-chomping-indicator rule.
#
#
# == Special Rule Names ==
#
# A handful of rule names are words inside angle brackets. These are called
# "special rules". They are not defined in the grammar. Each of these rules is
# an assertion that doesn't consume any text in the parse. They are:
#
# * <empty>
# Always matches.
#
# * <start-of-line>
# Current parse position is at the start of a line.
#
# * <end-of-stream>
# Current parse position is at the end of the input stream.
#
# * <auto-detect-indent>
# Detect the number of characters of new indentation.
#
#
# == Rule Definitions ==
#
# The rule definitions here are expressed as YAML data structures where the
# scalar values can be:
#
# * A rule name
# * A literal character (always single quoted for clarity)
# * Some special DSL forms
#
# The various DSL forms were chosen carefully to be easily read and understood
# by the people reading it. The rest of this section describes those forms.
#
# === Special Syntax Used ===
#
# There are some syntax conventions used for clarity. Everything is valid YAML.
# Certain things could be expressed in YAML different ways. For instance a
# single character to be matched could be unquoted, single-quoted or
# double-quoted. If the intent is to match a single character, it is single
# quoted.
#
# * unquoted-word -- Usually a rule name
# * unquoted-single-char -- A variable name
# * single-quoted-char -- A character to match
# * double-quoted-string -- A literal string value
# * (...) -- A parsing function
# * <...> -- A special rule name
#
# === Hex Codes ===
#
# Hex codes are scalars beginning with an `x` followed by an even number of
# uppercase hexadecimal digits. Since hex codes represent characters they are
# encoded in single quotes.
#
# There are hex ranges as well, which consist of a pair hex codes.
#
# === Rule Functions ===
#
# The grammar uses these functions. The function name is always in parentheses.
# The name is a key of a single pair mapping.
#
# * (any)
# One of the rules in the group must match.
#
# * (all)
# All of the rules in the group must match.
#
# * (+++)
# The rule must match 1 or more times.
#
# * (***)
# The rule must match 0 or more times.
#
# * (???)
# The rule must match 0 or 1 times.
#
# * ({x})
# The rule must match x times, where x is an integer or a variable.
#
# * (===)
# Positive look-ahead assertion.
#
# * (!==)
# Negative look-ahead assertion.
#
# * (<==)
# Positive look-behind assertion.
#
# * (---)
# A set of characters comprised of the first set minus each of the others.
#
# * (...)
# The argument variables that are passed to this rule. Either a single
# value or a sequence of values.
#
# * (case)
# A variable indicated by `var` must match one of the other keys. The value
# of the matching key pair is the next rule to be checked.
#
# * (flip)
# Some grammar rules like 136, don't attempt to match anything. They are
# functions that change a variable. Rule 136 uses the (flip) function to
# change a variable from one value to another.
#
# * (if)
# Check the rule indicated by the value. If it matches, call the (set)
# function that must be provided.
#
# * (set)
# The value is a 2 element sequence. The first element is a variable and
# the second is an expression. Set the variable to the value of the
# evaluated expression.
#
# * (ord)
# Convert a character (1-9) to an integer.
#
# * (match)
# Return the string or character value of the last rule match.
#
# * (len)
# Return the length of a string value.
#
# * (max)
# The YAML spec grammar constrains some rules to complete in the next 1024
# characters. The (max) function is used to indicate that this is the
# case. How it is implemented (like many things) is left as an exercise for
# the parser author.
#
# * (exclude)
# This one is a doozy. It indicates a rule that must not match during any
# of the recursive sub-matches. Since it is used near the top of the
# grammar, it affects almost everything.
#
# * (+)
# Return the first argument plus the second.
#
# * (-)
# Return the first argument minus the second.
#
# * (<)
# First argument is less than the second.
#
# * (<=)
# First argument is less than the second.
#
#===============================================================================
### 5.1. Character Set
#===============================================================================
# This is the complete set of characters allowed in a YAML stream.
#
# The (any) means that any one of the value group's rules must match for the
# group to match. The remaining rules are attempted in order.
#
# The rules beginning with `x` are hex codes. The pairs of hex codes are
# character ranges.
#===============================================================================
:001: c-printable
# c-printable ::=
# x:9 | x:A | x:D | [x:20-x:7E]
# | x:85 | [x:A0-x:D7FF] | [x:E000-x:FFFD]
# | [x:10000-x:10FFFF]
c-printable:
(any):
- 'x09'
- 'x0A'
- 'x0D'
- [ 'x20', 'x7E' ]
- 'x85'
- [ 'xA0', 'xD7FF' ]
- [ 'xE000', 'xFFFD' ]
- [ 'x010000', 'x10FFFF' ]
:002: nb-json
# nb-json ::=
# x:9 | [x:20-x:10FFFF]
nb-json:
(any):
- 'x09'
- [ 'x20', 'x10FFFF' ]
### 5.2. Character Encodings
#===============================================================================
# Rules 003-021 define single characters. These rules are never referenced in
# the rest of the grammar. They should at least be referenced in rule 022.
#===============================================================================
:003: c-byte-order-mark
# c-byte-order-mark ::=
# x:FEFF
c-byte-order-mark: 'xFEFF'
### 5.3. Indicator Characters
#===============================================================================
# Rules 004 to 021 are single characters to match. Instead of a hex code, they
# are specified as a single character in single quotes.
#===============================================================================
:004: c-sequence-entry
# c-sequence-entry ::=
# '-'
c-sequence-entry: '-'
:005: c-mapping-key
# c-mapping-key ::=
# '?'
c-mapping-key: '?'
:006: c-mapping-value
# c-mapping-value ::=
# ':'
c-mapping-value: ':'
:007: c-collect-entry
# c-collect-entry ::=
# ','
c-collect-entry: ','
:008: c-sequence-start
# c-sequence-start ::=
# '['
c-sequence-start: '['
:009: c-sequence-end
# c-sequence-end ::=
# ']'
c-sequence-end: ']'
:010: c-mapping-start
# c-mapping-start ::=
# '{'
c-mapping-start: '{'
:011: c-mapping-end
# c-mapping-end ::=
# '}'
c-mapping-end: '}'
:012: c-comment
# c-comment ::=
# '#'
c-comment: '#'
:013: c-anchor
# c-anchor ::=
# '&'
c-anchor: '&'
:014: c-alias
# c-alias ::=
# '*'
c-alias: '*'
:015: c-tag
# c-tag ::=
# '!'
c-tag: '!'
:016: c-literal
# c-literal ::=
# '|'
c-literal: '|'
:017: c-folded
# c-folded ::=
# '>'
c-folded: '>'
:018: c-single-quote
# c-single-quote ::=
# '''
c-single-quote: ''''
:019: c-double-quote
# c-double-quote ::=
# '"'
c-double-quote: '"'
:020: c-directive
# c-directive ::=
# '%'
c-directive: '%'
:021: c-reserved
# c-reserved ::=
# '@' | '`'
c-reserved:
(any):
- '@'
- '`'
#===============================================================================
# Rule 022 should probably be defined using the rule names above rather than
# literal characters.
#===============================================================================
:022: c-indicator
# c-indicator ::=
# '-' | '?' | ':' | ',' | '[' | ']' | '{' | '}'
# | '#' | '&' | '*' | '!' | '|' | '>' | ''' | '"'
# | '%' | '@' | '`'
c-indicator:
(any):
- '-'
- '?'
- ':'
- ','
- '['
- ']'
- '{'
- '}'
- '#'
- '&'
- '*'
- '!'
- '|'
- '>'
- ''''
- '"'
- '%'
- '@'
- '`'
:023: c-flow-indicator
# c-flow-indicator ::=
# ',' | '[' | ']' | '{' | '}'
c-flow-indicator:
(any):
- ','
- '['
- ']'
- '{'
- '}'
### 5.4. Line Break Characters
:024: b-line-feed
# b-line-feed ::=
# x:A
b-line-feed: 'x0A'
:025: b-carriage-return
# b-carriage-return ::=
# x:D
b-carriage-return: 'x0D'
#===============================================================================
# Rules can "call" other rules by name. This is called a rule reference. A
# reference matches if its named rule matches.
#===============================================================================
:026: b-char
# b-char ::=
# b-line-feed | b-carriage-return
b-char:
(any):
- b-line-feed
- b-carriage-return
#===============================================================================
# The (---) operator here matches if the next character is in the first range,
# but not in any of the following ranges.
#===============================================================================
:027: nb-char
# nb-char ::=
# c-printable - b-char - c-byte-order-mark
nb-char:
(---):
- c-printable
- b-char
- c-byte-order-mark
#===============================================================================
# A rule in in a group of rules can be another group. This is how parenthesized
# groups are represented.
#
# The (all) function matches if all the rules in its group match.
#===============================================================================
:028: b-break
# b-break ::=
# ( b-carriage-return b-line-feed )
# | b-carriage-return
# | b-line-feed
b-break:
(any):
- (all):
- b-carriage-return
- b-line-feed
- b-carriage-return
- b-line-feed
:029: b-as-line-feed
# b-as-line-feed ::=
# b-break
b-as-line-feed: b-break
:030: b-non-content
# b-non-content ::=
# b-break
b-non-content: b-break
### 5.5. White Space Characters
:031: s-space
# s-space ::=
# x:20
s-space: 'x20'
:032: s-tab
# s-tab ::=
# x:9
s-tab: 'x09'
:033: s-white
# s-white ::=
# s-space | s-tab
s-white:
(any):
- s-space
- s-tab
:034: ns-char
# ns-char ::=
# nb-char - s-white
ns-char:
(---):
- nb-char
- s-white
### 5.6. Miscellaneous Characters
:035: ns-dec-digit
# ns-dec-digit ::=
# [x:30-x:39]
ns-dec-digit: [ 'x30', 'x39' ]
:036: ns-hex-digit
# ns-hex-digit ::=
# ns-dec-digit
# | [x:41-x:46] | [x:61-x:66]
ns-hex-digit:
(any):
- ns-dec-digit
- [ 'x41', 'x46' ]
- [ 'x61', 'x66' ]
:037: ns-ascii-letter
# ns-ascii-letter ::=
# [x:41-x:5A] | [x:61-x:7A]
ns-ascii-letter:
(any):
- [ 'x41', 'x5A' ]
- [ 'x61', 'x7A' ]
:038: ns-word-char
# ns-word-char ::=
# ns-dec-digit | ns-ascii-letter | '-'
ns-word-char:
(any):
- ns-dec-digit
- ns-ascii-letter
- '-'
:039: ns-uri-char
# ns-uri-char ::=
# '%' ns-hex-digit ns-hex-digit | ns-word-char | '#'
# | ';' | '/' | '?' | ':' | '@' | '&' | '=' | '+' | '$' | ','
# | '_' | '.' | '!' | '~' | '*' | ''' | '(' | ')' | '[' | ']'
ns-uri-char:
(any):
- (all):
- '%'
- ns-hex-digit
- ns-hex-digit
- ns-word-char
- '#'
- ';'
- '/'
- '?'
- ':'
- '@'
- '&'
- '='
- '+'
- '$'
- ','
- '_'
- '.'
- '!'
- '~'
- '*'
- ''''
- '('
- ')'
- '['
- ']'
:040: ns-tag-char
# ns-tag-char ::=
# ns-uri-char - '!' - c-flow-indicator
ns-tag-char:
(---):
- ns-uri-char
- '!'
- c-flow-indicator
### 5.7. Escaped Characters
:041: c-escape
# c-escape ::=
# '\'
c-escape: '\'
:042: ns-esc-null
# ns-esc-null ::=
# '0'
ns-esc-null: '0'
:043: ns-esc-bell
# ns-esc-bell ::=
# 'a'
ns-esc-bell: 'a'
:044: ns-esc-backspace
# ns-esc-backspace ::=
# 'b'
ns-esc-backspace: 'b'
:045: ns-esc-horizontal-tab
# ns-esc-horizontal-tab ::=
# 't' | x:9
ns-esc-horizontal-tab:
(any):
- t
- 'x09'
:046: ns-esc-line-feed
# ns-esc-line-feed ::=
# 'n'
ns-esc-line-feed: 'n'
:047: ns-esc-vertical-tab
# ns-esc-vertical-tab ::=
# 'v'
ns-esc-vertical-tab: 'v'
:048: ns-esc-form-feed
# ns-esc-form-feed ::=
# 'f'
ns-esc-form-feed: 'f'
:049: ns-esc-carriage-return
# ns-esc-carriage-return ::=
# 'r'
ns-esc-carriage-return: 'r'
:050: ns-esc-escape
# ns-esc-escape ::=
# 'e'
ns-esc-escape: 'e'
:051: ns-esc-space
# ns-esc-space ::=
# x:20
ns-esc-space: 'x20'
:052: ns-esc-double-quote
# ns-esc-double-quote ::=
# '"'
ns-esc-double-quote: '"'
:053: ns-esc-slash
# ns-esc-slash ::=
# '/'
ns-esc-slash: '/'
:054: ns-esc-backslash
# ns-esc-backslash ::=
# '\'
ns-esc-backslash: '\'
:055: ns-esc-next-line
# ns-esc-next-line ::=
# 'N'
ns-esc-next-line: 'N'
:056: ns-esc-non-breaking-space
# ns-esc-non-breaking-space ::=
# '_'
ns-esc-non-breaking-space: '_'
:057: ns-esc-line-separator
# ns-esc-line-separator ::=
# 'L'
ns-esc-line-separator: 'L'
:058: ns-esc-paragraph-separator
# ns-esc-paragraph-separator ::=
# 'P'
ns-esc-paragraph-separator: 'P'
#===============================================================================
# The ({x}) is a numeric quantifier. ie The rule must match that exact number
# of times.
#===============================================================================
:059: ns-esc-8-bit
# ns-esc-8-bit ::=
# 'x'
# ( ns-hex-digit{2} )
ns-esc-8-bit:
(all):
- 'x'
- ({2}): ns-hex-digit
:060: ns-esc-16-bit
# ns-esc-16-bit ::=
# 'u'
# ( ns-hex-digit{4} )
ns-esc-16-bit:
(all):
- 'u'
- ({4}): ns-hex-digit
:061: ns-esc-32-bit
# ns-esc-32-bit ::=
# 'U'
# ( ns-hex-digit{8} )
ns-esc-32-bit:
(all):
- 'U'
- ({8}): ns-hex-digit
:062: c-ns-esc-char
# c-ns-esc-char ::=
# '\'
# ( ns-esc-null | ns-esc-bell | ns-esc-backspace
# | ns-esc-horizontal-tab | ns-esc-line-feed
# | ns-esc-vertical-tab | ns-esc-form-feed
# | ns-esc-carriage-return | ns-esc-escape | ns-esc-space
# | ns-esc-double-quote | ns-esc-slash | ns-esc-backslash
# | ns-esc-next-line | ns-esc-non-breaking-space
# | ns-esc-line-separator | ns-esc-paragraph-separator
# | ns-esc-8-bit | ns-esc-16-bit | ns-esc-32-bit )
c-ns-esc-char:
(all):
- '\'
- (any):
- ns-esc-null
- ns-esc-bell
- ns-esc-backspace
- ns-esc-horizontal-tab
- ns-esc-line-feed
- ns-esc-vertical-tab
- ns-esc-form-feed
- ns-esc-carriage-return
- ns-esc-escape
- ns-esc-space
- ns-esc-double-quote
- ns-esc-slash
- ns-esc-backslash
- ns-esc-next-line
- ns-esc-non-breaking-space
- ns-esc-line-separator
- ns-esc-paragraph-separator
- ns-esc-8-bit
- ns-esc-16-bit
- ns-esc-32-bit
### 6.1. Indentation Spaces
#===============================================================================
# Some rules are intended to be called with arguments. Arguments are specified
# with the (...) key. A single argument is a scalar value. Multiple arguments
# are specified as a sequence.
#===============================================================================
:063: s-indent
# s-indent(n) ::=
# s-space{n}
s-indent:
(...): n