X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDevel%2FDeclare%2FContext%2FSimple.pm;h=c8e2a31df6e61b2d1944cea5bce2a6d9c6b6b151;hb=eb885995e02ff14ddf174218c08db32ed85c610e;hp=1a47a7ff831c7016666f92260568218383a1063e;hpb=86964fb3f9ba6afc359b1ecb231fe44dae3665ef;p=p5sagit%2FDevel-Declare.git diff --git a/lib/Devel/Declare/Context/Simple.pm b/lib/Devel/Declare/Context/Simple.pm index 1a47a7f..c8e2a31 100644 --- a/lib/Devel/Declare/Context/Simple.pm +++ b/lib/Devel/Declare/Context/Simple.pm @@ -1,9 +1,12 @@ 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/; + +our $VERSION = '0.006011'; sub new { my $class = shift; @@ -12,7 +15,7 @@ sub new { sub init { my $self = shift; - @{$self}{ qw(Declarator Offset) } = @_; + @{$self}{ qw(Declarator Offset WarningOnRedefined) } = @_; return $self; } @@ -31,9 +34,24 @@ sub declarator { return $self->{Declarator} } +sub warning_on_redefine { + my $self = shift; + return $self->{WarningOnRedefined} +} + 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 { @@ -94,7 +112,8 @@ sub strip_proto { Devel::Declare::clear_lex_stuff(); $linestr = $self->get_linestr(); - substr($linestr, $self->offset, $length) = ''; + substr($linestr, $self->offset, + defined($length) ? $length : length($linestr)) = ''; $self->set_linestr($linestr); return $proto; @@ -102,6 +121,128 @@ sub strip_proto { 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 strip_attrs { + my $self = shift; + $self->skipspace; + + my $linestr = Devel::Declare::get_linestr; + my $attrs = ''; + + if (substr($linestr, $self->offset, 1) eq ':') { + while (substr($linestr, $self->offset, 1) ne '{') { + if (substr($linestr, $self->offset, 1) eq ':') { + substr($linestr, $self->offset, 1) = ''; + Devel::Declare::set_linestr($linestr); + + $attrs .= ':'; + } + + $self->skipspace; + $linestr = Devel::Declare::get_linestr(); + + if (my $len = Devel::Declare::toke_scan_word($self->offset, 0)) { + my $name = substr($linestr, $self->offset, $len); + substr($linestr, $self->offset, $len) = ''; + Devel::Declare::set_linestr($linestr); + + $attrs .= " ${name}"; + + if (substr($linestr, $self->offset, 1) eq '(') { + my $length = Devel::Declare::toke_scan_str($self->offset); + my $arg = Devel::Declare::get_lex_stuff(); + Devel::Declare::clear_lex_stuff(); + $linestr = Devel::Declare::get_linestr(); + substr($linestr, $self->offset, $length) = ''; + Devel::Declare::set_linestr($linestr); + + $attrs .= "(${arg})"; + } + } + } + + $linestr = Devel::Declare::get_linestr(); + } + + return $attrs; +} + + sub get_curstash_name { return Devel::Declare::get_curstash_name; }