Skip to content

Commit

Permalink
syntax: fix outdent bug in scanner (google#105)
Browse files Browse the repository at this point in the history
...caused by typo in slice expression.

Also:
- simplify expression for detecting blank lines. (I don't remember why
  the col > 0 clause was added; perhaps to mask this bug.)
- add missing tokenNames[WHILE].

Fixes google#104
  • Loading branch information
adonovan authored Jan 31, 2019
1 parent 367b72a commit c1a3d54
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
12 changes: 7 additions & 5 deletions syntax/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ var tokenNames = [...]string{
OR: "or",
PASS: "pass",
RETURN: "return",
WHILE: "while",
}

// A Position describes the location of a rune of input.
Expand Down Expand Up @@ -476,8 +477,9 @@ start:
break
}
}
// The third clause is "trailing spaces without newline at EOF".
if c == '#' || c == '\n' || c == 0 && col > 0 {

// The third clause matches EOF.
if c == '#' || c == '\n' || c == 0 {
blank = true
}

Expand All @@ -490,7 +492,7 @@ start:
sc.dents++
sc.indentstk = append(sc.indentstk, col)
} else if col < cur {
// dedent(s)
// outdent(s)
for len(sc.indentstk) > 0 && col < sc.indentstk[len(sc.indentstk)-1] {
sc.dents--
sc.indentstk = sc.indentstk[:len(sc.indentstk)-1] // pop
Expand Down Expand Up @@ -562,7 +564,7 @@ start:
goto start
} else if len(sc.indentstk) > 1 {
sc.dents = 1 - len(sc.indentstk)
sc.indentstk = sc.indentstk[1:]
sc.indentstk = sc.indentstk[:1]
goto start
}
}
Expand All @@ -581,7 +583,7 @@ start:
if len(sc.indentstk) > 1 {
if savedLineStart {
sc.dents = 1 - len(sc.indentstk)
sc.indentstk = sc.indentstk[1:]
sc.indentstk = sc.indentstk[:1]
goto start
} else {
sc.lineStart = true
Expand Down
3 changes: 3 additions & 0 deletions syntax/scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ pass`, "pass newline pass EOF"}, // consecutive newlines are consolidated
// github.com/google/starlark-go/issues/80
{"([{<>}])", "( [ { < > } ] ) EOF"},
{"f();", "f ( ) ; EOF"},
// github.com/google/starlark-go/issues/104
{"def f():\n if x:\n pass\n ", `def f ( ) : newline indent if x : newline indent pass newline outdent outdent EOF`},
{`while cond: pass`, "while cond : pass EOF"},
// github.com/google/starlark-go/issues/107
{"~= ~= 5", "~ = ~ = 5 EOF"},
} {
Expand Down

0 comments on commit c1a3d54

Please sign in to comment.