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

Commit

Permalink
Add keyed reductions / action triggers.
Browse files Browse the repository at this point in the history
  • Loading branch information
pmichaud committed Oct 15, 2009
1 parent 2b0f1ad commit 468b4eb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
38 changes: 16 additions & 22 deletions src/PAST/Compiler-Regex.pir
Expand Up @@ -624,12 +624,10 @@ second child of this node.
.sub 'pass' :method :multi(_,['PAST';'Regex'])
.param pmc node

.local pmc ops
ops = self.'reduce'(node)
.local pmc cur, pos, ops
(cur, pos) = self.'!rxregs'('cur pos')
ops = self.'post_new'('Ops', 'node'=>node, 'result'=>cur)
ops.'push_pirop'('inline', 'inline'=>' # rx pass')
.local string cur, pos
cur = ops.'result'()
pos = self.'!rxregs'('pos')
self.'!cursorop'(ops, '!cursor_pass', 0, pos, '""')
ops.'push_pirop'('return', cur)
.return (ops)
Expand All @@ -643,25 +641,21 @@ second child of this node.
.sub 'reduce' :method :multi(_,['PAST';'Regex'])
.param pmc node

.local pmc cur, act
(cur, act) = self.'!rxregs'('cur act')

.local pmc ops, name, redlabel
.local pmc cur, pos, ops
(cur, pos) = self.'!rxregs'('cur pos')
ops = self.'post_new'('Ops', 'node'=>node, 'result'=>cur)
$P0 = node.'name'()
unless $P0 goto done
name = self.'as_post'($P0, 'rtype'=>'~')
redlabel = self.'post_new'('Label', 'name'=>'rxreduce_')

ops.'push_pirop'('inline', name, 'inline'=>' # rx reduce %0')
ops.'push'(name)
ops.'push_pirop'('if_null', act, redlabel)
ops.'push_pirop'('find_method', '$P10', act, name)
ops.'push_pirop'('if_null', '$P10', redlabel)
self.'!cursorop'(ops, '!MATCH', 1, '$P11')
ops.'push_pirop'('callmethod', '$P10', act, '$P11')
ops.'push'(redlabel)
done:
.local pmc cpost, posargs, namedargs
(cpost, posargs, namedargs) = self.'post_children'(node, 'signature'=>'v:')

.local string name, key
name = '""'
key = posargs[0]

ops.'push_pirop'('inline', name, key, 'inline'=>' # rx reduce name=%0 key=%1')
ops.'push'(cpost)
self.'!cursorop'(ops, '!cursor_pos', 0, pos)
self.'!cursorop'(ops, '!reduce', 0, name, posargs :flat, namedargs :flat)
.return (ops)
.end

Expand Down
11 changes: 7 additions & 4 deletions src/Regex/P6Regex/Actions.pm
Expand Up @@ -173,10 +173,6 @@ method metachar:sym<rwb>($/) {
make $past;
}

method metachar:sym<{*}>($/) {
make 0;
}

method metachar:sym<bs>($/) {
make $<backslash>.ast;
}
Expand All @@ -189,6 +185,13 @@ method metachar:sym<assert>($/) {
make $<assertion>.ast;
}

method metachar:sym<{*}>($/) {
my $past := $<key>
?? PAST::Regex.new( ~$<key>[0], :pasttype('reduce') )
!! 0;
make $past;
}

method metachar:sym<var>($/) {
my $past;
my $name := $<pos> ?? +$<pos> !! ~$<name>;
Expand Down
7 changes: 6 additions & 1 deletion src/Regex/P6Regex/Grammar.pm
Expand Up @@ -80,9 +80,14 @@ grammar Regex::P6Regex::Grammar is PCT::Grammar;
token metachar:sym<::> { $<sym>=['::'] {*} }
token metachar:sym<lwb> { $<sym>=['<<'|'«'] {*} }
token metachar:sym<rwb> { $<sym>=['>>'|'»'] {*} }
token metachar:sym<{*}> { $<sym>=['{*}'] {*} }
token metachar:sym<bs> { \\ <backslash> {*} }
token metachar:sym<mod> { <mod_internal> {*} }

token metachar:sym<{*}> {
$<sym>=['{*}']
[ \h* '#= ' \h* $<key>=[[\S+] ** [\h+]] ]?
{*}
}
token metachar:sym<assert> {
'<' <assertion>
[ '>' || <.panic: 'regex assertion not terminated by angle bracket'> ]
Expand Down

0 comments on commit 468b4eb

Please sign in to comment.