Skip to content

Commit

Permalink
syntax: allow newline after expression
Browse files Browse the repository at this point in the history
Change-Id: I3087d84e4be9f85a68ec088ea5935a7d18fd0407
  • Loading branch information
adonovan committed Oct 10, 2017
1 parent 78b3c34 commit ae06384
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 0 deletions.
6 changes: 6 additions & 0 deletions syntax/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ func ParseExpr(filename string, src interface{}) (expr Expr, err error) {
p.nextToken() // read first lookahead token
expr = p.parseTest()

// A following newline (e.g. "f()\n") appears outside any brackets,
// on a non-blank line, and is thus materialized.
if p.tok == NEWLINE {
p.nextToken()
}

if p.tok != EOF {
p.in.errorf(p.in.pos, "got %#v after expression, want EOF", p.tok)
}
Expand Down
2 changes: 2 additions & 0 deletions syntax/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ func TestExprParseTrees(t *testing.T) {
}{
{`print(1)`,
`(CallExpr Fn=print Args=(1))`},
{"print(1)\n",
`(CallExpr Fn=print Args=(1))`},
{`x + 1`,
`(BinaryExpr X=x Op=+ Y=1)`},
{`[x for x in y]`,
Expand Down
1 change: 1 addition & 0 deletions syntax/scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func TestScanner(t *testing.T) {
{`123 "foo" hello x.y`, `123 "foo" hello x . y EOF`},
{`print(x)`, "print ( x ) EOF"},
{`print(x); print(y)`, "print ( x ) ; print ( y ) EOF"},
{"\nprint(\n1\n)\n", "print ( 1 ) newline EOF"}, // final \n is at toplevel on non-blank line => token
{`/ // /= //= ///=`, "/ // /= //= // /= EOF"},
{`# hello
print(x)`, "print ( x ) EOF"},
Expand Down

0 comments on commit ae06384

Please sign in to comment.