/
Grammar.nqp
executable file
·229 lines (195 loc) · 6.78 KB
/
Grammar.nqp
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
use NQPHLL;
use QAST;
grammar QRegex::P6Regex::Grammar is HLL::Grammar {
method obs ($old, $new, $when = ' in Perl 6') {
self.panic('Unsupported use of ' ~ ~$old ~ ';'
~ ~$when ~ ' please use ' ~ ~$new);
}
token ws { [ \s+ | '#' \N* ]* }
token normspace { <?before \s | '#' > <.ws> }
token identifier { <.ident> [ <[\-']> <.ident> ]* }
token arg {
[
| <?[']> <quote_EXPR: ':q'>
| <?["]> <quote_EXPR: ':qq'>
| $<val>=[\d+]
]
}
rule arglist { <arg> [ ',' <arg>]* }
token TOP {
:my %*RX;
<nibbler>
[ $ || <.panic: 'Confused'> ]
}
token nibbler {
:my %*RX := pir::clone(pir::find_dynamic_lex__Ps('%*RX'));
[ <.ws> ['||'|'|'|'&&'|'&'] ]?
<termaltseq>
}
token termaltseq {
<termconjseq>
[ '||' [ <termconjseq> || <.panic: 'Null pattern not allowed'> ] ]*
}
token termconjseq {
<termalt>
[ '&&' [ <termalt> || <.panic: 'Null pattern not allowed'> ] ]*
}
token termalt {
<termconj>
[ '|' <![|]> [ <termconj> || <.panic: 'Null pattern not allowed'> ] ]*
}
token termconj {
<termish>
[ '&' <![&]> [ <termish> || <.panic: 'Null pattern not allowed'> ] ]*
}
token termish {
|| <noun=.quantified_atom>+
|| (\W) <.panic: "Unrecognized regex metacharacter (must be quoted to match literally)">
}
token quantified_atom {
<atom>
[
<.ws> [ <quantifier> | <?before ':'> <backmod> <!alpha> ]
[ <.ws> <separator> ]?
]?
}
token separator {
$<septype>=['%''%'?] <normspace>? <quantified_atom>
}
token atom {
# :dba('regex atom')
[
| \w [ \w+! <?before \w> ]?
| <metachar>
]
}
proto token quantifier { <...> }
token quantifier:sym<*> { <sym> <backmod> }
token quantifier:sym<+> { <sym> <backmod> }
token quantifier:sym<?> { <sym> <backmod> }
token quantifier:sym<{N,M}> { {} '{' (\d+) (','?) (\d*) '}'
<.obs: '{N,M} as general quantifier', '** N..M (or ** N..*)'>
}
token quantifier:sym<**> {
<sym> <normspace>? <backmod> <normspace>?
[
|| $<min>=[\d+]
[ '..'
$<max>=[
|| \d+
|| '*'
|| <.panic: "Only integers or '*' allowed as range quantifier endpoint">
]
]?
|| <quantified_atom>
]
}
token backmod { ':'? [ '?' | '!' | <!before ':'> ] }
proto token metachar { <...> }
token metachar:sym<ws> { <.normspace> }
token metachar:sym<[ ]> { '[' <nibbler> ']' }
token metachar:sym<( )> { '(' <nibbler> ')' }
token metachar:sym<'> { <?[']> <quote_EXPR: ':q'> }
token metachar:sym<"> { <?["]> <quote_EXPR: ':qq'> }
token metachar:sym<.> { <sym> }
token metachar:sym<^> { <sym> }
token metachar:sym<^^> { <sym> }
token metachar:sym<$> { <sym> }
token metachar:sym<$$> { <sym> }
token metachar:sym<:::> { <sym> <.panic: '::: not yet implemented'> }
token metachar:sym<::> { <sym> <.panic: ':: not yet implemented'> }
token metachar:sym<lwb> { $<sym>=['<<'|'«'] }
token metachar:sym<rwb> { $<sym>=['>>'|'»'] }
token metachar:sym<bs> { \\ <backslash> }
token metachar:sym<mod> { <mod_internal> }
token metachar:sym<quantifier> {
<quantifier> <.panic: 'Quantifier quantifies nothing'>
}
## we cheat here, really should be regex_infix:sym<~>
token metachar:sym<~> {
<sym>
<.ws> <GOAL=.quantified_atom>
<.ws> <EXPR=.quantified_atom>
}
token metachar:sym<{*}> {
<sym>
[ \h* '#= ' \h* $<key>=[\S+ [\h+ \S+]*] ]?
}
token metachar:sym<assert> {
'<' <assertion>
[ '>' || <.panic: 'regex assertion not terminated by angle bracket'> ]
}
token metachar:sym<var> {
[
| '$<' $<name>=[<-[>]>+] '>'
| '$' $<pos>=[\d+]
]
[ <.ws> '=' <.ws> <quantified_atom> ]?
}
token metachar:sym<PIR> {
':PIR{{' $<pir>=[.*?] '}}'
}
proto token backslash { <...> }
token backslash:sym<s> { $<sym>=[<[dDnNsSwW]>] }
token backslash:sym<b> { $<sym>=[<[bB]>] }
token backslash:sym<e> { $<sym>=[<[eE]>] }
token backslash:sym<f> { $<sym>=[<[fF]>] }
token backslash:sym<h> { $<sym>=[<[hH]>] }
token backslash:sym<r> { $<sym>=[<[rR]>] }
token backslash:sym<t> { $<sym>=[<[tT]>] }
token backslash:sym<v> { $<sym>=[<[vV]>] }
token backslash:sym<o> { $<sym>=[<[oO]>] [ <octint> | '[' <octints> ']' ] }
token backslash:sym<x> { $<sym>=[<[xX]>] [ <hexint> | '[' <hexints> ']' ] }
token backslash:sym<c> { $<sym>=[<[cC]>] <charspec> }
token backslash:sym<A> { 'A' <.obs: '\\A as beginning-of-string matcher', '^'> }
token backslash:sym<z> { 'z' <.obs: '\\z as end-of-string matcher', '$'> }
token backslash:sym<Z> { 'Z' <.obs: '\\Z as end-of-string matcher', '\\n?$'> }
token backslash:sym<Q> { 'Q' <.obs: '\\Q as quotemeta', 'quotes or literal variable match'> }
token backslash:sym<unrec> { {} \w <.panic: 'Unrecognized backslash sequence'> }
token backslash:sym<misc> { \W }
proto token assertion { <...> }
token assertion:sym<?> { '?' [ <?before '>' > | <assertion> ] }
token assertion:sym<!> { '!' [ <?before '>' > | <assertion> ] }
token assertion:sym<method> {
'.' <assertion>
}
token assertion:sym<name> {
<longname=.identifier>
[
| <?before '>'>
| '=' <assertion>
| ':' <arglist>
| '(' <arglist> ')'
| <.normspace> <nibbler>
]?
}
token assertion:sym<[> { <?before '['|'+'|'-'|':'> <cclass_elem>+ }
token cclass_elem {
$<sign>=['+'|'-'|<?>]
<.normspace>?
[
| '[' $<charspec>=(
|| \s* '-' <!before \s* ']'> <.obs: '- as character range','.. for range, for explicit - in character class, escape it or place as last thing'>
|| \s* ( '\\' <backslash> || (<-[\]\\]>) )
[
\s* '..' \s*
( '\\' <backslash> || (<-[\]\\]>) )
]?
)*
\s* ']'
| $<name>=[\w+]
| ':' $<invert>=['!'|<?>] $<uniprop>=[\w+]
]
<.normspace>?
}
token mod_internal {
[
| ':' $<n>=('!' | \d+)**1 <mod_ident> »
| ':' <mod_ident> [ '(' $<n>=[\d+] ')' ]?
]
}
proto token mod_ident { <...> }
token mod_ident:sym<ignorecase> { $<sym>=[i] 'gnorecase'? }
token mod_ident:sym<ratchet> { $<sym>=[r] 'atchet'? }
token mod_ident:sym<sigspace> { $<sym>=[s] 'igspace'? }
}