Skip to content
This repository has been archived by the owner on Feb 3, 2021. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Clean up parse and !reduce methods.
  • Loading branch information
pmichaud committed Oct 16, 2009
1 parent 4506dba commit c9a4f85
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
29 changes: 22 additions & 7 deletions src/Regex/Cursor.pir
Expand Up @@ -138,10 +138,16 @@ If C<regex> is omitted, then use the C<TOP> rule for the grammar.

.sub 'parse' :method
.param pmc target
.param pmc regex
.param pmc regex :optional
.param int has_regex :opt_flag
.param pmc options :slurpy :named

if has_regex goto regex_done
regex = find_method self, 'TOP'
regex_done:

.local pmc cur
cur = self.'!cursor_init'(target, 'from'=>-1)
cur = self.'!cursor_init'(target, options :flat :named)
cur = cur.regex()
.return (cur)
.end
Expand Down Expand Up @@ -184,6 +190,7 @@ Create a new cursor for matching C<target>.
.sub '!cursor_init' :method
.param string target
.param int from :named('from') :optional
.param pmc action :named('action') :optional

.local pmc parrotclass, cur
$P0 = self.'HOW'()
Expand All @@ -198,6 +205,8 @@ Create a new cursor for matching C<target>.
setattribute cur, '$!from', $P0
$P0 = box from
setattribute cur, '$!pos', $P0

setattribute cur, '$!action', action
.return (cur)
.end

Expand Down Expand Up @@ -503,24 +512,30 @@ capture states.
.end


=item !reduce(name)
=item !reduce(name [, key])

Perform any action associated with the current regex match.

=cut

.sub '!reduce' :method
.param string name
.param string key :optional
.param int has_key :opt_flag
.local pmc action
action = getattribute self, '$!action'
if null action goto action_done
$P0 = find_method action, name
if null $P0 goto action_done
$I0 = can action, name
unless $I0 goto action_done
.local pmc match
match = self.'MATCH'()
$P1 = action.$P0(match)
if has_key goto action_key
action.name(match)
goto action_done
action_key:
.tailcall action.name(match, key)
action_done:
.return ($P1)
.return ()
.end


Expand Down
2 changes: 1 addition & 1 deletion t/p6regex/01-regex.t
Expand Up @@ -384,7 +384,7 @@ Description of the test.
throw $P1
match_it:
$P0 = get_hll_global ['Regex'], 'Cursor'
$P1 = $P0.'parse'(target, rule)
$P1 = $P0.'parse'(target, rule, 'from'=>-1)
match = $P1.'MATCH'()

.return (match)
Expand Down

0 comments on commit c9a4f85

Please sign in to comment.