-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathRegularExpressions.mc4
More file actions
453 lines (374 loc) · 14.1 KB
/
Copy pathRegularExpressions.mc4
File metadata and controls
453 lines (374 loc) · 14.1 KB
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
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.regex;
/* This is a MontiCore stable grammar.
* Adaptations -- if any -- are conservative. */
import de.monticore.literals.*;
/**
* This grammar defines regular expressions over UTF characters, such as
* a*b+a?cc
* [A-F0-9]{4,6}
* Hello_(World|Tom)
*
* The syntax used for this grammar is modeled after the syntax used
* in Java, see e.g.
* https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
*
* The grammar conforms to the specification at
* https://en.wikipedia.org/wiki/Regular_expression#Formal_language_theory
*
* A detailed explanation of regular expressions can be found at
* https://en.wikipedia.org/wiki/Regular_expression
*
* The grammar can be used in isolated form, but usually is embedded
* in DSLs where a regular expression is of use, e.g. when
* describing allowed input pattern.
*
* This grammar only relies on MontiCores literals.
* Although nonterminals are called "RegularExpression", they are
* defined independently of the expression grammars of MontiCore.
*
* The grammar uses a special mode, namely REGEX.
* This mode is needed, because the parsing of regular expressions
* and especially their tokens significantly differs from the normal
* forms of tokens.
*
* The nonterminal RegularExpression is designed in such a way that
* it assumes that the mode REGEX has already been switched on
* (and will be switched of) by the calling nonterminal.
* This functionality is provided by the nonterminal RegExLiteral.
*
*/
component grammar RegularExpressions extends MCCommonLiterals {
/*=================================================================*/
/*================== REGULAR EXPRESSION ===========================*/
/*=================================================================*/
/**
* Represents a regular expression. "left" and "right" are only present
* if regular expression comprises two parts split by a pipe. Otherwise, the
* regular expression is made up of (non-empty) items.
* Syntax e.g.: R1|R2 , R1R2R3
* Matches e.g.: alternatives and sequences
*/
RegularExpression =
left:RegularExpression Pipe right:RegularExpression
| RegExItem+
;
RegExLiteral = RegExStartToken RegularExpression RegExEndToken;
token RegExStartToken = 'R' '"' : -> pushMode(REGEX);
token RegExEndToken(REGEX) = '"' : -> popMode;
/*=================================================================*/
/*========================= ITEM ==================================*/
/*=================================================================*/
/**
* Interface for every character or symbol which can occur inside of
* a regular expression.
*/
interface RegExItem;
/*=================================================================*/
/*=================== BRACKET ITEMS [...] =========================*/
/*=================================================================*/
/**
* This represents an (inversed) bracket expression. A single character
* contained in the brackets is matched when evaluating the regular
* expression.
* Syntax e.g.: [R], [^R]
* Matches e.g.: a single character (not) contained in R
*/
BracketRegEx implements RegExItem =
LBrack Roof? BracketRegExItem* RBrack;
/**
* Interface for every character or symbol which can occur inside of a
* bracket expression.
*/
interface BracketRegExItem;
/**
* Represents a normal character used in a bracket expression
*/
CharOption implements BracketRegExItem =
CharacterInCharacterElement;
/**
* SpecialCharOption represents a single character
* of the specials characters, which are usable in a bracket expression.
*
* Remark:
* This nonterminal is needed because the token
* CharacterInCharacterElement does not match special characters used
* in other places of this grammar, as they have a higher priority than
* the CharacterInCharacterElement token in the lexing process.
*/
SpecialCharOption implements BracketRegExItem =
Point | Star | Plus | Comma | Roof | Pipe |
Backslash | LCurly | RCurly | LBrack | Dollar |
LParen | RParen | Question | SingleDigit;
/**
* Represents a range of characters or numbers.
*
* These are only characters and digits.
*/
CharRange implements BracketRegExItem = Range;
/*=================================================================*/
/*=================== CAPTURING GROUPS ============================*/
/*=================================================================*/
/**
* Bundles a part of a regular expression in parentheses to (optionally) use
* it later in the same expression or to replace it with a new String.
* Syntax : (R)
* Matches : R and allows to reuse it later in the
* regex via \n
*/
CapturingGroup implements RegExItem =
LParen RegularExpression RParen;
/*=================================================================*/
/**
* Bundles a part of a regular expression in parentheses to (optionally) use
* it later in the same expression or to replace it with a new String.
* Giving the capturing group a name fosters its reusability.
* Syntax : (?<name>R)
* Matches : R and allows to reuse it later via \k<name>
*/
symbol NamedCapturingGroup implements RegExItem =
NamedCapturingGroupStart Name NamedCapturingGroupEnd
RegularExpression
RParen;
/**
* Indicates the start of the name of a capturing group. Switches the lexer
* mode to recognize a consecutive Name token.
*/
token NamedCapturingGroupStart(REGEX) = "(?<" : -> pushMode(DEFAULT_MODE);
/**
* Indicates the end of the name of a capturing group. Switches the lexer
* mode to recognize consecutive regular expression tokens.
*/
token NamedCapturingGroupEnd = ">" : -> popMode;
/*=================================================================*/
/**
* Bundles a part of a regular expression that does not need to be reused
* anywhere else, i.e. this a normal parentheses.
* Syntax : (?:R)
* Matches : R
*/
NonCapturingGroup implements RegExItem =
NonCapturingGroupStart RegularExpression RParen;
/**
* Indicates the start of a non-capturing group.
*/
token NonCapturingGroupStart(REGEX) = "(?:";
/*=================================================================*/
/**
* Refers to a capturing group.
* Syntax e.g.: \0, \9, \k<name>
* Matches : what has been defined in a group before
*/
BackReference implements RegExItem =
Backslash (SingleDigit |
BackReferenceStart Name@NamedCapturingGroup NamedCapturingGroupEnd);
/**
* Indicates the start of the name of a capturing group. Switches
* the lexer mode to recognize a consecutive Name token.
*/
token BackReferenceStart(REGEX) = "k<" : -> pushMode(DEFAULT_MODE);
/*=================================================================*/
/*================== ITEM (cont.) =================================*/
/*=================================================================*/
/**
* Represents a character used in a regular expression.
* Syntax : a, b, Z, etc.
* Matches : a, b, Z, etc.
*/
RegExChar implements RegExItem =
CharacterInCharacterElement;
/**
* Represents a point in a regular expression which matches any character.
* Syntax : .
* Matches : a, §, 5, etc.
*/
RegExPoint implements RegExItem = Point;
/**
* Represents a single digit used in a regular expression.
* Syntax : 0, 1, 9, etc.
* Matches : 0, 1, 9, etc.
*/
RegExDigit implements RegExItem = SingleDigit;
/**
* Indicates that the consecutive regular expression should start
* with a new line.
* Syntax : ^R
* Matches : newline|linefeed and then R
*/
StartOfLine implements RegExItem = Roof RegularExpression;
/**
* This allows to qualify an item with an allowed quantity or
* marks it as the end of a line.
* Syntax : R?, R+, R*, R$
* R{3,5}, R{4}, R{3,}, R{,5}
* Matches : R in the appropriate quantity,
* or R before the end of a line with R$
*/
QualifiedRegEx implements RegExItem =
RegExItem Qualification;
/*=================================================================*/
/*==================== QUALIFICATION ==============================*/
/*=================================================================*/
/**
* Interface for the qualifications allowed in regular expressions.
*/
interface Qualification;
/**
* All simple qualifications which are allowed after
* a regular expression
*/
RegExQualification implements Qualification =
Star | Plus | Question | Dollar;
/**
* ASTRangeQualification can be used after an item in a regular
* expression if the item should be matched a specified amount of times.
*/
RangeQualification implements Qualification =
LCurly (lowerBound:SingleDigit+)? Comma (upperBound:SingleDigit+)? RCurly;
/**
* ASTNumberQualification can be used after an item in a regular
* expression if the item should be matched a specified amount of times.
*/
NumberQualification implements Qualification = LCurly SingleDigit+ RCurly;
/*=================================================================*/
/*==================== ESCAPES FOR SINGLE CHARACTERS ==============*/
/*=================================================================*/
/**
* ASTEscapeChar is an interface for all character descriptions using
* an "\"-operator as escape.
* Syntax : \p{ASCII}, \p{Cntrl}, \w, \W, \B, \S, ...
* Matches : single characters
*/
interface EscapeChar extends RegExItem;
/*=================================================================*/
/**
* ASTSpecificChars allows to specify \p - escapes, matching a
* single character withing a named (and thus predefined group):
* Syntax : \p{ASCII}, \p{Cntrl}, ...
* Matches : single characters
*
* The arguments usable for \p are:
* ASCII, Alnum, Alpha, Blank, Cntrl, Digit, Graph, Lower, Print,
* Punct, Space, Upper, XDigit
*/
SpecificChars implements EscapeChar =
SpecificCharsStart SpecificCharsName;
/** See SpecificChars nonterminal */
token SpecificCharsStart(REGEX) = "\\p" : -> pushMode(DEFAULT_MODE);
/** See SpecificChars nonterminal */
token SpecificCharsName = "{" Name "}" : -> popMode;
/*=================================================================*/
/*==================== SIMPLE MATCHERS ============================*/
/*=================================================================*/
/**
* ASTRegExEscapeChar allows to specify all escapes with no arguments,
* Syntax : \w, \W, \B, \S, ...
* Matches e.g.: single characters
*/
RegExEscapeChar implements EscapeChar =
AlphaNumCharsWithUnderscoreToken
| NonWordCharsToken
| WordBoundariesToken
| NonWordBoundariesToken
| DigitCharsToken
| NonDigitCharsToken
| WhitespaceCharsToken
| NonWhitespaceCharsToken
| Backslash;
/**
* Matches all alphanumeric characters as well as "_".
* Syntax : \w
* Matches e.g.: a, z, A, Z, 0, 9, _
*/
token AlphaNumCharsWithUnderscoreToken(REGEX) = "\\w";
/**
* Matches all non-alphanumeric characters except "_".
* Syntax : \W
* Matches e.g.: %, (, $, Space, Linefeed, etc.
*/
token NonWordCharsToken(REGEX) = "\\W";
/**
* Matches all word boundaries.
* A word boundary is not a character but a
* position between two characters.
* Syntax : \b
* Matches e.g.: |Hello| |World|
* ( where "|" indicates word boundary)
*/
token WordBoundariesToken(REGEX) = "\\b";
/**
* Matches all non-word boundaries.
* A non-word boundary is not a character but a
* position between two characters.
* Syntax : \B
* Matches e.g.: H|e|l|l|o W|o|r|l|d
* ( where "|" indicates non word boundary)
*/
token NonWordBoundariesToken(REGEX) = "\\B";
/**
* Matches all digits.
* Syntax : \s
* Matches e.g.: 0, 4, 9
*/
token DigitCharsToken(REGEX) = "\\d";
/**
* Matches everything except digits.
* Syntax : \D
* Matches e.g.: _, a
*/
token NonDigitCharsToken(REGEX) = "\\D";
/**
* Matches all whitespace characters.
* Syntax : \s
* Matches e.g.: Space, Linefeed, Newline, Tab
*/
token WhitespaceCharsToken(REGEX) = "\\s";
/**
* Matches everything except whitespace characters.
* Syntax : \S
* Matches e.g.: 0, _, a
*/
token NonWhitespaceCharsToken(REGEX) = "\\S";
/*=================================================================*/
/*=============== CORE TOKENS FOR REGEX CONTROL ===================*/
/*=================================================================*/
/**
* SingleDigit denotes a single digit
*/
token SingleDigit(REGEX) = Digit;
/**
* CharacterInCharacterElement describes any single character, except
* the closing ')'.
* Syntax e.g.: g, 0, /
* Matches e.g.: g, 0, / (i.e. itself)
*/
token CharacterInCharacterElement(REGEX) = . ;
/**
* Range is used to parse a range of characters or numbers.
* Syntax e.g.: g-j, 0-9
* Matches e.g.: a, 0, F
*/
token Range(REGEX) =
('a' .. 'z' | 'A' .. 'Z' | '0' .. '9') '-'
('a' .. 'z' | 'A' .. 'Z' | '0' .. '9');
/**
* The following tokens are defined in this grammar to make
* them accessible in the "REGEX" grammar mode.
*
*/
token Pipe(REGEX) = "|";
token LBrack(REGEX) = "[";
token RBrack(REGEX) = "]";
token Roof(REGEX) = "^";
token RParen(REGEX) = ")";
token LParen(REGEX) = "(";
token Point(REGEX) = ".";
token Backslash(REGEX) = "\\";
token Star(REGEX) = "*";
token Plus(REGEX) = "+";
token Question(REGEX) = "?";
token Comma(REGEX) = ",";
token Dollar(REGEX) = "$";
token RCurly(REGEX) = "}";
token LCurly(REGEX) = "{";
}