3 PGE::Match - implementation of PGE match objects
7 This file implements match objects returned by the Parrot Grammar Engine.
11 .namespace [ 'PGE::Match' ]
14 load_bytecode 'P6object.pbc'
15 load_bytecode 'Parrot/Capture_PIR.pbc'
16 load_bytecode 'PGE/Dumper.pir' # FIXME, XXX, etc.
18 p6meta = new 'P6metaclass'
19 p6meta.'new_class'('PGE::Match', 'parent'=>'Capture_PIR', 'attr'=>'$.target $.from $.pos &!corou')
27 =item C<new(PMC src, [ PMC adverbs :slurpy :named ])>
29 Creates a new Match object based on C<src>. If the C<grammar>
30 adverb is specified, then the new Match object is of the given
31 grammar class, otherwise if C<src> is an instance of C<Match>
32 (or a subclass) then that class is used to create the object,
33 otherwise it uses the class of the invocant.
35 The C<pos>, C<p>, C<continue>, or C<c> adverbs specify where
36 the match object should begin. If no starting position is
37 given, the current position of C<src> is used if it has one,
38 otherwise the start position is at offset zero. The C<from>
39 adverb can be used to initialize the Match's C<$.from>
40 attribute to a value other than the starting position.
42 The C<rw> adverb causes the invocant to be modified and
43 returned instead of creating a new Match object.
45 The C<new> method returns several values to the caller: the
46 initialized match object, the target the object is matching against,
47 a reference to its $.from attribute, a reference to its $.pos
48 attribute, the value of C<pos/p/continue/c> used to
49 initialize the object, and whether or not a continue flag
56 .param pmc adverbs :slurpy :named
58 ## set values based on src param
59 .local int issrcmatch, pos, iscont
62 issrcmatch = isa src, 'PGE::Match'
63 if issrcmatch goto target_from_src
72 target = getattribute src, '$.target'
73 $P0 = getattribute src, '$.pos'
77 if pos >= 0 goto adverb_pos
81 unless adverbs goto with_adverbs
82 ## determine the value of pos
83 $I0 = exists adverbs['pos']
84 unless $I0 goto adverb_p
89 $I0 = exists adverbs['p']
90 unless $I0 goto adverb_continue
95 $I0 = exists adverbs['continue']
96 unless $I0 goto adverb_c
97 pos = adverbs['continue']
101 $I0 = exists adverbs['c']
102 unless $I0 goto with_pos
107 ## figure out the class of the new object
108 $I0 = exists adverbs['grammar']
109 unless $I0 goto with_grammar
110 grammar = adverbs['grammar']
114 ## create the new match object
115 .local pmc mob, mfrom, mpos
117 setattribute mob, '$.target', target
118 mfrom = new 'Integer'
120 setattribute mob, '$.from', mfrom
123 setattribute mob, '$.pos', mpos
125 .return (mob, pos, target, mfrom, mpos, iscont)
131 Tell a Match object to continue the previous match from where
139 corou = getattribute self, '&!corou'
140 if_null corou, next_1
143 $P0 = getattribute self, '$.pos'
153 =item C<from([int pos])>
155 Returns or sets the offset in the target string of the first item
161 .param int from :optional
162 .param int has_from :opt_flag
163 $P0 = getattribute self, '$.from'
164 if has_from == 0 goto get
171 =item C<to([int pos])>
173 Returns or sets the offset at the end of this match.
178 .param int to :optional
179 .param int has_to :opt_flag
180 $P0 = getattribute self, '$.pos'
181 if has_to == 0 goto get
190 Returns C<.to()> - C<.from()>.
198 unless $I2 < 0 goto done
207 Returns the portion of the target string matched by this object.
212 $P0 = getattribute self, '$.target'
213 $P1 = getattribute self, '$.from'
214 $P2 = getattribute self, '$.pos'
215 if $P2 < 0 goto false
216 if $P2 <= $P1 goto false
220 $S1 = substr $P0, $I1, $I2
229 Returns the scalar value of this match -- the "result object"
230 if there is one, otherwise the substring matched by this match
236 .return self.'result_object'()
240 # deprecated RT#54000
241 .sub 'get_scalar' :method
242 .return self.'item'()
246 =item C<result_object([pmc obj])>
248 Returns or sets the "result object" for the match object.
252 .sub 'result_object' :method
253 .param pmc obj :optional
254 .param int has_obj :opt_flag
255 if has_obj == 0 goto get_obj
256 setattribute self, '$!item', obj
259 obj = getattribute self, '$!item'
261 if null obj goto ret_null
264 .return self.'text'()
268 =item C<find_key([ key1, key2, ... ])>
270 Find the first of C<key1>, C<key2>, etc. in the current
271 Match object, and return it. Returns '' if none of
272 the specified keys are found. If no keys are specified,
273 then simply return the first key found.
277 .sub 'find_key' :method
278 .param pmc keys :slurpy
279 if null keys goto first_key
280 unless keys goto first_key
282 unless keys goto not_found
284 $I0 = exists self[$S0]
289 $P1 = new 'Iterator', $P0
290 unless $P1 goto not_found
298 =item C<_failcut(int cutvalue)>
300 Immediately "fail" this Match object, removing any
301 captured entities and coroutine continuation. Set
302 the position of the match object to C<cutvalue>.
306 .sub '_failcut' :method
308 $P0 = getattribute self, '$.pos'
311 setattribute self, '$.target', $P0
312 setattribute self, '&!corou', $P0
313 setattribute self, '@!list', $P0
314 setattribute self, '$!item', $P0
316 iter = new 'Iterator', self
318 unless iter goto iter_end
327 =item C<__get_bool()>
329 Returns 1 if this object successfully matched the target string,
334 .sub 'get_bool' :vtable :method
335 $P1 = getattribute self, '$.pos'
341 =item C<__get_integer()>
343 Returns the integer value of this match.
347 .sub 'get_integer' :vtable :method
348 $I0 = self.'result_object'()
352 =item C<__get_number()>
354 Returns the numeric value of this match.
358 .sub 'get_number' :vtable :method
359 $N0 = self.'result_object'()
363 =item C<__get_string()>
365 Returns the portion of the target string matched by this object.
369 .sub 'get_string' :vtable :method
370 $S0 = self.'result_object'()
378 Patrick Michaud (pmichaud@pobox.com) is the author and maintainer.
379 Patches and suggestions should be sent to the Perl 6 compiler list
380 (perl6-compiler@perl.org).
388 # vim: expandtab shiftwidth=4 ft=pir: