forked from microsoft/botbuilder-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommonRegex.g4
384 lines (336 loc) · 7.83 KB
/
CommonRegex.g4
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
grammar CommonRegex;
@parser::header {
#pragma warning disable 3021 // Disable StyleCop warning CS3021 re CLSCompliant attribute in generated files.
#pragma warning disable 0108 // Disable StyleCop warning CS0108, hides inherited member in generated files.
}
@lexer::header {
#pragma warning disable 3021 // Disable StyleCop warning CS3021 re CLSCompliant attribute in generated files.
#pragma warning disable 0108 // Disable StyleCop warning CS0108, hides inherited member in generated files.
}
parse
: alternation EOF
;
alternation
: expr ('|' expr)*
;
expr
: element*
;
element
: atom quantifier?
;
// QUANTIFIERS
//
// ? 0 or 1, greedy
// ?+ 0 or 1, possessive
// ?? 0 or 1, lazy
// * 0 or more, greedy
// *+ 0 or more, possessive
// *? 0 or more, lazy
// + 1 or more, greedy
// ++ 1 or more, possessive
// +? 1 or more, lazy
// {n} exactly n
// {n,m} at least n, no more than m, greedy
// {n,m}+ at least n, no more than m, possessive
// {n,m}? at least n, no more than m, lazy
// {n,} n or more, greedy
// {n,}+ n or more, possessive
// {n,}? n or more, lazy
quantifier
: '?' quantifier_type
| '+' quantifier_type
| '*' quantifier_type
| '{' number '}' quantifier_type
| '{' number ',' '}' quantifier_type
| '{' number ',' number '}' quantifier_type
;
quantifier_type
: '+'
| '?'
| /* nothing */
;
// CHARACTER CLASSES
//
// [...] positive character class
// [^...] negative character class
// [x-y] range (can be used for hex characters)
character_class
: '[' '^' cc_atom+ ']'
| '[' cc_atom+ ']'
;
// CAPTURING
//
// (...) capturing group
// (?<name>...) named capturing group (.NET, javascript)
// (?:...) non-capturing group
capture
: '(' '?' '<' name '>' alternation ')'
| '(' alternation ')'
;
non_capture
: '(' '?' ':' alternation ')'
;
// OPTION SETTING(C# format, for javascript should be wrapped in code)
//
// (?i) caseless
// (?m) multiline
// (?s) single line (dotall)
option
: '(' '?' option_flag+ ')'
;
option_flag
: 'i'
| 'm'
| 's'
;
// QUOTING
//
// \x where x is non-alphanumeric is a literal x
// \Q...\E treat enclosed characters as literal
Quoted : '\\' NonAlphaNumeric;
BlockQuoted : '\\Q' .*? '\\E';
atom
: shared_atom
| literal
| character_class
| capture
| non_capture
| option
| Dot
| Caret
| EndOfSubject
;
cc_atom
: cc_literal Hyphen cc_literal
| shared_atom
| cc_literal
;
shared_atom
: ControlChar
| DecimalDigit
| NotDecimalDigit
| CharWithProperty
| CharWithoutProperty
| WhiteSpace
| NotWhiteSpace
| WordChar
| NotWordChar
;
literal
: shared_literal
| CharacterClassEnd
;
cc_literal
: shared_literal
| Dot
| CharacterClassStart
| Caret
| QuestionMark
| Plus
| Star
| EndOfSubject
| Pipe
| OpenParen
| CloseParen
;
shared_literal
: octal_char
| letter
| digit
| BellChar
| EscapeChar
| FormFeed
| NewLine
| CarriageReturn
| Tab
| HexChar
| Quoted
| BlockQuoted
| OpenBrace
| CloseBrace
| Comma
| Hyphen
| LessThan
| GreaterThan
| SingleQuote
| Underscore
| Colon
| Hash
| Equals
| Exclamation
| Ampersand
| OtherChar
;
number
: digits
;
octal_char
: ( Backslash (D0 | D1 | D2 | D3) octal_digit octal_digit | Backslash octal_digit octal_digit)
;
octal_digit
: D0 | D1 | D2 | D3 | D4 | D5 | D6 | D7
;
digits
: digit+
;
digit
: D0 | D1 | D2 | D3 | D4 | D5 | D6 | D7 | D8 | D9
;
name
: alpha_nums
;
alpha_nums
: (letter | Underscore) (letter | Underscore | digit)*
;
non_close_parens
: non_close_paren+
;
non_close_paren
: ~CloseParen
;
letter
: ALC | BLC | CLC | DLC | ELC | FLC | GLC | HLC | ILC | JLC | KLC | LLC | MLC | NLC | OLC | PLC | QLC | RLC | SLC | TLC | ULC | VLC | WLC | XLC | YLC | ZLC |
AUC | BUC | CUC | DUC | EUC | FUC | GUC | HUC | IUC | JUC | KUC | LUC | MUC | NUC | OUC | PUC | QUC | RUC | SUC | TUC | UUC | VUC | WUC | XUC | YUC | ZUC
;
// CHARACTERS
//
// \a alarm, that is, the BEL character (hex 07)
// \cx "control-x", where x is any ASCII character
// \e escape (hex 1B)
// \f form feed (hex 0C)
// \n newline (hex 0A)
// \r carriage return (hex 0D)
// \t tab (hex 09)
// \ddd character with octal code ddd, or backreference
// \xhh character with hex code hh
// \x{hhh..} character with hex code hhh..
BellChar : '\\a';
ControlChar : '\\c';
EscapeChar : '\\e';
FormFeed : '\\f';
NewLine : '\\n';
CarriageReturn : '\\r';
Tab : '\\t';
Backslash : '\\';
HexChar : '\\x' ( HexDigit HexDigit
| '{' HexDigit HexDigit HexDigit+ '}'
)
;
// CHARACTER TYPES
//
// . any character except newline;
// in dotall mode, any character whatsoever
// \d a decimal digit
// \D a character that is not a decimal digit
// \p{xx} a character with the xx property
// \P{xx} a character without the xx property
// \s a white space character
// \S a character that is not a white space character
// \w a "word" character
// \W a "non-word" character
Dot : '.';
DecimalDigit : '\\d';
NotDecimalDigit : '\\D';
CharWithProperty : '\\p{' UnderscoreAlphaNumerics '}';
CharWithoutProperty : '\\P{' UnderscoreAlphaNumerics '}';
WhiteSpace : '\\s';
NotWhiteSpace : '\\S';
WordChar : '\\w';
NotWordChar : '\\W';
CharacterClassStart : '[';
CharacterClassEnd : ']';
Caret : '^';
Hyphen : '-';
QuestionMark : '?';
Plus : '+';
Star : '*';
OpenBrace : '{';
CloseBrace : '}';
Comma : ',';
// ANCHORS AND SIMPLE ASSERTIONS
//
// $ end of subject
EndOfSubject : '$';
Pipe : '|';
OpenParen : '(';
CloseParen : ')';
LessThan : '<';
GreaterThan : '>';
SingleQuote : '\'';
Underscore : '_';
Colon : ':';
Hash : '#';
Equals : '=';
Exclamation : '!';
Ampersand : '&';
ALC : 'a';
BLC : 'b';
CLC : 'c';
DLC : 'd';
ELC : 'e';
FLC : 'f';
GLC : 'g';
HLC : 'h';
ILC : 'i';
JLC : 'j';
KLC : 'k';
LLC : 'l';
MLC : 'm';
NLC : 'n';
OLC : 'o';
PLC : 'p';
QLC : 'q';
RLC : 'r';
SLC : 's';
TLC : 't';
ULC : 'u';
VLC : 'v';
WLC : 'w';
XLC : 'x';
YLC : 'y';
ZLC : 'z';
AUC : 'A';
BUC : 'B';
CUC : 'C';
DUC : 'D';
EUC : 'E';
FUC : 'F';
GUC : 'G';
HUC : 'H';
IUC : 'I';
JUC : 'J';
KUC : 'K';
LUC : 'L';
MUC : 'M';
NUC : 'N';
OUC : 'O';
PUC : 'P';
QUC : 'Q';
RUC : 'R';
SUC : 'S';
TUC : 'T';
UUC : 'U';
VUC : 'V';
WUC : 'W';
XUC : 'X';
YUC : 'Y';
ZUC : 'Z';
D1 : '1';
D2 : '2';
D3 : '3';
D4 : '4';
D5 : '5';
D6 : '6';
D7 : '7';
D8 : '8';
D9 : '9';
D0 : '0';
OtherChar : . ;
// fragments
fragment UnderscoreAlphaNumerics : ('_' | AlphaNumeric)+;
fragment AlphaNumerics : AlphaNumeric+;
fragment AlphaNumeric : [a-zA-Z0-9];
fragment NonAlphaNumeric : ~[a-zA-Z0-9];
fragment HexDigit : [0-9a-fA-F];
fragment ASCII : [\u0000-\u007F];