Skip to content

Commit

Permalink
Stub in Capture, add capture and capterm in the grammar and actions f…
Browse files Browse the repository at this point in the history
…or them, and add Parcel.Capture for coercing a Parcel to a Capture (capterm is implemented in terms of this; we need it anyway for other bits).
  • Loading branch information
jnthn committed Feb 13, 2010
1 parent 00da096 commit f90dfa8
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 0 deletions.
1 change: 1 addition & 0 deletions build/Makefile.in
Expand Up @@ -124,6 +124,7 @@ BUILTINS_PIR = \
src/builtins/Failure.pir \
src/builtins/Signature.pir \
src/builtins/Junction.pir \
src/builtins/Capture.pir \
src/builtins/assign.pir \
src/builtins/control.pir \

Expand Down
19 changes: 19 additions & 0 deletions src/Perl6/Actions.pm
Expand Up @@ -1036,6 +1036,21 @@ method regex_declarator($/, $key?) {
}
}

method capterm($/) {
# Construct a Parcel, and then call .Capture to coerce it to a capture.
my $past := $<termish> ?? $<termish>.ast !!
$<capture> ?? $<capture>[0].ast !!
PAST::Op.new( :name('&infix:<,>') );
unless $past.isa(PAST::Op) && $past.name() eq '&infix:<,>' {
$past := PAST::Op.new( :name('&infix:<,>'), $past );
}
make PAST::Op.new( :pasttype('callmethod'), :name('Capture'), $past);
}

method capture($/) {
make $<EXPR>.ast;
}

method multisig($/) {
make $<signature>.ast;
}
Expand Down Expand Up @@ -1429,6 +1444,10 @@ method term:sym<*>($/) {
)
}

method term:sym<capterm>($/) {
make $<capterm>.ast;
}

method args($/) {
my $past;
if $<semiarglist> { $past := $<semiarglist>.ast; }
Expand Down
15 changes: 15 additions & 0 deletions src/Perl6/Grammar.pm
Expand Up @@ -633,6 +633,19 @@ rule method_def {
# Captures and Signatures #
###########################

token capterm {
'\\'
[
| '(' <capture>? ')'
| <?before \S> <termish>
| {} <.panic: "You can't backslash that">
]
}

rule capture {
<EXPR>
}

rule param_sep {
$<sep>=[','|':'|';;'|';'] { @*seps.push($<sep>) }
}
Expand Down Expand Up @@ -786,6 +799,8 @@ token term:sym<pir::op> {

token term:sym<dotty> { <dotty> }

token term:sym<capterm> { <capterm> }

token args {
| '(' <semiarglist> ')'
| [ \s <arglist> ]
Expand Down
51 changes: 51 additions & 0 deletions src/builtins/Capture.pir
@@ -0,0 +1,51 @@
## $Id$

=head1 TITLE

Capture - Perl 6 Capture class

=head1 DESCRIPTION

This file sets up the Perl 6 C<Capture> class.

=cut

.namespace ['Capture']

.sub 'onload' :anon :init :load
.local pmc p6meta, captureproto
p6meta = get_hll_global ['Mu'], '$!P6META'
captureproto = p6meta.'new_class'('Capture', 'parent'=>'Any', 'attr'=>'$!pos $!named')
.end


=head2 Methods

=over 4

=item new

Takes a bunch of positional and named arguments and builds a capture from
them.

=cut

.sub 'new' :method
.param pmc pos_args :slurpy
.param pmc named_args :slurpy :named

# Create capture.
$P0 = get_hll_global 'Whatever'
$P0 = self.'bless'($P0, '$!pos'=>pos_args, '$!named'=>named_args)
.return ($P0)
.end

=back

=cut

# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir:
32 changes: 32 additions & 0 deletions src/builtins/Parcel.pir
Expand Up @@ -65,6 +65,7 @@ Construct an iterator for the Parcel.
.return ($S0)
.end


=item Seq()

Return the Parcel as a Seq.
Expand All @@ -79,6 +80,37 @@ Return the Parcel as a Seq.
.end


=item Capture()

Coerce the Parcel into a capture.

=cut

.namespace ['Parcel']
.sub 'Capture' :method
.local pmc self_it, pos, named
self_it = iter self
pos = new ['ResizablePMCArray']
named = new ['Hash']
self_loop:
unless self_it goto self_done
$P0 = shift self_it
$I0 = isa $P0, 'Enum'
if $I0 goto to_named
push pos, $P0
goto self_loop
to_named:
$P1 = $P0.'key'()
$P2 = $P0.'value'()
named[$P1] = $P2
goto self_loop
self_done:
$P0 = get_hll_global 'Capture'
$P0 = $P0.'new'(pos :flat, named :flat :named)
.return ($P0)
.end


=item !FETCH()

=cut
Expand Down

0 comments on commit f90dfa8

Please sign in to comment.