Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Port most of STD's infix:lambda.
Gives good errors for if(1 < 2) { ... } style mistakes.
  • Loading branch information
jnthn committed Mar 9, 2013
1 parent a91c1e8 commit 8ea992a
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/Perl6/Grammar.pm
Expand Up @@ -1338,6 +1338,45 @@ grammar Perl6::Grammar is HLL::Grammar does STD {
$<sym> = [ '::?' <identifier> ] »
}

token infix:sym<lambda> {
<?before '{' | '->' > <!{ $*IN_META }> {
my $needparens := 0;
my $pos := $/.from;
my $line := HLL::Compiler.lineof($/.orig, $/.from, :cache(1));
my $lex := $*W.cur_lexpad();
for 'if', 'unless', 'while', 'until', 'for', 'given', 'when', 'loop', 'sub', 'method' {
$needparens++ if $_ eq 'loop';
my $m := %*MYSTERY{$_ ~ '-' ~ $lex.cuid};
next unless $m;
my $m_pos := $m<pos>[nqp::elems($m<pos>) - 1];
my $m_line := HLL::Compiler.lineof($/.orig, $m_pos, :cache(1));
if $line - $m_line < 5 {
if $m<ctx> eq '(' {
$/.CURSOR.'!clear_highwater'();
$/.CURSOR.'!cursor_pos'($m_pos);
$/.CURSOR.sorry("Word '$_' interpreted as '$_" ~ "()' function call; please use whitespace " ~
($needparens ?? 'around the parens' !! 'instead of parens'));
$/.CURSOR.'!cursor_pos'($pos);
$/.CURSOR.panic("Unexpected block in infix position (two terms in a row)");
}
else {
$/.CURSOR.'!clear_highwater'();
$/.CURSOR.'!cursor_pos'($m_pos);
$/.CURSOR.sorry("Word '$_' interpreted as a listop; please use 'do $_' to introduce the statement control word");
$/.CURSOR.'!cursor_pos'($pos);
$/.CURSOR.panic("Unexpected block in infix position (two terms in a row)");
}
}
}
}
[
|| <!{ $*IN_REDUCE }> {
$/.CURSOR.panic("Unexpected block in infix position (two terms in a row, or previous statement missing semicolon?)");
}
|| <!>
]
}

token term:sym<undef> {
<sym> >> {}
[ <?before \h*'$/' >
Expand Down

0 comments on commit 8ea992a

Please sign in to comment.