-
Notifications
You must be signed in to change notification settings - Fork 138
/
P5Regex.pir
414 lines (366 loc) · 10.6 KB
/
P5Regex.pir
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
# Copyright (C) 2005-2009, Parrot Foundation.
.namespace [ "PGE";"P5Regex" ]
.sub "compile_p5regex"
.param pmc source
.param pmc adverbs :slurpy :named
$I0 = exists adverbs['grammar']
if $I0 goto have_grammar
adverbs['grammar'] = 'PGE::Grammar'
have_grammar:
.local string target
target = adverbs['target']
target = downcase target
## If we're passed the results of a previous parse, use it.
.local pmc match
$I0 = isa source, ['PGE';'Match']
if $I0 == 0 goto parse
$P0 = source['expr']
if null $P0 goto parse
$I0 = isa $P0, ['PGE';'Exp']
if $I0 == 0 goto parse
match = source
goto analyze
parse:
$P0 = get_global "p5regex"
match = $P0(source, adverbs :flat :named)
if target != 'parse' goto check
.return (match)
check:
unless match goto check_1
$S0 = source
$S1 = match
if $S0 == $S1 goto analyze
check_1:
null $P0
.return ($P0)
analyze:
.local pmc exp, pad
exp = match['expr']
pad = new 'Hash'
pad['subpats'] = 0
exp = exp.'p5analyze'(pad)
.tailcall exp.'compile'(adverbs :flat :named)
.end
.sub "p5regex"
.param pmc mob
.param pmc adverbs :slurpy :named
.local string stop, tighter
.local pmc stopstack, optable
stopstack = get_hll_global ['PGE';'P5Regex'], '@!stopstack'
optable = get_hll_global ["PGE";"P5Regex"], "$optable"
stop = adverbs['stop']
tighter = adverbs['tighter']
push stopstack, stop
$P0 = optable."parse"(mob, 'stop'=>stop, 'tighter'=>tighter)
$S0 = pop stopstack
.return ($P0)
.end
.include "cclass.pasm"
.const int PGE_INF = 2147483647
.sub "__onload" :load
.local pmc optable
optable = new ['PGE';'OPTable']
set_hll_global ["PGE";"P5Regex"], "$optable", optable
$P0 = get_hll_global ["PGE";"P5Regex"], "parse_lit"
optable.'newtok'('term:', 'precedence'=>'=', 'nows'=>1, 'parsed'=>$P0)
optable.'newtok'('term:\b', 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::Anchor')
optable.'newtok'('term:\B', 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::Anchor')
optable.'newtok'('term:^', 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::Anchor')
optable.'newtok'('term:$', 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::Anchor')
optable.'newtok'('term:\d', 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::CCShortcut')
optable.'newtok'('term:\D', 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::CCShortcut')
optable.'newtok'('term:\s', 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::CCShortcut')
optable.'newtok'('term:\S', 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::CCShortcut')
optable.'newtok'('term:\w', 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::CCShortcut')
optable.'newtok'('term:\W', 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::CCShortcut')
optable.'newtok'('circumfix:( )', 'equiv'=>'term:', 'nows'=>1, 'nullterm'=>1, 'match'=>'PGE::Exp::CGroup')
optable.'newtok'('circumfix:(?: )', 'equiv'=>'term:', 'nows'=>1, 'nullterm'=>1, 'match'=>'PGE::Exp::Group')
$P0 = get_hll_global ['PGE';'P5Regex'], 'parse_enumclass'
optable.'newtok'('term:[', 'precedence'=>'=', 'nows'=>1, 'parsed'=>$P0)
optable.'newtok'('term:.', 'precedence'=>'=', 'nows'=>1, 'parsed'=>$P0)
$P0 = get_hll_global ['PGE';'P5Regex'], 'parse_quant'
optable.'newtok'('postfix:*', 'looser'=>'term:', 'left'=>1, 'nows'=>1, 'parsed'=>$P0)
optable.'newtok'('postfix:+', 'equiv'=>'postfix:*', 'left'=>1, 'nows'=>1, 'parsed'=>$P0)
optable.'newtok'('postfix:?', 'equiv'=>'postfix:*', 'left'=>1, 'nows'=>1, 'parsed'=>$P0)
optable.'newtok'('postfix:{', 'equiv'=>'postfix:*', 'left'=>1, 'nows'=>1, 'parsed'=>$P0)
optable.'newtok'('infix:', 'looser'=>'postfix:*', 'right'=>1, 'nows'=>1, 'match'=>'PGE::Exp::Concat')
optable.'newtok'('infix:|', 'looser'=>'infix:', 'left'=>1, 'nows'=>1, 'match'=>'PGE::Exp::Alt')
optable.'newtok'('close:}', 'looser'=>'infix:|', 'nows'=>1) # XXX: hack
# Create a stack for holding stop tokens
$P0 = new 'ResizablePMCArray'
set_hll_global ['PGE';'P5Regex'], '@!stopstack', $P0
$P0 = get_hll_global ["PGE";"P5Regex"], "compile_p5regex"
compreg "PGE::P5Regex", $P0
.end
.sub 'parse_error'
.param pmc mob
.param int pos
.param string message
$P0 = getattribute mob, '$.pos'
$P0 = pos
$P0 = new 'Exception'
$S0 = 'p5regex parse error: '
$S0 .= message
$S0 .= ' at offset '
$S1 = pos
$S0 .= $S1
$S0 .= ", found '"
$P1 = getattribute mob, '$.target'
$S1 = $P1
$S1 = substr $S1, pos, 1
$S0 .= $S1
$S0 .= "'"
$P0 = $S0
throw $P0
.return ()
.end
.sub "parse_lit"
.param pmc mob
.param pmc adverbs :slurpy :named
.local string target
.local int pos, lastpos
.local int litstart, litlen
.local string initchar
(mob, pos, target) = mob.'new'(mob, 'grammar'=>'PGE::Exp::Literal')
lastpos = length target
initchar = substr target, pos, 1
unless initchar == '*' goto initchar_ok
parse_error(mob, pos, "Quantifier follows nothing")
initchar_ok:
if initchar == ')' goto end
inc pos
if initchar != "\\" goto term_literal
term_backslash:
initchar = substr target, pos, 1
inc pos
if pos <= lastpos goto term_backslash_ok
parse_error(mob, pos, "Search pattern not terminated")
term_backslash_ok:
$I0 = index "nrteab", initchar
if $I0 < 0 goto term_literal
initchar = substr "\n\r\t\e\a\b", $I0, 1
term_literal:
litstart = pos
litlen = 0
.local string stop
.local int stoplen
$P0 = get_hll_global ['PGE';'P5Regex'], '@!stopstack'
stop = $P0[-1]
stoplen = length stop
term_literal_loop:
if pos >= lastpos goto term_literal_end
if stoplen == 0 goto not_stop
$S0 = substr target, pos, stoplen
if $S0 == stop goto term_literal_end
not_stop:
$S0 = substr target, pos, 1
$I0 = index "[](){}*?+\\|^$.", $S0
# if not in circumfix:( ) throw error on end paren
if $I0 >= 0 goto term_literal_end
inc pos
inc litlen
goto term_literal_loop
term_literal_end:
if litlen < 1 goto term_literal_one
dec pos
term_literal_one:
$I0 = pos - litstart
$S0 = substr target, litstart, $I0
$S0 = concat initchar, $S0
mob.'!make'($S0)
goto end
end:
mob.'to'(pos)
.return (mob)
.end
.sub "parse_quant"
.param pmc mob
.param pmc adverbs :slurpy :named
.local string target
.local int min, max, backtrack
.local int pos, lastpos
.local string key
key = mob['KEY']
(mob, pos, target) = mob.'new'(mob, 'grammar'=>'PGE::Exp::Quant')
lastpos = length target
min = 0
max = PGE_INF
backtrack = 0
if key == '{' goto quant_range
if key != '+' goto quant_max
min = 1
quant_max:
if key != "?" goto quant_lazy
max = 1
goto quant_lazy
quant_range:
$I1 = find_not_cclass .CCLASS_NUMERIC, target, pos, lastpos
if $I1 <= pos goto quant_range_max
$S0 = substr target, pos
min = $S0
max = $S0
pos = $I1
quant_range_max:
$S0 = substr target, pos, 1
if $S0 != "," goto quant_range_end
inc pos
max = PGE_INF
$I1 = find_not_cclass .CCLASS_NUMERIC, target, pos, lastpos
if $I1 <= pos goto quant_range_end
$S0 = substr target, pos
max = $S0
pos = $I1
quant_range_end:
$S0 = substr target, pos, 1
if $S0 != "}" goto err_range
inc pos
quant_lazy:
$S0 = substr target, pos, 1
if $S0 != "?" goto end
backtrack = PGE_BACKTRACK_EAGER
inc pos
end:
mob["min"] = min
mob["max"] = max
mob["backtrack"] = backtrack
mob.'to'(pos)
.return (mob)
err_range:
parse_error(mob, pos, "Error in quantified range")
.end
.sub parse_group
.param pmc mob
.param pmc adverbs :slurpy :named
.local string target
.local int pos, lastpos
(mob, pos, target) = mob.'new'(mob, 'grammar'=>'PGE::Exp::CGroup')
inc pos
$S0 = substr target, pos, 2
if $S0 == "?:" goto nocapture
goto end
nocapture:
pos += 2
end:
mob.'to'(pos)
.return (mob)
.end
.sub "parse_enumclass"
.param pmc mob
.param pmc adverbs :slurpy :named
.local string target
.local int pos, lastpos
.local int isrange
.local string charlist
.local string key
key = mob['KEY']
(mob, pos, target) = mob.'new'(mob, 'grammar'=>'PGE::Exp::EnumCharList')
if key == '.' goto dot
lastpos = length target
charlist = ""
mob["isnegated"] = 0
isrange = 0
$S0 = substr target, pos, 1
if $S0 != "^" goto scan_first
mob["isnegated"] = 1
inc pos
scan_first:
if pos >= lastpos goto err_close
$S0 = substr target, pos, 1
inc pos
if $S0 == "\\" goto backslash
goto addchar
scan:
if pos >= lastpos goto err_close
$S0 = substr target, pos, 1
inc pos
if $S0 == "]" goto endclass
if $S0 == "-" goto hyphenrange
if $S0 != "\\" goto addchar
backslash:
$S0 = substr target, pos, 1
inc pos
$I0 = index "nrtfae0b", $S0
if $I0 == -1 goto addchar
$S0 = substr "\n\r\t\f\a\e\0\b", $I0, 1
addchar:
if isrange goto addrange
charlist .= $S0
goto scan
addrange:
isrange = 0
$I2 = ord charlist, -1
$I0 = ord $S0
if $I0 < $I2 goto err_range
addrange_1:
inc $I2
if $I2 > $I0 goto scan
$S1 = chr $I2
charlist .= $S1
goto addrange_1
hyphenrange:
if isrange goto addrange
isrange = 1
goto scan
endclass:
if isrange == 0 goto end
charlist .= "-"
goto end
dot:
charlist = "\n"
mob["isnegated"] = 1
end:
mob.'to'(pos)
mob.'!make'(charlist)
.return (mob)
err_close:
parse_error(mob, pos, "Unmatched [")
err_range:
$S0 = 'Invalid [] range "'
$S1 = chr $I2
$S0 .= $S1
$S0 .= '-'
$S1 = chr $I0
$S0 .= $S1
$S0 .= '"'
parse_error(mob, pos, $S0)
.end
.namespace [ "PGE";"Exp" ]
.sub "p5analyze" :method
.param pmc pad
.local pmc exp
$I0 = 0
loop:
$I1 = defined self[$I0]
if $I1 == 0 goto end
$P0 = self[$I0]
$P0 = $P0."p5analyze"(pad)
self[$I0] = $P0
inc $I0
goto loop
end:
.return (self)
.end
.namespace [ "PGE";"Exp";"CGroup" ]
.sub "p5analyze" :method
.param pmc pad
.local pmc exp
self["iscapture"] = 0
if self != "(" goto end
self["iscapture"] = 1
self["isscope"] = 0
self["isarray"] = 0
$I0 = pad["subpats"]
self["cname"] = $I0
inc $I0
pad["subpats"] = $I0
end:
exp = self[0]
exp = exp."p5analyze"(pad)
self[0] = exp
.return (self)
.end
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir: