Commit d1f5e5f4 authored by Robert Griesemer's avatar Robert Griesemer

cmd/compile/internal/syntax: always construct a correct syntax tree

- parser creates sensible nodes in case of syntax errors instead of nil
- a new BadExpr node is used in places where we can't do better
- fixed error message for incorrect type switch guard
- minor cleanups

Fixes #19663.

Change-Id: I028394c6db9cba7371f0e417ebf93f594659786a
Reviewed-on: https://go-review.googlesource.com/38653
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: 's avatarMatthew Dempsky <mdempsky@google.com>
parent ecc6a816
......@@ -646,7 +646,6 @@ var knownFormats = map[string]string{
"cmd/compile/internal/ssa.regMask %d": "",
"cmd/compile/internal/ssa.register %d": "",
"cmd/compile/internal/syntax.Expr %#v": "",
"cmd/compile/internal/syntax.Expr %s": "",
"cmd/compile/internal/syntax.Node %T": "",
"cmd/compile/internal/syntax.Operator %d": "",
"cmd/compile/internal/syntax.Operator %s": "",
......
......@@ -416,7 +416,7 @@ func (p *noder) exprs(exprs []syntax.Expr) []*Node {
func (p *noder) expr(expr syntax.Expr) *Node {
p.lineno(expr)
switch expr := expr.(type) {
case nil:
case nil, *syntax.BadExpr:
return nil
case *syntax.Name:
return p.mkname(expr)
......
......@@ -125,6 +125,12 @@ type (
aExpr()
}
// Placeholder for an expression that failed to parse
// correctly and where we can't provide a better node.
BadExpr struct {
expr
}
// Value
Name struct {
Value string
......
This diff is collapsed.
......@@ -345,6 +345,9 @@ func (p *printer) printRawNode(n Node) {
// we should not reach here but don't crash
// expressions and types
case *BadExpr:
p.print(_Name, "<bad expr>")
case *Name:
p.print(_Name, n.Value) // _Name requires actual value following immediately
......
......@@ -65,7 +65,7 @@ func Parse(base *src.PosBase, src io.Reader, errh ErrorHandler, pragh PragmaHand
var p parser
p.init(base, src, errh, pragh)
p.next()
return p.file(), p.first
return p.fileOrNil(), p.first
}
// ParseBytes behaves like Parse but it reads the source from the []byte slice provided.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment