Skip to content

Commit

Permalink
Fixed panic when bailout is triggered in parser.ParseFile() (#113)
Browse files Browse the repository at this point in the history
* Fixed panic when bailout is triggered in parser.ParseFile()

* Removed redundant "parse error" prefix for parse errors.
  • Loading branch information
geseq authored and d5 committed Feb 25, 2019
1 parent 6dd573c commit 6ec360c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
16 changes: 16 additions & 0 deletions compiler/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,22 @@ func() {
intObject(1))))

expectError(t, `import("user1")`, "no such file or directory") // unknown module name

expectError(t, `
r["x"] = {
"a":1,
"b":1,
"c":1,
"d":1,
"e":1,
"f":1,
"g":1,
"h":1,
"i":1,
"j":1,
"k":1,
}
`, "Parse Error: expected 'IDENT', found \"a\"\n\tat test:3:5 (and 10 more errors)") // too many errors
}

func concat(instructions ...[]byte) []byte {
Expand Down
19 changes: 16 additions & 3 deletions compiler/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,18 @@ func NewParser(file *source.File, src []byte, trace io.Writer) *Parser {
}

// ParseFile parses the source and returns an AST file unit.
func (p *Parser) ParseFile() (*ast.File, error) {
func (p *Parser) ParseFile() (file *ast.File, err error) {
defer func() {
if e := recover(); e != nil {
if _, ok := e.(bailout); !ok {
panic(e)
}
}

p.errors.Sort()
err = p.errors.Err()
}()

if p.trace {
defer un(trace(p, "File"))
}
Expand All @@ -71,10 +82,12 @@ func (p *Parser) ParseFile() (*ast.File, error) {
return nil, p.errors.Err()
}

return &ast.File{
file = &ast.File{
InputFile: p.file,
Stmts: stmts,
}, nil
}

return
}

func (p *Parser) parseExpr() ast.Expr {
Expand Down
2 changes: 1 addition & 1 deletion script/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (s *Script) Compile() (*Compiled, error) {
p := parser.NewParser(srcFile, s.input, nil)
file, err := p.ParseFile()
if err != nil {
return nil, fmt.Errorf("parse error: %s", err.Error())
return nil, err
}

c := compiler.NewCompiler(srcFile, symbolTable, nil, stdModules, nil)
Expand Down

0 comments on commit 6ec360c

Please sign in to comment.