Skip to content

Improve some things around how errors are reported when parsing a file #177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package compile
// FIXME kill ast.Identifier and turn into string?

import (
"bytes"
"fmt"
"log"
"strings"
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
8 changes: 4 additions & 4 deletions parser/grammar_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, ""},
Expand Down Expand Up @@ -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"},
}
2 changes: 1 addition & 1 deletion parser/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions py/traceback.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
Expand Down
3 changes: 2 additions & 1 deletion repl/repl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 \"<string>\", line 1, offset 2\n if\n\n\nSyntaxError: 'invalid syntax'")
rt.assert(t, "compileError", NormalPrompt, "Compile error: \n File \"<stdin>\", line 1, offset 2\n if\n\n\nSyntaxError: 'invalid syntax'")

// test comments in the REPL work properly
r.Run("# this is a comment")
Expand Down