-
Notifications
You must be signed in to change notification settings - Fork 0
/
peg.peg
350 lines (259 loc) · 9.07 KB
/
peg.peg
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
%{
import 'package:peg/src/expressions/expressions.dart';
import 'package:peg/src/grammar/grammar.dart';
export 'package:peg/src/grammar/grammar.dart';
}%
%%
Expression _buildPrefix(String? prefix, Expression expression) {
if (prefix == null) {
return expression;
}
switch (prefix) {
case '&':
return AndPredicateExpression(expression: expression);
case '!':
return NotPredicateExpression(expression: expression);
case '\$':
return SliceExpression(expression: expression);
default:
throw StateError('Unknown prefix: $prefix');
}
}
Expression _buildSuffix(Object? suffix, Expression expression) {
if (suffix == null) {
return expression;
}
if (suffix is (int?, int?)) {
return RepetitionExpression(
expression: expression, min: suffix.$1, max: suffix.$2);
}
switch (suffix) {
case '+':
return OneOrMoreExpression(expression: expression);
case '?':
return OptionalExpression(expression: expression);
case '*':
return ZeroOrMoreExpression(expression: expression);
default:
throw StateError('Unknown suffix: $suffix');
}
}
int _escape(int charCode) {
switch (charCode) {
case 0x6e:
return 0xa;
case 0x72:
return 0xd;
case 0x74:
return 0x9;
case 0x22:
return 0x22;
case 0x27:
return 0x27;
case 0x5c:
return 0x5c;
case 0x5d:
return 0x5d;
case 0x5e:
return 0x5e;
default:
return charCode;
}
}
%%
Grammar
Start = Spaces g:Globals? m:Members? d:Definition* @eof() { $$ = Grammar(rules: d, globals: g, members: m); } ;
Globals = '%{' v:$(!'}%' v:. )* '}%' Spaces ;
Members = '%%' v:$(!'%%' v:. )* '%%' Spaces ;
Block = '{' v:$BlockBody* RightCurlyBracket ;
BlockBody =
'{' v:BlockBody* '}'
/ !'}' . ;
ProductionRule
Definition =
m:Metadata? t:Type? i:Identifier EqualsSign e:Expression Semicolon
{ $$ = ProductionRule(name: i, expression: e as OrderedChoiceExpression, resultType: t, metadata: m); }
/ m:Metadata? i:Identifier EqualsSign e:Expression Semicolon
{ $$ = ProductionRule(name: i, expression: e as OrderedChoiceExpression, metadata: m); } ;
ResultType
Type = t:(GenericType / RecordType) q:QuestionMark? { $$ = q == null ? t : t.getNullableType(); } ;
ResultType
GenericType =
i:NativeIdentifier LessThanSign p:TypeArguments GreaterThanSign { $$ = GenericType(name: i, arguments: p); }
/ i:NativeIdentifier { $$ = GenericType(name: i); } ;
String
NativeIdentifier = i:$([$a-zA-Z_] [$0-9a-zA-Z_]*) Spaces ;
List<ResultType>
TypeArguments = h:Type t:(Comma v:Type)* { $$ = [h, ...t]; } ;
ResultType
RecordType =
LeftParenthesis
v:(
n:NamedFields <RecordType>{ $$ = RecordType(named: n); }
/ p:PositionalFields Comma n:NamedFields <RecordType>{ $$ = RecordType(positional: p, named: n); }
/ h:PositionalField Comma t:PositionalFields <RecordType>{ $$ = RecordType(positional: [h, ...t]); }
/ t:PositionalField Comma <RecordType>{ $$ = RecordType(positional: [t]); }
)
RightParenthesis ;
List<({ResultType type, String name})>
NamedFields = LeftCurlyBracket h:NamedField t:(Comma v:NamedField)* RightCurlyBracket { $$ = [h, ...t]; } ;
({ResultType type, String name})
NamedField = type:Type name:NativeIdentifier ;
List<({ResultType type, String? name})>
PositionalFields = h:PositionalField t:(Comma v:PositionalField)* { $$ = [h, ...t]; } ;
({ResultType type, String? name})
PositionalField = type:Type name:NativeIdentifier? ;
List<({String name, List<Object?> arguments})>
Metadata = h:MetadataElement t:(Spaces v:MetadataElement)* { $$ = [h, ...t]; } ;
({String name, List<Object?> arguments})
MetadataElement = CommercialAt i:Identifier a:MetadataArguments? { $$ = (name: '@$i', arguments: a ?? const []); } ;
List<Object?>
MetadataArguments = LeftParenthesis v:MetadataArgumentList? RightParenthesis { $$ = v ?? const []; } ;
List<Object?>
MetadataArgumentList = h:MetadataArgument t:(Comma v:MetadataArgument)* { $$ = [h, ...t]; } ;
MetadataArgument = '\'' v:StringChar* Apostrophe { $$ = String.fromCharCodes(v); } ;
Expression = OrderedChoice ;
Expression
OrderedChoice = h:Sequence t:(Solidus v:Sequence)* { $$ = OrderedChoiceExpression(expressions: [h, ...t]); } ;
Expression
Sequence = e:SequenceElement+ a:Action? { $$ = SequenceExpression(expressions: e, action: a); } ;
SequenceElement =
i:Identifier Colon p:Prefix <Expression>{ $$ = p..semanticVariable = i; }
/ Prefix ;
SemanticAction
Action = t:(LessThanSign v:Type GreaterThanSign)? b:Block { $$ = SemanticAction(source: b, resultType: t); } ;
Expression
Prefix = p:(DollarSign / Ampersand / ExclamationMark)? s:Suffix {$$ = _buildPrefix(p, s); } ;
Expression
Suffix = p:Primary s:(
Asterisk
/ QuestionMark
/ PlusSign/
LeftCurlyBracket v:MinMax RightCurlyBracket
)? { $$ = _buildSuffix(s, p); } ;
Primary =
Symbol
/ CharacterClass
/ Literal
/ CharacterClass
/ AnyCharacter
/ Group
/ Cut
/ MetaExpression
;
Expression
AnyCharacter = FullStop { $$ = AnyCharacterExpression(); };
Expression
Cut = UpwardsArrow { $$ = CutExpression(); };
Expression
CharacterClass =
'[^' r:(!']' v:Range)+ RightSquareBracket { $$ = CharacterClassExpression(ranges: r, negate: true); }
/ '[' r:(!']' v:Range)+ RightSquareBracket { $$ = CharacterClassExpression(ranges: r); } ;
Expression
Group = LeftParenthesis e:Expression RightParenthesis { $$ = GroupExpression(expression: e); } ;
Expression
Literal = s:String { $$ = LiteralExpression(string: s); } ;
Expression
Symbol = i:Identifier { $$ = SymbolExpression(name: i); } ;
Expression
MetaExpression =
@indicate('Unknown meta expression', !MetaName) v:(
Eof
/ Expected
/ Indicate
/ List
/ List1
/ MatchString
/ Message
/ StringChars
/ Tag
/ Verify
) ;
Expression
Eof = '@eof' Spaces LeftParenthesis RightParenthesis Spaces { $$ = EofExpression(); } ;
Expression
Expected = '@expected' Spaces LeftParenthesis t:String Comma e:Expression RightParenthesis { $$ = ExpectedExpression(expression: e, tag: t); } ;
Expression
Indicate = '@indicate' Spaces LeftParenthesis s:String Comma e:Expression RightParenthesis { $$ = IndicateExpression(expression: e, message: s); } ;
Expression
List = '@list' Spaces LeftParenthesis h:Expression Comma t:Expression RightParenthesis { $$ = ListExpression(first: h, next: t); } ;
Expression
List1 = '@list1' Spaces LeftParenthesis h:Expression Comma t:Expression RightParenthesis { $$ = List1Expression(first: h, next: t); } ;
Expression
MatchString = '@matchString' Spaces LeftParenthesis b:Block RightParenthesis { $$ = MatchStringExpression(value: b); } ;
Expression
Message = '@message' Spaces LeftParenthesis s:String Comma e:Expression RightParenthesis { $$ = MessageExpression(expression: e, message: s); } ;
Expression
StringChars = '@stringChars' Spaces LeftParenthesis n:Expression Comma c:Expression Comma e:Expression RightParenthesis { $$ = StringCharsExpression(normalCharacters: n, escapeCharacter:c, escape: e); } ;
Expression
Tag = '@tag' Spaces LeftParenthesis t:String Comma e:Expression RightParenthesis { $$ = TagExpression(expression: e, tag: t); } ;
Expression
Verify = '@verify' Spaces LeftParenthesis m:String Comma e:Expression Comma b:Block RightParenthesis { $$ = VerifyExpression(expression: e, message:m, predicate: b); } ;
(int, int)
Range =
s:RangeChar '-' e:RangeChar { $$ = (s, e); }
/ s:RangeChar { $$ = (s, s); } ;
int
HexChar = 'u{' v:$[a-zA-Z0-9]+ '}' <int>{ $$ = int.parse(v, radix: 16); } ;
String
String = '\'' cs:(!'\'' c:StringChar)* '\'' Spaces { $$ = String.fromCharCodes(cs); } ;
int
StringChar =
[\u{20}-\u{5b}\u{5d}-\u{10ffff}]
/ '\\' v:(c:[rnt'"\\] <int>{ $$ = _escape(c); } / HexChar)
;
int
RangeChar =
'\\' v:(c:[-nrt\]\\^] <int>{ $$ = _escape(c); } / HexChar)
/ !'\\' c:.;
String
Identifier = i:$([a-zA-Z] [a-zA-Z_0-9]*) Spaces ;
String
MetaName = !ReservedMetaName v:$('@' [a-zA-Z] [a-zA-Z_0-9]*) Spaces ;
@indicate
ReservedMetaNames =
'eof'
/ 'expected'
/ 'indicate'
/ 'list1'
/ 'list'
/ 'matchString'
/ 'message'
/ 'stringChars'
/ 'tag'
/ 'verify'
;
ReservedMetaName = $('@' ReservedMetaNames ![a-zA-Z_0-9]+) ;
(int?, int?)
MinMax =
m:Integer Comma n:Integer { $$ = (m, n); }
/ Comma n:Integer { $$ = (null, n); }
/ m:Integer Comma { $$ = (m, null); }
/ n:Integer { $$ = (n, n); } ;
int
Integer = v:$[0-9]+ { $$ = int.parse(v); } ;
Ampersand = v:'&' Spaces ;
Apostrophe = v:'\'' Spaces ;
Asterisk = v:'*' Spaces ;
Colon = v:':' Spaces ;
Comma = v:',' Spaces ;
CommercialAt = v:'@' Spaces ;
DollarSign = v:'$' Spaces ;
EqualsSign = v:'=' Spaces ;
ExclamationMark = v:'!' Spaces ;
FullStop = v:'.' Spaces ;
GreaterThanSign = v:'>' Spaces ;
LeftCurlyBracket = v:'{' Spaces ;
LeftParenthesis = v:'(' Spaces ;
LessThanSign = v:'<' Spaces ;
PlusSign = v:'+' Spaces ;
RightCurlyBracket = v:'}' Spaces ;
RightParenthesis = v:')' Spaces ;
RightSquareBracket = v:']' Spaces ;
QuestionMark = v:'?' Spaces ;
Semicolon = v:';' Spaces ;
Solidus = v:'/' Spaces ;
UpwardsArrow = v:'↑' Spaces ;
Spaces = (WhiteSpace / Comment)* ;
Comment = '#' (![\n\r] .)* ;
WhiteSpace = [ \n\r\t];