diff --git a/compile/compile.go b/compile/compile.go index 775717a9..6b9d926d 100644 --- a/compile/compile.go +++ b/compile/compile.go @@ -9,6 +9,7 @@ package compile // FIXME kill ast.Identifier and turn into string? import ( + "bytes" "fmt" "log" "strings" @@ -107,7 +108,7 @@ func init() { // in addition to any features explicitly specified. func Compile(src, srcDesc string, mode py.CompileMode, futureFlags int, dont_inherit bool) (*py.Code, error) { // Parse Ast - Ast, err := parser.ParseString(src, mode) + Ast, err := parser.Parse(bytes.NewBufferString(src), srcDesc, mode) if err != nil { return nil, err } diff --git a/main.go b/main.go index bc906aac..4598a62d 100644 --- a/main.go +++ b/main.go @@ -78,7 +78,7 @@ func xmain(args []string) { _, err := py.RunFile(ctx, args[0], py.CompileOpts{}, nil) if err != nil { py.TracebackDump(err) - log.Fatal(err) + os.Exit(1) } } } diff --git a/parser/grammar_data_test.go b/parser/grammar_data_test.go index 4079325a..b0b53eeb 100644 --- a/parser/grammar_data_test.go +++ b/parser/grammar_data_test.go @@ -33,8 +33,8 @@ var grammarTestData = []struct { {"b'abc' b'''123'''", "eval", "Expression(body=Bytes(s=b'abc123'))", nil, ""}, {"1234", "eval", "Expression(body=Num(n=1234))", nil, ""}, {"01234", "eval", "", py.SyntaxError, "illegal decimal with leading zero"}, - {"1234d", "eval", "", py.SyntaxError, "invalid syntax"}, - {"1234d", "exec", "", py.SyntaxError, "invalid syntax"}, + {"1234d", "eval", "", py.SyntaxError, "unexpected EOF while parsing"}, + {"1234d", "exec", "", py.SyntaxError, "unexpected EOF while parsing"}, {"1234d", "single", "", py.SyntaxError, "unexpected EOF while parsing"}, {"0x1234", "eval", "Expression(body=Num(n=4660))", nil, ""}, {"12.34", "eval", "Expression(body=Num(n=12.34))", nil, ""}, @@ -325,10 +325,10 @@ var grammarTestData = []struct { {"pass\n", "single", "Interactive(body=[Pass()])", nil, ""}, {"if True:\n pass\n\n", "single", "Interactive(body=[If(test=NameConstant(value=True), body=[Pass()], orelse=[])])", nil, ""}, {"while True:\n pass\nelse:\n return\n", "single", "Interactive(body=[While(test=NameConstant(value=True), body=[Pass()], orelse=[Return(value=None)])])", nil, ""}, - {"a='potato", "eval", "", py.SyntaxError, "invalid syntax"}, + {"a='potato", "eval", "", py.SyntaxError, "unexpected EOF while parsing"}, {"a='potato", "exec", "", py.SyntaxError, "EOL while scanning string literal"}, {"a='potato", "single", "", py.SyntaxError, "EOL while scanning string literal"}, - {"a='''potato", "eval", "", py.SyntaxError, "invalid syntax"}, + {"a='''potato", "eval", "", py.SyntaxError, "unexpected EOF while parsing"}, {"a='''potato", "exec", "", py.SyntaxError, "EOF while scanning triple-quoted string literal"}, {"a='''potato", "single", "", py.SyntaxError, "EOF while scanning triple-quoted string literal"}, } diff --git a/parser/lexer.go b/parser/lexer.go index 76847893..801215ba 100644 --- a/parser/lexer.go +++ b/parser/lexer.go @@ -916,7 +916,7 @@ func (x *yyLex) SyntaxErrorf(format string, a ...interface{}) { func (x *yyLex) ErrorReturn() error { if x.error { if x.errorString == "" { - if x.eof && x.interactive { + if x.eof { x.errorString = "unexpected EOF while parsing" } else { x.errorString = "invalid syntax" diff --git a/py/traceback.go b/py/traceback.go index bf7ba6db..6dcc9ce6 100644 --- a/py/traceback.go +++ b/py/traceback.go @@ -64,10 +64,10 @@ func TracebackDump(err interface{}) { case *ExceptionInfo: e.TracebackDump(os.Stderr) case *Exception: - fmt.Fprintf(os.Stderr, "Exception %#v\n", e) + fmt.Fprintf(os.Stderr, "Exception %v\n", e) fmt.Fprintf(os.Stderr, "-- No traceback available --\n") default: - fmt.Fprintf(os.Stderr, "Error %#v\n", err) + fmt.Fprintf(os.Stderr, "Error %v\n", err) fmt.Fprintf(os.Stderr, "-- No traceback available --\n") } } diff --git a/repl/repl_test.go b/repl/repl_test.go index 06395234..b154bfec 100644 --- a/repl/repl_test.go +++ b/repl/repl_test.go @@ -25,6 +25,7 @@ func (rt *replTest) Print(out string) { } func (rt *replTest) assert(t *testing.T, what, wantPrompt, wantOut string) { + t.Helper() if rt.prompt != wantPrompt { t.Errorf("%s: Prompt wrong:\ngot= %q\nwant=%q", what, rt.prompt, wantPrompt) } @@ -63,7 +64,7 @@ func TestREPL(t *testing.T) { rt.assert(t, "multi#5", NormalPrompt, "45") r.Run("if") - rt.assert(t, "compileError", NormalPrompt, "Compile error: \n File \"\", line 1, offset 2\n if\n\n\nSyntaxError: 'invalid syntax'") + rt.assert(t, "compileError", NormalPrompt, "Compile error: \n File \"\", line 1, offset 2\n if\n\n\nSyntaxError: 'invalid syntax'") // test comments in the REPL work properly r.Run("# this is a comment")