// base to compute the corresponding Pos value.
func(line, col uint, msg string) {
if msg[0] != '/' {
- p.error_at(p.pos_at(line, col), msg)
+ p.errorAt(p.posAt(line, col), msg)
return
}
// go: directive (but be conservative and test)
if pragh != nil && strings.HasPrefix(text, "go:") {
- p.pragma |= pragh(p.pos_at(line, col), text)
+ p.pragma |= pragh(p.posAt(line, col), text)
}
},
directives,
if !ok {
// text has a suffix :xxx but xxx is not a number
- p.error_at(p.pos_at(line, col+i), "invalid line number: "+text[i:])
+ p.errorAt(p.posAt(line, col+i), "invalid line number: "+text[i:])
return
}
i, i2 = i2, i
n, n2 = n2, n
if n2 == 0 {
- p.error_at(p.pos_at(line, col+i2), "invalid column number: "+text[i2:])
+ p.errorAt(p.posAt(line, col+i2), "invalid column number: "+text[i2:])
return
}
text = text[:i2-1] // lop off :col
}
if n == 0 || n > lineMax {
- p.error_at(p.pos_at(line, col+i), "invalid line number: "+text[i:])
+ p.errorAt(p.posAt(line, col+i), "invalid line number: "+text[i:])
return
}
func (p *parser) want(tok token) {
if !p.got(tok) {
- p.syntax_error("expecting " + tokstring(tok))
+ p.syntaxError("expecting " + tokstring(tok))
p.advance()
}
}
// ----------------------------------------------------------------------------
// Error handling
-// pos_at returns the Pos value for (line, col) and the current position base.
-func (p *parser) pos_at(line, col uint) src.Pos {
+// posAt returns the Pos value for (line, col) and the current position base.
+func (p *parser) posAt(line, col uint) src.Pos {
return src.MakePos(p.base, line, col)
}
// error reports an error at the given position.
-func (p *parser) error_at(pos src.Pos, msg string) {
+func (p *parser) errorAt(pos src.Pos, msg string) {
err := Error{pos, msg}
if p.first == nil {
p.first = err
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) {
+// syntaxErrorAt reports a syntax error at the given position.
+func (p *parser) syntaxErrorAt(pos src.Pos, msg string) {
if trace {
p.print("syntax error: " + msg)
}
msg = ", " + msg
default:
// plain error - we don't care about current token
- p.error_at(pos, "syntax error: "+msg)
+ p.errorAt(pos, "syntax error: "+msg)
return
}
tok = tokstring(p.tok)
}
- p.error_at(pos, "syntax error: unexpected "+tok+msg)
+ p.errorAt(pos, "syntax error: unexpected "+tok+msg)
}
// tokstring returns the English word for selected punctuation tokens
}
// 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) }
+func (p *parser) pos() src.Pos { return p.posAt(p.line, p.col) }
+func (p *parser) syntaxError(msg string) { p.syntaxErrorAt(p.pos(), msg) }
// The stopset contains keywords that start a statement.
// They are good synchronization points in case of syntax
// PackageClause
if !p.got(_Package) {
- p.syntax_error("package statement must be first")
+ p.syntaxError("package statement must be first")
return nil
}
f.PkgName = p.name()
default:
if p.tok == _Lbrace && len(f.DeclList) > 0 && isEmptyFuncDecl(f.DeclList[len(f.DeclList)-1]) {
// opening { of function declaration on next line
- p.syntax_error("unexpected semicolon or newline before {")
+ p.syntaxError("unexpected semicolon or newline before {")
} else {
- p.syntax_error("non-declaration statement outside function body")
+ p.syntaxError("non-declaration statement outside function body")
}
p.advance(_Const, _Type, _Var, _Func)
continue
p.pragma = 0
if p.tok != _EOF && !p.got(_Semi) {
- p.syntax_error("after top level declaration")
+ p.syntaxError("after top level declaration")
p.advance(_Const, _Type, _Var, _Func)
}
}
done = f()
// sep is optional before close
if !p.got(sep) && p.tok != close {
- p.syntax_error(fmt.Sprintf("expecting %s or %s", tokstring(sep), tokstring(close)))
+ p.syntaxError(fmt.Sprintf("expecting %s or %s", tokstring(sep), tokstring(close)))
p.advance(_Rparen, _Rbrack, _Rbrace)
if p.tok != close {
// position could be better but we had an error so we don't care
}
d.Path = p.oliteral()
if d.Path == nil {
- p.syntax_error("missing import path")
+ p.syntaxError("missing import path")
p.advance(_Semi, _Rparen)
return nil
}
d.Type = p.typeOrNil()
if d.Type == nil {
d.Type = p.bad()
- p.syntax_error("in type declaration")
+ p.syntaxError("in type declaration")
p.advance(_Semi, _Rparen)
}
d.Group = group
}
if p.tok != _Name {
- p.syntax_error("expecting name or (")
+ p.syntaxError("expecting name or (")
p.advance(_Lbrace, _Semi)
return nil
}
if dir == RecvOnly {
// t is type <-chan E but <-<-chan E is not permitted
// (report same error as for "type _ <-<-chan E")
- p.syntax_error("unexpected <-, expecting chan")
+ p.syntaxError("unexpected <-, expecting chan")
// already progressed, no need to advance
}
c.Dir = RecvOnly
if dir == SendOnly {
// channel dir is <- but channel element E is not a channel
// (report same error as for "type _ <-chan<-E")
- p.syntax_error(fmt.Sprintf("unexpected %s, expecting chan", String(t)))
+ p.syntaxError(fmt.Sprintf("unexpected %s, expecting chan", String(t)))
// already progressed, no need to advance
}
return x
x := p.pexpr(p.tok == _Lparen) // keep_parens so we can report error below
if t := unparen(x); t != x {
- p.error(fmt.Sprintf("expression in %s must not be parenthesized", s.Tok))
+ p.errorAt(x.Pos(), fmt.Sprintf("expression in %s must not be parenthesized", s.Tok))
// already progressed, no need to advance
x = t
}
cx, ok := x.(*CallExpr)
if !ok {
- p.error(fmt.Sprintf("expression in %s must be function call", s.Tok))
+ p.errorAt(x.Pos(), fmt.Sprintf("expression in %s must be function call", s.Tok))
// already progressed, no need to advance
cx = new(CallExpr)
cx.pos = x.Pos()
default:
x := p.bad()
- p.syntax_error("expecting expression")
+ p.syntaxError("expecting expression")
p.advance()
return x
}
p.want(_Rparen)
default:
- p.syntax_error("expecting name or (")
+ p.syntaxError("expecting name or (")
p.advance(_Semi, _Rparen)
}
break loop
}
if t != x {
- p.syntax_error("cannot parenthesize type in composite literal")
+ p.syntaxError("cannot parenthesize type in composite literal")
// already progressed, no need to advance
}
n := p.complitexpr()
typ := p.typeOrNil()
if typ == nil {
typ = p.bad()
- p.syntax_error("expecting type")
+ p.syntaxError("expecting type")
p.advance(_Comma, _Colon, _Semi, _Rparen, _Rbrack, _Rbrace)
}
typ := p.typeOrNil()
if typ == nil {
typ = p.bad()
- p.syntax_error("missing channel element type")
+ p.syntaxError("missing channel element type")
// assume element type is simply absent - don't advance
}
p.want(_Rparen)
tag := p.oliteral()
p.addField(styp, pos, nil, typ, tag)
- p.syntax_error("cannot parenthesize embedded type")
+ p.syntaxError("cannot parenthesize embedded type")
} else {
// '(' embed ')' oliteral
p.want(_Rparen)
tag := p.oliteral()
p.addField(styp, pos, nil, typ, tag)
- p.syntax_error("cannot parenthesize embedded type")
+ p.syntaxError("cannot parenthesize embedded type")
}
case _Star:
p.want(_Rparen)
tag := p.oliteral()
p.addField(styp, pos, nil, typ, tag)
- p.syntax_error("cannot parenthesize embedded type")
+ p.syntaxError("cannot parenthesize embedded type")
} else {
// '*' embed oliteral
}
default:
- p.syntax_error("expecting field name or embedded type")
+ p.syntaxError("expecting field name or embedded type")
p.advance(_Semi, _Rbrace)
}
}
hasNameList = true
}
if hasNameList {
- p.syntax_error("name list not allowed in interface type")
+ p.syntaxError("name list not allowed in interface type")
// already progressed, no need to advance
}
return f
case _Lparen:
- p.syntax_error("cannot parenthesize embedded type")
+ p.syntaxError("cannot parenthesize embedded type")
f := new(Field)
f.pos = p.pos()
p.next()
return f
default:
- p.syntax_error("expecting method or interface name")
+ p.syntaxError("expecting method or interface name")
p.advance(_Semi, _Rbrace)
return nil
}
f.Type = p.dotsType()
default:
- p.syntax_error("expecting )")
+ p.syntaxError("expecting )")
p.advance(_Comma, _Rparen)
return nil
}
t.Elem = p.typeOrNil()
if t.Elem == nil {
t.Elem = p.bad()
- p.syntax_error("final argument in variadic function missing type")
+ p.syntaxError("final argument in variadic function missing type")
}
return t
}
}
if !ok {
- p.syntax_error_at(pos, "mixed named and unnamed function parameters")
+ p.syntaxErrorAt(pos, "mixed named and unnamed function parameters")
}
}
case *Name:
x.Lhs = lhs
case *ListExpr:
- p.error_at(lhs.Pos(), fmt.Sprintf("cannot assign 1 value to %d variables", len(lhs.ElemList)))
+ p.errorAt(lhs.Pos(), fmt.Sprintf("cannot assign 1 value to %d variables", len(lhs.ElemList)))
// make the best of what we have
if lhs, ok := lhs.ElemList[0].(*Name); ok {
x.Lhs = lhs
}
default:
- p.error_at(lhs.Pos(), fmt.Sprintf("invalid variable name %s in type switch", String(lhs)))
+ p.errorAt(lhs.Pos(), fmt.Sprintf("invalid variable name %s in type switch", String(lhs)))
}
s := new(ExprStmt)
s.pos = x.Pos()
return as
default:
- p.syntax_error("expecting := or = or comma")
+ p.syntaxError("expecting := or = or comma")
p.advance(_Semi, _Rbrace)
// make the best of what we have
if x, ok := lhs.(*ListExpr); ok {
}
// report error at line of ':' token
- p.syntax_error_at(s.pos, "missing statement after label")
+ p.syntaxErrorAt(s.pos, "missing statement after label")
// we are already at the end of the labeled statement - no need to advance
return nil // avoids follow-on errors (see e.g., fixedbugs/bug274.go)
}
// people coming from C may forget that braces are mandatory in Go
if !p.got(_Lbrace) {
- p.syntax_error("expecting { after " + context)
+ p.syntaxError("expecting { after " + context)
p.advance(_Name, _Rbrace)
s.Rbrace = p.pos() // in case we found "}"
if p.got(_Rbrace) {
if p.tok == _Lbrace {
if keyword == _If {
- p.syntax_error("missing condition in if statement")
+ p.syntaxError("missing condition in if statement")
}
return
}
if p.tok != _Semi {
// accept potential varDecl but complain
if p.got(_Var) {
- p.syntax_error(fmt.Sprintf("var declaration not allowed in %s initializer", keyword.String()))
+ p.syntaxError(fmt.Sprintf("var declaration not allowed in %s initializer", keyword.String()))
}
init = p.simpleStmt(nil, keyword == _For)
// If we have a range clause, we are done (can only happen for keyword == _For).
if keyword == _For {
if p.tok != _Semi {
if p.tok == _Lbrace {
- p.syntax_error("expecting for loop condition")
+ p.syntaxError("expecting for loop condition")
goto done
}
condStmt = p.simpleStmt(nil, false)
if p.tok != _Lbrace {
post = p.simpleStmt(nil, false)
if a, _ := post.(*AssignStmt); a != nil && a.Op == Def {
- p.syntax_error_at(a.Pos(), "cannot declare in post statement of for loop")
+ p.syntaxErrorAt(a.Pos(), "cannot declare in post statement of for loop")
}
}
} else if p.tok != _Lbrace {
case nil:
if keyword == _If && semi.pos.IsKnown() {
if semi.lit != "semicolon" {
- p.syntax_error_at(semi.pos, fmt.Sprintf("unexpected %s, expecting { after if clause", semi.lit))
+ p.syntaxErrorAt(semi.pos, fmt.Sprintf("unexpected %s, expecting { after if clause", semi.lit))
} else {
- p.syntax_error_at(semi.pos, "missing condition in if statement")
+ p.syntaxErrorAt(semi.pos, "missing condition in if statement")
}
}
case *ExprStmt:
if as, ok := s.(*AssignStmt); ok && as.Op == 0 {
str = "assignment " + str
}
- p.syntax_error(fmt.Sprintf("%s used as value", str))
+ p.syntaxError(fmt.Sprintf("%s used as value", str))
}
p.xnest = outer
case _Lbrace:
s.Else = p.blockStmt("")
default:
- p.syntax_error("else must be followed by if or statement block")
+ p.syntaxError("else must be followed by if or statement block")
p.advance(_Name, _Rbrace)
}
}
s.Init, s.Tag, _ = p.header(_Switch)
if !p.got(_Lbrace) {
- p.syntax_error("missing { after switch clause")
+ p.syntaxError("missing { after switch clause")
p.advance(_Case, _Default, _Rbrace)
}
for p.tok != _EOF && p.tok != _Rbrace {
p.want(_Select)
if !p.got(_Lbrace) {
- p.syntax_error("missing { after select clause")
+ p.syntaxError("missing { after select clause")
p.advance(_Case, _Default, _Rbrace)
}
for p.tok != _EOF && p.tok != _Rbrace {
p.next()
default:
- p.syntax_error("expecting case or default or }")
+ p.syntaxError("expecting case or default or }")
p.advance(_Colon, _Case, _Default, _Rbrace)
}
p.next()
default:
- p.syntax_error("expecting case or default or }")
+ p.syntaxError("expecting case or default or }")
p.advance(_Colon, _Case, _Default, _Rbrace)
}
l = append(l, s)
// ";" is optional before "}"
if !p.got(_Semi) && p.tok != _Rbrace {
- p.syntax_error("at end of statement")
+ p.syntaxError("at end of statement")
p.advance(_Semi, _Rbrace, _Case, _Default)
p.got(_Semi) // avoid spurious empty statement
}
}
n := p.newName("_")
- p.syntax_error("expecting name")
+ p.syntaxError("expecting name")
p.advance()
return n
}
name = p.name()
default:
name = p.newName("_")
- p.syntax_error("expecting name")
+ p.syntaxError("expecting name")
p.advance(_Dot, _Semi, _Rbrace)
}