-
Notifications
You must be signed in to change notification settings - Fork 19
/
parse-ebnf.pegjs
194 lines (160 loc) · 4.05 KB
/
parse-ebnf.pegjs
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
/*
* Parse EBNF format defined here:
* http://www.w3.org/TR/2004/REC-xml11-20040204/#sec-notation
* Based on:
* https://github.com/pegjs/pegjs/blob/master/src/parser.pegjs
*/
{
var OPS_TO_SUFFIXED_TYPES = {
"?": "optional",
"*": "zero_or_more",
"+": "one_or_more"
};
function extractList(list, index) {
var result = new Array(list.length), i;
for (i = 0; i < list.length; i++) {
result[i] = list[i][index];
}
return result;
}
function buildList(first, rest, index) {
return [first].concat(extractList(rest, index));
}
function location() {return null;}
}
Grammar = rules:(Rule __)+ {
return {
type: "grammar",
rules: extractList(rules, 0),
location: location()
};
}
Rule = __ name:IdentifierName __ "::=" __ expression:Expression {
return {
type: "rule",
name: name,
expression: expression,
location: location()
};
}
Expression
= ChoiceExpression
ChoiceExpression
= first:SequenceExpression rest:(__ "|" __ SequenceExpression)* {
return rest.length > 0
? {
type: "choice",
alternatives: buildList(first, rest, 3),
location: location()
}
: first;
}
SequenceExpression
= first:SubtractExpression rest:(__ SubtractExpression)* {
return rest.length > 0
? {
type: "sequence",
elements: buildList(first, rest, 1),
location: location()
}
: first;
}
SubtractExpression
= first:SuffixedExpression second:(__ '-' __ SuffixedExpression)? {
return second !== null ? {
type: "sequence",
elements: [
{
type: 'simple_not',
expression: second[3]
},
first
],
location: location()
} : first;
}
SuffixedExpression
= expression:PrimaryExpression __ operator:SuffixedOperator {
return {
type: OPS_TO_SUFFIXED_TYPES[operator],
expression: expression,
location: location()
};
}
/ PrimaryExpression
SuffixedOperator
= "?"
/ "*"
/ "+"
PrimaryExpression
= LiteralMatcher
/ CharacterClassMatcher
/ RuleReferenceExpression
/ "(" __ expression:Expression __ ")" { return expression; }
RuleReferenceExpression
= name:IdentifierName !(__ (StringLiteral __)? "::=") {
return { type: "rule_ref", name: name, location: location() };
}
LiteralMatcher "literal"
= value:StringLiteral {
return {
type: "literal",
value: value,
location: location()
};
}
/* ---- Lexical Grammar ----- */
SourceCharacter
= .
IdentifierName = name:[a-zA-Z0-9_]+ {return name.join('')}
StringLiteral "string"
= '"' chars:DoubleStringCharacter* '"' { return chars.join(""); }
/ "'" chars:SingleStringCharacter* "'" { return chars.join(""); }
/ UnicodeChar
DoubleStringCharacter
= !('"') SourceCharacter { return text(); }
SingleStringCharacter
= !("'") SourceCharacter { return text(); }
CharacterClassMatcher "character class"
= "["
inverted:"^"?
parts:(ClassCharacterRange / ClassCharacter)*
"]"
{
return {
type: "class",
//parts: filterEmptyStrings(parts),
inverted: inverted !== null,
rawText: text(),
location: location()
};
}
ClassCharacterRange
= begin:ClassCharacter "-" end:ClassCharacter {
return [begin, end];
}
ClassCharacter
= UnicodeChar
/ !("]") SourceCharacter { return text(); }
UnicodeChar = '#x' [A-Fa-f0-9]+ { return text(); }
Comment "comment"
= MultiLineComment
MultiLineComment
= "/*" (!"*/" SourceCharacter)* "*/"
__
= (WhiteSpace / LineTerminatorSequence / Comment)*
WhiteSpace "whitespace"
= "\t"
/ "\v"
/ "\f"
/ " "
/ "\u00A0"
/ "\uFEFF"
LineTerminator
= [\n\r\u2028\u2029]
LineTerminatorSequence "end of line"
= "\n"
/ "\r\n"
/ "\r"
/ "\u2028"
/ "\u2029"