all: merge dev.inline into master

Change-Id: I7715581a04e513dcda9918e853fa6b1ddc703770
This commit is contained in:
Russ Cox 2017-02-01 09:35:27 -05:00
commit 47ce87877b
124 changed files with 6096 additions and 5655 deletions

View file

@ -5,8 +5,10 @@
package syntax
import (
"cmd/internal/src"
"fmt"
"io"
"strconv"
"strings"
)
@ -19,21 +21,67 @@ const trace = false
const gcCompat = true
type parser struct {
base *src.PosBase
errh ErrorHandler
scanner
first error // first error encountered
pragma Pragma // pragma flags
fnest int // function nesting level (for error handling)
xnest int // expression nesting level (for complit ambiguity resolution)
indent []byte // tracing support
}
func (p *parser) init(src io.Reader, errh ErrorHandler, pragh PragmaHandler) {
p.scanner.init(src, errh, pragh)
func (p *parser) init(base *src.PosBase, r io.Reader, errh ErrorHandler, pragh PragmaHandler) {
p.base = base
p.errh = errh
p.scanner.init(
r,
// Error and pragma handlers for scanner.
// Because the (line, col) positions passed to these
// handlers are always at or after the current reading
// position, it is save to use the most recent position
// base to compute the corresponding Pos value.
func(line, col uint, msg string) {
p.error_at(p.pos_at(line, col), msg)
},
func(line, col uint, text string) {
if strings.HasPrefix(text, "line ") {
p.updateBase(line, col+5, text[5:])
return
}
if pragh != nil {
p.pragma |= pragh(p.pos_at(line, col), text)
}
},
)
p.first = nil
p.pragma = 0
p.fnest = 0
p.xnest = 0
p.indent = nil
}
const lineMax = 1<<24 - 1 // TODO(gri) this limit is defined for src.Pos - fix
func (p *parser) updateBase(line, col uint, text string) {
// Want to use LastIndexByte below but it's not defined in Go1.4 and bootstrap fails.
i := strings.LastIndex(text, ":") // look from right (Windows filenames may contain ':')
if i < 0 {
return // ignore (not a line directive)
}
nstr := text[i+1:]
n, err := strconv.Atoi(nstr)
if err != nil || n <= 0 || n > lineMax {
p.error_at(p.pos_at(line, col+uint(i+1)), "invalid line number: "+nstr)
return
}
p.base = src.NewLinePragmaBase(src.MakePos(p.base.Pos().Base(), line, col), text[:i], uint(n))
}
func (p *parser) got(tok token) bool {
if p.tok == tok {
p.next()
@ -52,13 +100,25 @@ func (p *parser) want(tok token) {
// ----------------------------------------------------------------------------
// Error handling
// syntax_error reports a syntax error at the current line.
func (p *parser) syntax_error(msg string) {
p.syntax_error_at(p.pos, p.line, msg)
// pos_at returns the Pos value for (line, col) and the current position base.
func (p *parser) pos_at(line, col uint) src.Pos {
return src.MakePos(p.base, line, col)
}
// Like syntax_error, but reports error at given line rather than current lexer line.
func (p *parser) syntax_error_at(pos, line int, msg string) {
// error reports an error at the given position.
func (p *parser) error_at(pos src.Pos, msg string) {
err := Error{pos, msg}
if p.first == nil {
p.first = err
}
if p.errh == nil {
panic(p.first)
}
p.errh(err)
}
// syntax_error_at reports a syntax error at the given position.
func (p *parser) syntax_error_at(pos src.Pos, msg string) {
if trace {
defer p.trace("syntax_error (" + msg + ")")()
}
@ -77,7 +137,7 @@ func (p *parser) syntax_error_at(pos, line int, msg string) {
msg = ", " + msg
default:
// plain error - we don't care about current token
p.error_at(pos, line, "syntax error: "+msg)
p.error_at(pos, "syntax error: "+msg)
return
}
@ -99,9 +159,14 @@ func (p *parser) syntax_error_at(pos, line int, msg string) {
tok = tokstring(p.tok)
}
p.error_at(pos, line, "syntax error: unexpected "+tok+msg)
p.error_at(pos, "syntax error: unexpected "+tok+msg)
}
// Convenience methods using the current token position.
func (p *parser) pos() src.Pos { return p.pos_at(p.line, p.col) }
func (p *parser) error(msg string) { p.error_at(p.pos(), msg) }
func (p *parser) syntax_error(msg string) { p.syntax_error_at(p.pos(), msg) }
// The stopset contains keywords that start a statement.
// They are good synchronization points in case of syntax
// errors and (usually) shouldn't be skipped over.
@ -429,7 +494,7 @@ func (p *parser) funcDecl() *FuncDecl {
f.Body = p.funcBody()
f.Pragma = p.pragma
f.EndLine = uint32(p.line)
f.EndLine = p.line
// TODO(gri) deal with function properties
// if noescape && body != nil {
@ -652,7 +717,7 @@ func (p *parser) operand(keep_parens bool) Expr {
f.init(p)
f.Type = t
f.Body = p.funcBody()
f.EndLine = uint32(p.line)
f.EndLine = p.line
p.xnest--
p.fnest--
return f
@ -873,7 +938,7 @@ func (p *parser) complitexpr() *CompositeLit {
}
}
x.EndLine = uint32(p.line)
x.EndLine = p.line
p.xnest--
p.want(_Rbrace)
@ -1198,7 +1263,7 @@ func (p *parser) fieldDecl(styp *StructType) {
p.want(_Rparen)
tag := p.oliteral()
p.addField(styp, nil, typ, tag)
p.error("cannot parenthesize embedded type")
p.syntax_error("cannot parenthesize embedded type")
} else {
// '(' embed ')' oliteral
@ -1206,7 +1271,7 @@ func (p *parser) fieldDecl(styp *StructType) {
p.want(_Rparen)
tag := p.oliteral()
p.addField(styp, nil, typ, tag)
p.error("cannot parenthesize embedded type")
p.syntax_error("cannot parenthesize embedded type")
}
case _Star:
@ -1217,7 +1282,7 @@ func (p *parser) fieldDecl(styp *StructType) {
p.want(_Rparen)
tag := p.oliteral()
p.addField(styp, nil, typ, tag)
p.error("cannot parenthesize embedded type")
p.syntax_error("cannot parenthesize embedded type")
} else {
// '*' embed oliteral
@ -1285,7 +1350,7 @@ func (p *parser) methodDecl() *Field {
f.init(p)
f.Type = p.qualifiedName(nil)
p.want(_Rparen)
p.error("cannot parenthesize embedded type")
p.syntax_error("cannot parenthesize embedded type")
return f
default:
@ -1352,7 +1417,7 @@ func (p *parser) dotsType() *DotsType {
p.want(_DotDotDot)
t.Elem = p.tryType()
if t.Elem == nil {
p.error("final argument in variadic function missing type")
p.syntax_error("final argument in variadic function missing type")
}
return t
@ -1563,7 +1628,7 @@ func (p *parser) labeledStmt(label *Name) Stmt {
s.Stmt = p.stmt()
if s.Stmt == missing_stmt {
// report error at line of ':' token
p.syntax_error_at(int(label.pos), int(label.line), "missing statement after label")
p.syntax_error_at(label.Pos(), "missing statement after label")
// we are already at the end of the labeled statement - no need to advance
return missing_stmt
}
@ -1646,7 +1711,7 @@ func (p *parser) header(forStmt bool) (init SimpleStmt, cond Expr, post SimpleSt
if p.tok != _Semi {
// accept potential varDecl but complain
if forStmt && p.got(_Var) {
p.error("var declaration not allowed in for initializer")
p.syntax_error("var declaration not allowed in for initializer")
}
init = p.simpleStmt(nil, forStmt)
// If we have a range clause, we are done.
@ -1699,7 +1764,7 @@ func (p *parser) ifStmt() *IfStmt {
p.want(_If)
s.Init, s.Cond, _ = p.header(false)
if s.Cond == nil {
p.error("missing condition in if statement")
p.syntax_error("missing condition in if statement")
}
if gcCompat {
@ -1715,7 +1780,7 @@ func (p *parser) ifStmt() *IfStmt {
case _Lbrace:
s.Else = p.blockStmt()
default:
p.error("else must be followed by if or statement block")
p.syntax_error("else must be followed by if or statement block")
p.advance(_Name, _Rbrace)
}
}
@ -2088,7 +2153,7 @@ func (p *parser) exprList() Expr {
list = append(list, p.expr())
}
t := new(ListExpr)
t.init(p) // TODO(gri) what is the correct thing here?
t.pos = x.Pos()
t.ElemList = list
x = t
}