Skip to content

Commit

Permalink
syntax: check for trailing junk in ParseCompoundStmt (google#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
adonovan authored Jan 25, 2019
1 parent e560c9b commit 1258e4d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
5 changes: 4 additions & 1 deletion syntax/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,11 @@ func ParseCompoundStmt(filename string, readline func() ([]byte, error)) (f *Fil
case NEWLINE:
// blank line
default:
// Don't consume newline, to avoid blocking again.
stmts = p.parseSimpleStmt(stmts, false)
// Require but don't consume newline, to avoid blocking again.
if p.tok != NEWLINE {
p.in.errorf(p.in.pos, "invalid syntax")
}
}

return &File{Path: filename, Stmts: stmts}, nil
Expand Down
17 changes: 11 additions & 6 deletions syntax/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,11 @@ func TestCompoundStmt(t *testing.T) {
// Even as a 1-liner, the following blank line is required.
{"if cond: pass\n\n",
`(IfStmt Cond=cond True=((BranchStmt Token=pass)))`},
// github.com/google/starlark-go/issues/121
{"a; b; c\n",
`(ExprStmt X=a)(ExprStmt X=b)(ExprStmt X=c)`},
{"a; b c\n",
`invalid syntax`},
} {

// Fake readline input from string.
Expand All @@ -301,14 +306,14 @@ func TestCompoundStmt(t *testing.T) {
return nil, sc.Err()
}

var got string
f, err := syntax.ParseCompoundStmt("foo.star", readline)
if err != nil {
t.Errorf("parse `%s` failed: %v", test.input, stripPos(err))
continue
}
var got string
for _, stmt := range f.Stmts {
got += treeString(stmt)
got = stripPos(err)
} else {
for _, stmt := range f.Stmts {
got += treeString(stmt)
}
}
if test.want != got {
t.Errorf("parse `%s` = %s, want %s", test.input, got, test.want)
Expand Down

0 comments on commit 1258e4d

Please sign in to comment.