package Devel::Declare::Context::Simple;
-use Devel::Declare ();
-use B::Hooks::EndOfScope;
use strict;
use warnings;
+use Devel::Declare ();
+use B::Hooks::EndOfScope;
+use Carp qw/confess/;
sub new {
my $class = shift;
sub skip_declarator {
my $self = shift;
- $self->inc_offset(Devel::Declare::toke_move_past_token($self->offset));
+ my $decl = $self->declarator;
+ my $len = Devel::Declare::toke_scan_word($self->offset, 0);
+ confess "Couldn't find declarator '$decl'"
+ unless $len;
+
+ my $linestr = $self->get_linestr;
+ my $name = substr($linestr, $self->offset, $len);
+ confess "Expected declarator '$decl', got '${name}'"
+ unless $name eq $decl;
+
+ $self->inc_offset($len);
}
sub skipspace {
return;
}
+sub strip_ident {
+ my $self = shift;
+ $self->skipspace;
+ if (my $len = Devel::Declare::toke_scan_ident( $self->offset )) {
+ my $linestr = $self->get_linestr();
+ my $ident = substr( $linestr, $self->offset, $len );
+ substr( $linestr, $self->offset, $len ) = '';
+ $self->set_linestr($linestr);
+ return $ident;
+ }
+
+ $self->skipspace;
+ return;
+}
+
sub strip_proto {
my $self = shift;
$self->skipspace;
my $length = Devel::Declare::toke_scan_str($self->offset);
my $proto = Devel::Declare::get_lex_stuff();
Devel::Declare::clear_lex_stuff();
- if( $length < 0 ) {
- # Need to scan ahead more
- $linestr .= $self->get_linestr();
- $length = rindex($linestr, ")") - $self->offset + 1;
- }
- else {
- $linestr = $self->get_linestr();
- }
+ $linestr = $self->get_linestr();
substr($linestr, $self->offset, $length) = '';
$self->set_linestr($linestr);
return;
}
+sub strip_names_and_args {
+ my $self = shift;
+ $self->skipspace;
+
+ my @args;
+
+ my $linestr = $self->get_linestr;
+ if (substr($linestr, $self->offset, 1) eq '(') {
+ # We had a leading paren, so we will now expect comma separated
+ # arguments
+ substr($linestr, $self->offset, 1) = '';
+ $self->set_linestr($linestr);
+ $self->skipspace;
+
+ # At this point we expect to have a comma-separated list of
+ # barewords with optional protos afterward, so loop until we
+ # run out of comma-separated values
+ while (1) {
+ # Get the bareword
+ my $thing = $self->strip_name;
+ # If there's no bareword here, bail
+ confess "failed to parse bareword. found ${linestr}"
+ unless defined $thing;
+
+ $linestr = $self->get_linestr;
+ if (substr($linestr, $self->offset, 1) eq '(') {
+ # This one had a proto, pull it out
+ push(@args, [ $thing, $self->strip_proto ]);
+ } else {
+ # This had no proto, so store it with an undef
+ push(@args, [ $thing, undef ]);
+ }
+ $self->skipspace;
+ $linestr = $self->get_linestr;
+
+ if (substr($linestr, $self->offset, 1) eq ',') {
+ # We found a comma, strip it out and set things up for
+ # another iteration
+ substr($linestr, $self->offset, 1) = '';
+ $self->set_linestr($linestr);
+ $self->skipspace;
+ } else {
+ # No comma, get outta here
+ last;
+ }
+ }
+
+ # look for the final closing paren of the list
+ if (substr($linestr, $self->offset, 1) eq ')') {
+ substr($linestr, $self->offset, 1) = '';
+ $self->set_linestr($linestr);
+ $self->skipspace;
+ }
+ else {
+ # fail if it isn't there
+ confess "couldn't find closing paren for argument. found ${linestr}"
+ }
+ } else {
+ # No parens, so expect a single arg
+ my $thing = $self->strip_name;
+ # If there's no bareword here, bail
+ confess "failed to parse bareword. found ${linestr}"
+ unless defined $thing;
+ $linestr = $self->get_linestr;
+ if (substr($linestr, $self->offset, 1) eq '(') {
+ # This one had a proto, pull it out
+ push(@args, [ $thing, $self->strip_proto ]);
+ } else {
+ # This had no proto, so store it with an undef
+ push(@args, [ $thing, undef ]);
+ }
+ }
+
+ return \@args;
+}
+
sub get_curstash_name {
return Devel::Declare::get_curstash_name;
}
substr($linestr, $self->offset + 1, 0) = $inject;
substr($linestr, $self->offset, 0) = $before;
$self->set_linestr($linestr);
+ return 1;
}
+ return 0;
}
sub scope_injector_call {