Skip to content

Commit

Permalink
RakuAST: throw correct exception when on missing unit scope
Browse files Browse the repository at this point in the history
  • Loading branch information
niner committed Feb 12, 2023
1 parent eff0244 commit 95a5479
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions src/Raku/Grammar.nqp
Expand Up @@ -534,6 +534,7 @@ grammar Raku::Grammar is HLL::Grammar does Raku::Common {
:my $*LITERALS;
:my $*EXPORT;
:my $*NEXT_STATEMENT_ID := 1; # to give each statement an ID
:my $*begin_compunit := 1; # whether we're at start of a compilation unit
<.comp_unit_stage0>
<.lang_setup($outer-cu)>
Expand Down Expand Up @@ -1911,9 +1912,14 @@ grammar Raku::Grammar is HLL::Grammar does Raku::Common {
<trait($*PACKAGE)>*
<.enter-package-scope>
[
|| <?[{]> <block>
|| <?[{]> { $*begin_compunit := 0; } <block>
|| ';'
<unit-block($*PKGDECL)>
[
|| <?{ $*begin_compunit }>
{ $*begin_compunit := 0; }
<unit-block($*PKGDECL)>
|| { $/.typed_panic("X::UnitScope::TooLate", what => $*PKGDECL); }
]
|| <.panic("Unable to parse $*PKGDECL definition")>
]
<.leave-block-scope>
Expand Down Expand Up @@ -2103,6 +2109,27 @@ grammar Raku::Grammar is HLL::Grammar does Raku::Common {
<trait($*BLOCK)>* :!s
{ $*IN_DECL := ''; }
[
|| ';'
{
if $<deflongname> ne 'MAIN' {
$/.typed_panic("X::UnitScope::Invalid", what => "sub",
where => "except on a MAIN sub", suggestion =>
'Please use the block form. If you did not mean to '
~ "declare a unit-scoped sub,\nperhaps you accidentally "
~ "placed a semicolon after routine's definition?"
);
}
unless $*begin_compunit {
$/.typed_panic("X::UnitScope::TooLate", what => "sub");
}
unless $*MULTINESS eq '' || $*MULTINESS eq 'only' {
$/.typed_panic("X::UnitScope::Invalid", what => "sub", where => "on a $*MULTINESS sub");
}
unless $*R.outer-scope =:= $*UNIT {
$/.typed_panic("X::UnitScope::Invalid", what => "sub", where => "in a subscope");
}
$*begin_compunit := 0;
}
|| <onlystar>
|| <blockoid>
]
Expand Down

0 comments on commit 95a5479

Please sign in to comment.