Skip to content
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

go/parser: wrong syntax error #2088

Closed
rsc opened this issue Jul 21, 2011 · 3 comments
Closed

go/parser: wrong syntax error #2088

rsc opened this issue Jul 21, 2011 · 3 comments

Comments

@rsc
Copy link
Contributor

rsc commented Jul 21, 2011

$ cat x.go
package main

func f() {
    x(%v: hi", y)
}
$ gofmt x.go
x.go:4:2: illegal label declaration
x.go:5:3: expected '}', found 'EOF'
$ 

The first error in this file is the %.
go/parser should say something about the %
before complaining about the colon indirectly
via the label declaration.

It may also be worth checking in the tokenizer
for strings with newlines.  That's often a
helpful thing to point out in addition to what
the parser sees.  Same for newline in character
constant.

$ 6g x.go
x.go:4: syntax error: unexpected %
x.go:4: newline in string
$
@griesemer
Copy link
Contributor

Comment 1:

This issue is fixed with (pending) CL 4803047 ( http://golang.org/cl/4803047 ).
By default, the parser, gofmt, and gotype report at most one error per line as often
subsequent errors on a line are spurious follow-up errors. In this case, the parser
guesses the label declaration to start at the x and reports it as such but does not
report the erroneous % (because it's considered a spurious error).
Changed the label error reporting to use the '%' position instead. Now the first error
reported is for the '%':
gofmt test.go
test.go:4:7: expected operand, found '%'
test.go:5:2: expected '}', found 'EOF'
Also added a -e flag to gotype and gofmt to get all errors:
gofmt -e test.go
test.go:4:7: expected operand, found '%'
test.go:4:8: expected ')', found 'IDENT' v
test.go:4:9: illegal label declaration
test.go:4:11: expected ';', found 'IDENT' hi
test.go:4:13: string not terminated
test.go:5:2: expected '}', found 'EOF'
test.go:5:2: expected ';', found 'EOF'
The scanner always checked for newlines in strings (again, in this specific case that
error is only reported with the -e flag set).

@griesemer
Copy link
Contributor

Comment 2:

Correction: The previous comment should have said:
"Changed the label error reporting to use the ':' position instead."

@griesemer
Copy link
Contributor

Comment 3:

This issue was closed by revision fa49779.

Status changed to Fixed.

@rsc rsc added fixed labels Jul 22, 2011
@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants