mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
cmd/compile: rationalize (lex)?lineno handling
Previously, many error messages inconsistantly used either lexlineno
and lineno. In general this works out okay because they're almost
always the same. The only exceptional case is after lexing a
multi-line raw string literal, where lineno will be the line number of
the opening quote and lexlineno is the line number of the closing
quote.
This CL makes the compiler's error message more consistent:
- Lexer error messages related to invalid byte sequences (i.e., NUL
bytes, bad UTF-8 sequences, and non-initial BOMs) are emitted at
lexlineno (i.e., the source line that contains the invalid byte
sequence).
- All other error messages (notably the parser's "syntax errors") now
use lineno. The minor change from this is that bogus input like:
package `
bogus`
will emit "syntax error: unexpected string literal, expecting name"
error at line 1, instead of line 2.
- Instead of maintaining prevlineno all the time, just record it
when/where actually needed and not already available elsewhere (which
turns out to be just one function).
- Lastly, we remove the legacy "syntax error near ..." fallback in
Yerror, now that the parser always emits more detailed syntax error
messages.
Change-Id: Iaf5f784223d0385fa3a5b09ef2b2ad447feab02f
Reviewed-on: https://go-review.googlesource.com/19925
Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
parent
687abca1ea
commit
e0fa809f4c
4 changed files with 26 additions and 34 deletions
|
|
@ -110,11 +110,11 @@ func (p *parser) syntax_error(msg string) {
|
|||
}
|
||||
|
||||
// Like syntax_error, but reports error at given line rather than current lexer line.
|
||||
func (p *parser) syntax_error_at(lineno int32, msg string) {
|
||||
defer func(lineno int32) {
|
||||
lexlineno = lineno
|
||||
}(lexlineno)
|
||||
lexlineno = lineno
|
||||
func (p *parser) syntax_error_at(lno int32, msg string) {
|
||||
defer func(lno int32) {
|
||||
lineno = lno
|
||||
}(lineno)
|
||||
lineno = lno
|
||||
p.syntax_error(msg)
|
||||
}
|
||||
|
||||
|
|
@ -687,7 +687,7 @@ func (p *parser) labeled_stmt(label *Node) *Node {
|
|||
ls = p.stmt()
|
||||
if ls == missing_stmt {
|
||||
// report error at line of ':' token
|
||||
p.syntax_error_at(prevlineno, "missing statement after label")
|
||||
p.syntax_error_at(label.Lineno, "missing statement after label")
|
||||
// we are already at the end of the labeled statement - no need to advance
|
||||
return missing_stmt
|
||||
}
|
||||
|
|
@ -1609,13 +1609,15 @@ func (p *parser) new_name(sym *Sym) *Node {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (p *parser) dcl_name(sym *Sym) *Node {
|
||||
func (p *parser) dcl_name() *Node {
|
||||
if trace && Debug['x'] != 0 {
|
||||
defer p.trace("dcl_name")()
|
||||
}
|
||||
|
||||
symlineno := lineno
|
||||
sym := p.sym()
|
||||
if sym == nil {
|
||||
yyerrorl(int(prevlineno), "invalid declaration")
|
||||
yyerrorl(int(symlineno), "invalid declaration")
|
||||
return nil
|
||||
}
|
||||
return dclname(sym)
|
||||
|
|
@ -2637,9 +2639,9 @@ func (p *parser) dcl_name_list() *NodeList {
|
|||
defer p.trace("dcl_name_list")()
|
||||
}
|
||||
|
||||
l := list1(p.dcl_name(p.sym()))
|
||||
l := list1(p.dcl_name())
|
||||
for p.got(',') {
|
||||
l = list(l, p.dcl_name(p.sym()))
|
||||
l = list(l, p.dcl_name())
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue