Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Make indentation handling less broken.
We're probably not all the way there just yet, but at least it's less
hilariously broken this way than it was.
  • Loading branch information
arnsholt committed Oct 12, 2013
1 parent 5e0b0a8 commit 85167cd
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 21 deletions.
19 changes: 1 addition & 18 deletions src/Snake/Actions.nqp
Expand Up @@ -29,31 +29,14 @@ method string($/) { make $<quote_EXPR>.ast; }
#method infix:sym<==>($/) { ... }
#method infix:sym<!=>($/) { ... }

method INDENT($/) {
my $new := $<sports>.ast;
my $old := @*INDENT[0];
if $new > $old {
nqp::unshift_i(@*INDENT, $new);
}
else {
nqp::die("Bad indentation: new level $new seen, but old level is greater ($old)");
}
}
method INDENT($/) { nqp::unshift_i(@*INDENT, $<sports>.ast); }

method DEDENT($/) {
my $new := $<EOF> ?? 0 !! $<sports>.ast;
nqp::shift_i(@*INDENT) while $new < @*INDENT[0];
nqp::die("Bad dedent: saw $new but expected @*INDENT[0]") if $new != @*INDENT[0];
}
method check-indent($/) {
if $<sports> {
my $got := $<sports>.ast;
my $expected := @*INDENT[0];
nqp::die("Incorrect indentation: saw $got when expecting $expected") if $got != $expected;
}
}
method sports($/) {
my $indent := 0;
$indent := $indent + nqp::chars(~$/[0]);
Expand Down
6 changes: 3 additions & 3 deletions src/Snake/Grammar.nqp
Expand Up @@ -115,17 +115,17 @@ token INDENT {
# Gobble up leading whitespace, push new indent onto stack (or die if bad
# indent).
#<!> # TODO
^^ <sports> <.MARKER: 'INDENT'> || <.panic: "Dedent not at beginning of line!">
^^ <sports> <?{ $<sports>.ast > @*INDENT[0] }> <.MARKER: 'INDENT'> || <.panic: "Dedent not at beginning of line!">
}

token DEDENT {
# Gobble up leading whitespace, pop until we're done (or die if bad
# indent).
[^^ <sports> <.MARKER: 'INDENT'> | $<EOF>=<?> $] || <.panic: "Indent not at beginning of line!">
[^^ <sports> <?{ $<sports>.ast < @*INDENT[0] }> <.MARKER: 'INDENT'> | $<EOF>=<?> $] || <.panic: "Indent not at beginning of line!">
}

token check-indent {
<.MARKED: 'INDENT'> || ^^ <sports>
<.MARKED: 'INDENT'> || ^^ <sports> <?{ $<sports>.ast == @*INDENT[0] }>
}

# Spaces or tabs. A valid Python indent consists of any number of spaces, then
Expand Down

0 comments on commit 85167cd

Please sign in to comment.