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
|
|
|
|
// tree (AST) representing the Go source.
|
|
|
|
//
|
|
|
|
// A client may parse the entire program (ParseProgram), only the package
|
|
|
|
// clause (ParsePackageClause), or the package clause and the import
|
2009-03-10 19:20:08 -06:00
|
|
|
// declarations (ParseImportDecls).
|
2009-03-10 17:31:19 -06:00
|
|
|
//
|
2008-09-18 17:58:37 -06:00
|
|
|
package Parser
|
|
|
|
|
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-11 13:52:11 -06:00
|
|
|
"scanner";
|
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-10 17:31:19 -06:00
|
|
|
// A Parser holds the parser's internal state while processing
|
|
|
|
// a given text. It can be allocated as part of another data
|
|
|
|
// structure but must be initialized via Init before use.
|
|
|
|
//
|
2009-01-20 15:40:40 -07:00
|
|
|
type Parser struct {
|
2009-03-11 13:52:11 -06:00
|
|
|
scanner *scanner.Scanner;
|
|
|
|
err scanner.ErrorHandler;
|
2009-03-02 21:27:09 -07:00
|
|
|
|
2008-10-18 17:42:25 -06:00
|
|
|
// Tracing/debugging
|
2009-03-05 18:15:36 -07:00
|
|
|
trace bool;
|
2008-09-18 17:58:37 -06:00
|
|
|
indent uint;
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-12 18:24:03 -06:00
|
|
|
comments vector.Vector;
|
|
|
|
last_comment ast.CommentGroup;
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-03 19:25:07 -07:00
|
|
|
// The next token
|
2009-03-11 13:52:11 -06:00
|
|
|
loc scanner.Location; // token location
|
2008-09-30 19:50:29 -06:00
|
|
|
tok int; // one token look-ahead
|
2009-03-05 18:15:36 -07:00
|
|
|
val []byte; // token value
|
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-11 13:52:11 -06:00
|
|
|
// When we don't have a location use noloc.
|
|
|
|
// TODO make sure we always have a location.
|
|
|
|
var noloc scanner.Location;
|
|
|
|
|
|
|
|
|
2008-09-18 17:58:37 -06:00
|
|
|
// ----------------------------------------------------------------------------
|
2009-03-05 18:15:36 -07:00
|
|
|
// Helper functions
|
2009-01-08 13:04:00 -07:00
|
|
|
|
|
|
|
func unreachable() {
|
|
|
|
panic("unreachable");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Parsing support
|
2008-09-18 17:58:37 -06:00
|
|
|
|
2009-02-05 15:22:09 -07:00
|
|
|
func (P *Parser) printIndent() {
|
|
|
|
i := P.indent;
|
2009-02-06 12:10:25 -07:00
|
|
|
// reduce printing time by a factor of 2 or more
|
2009-02-05 15:22:09 -07:00
|
|
|
for ; i > 10; i -= 10 {
|
|
|
|
fmt.Printf(". . . . . . . . . . ");
|
|
|
|
}
|
|
|
|
for ; i > 0; i-- {
|
2009-02-03 18:44:01 -07:00
|
|
|
fmt.Printf(". ");
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-05 15:22:09 -07:00
|
|
|
func trace(P *Parser, msg string) *Parser {
|
|
|
|
P.printIndent();
|
|
|
|
fmt.Printf("%s (\n", msg);
|
2009-02-03 18:44:01 -07:00
|
|
|
P.indent++;
|
2009-02-05 15:22:09 -07:00
|
|
|
return P;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-05 15:22:09 -07:00
|
|
|
func un/*trace*/(P *Parser) {
|
2009-02-03 18:44:01 -07:00
|
|
|
P.indent--;
|
2009-02-05 15:22:09 -07:00
|
|
|
P.printIndent();
|
|
|
|
fmt.Printf(")\n");
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-05 15:22:09 -07:00
|
|
|
func (P *Parser) next0() {
|
2009-03-11 13:52:11 -06:00
|
|
|
P.loc, P.tok, P.val = P.scanner.Scan();
|
2008-10-10 17:03:32 -06:00
|
|
|
P.opt_semi = false;
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-02-05 15:22:09 -07:00
|
|
|
if P.trace {
|
|
|
|
P.printIndent();
|
|
|
|
switch P.tok {
|
2009-03-03 19:25:07 -07:00
|
|
|
case token.IDENT, token.INT, token.FLOAT, token.CHAR, token.STRING:
|
2009-03-11 13:52:11 -06:00
|
|
|
fmt.Printf("%d:%d: %s = %s\n", P.loc.Line, P.loc.Col, token.TokenString(P.tok), P.val);
|
2009-03-03 19:25:07 -07:00
|
|
|
case token.LPAREN:
|
2009-02-05 15:22:09 -07:00
|
|
|
// don't print '(' - screws up selection in terminal window
|
2009-03-11 13:52:11 -06:00
|
|
|
fmt.Printf("%d:%d: LPAREN\n", P.loc.Line, P.loc.Col);
|
2009-03-03 19:25:07 -07:00
|
|
|
case token.RPAREN:
|
2009-02-05 15:22:09 -07:00
|
|
|
// don't print ')' - screws up selection in terminal window
|
2009-03-11 13:52:11 -06:00
|
|
|
fmt.Printf("%d:%d: RPAREN\n", P.loc.Line, P.loc.Col);
|
2009-02-05 15:22:09 -07:00
|
|
|
default:
|
2009-03-11 13:52:11 -06:00
|
|
|
fmt.Printf("%d:%d: %s\n", P.loc.Line, P.loc.Col, token.TokenString(P.tok));
|
2009-01-23 10:44:01 -07:00
|
|
|
}
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-12 18:24:03 -06:00
|
|
|
func (P *Parser) getComment() *ast.Comment {
|
|
|
|
defer P.next0();
|
|
|
|
|
|
|
|
// for /*-style comments, the comment may end on a different line
|
|
|
|
endline := P.loc.Line;
|
|
|
|
if P.val[1] == '*' {
|
|
|
|
for i, b := range P.val {
|
|
|
|
if b == '\n' {
|
|
|
|
endline++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ast.Comment{P.loc, endline, P.val};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (P *Parser) getCommentGroup() ast.CommentGroup {
|
|
|
|
list := vector.New(0);
|
|
|
|
|
|
|
|
// group adjacent comments
|
|
|
|
// (an empty line terminates a group)
|
|
|
|
endline := P.loc.Line;
|
|
|
|
for P.tok == token.COMMENT && endline+1 >= P.loc.Line {
|
|
|
|
c := P.getComment();
|
|
|
|
list.Push(c);
|
|
|
|
endline = c.EndLine;
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert list
|
|
|
|
group := make(ast.CommentGroup, list.Len());
|
|
|
|
for i := 0; i < list.Len(); i++ {
|
|
|
|
group[i] = list.At(i).(*ast.Comment);
|
|
|
|
}
|
|
|
|
|
|
|
|
return group;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (P *Parser) getLastComment() ast.CommentGroup {
|
|
|
|
c := P.last_comment;
|
|
|
|
if c != nil && c[len(c) - 1].EndLine + 1 < P.loc.Line {
|
|
|
|
// empty line between last comment and current token,
|
|
|
|
// at least one line of space between last comment
|
|
|
|
// and current token; ignore this comment
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-05 15:22:09 -07:00
|
|
|
func (P *Parser) next() {
|
2009-03-12 18:24:03 -06:00
|
|
|
P.next0();
|
|
|
|
P.last_comment = nil;
|
|
|
|
for P.tok == token.COMMENT {
|
|
|
|
P.last_comment = P.getCommentGroup();
|
|
|
|
P.comments.Push(P.last_comment);
|
2008-10-17 00:30:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-11 13:52:11 -06:00
|
|
|
func (P *Parser) Init(scanner *scanner.Scanner, err scanner.ErrorHandler, trace bool) {
|
2009-03-02 21:27:09 -07:00
|
|
|
P.scanner = scanner;
|
|
|
|
P.err = err;
|
2009-02-05 15:22:09 -07:00
|
|
|
P.trace = trace;
|
2009-03-12 18:24:03 -06:00
|
|
|
P.comments.Init(0);
|
2009-02-05 15:22:09 -07:00
|
|
|
P.next();
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-11 13:52:11 -06:00
|
|
|
func (P *Parser) error(loc scanner.Location, msg string) {
|
|
|
|
P.err.Error(loc, msg);
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-05 15:22:09 -07:00
|
|
|
func (P *Parser) expect(tok int) {
|
2008-09-18 17:58:37 -06:00
|
|
|
if P.tok != tok {
|
2009-03-03 19:25:07 -07:00
|
|
|
msg := "expected '" + token.TokenString(tok) + "', found '" + token.TokenString(P.tok) + "'";
|
|
|
|
if token.IsLiteral(P.tok) {
|
2009-03-05 18:15:36 -07:00
|
|
|
msg += " " + string(P.val);
|
2008-10-18 17:42:25 -06:00
|
|
|
}
|
2009-03-11 13:52:11 -06:00
|
|
|
P.error(P.loc, msg);
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2009-02-05 15:22:09 -07:00
|
|
|
P.next(); // make progress in any case
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Common productions
|
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
func (P *Parser) tryType() ast.Expr;
|
|
|
|
func (P *Parser) parseExpression(prec int) ast.Expr;
|
|
|
|
func (P *Parser) parseStatement() ast.Stat;
|
|
|
|
func (P *Parser) parseDeclaration() ast.Decl;
|
2008-09-18 17:58:37 -06:00
|
|
|
|
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
func (P *Parser) parseIdent() *ast.Ident {
|
2009-02-05 15:22:09 -07:00
|
|
|
if P.trace {
|
|
|
|
defer un(trace(P, "Ident"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2009-01-20 15:40:40 -07:00
|
|
|
|
2009-03-03 19:25:07 -07:00
|
|
|
if P.tok == token.IDENT {
|
2009-03-11 13:52:11 -06:00
|
|
|
x := &ast.Ident{P.loc, string(P.val)};
|
2009-02-05 15:22:09 -07:00
|
|
|
P.next();
|
2009-02-03 18:44:01 -07:00
|
|
|
return x;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2009-02-13 15:48:32 -07:00
|
|
|
|
2009-03-03 19:25:07 -07:00
|
|
|
P.expect(token.IDENT); // use expect() error handling
|
2009-03-11 13:52:11 -06:00
|
|
|
return &ast.Ident{P.loc, ""};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-16 21:29:31 -06:00
|
|
|
func (P *Parser) parseIdentList(x ast.Expr) []*ast.Ident {
|
2009-02-05 15:22:09 -07:00
|
|
|
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-05 18:15:36 -07: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-03 19:25:07 -07:00
|
|
|
for P.tok == token.COMMA {
|
2009-02-05 15:22:09 -07:00
|
|
|
P.next();
|
2009-03-16 21:29:31 -06:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
return idents;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-16 21:29:31 -06:00
|
|
|
func (P *Parser) parseExpressionList() []ast.Expr {
|
2009-02-27 16:40:17 -07:00
|
|
|
if P.trace {
|
2009-03-16 21:29:31 -06:00
|
|
|
defer un(trace(P, "ExpressionList"));
|
2009-02-27 16:40:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
list := vector.New(0);
|
2009-03-16 21:29:31 -06:00
|
|
|
list.Push(P.parseExpression(1)); // TODO should use a const instead of 1
|
2009-03-03 19:25:07 -07:00
|
|
|
for P.tok == token.COMMA {
|
2009-02-27 16:40:17 -07:00
|
|
|
P.next();
|
2009-03-16 21:29:31 -06:00
|
|
|
list.Push(P.parseExpression(1)); // TODO should use a const instead of 1
|
2009-02-27 16:40:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// convert vector
|
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-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-05 18:15:36 -07:00
|
|
|
func (P *Parser) parseType() ast.Expr {
|
2009-02-05 15:22:09 -07:00
|
|
|
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-02-05 15:22:09 -07:00
|
|
|
t := P.tryType();
|
2008-10-17 17:19:31 -06:00
|
|
|
if t == nil {
|
2009-03-11 13:52:11 -06:00
|
|
|
P.error(P.loc, "type expected");
|
|
|
|
t = &ast.BadExpr{P.loc};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2008-10-17 17:19:31 -06:00
|
|
|
return t;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
func (P *Parser) parseVarType() ast.Expr {
|
2009-02-05 15:22:09 -07:00
|
|
|
if P.trace {
|
|
|
|
defer un(trace(P, "VarType"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-02-05 15:22:09 -07:00
|
|
|
return P.parseType();
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
func (P *Parser) parseQualifiedIdent() ast.Expr {
|
2009-02-05 15:22:09 -07:00
|
|
|
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-05 18:15:36 -07:00
|
|
|
var x ast.Expr = P.parseIdent();
|
2009-03-03 19:25:07 -07:00
|
|
|
for P.tok == token.PERIOD {
|
2009-03-11 13:52:11 -06:00
|
|
|
loc := P.loc;
|
2009-02-05 15:22:09 -07:00
|
|
|
P.next();
|
2009-03-05 18:15:36 -07:00
|
|
|
y := P.parseIdent();
|
2009-03-11 13:52:11 -06:00
|
|
|
x = &ast.Selector{loc, x, y};
|
2008-10-14 19:14:01 -06:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2008-10-14 19:14:01 -06:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
func (P *Parser) parseTypeName() ast.Expr {
|
2009-02-05 15:22:09 -07:00
|
|
|
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-02-27 16:40:17 -07:00
|
|
|
return P.parseQualifiedIdent();
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
func (P *Parser) parseArrayType() *ast.ArrayType {
|
2009-02-05 15:22:09 -07:00
|
|
|
if P.trace {
|
|
|
|
defer un(trace(P, "ArrayType"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-11 13:52:11 -06:00
|
|
|
loc := P.loc;
|
2009-03-03 19:25:07 -07:00
|
|
|
P.expect(token.LBRACK);
|
2009-03-05 18:15:36 -07:00
|
|
|
var len ast.Expr;
|
2009-03-03 19:25:07 -07:00
|
|
|
if P.tok == token.ELLIPSIS {
|
2009-03-11 13:52:11 -06:00
|
|
|
len = &ast.Ellipsis{P.loc};
|
2009-02-05 15:22:09 -07:00
|
|
|
P.next();
|
2009-03-03 19:25:07 -07:00
|
|
|
} else if P.tok != token.RBRACK {
|
2009-02-27 16:40:17 -07:00
|
|
|
len = P.parseExpression(1);
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2009-03-03 19:25:07 -07:00
|
|
|
P.expect(token.RBRACK);
|
2009-02-27 16:40:17 -07:00
|
|
|
elt := P.parseType();
|
2008-09-18 17:58:37 -06:00
|
|
|
|
2009-03-11 13:52:11 -06:00
|
|
|
return &ast.ArrayType{loc, len, elt};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
func (P *Parser) parseChannelType() *ast.ChannelType {
|
2009-02-05 15:22:09 -07:00
|
|
|
if P.trace {
|
|
|
|
defer un(trace(P, "ChannelType"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-11 13:52:11 -06:00
|
|
|
loc := P.loc;
|
2009-03-05 18:15:36 -07:00
|
|
|
mode := ast.FULL;
|
2009-03-03 19:25:07 -07:00
|
|
|
if P.tok == token.CHAN {
|
2009-02-05 15:22:09 -07:00
|
|
|
P.next();
|
2009-03-03 19:25:07 -07:00
|
|
|
if P.tok == token.ARROW {
|
2009-02-05 15:22:09 -07:00
|
|
|
P.next();
|
2009-03-05 18:15:36 -07:00
|
|
|
mode = ast.SEND;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
} else {
|
2009-03-03 19:25:07 -07:00
|
|
|
P.expect(token.ARROW);
|
|
|
|
P.expect(token.CHAN);
|
2009-03-05 18:15:36 -07:00
|
|
|
mode = ast.RECV;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2009-02-27 16:40:17 -07:00
|
|
|
val := P.parseVarType();
|
2008-09-18 17:58:37 -06:00
|
|
|
|
2009-03-11 13:52:11 -06:00
|
|
|
return &ast.ChannelType{loc, mode, val};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
func (P *Parser) tryParameterType() ast.Expr {
|
2009-03-03 19:25:07 -07:00
|
|
|
if P.tok == token.ELLIPSIS {
|
2009-03-11 13:52:11 -06:00
|
|
|
loc := P.loc;
|
2009-02-05 15:22:09 -07:00
|
|
|
P.next();
|
2009-03-11 13:52:11 -06:00
|
|
|
return &ast.Ellipsis{loc};
|
2008-10-18 17:42:25 -06:00
|
|
|
}
|
2009-02-27 16:40:17 -07:00
|
|
|
return P.tryType();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
func (P *Parser) parseParameterType() ast.Expr {
|
2009-02-27 16:40:17 -07:00
|
|
|
typ := P.tryParameterType();
|
|
|
|
if typ == nil {
|
2009-03-11 13:52:11 -06:00
|
|
|
P.error(P.loc, "type expected");
|
|
|
|
typ = &ast.BadExpr{P.loc};
|
2009-02-27 16:40:17 -07:00
|
|
|
}
|
|
|
|
return typ;
|
2008-10-18 17:42:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
func (P *Parser) parseParameterDecl(ellipsis_ok bool) (*vector.Vector, ast.Expr) {
|
2009-02-05 15:22:09 -07:00
|
|
|
if P.trace {
|
2009-02-27 16:40:17 -07:00
|
|
|
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
|
|
|
|
list.Push(P.parseParameterType());
|
2009-03-03 19:25:07 -07:00
|
|
|
if P.tok == token.COMMA {
|
2009-02-05 15:22:09 -07:00
|
|
|
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-02-27 16:40:17 -07:00
|
|
|
typ := P.tryParameterType();
|
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-05 18:15:36 -07:00
|
|
|
func (P *Parser) parseParameterList(ellipsis_ok bool) []*ast.Field {
|
2009-02-27 16:40:17 -07:00
|
|
|
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-02-27 16:40:17 -07:00
|
|
|
list, typ := P.parseParameterDecl(false);
|
2008-10-15 12:48:18 -06:00
|
|
|
if typ != nil {
|
2009-02-27 16:40:17 -07:00
|
|
|
// IdentifierList Type
|
|
|
|
// convert list of identifiers into []*Ident
|
2009-03-05 18:15:36 -07:00
|
|
|
idents := make([]*ast.Ident, 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
|
|
|
idents[i] = list.At(i).(*ast.Ident);
|
2009-02-27 16:40:17 -07:00
|
|
|
}
|
|
|
|
list.Init(0);
|
2009-03-12 18:24:03 -06:00
|
|
|
list.Push(&ast.Field{idents, typ, nil, nil});
|
2009-03-11 17:06:17 -06:00
|
|
|
|
2009-03-03 19:25:07 -07:00
|
|
|
for P.tok == token.COMMA {
|
2009-02-27 16:40:17 -07:00
|
|
|
P.next();
|
2009-03-16 21:29:31 -06:00
|
|
|
idents := P.parseIdentList(nil);
|
2009-02-27 16:40:17 -07:00
|
|
|
typ := P.parseParameterType();
|
2009-03-12 18:24:03 -06:00
|
|
|
list.Push(&ast.Field{idents, typ, nil, 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-02-27 16:40:17 -07:00
|
|
|
// Type { "," Type }
|
|
|
|
// convert list of types into list of *Param
|
|
|
|
for i := 0; i < list.Len(); i++ {
|
2009-03-12 18:24:03 -06:00
|
|
|
list.Set(i, &ast.Field{nil, list.At(i).(ast.Expr), nil, 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-02-27 16:40:17 -07:00
|
|
|
// TODO make sure Go spec is updated
|
2009-03-05 18:15:36 -07:00
|
|
|
func (P *Parser) parseParameters(ellipsis_ok bool) []*ast.Field {
|
2009-02-05 15:22:09 -07:00
|
|
|
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-03 19:25:07 -07:00
|
|
|
P.expect(token.LPAREN);
|
|
|
|
if P.tok != token.RPAREN {
|
2009-02-27 16:40:17 -07:00
|
|
|
params = P.parseParameterList(ellipsis_ok);
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2009-03-03 19:25:07 -07: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-05 18:15:36 -07:00
|
|
|
func (P *Parser) parseResult() []*ast.Field {
|
2009-02-05 15:22:09 -07:00
|
|
|
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-05 18:15:36 -07:00
|
|
|
var result []*ast.Field;
|
2009-03-03 19:25:07 -07:00
|
|
|
if P.tok == token.LPAREN {
|
2009-02-27 16:40:17 -07:00
|
|
|
result = P.parseParameters(false);
|
2009-03-03 19:25:07 -07:00
|
|
|
} else if P.tok != token.FUNC {
|
2009-02-05 15:22:09 -07:00
|
|
|
typ := P.tryType();
|
2008-10-15 18:06:28 -06:00
|
|
|
if typ != nil {
|
2009-03-05 18:15:36 -07:00
|
|
|
result = make([]*ast.Field, 1);
|
2009-03-12 18:24:03 -06:00
|
|
|
result[0] = &ast.Field{nil, typ, nil, nil};
|
2008-10-15 18:06:28 -06:00
|
|
|
}
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
2009-02-27 16:40:17 -07:00
|
|
|
return result;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-19 13:12:28 -06:00
|
|
|
// Function types
|
2008-09-18 17:58:37 -06:00
|
|
|
//
|
|
|
|
// (params)
|
|
|
|
// (params) type
|
|
|
|
// (params) (results)
|
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
func (P *Parser) parseSignature() *ast.Signature {
|
2009-02-05 15:22:09 -07:00
|
|
|
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-02-27 16:40:17 -07:00
|
|
|
params := P.parseParameters(true); // TODO find better solution
|
2009-03-11 13:52:11 -06:00
|
|
|
//t.End = P.loc;
|
2009-02-27 16:40:17 -07:00
|
|
|
result := P.parseResult();
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
return &ast.Signature{params, result};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
func (P *Parser) parseFunctionType() *ast.FunctionType {
|
2009-02-05 15:22:09 -07:00
|
|
|
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-11 13:52:11 -06:00
|
|
|
loc := P.loc;
|
2009-03-03 19:25:07 -07:00
|
|
|
P.expect(token.FUNC);
|
2009-02-27 16:40:17 -07:00
|
|
|
sig := P.parseSignature();
|
2009-03-11 17:06:17 -06:00
|
|
|
|
2009-03-11 13:52:11 -06:00
|
|
|
return &ast.FunctionType{loc, sig};
|
2009-01-30 16:31:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
func (P *Parser) parseMethodSpec() *ast.Field {
|
2009-02-05 15:22:09 -07:00
|
|
|
if P.trace {
|
2009-02-27 16:40:17 -07:00
|
|
|
defer un(trace(P, "MethodSpec"));
|
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 idents []*ast.Ident;
|
|
|
|
var typ ast.Expr;
|
2009-02-19 17:47:58 -07:00
|
|
|
x := P.parseQualifiedIdent();
|
2009-03-05 18:15:36 -07:00
|
|
|
if tmp, is_ident := x.(*ast.Ident); is_ident && (P.tok == token.COMMA || P.tok == token.LPAREN) {
|
2009-02-19 17:47:58 -07:00
|
|
|
// method(s)
|
2009-03-16 21:29:31 -06:00
|
|
|
idents = P.parseIdentList(x);
|
2009-03-11 13:52:11 -06:00
|
|
|
typ = &ast.FunctionType{noloc, P.parseSignature()};
|
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-12 18:24:03 -06:00
|
|
|
return &ast.Field{idents, typ, nil, nil};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
func (P *Parser) parseInterfaceType() *ast.InterfaceType {
|
2009-02-05 15:22:09 -07:00
|
|
|
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-11 13:52:11 -06:00
|
|
|
loc := P.loc;
|
|
|
|
var end scanner.Location;
|
2009-03-05 18:15:36 -07:00
|
|
|
var methods []*ast.Field;
|
2009-02-27 16:40:17 -07:00
|
|
|
|
2009-03-03 19:25:07 -07:00
|
|
|
P.expect(token.INTERFACE);
|
|
|
|
if P.tok == token.LBRACE {
|
2009-02-05 15:22:09 -07:00
|
|
|
P.next();
|
2009-01-08 13:04:00 -07:00
|
|
|
|
2009-02-27 16:40:17 -07:00
|
|
|
list := vector.New(0);
|
2009-03-03 19:25:07 -07:00
|
|
|
for P.tok == token.IDENT {
|
2009-02-27 16:40:17 -07:00
|
|
|
list.Push(P.parseMethodSpec());
|
2009-03-03 19:25:07 -07:00
|
|
|
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-11 13:52:11 -06:00
|
|
|
end = P.loc;
|
2009-03-03 19:25:07 -07:00
|
|
|
P.expect(token.RBRACE);
|
2009-02-27 16:40:17 -07:00
|
|
|
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-11 13:52:11 -06:00
|
|
|
return &ast.InterfaceType{loc, methods, end};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
func (P *Parser) parseMapType() *ast.MapType {
|
2009-02-05 15:22:09 -07:00
|
|
|
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-11 13:52:11 -06:00
|
|
|
loc := P.loc;
|
2009-03-03 19:25:07 -07:00
|
|
|
P.expect(token.MAP);
|
|
|
|
P.expect(token.LBRACK);
|
2009-02-27 16:40:17 -07:00
|
|
|
key := P.parseVarType();
|
2009-03-03 19:25:07 -07:00
|
|
|
P.expect(token.RBRACK);
|
2009-02-27 16:40:17 -07:00
|
|
|
val := P.parseVarType();
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-11 13:52:11 -06:00
|
|
|
return &ast.MapType{loc, key, val};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
func (P *Parser) parseStringLit() ast.Expr
|
2008-10-31 15:27:34 -06:00
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
func (P *Parser) parseFieldDecl() *ast.Field {
|
2009-02-27 16:40:17 -07:00
|
|
|
if P.trace {
|
|
|
|
defer un(trace(P, "FieldDecl"));
|
|
|
|
}
|
|
|
|
|
2009-03-12 18:24:03 -06:00
|
|
|
comment := P.getLastComment();
|
|
|
|
|
2009-02-27 16:40:17 -07:00
|
|
|
// 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());
|
2009-03-03 19:25:07 -07:00
|
|
|
if P.tok == token.COMMA {
|
2009-02-27 16:40:17 -07:00
|
|
|
P.next();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we had a list of identifiers, it must be followed by a type
|
|
|
|
typ := P.tryType();
|
|
|
|
|
|
|
|
// optional tag
|
2009-03-05 18:15:36 -07:00
|
|
|
var tag ast.Expr;
|
2009-03-03 19:25:07 -07:00
|
|
|
if P.tok == token.STRING {
|
2009-03-05 18:15:36 -07:00
|
|
|
tag = P.parseStringLit();
|
2009-02-27 16:40:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// analyze case
|
2009-03-05 18:15:36 -07:00
|
|
|
var idents []*ast.Ident;
|
2009-02-27 16:40:17 -07:00
|
|
|
if typ != nil {
|
|
|
|
// non-empty identifier list followed by a type
|
2009-03-05 18:15:36 -07:00
|
|
|
idents = make([]*ast.Ident, 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
|
|
|
if ident, is_ident := list.At(i).(*ast.Ident); is_ident {
|
2009-02-27 16:40:17 -07:00
|
|
|
idents[i] = ident;
|
|
|
|
} else {
|
2009-03-11 13:52:11 -06:00
|
|
|
P.error(list.At(i).(ast.Expr).Loc(), "identifier expected");
|
2009-02-27 16:40:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// anonymous field
|
|
|
|
if list.Len() == 1 {
|
|
|
|
// TODO should do more checks here
|
2009-03-05 18:15:36 -07:00
|
|
|
typ = list.At(0).(ast.Expr);
|
2009-02-27 16:40:17 -07:00
|
|
|
} else {
|
2009-03-11 13:52:11 -06:00
|
|
|
P.error(P.loc, "anonymous field expected");
|
2009-02-27 16:40:17 -07:00
|
|
|
}
|
|
|
|
}
|
2009-03-11 17:06:17 -06:00
|
|
|
|
2009-03-12 18:24:03 -06:00
|
|
|
return &ast.Field{idents, typ, tag, comment};
|
2009-02-27 16:40:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
func (P *Parser) parseStructType() ast.Expr {
|
2009-02-05 15:22:09 -07:00
|
|
|
if P.trace {
|
|
|
|
defer un(trace(P, "StructType"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-09-25 12:50:34 -06:00
|
|
|
|
2009-03-11 13:52:11 -06:00
|
|
|
loc := P.loc;
|
|
|
|
var end scanner.Location;
|
2009-03-05 18:15:36 -07:00
|
|
|
var fields []*ast.Field;
|
2009-03-11 17:06:17 -06:00
|
|
|
|
2009-03-03 19:25:07 -07:00
|
|
|
P.expect(token.STRUCT);
|
|
|
|
if P.tok == token.LBRACE {
|
2009-02-05 15:22:09 -07:00
|
|
|
P.next();
|
2009-01-08 13:04:00 -07:00
|
|
|
|
2009-02-27 16:40:17 -07:00
|
|
|
list := vector.New(0);
|
2009-03-03 19:25:07 -07:00
|
|
|
for P.tok != token.RBRACE && P.tok != token.EOF {
|
2009-02-27 16:40:17 -07:00
|
|
|
list.Push(P.parseFieldDecl());
|
2009-03-03 19:25:07 -07:00
|
|
|
if P.tok == token.SEMICOLON {
|
2009-02-05 15:22:09 -07:00
|
|
|
P.next();
|
2008-10-31 15:27:34 -06:00
|
|
|
} else {
|
|
|
|
break;
|
2008-10-07 18:57:19 -06:00
|
|
|
}
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2009-03-10 19:20:08 -06:00
|
|
|
if P.tok == token.SEMICOLON {
|
|
|
|
P.next();
|
|
|
|
}
|
2009-01-08 13:04:00 -07:00
|
|
|
|
2009-03-11 13:52:11 -06:00
|
|
|
end = P.loc;
|
2009-03-03 19:25:07 -07:00
|
|
|
P.expect(token.RBRACE);
|
2009-02-27 16:40:17 -07:00
|
|
|
P.opt_semi = true;
|
2009-02-13 15:48:32 -07:00
|
|
|
|
2009-02-27 16:40:17 -07:00
|
|
|
// convert vector
|
2009-03-05 18:15:36 -07:00
|
|
|
fields = 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
|
|
|
fields[i] = list.At(i).(*ast.Field);
|
2009-01-23 10:44:01 -07:00
|
|
|
}
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2008-10-07 18:57:19 -06:00
|
|
|
|
2009-03-11 17:06:17 -06:00
|
|
|
return &ast.StructType{loc, fields, end};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
func (P *Parser) parsePointerType() ast.Expr {
|
2009-02-05 15:22:09 -07:00
|
|
|
if P.trace {
|
|
|
|
defer un(trace(P, "PointerType"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-11 13:52:11 -06:00
|
|
|
loc := P.loc;
|
2009-03-03 19:25:07 -07:00
|
|
|
P.expect(token.MUL);
|
2009-02-27 16:40:17 -07:00
|
|
|
base := P.parseType();
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-11 13:52:11 -06:00
|
|
|
return &ast.PointerType{loc, base};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
func (P *Parser) tryType() ast.Expr {
|
2009-02-05 15:22:09 -07:00
|
|
|
if P.trace {
|
|
|
|
defer un(trace(P, "Type (try)"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2008-09-18 17:58:37 -06:00
|
|
|
switch P.tok {
|
2009-03-03 19:25:07 -07:00
|
|
|
case token.IDENT: return P.parseTypeName();
|
|
|
|
case token.LBRACK: return P.parseArrayType();
|
|
|
|
case token.CHAN, token.ARROW: return P.parseChannelType();
|
|
|
|
case token.INTERFACE: return P.parseInterfaceType();
|
|
|
|
case token.FUNC: return P.parseFunctionType();
|
|
|
|
case token.MAP: return P.parseMapType();
|
|
|
|
case token.STRUCT: return P.parseStructType();
|
|
|
|
case token.MUL: return P.parsePointerType();
|
|
|
|
case token.LPAREN:
|
2009-03-11 13:52:11 -06:00
|
|
|
loc := P.loc;
|
2009-02-19 17:47:58 -07:00
|
|
|
P.next();
|
|
|
|
t := P.parseType();
|
2009-03-03 19:25:07 -07:00
|
|
|
P.expect(token.RPAREN);
|
2009-03-11 13:52:11 -06:00
|
|
|
return &ast.Group{loc, t};
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Blocks
|
|
|
|
|
2009-02-04 19:28:41 -07:00
|
|
|
|
2009-02-13 16:07:56 -07:00
|
|
|
func (P *Parser) parseStatementList(list *vector.Vector) {
|
2009-02-05 15:22:09 -07:00
|
|
|
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-02-12 17:06:21 -07:00
|
|
|
expect_semi := false;
|
2009-03-03 19:25:07 -07: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-03 19:25:07 -07: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-02-12 17:06:21 -07:00
|
|
|
list.Push(P.parseStatement());
|
2009-03-03 19:25:07 -07:00
|
|
|
if P.tok == token.SEMICOLON {
|
2009-02-05 15:22:09 -07:00
|
|
|
P.next();
|
2008-10-10 17:03:32 -06:00
|
|
|
} 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-05 18:15:36 -07:00
|
|
|
func (P *Parser) parseBlock(tok int) *ast.Block {
|
2009-02-05 15:22:09 -07:00
|
|
|
if P.trace {
|
|
|
|
defer un(trace(P, "Block"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-11 13:52:11 -06:00
|
|
|
b := ast.NewBlock(P.loc, tok);
|
2009-02-05 15:22:09 -07:00
|
|
|
P.expect(tok);
|
2009-01-08 13:04:00 -07:00
|
|
|
|
2009-02-05 15:22:09 -07:00
|
|
|
P.parseStatementList(b.List);
|
2009-03-11 17:06:17 -06:00
|
|
|
|
2009-03-03 19:25:07 -07:00
|
|
|
if tok == token.LBRACE {
|
2009-03-11 13:52:11 -06:00
|
|
|
b.End = P.loc;
|
2009-03-03 19:25:07 -07:00
|
|
|
P.expect(token.RBRACE);
|
2009-01-23 10:44:01 -07:00
|
|
|
P.opt_semi = true;
|
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-01-23 10:44:01 -07:00
|
|
|
return b;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Expressions
|
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
func (P *Parser) parseFunctionLit() ast.Expr {
|
2009-02-05 15:22:09 -07:00
|
|
|
if P.trace {
|
|
|
|
defer un(trace(P, "FunctionLit"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-11 13:52:11 -06:00
|
|
|
loc := P.loc;
|
2009-03-03 19:25:07 -07:00
|
|
|
P.expect(token.FUNC);
|
2009-02-05 15:22:09 -07:00
|
|
|
typ := P.parseSignature();
|
2009-03-03 17:00:06 -07:00
|
|
|
P.expr_lev++;
|
2009-03-03 19:25:07 -07:00
|
|
|
body := P.parseBlock(token.LBRACE);
|
2009-03-03 17:00:06 -07:00
|
|
|
P.expr_lev--;
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-11 13:52:11 -06:00
|
|
|
return &ast.FunctionLit{loc, typ, body};
|
2009-03-05 18:15:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (P *Parser) parseStringLit() ast.Expr {
|
|
|
|
if P.trace {
|
|
|
|
defer un(trace(P, "StringLit"));
|
|
|
|
}
|
|
|
|
|
2009-03-11 13:52:11 -06:00
|
|
|
var x ast.Expr = &ast.BasicLit{P.loc, P.tok, P.val};
|
2009-03-10 19:20:08 -06:00
|
|
|
P.expect(token.STRING); // always satisfied
|
2009-03-11 17:06:17 -06:00
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
for P.tok == token.STRING {
|
2009-03-11 13:52:11 -06:00
|
|
|
y := &ast.BasicLit{P.loc, P.tok, P.val};
|
2009-03-05 18:15:36 -07:00
|
|
|
P.next();
|
|
|
|
x = &ast.ConcatExpr{x, y};
|
|
|
|
}
|
|
|
|
|
|
|
|
return x;
|
2008-10-23 18:56:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
func (P *Parser) parseOperand() ast.Expr {
|
2009-02-05 15:22:09 -07:00
|
|
|
if P.trace {
|
|
|
|
defer un(trace(P, "Operand"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-09-18 17:58:37 -06:00
|
|
|
|
2008-09-23 17:40:12 -06:00
|
|
|
switch P.tok {
|
2009-03-03 19:25:07 -07:00
|
|
|
case token.IDENT:
|
2009-03-05 18:15:36 -07:00
|
|
|
return P.parseIdent();
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
case token.INT, token.FLOAT, token.CHAR:
|
2009-03-11 13:52:11 -06:00
|
|
|
x := &ast.BasicLit{P.loc, P.tok, P.val};
|
2009-02-05 15:22:09 -07:00
|
|
|
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:
|
|
|
|
return P.parseStringLit();
|
2008-10-10 17:03:32 -06:00
|
|
|
|
2009-03-03 19:25:07 -07:00
|
|
|
case token.LPAREN:
|
2009-03-11 13:52:11 -06:00
|
|
|
loc := P.loc;
|
2009-03-03 19:25:07 -07:00
|
|
|
P.next();
|
|
|
|
P.expr_lev++;
|
|
|
|
x := P.parseExpression(1);
|
|
|
|
P.expr_lev--;
|
|
|
|
P.expect(token.RPAREN);
|
2009-03-11 13:52:11 -06:00
|
|
|
return &ast.Group{loc, x};
|
2009-03-03 19:25:07 -07:00
|
|
|
|
|
|
|
case token.FUNC:
|
2009-02-05 15:22:09 -07:00
|
|
|
return P.parseFunctionLit();
|
2008-10-23 18:56:54 -06:00
|
|
|
|
2008-09-23 17:40:12 -06:00
|
|
|
default:
|
2009-02-05 15:22:09 -07:00
|
|
|
t := P.tryType();
|
2008-10-15 18:06:28 -06:00
|
|
|
if t != nil {
|
2009-02-27 16:40:17 -07:00
|
|
|
return t;
|
2008-10-15 18:06:28 -06:00
|
|
|
} else {
|
2009-03-11 13:52:11 -06:00
|
|
|
P.error(P.loc, "operand expected");
|
2009-02-05 15:22:09 -07:00
|
|
|
P.next(); // make progress
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-11 13:52:11 -06:00
|
|
|
return &ast.BadExpr{P.loc};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
func (P *Parser) parseSelectorOrTypeGuard(x ast.Expr) ast.Expr {
|
2009-02-05 15:22:09 -07:00
|
|
|
if P.trace {
|
|
|
|
defer un(trace(P, "SelectorOrTypeGuard"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-09-18 17:58:37 -06:00
|
|
|
|
2009-03-11 13:52:11 -06:00
|
|
|
loc := P.loc;
|
2009-03-03 19:25:07 -07:00
|
|
|
P.expect(token.PERIOD);
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-03 19:25:07 -07:00
|
|
|
if P.tok == token.IDENT {
|
2009-03-11 13:52:11 -06:00
|
|
|
x = &ast.Selector{loc, x, P.parseIdent()};
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2008-09-18 17:58:37 -06:00
|
|
|
} else {
|
2009-03-03 19:25:07 -07:00
|
|
|
P.expect(token.LPAREN);
|
2009-03-05 18:15:36 -07:00
|
|
|
var typ ast.Expr;
|
|
|
|
if P.tok == token.TYPE {
|
2009-03-11 13:52:11 -06:00
|
|
|
typ = &ast.TypeType{P.loc};
|
2009-03-05 18:15:36 -07:00
|
|
|
P.next();
|
|
|
|
} else {
|
|
|
|
typ = P.parseType();
|
|
|
|
}
|
2009-03-11 13:52:11 -06:00
|
|
|
x = &ast.TypeGuard{loc, x, typ};
|
2009-03-03 19:25:07 -07:00
|
|
|
P.expect(token.RPAREN);
|
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-05 18:15:36 -07:00
|
|
|
func (P *Parser) parseIndex(x ast.Expr) ast.Expr {
|
2009-02-05 15:22:09 -07:00
|
|
|
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-11 13:52:11 -06:00
|
|
|
loc := P.loc;
|
2009-03-03 19:25:07 -07:00
|
|
|
P.expect(token.LBRACK);
|
2009-03-03 17:00:06 -07:00
|
|
|
P.expr_lev++;
|
2009-02-05 15:22:09 -07:00
|
|
|
i := P.parseExpression(0);
|
2009-03-03 17:00:06 -07:00
|
|
|
P.expr_lev--;
|
2009-03-03 19:25:07 -07:00
|
|
|
P.expect(token.RBRACK);
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-11 13:52:11 -06:00
|
|
|
return &ast.Index{loc, x, i};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
func (P *Parser) parseBinaryExpr(prec1 int) ast.Expr
|
2008-10-17 00:30:42 -06:00
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
func (P *Parser) parseCompositeElements(close int) ast.Expr {
|
2009-02-05 15:22:09 -07:00
|
|
|
x := P.parseExpression(0);
|
2009-03-03 19:25:07 -07:00
|
|
|
if P.tok == token.COMMA {
|
2009-03-11 13:52:11 -06:00
|
|
|
loc := P.loc;
|
2009-02-05 15:22:09 -07:00
|
|
|
P.next();
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
// first element determines mode
|
|
|
|
singles := true;
|
2009-03-05 18:15:36 -07:00
|
|
|
if t, is_binary := x.(*ast.BinaryExpr); is_binary && t.Tok == token.COLON {
|
2008-10-20 16:03:40 -06:00
|
|
|
singles = false;
|
2008-10-16 13:16:50 -06:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
var last *ast.BinaryExpr;
|
2009-03-03 19:25:07 -07:00
|
|
|
for P.tok != close && P.tok != token.EOF {
|
2009-02-05 15:22:09 -07:00
|
|
|
y := P.parseExpression(0);
|
2008-10-18 21:20:30 -06:00
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
if singles {
|
2009-03-05 18:15:36 -07:00
|
|
|
if t, is_binary := y.(*ast.BinaryExpr); is_binary && t.Tok == token.COLON {
|
2009-03-11 13:52:11 -06:00
|
|
|
P.error(t.X.Loc(), "single value expected; found pair");
|
2008-10-20 16:03:40 -06:00
|
|
|
}
|
|
|
|
} else {
|
2009-03-05 18:15:36 -07:00
|
|
|
if t, is_binary := y.(*ast.BinaryExpr); !is_binary || t.Tok != token.COLON {
|
2009-03-11 13:52:11 -06:00
|
|
|
P.error(y.Loc(), "key:value pair expected; found single value");
|
2008-10-20 16:03:40 -06:00
|
|
|
}
|
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-01-08 13:04:00 -07:00
|
|
|
if last == nil {
|
2009-03-11 13:52:11 -06:00
|
|
|
last = &ast.BinaryExpr{loc, token.COMMA, x, y};
|
2009-02-03 18:44:01 -07:00
|
|
|
x = last;
|
2008-10-20 16:03:40 -06:00
|
|
|
} else {
|
2009-03-11 13:52:11 -06:00
|
|
|
last.Y = &ast.BinaryExpr{loc, token.COMMA, last.Y, y};
|
2009-03-05 18:15:36 -07:00
|
|
|
last = last.Y.(*ast.BinaryExpr);
|
2008-10-20 16:03:40 -06:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-03 19:25:07 -07:00
|
|
|
if P.tok == token.COMMA {
|
2009-03-11 13:52:11 -06:00
|
|
|
loc = P.loc;
|
2009-02-05 15:22:09 -07:00
|
|
|
P.next();
|
2008-10-20 16:03:40 -06:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2008-10-16 13:16:50 -06:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
func (P *Parser) parseCallOrCompositeLit(f ast.Expr, open, close int) ast.Expr {
|
2009-02-05 15:22:09 -07:00
|
|
|
if P.trace {
|
2009-02-13 17:27:53 -07:00
|
|
|
defer un(trace(P, "CallOrCompositeLit"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-11 13:52:11 -06:00
|
|
|
loc := P.loc;
|
2009-03-03 17:00:06 -07:00
|
|
|
P.expect(open);
|
2009-03-05 18:15:36 -07:00
|
|
|
var args ast.Expr;
|
2009-03-03 17:00:06 -07:00
|
|
|
if P.tok != close {
|
|
|
|
args = P.parseCompositeElements(close);
|
2008-10-17 17:19:31 -06:00
|
|
|
}
|
2009-03-03 17:00:06 -07:00
|
|
|
P.expect(close);
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-11 13:52:11 -06:00
|
|
|
return &ast.Call{loc, open, f, args};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
func (P *Parser) parsePrimaryExpr() ast.Expr {
|
2009-02-05 15:22:09 -07:00
|
|
|
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-02-05 15:22:09 -07:00
|
|
|
x := P.parseOperand();
|
2008-10-09 19:03:16 -06:00
|
|
|
for {
|
2008-09-18 17:58:37 -06:00
|
|
|
switch P.tok {
|
2009-03-03 19:25:07 -07:00
|
|
|
case token.PERIOD: x = P.parseSelectorOrTypeGuard(x);
|
|
|
|
case token.LBRACK: x = P.parseIndex(x);
|
2009-03-03 17:00:06 -07:00
|
|
|
// TODO fix once we have decided on literal/conversion syntax
|
2009-03-03 19:25:07 -07:00
|
|
|
case token.LPAREN: x = P.parseCallOrCompositeLit(x, token.LPAREN, token.RPAREN);
|
|
|
|
case token.LBRACE:
|
2009-03-03 17:00:06 -07:00
|
|
|
if P.expr_lev >= 0 {
|
2009-03-03 19:25:07 -07:00
|
|
|
x = P.parseCallOrCompositeLit(x, token.LBRACE, token.RBRACE);
|
2009-03-03 17:00:06 -07:00
|
|
|
} else {
|
|
|
|
return x;
|
|
|
|
}
|
2009-02-03 18:44:01 -07:00
|
|
|
default:
|
|
|
|
return x;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-02-03 18:44:01 -07:00
|
|
|
unreachable();
|
|
|
|
return nil;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
func (P *Parser) parseUnaryExpr() ast.Expr {
|
2009-02-05 15:22:09 -07:00
|
|
|
if P.trace {
|
|
|
|
defer un(trace(P, "UnaryExpr"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2008-09-18 17:58:37 -06:00
|
|
|
switch P.tok {
|
2009-03-03 19:25:07 -07:00
|
|
|
case token.ADD, token.SUB, token.MUL, token.NOT, token.XOR, token.ARROW, token.AND:
|
2009-03-11 13:52:11 -06:00
|
|
|
loc, tok := P.loc, P.tok;
|
2009-02-05 15:22:09 -07:00
|
|
|
P.next();
|
|
|
|
y := P.parseUnaryExpr();
|
2009-03-11 13:52:11 -06:00
|
|
|
return &ast.UnaryExpr{loc, tok, y};
|
2009-02-27 16:40:17 -07:00
|
|
|
/*
|
2009-03-05 18:15:36 -07:00
|
|
|
if lit, ok := y.(*ast.TypeLit); ok && tok == token.MUL {
|
2008-10-17 00:30:42 -06:00
|
|
|
// pointer type
|
2009-03-05 18:15:36 -07:00
|
|
|
t := ast.NewType(pos, ast.POINTER);
|
2009-02-03 18:44:01 -07:00
|
|
|
t.Elt = lit.Typ;
|
2009-03-05 18:15:36 -07:00
|
|
|
return &ast.TypeLit{t};
|
2008-10-17 00:30:42 -06:00
|
|
|
} else {
|
2009-03-11 13:52:11 -06:00
|
|
|
return &ast.UnaryExpr{loc, tok, y};
|
2008-10-17 00:30:42 -06:00
|
|
|
}
|
2009-02-27 16:40:17 -07:00
|
|
|
*/
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-02-05 15:22:09 -07:00
|
|
|
return P.parsePrimaryExpr();
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
func (P *Parser) parseBinaryExpr(prec1 int) ast.Expr {
|
2009-02-05 15:22:09 -07:00
|
|
|
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-02-05 15:22:09 -07:00
|
|
|
x := P.parseUnaryExpr();
|
2009-03-03 19:25:07 -07:00
|
|
|
for prec := token.Precedence(P.tok); prec >= prec1; prec-- {
|
|
|
|
for token.Precedence(P.tok) == prec {
|
2009-03-11 13:52:11 -06:00
|
|
|
loc, tok := P.loc, P.tok;
|
2009-02-05 15:22:09 -07:00
|
|
|
P.next();
|
|
|
|
y := P.parseBinaryExpr(prec + 1);
|
2009-03-11 13:52:11 -06:00
|
|
|
x = &ast.BinaryExpr{loc, tok, x, 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-05 18:15:36 -07:00
|
|
|
func (P *Parser) parseExpression(prec int) ast.Expr {
|
2009-02-05 15:22:09 -07: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
|
|
|
|
|
|
|
if prec < 0 {
|
|
|
|
panic("precedence must be >= 0");
|
|
|
|
}
|
2009-02-03 18:44:01 -07:00
|
|
|
|
2009-02-13 17:27:53 -07:00
|
|
|
return P.parseBinaryExpr(prec);
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Statements
|
|
|
|
|
2009-03-16 21:29:31 -06:00
|
|
|
|
2009-03-13 17:59:51 -06:00
|
|
|
const /* mode */ (
|
|
|
|
label_ok = 1 << iota;
|
|
|
|
range_ok;
|
|
|
|
)
|
|
|
|
|
2009-03-16 21:29:31 -06:00
|
|
|
|
2009-03-13 17:59:51 -06:00
|
|
|
func (P *Parser) parseSimpleStat(mode int) ast.Stat {
|
2009-02-05 15:22:09 -07:00
|
|
|
if P.trace {
|
|
|
|
defer un(trace(P, "SimpleStat"));
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
|
|
|
|
2009-03-16 21:29:31 -06:00
|
|
|
loc := P.loc;
|
2009-02-05 15:22:09 -07:00
|
|
|
x := P.parseExpressionList();
|
2009-02-04 19:28:41 -07: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-11 13:52:11 -06:00
|
|
|
loc := P.loc;
|
2009-03-16 21:29:31 -06:00
|
|
|
P.expect(token.COLON);
|
2009-02-05 12:05:02 -07:00
|
|
|
P.opt_semi = true;
|
2009-03-16 21:29:31 -06:00
|
|
|
if mode & label_ok != 0 && len(x) == 1 {
|
|
|
|
if label, is_ident := x[0].(*ast.Ident); is_ident {
|
2009-03-13 17:59:51 -06:00
|
|
|
return &ast.LabeledStat{loc, label, P.parseStatement()};
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
|
|
|
}
|
2009-03-16 21:29:31 -06:00
|
|
|
P.error(loc, "illegal label declaration");
|
2009-02-04 19:28:41 -07:00
|
|
|
return nil;
|
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-16 21:29:31 -06:00
|
|
|
// assignment statement or range clause
|
2009-03-11 13:52:11 -06:00
|
|
|
loc, tok := P.loc, P.tok;
|
2009-02-05 15:22:09 -07:00
|
|
|
P.next();
|
2009-03-13 17:59:51 -06:00
|
|
|
if mode & range_ok != 0 && P.tok == token.RANGE {
|
2009-03-16 21:29:31 -06:00
|
|
|
// range clause
|
2009-02-05 15:22:09 -07:00
|
|
|
P.next();
|
2009-03-16 21:29:31 -06:00
|
|
|
if len(x) != 1 && len(x) != 2 {
|
|
|
|
P.error(loc, "expected 1 or 2 expressions on lhs of range clause");
|
|
|
|
}
|
2009-03-03 19:25:07 -07:00
|
|
|
if tok != token.DEFINE && tok != token.ASSIGN {
|
2009-03-11 13:52:11 -06:00
|
|
|
P.error(loc, "expected '=' or ':=', found '" + token.TokenString(tok) + "'");
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
2009-03-16 21:29:31 -06:00
|
|
|
y := P.parseExpression(1);
|
|
|
|
return &ast.RangeClause{loc, tok, x, y};
|
2009-02-04 19:28:41 -07:00
|
|
|
} else {
|
2009-03-16 21:29:31 -06:00
|
|
|
// assignment statement
|
|
|
|
y := P.parseExpressionList();
|
|
|
|
xl, yl := len(x), len(y);
|
|
|
|
if xl > 1 && yl > 1 && xl != yl {
|
|
|
|
P.error(loc, "arity of lhs doesn't match rhs"); // TODO use better loc for error
|
|
|
|
}
|
|
|
|
if xl == 1 && yl == 1 {
|
|
|
|
// common case - use smaller node
|
|
|
|
return &ast.AssignmentStat{loc, tok, x[0], y[0]};
|
|
|
|
} else {
|
|
|
|
// general case
|
|
|
|
return &ast.TupleAssignStat{loc, tok, x, y};
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
|
|
|
}
|
2009-02-13 15:48:32 -07:00
|
|
|
|
2009-02-04 19:28:41 -07:00
|
|
|
default:
|
2009-03-16 21:29:31 -06:00
|
|
|
if len(x) != 1 {
|
|
|
|
P.error(loc, "only one expression allowed");
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
2009-02-13 15:48:32 -07:00
|
|
|
|
2009-03-03 19:25:07 -07:00
|
|
|
if P.tok == token.INC || P.tok == token.DEC {
|
2009-03-16 21:29:31 -06:00
|
|
|
s := &ast.IncDecStat{P.loc, P.tok, x[0]};
|
2009-02-05 15:22:09 -07:00
|
|
|
P.next(); // consume "++" or "--"
|
2009-02-04 19:28:41 -07:00
|
|
|
return s;
|
|
|
|
}
|
2009-02-13 15:48:32 -07:00
|
|
|
|
2009-03-16 21:29:31 -06:00
|
|
|
// TODO change ILLEGAL -> NONE
|
|
|
|
return &ast.ExpressionStat{loc, token.ILLEGAL, x[0]};
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
unreachable();
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
func (P *Parser) parseInvocationStat(keyword int) *ast.ExpressionStat {
|
2009-02-05 15:22:09 -07:00
|
|
|
if P.trace {
|
|
|
|
defer un(trace(P, "InvocationStat"));
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
|
|
|
|
2009-03-11 13:52:11 -06:00
|
|
|
loc := P.loc;
|
2009-02-05 15:22:09 -07:00
|
|
|
P.expect(keyword);
|
2009-03-11 13:52:11 -06:00
|
|
|
return &ast.ExpressionStat{loc, keyword, P.parseExpression(1)};
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-16 21:29:31 -06:00
|
|
|
func (P *Parser) parseReturnStat() *ast.ReturnStat {
|
2009-02-05 15:22:09 -07:00
|
|
|
if P.trace {
|
|
|
|
defer un(trace(P, "ReturnStat"));
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
|
|
|
|
2009-03-11 13:52:11 -06:00
|
|
|
loc := P.loc;
|
2009-03-03 19:25:07 -07:00
|
|
|
P.expect(token.RETURN);
|
2009-03-16 21:29:31 -06:00
|
|
|
var x []ast.Expr;
|
2009-03-03 19:25:07 -07:00
|
|
|
if P.tok != token.SEMICOLON && P.tok != token.RBRACE {
|
2009-02-05 15:22:09 -07:00
|
|
|
x = P.parseExpressionList();
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
|
|
|
|
2009-03-16 21:29:31 -06:00
|
|
|
return &ast.ReturnStat{loc, x};
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
func (P *Parser) parseControlFlowStat(tok int) *ast.ControlFlowStat {
|
2009-02-05 15:22:09 -07:00
|
|
|
if P.trace {
|
|
|
|
defer un(trace(P, "ControlFlowStat"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-11 13:52:11 -06:00
|
|
|
s := &ast.ControlFlowStat{P.loc, tok, nil};
|
2009-02-05 15:22:09 -07:00
|
|
|
P.expect(tok);
|
2009-03-03 19:25:07 -07:00
|
|
|
if tok != token.FALLTHROUGH && P.tok == token.IDENT {
|
2009-03-05 18:15:36 -07:00
|
|
|
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-05 18:15:36 -07:00
|
|
|
func (P *Parser) parseControlClause(isForStat bool) (init ast.Stat, expr ast.Expr, post ast.Stat) {
|
2009-02-05 15:22:09 -07:00
|
|
|
if P.trace {
|
|
|
|
defer un(trace(P, "ControlClause"));
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
|
|
|
|
2009-03-03 19:25:07 -07:00
|
|
|
if P.tok != token.LBRACE {
|
2009-03-03 17:00:06 -07:00
|
|
|
prev_lev := P.expr_lev;
|
2009-03-11 17:06:17 -06:00
|
|
|
P.expr_lev = -1;
|
2009-03-16 21:29:31 -06:00
|
|
|
|
2009-03-03 19:25:07 -07:00
|
|
|
if P.tok != token.SEMICOLON {
|
2009-03-13 17:59:51 -06:00
|
|
|
mode := 0;
|
|
|
|
if isForStat {
|
|
|
|
mode = range_ok;
|
|
|
|
}
|
|
|
|
init = P.parseSimpleStat(mode);
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
2009-03-16 21:29:31 -06:00
|
|
|
if dummy, is_range := init.(*ast.RangeClause); !is_range {
|
|
|
|
if P.tok == token.SEMICOLON {
|
|
|
|
P.next();
|
|
|
|
if P.tok != token.SEMICOLON && P.tok != token.LBRACE {
|
|
|
|
expr = P.parseExpression(1);
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
2009-03-16 21:29:31 -06:00
|
|
|
if isForStat {
|
|
|
|
P.expect(token.SEMICOLON);
|
|
|
|
if P.tok != token.LBRACE {
|
|
|
|
post = P.parseSimpleStat(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if init != nil { // guard in case of errors
|
|
|
|
if s, is_expr_stat := init.(*ast.ExpressionStat); is_expr_stat {
|
|
|
|
expr, init = s.Expr, nil;
|
|
|
|
} else {
|
|
|
|
P.error(noloc, "illegal control clause");
|
|
|
|
}
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-03-16 21:29:31 -06:00
|
|
|
|
2009-03-03 17:00:06 -07:00
|
|
|
P.expr_lev = prev_lev;
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return init, expr, post;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
func (P *Parser) parseIfStat() *ast.IfStat {
|
2009-02-05 15:22:09 -07:00
|
|
|
if P.trace {
|
|
|
|
defer un(trace(P, "IfStat"));
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
|
|
|
|
2009-03-11 13:52:11 -06:00
|
|
|
loc := P.loc;
|
2009-03-03 19:25:07 -07:00
|
|
|
P.expect(token.IF);
|
2009-02-05 15:22:09 -07:00
|
|
|
init, cond, dummy := P.parseControlClause(false);
|
2009-03-03 19:25:07 -07:00
|
|
|
body := P.parseBlock(token.LBRACE);
|
2009-03-05 18:15:36 -07:00
|
|
|
var else_ ast.Stat;
|
2009-03-03 19:25:07 -07:00
|
|
|
if P.tok == token.ELSE {
|
2009-02-05 15:22:09 -07:00
|
|
|
P.next();
|
2009-03-05 18:15:36 -07:00
|
|
|
else_ = P.parseStatement();
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
|
|
|
|
2009-03-11 13:52:11 -06:00
|
|
|
return &ast.IfStat{loc, init, cond, body, else_};
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
func (P *Parser) parseForStat() *ast.ForStat {
|
2009-02-05 15:22:09 -07:00
|
|
|
if P.trace {
|
|
|
|
defer un(trace(P, "ForStat"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-11 13:52:11 -06:00
|
|
|
loc := P.loc;
|
2009-03-03 19:25:07 -07:00
|
|
|
P.expect(token.FOR);
|
2009-02-05 15:22:09 -07:00
|
|
|
init, cond, post := P.parseControlClause(true);
|
2009-03-03 19:25:07 -07:00
|
|
|
body := P.parseBlock(token.LBRACE);
|
2009-02-04 19:28:41 -07:00
|
|
|
|
2009-03-11 13:52:11 -06:00
|
|
|
return &ast.ForStat{loc, init, cond, post, body};
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-16 21:29:31 -06:00
|
|
|
func (P *Parser) asIdent(x ast.Expr) *ast.Ident {
|
|
|
|
if name, ok := x.(*ast.Ident); ok {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
P.error(x.Loc(), "identifier expected");
|
|
|
|
return &ast.Ident{noloc, "BAD"};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (P *Parser) isTypeSwitch(init ast.Stat) (lhs *ast.Ident, rhs ast.Expr) {
|
|
|
|
if assign, ok := init.(*ast.AssignmentStat); ok {
|
|
|
|
if guard, ok := assign.Rhs.(*ast.TypeGuard); ok {
|
|
|
|
if tmp, ok := guard.Typ.(*ast.TypeType); ok {
|
|
|
|
// we appear to have a type switch
|
|
|
|
// TODO various error checks
|
|
|
|
return P.asIdent(assign.Lhs), guard.X;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
func (P *Parser) parseCaseClause() *ast.CaseClause {
|
2009-02-05 15:22:09 -07:00
|
|
|
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-02-05 12:05:02 -07:00
|
|
|
// SwitchCase
|
2009-03-11 13:52:11 -06:00
|
|
|
loc := P.loc;
|
2009-03-16 21:29:31 -06:00
|
|
|
var x []ast.Expr;
|
2009-03-03 19:25:07 -07:00
|
|
|
if P.tok == token.CASE {
|
2009-02-05 15:22:09 -07:00
|
|
|
P.next();
|
2009-03-16 21:29:31 -06:00
|
|
|
x = P.parseExpressionList();
|
2008-09-18 17:58:37 -06:00
|
|
|
} else {
|
2009-03-03 19:25:07 -07:00
|
|
|
P.expect(token.DEFAULT);
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-16 21:29:31 -06:00
|
|
|
return &ast.CaseClause{loc, x, P.parseBlock(token.COLON)};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
func (P *Parser) parseSwitchStat() *ast.SwitchStat {
|
2009-02-05 15:22:09 -07:00
|
|
|
if P.trace {
|
|
|
|
defer un(trace(P, "SwitchStat"));
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
|
|
|
|
2009-03-11 13:52:11 -06:00
|
|
|
loc := P.loc;
|
2009-03-03 19:25:07 -07:00
|
|
|
P.expect(token.SWITCH);
|
2009-02-05 15:22:09 -07:00
|
|
|
init, tag, post := P.parseControlClause(false);
|
2009-03-11 13:52:11 -06:00
|
|
|
body := ast.NewBlock(P.loc, token.LBRACE);
|
2009-03-03 19:25:07 -07:00
|
|
|
P.expect(token.LBRACE);
|
|
|
|
for P.tok != token.RBRACE && P.tok != token.EOF {
|
2009-02-05 15:22:09 -07:00
|
|
|
body.List.Push(P.parseCaseClause());
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
2009-03-11 13:52:11 -06:00
|
|
|
body.End = P.loc;
|
2009-03-03 19:25:07 -07:00
|
|
|
P.expect(token.RBRACE);
|
2009-02-04 19:28:41 -07:00
|
|
|
P.opt_semi = true;
|
|
|
|
|
2009-03-16 21:29:31 -06:00
|
|
|
if lhs, rhs := P.isTypeSwitch(init); lhs != nil {
|
|
|
|
if tag != nil {
|
|
|
|
P.error(loc, "illegal type switch clause");
|
|
|
|
}
|
|
|
|
// TODO fix location
|
|
|
|
init = &ast.TypeSwitchClause{loc, lhs, rhs};
|
|
|
|
}
|
|
|
|
|
2009-03-11 13:52:11 -06:00
|
|
|
return &ast.SwitchStat{loc, init, tag, body};
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-16 21:29:31 -06:00
|
|
|
func (P *Parser) parseCommClause() *ast.CommClause {
|
2009-02-05 15:22:09 -07:00
|
|
|
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-11 13:52:11 -06:00
|
|
|
loc := P.loc;
|
2009-03-16 21:29:31 -06:00
|
|
|
var tok int;
|
|
|
|
var lhs, rhs ast.Expr;
|
2009-03-03 19:25:07 -07:00
|
|
|
if P.tok == token.CASE {
|
2009-02-05 15:22:09 -07:00
|
|
|
P.next();
|
2009-03-16 21:29:31 -06:00
|
|
|
if P.tok == token.ARROW {
|
|
|
|
// RecvExpr without assignment
|
|
|
|
rhs = P.parseExpression(1);
|
|
|
|
} else {
|
|
|
|
// SendExpr or RecvExpr
|
|
|
|
rhs = P.parseExpression(1);
|
|
|
|
if P.tok == token.ASSIGN || P.tok == token.DEFINE {
|
|
|
|
// RecvExpr with assignment
|
|
|
|
tok = P.tok;
|
|
|
|
P.next();
|
|
|
|
lhs = rhs;
|
|
|
|
if P.tok == token.ARROW {
|
|
|
|
rhs = P.parseExpression(1);
|
|
|
|
} else {
|
|
|
|
P.expect(token.ARROW); // use expect() error handling
|
|
|
|
}
|
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-03 19:25:07 -07:00
|
|
|
P.expect(token.DEFAULT);
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2008-10-14 19:14:01 -06:00
|
|
|
|
2009-03-16 21:29:31 -06:00
|
|
|
return &ast.CommClause{loc, tok, lhs, rhs, P.parseBlock(token.COLON)};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
func (P *Parser) parseSelectStat() *ast.SelectStat {
|
2009-02-05 15:22:09 -07:00
|
|
|
if P.trace {
|
|
|
|
defer un(trace(P, "SelectStat"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-11 13:52:11 -06:00
|
|
|
loc := P.loc;
|
2009-03-03 19:25:07 -07:00
|
|
|
P.expect(token.SELECT);
|
2009-03-11 13:52:11 -06:00
|
|
|
body := ast.NewBlock(P.loc, token.LBRACE);
|
2009-03-03 19:25:07 -07:00
|
|
|
P.expect(token.LBRACE);
|
|
|
|
for P.tok != token.RBRACE && P.tok != token.EOF {
|
2009-02-05 15:22:09 -07:00
|
|
|
body.List.Push(P.parseCommClause());
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
2009-03-11 13:52:11 -06:00
|
|
|
body.End = P.loc;
|
2009-03-03 19:25:07 -07:00
|
|
|
P.expect(token.RBRACE);
|
2009-02-04 19:28:41 -07:00
|
|
|
P.opt_semi = true;
|
|
|
|
|
2009-03-11 13:52:11 -06:00
|
|
|
return &ast.SelectStat{loc, body};
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
func (P *Parser) parseStatement() ast.Stat {
|
2009-02-05 15:22:09 -07:00
|
|
|
if P.trace {
|
|
|
|
defer un(trace(P, "Statement"));
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
switch P.tok {
|
2009-03-03 19:25:07 -07:00
|
|
|
case token.CONST, token.TYPE, token.VAR:
|
2009-03-05 18:15:36 -07:00
|
|
|
return &ast.DeclarationStat{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
|
|
|
|
return P.parseSimpleStat(label_ok);
|
2009-03-03 19:25:07 -07:00
|
|
|
case token.GO, token.DEFER:
|
2009-02-05 15:22:09 -07:00
|
|
|
return P.parseInvocationStat(P.tok);
|
2009-03-03 19:25:07 -07:00
|
|
|
case token.RETURN:
|
2009-02-05 15:22:09 -07:00
|
|
|
return P.parseReturnStat();
|
2009-03-03 19:25:07 -07:00
|
|
|
case token.BREAK, token.CONTINUE, token.GOTO, token.FALLTHROUGH:
|
2009-02-05 15:22:09 -07:00
|
|
|
return P.parseControlFlowStat(P.tok);
|
2009-03-03 19:25:07 -07:00
|
|
|
case token.LBRACE:
|
2009-03-05 18:15:36 -07:00
|
|
|
return &ast.CompositeStat{P.parseBlock(token.LBRACE)};
|
2009-03-03 19:25:07 -07:00
|
|
|
case token.IF:
|
2009-02-05 15:22:09 -07:00
|
|
|
return P.parseIfStat();
|
2009-03-03 19:25:07 -07:00
|
|
|
case token.FOR:
|
2009-02-05 15:22:09 -07:00
|
|
|
return P.parseForStat();
|
2009-03-03 19:25:07 -07:00
|
|
|
case token.SWITCH:
|
2009-02-05 15:22:09 -07:00
|
|
|
return P.parseSwitchStat();
|
2009-03-03 19:25:07 -07:00
|
|
|
case token.SELECT:
|
2009-02-05 15:22:09 -07:00
|
|
|
return P.parseSelectStat();
|
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-11 13:52:11 -06:00
|
|
|
return &ast.EmptyStat{P.loc};
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
|
|
|
|
2009-02-12 17:06:21 -07:00
|
|
|
// no statement found
|
2009-03-11 13:52:11 -06:00
|
|
|
P.error(P.loc, "statement expected");
|
|
|
|
return &ast.BadStat{P.loc};
|
2009-02-04 19:28:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-18 17:58:37 -06:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Declarations
|
|
|
|
|
2009-03-11 13:52:11 -06:00
|
|
|
func (P *Parser) parseImportSpec(loc scanner.Location) *ast.ImportDecl {
|
2009-02-05 15:22:09 -07:00
|
|
|
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-03 19:25:07 -07:00
|
|
|
if P.tok == token.PERIOD {
|
2009-03-11 13:52:11 -06:00
|
|
|
P.error(P.loc, `"import ." not yet handled properly`);
|
2009-02-05 15:22:09 -07:00
|
|
|
P.next();
|
2009-03-03 19:25:07 -07:00
|
|
|
} else if P.tok == token.IDENT {
|
2009-03-05 18:15:36 -07:00
|
|
|
ident = P.parseIdent();
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
var path ast.Expr;
|
2009-03-03 19:25:07 -07:00
|
|
|
if P.tok == token.STRING {
|
2009-03-05 18:15:36 -07:00
|
|
|
path = P.parseStringLit();
|
2008-09-18 17:58:37 -06:00
|
|
|
} else {
|
2009-03-03 19:25:07 -07: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-11 13:52:11 -06:00
|
|
|
return &ast.ImportDecl{loc, ident, path};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-12 18:24:03 -06:00
|
|
|
func (P *Parser) parseConstSpec(loc scanner.Location, comment ast.CommentGroup) *ast.ConstDecl {
|
2009-02-05 15:22:09 -07:00
|
|
|
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-16 21:29:31 -06:00
|
|
|
names := P.parseIdentList(nil);
|
2009-02-27 16:40:17 -07:00
|
|
|
typ := P.tryType();
|
2009-03-16 21:29:31 -06:00
|
|
|
var values []ast.Expr;
|
2009-03-13 17:59:51 -06:00
|
|
|
if typ != nil || P.tok == token.ASSIGN {
|
|
|
|
P.expect(token.ASSIGN);
|
2009-03-16 21:29:31 -06:00
|
|
|
values = P.parseExpressionList();
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2009-03-11 17:06:17 -06:00
|
|
|
|
2009-03-16 21:29:31 -06:00
|
|
|
return &ast.ConstDecl{loc, names, typ, values, comment};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-12 18:24:03 -06:00
|
|
|
func (P *Parser) parseTypeSpec(loc scanner.Location, comment ast.CommentGroup) *ast.TypeDecl {
|
2009-02-05 15:22:09 -07:00
|
|
|
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-05 18:15:36 -07:00
|
|
|
ident := P.parseIdent();
|
2009-02-27 16:40:17 -07:00
|
|
|
typ := P.parseType();
|
2009-03-11 17:06:17 -06:00
|
|
|
|
2009-03-12 18:24:03 -06:00
|
|
|
return &ast.TypeDecl{loc, ident, typ, comment};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-12 18:24:03 -06:00
|
|
|
func (P *Parser) parseVarSpec(loc scanner.Location, comment ast.CommentGroup) *ast.VarDecl {
|
2009-02-05 15:22:09 -07:00
|
|
|
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-16 21:29:31 -06:00
|
|
|
names := P.parseIdentList(nil);
|
2009-03-13 17:59:51 -06:00
|
|
|
typ := P.tryType();
|
2009-03-16 21:29:31 -06:00
|
|
|
var values []ast.Expr;
|
2009-03-13 17:59:51 -06:00
|
|
|
if typ == nil || P.tok == token.ASSIGN {
|
|
|
|
P.expect(token.ASSIGN);
|
2009-03-16 21:29:31 -06:00
|
|
|
values = P.parseExpressionList();
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2009-03-11 17:06:17 -06:00
|
|
|
|
2009-03-16 21:29:31 -06:00
|
|
|
return &ast.VarDecl{loc, names, typ, values, comment};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-12 18:24:03 -06:00
|
|
|
func (P *Parser) parseSpec(loc scanner.Location, comment ast.CommentGroup, keyword int) ast.Decl {
|
2009-02-27 16:40:17 -07:00
|
|
|
switch keyword {
|
2009-03-11 13:52:11 -06:00
|
|
|
case token.IMPORT: return P.parseImportSpec(loc);
|
2009-03-12 18:24:03 -06:00
|
|
|
case token.CONST: return P.parseConstSpec(loc, comment);
|
|
|
|
case token.TYPE: return P.parseTypeSpec(loc, comment);
|
|
|
|
case token.VAR: return P.parseVarSpec(loc, comment);
|
2009-01-23 10:44:01 -07:00
|
|
|
}
|
2009-03-11 17:06:17 -06:00
|
|
|
|
2009-02-27 16:40:17 -07:00
|
|
|
unreachable();
|
|
|
|
return nil;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
func (P *Parser) parseDecl(keyword int) ast.Decl {
|
2009-02-05 15:22:09 -07:00
|
|
|
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-12 18:24:03 -06:00
|
|
|
comment := P.getLastComment();
|
2009-03-11 13:52:11 -06:00
|
|
|
loc := P.loc;
|
2009-02-05 15:22:09 -07:00
|
|
|
P.expect(keyword);
|
2009-03-03 19:25:07 -07:00
|
|
|
if P.tok == token.LPAREN {
|
2009-02-05 15:22:09 -07:00
|
|
|
P.next();
|
2009-02-27 16:40:17 -07:00
|
|
|
list := vector.New(0);
|
2009-03-03 19:25:07 -07:00
|
|
|
for P.tok != token.RPAREN && P.tok != token.EOF {
|
2009-03-12 18:24:03 -06:00
|
|
|
list.Push(P.parseSpec(noloc, nil, keyword));
|
2009-03-03 19:25:07 -07:00
|
|
|
if P.tok == token.SEMICOLON {
|
2009-02-05 15:22:09 -07:00
|
|
|
P.next();
|
2008-10-09 19:03:16 -06:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2009-03-11 13:52:11 -06:00
|
|
|
end := P.loc;
|
2009-03-03 19:25:07 -07:00
|
|
|
P.expect(token.RPAREN);
|
2008-10-10 17:03:32 -06:00
|
|
|
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-11 13:52:11 -06:00
|
|
|
return &ast.DeclList{loc, keyword, decls, end};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-12 18:24:03 -06:00
|
|
|
return P.parseSpec(loc, comment, keyword);
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
// Function and method declarations
|
2008-09-19 13:12:28 -06:00
|
|
|
//
|
|
|
|
// func ident (params)
|
|
|
|
// func ident (params) type
|
|
|
|
// func ident (params) (results)
|
|
|
|
// func (recv) ident (params)
|
|
|
|
// func (recv) ident (params) type
|
|
|
|
// func (recv) ident (params) (results)
|
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
func (P *Parser) parseFunctionDecl() *ast.FuncDecl {
|
2009-02-05 15:22:09 -07:00
|
|
|
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-12 18:24:03 -06:00
|
|
|
comment := P.getLastComment();
|
2009-03-11 13:52:11 -06:00
|
|
|
loc := P.loc;
|
2009-03-03 19:25:07 -07:00
|
|
|
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-03 19:25:07 -07:00
|
|
|
if P.tok == token.LPAREN {
|
2009-03-11 13:52:11 -06:00
|
|
|
loc := P.loc;
|
2009-02-27 16:40:17 -07:00
|
|
|
tmp := P.parseParameters(true);
|
|
|
|
if len(tmp) == 1 {
|
|
|
|
recv = tmp[0];
|
|
|
|
} else {
|
2009-03-11 13:52:11 -06:00
|
|
|
P.error(loc, "must have exactly one receiver");
|
2008-09-19 13:12:28 -06:00
|
|
|
}
|
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
ident := P.parseIdent();
|
2009-02-27 16:40:17 -07:00
|
|
|
sig := P.parseSignature();
|
2008-10-16 13:16:50 -06:00
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
var body *ast.Block;
|
2009-03-03 19:25:07 -07:00
|
|
|
if P.tok == token.LBRACE {
|
|
|
|
body = P.parseBlock(token.LBRACE);
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-12 18:24:03 -06:00
|
|
|
return &ast.FuncDecl{loc, recv, ident, sig, body, comment};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-05 18:15:36 -07:00
|
|
|
func (P *Parser) parseDeclaration() ast.Decl {
|
2009-02-05 15:22:09 -07:00
|
|
|
if P.trace {
|
|
|
|
defer un(trace(P, "Declaration"));
|
2009-02-03 18:44:01 -07:00
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2008-09-18 17:58:37 -06:00
|
|
|
switch P.tok {
|
2009-03-03 19:25:07 -07:00
|
|
|
case token.CONST, token.TYPE, token.VAR:
|
2009-02-27 16:40:17 -07:00
|
|
|
return P.parseDecl(P.tok);
|
2009-03-03 19:25:07 -07:00
|
|
|
case token.FUNC:
|
2009-02-27 16:40:17 -07:00
|
|
|
return P.parseFunctionDecl();
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2009-03-11 17:06:17 -06:00
|
|
|
|
2009-03-11 13:52:11 -06:00
|
|
|
loc := P.loc;
|
|
|
|
P.error(loc, "declaration expected");
|
2009-02-27 16:40:17 -07:00
|
|
|
P.next(); // make progress
|
2009-03-11 13:52:11 -06:00
|
|
|
return &ast.BadDecl{loc};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Program
|
|
|
|
|
2009-03-12 18:24:03 -06:00
|
|
|
func (P *Parser) getComments() []ast.CommentGroup {
|
2009-03-05 18:15:36 -07:00
|
|
|
// convert comments vector
|
2009-03-12 18:24:03 -06:00
|
|
|
list := make([]ast.CommentGroup, P.comments.Len());
|
2009-03-05 18:15:36 -07:00
|
|
|
for i := 0; i < P.comments.Len(); i++ {
|
2009-03-12 18:24:03 -06:00
|
|
|
list[i] = P.comments.At(i).(ast.CommentGroup);
|
2009-03-05 18:15:36 -07:00
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-12 18:24:03 -06:00
|
|
|
// The Parse function is parametrized with one of the following
|
|
|
|
// constants. They control how much of the source text is parsed.
|
|
|
|
//
|
|
|
|
const (
|
|
|
|
ParseEntirePackage = iota;
|
|
|
|
ParseImportDeclsOnly;
|
|
|
|
ParsePackageClauseOnly;
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// Parse parses the source...
|
|
|
|
//
|
|
|
|
// foo bar
|
|
|
|
//
|
|
|
|
func (P *Parser) Parse(mode int) *ast.Program {
|
2009-03-05 18:15:36 -07:00
|
|
|
if P.trace {
|
|
|
|
defer un(trace(P, "Program"));
|
|
|
|
}
|
|
|
|
|
2009-03-12 18:24:03 -06:00
|
|
|
// package clause
|
|
|
|
comment := P.getLastComment();
|
|
|
|
loc := P.loc;
|
|
|
|
P.expect(token.PACKAGE);
|
|
|
|
name := P.parseIdent();
|
2009-03-13 17:59:51 -06:00
|
|
|
if P.tok == token.SEMICOLON {
|
|
|
|
// common error
|
|
|
|
P.error(P.loc, "extra semicolon");
|
|
|
|
P.next();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-12 18:24:03 -06:00
|
|
|
var decls []ast.Decl;
|
|
|
|
if mode <= ParseImportDeclsOnly {
|
|
|
|
// import decls
|
|
|
|
list := vector.New(0);
|
|
|
|
for P.tok == token.IMPORT {
|
|
|
|
list.Push(P.parseDecl(token.IMPORT));
|
|
|
|
if P.tok == token.SEMICOLON {
|
|
|
|
P.next();
|
|
|
|
}
|
2009-03-10 19:20:08 -06:00
|
|
|
}
|
2009-02-27 16:40:17 -07:00
|
|
|
|
2009-03-12 18:24:03 -06:00
|
|
|
if mode <= ParseEntirePackage {
|
|
|
|
// rest of package body
|
|
|
|
for P.tok != token.EOF {
|
|
|
|
list.Push(P.parseDeclaration());
|
|
|
|
if P.tok == token.SEMICOLON {
|
|
|
|
P.next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-12-19 04:05:37 -07:00
|
|
|
|
2009-03-12 18:24:03 -06:00
|
|
|
// convert list
|
|
|
|
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-12 18:24:03 -06:00
|
|
|
return &ast.Program{loc, name, decls, comment, P.getComments()};
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|