Skip to content

Commit

Permalink
Parse signature declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
niner committed May 12, 2022
1 parent 27c8f6e commit 745d8ac
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
30 changes: 30 additions & 0 deletions src/Raku/Actions.nqp
Original file line number Diff line number Diff line change
Expand Up @@ -1103,6 +1103,36 @@ class Raku::Actions is HLL::Actions does Raku::CommonActions {
if $<variable_declarator> {
self.attach: $/, $<variable_declarator>.ast;
}
elsif $<signature> {
my $signature := $<signature>.ast;
my str $scope := $*SCOPE;
my $type := $*OFTYPE ?? $*OFTYPE.ast !! self.r('Type');
my $initializer := $<initializer>
#?? $<initializer>.ast
?? self.r('Initializer')
!! self.r('Initializer');
for $signature.IMPL-UNWRAP-LIST($signature.parameters) {
my str $name := $_.target.lexical-name;
if $name {
my $decl := self.r('VarDeclaration', 'Simple').new:
:$scope, :$type, :$name, :$initializer;
if $scope eq 'my' || $scope eq 'state' || $scope eq 'our' {
$*R.declare-lexical($decl);
}
self.attach: $/, $decl;
}
else {
if $scope ne 'my' && $scope ne 'state' {
$/.panic("Cannot declare an anonymous {$scope}-scoped variable");
}
if $<twigil> {
$/.panic("Cannot declare an anonymous variable with a twigil");
}
self.attach: $/, self.r('VarDeclaration', 'Anonymous').new:
:$scope, :$type, :sigil(~$<sigil>), :$initializer;
}
}
}
elsif $<routine_declarator> {
self.attach: $/, $<routine_declarator>.ast;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Raku/Grammar.nqp
Original file line number Diff line number Diff line change
Expand Up @@ -1630,7 +1630,7 @@ grammar Raku::Grammar is HLL::Grammar does Raku::Common {
| $<sigil>=['$'] $<desigilname>=[<[/_!¢]>]
| <sigil> $<index>=[\d+]
| <sigil> <?[<]> <postcircumfix>
| <?before <.sigil> <.?[ ( [ { ]>> <!RESTRICTED> <contextualizer>
| <?before <.sigil> <.?[ ( [ { ]>> <!RESTRICTED> <?{ !$*IN_DECL }> <contextualizer>
| {} <sigil> <!{ $*QSIGIL }> <?MARKER('baresigil')> # try last, to allow sublanguages to redefine sigils (like & in regex)
]
{ $*LEFTSIGIL := nqp::substr(self.orig(), self.from, 1) unless $*LEFTSIGIL }
Expand Down Expand Up @@ -1784,6 +1784,7 @@ grammar Raku::Grammar is HLL::Grammar does Raku::Common {
| '\\' <defterm>
[ <.ws> <term_init=initializer> || <.typed_panic: "X::Syntax::Term::MissingInitializer"> ]
| <variable_declarator>
| '(' ~ ')' <signature> [ <.ws> <trait>+ ]? [ <.ws> <initializer> ]?
| <routine_declarator>
]
}
Expand Down

0 comments on commit 745d8ac

Please sign in to comment.