Skip to content

Commit

Permalink
Ensure that interpretation of variables always precedes validation
Browse files Browse the repository at this point in the history
  • Loading branch information
sorear committed Mar 31, 2012
1 parent 8609d73 commit 58f8f28
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
7 changes: 7 additions & 0 deletions src/NieczaActions.pm6
Expand Up @@ -428,6 +428,7 @@ method sublongname($/) {
method desigilname($/) { method desigilname($/) {
if $<variable> { if $<variable> {
make { ind => self.do_variable_reference($/, $<variable>.ast) }; make { ind => self.do_variable_reference($/, $<variable>.ast) };
self.check_variable($<variable>);
} else { } else {
make self.process_name($<longname>, :defer); make self.process_name($<longname>, :defer);
} }
Expand Down Expand Up @@ -786,6 +787,7 @@ method metachar:var ($/) {
if !defined $cid { if !defined $cid {
$/.CURSOR.sorry("Non-Match bindings NYI"); $/.CURSOR.sorry("Non-Match bindings NYI");
# self.check_variable($<variable>);
make $RxOpSequence.new; make $RxOpSequence.new;
return Nil; return Nil;
} }
Expand Down Expand Up @@ -814,6 +816,7 @@ method metachar:var ($/) {
make $RxOpListPrim.new(name => ~$<variable>, type => $kind, make $RxOpListPrim.new(name => ~$<variable>, type => $kind,
ops => self.rxembed($/, ops => self.rxembed($/,
self.do_variable_reference($/, $<variable>.ast), True)); self.do_variable_reference($/, $<variable>.ast), True));
self.check_variable($<variable>);
} }
method rxcapturize($M, $name, $rxop is copy) { #OK not used method rxcapturize($M, $name, $rxop is copy) { #OK not used
Expand Down Expand Up @@ -1682,6 +1685,7 @@ method methodop($/) {
} elsif $<variable> { } elsif $<variable> {
make $Operator_Function.new(function => make $Operator_Function.new(function =>
self.do_variable_reference($/, $<variable>.ast)); self.do_variable_reference($/, $<variable>.ast));
self.check_variable($<variable>);
} }
$/.ast.args = $<args>.ast[0] // [] if $<args>; $/.ast.args = $<args>.ast[0] // [] if $<args>;
Expand Down Expand Up @@ -1773,6 +1777,7 @@ method colonpair($/) {
if $tv ~~ Hash { if $tv ~~ Hash {
$tv = self.do_variable_reference($/, $tv); $tv = self.do_variable_reference($/, $tv);
self.check_variable($<v>);
} }
make { term => $OpSimplePair.new(key => $<k>, value => $tv) }; make { term => $OpSimplePair.new(key => $<k>, value => $tv) };
Expand Down Expand Up @@ -1986,6 +1991,7 @@ method check_variable ($variable) {
my $here = $variable.CURSOR.cursor($variable.from); my $here = $variable.CURSOR.cursor($variable.from);
$here.deb("check_variable $name") if $*DEBUG +& DEBUG::symtab; $here.deb("check_variable $name") if $*DEBUG +& DEBUG::symtab;
my ($sigil, $twigil, $first) = $name ~~ /(\$|\@|\%|\&)(\W*)(.?)/; my ($sigil, $twigil, $first) = $name ~~ /(\$|\@|\%|\&)(\W*)(.?)/;
$variable.ast<checked> || $here.sorry("do_variable_reference must always precede check_variable");
($first,$twigil) = ($twigil, '') if $first eq ''; ($first,$twigil) = ($twigil, '') if $first eq '';
given $twigil { given $twigil {
when '' { when '' {
Expand Down Expand Up @@ -2060,6 +2066,7 @@ method check_variable ($variable) {
} }
method do_variable_reference($M, $v) { method do_variable_reference($M, $v) {
$v<checked> = True;
if $v<term> { if $v<term> {
return $v<term>; return $v<term>;
} }
Expand Down
13 changes: 4 additions & 9 deletions src/STD.pm6
Expand Up @@ -2050,7 +2050,7 @@ grammar P6 is STD {
| <coloncircumfix('')> | <coloncircumfix('')>
{ $key = ""; $value = $<coloncircumfix>; } { $key = ""; $value = $<coloncircumfix>; }
| $<var> = <.colonpair_var> | $<var> = <.colonpair_var>
{ $key = $<var><desigilname>.Str; $value = $<var>; $Actions.check_variable($value); } { $key = $<var><desigilname>.Str; $value = $<var>; }
] ]
$<k> = {$key} $<v> = {$value} $<k> = {$key} $<v> = {$value}
} }
Expand Down Expand Up @@ -2354,10 +2354,7 @@ grammar P6 is STD {
[ [
| <?before '$' > | <?before '$' >
[ <?{ $*IN_DECL }> <.panic: "Cannot declare an indirect variable name"> ]? [ <?{ $*IN_DECL }> <.panic: "Cannot declare an indirect variable name"> ]?
<variable> { <variable>
$*VAR = $<variable>;
$Actions.check_variable($*VAR) if substr($*VAR,1,1) ne '$';
}
| <?before <[\@\%\&]> <sigil>* \w > <.panic: "Invalid hard reference syntax"> | <?before <[\@\%\&]> <sigil>* \w > <.panic: "Invalid hard reference syntax">
| <longname> | <longname>
] ]
Expand Down Expand Up @@ -3371,7 +3368,7 @@ grammar P6 is STD {
token methodop { token methodop {
[ [
| <longname> | <longname>
| <?before '$' | '@' | '&' > <variable> { $Actions.check_variable($<variable>) } | <?before '$' | '@' | '&' > <variable>
| <?before <[ ' " ]> > | <?before <[ ' " ]> >
[ <!{$*QSIGIL}> || <!before '"' <-["]>*? \s > ] # dwim on "$foo." [ <!{$*QSIGIL}> || <!before '"' <-["]>*? \s > ] # dwim on "$foo."
<quote> <quote>
Expand Down Expand Up @@ -4935,9 +4932,7 @@ grammar Regex is STD {
$<sym> = {$<variable>.Str} $<sym> = {$<variable>.Str}
[ [
|| $<binding> = ( \s* '=' \s* <quantified_atom> ) || $<binding> = ( \s* '=' \s* <quantified_atom> )
{ $Actions.check_variable($<variable>) unless substr($<sym>,1,1) eq '<' } || [ <?before '.'? <[ \[ \{ \< ]>> <.worry: "Apparent subscript will be treated as regex"> ]?
|| { $Actions.check_variable($<variable>) }
[ <?before '.'? <[ \[ \{ \< ]>> <.worry: "Apparent subscript will be treated as regex"> ]?
] ]
} }


Expand Down

0 comments on commit 58f8f28

Please sign in to comment.