3 Glob - Parse and compile glob notation expressions.
7 A parser for shell-stype glob notation.
13 =item C<compile_glob(PMC source, PMC adverbs :slurpy :named)>
15 Return the result of compiling the glob expression given by
16 C<source>. Normally this function is obtained using
17 C<compreg 'PGE::Glob'> instead of calling it directly.
19 Returns the compiled regular expression. If a C<target>
20 named parameter is supplied, then it will return the parse tree
21 (target='parse'), the expression tree (target='exp'),
22 or the resulting PIR code (target='PIR').
26 .namespace [ 'PGE::Glob' ]
30 .param pmc adverbs :slurpy :named
33 target = adverbs['target']
34 target = downcase target
38 if source == '' goto analyze
39 $P0 = get_global 'glob'
41 if target != 'parse' goto check
45 unless match goto check_1
48 if $S0 == $S1 goto analyze
55 exp = new 'PGE::Exp::Concat'
57 $P0 = new 'PGE::Exp::Anchor'
58 $P0.'result_object'('^')
60 if null match goto analyze_1
65 $P0 = new 'PGE::Exp::Anchor'
66 $P0.'result_object'('$')
69 .return exp.'compile'(adverbs :flat :named)
76 load_bytecode 'PGE.pbc'
77 load_bytecode 'PGE/Dumper.pbc'
79 $P0 = compreg 'PGE::Glob'
80 .return $P0.'command_line'(args)
84 .sub '__onload' :load :init
86 load_bytecode 'PGE.pbc'
88 optable = new 'PGE::OPTable'
89 store_global '$optable', optable
91 $P0 = find_global 'glob_literal'
92 optable.newtok('term:', 'precedence'=>'=', 'nows'=>1, 'parsed'=>$P0)
94 $P0 = find_global 'glob_quest'
95 optable.newtok('term:?', 'equiv'=>'term:', 'nows'=>1, 'parsed'=>$P0)
97 $P0 = find_global 'glob_star'
98 optable.newtok('term:*', 'equiv'=>'term:', 'nows'=>1, 'parsed'=>$P0)
100 $P0 = find_global 'glob_enum'
101 optable.newtok('term:[', 'equiv'=>'term:', 'nows'=>1, 'parsed'=>$P0)
103 $P0 = find_global 'glob_alt'
104 optable.newtok('term:{', 'equiv'=>'term:', 'nows'=>1, 'parsed'=>$P0)
106 optable.newtok('infix:', 'looser'=>'term:', 'assoc'=>'list', 'nows'=>1, 'match'=>'PGE::Exp::Concat')
108 $P2 = newclass [ 'PGE::Glob::Compiler' ]
109 addattribute $P2, '$!compsub'
111 $P0 = get_global 'compile_glob'
112 $P1 = new [ 'PGE::Glob::Compiler' ]
113 $P1.'register'('PGE::Glob', $P0)
118 =item C<glob(PMC mob, PMC adverbs :slurpy :named)>
120 Parses a glob expression, returning the corresponding
121 parse C<PGE::Match> object.
125 .const int GLOB_INF = 2147483647
129 .param pmc adverbs :slurpy :named
131 .local pmc optable, match
132 optable = find_global 'PGE::Glob', '$optable'
133 match = optable.'parse'(mob)
144 lastpos = length target
145 .local string literal
148 if pos >= lastpos goto literal_end
149 $S0 = substr target, pos, 1
150 $I0 = index delim, $S0
151 if $I0 >= 0 goto literal_end
152 if $S0 != '\' goto literal_add
154 $S0 = substr target, pos, 1
160 .return (literal, pos)
164 =item C<glob_literal(PMC mob, PMC adverbs)>
166 Scan a literal from a string, stopping at any metacharacters such
167 as C<*> or C<[>. Return the matched portion, with the C<result_object>
168 set to the decoded literal.
174 .param pmc adverbs :slurpy :named
178 (mob, pos, target) = mob.'new'(mob, 'grammar'=>'PGE::Exp::Literal')
179 ($S0, $I0) = 'scan_literal'(target, pos, '*?[{')
180 if $I0 <= pos goto end
182 mob.'result_object'($S0)
188 =item C<glob_quest(PMC mob, PMC adverbs)>
190 Process a C<?> wildcard character in a glob. For this we just
191 return a CCShortcut that is set to '.'
197 .param pmc adverbs :slurpy :named
198 ## The '?' is already in mob['KEY'], so we don't need to find it here.
199 (mob, $I0) = mob.'new'(mob, 'grammar'=>'PGE::Exp::CCShortcut')
201 mob.'result_object'('.')
206 =item C<glob_star(PMC mob, PMC adverbs)>
208 Process a C<*> wildcard character in a glob. This is a little
209 bit more complex, as we have to return a quantified '.'.
215 .param pmc adverbs :slurpy :named
217 ## The '*' is already in mob['KEY'], so we don't need to find it here.
218 ## We create a Quant object, then a CCShortcut inside of it.
219 (mob, pos) = mob.'new'(mob, 'grammar'=>'PGE::Exp::Quant')
222 mob['max'] = GLOB_INF
223 $P0 = mob.'new'(mob, 'grammar'=>'PGE::Exp::CCShortcut')
225 $P0.'result_object'('.')
230 =item C<glob_enum(PMC mob, PMC adverbs)>
232 Parse an enumerated character list, such as [abcd],
239 .param pmc adverbs :slurpy :named
242 .local int pos, lastpos
243 (mob, pos, target) = mob.'new'(mob, 'grammar'=>'PGE::Exp::EnumCharList')
244 lastpos = length target
246 $S0 = substr target, pos, 1
247 if $S0 == '!' goto negate
248 if $S0 == '^' goto negate
255 .local string charlist
257 $S0 = substr target, pos, 1
258 if $S0 == '-' goto addfirst
259 if $S0 == ']' goto addfirst
265 ($S0, pos) = 'scan_literal'(target, pos, '-]')
266 if pos >= lastpos goto err_noclose
268 $S0 = substr target, pos, 1
269 if $S0 == ']' goto scan_end
271 $S0 = substr target, pos, 1
272 if $S0 == ']' goto scan_endhyphen
275 $I0 = ord charlist, -1
277 if $I0 > $I1 goto scan_loop
287 mob.'result_object'(charlist)
295 =item C<glob_alt(PMC mob, PMC adverbs)>
297 Parse an enumerated character list, such as [abcd],
304 .param pmc adverbs :slurpy :named
307 .local int pos, lastpos
308 (mob, pos, target) = mob.'new'(mob, 'grammar'=>'PGE::Exp::Literal')
309 lastpos = length target
311 ($S0, pos) = 'scan_literal'(target, pos, ',}')
313 mob.'result_object'($S0)
315 if pos >= lastpos goto err_noclose
316 $S0 = substr target, pos, 1
317 if $S0 == '}' goto end
318 $P0 = mob.'new'(mob, 'grammar'=>'PGE::Exp::Alt')
323 $P0 = mob.'new'(mob, 'grammar'=>'PGE::Exp::Literal')
324 ($S0, pos) = 'scan_literal'(target, pos, ',}')
326 $P0.'result_object'($S0)
339 .namespace [ 'PGE::Glob::Compiler' ]
341 =item register(string name, pmc compsub)
343 Registers this compiler object as C<name> and
344 using C<compsub> as the subroutine to call for performing compilation.
348 .sub 'register' :method
352 setattribute self, '$!compsub', compsub
358 =item compile(pmc code [, "option" => value, ... ])
360 Compile C<source> (possibly modified by any provided options).
364 .sub 'compile' :method
366 .param pmc adverbs :slurpy :named
370 # $!compsub is deprecated
371 compsub = getattribute self, '$!compsub'
373 .return compsub(source, adverbs :flat :named)
380 PGE::Glob was originally authored by Jonathan Scott Duff (duff@pobox.com),
381 It has been updated for later versions of PGE by Patrick R. Michaud
382 (pmichaud@pobox.com).
390 # vim: expandtab shiftwidth=4 ft=pir: