Skip to content

Commit 11bafbe

Browse files
committed
catch miscontextualized attrs, has/method decls
1 parent 9f27365 commit 11bafbe

File tree

1 file changed

+42
-11
lines changed

1 file changed

+42
-11
lines changed

STD.pm6

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,10 +1648,17 @@ grammar P6 is STD {
16481648
token scope_declarator:our { <sym> <scoped('our')> }
16491649
token scope_declarator:anon { <sym> <scoped('anon')> }
16501650
token scope_declarator:state { <sym> <scoped('state')> }
1651-
token scope_declarator:has { <sym> <scoped('has')> }
16521651
token scope_declarator:augment { <sym> <scoped('augment')> }
16531652
token scope_declarator:supersede { <sym> <scoped('supersede')> }
1654-
1653+
token scope_declarator:has {
1654+
<sym> {
1655+
given $*PKGDECL {
1656+
when 'class' | 'grammar' | 'role' {}
1657+
default { $¢.worry("'has' declaration outside of class") }
1658+
}
1659+
}
1660+
<scoped('has')>
1661+
}
16551662

16561663
token package_declarator:class {
16571664
:my $*PKGDECL ::= 'class';
@@ -1808,13 +1815,13 @@ grammar P6 is STD {
18081815
}
18091816

18101817
token routine_declarator:sub { <sym> <routine_def('sub')> }
1811-
token routine_declarator:method { <sym> <method_def> }
1812-
token routine_declarator:submethod { <sym> <method_def> }
1818+
token routine_declarator:method { <sym> <method_def('method')> }
1819+
token routine_declarator:submethod { <sym> <method_def('submethod')> }
18131820
token routine_declarator:macro { <sym> <macro_def> }
18141821

1815-
token regex_declarator:regex { <sym> <regex_def(:!r,:!s)> }
1816-
token regex_declarator:token { <sym> <regex_def(:r,:!s)> }
1817-
token regex_declarator:rule { <sym> <regex_def(:r,:s)> }
1822+
token regex_declarator:regex { <sym> <regex_def('regex', :!r,:!s)> }
1823+
token regex_declarator:token { <sym> <regex_def('token', :r,:!s)> }
1824+
token regex_declarator:rule { <sym> <regex_def('rule', :r,:s)> }
18181825

18191826
rule multisig {
18201827
:my $signum = 0;
@@ -1863,10 +1870,16 @@ grammar P6 is STD {
18631870
] || <.panic: "Malformed routine">
18641871
}
18651872

1866-
rule method_def () {
1873+
rule method_def ($d) {
18671874
:temp $*CURLEX;
1868-
:my $*IN_DECL = 'method';
1875+
:my $*IN_DECL = $d;
18691876
:my $*DECLARAND;
1877+
{
1878+
given $*PKGDECL {
1879+
when 'class' | 'grammar' | 'role' {}
1880+
default {$¢.worry("'$d' declaration outside of class") unless $*SCOPE }
1881+
}
1882+
}
18701883
<.newlex(1)>
18711884
[
18721885
[
@@ -1891,11 +1904,17 @@ grammar P6 is STD {
18911904
] || <.panic: "Malformed method">
18921905
}
18931906

1894-
rule regex_def (:$r, :$s) {
1907+
rule regex_def ($d, :$r, :$s) {
18951908
:temp $*CURLEX;
1896-
:my $*IN_DECL = 'regex';
1909+
:my $*IN_DECL = $d;
18971910
:temp %*RX;
18981911
:my $*DECLARAND;
1912+
{
1913+
given $*PKGDECL {
1914+
when 'grammar' | 'role' {}
1915+
default { $¢.worry("'$d' declaration outside of grammar") unless $*SCOPE }
1916+
}
1917+
}
18991918
{ %*RX<s> = $s; %*RX<r> = $r; }
19001919
[
19011920
[ '&'<deflongname>? | <deflongname> ]?
@@ -5832,6 +5851,18 @@ method check_variable ($variable) {
58325851
$*CURLEX{$name}<used>++;
58335852
}
58345853
}
5854+
when '!' {
5855+
given $*SCOPE {
5856+
when 'method' | 'submethod' {}
5857+
default { $variable.worry("Variable $name used outside of method/submethod declaration"); }
5858+
}
5859+
}
5860+
when '.' {
5861+
given $*SCOPE {
5862+
when 'method' {}
5863+
default { $variable.worry("Variable $name used outside of method declaration"); }
5864+
}
5865+
}
58355866
when '^' {
58365867
my $*MULTINESS = 'multi';
58375868
$variable.add_placeholder($name);

0 commit comments

Comments
 (0)