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
Add lexical $¢.
Add subrule matching -- non-capturing and non-backtracking as yet.
  • Loading branch information
pmichaud committed Oct 10, 2009
1 parent a0788f8 commit 86f5d68
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 6 deletions.
1 change: 1 addition & 0 deletions build/Makefile.in
Expand Up @@ -54,6 +54,7 @@ P6REGEX_SOURCES = \
src/PAST/Regex.pir \
src/PAST/Compiler-Regex.pir \
src/Regex/Cursor.pir \
src/Regex/Cursor-builtins.pir \
src/Regex/Match.pir \
src/Regex/Dumper.pir \

Expand Down
41 changes: 40 additions & 1 deletion src/PAST/Compiler-Regex.pir
Expand Up @@ -75,6 +75,7 @@ Return the POST representation of the regex AST rooted by C<node>.
concat $S0, tgt
concat $S0, ')'
ops.'push_pirop'('callmethod', '"!cursor_start"', 'self', 'result'=>$S0)
ops.'push_pirop'('.lex', 'unicode:"$\x{a2}"', cur)
ops.'push_pirop'('length', eos, tgt, 'result'=>eos)

# On Parrot, indexing into variable-width encoded strings
Expand Down Expand Up @@ -561,7 +562,7 @@ second child of this node.
pos = self.'!rxregs'('pos')
$S0 = concat pos, " :named('pos')"
self.'!cursorop'(ops, '!matchify', 0, $S0)
ops.'push_pirop'('yield', cur)
ops.'push_pirop'('return', cur)
.return (ops)
.end

Expand Down Expand Up @@ -770,6 +771,44 @@ Code for initial regex scan.
.end


=item subrule(PAST::Regex node)

Perform a subrule call.

=cut

.sub 'subrule' :method :multi(_, ['PAST';'Regex'])
.param pmc node

.local pmc cur, pos, fail, ops
(cur, pos, fail) = self.'!rxregs'('cur pos fail')
ops = self.'post_new'('Ops', 'node'=>node, 'result'=>cur)

.local pmc name, negate
$P0 = node.'name'()
name = self.'as_post'($P0, 'rtype'=>'~')
ops.'push'(name)

.local pmc negate
.local string testop
negate = node.'negate'()
testop = self.'??!!'(negate, 'if', 'unless')

.local pmc zerowidth
zerowidth = node.'zerowidth'()

ops.'push_pirop'('inline', name, negate, zerowidth, 'inline'=>" # rx subrule %0 negate=%1 zerowidth=%2")

self.'!cursorop'(ops, '!cursor_pos', 0, pos)
ops.'push_pirop'('callmethod', name, cur, 'result'=>'$P10')
ops.'push_pirop'(testop, '$P10', fail)
if zerowidth goto done
ops.'push_pirop'('callmethod', "'pos'", '$P10', 'result'=>pos)
done:
.return (ops)
.end


=item post_new(type, args :slurpy, options :slurpy :named)

Helper method to create a new POST node of C<type>.
Expand Down
7 changes: 7 additions & 0 deletions src/PAST/Regex.pir
Expand Up @@ -69,6 +69,13 @@ for regular expressions.
.end
.sub 'zerowidth' :method
.param pmc value :optional
.param int has_value :opt_flag
.tailcall self.'attr'('zerowidth', value, has_value)
.end
=item peek()
Returns the prefixes associated with the regex tree rooted
Expand Down
1 change: 1 addition & 0 deletions src/Regex.pir
Expand Up @@ -12,6 +12,7 @@ Regex.pbc .
=cut

.include 'src/Regex/Cursor.pir'
.include 'src/Regex/Cursor-builtins.pir'

.include 'src/Regex/Match.pir'

Expand Down
4 changes: 1 addition & 3 deletions src/Regex/Cursor.pir
Expand Up @@ -141,10 +141,8 @@ Create and initialize a new cursor from C<self>.

.local pmc from, pos, target, action
from = getattribute self, '$!pos'
from = clone from
setattribute cur, '$!from', from
pos = clone from
setattribute cur, '$!pos', pos
setattribute cur, '$!pos', from

target = getattribute self, '$!target'
setattribute cur, '$!target', target
Expand Down
1 change: 1 addition & 0 deletions src/Regex/P6Regex.pir
Expand Up @@ -40,6 +40,7 @@ Regex::P6Regex - Parser/compiler for Perl 6 regexes
.include 'src/PAST/Regex.pir'
.include 'src/PAST/Compiler-Regex.pir'
.include 'src/Regex/Cursor.pir'
.include 'src/Regex/Cursor-builtins.pir'
.include 'src/Regex/Match.pir'
.include 'src/Regex/Dumper.pir'
Expand Down
19 changes: 19 additions & 0 deletions src/Regex/P6Regex/Actions.pm
Expand Up @@ -198,6 +198,25 @@ method backslash:sym<misc>($/) {
make $past;
}


method assertion:sym<?>($/) {
my $past := $<assertion>.ast;
$past.zerowidth(1);
make $past;
}

method assertion:sym<!>($/) {
my $past := $<assertion>.ast;
$past.negate( !$past.negate );
$past.zerowidth(1);
make $past;
}

method assertion:sym<name>($/) {
my $past := PAST::Regex.new( :name(~$<longname>) , :pasttype('subrule') );
make $past;
}

method assertion:sym<[>($/) {
make $<cclass_elem>[0].ast;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Regex/P6Regex/Grammar.pm
Expand Up @@ -87,8 +87,8 @@ grammar Regex::P6Regex::Grammar is PCT::Grammar;

# proto token assertion { <...> }

token assertion:sym<?> { '?' [ <?before '>' > | <assertion> ] }
token assertion:sym<!> { '!' [ <?before '>' > | <assertion> ] }
token assertion:sym<?> { '?' [ <?before '>' > | <assertion> ] {*} }
token assertion:sym<!> { '!' [ <?before '>' > | <assertion> ] {*} }

token assertion:sym<method> {
'.' <assertion>
Expand Down

0 comments on commit 86f5d68

Please sign in to comment.