2008-09-18 17:58:37 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2009-03-10 17:31:19 -06:00
|
|
|
// A parser for Go source text. The input is a stream of lexical tokens
|
|
|
|
// provided via the Scanner interface. The output is an abstract syntax
|
2009-03-26 23:16:06 -06:00
|
|
|
// tree (AST) representing the Go source. The parser is invoked by calling
|
|
|
|
// Parse.
|
2009-03-10 17:31:19 -06:00
|
|
|
//
|
2009-03-26 17:10:07 -06:00
|
|
|
package parser
|
2008-09-18 17:58:37 -06:00
|
|
|
|
2009-01-09 17:28:09 -07:00
|
|
|
import (
|
2009-01-23 14:50:14 -07:00
|
|
|
"fmt";
|
2009-02-13 16:07:56 -07:00
|
|
|
"vector";
|
2009-03-03 19:25:07 -07:00
|
|
|
"token";
|
2009-03-05 18:15:36 -07:00
|
|
|
"ast";
|
2009-01-09 17:28:09 -07:00
|
|
|
)
|
2008-09-22 19:26:12 -06:00
|
|
|
|
2008-09-18 17:58:37 -06:00
|
|
|
|
2009-03-26 17:10:07 -06:00
|
|
|
// An implementation of a Scanner must be provided to the Parser.
|
|
|
|
// The parser calls Scan() repeatedly until token.EOF is returned.
|
|
|
|
// Scan must return the current token position pos, the token value
|
2009-03-26 23:16:06 -06:00
|
|
|
// tok, and the corresponding token literal string lit; lit can be
|
2009-03-27 20:27:09 -06:00
|
|
|
// undefined/nil unless the token is a literal (tok.IsLiteral() == true).
|
2009-03-26 17:10:07 -06:00
|
|
|
//
|
|
|
|
type Scanner interface {
|
|
|
|
Scan() (pos token.Position, tok token.Token, lit []byte);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
// An implementation of an ErrorHandler may be provided to the parser.
|
|
|
|
// If a syntax error is encountered and a handler was installed, Error
|
|
|
|
// is called with a position and an error message. The position points
|
|
|
|
// to the beginning of the offending token.
|
2009-03-10 17:31:19 -06:00
|
|
|
//
|
2009-03-26 17:10:07 -06:00
|
|
|
type ErrorHandler interface {
|
|
|
|
Error(pos token.Position, msg string);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
type interval struct {
|
|
|
|
beg, end int;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 17:10:07 -06:00
|
|
|
// The parser structure holds the parser's internal state.
|
|
|
|
type parser struct {
|
|
|
|
scanner Scanner;
|
2009-03-26 23:16:06 -06:00
|
|
|
err ErrorHandler; // nil if no handler installed
|
|
|
|
errorCount int;
|
2009-03-02 21:27:09 -07:00
|
|
|
|
2008-10-18 17:42:25 -06:00
|
|
|
// Tracing/debugging
|
2009-03-27 20:27:09 -06:00
|
|
|
mode uint; // parsing mode
|
|
|
|
trace bool; // == (mode & Trace != 0)
|
|
|
|
indent uint; // indentation used for tracing output
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
// Comments
|
2009-03-20 18:18:48 -06:00
|
|
|
comments vector.Vector; // list of collected, unassociated comments
|
|
|
|
last_doc interval; // last comments interval of consecutive comments
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-03 19:25:07 -07:00
|
|
|
// The next token
|
2009-03-26 23:16:06 -06:00
|
|
|
pos token.Position; // token position
|
2009-03-26 11:53:14 -06:00
|
|
|
tok token.Token; // one token look-ahead
|
2009-03-26 17:10:07 -06:00
|
|
|
lit []byte; // token literal
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2008-10-10 17:03:32 -06:00
|
|
|
// Non-syntactic parser control
|
2009-02-27 16:40:17 -07:00
|
|
|
opt_semi bool; // true if semicolon separator is optional in statement list
|
2009-03-03 17:00:06 -07:00
|
|
|
expr_lev int; // < 0: in control clause, >= 0: in expression
|
2008-09-18 17:58:37 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-03-27 20:27:09 -06:00
|
|
|
// noPos is used when there is no corresponding source position for a token
|
|
|
|
var noPos token.Position;
|
2009-01-08 13:04:00 -07:00
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Parsing support
|
2008-09-18 17:58:37 -06:00
|
|
|
|
2009-03-27 20:27:09 -06:00
|
|
|
func (p *parser) printTrace(a ...) {
|
|
|
|
const dots =
|
|
|
|
". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . "
|
|
|
|
". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ";
|
|
|
|
const n = uint(len(dots));
|
|
|
|
|
|
|
|
fmt.Printf("%5d:%3d: ", p.pos.Line, p.pos.Column);
|
|
|
|
i := 2*p.indent;
|
|
|
|
for ; i > n; i -= n {
|
|
|
|
fmt.Print(dots[0 : i%n]);
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2009-03-27 20:27:09 -06:00
|
|
|
fmt.Print(dots[0 : i]);
|
|
|
|
fmt.Println(a);
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func trace(p *parser, msg string) *parser {
|
2009-03-27 20:27:09 -06:00
|
|
|
p.printTrace(msg, "(");
|
2009-03-26 23:16:06 -06:00
|
|
|
p.indent++;
|
|
|
|
return p;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func un/*trace*/(p *parser) {
|
|
|
|
p.indent--;
|
2009-03-27 20:27:09 -06:00
|
|
|
p.printTrace(")");
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) next0() {
|
2009-03-27 20:27:09 -06:00
|
|
|
// Because of one-token look-ahead, print the previous token
|
|
|
|
// when tracing as it provides a more readable output. The
|
|
|
|
// very first token (p.pos.Line == 0) is not initialized (it
|
|
|
|
// is token.ILLEGAL), so don't print it .
|
|
|
|
if p.trace && p.pos.Line > 0 {
|
|
|
|
s := p.tok.String();
|
|
|
|
switch {
|
|
|
|
case p.tok.IsLiteral():
|
|
|
|
p.printTrace(s, string(p.lit));
|
|
|
|
case p.tok.IsOperator(), p.tok.IsKeyword():
|
|
|
|
p.printTrace("\"" + s + "\"");
|
2009-02-05 15:22:09 -07:00
|
|
|
default:
|
2009-03-27 20:27:09 -06:00
|
|
|
p.printTrace(s);
|
2009-01-23 10:44:01 -07:00
|
|
|
}
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2009-03-27 20:27:09 -06:00
|
|
|
|
|
|
|
p.pos, p.tok, p.lit = p.scanner.Scan();
|
|
|
|
p.opt_semi = false;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// Collect a comment in the parser's comment list and return the line
|
|
|
|
// on which the comment ends.
|
2009-03-27 20:27:09 -06:00
|
|
|
//
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) collectComment() int {
|
2009-03-20 18:18:48 -06:00
|
|
|
// For /*-style comments, the comment may end on a different line.
|
|
|
|
// Scan the comment for '\n' chars and adjust the end line accordingly.
|
|
|
|
// (Note that the position of the next token may be even further down
|
|
|
|
// as there may be more whitespace lines after the comment.)
|
2009-03-26 23:16:06 -06:00
|
|
|
endline := p.pos.Line;
|
|
|
|
if p.lit[1] == '*' {
|
|
|
|
for i, b := range p.lit {
|
2009-03-12 18:24:03 -06:00
|
|
|
if b == '\n' {
|
|
|
|
endline++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-03-26 23:16:06 -06:00
|
|
|
p.comments.Push(&ast.Comment{p.pos, p.lit, endline});
|
|
|
|
p.next0();
|
2009-03-12 18:24:03 -06:00
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
return endline;
|
2009-03-12 18:24:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) getComments() interval {
|
2009-03-20 18:18:48 -06:00
|
|
|
// group adjacent comments, an empty line terminates a group
|
2009-03-26 23:16:06 -06:00
|
|
|
beg := p.comments.Len();
|
|
|
|
endline := p.pos.Line;
|
|
|
|
for p.tok == token.COMMENT && endline+1 >= p.pos.Line {
|
|
|
|
endline = p.collectComment();
|
2009-03-12 18:24:03 -06:00
|
|
|
}
|
2009-03-26 23:16:06 -06:00
|
|
|
end := p.comments.Len();
|
2009-03-20 18:18:48 -06:00
|
|
|
return interval {beg, end};
|
2009-03-12 18:24:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-27 20:27:09 -06:00
|
|
|
func (p *parser) getDoc() ast.Comments {
|
|
|
|
doc := p.last_doc;
|
|
|
|
n := doc.end - doc.beg;
|
|
|
|
|
|
|
|
if n <= 0 || p.comments.At(doc.end - 1).(*ast.Comment).EndLine + 1 < p.pos.Line {
|
|
|
|
// no comments or empty line between last comment and current token;
|
|
|
|
// do not use as documentation
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
// found immediately adjacent comment interval;
|
|
|
|
// use as documentation
|
|
|
|
c := make(ast.Comments, n);
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
c[i] = p.comments.At(doc.beg + i).(*ast.Comment);
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove comments from the general list
|
|
|
|
p.comments.Cut(doc.beg, doc.end);
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) next() {
|
|
|
|
p.next0();
|
|
|
|
p.last_doc = interval{0, 0};
|
|
|
|
for p.tok == token.COMMENT {
|
|
|
|
p.last_doc = p.getComments();
|
2008-10-17 00:30:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) error(pos token.Position, msg string) {
|
|
|
|
if p.err != nil {
|
|
|
|
p.err.Error(pos, msg);
|
|
|
|
}
|
|
|
|
p.errorCount++;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-27 20:27:09 -06:00
|
|
|
func (p *parser) error_expected(pos token.Position, msg string) {
|
|
|
|
msg = "expected " + msg;
|
|
|
|
if pos.Offset == p.pos.Offset {
|
|
|
|
// the error happened at the current position;
|
|
|
|
// make the error message more specific
|
|
|
|
msg += ", found '" + p.tok.String() + "'";
|
2009-03-26 23:16:06 -06:00
|
|
|
if p.tok.IsLiteral() {
|
|
|
|
msg += " " + string(p.lit);
|
2008-10-18 17:42:25 -06:00
|
|
|
}
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2009-03-27 20:27:09 -06:00
|
|
|
p.error(pos, msg);
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-27 20:27:09 -06:00
|
|
|
func (p *parser) expect(tok token.Token) token.Position {
|
|
|
|
pos := p.pos;
|
|
|
|
if p.tok != tok {
|
|
|
|
p.error_expected(pos, "'" + tok.String() + "'");
|
2009-03-20 18:18:48 -06:00
|
|
|
}
|
2009-03-27 20:27:09 -06:00
|
|
|
p.next(); // make progress in any case
|
|
|
|
return pos;
|
2009-03-20 18:18:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-18 17:58:37 -06:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Common productions
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) tryType() ast.Expr;
|
2009-03-27 20:27:09 -06:00
|
|
|
func (p *parser) parseStringList(x *ast.StringLit) []*ast.StringLit
|
|
|
|
func (p *parser) parseExpression() ast.Expr;
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseStatement() ast.Stmt;
|
|
|
|
func (p *parser) parseDeclaration() ast.Decl;
|
2008-09-18 17:58:37 -06:00
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseIdent() *ast.Ident {
|
|
|
|
if p.tok == token.IDENT {
|
|
|
|
x := &ast.Ident{p.pos, p.lit};
|
|
|
|
p.next();
|
2009-02-03 18:44:01 -07:00
|
|
|
return x;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2009-03-26 23:16:06 -06:00
|
|
|
p.expect(token.IDENT); // use expect() error handling
|
|
|
|
return &ast.Ident{p.pos, [0]byte{}};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseIdentList(x ast.Expr) []*ast.Ident {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "IdentList"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-09-18 17:58:37 -06:00
|
|
|
|
2009-03-16 21:29:31 -06:00
|
|
|
list := vector.New(0);
|
2009-02-19 17:47:58 -07:00
|
|
|
if x == nil {
|
2009-03-26 23:16:06 -06:00
|
|
|
x = p.parseIdent();
|
2009-02-19 17:47:58 -07:00
|
|
|
}
|
2009-03-16 21:29:31 -06:00
|
|
|
list.Push(x);
|
2009-03-26 23:16:06 -06:00
|
|
|
for p.tok == token.COMMA {
|
|
|
|
p.next();
|
|
|
|
list.Push(p.parseIdent());
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
2009-03-16 21:29:31 -06:00
|
|
|
// convert vector
|
|
|
|
idents := make([]*ast.Ident, list.Len());
|
|
|
|
for i := 0; i < list.Len(); i++ {
|
|
|
|
idents[i] = list.At(i).(*ast.Ident);
|
|
|
|
}
|
2009-03-20 18:18:48 -06:00
|
|
|
|
2009-03-16 21:29:31 -06:00
|
|
|
return idents;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseExpressionList() []ast.Expr {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "ExpressionList"));
|
2009-02-27 16:40:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
list := vector.New(0);
|
2009-03-27 20:27:09 -06:00
|
|
|
list.Push(p.parseExpression());
|
2009-03-26 23:16:06 -06:00
|
|
|
for p.tok == token.COMMA {
|
|
|
|
p.next();
|
2009-03-27 20:27:09 -06:00
|
|
|
list.Push(p.parseExpression());
|
2009-02-27 16:40:17 -07:00
|
|
|
}
|
|
|
|
|
2009-03-17 19:41:35 -06:00
|
|
|
// convert list
|
2009-03-16 21:29:31 -06:00
|
|
|
exprs := make([]ast.Expr, list.Len());
|
2009-02-27 16:40:17 -07:00
|
|
|
for i := 0; i < list.Len(); i++ {
|
2009-03-16 21:29:31 -06:00
|
|
|
exprs[i] = list.At(i).(ast.Expr);
|
2009-02-27 16:40:17 -07:00
|
|
|
}
|
2009-03-20 18:18:48 -06:00
|
|
|
|
2009-03-16 21:29:31 -06:00
|
|
|
return exprs;
|
2009-02-27 16:40:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-18 17:58:37 -06:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Types
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseType() ast.Expr {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "Type"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
typ := p.tryType();
|
2009-03-27 20:27:09 -06:00
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
if typ == nil {
|
2009-03-27 20:27:09 -06:00
|
|
|
p.error_expected(p.pos, "type");
|
|
|
|
return &ast.BadExpr{p.pos};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
return typ;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseQualifiedIdent() ast.Expr {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "QualifiedIdent"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-10-14 19:14:01 -06:00
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
var x ast.Expr = p.parseIdent();
|
2009-03-27 20:27:09 -06:00
|
|
|
if p.tok == token.PERIOD {
|
|
|
|
// first identifier is a package identifier
|
2009-03-26 23:16:06 -06:00
|
|
|
p.next();
|
|
|
|
sel := p.parseIdent();
|
2009-03-20 18:18:48 -06:00
|
|
|
x = &ast.SelectorExpr{x, sel};
|
2008-10-14 19:14:01 -06:00
|
|
|
}
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseTypeName() ast.Expr {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "TypeName"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
return p.parseQualifiedIdent();
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-27 20:27:09 -06:00
|
|
|
func (p *parser) parseArrayOrSliceType(ellipsis_ok bool) ast.Expr {
|
2009-03-26 23:16:06 -06:00
|
|
|
if p.trace {
|
2009-03-27 20:27:09 -06:00
|
|
|
defer un(trace(p, "ArrayOrSliceType"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
lbrack := p.expect(token.LBRACK);
|
2009-03-05 18:15:36 -07:00
|
|
|
var len ast.Expr;
|
2009-03-27 20:27:09 -06:00
|
|
|
if ellipsis_ok && p.tok == token.ELLIPSIS {
|
2009-03-26 23:16:06 -06:00
|
|
|
len = &ast.Ellipsis{p.pos};
|
|
|
|
p.next();
|
|
|
|
} else if p.tok != token.RBRACK {
|
2009-03-27 20:27:09 -06:00
|
|
|
len = p.parseExpression();
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2009-03-26 23:16:06 -06:00
|
|
|
p.expect(token.RBRACK);
|
|
|
|
elt := p.parseType();
|
2008-09-18 17:58:37 -06:00
|
|
|
|
2009-03-27 20:27:09 -06:00
|
|
|
if len != nil {
|
|
|
|
return &ast.ArrayType{lbrack, len, elt};
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ast.SliceType{lbrack, elt};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-27 20:27:09 -06:00
|
|
|
func (p *parser) makeIdentList(list *vector.Vector) []*ast.Ident {
|
|
|
|
idents := make([]*ast.Ident, list.Len());
|
|
|
|
for i := 0; i < list.Len(); i++ {
|
|
|
|
ident, is_ident := list.At(i).(*ast.Ident);
|
|
|
|
if !is_ident {
|
|
|
|
pos := list.At(i).(ast.Expr).Pos();
|
|
|
|
p.error_expected(pos, "identifier");
|
|
|
|
idents[i] = &ast.Ident{pos, []byte{}};
|
|
|
|
}
|
|
|
|
idents[i] = ident;
|
|
|
|
}
|
|
|
|
return idents;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (p *parser) parseFieldDecl() *ast.Field {
|
2009-03-26 23:16:06 -06:00
|
|
|
if p.trace {
|
2009-03-27 20:27:09 -06:00
|
|
|
defer un(trace(p, "FieldDecl"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-27 20:27:09 -06:00
|
|
|
doc := p.getDoc();
|
|
|
|
|
|
|
|
// a list of identifiers looks like a list of type names
|
|
|
|
list := vector.New(0);
|
|
|
|
for {
|
|
|
|
// TODO do not allow ()'s here
|
|
|
|
list.Push(p.parseType());
|
|
|
|
if p.tok == token.COMMA {
|
2009-03-26 23:16:06 -06:00
|
|
|
p.next();
|
2009-03-27 20:27:09 -06:00
|
|
|
} else {
|
|
|
|
break;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2009-03-27 20:27:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// if we had a list of identifiers, it must be followed by a type
|
|
|
|
typ := p.tryType();
|
|
|
|
|
|
|
|
// optional tag
|
|
|
|
var tag []*ast.StringLit;
|
|
|
|
if p.tok == token.STRING {
|
|
|
|
tag = p.parseStringList(nil);
|
|
|
|
}
|
|
|
|
|
|
|
|
// analyze case
|
|
|
|
var idents []*ast.Ident;
|
|
|
|
if typ != nil {
|
|
|
|
// IdentifierList Type
|
|
|
|
idents = p.makeIdentList(list);
|
2008-09-18 17:58:37 -06:00
|
|
|
} else {
|
2009-03-27 20:27:09 -06:00
|
|
|
// Type (anonymous field)
|
|
|
|
if list.Len() == 1 {
|
|
|
|
// TODO check that this looks like a type
|
|
|
|
typ = list.At(0).(ast.Expr);
|
|
|
|
} else {
|
|
|
|
p.error_expected(p.pos, "anonymous field");
|
|
|
|
typ = &ast.BadExpr{p.pos};
|
|
|
|
}
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
2009-03-27 20:27:09 -06:00
|
|
|
return &ast.Field{doc, idents, typ, tag};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-27 20:27:09 -06:00
|
|
|
func (p *parser) parseStructType() *ast.StructType {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "StructType"));
|
|
|
|
}
|
|
|
|
|
|
|
|
pos := p.expect(token.STRUCT);
|
|
|
|
var lbrace, rbrace token.Position;
|
|
|
|
var fields []*ast.Field;
|
|
|
|
if p.tok == token.LBRACE {
|
|
|
|
lbrace = p.pos;
|
2009-03-26 23:16:06 -06:00
|
|
|
p.next();
|
2009-03-27 20:27:09 -06:00
|
|
|
|
|
|
|
list := vector.New(0);
|
|
|
|
for p.tok != token.RBRACE && p.tok != token.EOF {
|
|
|
|
list.Push(p.parseFieldDecl());
|
|
|
|
if p.tok == token.SEMICOLON {
|
|
|
|
p.next();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if p.tok == token.SEMICOLON {
|
|
|
|
p.next();
|
|
|
|
}
|
|
|
|
|
|
|
|
rbrace = p.expect(token.RBRACE);
|
|
|
|
p.opt_semi = true;
|
|
|
|
|
|
|
|
// convert vector
|
|
|
|
fields = make([]*ast.Field, list.Len());
|
|
|
|
for i := list.Len() - 1; i >= 0; i-- {
|
|
|
|
fields[i] = list.At(i).(*ast.Field);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ast.StructType{pos, lbrace, fields, rbrace};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (p *parser) parsePointerType() *ast.StarExpr {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "PointerType"));
|
|
|
|
}
|
|
|
|
|
|
|
|
star := p.expect(token.MUL);
|
|
|
|
base := p.parseType();
|
|
|
|
|
|
|
|
return &ast.StarExpr{star, base};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (p *parser) tryParameterType(ellipsis_ok bool) ast.Expr {
|
|
|
|
if ellipsis_ok && p.tok == token.ELLIPSIS {
|
|
|
|
pos := p.pos;
|
|
|
|
p.next();
|
|
|
|
if p.tok != token.RPAREN {
|
|
|
|
// "..." always must be at the very end of a parameter list
|
|
|
|
p.error(pos, "expected type, found '...'");
|
|
|
|
}
|
|
|
|
return &ast.Ellipsis{pos};
|
2008-10-18 17:42:25 -06:00
|
|
|
}
|
2009-03-26 23:16:06 -06:00
|
|
|
return p.tryType();
|
2009-02-27 16:40:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-27 20:27:09 -06:00
|
|
|
func (p *parser) parseParameterType(ellipsis_ok bool) ast.Expr {
|
|
|
|
typ := p.tryParameterType(ellipsis_ok);
|
2009-02-27 16:40:17 -07:00
|
|
|
if typ == nil {
|
2009-03-27 20:27:09 -06:00
|
|
|
p.error_expected(p.pos, "type");
|
2009-03-26 23:16:06 -06:00
|
|
|
typ = &ast.BadExpr{p.pos};
|
2009-02-27 16:40:17 -07:00
|
|
|
}
|
|
|
|
return typ;
|
2008-10-18 17:42:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseParameterDecl(ellipsis_ok bool) (*vector.Vector, ast.Expr) {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "ParameterDecl"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-10-01 15:31:44 -06:00
|
|
|
|
2009-02-27 16:40:17 -07:00
|
|
|
// a list of identifiers looks like a list of type names
|
|
|
|
list := vector.New(0);
|
2008-10-24 15:04:54 -06:00
|
|
|
for {
|
2009-02-27 16:40:17 -07:00
|
|
|
// TODO do not allow ()'s here
|
2009-03-27 20:27:09 -06:00
|
|
|
list.Push(p.parseParameterType(ellipsis_ok));
|
2009-03-26 23:16:06 -06:00
|
|
|
if p.tok == token.COMMA {
|
|
|
|
p.next();
|
2008-10-24 15:04:54 -06:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2008-10-01 15:31:44 -06:00
|
|
|
}
|
2008-10-15 12:48:18 -06:00
|
|
|
|
2009-01-12 18:44:10 -07:00
|
|
|
// if we had a list of identifiers, it must be followed by a type
|
2009-03-27 20:27:09 -06:00
|
|
|
typ := p.tryParameterType(ellipsis_ok);
|
2009-03-11 17:06:17 -06:00
|
|
|
|
2009-02-27 16:40:17 -07:00
|
|
|
return list, typ;
|
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-02-27 16:40:17 -07:00
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseParameterList(ellipsis_ok bool) []*ast.Field {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "ParameterList"));
|
2008-10-18 17:42:25 -06:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-27 20:27:09 -06:00
|
|
|
list, typ := p.parseParameterDecl(ellipsis_ok);
|
2008-10-15 12:48:18 -06:00
|
|
|
if typ != nil {
|
2009-02-27 16:40:17 -07:00
|
|
|
// IdentifierList Type
|
2009-03-27 20:27:09 -06:00
|
|
|
idents := p.makeIdentList(list);
|
2009-02-27 16:40:17 -07:00
|
|
|
list.Init(0);
|
2009-03-20 18:18:48 -06:00
|
|
|
list.Push(&ast.Field{nil, idents, typ, nil});
|
2009-03-11 17:06:17 -06:00
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
for p.tok == token.COMMA {
|
|
|
|
p.next();
|
|
|
|
idents := p.parseIdentList(nil);
|
2009-03-27 20:27:09 -06:00
|
|
|
typ := p.parseParameterType(ellipsis_ok);
|
2009-03-20 18:18:48 -06:00
|
|
|
list.Push(&ast.Field{nil, idents, typ, nil});
|
2008-10-15 12:48:18 -06:00
|
|
|
}
|
2008-10-15 18:06:28 -06:00
|
|
|
|
2008-10-15 12:48:18 -06:00
|
|
|
} else {
|
2009-03-27 20:27:09 -06:00
|
|
|
// Type { "," Type } (anonymous parameters)
|
2009-02-27 16:40:17 -07:00
|
|
|
// convert list of types into list of *Param
|
|
|
|
for i := 0; i < list.Len(); i++ {
|
2009-03-20 18:18:48 -06:00
|
|
|
list.Set(i, &ast.Field{nil, nil, list.At(i).(ast.Expr), nil});
|
2008-10-15 18:06:28 -06:00
|
|
|
}
|
2008-09-27 18:42:18 -06:00
|
|
|
}
|
2008-09-18 17:58:37 -06:00
|
|
|
|
2009-02-27 16:40:17 -07:00
|
|
|
// convert list
|
2009-03-05 18:15:36 -07:00
|
|
|
params := make([]*ast.Field, list.Len());
|
2009-02-27 16:40:17 -07:00
|
|
|
for i := 0; i < list.Len(); i++ {
|
2009-03-05 18:15:36 -07:00
|
|
|
params[i] = list.At(i).(*ast.Field);
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-02-27 16:40:17 -07:00
|
|
|
return params;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseParameters(ellipsis_ok bool) []*ast.Field {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "Parameters"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
var params []*ast.Field;
|
2009-03-26 23:16:06 -06:00
|
|
|
p.expect(token.LPAREN);
|
|
|
|
if p.tok != token.RPAREN {
|
|
|
|
params = p.parseParameterList(ellipsis_ok);
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2009-03-26 23:16:06 -06:00
|
|
|
p.expect(token.RPAREN);
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-02-27 16:40:17 -07:00
|
|
|
return params;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseResult() []*ast.Field {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "Result"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-25 13:45:06 -06:00
|
|
|
var results []*ast.Field;
|
2009-03-26 23:16:06 -06:00
|
|
|
if p.tok == token.LPAREN {
|
|
|
|
results = p.parseParameters(false);
|
|
|
|
} else if p.tok != token.FUNC {
|
|
|
|
typ := p.tryType();
|
2008-10-15 18:06:28 -06:00
|
|
|
if typ != nil {
|
2009-03-25 13:45:06 -06:00
|
|
|
results = make([]*ast.Field, 1);
|
|
|
|
results[0] = &ast.Field{nil, nil, typ, nil};
|
2008-10-15 18:06:28 -06:00
|
|
|
}
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
2009-03-25 13:45:06 -06:00
|
|
|
return results;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseSignature() (params []*ast.Field, results []*ast.Field) {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "Signature"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-27 20:27:09 -06:00
|
|
|
params = p.parseParameters(true);
|
2009-03-26 23:16:06 -06:00
|
|
|
results = p.parseResult();
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-25 13:45:06 -06:00
|
|
|
return params, results;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseFunctionType() *ast.FunctionType {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "FunctionType"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2009-01-30 16:31:04 -07:00
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
pos := p.expect(token.FUNC);
|
|
|
|
params, results := p.parseSignature();
|
2009-03-11 17:06:17 -06:00
|
|
|
|
2009-03-25 13:45:06 -06:00
|
|
|
return &ast.FunctionType{pos, params, results};
|
2009-01-30 16:31:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseMethodSpec() *ast.Field {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "MethodSpec"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
doc := p.getDoc();
|
2009-03-05 18:15:36 -07:00
|
|
|
var idents []*ast.Ident;
|
|
|
|
var typ ast.Expr;
|
2009-03-26 23:16:06 -06:00
|
|
|
x := p.parseQualifiedIdent();
|
|
|
|
if tmp, is_ident := x.(*ast.Ident); is_ident && (p.tok == token.COMMA || p.tok == token.LPAREN) {
|
2009-03-27 20:27:09 -06:00
|
|
|
// methods
|
2009-03-26 23:16:06 -06:00
|
|
|
idents = p.parseIdentList(x);
|
|
|
|
params, results := p.parseSignature();
|
2009-03-27 20:27:09 -06:00
|
|
|
typ = &ast.FunctionType{noPos, params, results};
|
2009-02-19 17:47:58 -07:00
|
|
|
} else {
|
|
|
|
// embedded interface
|
2009-02-27 16:40:17 -07:00
|
|
|
typ = x;
|
2009-02-19 17:47:58 -07:00
|
|
|
}
|
2009-03-11 17:06:17 -06:00
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
return &ast.Field{doc, idents, typ, nil};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseInterfaceType() *ast.InterfaceType {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "InterfaceType"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
pos := p.expect(token.INTERFACE);
|
2009-03-26 17:10:07 -06:00
|
|
|
var lbrace, rbrace token.Position;
|
2009-03-05 18:15:36 -07:00
|
|
|
var methods []*ast.Field;
|
2009-03-26 23:16:06 -06:00
|
|
|
if p.tok == token.LBRACE {
|
|
|
|
lbrace = p.pos;
|
|
|
|
p.next();
|
2009-01-08 13:04:00 -07:00
|
|
|
|
2009-02-27 16:40:17 -07:00
|
|
|
list := vector.New(0);
|
2009-03-26 23:16:06 -06:00
|
|
|
for p.tok == token.IDENT {
|
|
|
|
list.Push(p.parseMethodSpec());
|
|
|
|
if p.tok != token.RBRACE {
|
|
|
|
p.expect(token.SEMICOLON);
|
2008-10-07 18:57:19 -06:00
|
|
|
}
|
|
|
|
}
|
2009-01-08 13:04:00 -07:00
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
rbrace = p.expect(token.RBRACE);
|
|
|
|
p.opt_semi = true;
|
2009-03-11 17:06:17 -06:00
|
|
|
|
2009-02-27 16:40:17 -07:00
|
|
|
// convert vector
|
2009-03-05 18:15:36 -07:00
|
|
|
methods = make([]*ast.Field, list.Len());
|
2009-02-27 16:40:17 -07:00
|
|
|
for i := list.Len() - 1; i >= 0; i-- {
|
2009-03-05 18:15:36 -07:00
|
|
|
methods[i] = list.At(i).(*ast.Field);
|
2009-02-27 16:40:17 -07:00
|
|
|
}
|
2008-10-07 18:57:19 -06:00
|
|
|
}
|
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
return &ast.InterfaceType{pos, lbrace, methods, rbrace};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseMapType() *ast.MapType {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "MapType"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
pos := p.expect(token.MAP);
|
|
|
|
p.expect(token.LBRACK);
|
|
|
|
key := p.parseType();
|
|
|
|
p.expect(token.RBRACK);
|
|
|
|
value := p.parseType();
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
return &ast.MapType{pos, key, value};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-27 20:27:09 -06:00
|
|
|
func (p *parser) parseChannelType() *ast.ChannelType {
|
2009-03-26 23:16:06 -06:00
|
|
|
if p.trace {
|
2009-03-27 20:27:09 -06:00
|
|
|
defer un(trace(p, "ChannelType"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-09-25 12:50:34 -06:00
|
|
|
|
2009-03-27 20:27:09 -06:00
|
|
|
pos := p.pos;
|
|
|
|
dir := ast.SEND | ast.RECV;
|
|
|
|
if p.tok == token.CHAN {
|
2009-03-26 23:16:06 -06:00
|
|
|
p.next();
|
2009-03-27 20:27:09 -06:00
|
|
|
if p.tok == token.ARROW {
|
2009-03-26 23:16:06 -06:00
|
|
|
p.next();
|
2009-03-27 20:27:09 -06:00
|
|
|
dir = ast.SEND;
|
2009-03-10 19:20:08 -06:00
|
|
|
}
|
2009-03-27 20:27:09 -06:00
|
|
|
} else {
|
|
|
|
p.expect(token.ARROW);
|
|
|
|
p.expect(token.CHAN);
|
|
|
|
dir = ast.RECV;
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2009-03-27 20:27:09 -06:00
|
|
|
value := p.parseType();
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-27 20:27:09 -06:00
|
|
|
return &ast.ChannelType{pos, dir, value};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-27 20:27:09 -06:00
|
|
|
func (p *parser) tryRawType(ellipsis_ok bool) ast.Expr {
|
2009-03-26 23:16:06 -06:00
|
|
|
switch p.tok {
|
|
|
|
case token.IDENT: return p.parseTypeName();
|
2009-03-27 20:27:09 -06:00
|
|
|
case token.LBRACK: return p.parseArrayOrSliceType(ellipsis_ok);
|
2009-03-26 23:16:06 -06:00
|
|
|
case token.STRUCT: return p.parseStructType();
|
|
|
|
case token.MUL: return p.parsePointerType();
|
2009-03-27 20:27:09 -06:00
|
|
|
case token.FUNC: return p.parseFunctionType();
|
|
|
|
case token.INTERFACE: return p.parseInterfaceType();
|
|
|
|
case token.MAP: return p.parseMapType();
|
|
|
|
case token.CHAN, token.ARROW: return p.parseChannelType();
|
2009-03-03 19:25:07 -07:00
|
|
|
case token.LPAREN:
|
2009-03-26 23:16:06 -06:00
|
|
|
lparen := p.pos;
|
|
|
|
p.next();
|
2009-03-27 20:27:09 -06:00
|
|
|
typ := p.parseType();
|
2009-03-26 23:16:06 -06:00
|
|
|
rparen := p.expect(token.RPAREN);
|
2009-03-27 20:27:09 -06:00
|
|
|
return &ast.ParenExpr{lparen, typ, rparen};
|
2009-02-13 17:27:53 -07:00
|
|
|
}
|
2009-03-03 17:00:06 -07:00
|
|
|
|
2009-02-13 17:27:53 -07:00
|
|
|
// no type found
|
|
|
|
return nil;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-27 20:27:09 -06:00
|
|
|
func (p *parser) tryType() ast.Expr {
|
|
|
|
return p.tryRawType(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-18 17:58:37 -06:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Blocks
|
|
|
|
|
2009-03-27 20:27:09 -06:00
|
|
|
func makeStmtList(list *vector.Vector) []ast.Stmt {
|
2009-03-25 13:45:06 -06:00
|
|
|
stats := make([]ast.Stmt, list.Len());
|
2009-03-20 18:18:48 -06:00
|
|
|
for i := 0; i < list.Len(); i++ {
|
2009-03-25 13:45:06 -06:00
|
|
|
stats[i] = list.At(i).(ast.Stmt);
|
2009-03-20 18:18:48 -06:00
|
|
|
}
|
|
|
|
return stats;
|
|
|
|
}
|
|
|
|
|
2009-02-04 19:28:41 -07:00
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseStatementList() []ast.Stmt {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "StatementList"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
list := vector.New(0);
|
2009-02-12 17:06:21 -07:00
|
|
|
expect_semi := false;
|
2009-03-26 23:16:06 -06:00
|
|
|
for p.tok != token.CASE && p.tok != token.DEFAULT && p.tok != token.RBRACE && p.tok != token.EOF {
|
2009-02-12 17:06:21 -07:00
|
|
|
if expect_semi {
|
2009-03-26 23:16:06 -06:00
|
|
|
p.expect(token.SEMICOLON);
|
2009-02-12 17:06:21 -07:00
|
|
|
expect_semi = false;
|
2008-10-17 17:19:31 -06:00
|
|
|
}
|
2009-03-26 23:16:06 -06:00
|
|
|
list.Push(p.parseStatement());
|
|
|
|
if p.tok == token.SEMICOLON {
|
|
|
|
p.next();
|
|
|
|
} else if p.opt_semi {
|
|
|
|
p.opt_semi = false; // "consume" optional semicolon
|
2008-09-23 17:40:12 -06:00
|
|
|
} else {
|
2009-02-12 17:06:21 -07:00
|
|
|
expect_semi = true;
|
2008-09-23 17:40:12 -06:00
|
|
|
}
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2009-03-20 18:18:48 -06:00
|
|
|
|
2009-03-27 20:27:09 -06:00
|
|
|
return makeStmtList(list);
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseBlockStmt() *ast.BlockStmt {
|
|
|
|
if p.trace {
|
2009-03-27 20:27:09 -06:00
|
|
|
defer un(trace(p, "BlockStmt"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
lbrace := p.expect(token.LBRACE);
|
|
|
|
list := p.parseStatementList();
|
|
|
|
rbrace := p.expect(token.RBRACE);
|
|
|
|
p.opt_semi = true;
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-25 13:45:06 -06:00
|
|
|
return &ast.BlockStmt{lbrace, list, rbrace};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Expressions
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseStringList(x *ast.StringLit) []*ast.StringLit {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "StringList"));
|
2009-03-05 18:15:36 -07:00
|
|
|
}
|
|
|
|
|
2009-03-17 19:41:35 -06:00
|
|
|
list := vector.New(0);
|
2009-03-20 18:18:48 -06:00
|
|
|
if x != nil {
|
|
|
|
list.Push(x);
|
|
|
|
}
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
for p.tok == token.STRING {
|
|
|
|
list.Push(&ast.StringLit{p.pos, p.lit});
|
|
|
|
p.next();
|
2009-03-05 18:15:36 -07:00
|
|
|
}
|
|
|
|
|
2009-03-17 19:41:35 -06:00
|
|
|
// convert list
|
2009-03-25 13:45:06 -06:00
|
|
|
strings := make([]*ast.StringLit, list.Len());
|
2009-03-17 19:41:35 -06:00
|
|
|
for i := 0; i < list.Len(); i++ {
|
2009-03-25 13:45:06 -06:00
|
|
|
strings[i] = list.At(i).(*ast.StringLit);
|
2009-03-17 19:41:35 -06:00
|
|
|
}
|
|
|
|
|
2009-03-25 13:45:06 -06:00
|
|
|
return strings;
|
2008-10-23 18:56:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-27 20:27:09 -06:00
|
|
|
func (p *parser) parseFunctionLit() ast.Expr {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "FunctionLit"));
|
|
|
|
}
|
|
|
|
|
|
|
|
typ := p.parseFunctionType();
|
|
|
|
p.expr_lev++;
|
|
|
|
body := p.parseBlockStmt();
|
|
|
|
p.expr_lev--;
|
|
|
|
|
|
|
|
return &ast.FunctionLit{typ, body};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// parseOperand may return an expression or a raw type (incl. array
|
|
|
|
// types of the form [...]T. Callers must verify the result.
|
|
|
|
//
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseOperand() ast.Expr {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "Operand"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-09-18 17:58:37 -06:00
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
switch p.tok {
|
2009-03-03 19:25:07 -07:00
|
|
|
case token.IDENT:
|
2009-03-26 23:16:06 -06:00
|
|
|
return p.parseIdent();
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-25 13:45:06 -06:00
|
|
|
case token.INT:
|
2009-03-26 23:16:06 -06:00
|
|
|
x := &ast.IntLit{p.pos, p.lit};
|
|
|
|
p.next();
|
2009-03-25 13:45:06 -06:00
|
|
|
return x;
|
|
|
|
|
|
|
|
case token.FLOAT:
|
2009-03-26 23:16:06 -06:00
|
|
|
x := &ast.FloatLit{p.pos, p.lit};
|
|
|
|
p.next();
|
2009-03-25 13:45:06 -06:00
|
|
|
return x;
|
|
|
|
|
|
|
|
case token.CHAR:
|
2009-03-26 23:16:06 -06:00
|
|
|
x := &ast.CharLit{p.pos, p.lit};
|
|
|
|
p.next();
|
2009-02-03 18:44:01 -07:00
|
|
|
return x;
|
2009-03-11 17:06:17 -06:00
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
case token.STRING:
|
2009-03-26 23:16:06 -06:00
|
|
|
x := &ast.StringLit{p.pos, p.lit};
|
|
|
|
p.next();
|
|
|
|
if p.tok == token.STRING {
|
|
|
|
return &ast.StringList{p.parseStringList(x)};
|
2009-03-20 18:18:48 -06:00
|
|
|
}
|
|
|
|
return x;
|
2008-10-10 17:03:32 -06:00
|
|
|
|
2009-03-03 19:25:07 -07:00
|
|
|
case token.LPAREN:
|
2009-03-26 23:16:06 -06:00
|
|
|
lparen := p.pos;
|
|
|
|
p.next();
|
|
|
|
p.expr_lev++;
|
2009-03-27 20:27:09 -06:00
|
|
|
x := p.parseExpression();
|
2009-03-26 23:16:06 -06:00
|
|
|
p.expr_lev--;
|
|
|
|
rparen := p.expect(token.RPAREN);
|
2009-03-20 18:18:48 -06:00
|
|
|
return &ast.ParenExpr{lparen, x, rparen};
|
2009-03-03 19:25:07 -07:00
|
|
|
|
|
|
|
case token.FUNC:
|
2009-03-26 23:16:06 -06:00
|
|
|
return p.parseFunctionLit();
|
2008-10-23 18:56:54 -06:00
|
|
|
|
2008-09-23 17:40:12 -06:00
|
|
|
default:
|
2009-03-27 20:27:09 -06:00
|
|
|
t := p.tryRawType(true); // could be type for composite literal
|
2008-10-15 18:06:28 -06:00
|
|
|
if t != nil {
|
2009-02-27 16:40:17 -07:00
|
|
|
return t;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-27 20:27:09 -06:00
|
|
|
p.error_expected(p.pos, "operand");
|
|
|
|
p.next(); // make progress
|
2009-03-26 23:16:06 -06:00
|
|
|
return &ast.BadExpr{p.pos};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseSelectorOrTypeAssertion(x ast.Expr) ast.Expr {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "SelectorOrTypeAssertion"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-09-18 17:58:37 -06:00
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
p.expect(token.PERIOD);
|
|
|
|
if p.tok == token.IDENT {
|
2009-03-17 19:41:35 -06:00
|
|
|
// selector
|
2009-03-26 23:16:06 -06:00
|
|
|
sel := p.parseIdent();
|
2009-03-20 18:18:48 -06:00
|
|
|
return &ast.SelectorExpr{x, sel};
|
2009-03-27 20:27:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// type assertion
|
|
|
|
p.expect(token.LPAREN);
|
|
|
|
var typ ast.Expr;
|
|
|
|
if p.tok == token.TYPE {
|
|
|
|
// special case for type switch
|
|
|
|
typ = &ast.Ident{p.pos, p.lit};
|
|
|
|
p.next();
|
2008-09-18 17:58:37 -06:00
|
|
|
} else {
|
2009-03-27 20:27:09 -06:00
|
|
|
typ = p.parseType();
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2009-03-27 20:27:09 -06:00
|
|
|
p.expect(token.RPAREN);
|
2009-03-20 18:18:48 -06:00
|
|
|
|
2009-03-27 20:27:09 -06:00
|
|
|
return &ast.TypeAssertExpr{x, typ};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseIndexOrSlice(x ast.Expr) ast.Expr {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "IndexOrSlice"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
p.expect(token.LBRACK);
|
|
|
|
p.expr_lev++;
|
2009-03-27 20:27:09 -06:00
|
|
|
begin := p.parseExpression();
|
|
|
|
var end ast.Expr;
|
|
|
|
if p.tok == token.COLON {
|
2009-03-26 23:16:06 -06:00
|
|
|
p.next();
|
2009-03-27 20:27:09 -06:00
|
|
|
end = p.parseExpression();
|
2009-03-17 19:41:35 -06:00
|
|
|
}
|
2009-03-26 23:16:06 -06:00
|
|
|
p.expr_lev--;
|
|
|
|
p.expect(token.RBRACK);
|
2009-03-27 20:27:09 -06:00
|
|
|
|
|
|
|
if end != nil {
|
|
|
|
return &ast.SliceExpr{x, begin, end};
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ast.IndexExpr{x, begin};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-27 20:27:09 -06:00
|
|
|
func (p *parser) parseCallOrConversion(fun ast.Expr) *ast.CallExpr {
|
2009-03-26 23:16:06 -06:00
|
|
|
if p.trace {
|
2009-03-27 20:27:09 -06:00
|
|
|
defer un(trace(p, "CallOrConversion"));
|
2009-03-17 19:41:35 -06:00
|
|
|
}
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
lparen := p.expect(token.LPAREN);
|
2009-03-17 19:41:35 -06:00
|
|
|
var args []ast.Expr;
|
2009-03-26 23:16:06 -06:00
|
|
|
if p.tok != token.RPAREN {
|
|
|
|
args = p.parseExpressionList();
|
2009-03-17 19:41:35 -06:00
|
|
|
}
|
2009-03-26 23:16:06 -06:00
|
|
|
rparen := p.expect(token.RPAREN);
|
2009-03-27 20:27:09 -06:00
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
return &ast.CallExpr{fun, lparen, args, rparen};
|
2008-10-16 13:16:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-27 20:27:09 -06:00
|
|
|
func (p *parser) parseKeyValueExpr() ast.Expr {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "KeyValueExpr"));
|
|
|
|
}
|
|
|
|
|
|
|
|
key := p.parseExpression();
|
|
|
|
|
|
|
|
if p.tok == token.COLON {
|
|
|
|
colon := p.pos;
|
|
|
|
p.next();
|
|
|
|
value := p.parseExpression();
|
|
|
|
return &ast.KeyValueExpr{key, colon, value};
|
|
|
|
}
|
|
|
|
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func isPair(x ast.Expr) bool {
|
|
|
|
tmp, is_pair := x.(*ast.KeyValueExpr);
|
|
|
|
return is_pair;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (p *parser) parseExpressionOrKeyValueList() []ast.Expr {
|
2009-03-26 23:16:06 -06:00
|
|
|
if p.trace {
|
2009-03-27 20:27:09 -06:00
|
|
|
defer un(trace(p, "ExpressionOrKeyValueList"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-27 20:27:09 -06:00
|
|
|
var pairs bool;
|
2009-03-17 19:41:35 -06:00
|
|
|
list := vector.New(0);
|
2009-03-27 20:27:09 -06:00
|
|
|
for p.tok != token.RBRACE && p.tok != token.EOF {
|
|
|
|
x := p.parseKeyValueExpr();
|
|
|
|
|
2009-03-17 19:41:35 -06:00
|
|
|
if list.Len() == 0 {
|
2009-03-27 20:27:09 -06:00
|
|
|
pairs = isPair(x);
|
2009-03-17 19:41:35 -06:00
|
|
|
} else {
|
|
|
|
// not the first element - check syntax
|
2009-03-27 20:27:09 -06:00
|
|
|
if pairs != isPair(x) {
|
|
|
|
p.error_expected(x.Pos(), "all single expressions or all key-value pairs");
|
2009-03-17 19:41:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
list.Push(x);
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
if p.tok == token.COMMA {
|
|
|
|
p.next();
|
2009-03-17 19:41:35 -06:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert list
|
|
|
|
elts := make([]ast.Expr, list.Len());
|
|
|
|
for i := 0; i < list.Len(); i++ {
|
|
|
|
elts[i] = list.At(i).(ast.Expr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return elts;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseCompositeLit(typ ast.Expr) ast.Expr {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "CompositeLit"));
|
2008-10-17 17:19:31 -06:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
lbrace := p.expect(token.LBRACE);
|
2009-03-17 19:41:35 -06:00
|
|
|
var elts []ast.Expr;
|
2009-03-26 23:16:06 -06:00
|
|
|
if p.tok != token.RBRACE {
|
2009-03-27 20:27:09 -06:00
|
|
|
elts = p.parseExpressionOrKeyValueList();
|
2009-03-17 19:41:35 -06:00
|
|
|
}
|
2009-03-26 23:16:06 -06:00
|
|
|
rbrace := p.expect(token.RBRACE);
|
2009-03-20 18:18:48 -06:00
|
|
|
return &ast.CompositeLit{typ, lbrace, elts, rbrace};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-30 18:13:11 -06:00
|
|
|
// TODO Consider different approach to checking syntax after parsing:
|
|
|
|
// Provide a arguments (set of flags) to parsing functions
|
|
|
|
// restricting what they are syupposed to accept depending
|
|
|
|
// on context.
|
2009-03-27 20:27:09 -06:00
|
|
|
|
2009-03-30 18:13:11 -06:00
|
|
|
// checkExpr checks that x is an expression (and not a type).
|
|
|
|
func (p *parser) checkExpr(x ast.Expr) ast.Expr {
|
2009-03-27 20:27:09 -06:00
|
|
|
// TODO should provide predicate in AST nodes
|
|
|
|
switch t := x.(type) {
|
2009-03-30 18:13:11 -06:00
|
|
|
case *ast.BadExpr:
|
|
|
|
case *ast.Ident:
|
|
|
|
case *ast.IntLit:
|
|
|
|
case *ast.FloatLit:
|
|
|
|
case *ast.CharLit:
|
|
|
|
case *ast.StringLit:
|
|
|
|
case *ast.StringList:
|
|
|
|
case *ast.FunctionLit:
|
|
|
|
case *ast.CompositeLit:
|
|
|
|
case *ast.ParenExpr:
|
|
|
|
case *ast.SelectorExpr:
|
|
|
|
case *ast.IndexExpr:
|
|
|
|
case *ast.SliceExpr:
|
|
|
|
case *ast.TypeAssertExpr:
|
|
|
|
case *ast.CallExpr:
|
|
|
|
case *ast.StarExpr:
|
|
|
|
case *ast.UnaryExpr:
|
|
|
|
if t.Op == token.RANGE {
|
|
|
|
// the range operator is only allowed at the top of a for statement
|
|
|
|
p.error_expected(x.Pos(), "expression");
|
|
|
|
x = &ast.BadExpr{x.Pos()};
|
|
|
|
}
|
|
|
|
case *ast.BinaryExpr:
|
|
|
|
default:
|
|
|
|
// all other nodes are not proper expressions
|
|
|
|
p.error_expected(x.Pos(), "expression");
|
|
|
|
x = &ast.BadExpr{x.Pos()};
|
2009-03-27 20:27:09 -06:00
|
|
|
}
|
2009-03-30 18:13:11 -06:00
|
|
|
return x;
|
2009-03-27 20:27:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-30 18:13:11 -06:00
|
|
|
// checkTypeName checks that x is type name.
|
|
|
|
func (p *parser) checkTypeName(x ast.Expr) ast.Expr {
|
2009-03-27 20:27:09 -06:00
|
|
|
// TODO should provide predicate in AST nodes
|
|
|
|
switch t := x.(type) {
|
2009-03-30 18:13:11 -06:00
|
|
|
case *ast.BadExpr:
|
|
|
|
case *ast.Ident:
|
|
|
|
case *ast.ParenExpr: p.checkTypeName(t.X); // TODO should (TypeName) be illegal?
|
|
|
|
case *ast.SelectorExpr: p.checkTypeName(t.X);
|
|
|
|
default:
|
|
|
|
// all other nodes are not type names
|
|
|
|
p.error_expected(x.Pos(), "type name");
|
|
|
|
x = &ast.BadExpr{x.Pos()};
|
2009-03-28 00:11:54 -06:00
|
|
|
}
|
2009-03-30 18:13:11 -06:00
|
|
|
return x;
|
2009-03-28 00:11:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-30 18:13:11 -06:00
|
|
|
// checkCompositeLitType checks that x is a legal composite literal type.
|
|
|
|
func (p *parser) checkCompositeLitType(x ast.Expr) ast.Expr {
|
2009-03-28 00:11:54 -06:00
|
|
|
// TODO should provide predicate in AST nodes
|
|
|
|
switch t := x.(type) {
|
|
|
|
case *ast.BadExpr: return x;
|
|
|
|
case *ast.Ident: return x;
|
2009-03-30 18:13:11 -06:00
|
|
|
case *ast.ParenExpr: p.checkCompositeLitType(t.X);
|
|
|
|
case *ast.SelectorExpr: p.checkTypeName(t.X);
|
2009-03-28 00:11:54 -06:00
|
|
|
case *ast.ArrayType: return x;
|
2009-03-27 20:27:09 -06:00
|
|
|
case *ast.SliceType: return x;
|
|
|
|
case *ast.StructType: return x;
|
|
|
|
case *ast.MapType: return x;
|
2009-03-30 18:13:11 -06:00
|
|
|
default:
|
|
|
|
// all other nodes are not legal composite literal types
|
|
|
|
p.error_expected(x.Pos(), "composite literal type");
|
|
|
|
x = &ast.BadExpr{x.Pos()};
|
2009-03-27 20:27:09 -06:00
|
|
|
}
|
2009-03-30 18:13:11 -06:00
|
|
|
return x;
|
2009-03-27 20:27:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-30 18:13:11 -06:00
|
|
|
// checkExprOrType checks that x is an expression or a type
|
2009-03-27 20:27:09 -06:00
|
|
|
// (and not a raw type such as [...]T).
|
|
|
|
//
|
2009-03-30 18:13:11 -06:00
|
|
|
func (p *parser) checkExprOrType(x ast.Expr) ast.Expr {
|
2009-03-27 20:27:09 -06:00
|
|
|
// TODO should provide predicate in AST nodes
|
2009-03-30 18:13:11 -06:00
|
|
|
switch t := x.(type) {
|
|
|
|
case *ast.UnaryExpr:
|
|
|
|
if t.Op == token.RANGE {
|
|
|
|
// the range operator is only allowed at the top of a for statement
|
|
|
|
p.error_expected(x.Pos(), "expression");
|
|
|
|
x = &ast.BadExpr{x.Pos()};
|
|
|
|
}
|
|
|
|
case *ast.ArrayType:
|
2009-03-27 20:27:09 -06:00
|
|
|
if len, is_ellipsis := t.Len.(*ast.Ellipsis); is_ellipsis {
|
|
|
|
p.error(len.Pos(), "expected array length, found '...'");
|
2009-03-30 18:13:11 -06:00
|
|
|
x = &ast.BadExpr{x.Pos()};
|
2009-03-27 20:27:09 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// all other nodes are expressions or types
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parsePrimaryExpr() ast.Expr {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "PrimaryExpr"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
x := p.parseOperand();
|
2008-10-09 19:03:16 -06:00
|
|
|
for {
|
2009-03-26 23:16:06 -06:00
|
|
|
switch p.tok {
|
2009-03-30 18:13:11 -06:00
|
|
|
case token.PERIOD: x = p.parseSelectorOrTypeAssertion(p.checkExpr(x));
|
|
|
|
case token.LBRACK: x = p.parseIndexOrSlice(p.checkExpr(x));
|
|
|
|
case token.LPAREN: x = p.parseCallOrConversion(p.checkExprOrType(x));
|
2009-03-03 19:25:07 -07:00
|
|
|
case token.LBRACE:
|
2009-03-26 23:16:06 -06:00
|
|
|
if p.expr_lev >= 0 {
|
2009-03-30 18:13:11 -06:00
|
|
|
x = p.parseCompositeLit(p.checkCompositeLitType(x));
|
2009-03-03 17:00:06 -07:00
|
|
|
} else {
|
2009-03-30 18:13:11 -06:00
|
|
|
return p.checkExprOrType(x);
|
2009-03-03 17:00:06 -07:00
|
|
|
}
|
2009-02-03 18:44:01 -07:00
|
|
|
default:
|
2009-03-30 18:13:11 -06:00
|
|
|
return p.checkExprOrType(x);
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-27 20:27:09 -06:00
|
|
|
panic(); // unreachable
|
2009-02-03 18:44:01 -07:00
|
|
|
return nil;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseUnaryExpr() ast.Expr {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "UnaryExpr"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
switch p.tok {
|
2009-03-20 18:18:48 -06:00
|
|
|
case token.ADD, token.SUB, token.NOT, token.XOR, token.ARROW, token.AND, token.RANGE:
|
2009-03-27 20:27:09 -06:00
|
|
|
pos, op := p.pos, p.tok;
|
2009-03-26 23:16:06 -06:00
|
|
|
p.next();
|
|
|
|
x := p.parseUnaryExpr();
|
2009-03-30 18:13:11 -06:00
|
|
|
return &ast.UnaryExpr{pos, op, p.checkExpr(x)};
|
2009-03-20 18:18:48 -06:00
|
|
|
|
|
|
|
case token.MUL:
|
|
|
|
// unary "*" expression or pointer type
|
2009-03-26 23:16:06 -06:00
|
|
|
pos := p.pos;
|
|
|
|
p.next();
|
|
|
|
x := p.parseUnaryExpr();
|
2009-03-30 18:13:11 -06:00
|
|
|
return &ast.StarExpr{pos, p.checkExprOrType(x)};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
return p.parsePrimaryExpr();
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseBinaryExpr(prec1 int) ast.Expr {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "BinaryExpr"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
x := p.parseUnaryExpr();
|
|
|
|
for prec := p.tok.Precedence(); prec >= prec1; prec-- {
|
|
|
|
for p.tok.Precedence() == prec {
|
2009-03-27 20:27:09 -06:00
|
|
|
pos, op := p.pos, p.tok;
|
2009-03-26 23:16:06 -06:00
|
|
|
p.next();
|
|
|
|
y := p.parseBinaryExpr(prec + 1);
|
2009-03-30 18:13:11 -06:00
|
|
|
x = &ast.BinaryExpr{p.checkExpr(x), pos, op, p.checkExpr(y)};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2008-09-18 17:58:37 -06:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-27 20:27:09 -06:00
|
|
|
func (p *parser) parseExpression() ast.Expr {
|
2009-03-26 23:16:06 -06:00
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "Expression"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-10-18 21:20:30 -06:00
|
|
|
|
2009-03-27 20:27:09 -06:00
|
|
|
return p.parseBinaryExpr(token.LowestPrec + 1);
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Statements
|
|
|
|
|
2009-03-16 21:29:31 -06:00
|
|
|
|
2009-03-30 18:13:11 -06:00
|
|
|
func (p *parser) parseSimpleStmt(label_ok bool) ast.Stmt {
|
2009-03-26 23:16:06 -06:00
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "SimpleStmt"));
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
x := p.parseExpressionList();
|
2009-02-04 19:28:41 -07:00
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
switch p.tok {
|
2009-03-03 19:25:07 -07:00
|
|
|
case token.COLON:
|
2009-03-13 17:59:51 -06:00
|
|
|
// labeled statement
|
2009-03-30 18:13:11 -06:00
|
|
|
p.next();
|
|
|
|
if label_ok && len(x) == 1 {
|
2009-03-16 21:29:31 -06:00
|
|
|
if label, is_ident := x[0].(*ast.Ident); is_ident {
|
2009-03-26 23:16:06 -06:00
|
|
|
return &ast.LabeledStmt{label, p.parseStatement()};
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
|
|
|
}
|
2009-03-26 23:16:06 -06:00
|
|
|
p.error(x[0].Pos(), "illegal label declaration");
|
2009-03-25 13:45:06 -06:00
|
|
|
return &ast.BadStmt{x[0].Pos()};
|
2009-02-13 15:48:32 -07:00
|
|
|
|
2009-02-04 19:28:41 -07:00
|
|
|
case
|
2009-03-03 19:25:07 -07:00
|
|
|
token.DEFINE, token.ASSIGN, token.ADD_ASSIGN,
|
|
|
|
token.SUB_ASSIGN, token.MUL_ASSIGN, token.QUO_ASSIGN,
|
|
|
|
token.REM_ASSIGN, token.AND_ASSIGN, token.OR_ASSIGN,
|
|
|
|
token.XOR_ASSIGN, token.SHL_ASSIGN, token.SHR_ASSIGN:
|
2009-03-25 13:45:06 -06:00
|
|
|
// assignment statement
|
2009-03-26 23:16:06 -06:00
|
|
|
pos, tok := p.pos, p.tok;
|
|
|
|
p.next();
|
|
|
|
y := p.parseExpressionList();
|
2009-03-25 13:45:06 -06:00
|
|
|
if len(x) > 1 && len(y) > 1 && len(x) != len(y) {
|
2009-03-26 23:16:06 -06:00
|
|
|
p.error(x[0].Pos(), "arity of lhs doesn't match rhs");
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
2009-03-25 13:45:06 -06:00
|
|
|
return &ast.AssignStmt{x, pos, tok, y};
|
|
|
|
}
|
2009-02-13 15:48:32 -07:00
|
|
|
|
2009-03-25 13:45:06 -06:00
|
|
|
if len(x) > 1 {
|
2009-03-26 23:16:06 -06:00
|
|
|
p.error(x[0].Pos(), "only one expression allowed");
|
2009-03-25 13:45:06 -06:00
|
|
|
// continue with first expression
|
|
|
|
}
|
2009-02-13 15:48:32 -07:00
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
if p.tok == token.INC || p.tok == token.DEC {
|
2009-03-25 13:45:06 -06:00
|
|
|
// increment or decrement
|
2009-03-26 23:16:06 -06:00
|
|
|
s := &ast.IncDecStmt{x[0], p.tok};
|
|
|
|
p.next(); // consume "++" or "--"
|
2009-03-25 13:45:06 -06:00
|
|
|
return s;
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
|
|
|
|
2009-03-25 13:45:06 -06:00
|
|
|
// expression
|
|
|
|
return &ast.ExprStmt{x[0]};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseCallExpr() *ast.CallExpr {
|
2009-03-27 20:27:09 -06:00
|
|
|
x := p.parseExpression();
|
2009-03-25 13:45:06 -06:00
|
|
|
if call, is_call := x.(*ast.CallExpr); is_call {
|
|
|
|
return call;
|
|
|
|
}
|
2009-03-27 20:27:09 -06:00
|
|
|
p.error_expected(x.Pos(), "function/method call");
|
2009-02-04 19:28:41 -07:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseGoStmt() ast.Stmt {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "GoStmt"));
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
pos := p.expect(token.GO);
|
|
|
|
call := p.parseCallExpr();
|
2009-03-25 13:45:06 -06:00
|
|
|
if call != nil {
|
|
|
|
return &ast.GoStmt{pos, call};
|
|
|
|
}
|
|
|
|
return &ast.BadStmt{pos};
|
2009-03-20 18:18:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseDeferStmt() ast.Stmt {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "DeferStmt"));
|
2009-03-20 18:18:48 -06:00
|
|
|
}
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
pos := p.expect(token.DEFER);
|
|
|
|
call := p.parseCallExpr();
|
2009-03-25 13:45:06 -06:00
|
|
|
if call != nil {
|
|
|
|
return &ast.DeferStmt{pos, call};
|
|
|
|
}
|
|
|
|
return &ast.BadStmt{pos};
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseReturnStmt() *ast.ReturnStmt {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "ReturnStmt"));
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
pos := p.pos;
|
|
|
|
p.expect(token.RETURN);
|
2009-03-16 21:29:31 -06:00
|
|
|
var x []ast.Expr;
|
2009-03-26 23:16:06 -06:00
|
|
|
if p.tok != token.SEMICOLON && p.tok != token.RBRACE {
|
|
|
|
x = p.parseExpressionList();
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
return &ast.ReturnStmt{pos, x};
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseBranchStmt(tok token.Token) *ast.BranchStmt {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "BranchStmt"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
s := &ast.BranchStmt{p.pos, tok, nil};
|
|
|
|
p.expect(tok);
|
|
|
|
if tok != token.FALLTHROUGH && p.tok == token.IDENT {
|
|
|
|
s.Label = p.parseIdent();
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2008-10-14 19:14:01 -06:00
|
|
|
return s;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) isExpr(s ast.Stmt) bool {
|
2009-03-25 13:45:06 -06:00
|
|
|
if s == nil {
|
|
|
|
return true;
|
2009-03-20 18:18:48 -06:00
|
|
|
}
|
2009-03-25 13:45:06 -06:00
|
|
|
dummy, is_expr := s.(*ast.ExprStmt);
|
|
|
|
return is_expr;
|
2009-03-20 18:18:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-30 18:13:11 -06:00
|
|
|
func (p *parser) makeExpr(s ast.Stmt) ast.Expr {
|
2009-03-25 13:45:06 -06:00
|
|
|
if s == nil {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
if es, is_expr := s.(*ast.ExprStmt); is_expr {
|
2009-03-30 18:13:11 -06:00
|
|
|
return p.checkExpr(es.X);
|
2009-03-20 18:18:48 -06:00
|
|
|
}
|
2009-03-27 20:27:09 -06:00
|
|
|
p.error(s.Pos(), "expected condition, found simple statement");
|
2009-03-25 13:45:06 -06:00
|
|
|
return &ast.BadExpr{s.Pos()};
|
2009-03-20 18:18:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseControlClause(isForStmt bool) (s1, s2, s3 ast.Stmt) {
|
|
|
|
if p.tok != token.LBRACE {
|
|
|
|
prev_lev := p.expr_lev;
|
|
|
|
p.expr_lev = -1;
|
2009-03-16 21:29:31 -06:00
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
if p.tok != token.SEMICOLON {
|
2009-03-30 18:13:11 -06:00
|
|
|
s1 = p.parseSimpleStmt(false);
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
2009-03-26 23:16:06 -06:00
|
|
|
if p.tok == token.SEMICOLON {
|
|
|
|
p.next();
|
|
|
|
if p.tok != token.LBRACE && p.tok != token.SEMICOLON {
|
2009-03-30 18:13:11 -06:00
|
|
|
s2 = p.parseSimpleStmt(false);
|
2009-03-20 18:18:48 -06:00
|
|
|
}
|
2009-03-25 13:45:06 -06:00
|
|
|
if isForStmt {
|
2009-03-20 18:18:48 -06:00
|
|
|
// for statements have a 3rd section
|
2009-03-26 23:16:06 -06:00
|
|
|
p.expect(token.SEMICOLON);
|
|
|
|
if p.tok != token.LBRACE {
|
2009-03-30 18:13:11 -06:00
|
|
|
s3 = p.parseSimpleStmt(false);
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
|
|
|
}
|
2009-03-20 18:18:48 -06:00
|
|
|
} else {
|
|
|
|
s1, s2 = nil, s1;
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
2009-03-20 18:18:48 -06:00
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
p.expr_lev = prev_lev;
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
return s1, s2, s3;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseIfStmt() *ast.IfStmt {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "IfStmt"));
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
pos := p.expect(token.IF);
|
|
|
|
s1, s2, dummy := p.parseControlClause(false);
|
|
|
|
body := p.parseBlockStmt();
|
2009-03-25 13:45:06 -06:00
|
|
|
var else_ ast.Stmt;
|
2009-03-26 23:16:06 -06:00
|
|
|
if p.tok == token.ELSE {
|
|
|
|
p.next();
|
|
|
|
else_ = p.parseStatement();
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
|
|
|
|
2009-03-30 18:13:11 -06:00
|
|
|
return &ast.IfStmt{pos, s1, p.makeExpr(s2), body, else_};
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseCaseClause() *ast.CaseClause {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "CaseClause"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// SwitchCase
|
2009-03-26 23:16:06 -06:00
|
|
|
pos := p.pos;
|
2009-03-20 18:18:48 -06:00
|
|
|
var x []ast.Expr;
|
2009-03-26 23:16:06 -06:00
|
|
|
if p.tok == token.CASE {
|
|
|
|
p.next();
|
|
|
|
x = p.parseExpressionList();
|
2009-03-20 18:18:48 -06:00
|
|
|
} else {
|
2009-03-26 23:16:06 -06:00
|
|
|
p.expect(token.DEFAULT);
|
2009-03-16 21:29:31 -06:00
|
|
|
}
|
2009-03-25 13:45:06 -06:00
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
colon := p.expect(token.COLON);
|
|
|
|
body := p.parseStatementList();
|
2009-03-16 21:29:31 -06:00
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
return &ast.CaseClause{pos, x, colon, body};
|
2009-03-16 21:29:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseTypeCaseClause() *ast.TypeCaseClause {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "CaseClause"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// TypeSwitchCase
|
2009-03-26 23:16:06 -06:00
|
|
|
pos := p.pos;
|
2009-03-20 18:18:48 -06:00
|
|
|
var typ ast.Expr;
|
2009-03-26 23:16:06 -06:00
|
|
|
if p.tok == token.CASE {
|
|
|
|
p.next();
|
|
|
|
typ = p.parseType();
|
2008-09-18 17:58:37 -06:00
|
|
|
} else {
|
2009-03-26 23:16:06 -06:00
|
|
|
p.expect(token.DEFAULT);
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
colon := p.expect(token.COLON);
|
|
|
|
body := p.parseStatementList();
|
2009-03-25 13:45:06 -06:00
|
|
|
|
|
|
|
return &ast.TypeCaseClause{pos, typ, colon, body};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseSwitchStmt() ast.Stmt {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "SwitchStmt"));
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
pos := p.expect(token.SWITCH);
|
|
|
|
s1, s2, dummy := p.parseControlClause(false);
|
2009-02-04 19:28:41 -07:00
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
if p.isExpr(s2) {
|
2009-03-20 18:18:48 -06:00
|
|
|
// expression switch
|
2009-03-26 23:16:06 -06:00
|
|
|
lbrace := p.expect(token.LBRACE);
|
2009-03-20 18:18:48 -06:00
|
|
|
cases := vector.New(0);
|
2009-03-26 23:16:06 -06:00
|
|
|
for p.tok == token.CASE || p.tok == token.DEFAULT {
|
|
|
|
cases.Push(p.parseCaseClause());
|
2009-03-16 21:29:31 -06:00
|
|
|
}
|
2009-03-26 23:16:06 -06:00
|
|
|
rbrace := p.expect(token.RBRACE);
|
|
|
|
p.opt_semi = true;
|
2009-03-27 20:27:09 -06:00
|
|
|
body := &ast.BlockStmt{lbrace, makeStmtList(cases), rbrace};
|
2009-03-30 18:13:11 -06:00
|
|
|
return &ast.SwitchStmt{pos, s1, p.makeExpr(s2), body};
|
2009-03-16 21:29:31 -06:00
|
|
|
}
|
|
|
|
|
2009-03-27 20:27:09 -06:00
|
|
|
// type switch
|
|
|
|
// TODO do all the checks!
|
|
|
|
lbrace := p.expect(token.LBRACE);
|
|
|
|
cases := vector.New(0);
|
|
|
|
for p.tok == token.CASE || p.tok == token.DEFAULT {
|
|
|
|
cases.Push(p.parseTypeCaseClause());
|
|
|
|
}
|
|
|
|
rbrace := p.expect(token.RBRACE);
|
|
|
|
p.opt_semi = true;
|
|
|
|
body := &ast.BlockStmt{lbrace, makeStmtList(cases), rbrace};
|
|
|
|
return &ast.TypeSwitchStmt{pos, s1, s2, body};
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseCommClause() *ast.CommClause {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "CommClause"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-10-14 19:14:01 -06:00
|
|
|
|
2009-02-05 12:05:02 -07:00
|
|
|
// CommCase
|
2009-03-26 23:16:06 -06:00
|
|
|
pos := p.pos;
|
2009-03-26 11:53:14 -06:00
|
|
|
var tok token.Token;
|
2009-03-16 21:29:31 -06:00
|
|
|
var lhs, rhs ast.Expr;
|
2009-03-26 23:16:06 -06:00
|
|
|
if p.tok == token.CASE {
|
|
|
|
p.next();
|
|
|
|
if p.tok == token.ARROW {
|
2009-03-16 21:29:31 -06:00
|
|
|
// RecvExpr without assignment
|
2009-03-27 20:27:09 -06:00
|
|
|
rhs = p.parseExpression();
|
2009-03-16 21:29:31 -06:00
|
|
|
} else {
|
|
|
|
// SendExpr or RecvExpr
|
2009-03-27 20:27:09 -06:00
|
|
|
rhs = p.parseExpression();
|
2009-03-26 23:16:06 -06:00
|
|
|
if p.tok == token.ASSIGN || p.tok == token.DEFINE {
|
2009-03-16 21:29:31 -06:00
|
|
|
// RecvExpr with assignment
|
2009-03-26 23:16:06 -06:00
|
|
|
tok = p.tok;
|
|
|
|
p.next();
|
2009-03-16 21:29:31 -06:00
|
|
|
lhs = rhs;
|
2009-03-26 23:16:06 -06:00
|
|
|
if p.tok == token.ARROW {
|
2009-03-27 20:27:09 -06:00
|
|
|
rhs = p.parseExpression();
|
2009-03-16 21:29:31 -06:00
|
|
|
} else {
|
2009-03-26 23:16:06 -06:00
|
|
|
p.expect(token.ARROW); // use expect() error handling
|
2009-03-16 21:29:31 -06:00
|
|
|
}
|
2008-10-26 22:32:30 -06:00
|
|
|
}
|
2009-03-16 21:29:31 -06:00
|
|
|
// else SendExpr
|
2008-10-14 19:14:01 -06:00
|
|
|
}
|
|
|
|
} else {
|
2009-03-26 23:16:06 -06:00
|
|
|
p.expect(token.DEFAULT);
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2008-10-14 19:14:01 -06:00
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
colon := p.expect(token.COLON);
|
|
|
|
body := p.parseStatementList();
|
2009-03-25 13:45:06 -06:00
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
return &ast.CommClause{pos, tok, lhs, rhs, colon, body};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseSelectStmt() *ast.SelectStmt {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "SelectStmt"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
pos := p.expect(token.SELECT);
|
|
|
|
lbrace := p.expect(token.LBRACE);
|
2009-03-20 18:18:48 -06:00
|
|
|
cases := vector.New(0);
|
2009-03-26 23:16:06 -06:00
|
|
|
for p.tok == token.CASE || p.tok == token.DEFAULT {
|
|
|
|
cases.Push(p.parseCommClause());
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
2009-03-26 23:16:06 -06:00
|
|
|
rbrace := p.expect(token.RBRACE);
|
|
|
|
p.opt_semi = true;
|
2009-03-27 20:27:09 -06:00
|
|
|
body := &ast.BlockStmt{lbrace, makeStmtList(cases), rbrace};
|
2009-02-04 19:28:41 -07:00
|
|
|
|
2009-03-25 13:45:06 -06:00
|
|
|
return &ast.SelectStmt{pos, body};
|
2009-03-20 18:18:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseForStmt() ast.Stmt {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "ForStmt"));
|
2009-03-20 18:18:48 -06:00
|
|
|
}
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
pos := p.expect(token.FOR);
|
|
|
|
s1, s2, s3 := p.parseControlClause(true);
|
|
|
|
body := p.parseBlockStmt();
|
2009-03-20 18:18:48 -06:00
|
|
|
|
2009-03-25 13:45:06 -06:00
|
|
|
if as, is_as := s2.(*ast.AssignStmt); is_as {
|
|
|
|
// possibly a for statement with a range clause; check assignment operator
|
|
|
|
if as.Tok != token.ASSIGN && as.Tok != token.DEFINE {
|
2009-03-27 20:27:09 -06:00
|
|
|
p.error_expected(as.TokPos, "'=' or ':='");
|
2009-03-25 13:45:06 -06:00
|
|
|
return &ast.BadStmt{pos};
|
|
|
|
}
|
|
|
|
// check lhs
|
|
|
|
var key, value ast.Expr;
|
|
|
|
switch len(as.Lhs) {
|
|
|
|
case 2:
|
|
|
|
value = as.Lhs[1];
|
|
|
|
fallthrough;
|
|
|
|
case 1:
|
|
|
|
key = as.Lhs[0];
|
|
|
|
default:
|
2009-03-27 20:27:09 -06:00
|
|
|
p.error_expected(as.Lhs[0].Pos(), "1 or 2 expressions");
|
2009-03-25 13:45:06 -06:00
|
|
|
return &ast.BadStmt{pos};
|
|
|
|
}
|
|
|
|
// check rhs
|
|
|
|
if len(as.Rhs) != 1 {
|
2009-03-27 20:27:09 -06:00
|
|
|
p.error_expected(as.Rhs[0].Pos(), "1 expressions");
|
2009-03-25 13:45:06 -06:00
|
|
|
return &ast.BadStmt{pos};
|
|
|
|
}
|
2009-03-26 17:10:07 -06:00
|
|
|
if rhs, is_unary := as.Rhs[0].(*ast.UnaryExpr); is_unary && rhs.Op == token.RANGE {
|
2009-03-25 13:45:06 -06:00
|
|
|
// rhs is range expression; check lhs
|
2009-03-26 17:10:07 -06:00
|
|
|
return &ast.RangeStmt{pos, key, value, as.TokPos, as.Tok, rhs.X, body}
|
2009-03-25 13:45:06 -06:00
|
|
|
} else {
|
2009-03-27 20:27:09 -06:00
|
|
|
p.error_expected(s2.Pos(), "range clause");
|
2009-03-25 13:45:06 -06:00
|
|
|
return &ast.BadStmt{pos};
|
|
|
|
}
|
2009-03-20 18:18:48 -06:00
|
|
|
} else {
|
|
|
|
// regular for statement
|
2009-03-30 18:13:11 -06:00
|
|
|
return &ast.ForStmt{pos, s1, p.makeExpr(s2), s3, body};
|
2009-03-20 18:18:48 -06:00
|
|
|
}
|
|
|
|
|
2009-03-27 20:27:09 -06:00
|
|
|
panic(); // unreachable
|
2009-03-20 18:18:48 -06:00
|
|
|
return nil;
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseStatement() ast.Stmt {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "Statement"));
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
switch p.tok {
|
2009-03-03 19:25:07 -07:00
|
|
|
case token.CONST, token.TYPE, token.VAR:
|
2009-03-26 23:16:06 -06:00
|
|
|
return &ast.DeclStmt{p.parseDeclaration()};
|
2009-02-04 19:28:41 -07:00
|
|
|
case
|
2009-03-13 17:59:51 -06:00
|
|
|
// tokens that may start a top-level expression
|
|
|
|
token.IDENT, token.INT, token.FLOAT, token.CHAR, token.STRING, token.FUNC, token.LPAREN, // operand
|
2009-03-03 19:25:07 -07:00
|
|
|
token.LBRACK, token.STRUCT, // composite type
|
2009-03-13 17:59:51 -06:00
|
|
|
token.MUL, token.AND, token.ARROW: // unary operators
|
2009-03-30 18:13:11 -06:00
|
|
|
return p.parseSimpleStmt(true);
|
2009-03-20 18:18:48 -06:00
|
|
|
case token.GO:
|
2009-03-26 23:16:06 -06:00
|
|
|
return p.parseGoStmt();
|
2009-03-20 18:18:48 -06:00
|
|
|
case token.DEFER:
|
2009-03-26 23:16:06 -06:00
|
|
|
return p.parseDeferStmt();
|
2009-03-03 19:25:07 -07:00
|
|
|
case token.RETURN:
|
2009-03-26 23:16:06 -06:00
|
|
|
return p.parseReturnStmt();
|
2009-03-03 19:25:07 -07:00
|
|
|
case token.BREAK, token.CONTINUE, token.GOTO, token.FALLTHROUGH:
|
2009-03-26 23:16:06 -06:00
|
|
|
return p.parseBranchStmt(p.tok);
|
2009-03-03 19:25:07 -07:00
|
|
|
case token.LBRACE:
|
2009-03-26 23:16:06 -06:00
|
|
|
return p.parseBlockStmt();
|
2009-03-03 19:25:07 -07:00
|
|
|
case token.IF:
|
2009-03-26 23:16:06 -06:00
|
|
|
return p.parseIfStmt();
|
2009-03-03 19:25:07 -07:00
|
|
|
case token.SWITCH:
|
2009-03-26 23:16:06 -06:00
|
|
|
return p.parseSwitchStmt();
|
2009-03-03 19:25:07 -07:00
|
|
|
case token.SELECT:
|
2009-03-26 23:16:06 -06:00
|
|
|
return p.parseSelectStmt();
|
2009-03-27 20:27:09 -06:00
|
|
|
case token.FOR:
|
|
|
|
return p.parseForStmt();
|
2009-03-13 17:59:51 -06:00
|
|
|
case token.SEMICOLON, token.RBRACE:
|
2009-02-12 17:06:21 -07:00
|
|
|
// don't consume the ";", it is the separator following the empty statement
|
2009-03-26 23:16:06 -06:00
|
|
|
return &ast.EmptyStmt{p.pos};
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
|
|
|
|
2009-02-12 17:06:21 -07:00
|
|
|
// no statement found
|
2009-03-27 20:27:09 -06:00
|
|
|
p.error_expected(p.pos, "statement");
|
2009-03-26 23:16:06 -06:00
|
|
|
return &ast.BadStmt{p.pos};
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-18 17:58:37 -06:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Declarations
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseImportSpec(pos token.Position, doc ast.Comments) *ast.ImportDecl {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "ImportSpec"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
var ident *ast.Ident;
|
2009-03-26 23:16:06 -06:00
|
|
|
if p.tok == token.PERIOD {
|
|
|
|
p.error(p.pos, `"import ." not yet handled properly`);
|
|
|
|
p.next();
|
|
|
|
} else if p.tok == token.IDENT {
|
|
|
|
ident = p.parseIdent();
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-25 13:45:06 -06:00
|
|
|
var path []*ast.StringLit;
|
2009-03-26 23:16:06 -06:00
|
|
|
if p.tok == token.STRING {
|
|
|
|
path = p.parseStringList(nil);
|
2008-09-18 17:58:37 -06:00
|
|
|
} else {
|
2009-03-26 23:16:06 -06:00
|
|
|
p.expect(token.STRING); // use expect() error handling
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2009-03-11 17:06:17 -06:00
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
return &ast.ImportDecl{doc, pos, ident, path};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseConstSpec(pos token.Position, doc ast.Comments) *ast.ConstDecl {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "ConstSpec"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-27 20:27:09 -06:00
|
|
|
idents := p.parseIdentList(nil);
|
2009-03-26 23:16:06 -06:00
|
|
|
typ := p.tryType();
|
2009-03-16 21:29:31 -06:00
|
|
|
var values []ast.Expr;
|
2009-03-26 23:16:06 -06:00
|
|
|
if typ != nil || p.tok == token.ASSIGN {
|
|
|
|
p.expect(token.ASSIGN);
|
|
|
|
values = p.parseExpressionList();
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2009-03-11 17:06:17 -06:00
|
|
|
|
2009-03-27 20:27:09 -06:00
|
|
|
return &ast.ConstDecl{doc, pos, idents, typ, values};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseTypeSpec(pos token.Position, doc ast.Comments) *ast.TypeDecl {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "TypeSpec"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-09-18 17:58:37 -06:00
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
ident := p.parseIdent();
|
|
|
|
typ := p.parseType();
|
2009-03-11 17:06:17 -06:00
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
return &ast.TypeDecl{doc, pos, ident, typ};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseVarSpec(pos token.Position, doc ast.Comments) *ast.VarDecl {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "VarSpec"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-27 20:27:09 -06:00
|
|
|
idents := p.parseIdentList(nil);
|
2009-03-26 23:16:06 -06:00
|
|
|
typ := p.tryType();
|
2009-03-16 21:29:31 -06:00
|
|
|
var values []ast.Expr;
|
2009-03-26 23:16:06 -06:00
|
|
|
if typ == nil || p.tok == token.ASSIGN {
|
|
|
|
p.expect(token.ASSIGN);
|
|
|
|
values = p.parseExpressionList();
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2009-03-11 17:06:17 -06:00
|
|
|
|
2009-03-27 20:27:09 -06:00
|
|
|
return &ast.VarDecl{doc, pos, idents, typ, values};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseSpec(pos token.Position, doc ast.Comments, keyword int) ast.Decl {
|
2009-02-27 16:40:17 -07:00
|
|
|
switch keyword {
|
2009-03-26 23:16:06 -06:00
|
|
|
case token.IMPORT: return p.parseImportSpec(pos, doc);
|
|
|
|
case token.CONST: return p.parseConstSpec(pos, doc);
|
|
|
|
case token.TYPE: return p.parseTypeSpec(pos, doc);
|
|
|
|
case token.VAR: return p.parseVarSpec(pos, doc);
|
2009-01-23 10:44:01 -07:00
|
|
|
}
|
2009-03-11 17:06:17 -06:00
|
|
|
|
2009-03-27 20:27:09 -06:00
|
|
|
panic(); // unreachable
|
2009-02-27 16:40:17 -07:00
|
|
|
return nil;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseDecl(keyword int) ast.Decl {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "Decl"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
doc := p.getDoc();
|
|
|
|
pos := p.expect(keyword);
|
|
|
|
if p.tok == token.LPAREN {
|
|
|
|
lparen := p.pos;
|
|
|
|
p.next();
|
2009-02-27 16:40:17 -07:00
|
|
|
list := vector.New(0);
|
2009-03-26 23:16:06 -06:00
|
|
|
for p.tok != token.RPAREN && p.tok != token.EOF {
|
2009-03-27 20:27:09 -06:00
|
|
|
list.Push(p.parseSpec(noPos, nil, keyword));
|
2009-03-26 23:16:06 -06:00
|
|
|
if p.tok == token.SEMICOLON {
|
|
|
|
p.next();
|
2008-10-09 19:03:16 -06:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2009-03-26 23:16:06 -06:00
|
|
|
rparen := p.expect(token.RPAREN);
|
|
|
|
p.opt_semi = true;
|
2009-03-11 17:06:17 -06:00
|
|
|
|
2009-02-27 16:40:17 -07:00
|
|
|
// convert vector
|
2009-03-05 18:15:36 -07:00
|
|
|
decls := make([]ast.Decl, list.Len());
|
2009-02-27 16:40:17 -07:00
|
|
|
for i := 0; i < list.Len(); i++ {
|
2009-03-05 18:15:36 -07:00
|
|
|
decls[i] = list.At(i).(ast.Decl);
|
2009-02-27 16:40:17 -07:00
|
|
|
}
|
2009-03-11 17:06:17 -06:00
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
return &ast.DeclList{doc, pos, keyword, lparen, decls, rparen};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
return p.parseSpec(pos, doc, keyword);
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-28 00:11:54 -06:00
|
|
|
func (p *parser) parseReceiver() *ast.Field {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "Receiver"));
|
|
|
|
}
|
|
|
|
|
|
|
|
pos := p.pos;
|
|
|
|
par := p.parseParameters(false);
|
|
|
|
|
|
|
|
// must have exactly one receiver
|
|
|
|
if len(par) != 1 || len(par) == 1 && len(par[0].Names) > 1 {
|
|
|
|
p.error_expected(pos, "exactly one receiver");
|
|
|
|
return &ast.Field{nil, nil, &ast.BadExpr{noPos}, nil};
|
|
|
|
}
|
|
|
|
|
|
|
|
recv := par[0];
|
|
|
|
|
|
|
|
// recv type must be TypeName or *TypeName
|
|
|
|
base := recv.Type;
|
|
|
|
if ptr, is_ptr := base.(*ast.StarExpr); is_ptr {
|
|
|
|
base = ptr.X;
|
|
|
|
}
|
2009-03-30 18:13:11 -06:00
|
|
|
p.checkTypeName(base);
|
2009-03-28 00:11:54 -06:00
|
|
|
|
|
|
|
return recv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseFunctionDecl() *ast.FuncDecl {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "FunctionDecl"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
doc := p.getDoc();
|
|
|
|
pos := p.expect(token.FUNC);
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
var recv *ast.Field;
|
2009-03-26 23:16:06 -06:00
|
|
|
if p.tok == token.LPAREN {
|
2009-03-28 00:11:54 -06:00
|
|
|
recv = p.parseReceiver();
|
2008-09-19 13:12:28 -06:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
ident := p.parseIdent();
|
|
|
|
params, results := p.parseSignature();
|
2008-10-16 13:16:50 -06:00
|
|
|
|
2009-03-25 13:45:06 -06:00
|
|
|
var body *ast.BlockStmt;
|
2009-03-26 23:16:06 -06:00
|
|
|
if p.tok == token.LBRACE {
|
|
|
|
body = p.parseBlockStmt();
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-25 13:45:06 -06:00
|
|
|
return &ast.FuncDecl{doc, recv, ident, &ast.FunctionType{pos, params, results}, body};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
func (p *parser) parseDeclaration() ast.Decl {
|
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "Declaration"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
switch p.tok {
|
2009-03-03 19:25:07 -07:00
|
|
|
case token.CONST, token.TYPE, token.VAR:
|
2009-03-26 23:16:06 -06:00
|
|
|
return p.parseDecl(p.tok);
|
2009-03-03 19:25:07 -07:00
|
|
|
case token.FUNC:
|
2009-03-26 23:16:06 -06:00
|
|
|
return p.parseFunctionDecl();
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2009-03-11 17:06:17 -06:00
|
|
|
|
2009-03-26 23:16:06 -06:00
|
|
|
pos := p.pos;
|
2009-03-27 20:27:09 -06:00
|
|
|
p.error_expected(pos, "declaration");
|
2009-03-26 23:16:06 -06:00
|
|
|
p.next(); // make progress
|
|
|
|
return &ast.BadDecl{pos};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2009-03-26 17:10:07 -06:00
|
|
|
// Packages
|
2008-09-18 17:58:37 -06:00
|
|
|
|
2009-03-27 20:27:09 -06:00
|
|
|
// A set of flags (or 0) must be provided via the mode parameter to
|
|
|
|
// the Parse function. They control the amount of source code parsed
|
|
|
|
// and other optional parser functionality.
|
|
|
|
//
|
2009-03-12 18:24:03 -06:00
|
|
|
const (
|
2009-03-27 20:27:09 -06:00
|
|
|
PackageClauseOnly = 1 << iota; // parsing stops after package clause
|
|
|
|
ImportsOnly; // parsing stops after import declarations
|
|
|
|
Trace; // print a trace of parsed productions
|
2009-03-12 18:24:03 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2009-03-27 20:27:09 -06:00
|
|
|
func (p *parser) parsePackage() *ast.Package {
|
2009-03-26 23:16:06 -06:00
|
|
|
if p.trace {
|
|
|
|
defer un(trace(p, "Program"));
|
2009-03-05 18:15:36 -07:00
|
|
|
}
|
|
|
|
|
2009-03-12 18:24:03 -06:00
|
|
|
// package clause
|
2009-03-26 23:16:06 -06:00
|
|
|
comment := p.getDoc();
|
|
|
|
pos := p.expect(token.PACKAGE);
|
2009-03-27 20:27:09 -06:00
|
|
|
ident := p.parseIdent();
|
2009-03-26 23:16:06 -06:00
|
|
|
if p.tok == token.SEMICOLON {
|
2009-03-13 17:59:51 -06:00
|
|
|
// common error
|
2009-03-26 23:16:06 -06:00
|
|
|
p.error(p.pos, "extra semicolon");
|
|
|
|
p.next();
|
2009-03-13 17:59:51 -06:00
|
|
|
}
|
2009-03-27 20:27:09 -06:00
|
|
|
|
2009-03-12 18:24:03 -06:00
|
|
|
var decls []ast.Decl;
|
2009-03-27 20:27:09 -06:00
|
|
|
if p.mode & PackageClauseOnly == 0 {
|
2009-03-12 18:24:03 -06:00
|
|
|
// import decls
|
|
|
|
list := vector.New(0);
|
2009-03-26 23:16:06 -06:00
|
|
|
for p.tok == token.IMPORT {
|
|
|
|
list.Push(p.parseDecl(token.IMPORT));
|
|
|
|
if p.tok == token.SEMICOLON {
|
|
|
|
p.next();
|
2009-03-12 18:24:03 -06:00
|
|
|
}
|
2009-03-10 19:20:08 -06:00
|
|
|
}
|
2009-02-27 16:40:17 -07:00
|
|
|
|
2009-03-27 20:27:09 -06:00
|
|
|
if p.mode & ImportsOnly == 0 {
|
2009-03-12 18:24:03 -06:00
|
|
|
// rest of package body
|
2009-03-26 23:16:06 -06:00
|
|
|
for p.tok != token.EOF {
|
|
|
|
list.Push(p.parseDeclaration());
|
|
|
|
if p.tok == token.SEMICOLON {
|
|
|
|
p.next();
|
2009-03-12 18:24:03 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// convert declaration list
|
2009-03-12 18:24:03 -06:00
|
|
|
decls = make([]ast.Decl, list.Len());
|
|
|
|
for i := 0; i < list.Len(); i++ {
|
|
|
|
decls[i] = list.At(i).(ast.Decl);
|
|
|
|
}
|
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-20 18:18:48 -06:00
|
|
|
// convert comments list
|
2009-03-26 23:16:06 -06:00
|
|
|
comments := make([]*ast.Comment, p.comments.Len());
|
|
|
|
for i := 0; i < p.comments.Len(); i++ {
|
|
|
|
comments[i] = p.comments.At(i).(*ast.Comment);
|
2009-03-20 18:18:48 -06:00
|
|
|
}
|
|
|
|
|
2009-03-27 20:27:09 -06:00
|
|
|
return &ast.Package{comment, pos, ident, decls, comments};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2009-03-26 17:10:07 -06:00
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Parsing of entire programs.
|
|
|
|
|
|
|
|
// Parse invokes the Go parser. It calls the scanner's Scan method repeatedly
|
2009-03-26 23:16:06 -06:00
|
|
|
// to obtain a token sequence which is parsed according to Go syntax. If an
|
|
|
|
// error handler is provided (err != nil), it is invoked for each syntax error
|
|
|
|
// encountered.
|
2009-03-26 17:10:07 -06:00
|
|
|
//
|
2009-03-26 23:16:06 -06:00
|
|
|
// Parse returns an AST and the number of syntax errors encountered. If the
|
|
|
|
// error count is 0, the result is the correct AST for the token sequence
|
|
|
|
// returned by the scanner (*). If the error count is > 0, the AST may only
|
|
|
|
// be constructed partially, with ast.BadX nodes representing the fragments
|
|
|
|
// of source code that contained syntax errors.
|
2009-03-26 17:10:07 -06:00
|
|
|
//
|
2009-03-27 20:27:09 -06:00
|
|
|
// The mode parameter controls the amount of source text parsed and other
|
|
|
|
// optional parser functionality.
|
2009-03-26 17:10:07 -06:00
|
|
|
//
|
2009-03-26 23:16:06 -06:00
|
|
|
// (*) Note that a scanner may find lexical syntax errors but still return
|
|
|
|
// a legal token sequence. To be sure there are no syntax errors in the
|
|
|
|
// source (and not just the token sequence corresponding to the source)
|
|
|
|
// both the parser and scanner error count must be 0.
|
|
|
|
//
|
2009-03-27 20:27:09 -06:00
|
|
|
func Parse(scanner Scanner, err ErrorHandler, mode uint) (*ast.Package, int) {
|
2009-03-26 17:10:07 -06:00
|
|
|
// initialize parser state
|
|
|
|
var p parser;
|
|
|
|
p.scanner = scanner;
|
|
|
|
p.err = err;
|
2009-03-27 20:27:09 -06:00
|
|
|
p.mode = mode;
|
|
|
|
p.trace = mode & Trace != 0; // for convenience (p.trace is used frequently)
|
2009-03-26 17:10:07 -06:00
|
|
|
p.comments.Init(0);
|
|
|
|
p.next();
|
|
|
|
|
|
|
|
// parse program
|
2009-03-27 20:27:09 -06:00
|
|
|
pak := p.parsePackage();
|
|
|
|
return pak, p.errorCount;
|
2009-03-26 17:10:07 -06:00
|
|
|
}
|