1 # Copyright (C) 2005-2009, Parrot Foundation.
6 PGE::Match - implementation of PGE match objects
10 This file implements match objects returned by the Parrot Grammar Engine.
14 .namespace [ 'PGE';'Match' ]
17 load_bytecode 'P6object.pbc'
18 load_bytecode 'PGE/Dumper.pir' # FIXME, XXX, etc.
20 p6meta = new 'P6metaclass'
21 $P0 = p6meta.'new_class'('PGE::Match', 'parent'=>'Capture', 'attr'=>'$.target $.from $.pos &!corou $!ast')
22 set_hll_global ['PGE'], '$!MATCH', $P0
30 =item C<new(PMC src, [ PMC adverbs :slurpy :named ])>
32 Creates a new Match object based on C<src>. If the C<grammar>
33 adverb is specified, then the new Match object is of the given
34 grammar class, otherwise if C<src> is an instance of C<Match>
35 (or a subclass) then that class is used to create the object,
36 otherwise it uses the class of the invocant.
38 The C<pos>, C<p>, C<continue>, or C<c> adverbs specify where
39 the match object should begin. If no starting position is
40 given, the current position of C<src> is used if it has one,
41 otherwise the start position is at offset zero. The C<from>
42 adverb can be used to initialize the Match's C<$.from>
43 attribute to a value other than the starting position.
45 The C<rw> adverb causes the invocant to be modified and
46 returned instead of creating a new Match object.
48 The C<new> method returns several values to the caller: the
49 initialized match object, the target the object is matching against,
50 a reference to its $.from attribute, a reference to its $.pos
51 attribute, the value of C<pos/p/continue/c> used to
52 initialize the object, and whether or not a continue flag
59 .param pmc adverbs :slurpy :named
61 ## set values based on src param
62 .local int issrcmatch, pos, iscont
64 .local pmc target, grammar_class
65 issrcmatch = isa src, ['PGE';'Match']
66 if issrcmatch goto target_from_src
67 target = new 'CodeString'
72 grammar_class = getattribute $P0, 'parrotclass'
75 target = getattribute src, '$.target'
76 $P0 = getattribute src, '$.pos'
79 grammar_class = typeof src
80 if pos >= 0 goto adverb_pos
84 unless adverbs goto with_adverbs
85 ## determine the value of pos
86 $I0 = exists adverbs['pos']
87 unless $I0 goto adverb_p
92 $I0 = exists adverbs['p']
93 unless $I0 goto adverb_continue
98 $I0 = exists adverbs['continue']
99 unless $I0 goto adverb_c
100 pos = adverbs['continue']
104 $I0 = exists adverbs['c']
105 unless $I0 goto with_pos
110 ## figure out the class of the new object
111 $I0 = exists adverbs['grammar']
112 unless $I0 goto with_grammar
113 grammar = adverbs['grammar']
115 eq $S0, 'NameSpace', grammar_namespace
116 ne $S0, 'String', have_grammar
118 $P0 = split '::', $S0
119 grammar_class = get_class $P0
122 grammar_class = grammar
124 grammar_class = get_class grammar
128 ## create the new match object
129 .local pmc mob, mfrom, mpos
130 mob = new grammar_class
131 setattribute mob, '$.target', target
133 setattribute mob, '$.from', mfrom
135 setattribute mob, '$.pos', mpos
137 .return (mob, pos, target, mfrom, mpos, iscont)
143 Tell a Match object to continue the previous match from where
151 corou = getattribute self, '&!corou'
152 if_null corou, next_1
155 $P0 = getattribute self, '$.pos'
165 =item C<from([int pos])>
167 Returns or sets the offset in the target string of the first item
173 .param int from :optional
174 .param int has_from :opt_flag
175 $P0 = getattribute self, '$.from'
176 if has_from == 0 goto get
183 =item C<to([int pos])>
185 Returns or sets the offset at the end of this match.
190 .param int to :optional
191 .param int has_to :opt_flag
192 $P0 = getattribute self, '$.pos'
193 if has_to == 0 goto get
202 Returns C<.to()> - C<.from()>.
210 unless $I2 < 0 goto done
219 Returns the portion of the target string matched by this object.
224 $P0 = getattribute self, '$.target'
225 $P1 = getattribute self, '$.from'
226 $P2 = getattribute self, '$.pos'
227 if $P2 < 0 goto false
228 if $P2 <= $P1 goto false
232 $S1 = substr $P0, $I1, $I2
241 Return the original item being matched.
246 $P0 = getattribute self, '$.target'
251 =item C<!make(pmc obj)>
253 Sets the "ast object" for the Match invocant.
259 setattribute self, '$!ast', obj
264 =item C<ast([pmc obj])>
266 Returns the "ast object" for the match object. If no ast object
267 has been set, then it returns the string between C<.from> and C<.to>.
273 obj = getattribute self, '$!ast'
274 if null obj goto ret_null
277 .tailcall self.'Str'()
281 =item C<_failcut(int cutvalue)>
283 Immediately "fail" this Match object, removing any
284 captured entities and coroutine continuation. Set
285 the position of the match object to C<cutvalue>.
289 .sub '_failcut' :method
291 $P0 = getattribute self, '$.pos'
294 setattribute self, '$.target', $P0
295 setattribute self, '&!corou', $P0
296 setattribute self, '$!ast', $P0
304 Returns 1 if this object successfully matched the target string,
309 .sub 'get_bool' :vtable :method
310 $P1 = getattribute self, '$.pos'
316 =item C<get_integer()>
318 Returns the integer value of this match.
322 .sub '' :vtable('get_integer') :method
327 =item C<get_number()>
329 Returns the numeric value of this match.
333 .sub '' :vtable('get_number') :method
338 =item C<get_string()>
340 Returns the portion of the target string matched by this object.
344 .sub '' :vtable('get_string') :method
353 Patrick Michaud (pmichaud@pobox.com) is the author and maintainer.
354 Patches and suggestions should be sent to the Perl 6 compiler list
355 (perl6-compiler@perl.org).
363 # vim: expandtab shiftwidth=4 ft=pir: