mirror of
https://github.com/golang/go
synced 2024-11-26 20:01:19 -07:00
cmd/compile/internal/syntax: differentiate between ';' and '\n' in syntax errors
Towards better syntax error messages: With this change, the parser knows whether a semicolon was an actual ';' in the source, or whether it was an automatically inserted semicolon as result of a '\n' or EOF. Using this information in error messages makes them more understandable. For #17328. Change-Id: I8cd9accee8681b62569d0ecef922d38682b401eb Reviewed-on: https://go-review.googlesource.com/36636 Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
parent
4f6d4bb3f4
commit
9799622f09
@ -144,7 +144,7 @@ func (p *parser) syntax_error_at(pos src.Pos, msg string) {
|
|||||||
// determine token string
|
// determine token string
|
||||||
var tok string
|
var tok string
|
||||||
switch p.tok {
|
switch p.tok {
|
||||||
case _Name:
|
case _Name, _Semi:
|
||||||
tok = p.lit
|
tok = p.lit
|
||||||
case _Literal:
|
case _Literal:
|
||||||
tok = "literal " + p.lit
|
tok = "literal " + p.lit
|
||||||
@ -215,7 +215,7 @@ func tokstring(tok token) string {
|
|||||||
case _Comma:
|
case _Comma:
|
||||||
return "comma"
|
return "comma"
|
||||||
case _Semi:
|
case _Semi:
|
||||||
return "semicolon or newline"
|
return "semicolon"
|
||||||
}
|
}
|
||||||
return tok.String()
|
return tok.String()
|
||||||
}
|
}
|
||||||
|
@ -27,7 +27,7 @@ type scanner struct {
|
|||||||
// current token, valid after calling next()
|
// current token, valid after calling next()
|
||||||
line, col uint
|
line, col uint
|
||||||
tok token
|
tok token
|
||||||
lit string // valid if tok is _Name or _Literal
|
lit string // valid if tok is _Name, _Literal, or _Semi ("semicolon", "newline", or "EOF")
|
||||||
kind LitKind // valid if tok is _Literal
|
kind LitKind // valid if tok is _Literal
|
||||||
op Operator // valid if tok is _Operator, _AssignOp, or _IncOp
|
op Operator // valid if tok is _Operator, _AssignOp, or _IncOp
|
||||||
prec int // valid if tok is _Operator, _AssignOp, or _IncOp
|
prec int // valid if tok is _Operator, _AssignOp, or _IncOp
|
||||||
@ -73,12 +73,14 @@ redo:
|
|||||||
switch c {
|
switch c {
|
||||||
case -1:
|
case -1:
|
||||||
if nlsemi {
|
if nlsemi {
|
||||||
|
s.lit = "EOF"
|
||||||
s.tok = _Semi
|
s.tok = _Semi
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
s.tok = _EOF
|
s.tok = _EOF
|
||||||
|
|
||||||
case '\n':
|
case '\n':
|
||||||
|
s.lit = "newline"
|
||||||
s.tok = _Semi
|
s.tok = _Semi
|
||||||
|
|
||||||
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
||||||
@ -106,6 +108,7 @@ redo:
|
|||||||
s.tok = _Comma
|
s.tok = _Comma
|
||||||
|
|
||||||
case ';':
|
case ';':
|
||||||
|
s.lit = "semicolon"
|
||||||
s.tok = _Semi
|
s.tok = _Semi
|
||||||
|
|
||||||
case ')':
|
case ')':
|
||||||
@ -185,6 +188,7 @@ redo:
|
|||||||
if s.source.line > s.line && nlsemi {
|
if s.source.line > s.line && nlsemi {
|
||||||
// A multi-line comment acts like a newline;
|
// A multi-line comment acts like a newline;
|
||||||
// it translates to a ';' if nlsemi is set.
|
// it translates to a ';' if nlsemi is set.
|
||||||
|
s.lit = "newline"
|
||||||
s.tok = _Semi
|
s.tok = _Semi
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -66,6 +66,11 @@ func TestTokens(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
switch want.tok {
|
switch want.tok {
|
||||||
|
case _Semi:
|
||||||
|
if got.lit != "semicolon" {
|
||||||
|
t.Errorf("got %s; want semicolon", got.lit)
|
||||||
|
}
|
||||||
|
|
||||||
case _Name, _Literal:
|
case _Name, _Literal:
|
||||||
if got.lit != want.src {
|
if got.lit != want.src {
|
||||||
t.Errorf("got lit = %q; want %q", got.lit, want.src)
|
t.Errorf("got lit = %q; want %q", got.lit, want.src)
|
||||||
@ -94,6 +99,9 @@ func TestTokens(t *testing.T) {
|
|||||||
t.Errorf("got tok = %s; want ;", got.tok)
|
t.Errorf("got tok = %s; want ;", got.tok)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
if got.lit != "newline" {
|
||||||
|
t.Errorf("got %s; want newline", got.lit)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
got.next()
|
got.next()
|
||||||
|
@ -9,6 +9,6 @@ package f
|
|||||||
import /* // ERROR "import path" */ `
|
import /* // ERROR "import path" */ `
|
||||||
bogus`
|
bogus`
|
||||||
|
|
||||||
func f(x int /* // ERROR "unexpected semicolon"
|
func f(x int /* // ERROR "unexpected newline"
|
||||||
|
|
||||||
*/)
|
*/)
|
||||||
|
@ -6,6 +6,6 @@
|
|||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
type T // ERROR "unexpected semicolon or newline in type declaration"
|
type T1 // ERROR "unexpected newline in type declaration"
|
||||||
// line below uncommented to avoid follow-up error
|
|
||||||
// {
|
type T2 /* // ERROR "unexpected EOF in type declaration" */
|
Loading…
Reference in New Issue
Block a user