-
Notifications
You must be signed in to change notification settings - Fork 3
/
grammar.js
162 lines (120 loc) · 3.44 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
num_val_template = (indicator, char_rule) => seq(
indicator, repeat1(char_rule),
optional(choice(
repeat1(seq(".", repeat1(char_rule))),
seq("-", repeat1(char_rule))
))
);
module.exports = grammar({
name: 'abnf',
extras: $ => [],
conflicts: $ => [
[$.concatenation],
[$.alternation]
],
rules: {
source_file: $ => $.rulelist,
rulelist: $ => repeat1(choice(
$.rule,
prec(1, seq(repeat($._c_wsp), $._c_nl))
)),
rule: $ => seq($.rulename, $.defined_as, $.elements, $._c_nl),
// rulename: $ => seq(
// $.ALPHA,
// repeat(choice($.ALPHA, $.DIGIT, "-"))
// ),
rulename: $ => /[A-Za-z][A-Za-z0-9-]*/,
defined_as: $ => seq(
repeat($._c_wsp),
choice("=", "=/"),
repeat($._c_wsp)
),
elements: $ => prec.left(1, seq($.alternation, repeat($._c_wsp))),
_c_wsp: $ => choice(
$._WSP,
seq($._c_nl, $._WSP)
),
_c_nl: $ => choice($.comment, $._CRLF),
// comment: $ => seq(
// ";",
// repeat(choice($._WSP, $.VCHAR)),
// $._CRLF
// ),
comment: $ => /;[ \t\x21-\x7E]*\r\n/,
alternation: $ => seq(
$.concatenation,
repeat(seq(
repeat($._c_wsp),
"/",
repeat($._c_wsp),
$.concatenation
))
),
concatenation: $ => seq(
$.repetition,
repeat(seq(
repeat1($._c_wsp),
$.repetition
))
),
repetition: $ => seq(optional($.repeat), $.element),
repeat: $ => choice(
repeat1($.DIGIT),
seq(repeat($.DIGIT), "*", repeat($.DIGIT))
),
// NB: $.core_rulename isn't explicitly defined in RFC 5234.
element: $ => choice(
$.core_rulename,
$.rulename, $.group, $.option,
$.char_val, $.num_val, $.prose_val
),
group: $ => seq("(", repeat($._c_wsp), $.alternation, repeat($._c_wsp), ")"),
option: $ => seq("[", repeat($._c_wsp), $.alternation, repeat($._c_wsp), "]"),
// Updated by RFC 7405
char_val: $ => choice($.case_insensitive_string, $.case_sensitive_string),
case_insensitive_string: $ => seq(optional("%i"), $.quoted_string),
case_sensitive_string: $ => seq("%s", $.quoted_string),
quoted_string: $ => seq($._DQUOTE, /[\x20-\x21\x23-\x7E]*/, $._DQUOTE),
num_val: $ => seq("%", choice($.bin_val, $.dec_val, $.hex_val)),
bin_val: $ => num_val_template("b", $.BIT),
dec_val: $ => num_val_template("d", $.DIGIT),
hex_val: $ => num_val_template("x", $.HEXDIG),
prose_val: $ => seq("<", /[\x20-\x3D\x3F-\x7E]*/, ">"),
// RFC 5234 doesn't define these as special rulenames; they just
// happen to match the rulename rule. However, Appendix B defines
// commonly used rules which use these rulenames and they are
// implicitly used in many ABNF grammars.
core_rulename: $ => choice(
"ALPHA",
"BIT",
"CHAR",
"CR",
"CRLF",
"CTL",
"DIGIT",
"DQUOTE",
"HEXDIG",
"HTAB",
"LF",
"LWSP",
"OCTET",
"SP",
"VCHAR",
"WSP"
),
ALPHA: $ => /[A-Za-z]/,
BIT: $ => choice("0", "1"),
DIGIT: $ => /[0-9]/,
_CR: $ => "\r",
_CRLF: $ => seq($._CR, $._LF),
_DQUOTE: $ => "\"",
// RFC 5234 only defines upper-case HEXDIGs, but this grammar is
// more lenient.
HEXDIG: $ => /[0-9A-Fa-f]/,
_HTAB: $ => "\t",
_LF: $ => "\n",
_SP: $ => " ",
VCHAR: $ => /[\x21-\x7E]/,
_WSP: $ => choice($._SP, $._HTAB)
}
});