Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 376 lines (332 sloc) 9.58 kb
9cbdcbc @pmichaud Merge pge-pm branch with updated PGE into trunk.
pmichaud authored
1
2
1ebf2db @tene Merge hllmagic branch into trunk.
tene authored
3 .namespace [ "PGE";"P5Regex" ]
9cbdcbc @pmichaud Merge pge-pm branch with updated PGE into trunk.
pmichaud authored
4
5 .sub "compile_p5regex"
6 .param pmc source
7 .param pmc adverbs :slurpy :named
8
9 $I0 = exists adverbs['grammar']
8e57fe8 @allisonrandal [pdd15oo] Merging the pdd15oo branch at r22176 back into trunk.
allisonrandal authored
10 if $I0 goto have_grammar
1c757c9 @pmichaud [PGE]:
pmichaud authored
11 adverbs['grammar'] = 'PGE::Grammar'
8e57fe8 @allisonrandal [pdd15oo] Merging the pdd15oo branch at r22176 back into trunk.
allisonrandal authored
12 have_grammar:
9cbdcbc @pmichaud Merge pge-pm branch with updated PGE into trunk.
pmichaud authored
13
14 .local string target
15 target = adverbs['target']
e1f7b0c @pmichaud [PGE]:
pmichaud authored
16 target = downcase target
9cbdcbc @pmichaud Merge pge-pm branch with updated PGE into trunk.
pmichaud authored
17
18 .local pmc match
e1f7b0c @pmichaud [PGE]:
pmichaud authored
19 $P0 = get_global "p5regex"
9cbdcbc @pmichaud Merge pge-pm branch with updated PGE into trunk.
pmichaud authored
20 match = $P0(source)
21 if target != 'parse' goto check
22 .return (match)
23
24 check:
25 unless match goto check_1
26 $S0 = source
27 $S1 = match
28 if $S0 == $S1 goto analyze
29 check_1:
30 null $P0
31 .return ($P0)
32
33 analyze:
34 .local pmc exp, pad
35 exp = match['expr']
b3ef02f @pmichaud [pge]:
pmichaud authored
36 pad = new 'Hash'
9cbdcbc @pmichaud Merge pge-pm branch with updated PGE into trunk.
pmichaud authored
37 pad['subpats'] = 0
38 exp = exp.'p5analyze'(pad)
f7e5e32 @coke RT #58974 - .return is deprecated when .tailcall could be used.
coke authored
39 .tailcall exp.'compile'(adverbs :flat :named)
9cbdcbc @pmichaud Merge pge-pm branch with updated PGE into trunk.
pmichaud authored
40 .end
41
42
43 .sub "p5regex"
44 .param pmc mob
45 .local pmc optable
1ebf2db @tene Merge hllmagic branch into trunk.
tene authored
46 optable = get_hll_global ["PGE";"P5Regex"], "$optable"
9cbdcbc @pmichaud Merge pge-pm branch with updated PGE into trunk.
pmichaud authored
47 $P0 = optable."parse"(mob)
48 .return ($P0)
49 .end
50
51
c346e6b @pmichaud * Added initial version of compreg "PGE::P5Regexp"
pmichaud authored
52 .include "cclass.pasm"
53
54 .const int PGE_INF = 2147483647
55
56 .sub "__onload" :load
57 .local pmc optable
58
1ebf2db @tene Merge hllmagic branch into trunk.
tene authored
59 optable = new ['PGE';'OPTable']
60 set_hll_global ["PGE";"P5Regex"], "$optable", optable
c346e6b @pmichaud * Added initial version of compreg "PGE::P5Regexp"
pmichaud authored
61
1ebf2db @tene Merge hllmagic branch into trunk.
tene authored
62 $P0 = get_hll_global ["PGE";"P5Regex"], "parse_lit"
0700c33 @particle [PGE]: convert deprecated 'addtok' calls to 'newtok'
particle authored
63 optable.newtok('term:', 'precedence'=>'=', 'nows'=>1, 'parsed'=>$P0)
64
c82c9aa @pmichaud [pge]:
pmichaud authored
65 optable.newtok('term:\b', 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::Anchor')
66 optable.newtok('term:\B', 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::Anchor')
0700c33 @particle [PGE]: convert deprecated 'addtok' calls to 'newtok'
particle authored
67 optable.newtok('term:^', 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::Anchor')
68 optable.newtok('term:$', 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::Anchor')
69
c82c9aa @pmichaud [pge]:
pmichaud authored
70 optable.newtok('term:\d', 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::CCShortcut')
71 optable.newtok('term:\D', 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::CCShortcut')
72 optable.newtok('term:\s', 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::CCShortcut')
73 optable.newtok('term:\S', 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::CCShortcut')
74 optable.newtok('term:\w', 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::CCShortcut')
75 optable.newtok('term:\W', 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::CCShortcut')
0700c33 @particle [PGE]: convert deprecated 'addtok' calls to 'newtok'
particle authored
76
77 optable.newtok('circumfix:( )', 'equiv'=>'term:', 'nows'=>1, 'nullterm'=>1, 'match'=>'PGE::Exp::CGroup')
78 optable.newtok('circumfix:(?: )', 'equiv'=>'term:', 'nows'=>1, 'nullterm'=>1, 'match'=>'PGE::Exp::Group')
79
1ebf2db @tene Merge hllmagic branch into trunk.
tene authored
80 $P0 = get_hll_global ['PGE';'P5Regex'], 'parse_enumclass'
0700c33 @particle [PGE]: convert deprecated 'addtok' calls to 'newtok'
particle authored
81 optable.newtok('term:[', 'precedence'=>'=', 'nows'=>1, 'parsed'=>$P0)
82 optable.newtok('term:.', 'precedence'=>'=', 'nows'=>1, 'parsed'=>$P0)
83
1ebf2db @tene Merge hllmagic branch into trunk.
tene authored
84 $P0 = get_hll_global ['PGE';'P5Regex'], 'parse_quant'
404a2a7 [callin convs] attempt to fix t/op/calling_95
Leopold Toetsch authored
85 optable.newtok('postfix:*', 'looser'=>'term:', 'left'=>1, 'nows'=>1, 'parsed'=>$P0)
0700c33 @particle [PGE]: convert deprecated 'addtok' calls to 'newtok'
particle authored
86 optable.newtok('postfix:+', 'equiv'=>'postfix:*', 'left'=>1, 'nows'=>1, 'parsed'=>$P0)
87 optable.newtok('postfix:?', 'equiv'=>'postfix:*', 'left'=>1, 'nows'=>1, 'parsed'=>$P0)
88 optable.newtok('postfix:{', 'equiv'=>'postfix:*', 'left'=>1, 'nows'=>1, 'parsed'=>$P0)
89
90 optable.newtok('infix:', 'looser'=>'postfix:*', 'right'=>1, 'nows'=>1, 'match'=>'PGE::Exp::Concat')
91 optable.newtok('infix:|', 'looser'=>'infix:', 'left'=>1, 'nows'=>1, 'match'=>'PGE::Exp::Alt')
92
93 optable.newtok('close:}', 'looser'=>'infix:|', 'nows'=>1) # XXX: hack
c346e6b @pmichaud * Added initial version of compreg "PGE::P5Regexp"
pmichaud authored
94
1ebf2db @tene Merge hllmagic branch into trunk.
tene authored
95 $P0 = get_hll_global ["PGE";"P5Regex"], "compile_p5regex"
9cbdcbc @pmichaud Merge pge-pm branch with updated PGE into trunk.
pmichaud authored
96 compreg "PGE::P5Regex", $P0
c346e6b @pmichaud * Added initial version of compreg "PGE::P5Regexp"
pmichaud authored
97 .end
98
99
1af8ba3 @particle [PGE]: #40106: [PATCH] 15 more tests work for PGE::P5Regex
particle authored
100 .sub 'parse_error'
101 .param pmc mob
102 .param int pos
103 .param string message
abb2d74 @particle [pge]: p5regex compiler now throws exception upon parse error
particle authored
104 $P0 = getattribute mob, '$.pos'
105 $P0 = pos
b3ef02f @pmichaud [pge]:
pmichaud authored
106 $P0 = new 'Exception'
abb2d74 @particle [pge]: p5regex compiler now throws exception upon parse error
particle authored
107 $S0 = 'p5regex parse error: '
108 $S0 .= message
109 $S0 .= ' at offset '
110 $S1 = pos
111 $S0 .= $S1
112 $S0 .= ", found '"
113 $P1 = getattribute mob, '$.target'
114 $S1 = $P1
115 $S1 = substr $S1, pos, 1
116 $S0 .= $S1
117 $S0 .= "'"
2255840 @allisonrandal [pdd17pmc] Merging the pdd25cx branch into trunk for r26555 to r29949.
allisonrandal authored
118 $P0 = $S0
abb2d74 @particle [pge]: p5regex compiler now throws exception upon parse error
particle authored
119 throw $P0
1af8ba3 @particle [PGE]: #40106: [PATCH] 15 more tests work for PGE::P5Regex
particle authored
120 .return ()
121 .end
122
123
c346e6b @pmichaud * Added initial version of compreg "PGE::P5Regexp"
pmichaud authored
124 .sub "parse_lit"
125 .param pmc mob
126 .local string target
127 .local int pos, lastpos
128 .local int litstart, litlen
129 .local string initchar
281fd4b @pmichaud [pge]:
pmichaud authored
130 (mob, pos, target) = mob.'new'(mob, 'grammar'=>'PGE::Exp::Literal')
c346e6b @pmichaud * Added initial version of compreg "PGE::P5Regexp"
pmichaud authored
131 lastpos = length target
132 initchar = substr target, pos, 1
1af8ba3 @particle [PGE]: #40106: [PATCH] 15 more tests work for PGE::P5Regex
particle authored
133 unless initchar == '*' goto initchar_ok
134 parse_error(mob, pos, "Quantifier follows nothing")
135
136 initchar_ok:
9cbdcbc @pmichaud Merge pge-pm branch with updated PGE into trunk.
pmichaud authored
137 if initchar == ')' goto end
c346e6b @pmichaud * Added initial version of compreg "PGE::P5Regexp"
pmichaud authored
138 inc pos
139 if initchar != "\\" goto term_literal
140 term_backslash:
141 initchar = substr target, pos, 1
142 inc pos
1af8ba3 @particle [PGE]: #40106: [PATCH] 15 more tests work for PGE::P5Regex
particle authored
143 if pos <= lastpos goto term_backslash_ok
144 parse_error(mob, pos, "Search pattern not terminated")
145 term_backslash_ok:
c346e6b @pmichaud * Added initial version of compreg "PGE::P5Regexp"
pmichaud authored
146 $I0 = index "nrteab", initchar
147 if $I0 < 0 goto term_literal
148 initchar = substr "\n\r\t\e\a\b", $I0, 1
149 term_literal:
150 litstart = pos
151 litlen = 0
152 term_literal_loop:
153 if pos >= lastpos goto term_literal_end
154 $S0 = substr target, pos, 1
155 $I0 = index "[](){}*?+\\|^$.", $S0
1af8ba3 @particle [PGE]: #40106: [PATCH] 15 more tests work for PGE::P5Regex
particle authored
156 # if not in circumfix:( ) throw error on end paren
c346e6b @pmichaud * Added initial version of compreg "PGE::P5Regexp"
pmichaud authored
157 if $I0 >= 0 goto term_literal_end
158 inc pos
159 inc litlen
160 goto term_literal_loop
161 term_literal_end:
162 if litlen < 1 goto term_literal_one
163 dec pos
164 term_literal_one:
165 $I0 = pos - litstart
166 $S0 = substr target, litstart, $I0
167 $S0 = concat initchar, $S0
19472de @pmichaud Change 'value' method to 'result_object' method, to address
pmichaud authored
168 mob.'result_object'($S0)
c346e6b @pmichaud * Added initial version of compreg "PGE::P5Regexp"
pmichaud authored
169 goto end
170 end:
281fd4b @pmichaud [pge]:
pmichaud authored
171 mob.'to'(pos)
c346e6b @pmichaud * Added initial version of compreg "PGE::P5Regexp"
pmichaud authored
172 .return (mob)
173 .end
174
175 .sub "parse_quant"
176 .param pmc mob
177 .local string target
9cbdcbc @pmichaud Merge pge-pm branch with updated PGE into trunk.
pmichaud authored
178 .local int min, max, backtrack
c346e6b @pmichaud * Added initial version of compreg "PGE::P5Regexp"
pmichaud authored
179 .local int pos, lastpos
9cbdcbc @pmichaud Merge pge-pm branch with updated PGE into trunk.
pmichaud authored
180 .local string key
181 key = mob['KEY']
281fd4b @pmichaud [pge]:
pmichaud authored
182 (mob, pos, target) = mob.'new'(mob, 'grammar'=>'PGE::Exp::Quant')
c346e6b @pmichaud * Added initial version of compreg "PGE::P5Regexp"
pmichaud authored
183 lastpos = length target
184 min = 0
185 max = PGE_INF
9cbdcbc @pmichaud Merge pge-pm branch with updated PGE into trunk.
pmichaud authored
186 backtrack = 0
187 if key == '{' goto quant_range
188 if key != '+' goto quant_max
c346e6b @pmichaud * Added initial version of compreg "PGE::P5Regexp"
pmichaud authored
189 min = 1
190 quant_max:
9cbdcbc @pmichaud Merge pge-pm branch with updated PGE into trunk.
pmichaud authored
191 if key != "?" goto quant_lazy
c346e6b @pmichaud * Added initial version of compreg "PGE::P5Regexp"
pmichaud authored
192 max = 1
193 goto quant_lazy
194 quant_range:
195 $I1 = find_not_cclass .CCLASS_NUMERIC, target, pos, lastpos
196 if $I1 <= pos goto quant_range_max
197 $S0 = substr target, pos
198 min = $S0
199 max = $S0
200 pos = $I1
201 quant_range_max:
202 $S0 = substr target, pos, 1
203 if $S0 != "," goto quant_range_end
204 inc pos
205 max = PGE_INF
206 $I1 = find_not_cclass .CCLASS_NUMERIC, target, pos, lastpos
207 if $I1 <= pos goto quant_range_end
208 $S0 = substr target, pos
209 max = $S0
210 pos = $I1
211 quant_range_end:
212 $S0 = substr target, pos, 1
213 if $S0 != "}" goto err_range
214 inc pos
215 quant_lazy:
216 $S0 = substr target, pos, 1
217 if $S0 != "?" goto end
9cbdcbc @pmichaud Merge pge-pm branch with updated PGE into trunk.
pmichaud authored
218 backtrack = PGE_BACKTRACK_EAGER
c346e6b @pmichaud * Added initial version of compreg "PGE::P5Regexp"
pmichaud authored
219 inc pos
220 end:
221 mob["min"] = min
222 mob["max"] = max
9cbdcbc @pmichaud Merge pge-pm branch with updated PGE into trunk.
pmichaud authored
223 mob["backtrack"] = backtrack
281fd4b @pmichaud [pge]:
pmichaud authored
224 mob.'to'(pos)
c346e6b @pmichaud * Added initial version of compreg "PGE::P5Regexp"
pmichaud authored
225 .return (mob)
226 err_range:
227 parse_error(mob, pos, "Error in quantified range")
228 .end
229
230
231 .sub parse_group
232 .param pmc mob
233 .local string target
234 .local int pos, lastpos
281fd4b @pmichaud [pge]:
pmichaud authored
235 (mob, pos, target) = mob.'new'(mob, 'grammar'=>'PGE::Exp::CGroup')
c346e6b @pmichaud * Added initial version of compreg "PGE::P5Regexp"
pmichaud authored
236 inc pos
237 $S0 = substr target, pos, 2
238 if $S0 == "?:" goto nocapture
239 goto end
240 nocapture:
241 pos += 2
242 end:
281fd4b @pmichaud [pge]:
pmichaud authored
243 mob.'to'(pos)
c346e6b @pmichaud * Added initial version of compreg "PGE::P5Regexp"
pmichaud authored
244 .return (mob)
245 .end
246
247 .sub "parse_enumclass"
248 .param pmc mob
249 .local string target
250 .local int pos, lastpos
251 .local int isrange
252 .local string charlist
9cbdcbc @pmichaud Merge pge-pm branch with updated PGE into trunk.
pmichaud authored
253 .local string key
254 key = mob['KEY']
281fd4b @pmichaud [pge]:
pmichaud authored
255 (mob, pos, target) = mob.'new'(mob, 'grammar'=>'PGE::Exp::EnumCharList')
9cbdcbc @pmichaud Merge pge-pm branch with updated PGE into trunk.
pmichaud authored
256 if key == '.' goto dot
c346e6b @pmichaud * Added initial version of compreg "PGE::P5Regexp"
pmichaud authored
257 lastpos = length target
258 charlist = ""
259 mob["isnegated"] = 0
260 isrange = 0
261 $S0 = substr target, pos, 1
9cbdcbc @pmichaud Merge pge-pm branch with updated PGE into trunk.
pmichaud authored
262 if $S0 != "^" goto scan_first
c346e6b @pmichaud * Added initial version of compreg "PGE::P5Regexp"
pmichaud authored
263 mob["isnegated"] = 1
264 inc pos
9cbdcbc @pmichaud Merge pge-pm branch with updated PGE into trunk.
pmichaud authored
265 scan_first:
266 if pos >= lastpos goto err_close
267 $S0 = substr target, pos, 1
268 inc pos
269 if $S0 == "\\" goto backslash
270 goto addchar
c346e6b @pmichaud * Added initial version of compreg "PGE::P5Regexp"
pmichaud authored
271 scan:
272 if pos >= lastpos goto err_close
273 $S0 = substr target, pos, 1
274 inc pos
275 if $S0 == "]" goto endclass
276 if $S0 == "-" goto hyphenrange
277 if $S0 != "\\" goto addchar
278 backslash:
279 $S0 = substr target, pos, 1
280 inc pos
281 $I0 = index "nrtfae0b", $S0
282 if $I0 == -1 goto addchar
283 $S0 = substr "\n\r\t\f\a\e\0\b", $I0, 1
284 addchar:
285 if isrange goto addrange
286 charlist .= $S0
287 goto scan
288 addrange:
289 isrange = 0
290 $I2 = ord charlist, -1
291 $I0 = ord $S0
1af8ba3 @particle [PGE]: #40106: [PATCH] 15 more tests work for PGE::P5Regex
particle authored
292 if $I0 < $I2 goto err_range
c346e6b @pmichaud * Added initial version of compreg "PGE::P5Regexp"
pmichaud authored
293 addrange_1:
294 inc $I2
295 if $I2 > $I0 goto scan
296 $S1 = chr $I2
297 charlist .= $S1
298 goto addrange_1
299 hyphenrange:
300 if isrange goto addrange
301 isrange = 1
302 goto scan
303 endclass:
304 if isrange == 0 goto end
305 charlist .= "-"
306 goto end
307 dot:
308 charlist = "\n"
309 mob["isnegated"] = 1
310 end:
281fd4b @pmichaud [pge]:
pmichaud authored
311 mob.'to'(pos)
19472de @pmichaud Change 'value' method to 'result_object' method, to address
pmichaud authored
312 mob.'result_object'(charlist)
c346e6b @pmichaud * Added initial version of compreg "PGE::P5Regexp"
pmichaud authored
313 .return (mob)
1af8ba3 @particle [PGE]: #40106: [PATCH] 15 more tests work for PGE::P5Regex
particle authored
314
c346e6b @pmichaud * Added initial version of compreg "PGE::P5Regexp"
pmichaud authored
315 err_close:
1af8ba3 @particle [PGE]: #40106: [PATCH] 15 more tests work for PGE::P5Regex
particle authored
316 parse_error(mob, pos, "Unmatched [")
317 err_range:
318 $S0 = 'Invalid [] range "'
319 $S1 = chr $I2
320 $S0 .= $S1
321 $S0 .= '-'
322 $S1 = chr $I0
323 $S0 .= $S1
324 $S0 .= '"'
325 parse_error(mob, pos, $S0)
c346e6b @pmichaud * Added initial version of compreg "PGE::P5Regexp"
pmichaud authored
326 .end
1af8ba3 @particle [PGE]: #40106: [PATCH] 15 more tests work for PGE::P5Regex
particle authored
327
c346e6b @pmichaud * Added initial version of compreg "PGE::P5Regexp"
pmichaud authored
328
1ebf2db @tene Merge hllmagic branch into trunk.
tene authored
329 .namespace [ "PGE";"Exp" ]
c346e6b @pmichaud * Added initial version of compreg "PGE::P5Regexp"
pmichaud authored
330
331 .sub "p5analyze" :method
332 .param pmc pad
333 .local pmc exp
334 $I0 = 0
335 loop:
336 $I1 = defined self[$I0]
9cbdcbc @pmichaud Merge pge-pm branch with updated PGE into trunk.
pmichaud authored
337 if $I1 == 0 goto end
c346e6b @pmichaud * Added initial version of compreg "PGE::P5Regexp"
pmichaud authored
338 $P0 = self[$I0]
339 $P0 = $P0."p5analyze"(pad)
340 self[$I0] = $P0
341 inc $I0
342 goto loop
343 end:
344 .return (self)
345 .end
346
1ebf2db @tene Merge hllmagic branch into trunk.
tene authored
347 .namespace [ "PGE";"Exp";"CGroup" ]
c346e6b @pmichaud * Added initial version of compreg "PGE::P5Regexp"
pmichaud authored
348
349 .sub "p5analyze" :method
350 .param pmc pad
351 .local pmc exp
352
353 self["iscapture"] = 0
354 if self != "(" goto end
355 self["iscapture"] = 1
356 self["isscope"] = 0
357 self["isarray"] = 0
358 $I0 = pad["subpats"]
359 self["cname"] = $I0
360 inc $I0
361 pad["subpats"] = $I0
362 end:
363 exp = self[0]
364 exp = exp."p5analyze"(pad)
365 self[0] = exp
366 .return (self)
367 .end
368
369
99869c4 @paultcochrane [pge] Added pir coda as per coding standards (part 5)
paultcochrane authored
370
371 # Local Variables:
372 # mode: pir
373 # fill-column: 100
374 # End:
95eed07 @coke [codingstd]
coke authored
375 # vim: expandtab shiftwidth=4 ft=pir:
Something went wrong with that request. Please try again.