Skip to content

Commit 9799622

Browse files
committed
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 <[email protected]>
1 parent 4f6d4bb commit 9799622

File tree

5 files changed

+19
-7
lines changed

5 files changed

+19
-7
lines changed

src/cmd/compile/internal/syntax/parser.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func (p *parser) syntax_error_at(pos src.Pos, msg string) {
144144
// determine token string
145145
var tok string
146146
switch p.tok {
147-
case _Name:
147+
case _Name, _Semi:
148148
tok = p.lit
149149
case _Literal:
150150
tok = "literal " + p.lit
@@ -215,7 +215,7 @@ func tokstring(tok token) string {
215215
case _Comma:
216216
return "comma"
217217
case _Semi:
218-
return "semicolon or newline"
218+
return "semicolon"
219219
}
220220
return tok.String()
221221
}

src/cmd/compile/internal/syntax/scanner.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type scanner struct {
2727
// current token, valid after calling next()
2828
line, col uint
2929
tok token
30-
lit string // valid if tok is _Name or _Literal
30+
lit string // valid if tok is _Name, _Literal, or _Semi ("semicolon", "newline", or "EOF")
3131
kind LitKind // valid if tok is _Literal
3232
op Operator // valid if tok is _Operator, _AssignOp, or _IncOp
3333
prec int // valid if tok is _Operator, _AssignOp, or _IncOp
@@ -73,12 +73,14 @@ redo:
7373
switch c {
7474
case -1:
7575
if nlsemi {
76+
s.lit = "EOF"
7677
s.tok = _Semi
7778
break
7879
}
7980
s.tok = _EOF
8081

8182
case '\n':
83+
s.lit = "newline"
8284
s.tok = _Semi
8385

8486
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
@@ -106,6 +108,7 @@ redo:
106108
s.tok = _Comma
107109

108110
case ';':
111+
s.lit = "semicolon"
109112
s.tok = _Semi
110113

111114
case ')':
@@ -185,6 +188,7 @@ redo:
185188
if s.source.line > s.line && nlsemi {
186189
// A multi-line comment acts like a newline;
187190
// it translates to a ';' if nlsemi is set.
191+
s.lit = "newline"
188192
s.tok = _Semi
189193
break
190194
}

src/cmd/compile/internal/syntax/scanner_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ func TestTokens(t *testing.T) {
6666
}
6767

6868
switch want.tok {
69+
case _Semi:
70+
if got.lit != "semicolon" {
71+
t.Errorf("got %s; want semicolon", got.lit)
72+
}
73+
6974
case _Name, _Literal:
7075
if got.lit != want.src {
7176
t.Errorf("got lit = %q; want %q", got.lit, want.src)
@@ -94,6 +99,9 @@ func TestTokens(t *testing.T) {
9499
t.Errorf("got tok = %s; want ;", got.tok)
95100
continue
96101
}
102+
if got.lit != "newline" {
103+
t.Errorf("got %s; want newline", got.lit)
104+
}
97105
}
98106

99107
got.next()

test/fixedbugs/issue14520.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ package f
99
import /* // ERROR "import path" */ `
1010
bogus`
1111

12-
func f(x int /* // ERROR "unexpected semicolon"
12+
func f(x int /* // ERROR "unexpected newline"
1313
1414
*/)

test/syntax/semi6.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66

77
package main
88

9-
type T // ERROR "unexpected semicolon or newline in type declaration"
10-
// line below uncommented to avoid follow-up error
11-
// {
9+
type T1 // ERROR "unexpected newline in type declaration"
10+
11+
type T2 /* // ERROR "unexpected EOF in type declaration" */

0 commit comments

Comments
 (0)