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.
|
|
|
|
|
|
|
|
package Parser
|
|
|
|
|
|
|
|
import Scanner "scanner"
|
2008-10-20 16:03:40 -06:00
|
|
|
import AST "ast"
|
2008-09-22 19:26:12 -06:00
|
|
|
|
2008-09-18 17:58:37 -06:00
|
|
|
|
|
|
|
export type Parser struct {
|
2008-10-18 17:42:25 -06:00
|
|
|
// Tracing/debugging
|
2008-10-26 22:32:30 -06:00
|
|
|
verbose, sixg, deps bool;
|
2008-09-18 17:58:37 -06:00
|
|
|
indent uint;
|
2008-10-18 17:42:25 -06:00
|
|
|
|
|
|
|
// Scanner
|
2008-09-18 17:58:37 -06:00
|
|
|
scanner *Scanner.Scanner;
|
|
|
|
tokchan *<-chan *Scanner.Token;
|
2008-10-20 16:03:40 -06:00
|
|
|
comments *AST.List;
|
2008-09-18 17:58:37 -06:00
|
|
|
|
2008-09-30 19:50:29 -06:00
|
|
|
// Scanner.Token
|
2008-09-18 17:58:37 -06:00
|
|
|
pos int; // token source position
|
2008-09-30 19:50:29 -06:00
|
|
|
tok int; // one token look-ahead
|
2008-09-18 17:58:37 -06:00
|
|
|
val string; // token value (for IDENT, NUMBER, STRING only)
|
2008-10-10 17:03:32 -06:00
|
|
|
|
|
|
|
// Non-syntactic parser control
|
|
|
|
opt_semi bool; // true if semicolon is optional
|
2008-09-18 17:58:37 -06:00
|
|
|
|
2008-10-10 17:03:32 -06:00
|
|
|
// Nesting levels
|
|
|
|
expr_lev int; // 0 = control clause level, 1 = expr inside ()'s
|
|
|
|
scope_lev int; // 0 = global scope, 1 = function scope of global functions, etc.
|
2008-09-18 17:58:37 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Support functions
|
|
|
|
|
|
|
|
func (P *Parser) PrintIndent() {
|
|
|
|
for i := P.indent; i > 0; i-- {
|
|
|
|
print(". ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (P *Parser) Trace(msg string) {
|
|
|
|
if P.verbose {
|
|
|
|
P.PrintIndent();
|
|
|
|
print(msg, " {\n");
|
|
|
|
}
|
2008-10-14 19:14:01 -06:00
|
|
|
P.indent++; // always check proper identation
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (P *Parser) Ecart() {
|
2008-10-14 19:14:01 -06:00
|
|
|
P.indent--; // always check proper identation
|
2008-09-18 17:58:37 -06:00
|
|
|
if P.verbose {
|
|
|
|
P.PrintIndent();
|
|
|
|
print("}\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-17 00:30:42 -06:00
|
|
|
func (P *Parser) Next0() {
|
2008-09-18 17:58:37 -06:00
|
|
|
if P.tokchan == nil {
|
2008-09-30 19:50:29 -06:00
|
|
|
P.pos, P.tok, P.val = P.scanner.Scan();
|
2008-09-18 17:58:37 -06:00
|
|
|
} else {
|
|
|
|
t := <-P.tokchan;
|
|
|
|
P.tok, P.pos, P.val = t.tok, t.pos, t.val;
|
|
|
|
}
|
2008-10-10 17:03:32 -06:00
|
|
|
P.opt_semi = false;
|
2008-10-17 17:19:31 -06:00
|
|
|
|
2008-09-18 17:58:37 -06:00
|
|
|
if P.verbose {
|
|
|
|
P.PrintIndent();
|
2008-10-15 12:48:18 -06:00
|
|
|
print("[", P.pos, "] ", Scanner.TokenString(P.tok), "\n");
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-17 00:30:42 -06:00
|
|
|
func (P *Parser) Next() {
|
|
|
|
for P.Next0(); P.tok == Scanner.COMMENT; P.Next0() {
|
2008-10-20 16:03:40 -06:00
|
|
|
P.comments.Add(AST.NewComment(P.pos, P.val));
|
2008-10-17 00:30:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-26 22:32:30 -06:00
|
|
|
func (P *Parser) Open(verbose, sixg, deps bool, scanner *Scanner.Scanner, tokchan *<-chan *Scanner.Token) {
|
2008-09-22 19:26:12 -06:00
|
|
|
P.verbose = verbose;
|
2008-10-18 17:42:25 -06:00
|
|
|
P.sixg = sixg;
|
2008-10-26 22:32:30 -06:00
|
|
|
P.deps = deps;
|
2008-09-18 17:58:37 -06:00
|
|
|
P.indent = 0;
|
2008-10-18 17:42:25 -06:00
|
|
|
|
2008-09-18 17:58:37 -06:00
|
|
|
P.scanner = scanner;
|
|
|
|
P.tokchan = tokchan;
|
2008-10-20 16:03:40 -06:00
|
|
|
P.comments = AST.NewList();
|
2008-10-18 17:42:25 -06:00
|
|
|
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Next();
|
2008-10-23 18:56:54 -06:00
|
|
|
P.expr_lev = 0;
|
2008-10-10 17:03:32 -06:00
|
|
|
P.scope_lev = 0;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (P *Parser) Error(pos int, msg string) {
|
|
|
|
P.scanner.Error(pos, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (P *Parser) Expect(tok int) {
|
|
|
|
if P.tok != tok {
|
2008-10-18 17:42:25 -06:00
|
|
|
msg := "expected '" + Scanner.TokenString(tok) + "', found '" + Scanner.TokenString(P.tok) + "'";
|
|
|
|
switch P.tok {
|
|
|
|
case Scanner.IDENT, Scanner.INT, Scanner.FLOAT, Scanner.STRING:
|
|
|
|
msg += " " + P.val;
|
|
|
|
}
|
|
|
|
P.Error(P.pos, msg);
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
P.Next(); // make progress in any case
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-09 19:03:16 -06:00
|
|
|
func (P *Parser) OptSemicolon() {
|
|
|
|
if P.tok == Scanner.SEMICOLON {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-17 17:19:31 -06:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// AST support
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func ExprType(x *AST.Expr) *AST.Type {
|
|
|
|
var t *AST.Type;
|
2008-10-17 17:19:31 -06:00
|
|
|
if x.tok == Scanner.TYPE {
|
|
|
|
t = x.t;
|
|
|
|
} else if x.tok == Scanner.IDENT {
|
|
|
|
// assume a type name
|
2008-10-20 16:03:40 -06:00
|
|
|
t = AST.NewType(x.pos, Scanner.IDENT);
|
2008-10-17 17:19:31 -06:00
|
|
|
t.expr = x;
|
|
|
|
} else if x.tok == Scanner.PERIOD && x.y != nil && ExprType(x.x) != nil {
|
|
|
|
// possibly a qualified (type) identifier
|
2008-10-20 16:03:40 -06:00
|
|
|
t = AST.NewType(x.pos, Scanner.IDENT);
|
2008-10-17 17:19:31 -06:00
|
|
|
t.expr = x;
|
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) NoType(x *AST.Expr) *AST.Expr {
|
2008-10-17 00:30:42 -06:00
|
|
|
if x != nil && x.tok == Scanner.TYPE {
|
|
|
|
P.Error(x.pos, "expected expression, found type");
|
2008-10-20 16:03:40 -06:00
|
|
|
x = AST.NewLit(x.pos, Scanner.INT, "");
|
2008-10-17 00:30:42 -06:00
|
|
|
}
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) NewExpr(pos, tok int, x, y *AST.Expr) *AST.Expr {
|
|
|
|
return AST.NewExpr(pos, tok, P.NoType(x), P.NoType(y));
|
2008-10-17 00:30:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-18 17:58:37 -06:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Common productions
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) TryType() *AST.Type;
|
|
|
|
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
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseIdent() *AST.Expr {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Trace("Ident");
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
x := AST.BadExpr;
|
2008-09-18 17:58:37 -06:00
|
|
|
if P.tok == Scanner.IDENT {
|
2008-10-20 16:03:40 -06:00
|
|
|
x = AST.NewLit(P.pos, Scanner.IDENT, P.val);
|
2008-09-18 17:58:37 -06:00
|
|
|
if P.verbose {
|
|
|
|
P.PrintIndent();
|
2008-10-15 12:48:18 -06:00
|
|
|
print("Ident = \"", x.s, "\"\n");
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
P.Next();
|
|
|
|
} else {
|
|
|
|
P.Expect(Scanner.IDENT); // use Expect() error handling
|
|
|
|
}
|
|
|
|
|
|
|
|
P.Ecart();
|
2008-10-14 19:14:01 -06:00
|
|
|
return x;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseIdentList() *AST.Expr {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Trace("IdentList");
|
|
|
|
|
2008-10-16 13:16:50 -06:00
|
|
|
x := P.ParseIdent();
|
2008-10-20 17:44:03 -06:00
|
|
|
for first := true; P.tok == Scanner.COMMA; {
|
2008-10-16 13:16:50 -06:00
|
|
|
pos := P.pos;
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Next();
|
2008-10-20 17:44:03 -06:00
|
|
|
y := P.ParseIdent();
|
|
|
|
if first {
|
|
|
|
x = P.NewExpr(pos, Scanner.COMMA, x, y);
|
|
|
|
first = false;
|
|
|
|
} else {
|
|
|
|
x.y = P.NewExpr(pos, Scanner.COMMA, x.y, y);
|
|
|
|
}
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
P.Ecart();
|
2008-10-16 13:16:50 -06:00
|
|
|
return x;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Types
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseType() *AST.Type {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Trace("Type");
|
|
|
|
|
2008-10-17 17:19:31 -06:00
|
|
|
t := P.TryType();
|
|
|
|
if t == nil {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Error(P.pos, "type expected");
|
2008-10-20 16:03:40 -06:00
|
|
|
t = AST.BadType;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
P.Ecart();
|
2008-10-17 17:19:31 -06:00
|
|
|
return t;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseVarType() *AST.Type {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Trace("VarType");
|
|
|
|
|
2008-09-24 16:50:28 -06:00
|
|
|
typ := P.ParseType();
|
2008-09-18 17:58:37 -06:00
|
|
|
|
|
|
|
P.Ecart();
|
2008-09-24 16:50:28 -06:00
|
|
|
return typ;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseQualifiedIdent() *AST.Expr {
|
2008-10-14 19:14:01 -06:00
|
|
|
P.Trace("QualifiedIdent");
|
|
|
|
|
|
|
|
x := P.ParseIdent();
|
|
|
|
for P.tok == Scanner.PERIOD {
|
|
|
|
pos := P.pos;
|
|
|
|
P.Next();
|
|
|
|
y := P.ParseIdent();
|
2008-10-17 00:30:42 -06:00
|
|
|
x = P.NewExpr(pos, Scanner.PERIOD, x, y);
|
2008-10-14 19:14:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
P.Ecart();
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseTypeName() *AST.Type {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Trace("TypeName");
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
t := AST.NewType(P.pos, P.tok);
|
2008-10-14 19:14:01 -06:00
|
|
|
t.expr = P.ParseQualifiedIdent();
|
2008-09-18 17:58:37 -06:00
|
|
|
|
|
|
|
P.Ecart();
|
2008-10-14 19:14:01 -06:00
|
|
|
return t;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseArrayType() *AST.Type {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Trace("ArrayType");
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
t := AST.NewType(P.pos, Scanner.LBRACK);
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Expect(Scanner.LBRACK);
|
|
|
|
if P.tok != Scanner.RBRACK {
|
2008-10-18 21:20:30 -06:00
|
|
|
t.expr = P.ParseExpression(1);
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
P.Expect(Scanner.RBRACK);
|
2008-10-14 19:14:01 -06:00
|
|
|
t.elt = P.ParseType();
|
2008-09-18 17:58:37 -06:00
|
|
|
|
2008-09-24 16:50:28 -06:00
|
|
|
P.Ecart();
|
2008-10-14 19:14:01 -06:00
|
|
|
return t;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseChannelType() *AST.Type {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Trace("ChannelType");
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
t := AST.NewType(P.pos, Scanner.CHAN);
|
|
|
|
t.mode = AST.FULL;
|
2008-09-18 17:58:37 -06:00
|
|
|
if P.tok == Scanner.CHAN {
|
|
|
|
P.Next();
|
|
|
|
if P.tok == Scanner.ARROW {
|
|
|
|
P.Next();
|
2008-10-20 16:03:40 -06:00
|
|
|
t.mode = AST.SEND;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
P.Expect(Scanner.ARROW);
|
|
|
|
P.Expect(Scanner.CHAN);
|
2008-10-20 16:03:40 -06:00
|
|
|
t.mode = AST.RECV;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2008-10-14 19:14:01 -06:00
|
|
|
t.elt = P.ParseVarType();
|
2008-09-18 17:58:37 -06:00
|
|
|
|
2008-09-24 16:50:28 -06:00
|
|
|
P.Ecart();
|
2008-10-14 19:14:01 -06:00
|
|
|
return t;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-18 17:42:25 -06:00
|
|
|
// TODO: The code below (ParseVarDecl, ParseVarDeclList) is all too
|
|
|
|
// complicated. There must be a better way to do this.
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseVarDecl(expect_ident bool) *AST.Type {
|
|
|
|
t := AST.BadType;
|
2008-10-18 17:42:25 -06:00
|
|
|
if expect_ident {
|
|
|
|
x := P.ParseIdent();
|
2008-10-20 16:03:40 -06:00
|
|
|
t = AST.NewType(x.pos, Scanner.IDENT);
|
2008-10-18 17:42:25 -06:00
|
|
|
t.expr = x;
|
2008-10-24 15:04:54 -06:00
|
|
|
} else if P.tok == Scanner.ELLIPSIS {
|
|
|
|
t = AST.NewType(P.pos, Scanner.ELLIPSIS);
|
|
|
|
P.Next();
|
2008-10-18 17:42:25 -06:00
|
|
|
} else {
|
|
|
|
t = P.ParseType();
|
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-24 15:04:54 -06:00
|
|
|
func (P *Parser) ParseVarDeclList(list *AST.List, ellipsis_ok bool) {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Trace("VarDeclList");
|
2008-10-01 15:31:44 -06:00
|
|
|
|
2008-10-16 13:16:50 -06:00
|
|
|
// parse a list of types
|
2008-10-15 18:06:28 -06:00
|
|
|
i0 := list.len();
|
2008-10-24 15:04:54 -06:00
|
|
|
for {
|
2008-10-18 17:42:25 -06:00
|
|
|
list.Add(P.ParseVarDecl(i0 > 0));
|
2008-10-24 15:04:54 -06:00
|
|
|
if P.tok == Scanner.COMMA {
|
|
|
|
P.Next();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2008-10-01 15:31:44 -06:00
|
|
|
}
|
2008-10-15 12:48:18 -06:00
|
|
|
|
2008-10-24 15:04:54 -06:00
|
|
|
typ := P.TryType();
|
|
|
|
if typ == nil && P.tok == Scanner.ELLIPSIS {
|
|
|
|
typ = AST.NewType(P.pos, Scanner.ELLIPSIS);
|
|
|
|
P.Next();
|
|
|
|
}
|
|
|
|
|
|
|
|
if i0 > 0 && typ == nil {
|
2008-10-18 17:42:25 -06:00
|
|
|
// not the first parameter section; we must have a type
|
2008-10-24 15:04:54 -06:00
|
|
|
P.Error(P.pos, "type expected");
|
|
|
|
typ = AST.BadType;
|
2008-10-18 17:42:25 -06:00
|
|
|
}
|
2008-10-24 15:04:54 -06:00
|
|
|
|
2008-10-16 13:16:50 -06:00
|
|
|
// convert the list into a list of (type) expressions
|
2008-10-15 12:48:18 -06:00
|
|
|
if typ != nil {
|
2008-10-15 18:06:28 -06:00
|
|
|
// all list entries must be identifiers
|
|
|
|
// convert the type entries into identifiers
|
|
|
|
for i, n := i0, list.len(); i < n; i++ {
|
2008-10-20 16:03:40 -06:00
|
|
|
t := list.at(i).(*AST.Type);
|
2008-10-15 12:48:18 -06:00
|
|
|
if t.tok == Scanner.IDENT && t.expr.tok == Scanner.IDENT {
|
2008-10-15 18:06:28 -06:00
|
|
|
list.set(i, t.expr);
|
2008-10-15 12:48:18 -06:00
|
|
|
} else {
|
2008-10-20 16:03:40 -06:00
|
|
|
list.set(i, AST.BadExpr);
|
2008-10-15 12:48:18 -06:00
|
|
|
P.Error(t.pos, "identifier expected");
|
|
|
|
}
|
|
|
|
}
|
2008-10-15 18:06:28 -06:00
|
|
|
// add type
|
2008-10-20 16:03:40 -06:00
|
|
|
list.Add(AST.NewTypeExpr(typ));
|
2008-10-15 18:06:28 -06:00
|
|
|
|
2008-10-15 12:48:18 -06:00
|
|
|
} else {
|
|
|
|
// all list entries are types
|
2008-10-15 18:06:28 -06:00
|
|
|
// convert all type entries into type expressions
|
2008-10-24 15:04:54 -06:00
|
|
|
if i0 > 0 {
|
|
|
|
panic("internal parser error");
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, n := 0, list.len(); i < n; i++ {
|
2008-10-20 16:03:40 -06:00
|
|
|
t := list.at(i).(*AST.Type);
|
|
|
|
list.set(i, AST.NewTypeExpr(t));
|
2008-10-15 18:06:28 -06:00
|
|
|
}
|
2008-10-15 12:48:18 -06:00
|
|
|
|
2008-10-15 18:06:28 -06:00
|
|
|
if P.tok == Scanner.COMMA {
|
|
|
|
panic("internal parser error");
|
|
|
|
}
|
2008-09-27 18:42:18 -06:00
|
|
|
}
|
2008-09-18 17:58:37 -06:00
|
|
|
|
|
|
|
P.Ecart();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-24 15:04:54 -06:00
|
|
|
func (P *Parser) ParseParameterList(ellipsis_ok bool) *AST.List {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Trace("ParameterList");
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
list := AST.NewList();
|
2008-10-24 15:04:54 -06:00
|
|
|
P.ParseVarDeclList(list, ellipsis_ok);
|
2008-09-18 17:58:37 -06:00
|
|
|
for P.tok == Scanner.COMMA {
|
|
|
|
P.Next();
|
2008-10-24 15:04:54 -06:00
|
|
|
P.ParseVarDeclList(list, ellipsis_ok);
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
P.Ecart();
|
2008-09-23 19:34:17 -06:00
|
|
|
return list;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-24 15:04:54 -06:00
|
|
|
func (P *Parser) ParseParameters(ellipsis_ok bool) *AST.Type {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Trace("Parameters");
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
t := AST.NewType(P.pos, Scanner.STRUCT);
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Expect(Scanner.LPAREN);
|
|
|
|
if P.tok != Scanner.RPAREN {
|
2008-10-24 15:04:54 -06:00
|
|
|
t.list = P.ParseParameterList(ellipsis_ok);
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
P.Expect(Scanner.RPAREN);
|
|
|
|
|
|
|
|
P.Ecart();
|
2008-10-15 18:06:28 -06:00
|
|
|
return t;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-19 19:06:53 -06:00
|
|
|
func (P *Parser) ParseResultList() {
|
|
|
|
P.Trace("ResultList");
|
|
|
|
|
|
|
|
P.ParseType();
|
|
|
|
for P.tok == Scanner.COMMA {
|
|
|
|
P.Next();
|
|
|
|
P.ParseType();
|
|
|
|
}
|
|
|
|
if P.tok != Scanner.RPAREN {
|
|
|
|
P.ParseType();
|
|
|
|
}
|
|
|
|
|
|
|
|
P.Ecart();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseResult() *AST.Type {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Trace("Result");
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
var t *AST.Type;
|
2008-09-18 17:58:37 -06:00
|
|
|
if P.tok == Scanner.LPAREN {
|
2008-10-24 15:04:54 -06:00
|
|
|
t = P.ParseParameters(false);
|
2008-09-18 17:58:37 -06:00
|
|
|
} else {
|
2008-10-14 19:14:01 -06:00
|
|
|
typ := P.TryType();
|
2008-10-15 18:06:28 -06:00
|
|
|
if typ != nil {
|
2008-10-20 16:03:40 -06:00
|
|
|
t = AST.NewType(P.pos, Scanner.STRUCT);
|
|
|
|
t.list = AST.NewList();
|
|
|
|
t.list.Add(AST.NewTypeExpr(typ));
|
2008-10-15 18:06:28 -06:00
|
|
|
}
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
P.Ecart();
|
2008-10-15 18:06:28 -06:00
|
|
|
return t;
|
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)
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseFunctionType() *AST.Type {
|
2008-09-19 13:12:28 -06:00
|
|
|
P.Trace("FunctionType");
|
2008-09-18 17:58:37 -06:00
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
t := AST.NewType(P.pos, Scanner.LPAREN);
|
2008-10-24 15:04:54 -06:00
|
|
|
t.list = P.ParseParameters(true).list; // TODO find better solution
|
2008-10-15 18:06:28 -06:00
|
|
|
t.elt = P.ParseResult();
|
2008-09-18 17:58:37 -06:00
|
|
|
|
|
|
|
P.Ecart();
|
2008-10-14 19:14:01 -06:00
|
|
|
return t;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseMethodSpec(list *AST.List) {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Trace("MethodDecl");
|
|
|
|
|
2008-10-16 15:25:23 -06:00
|
|
|
list.Add(P.ParseIdent());
|
2008-10-20 16:03:40 -06:00
|
|
|
list.Add(AST.NewTypeExpr(P.ParseFunctionType()));
|
2008-09-18 17:58:37 -06:00
|
|
|
|
|
|
|
P.Ecart();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseInterfaceType() *AST.Type {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Trace("InterfaceType");
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
t := AST.NewType(P.pos, Scanner.INTERFACE);
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Expect(Scanner.INTERFACE);
|
2008-10-07 18:57:19 -06:00
|
|
|
if P.tok == Scanner.LBRACE {
|
|
|
|
P.Next();
|
2008-10-20 16:03:40 -06:00
|
|
|
t.list = AST.NewList();
|
2008-10-07 18:57:19 -06:00
|
|
|
for P.tok == Scanner.IDENT {
|
2008-10-16 15:25:23 -06:00
|
|
|
P.ParseMethodSpec(t.list);
|
2008-10-07 18:57:19 -06:00
|
|
|
if P.tok != Scanner.RBRACE {
|
|
|
|
P.Expect(Scanner.SEMICOLON);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
P.Expect(Scanner.RBRACE);
|
|
|
|
}
|
|
|
|
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Ecart();
|
2008-10-14 19:14:01 -06:00
|
|
|
return t;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseMapType() *AST.Type {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Trace("MapType");
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
t := AST.NewType(P.pos, Scanner.MAP);
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Expect(Scanner.MAP);
|
|
|
|
P.Expect(Scanner.LBRACK);
|
2008-10-14 19:14:01 -06:00
|
|
|
t.key = P.ParseVarType();
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Expect(Scanner.RBRACK);
|
2008-10-14 19:14:01 -06:00
|
|
|
t.elt = P.ParseVarType();
|
2008-09-18 17:58:37 -06:00
|
|
|
|
|
|
|
P.Ecart();
|
2008-10-14 19:14:01 -06:00
|
|
|
return t;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseStructType() *AST.Type {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Trace("StructType");
|
2008-09-25 12:50:34 -06:00
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
t := AST.NewType(P.pos, Scanner.STRUCT);
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Expect(Scanner.STRUCT);
|
2008-10-07 18:57:19 -06:00
|
|
|
if P.tok == Scanner.LBRACE {
|
|
|
|
P.Next();
|
2008-10-20 16:03:40 -06:00
|
|
|
t.list = AST.NewList();
|
2008-10-07 18:57:19 -06:00
|
|
|
for P.tok == Scanner.IDENT {
|
2008-10-24 15:04:54 -06:00
|
|
|
P.ParseVarDeclList(t.list, false);
|
2008-10-07 18:57:19 -06:00
|
|
|
if P.tok != Scanner.RBRACE {
|
|
|
|
P.Expect(Scanner.SEMICOLON);
|
|
|
|
}
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2008-10-09 19:03:16 -06:00
|
|
|
P.OptSemicolon();
|
2008-10-07 18:57:19 -06:00
|
|
|
P.Expect(Scanner.RBRACE);
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2008-10-07 18:57:19 -06:00
|
|
|
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Ecart();
|
2008-10-14 19:14:01 -06:00
|
|
|
return t;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParsePointerType() *AST.Type {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Trace("PointerType");
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
t := AST.NewType(P.pos, Scanner.MUL);
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Expect(Scanner.MUL);
|
2008-10-17 00:30:42 -06:00
|
|
|
t.elt = P.ParseType();
|
2008-09-18 17:58:37 -06:00
|
|
|
|
|
|
|
P.Ecart();
|
2008-10-17 00:30:42 -06:00
|
|
|
return t;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) TryType() *AST.Type {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Trace("Type (try)");
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
t := AST.BadType;
|
2008-09-18 17:58:37 -06:00
|
|
|
switch P.tok {
|
2008-10-14 19:14:01 -06:00
|
|
|
case Scanner.IDENT: t = P.ParseTypeName();
|
|
|
|
case Scanner.LBRACK: t = P.ParseArrayType();
|
|
|
|
case Scanner.CHAN, Scanner.ARROW: t = P.ParseChannelType();
|
|
|
|
case Scanner.INTERFACE: t = P.ParseInterfaceType();
|
|
|
|
case Scanner.LPAREN: t = P.ParseFunctionType();
|
|
|
|
case Scanner.MAP: t = P.ParseMapType();
|
|
|
|
case Scanner.STRUCT: t = P.ParseStructType();
|
|
|
|
case Scanner.MUL: t = P.ParsePointerType();
|
2008-10-17 17:19:31 -06:00
|
|
|
default: t = nil; // no type found
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
P.Ecart();
|
2008-10-14 19:14:01 -06:00
|
|
|
return t;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Blocks
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseStatementList() *AST.List {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Trace("StatementList");
|
2008-09-23 17:40:12 -06:00
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
list := AST.NewList();
|
2008-10-09 19:03:16 -06:00
|
|
|
for P.tok != Scanner.CASE && P.tok != Scanner.DEFAULT && P.tok != Scanner.RBRACE && P.tok != Scanner.EOF {
|
2008-10-17 17:19:31 -06:00
|
|
|
s := P.ParseStatement();
|
|
|
|
if s != nil {
|
|
|
|
// not the empty statement
|
|
|
|
list.Add(s);
|
|
|
|
}
|
2008-10-09 19:03:16 -06:00
|
|
|
if P.tok == Scanner.SEMICOLON {
|
|
|
|
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 {
|
|
|
|
break;
|
|
|
|
}
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2008-09-23 17:40:12 -06:00
|
|
|
|
2008-10-17 00:30:42 -06:00
|
|
|
// Try to provide a good error message
|
|
|
|
if P.tok != Scanner.CASE && P.tok != Scanner.DEFAULT && P.tok != Scanner.RBRACE && P.tok != Scanner.EOF {
|
|
|
|
P.Error(P.pos, "expected end of statement list (semicolon missing?)");
|
|
|
|
}
|
|
|
|
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Ecart();
|
2008-10-14 19:14:01 -06:00
|
|
|
return list;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseBlock() *AST.List {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Trace("Block");
|
|
|
|
|
|
|
|
P.Expect(Scanner.LBRACE);
|
2008-10-18 17:42:25 -06:00
|
|
|
s := P.ParseStatementList();
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Expect(Scanner.RBRACE);
|
2008-10-10 17:03:32 -06:00
|
|
|
P.opt_semi = true;
|
2008-09-18 17:58:37 -06:00
|
|
|
|
|
|
|
P.Ecart();
|
2008-10-14 19:14:01 -06:00
|
|
|
return s;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Expressions
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseExpressionList() *AST.Expr {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Trace("ExpressionList");
|
2008-09-22 19:26:12 -06:00
|
|
|
|
2008-10-18 21:20:30 -06:00
|
|
|
x := P.ParseExpression(1);
|
2008-10-20 16:03:40 -06:00
|
|
|
for first := true; P.tok == Scanner.COMMA; {
|
2008-10-14 19:14:01 -06:00
|
|
|
pos := P.pos;
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Next();
|
2008-10-20 16:03:40 -06:00
|
|
|
y := P.ParseExpression(1);
|
|
|
|
if first {
|
|
|
|
x = P.NewExpr(pos, Scanner.COMMA, x, y);
|
|
|
|
first = false;
|
|
|
|
} else {
|
|
|
|
x.y = P.NewExpr(pos, Scanner.COMMA, x.y, y);
|
|
|
|
}
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2008-10-20 16:03:40 -06:00
|
|
|
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Ecart();
|
2008-10-14 19:14:01 -06:00
|
|
|
return x;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseFunctionLit() *AST.Expr {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Trace("FunctionLit");
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
x := AST.NewLit(P.pos, Scanner.FUNC, "");
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Expect(Scanner.FUNC);
|
2008-10-17 17:19:31 -06:00
|
|
|
x.t = P.ParseFunctionType();
|
2008-10-23 18:56:54 -06:00
|
|
|
P.expr_lev++;
|
2008-10-10 17:03:32 -06:00
|
|
|
P.scope_lev++;
|
2008-10-17 17:19:31 -06:00
|
|
|
x.block = P.ParseBlock();
|
2008-10-10 17:03:32 -06:00
|
|
|
P.scope_lev--;
|
2008-10-23 18:56:54 -06:00
|
|
|
P.expr_lev--;
|
2008-09-18 17:58:37 -06:00
|
|
|
|
|
|
|
P.Ecart();
|
2008-10-17 17:19:31 -06:00
|
|
|
return x;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-23 18:56:54 -06:00
|
|
|
/*
|
|
|
|
func (P *Parser) ParseNewCall() *AST.Expr {
|
|
|
|
P.Trace("NewCall");
|
|
|
|
|
|
|
|
x := AST.NewExpr(P.pos, Scanner.NEW, nil, nil);
|
|
|
|
P.Next();
|
|
|
|
P.Expect(Scanner.LPAREN);
|
|
|
|
P.expr_lev++;
|
|
|
|
x.t = P.ParseType();
|
|
|
|
if P.tok == Scanner.COMMA {
|
|
|
|
P.Next();
|
|
|
|
x.y = P.ParseExpressionList();
|
|
|
|
}
|
|
|
|
P.expr_lev--;
|
|
|
|
P.Expect(Scanner.RPAREN);
|
|
|
|
|
|
|
|
P.Ecart();
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseOperand() *AST.Expr {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Trace("Operand");
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
x := AST.BadExpr;
|
2008-09-23 17:40:12 -06:00
|
|
|
switch P.tok {
|
|
|
|
case Scanner.IDENT:
|
2008-10-14 19:14:01 -06:00
|
|
|
x = P.ParseIdent();
|
2008-09-23 17:40:12 -06:00
|
|
|
|
|
|
|
case Scanner.LPAREN:
|
2008-10-17 17:19:31 -06:00
|
|
|
// TODO we could have a function type here as in: new(*())
|
|
|
|
// (currently not working)
|
2008-09-23 17:40:12 -06:00
|
|
|
P.Next();
|
2008-10-10 17:03:32 -06:00
|
|
|
P.expr_lev++;
|
2008-10-18 21:20:30 -06:00
|
|
|
x = P.ParseExpression(1);
|
2008-10-10 17:03:32 -06:00
|
|
|
P.expr_lev--;
|
2008-09-23 17:40:12 -06:00
|
|
|
P.Expect(Scanner.RPAREN);
|
2008-09-18 17:58:37 -06:00
|
|
|
|
2008-10-15 12:48:18 -06:00
|
|
|
case Scanner.INT, Scanner.FLOAT, Scanner.STRING:
|
2008-10-20 16:03:40 -06:00
|
|
|
x = AST.NewLit(P.pos, P.tok, P.val);
|
2008-09-23 17:40:12 -06:00
|
|
|
P.Next();
|
2008-10-15 12:48:18 -06:00
|
|
|
if x.tok == Scanner.STRING {
|
|
|
|
for ; P.tok == Scanner.STRING; P.Next() {
|
|
|
|
x.s += P.val;
|
|
|
|
}
|
2008-10-10 17:03:32 -06:00
|
|
|
}
|
|
|
|
|
2008-09-23 17:40:12 -06:00
|
|
|
case Scanner.FUNC:
|
2008-10-15 18:06:28 -06:00
|
|
|
x = P.ParseFunctionLit();
|
2008-10-23 18:56:54 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
case Scanner.NEW:
|
|
|
|
x = P.ParseNewCall();
|
|
|
|
*/
|
|
|
|
|
2008-09-23 17:40:12 -06:00
|
|
|
default:
|
2008-10-15 18:06:28 -06:00
|
|
|
t := P.TryType();
|
|
|
|
if t != nil {
|
2008-10-20 16:03:40 -06:00
|
|
|
x = AST.NewTypeExpr(t);
|
2008-10-15 18:06:28 -06:00
|
|
|
} else {
|
|
|
|
P.Error(P.pos, "operand expected");
|
|
|
|
P.Next(); // make progress
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
P.Ecart();
|
2008-10-14 19:14:01 -06:00
|
|
|
return x;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseSelectorOrTypeGuard(x *AST.Expr) *AST.Expr {
|
2008-09-19 13:12:28 -06:00
|
|
|
P.Trace("SelectorOrTypeGuard");
|
2008-09-18 17:58:37 -06:00
|
|
|
|
2008-10-17 00:30:42 -06:00
|
|
|
x = P.NewExpr(P.pos, Scanner.PERIOD, x, nil);
|
2008-09-22 19:26:12 -06:00
|
|
|
P.Expect(Scanner.PERIOD);
|
2008-09-18 17:58:37 -06:00
|
|
|
|
2008-09-22 19:26:12 -06:00
|
|
|
if P.tok == Scanner.IDENT {
|
2008-10-16 15:25:23 -06:00
|
|
|
x.y = P.ParseIdent();
|
2008-09-22 19:26:12 -06:00
|
|
|
|
2008-09-18 17:58:37 -06:00
|
|
|
} else {
|
|
|
|
P.Expect(Scanner.LPAREN);
|
2008-10-16 15:25:23 -06:00
|
|
|
x.t = P.ParseType();
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Expect(Scanner.RPAREN);
|
|
|
|
}
|
|
|
|
|
|
|
|
P.Ecart();
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseIndex(x *AST.Expr) *AST.Expr {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Trace("IndexOrSlice");
|
|
|
|
|
2008-09-22 19:26:12 -06:00
|
|
|
pos := P.pos;
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Expect(Scanner.LBRACK);
|
2008-10-23 18:56:54 -06:00
|
|
|
P.expr_lev++;
|
2008-10-18 21:20:30 -06:00
|
|
|
i := P.ParseExpression(0);
|
2008-10-23 18:56:54 -06:00
|
|
|
P.expr_lev--;
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Expect(Scanner.RBRACK);
|
2008-09-22 19:26:12 -06:00
|
|
|
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Ecart();
|
2008-10-17 00:30:42 -06:00
|
|
|
return P.NewExpr(pos, Scanner.LBRACK, x, i);
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseBinaryExpr(prec1 int) *AST.Expr
|
2008-10-17 00:30:42 -06:00
|
|
|
|
2008-10-23 18:56:54 -06:00
|
|
|
func (P *Parser) ParseCall(x0 *AST.Expr) *AST.Expr {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Trace("Call");
|
|
|
|
|
2008-10-23 18:56:54 -06:00
|
|
|
x := P.NewExpr(P.pos, Scanner.LPAREN, x0, nil);
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Expect(Scanner.LPAREN);
|
|
|
|
if P.tok != Scanner.RPAREN {
|
2008-10-23 18:56:54 -06:00
|
|
|
P.expr_lev++;
|
|
|
|
var t *AST.Type;
|
|
|
|
if x0.tok == Scanner.IDENT && x0.s == "new" {
|
|
|
|
// heuristic: assume it's a new(T, ...) call, try to parse a type
|
|
|
|
t = P.TryType();
|
|
|
|
}
|
|
|
|
if t != nil {
|
|
|
|
// we found a type
|
|
|
|
x.y = AST.NewTypeExpr(t);
|
|
|
|
if P.tok == Scanner.COMMA {
|
|
|
|
pos := P.pos;
|
|
|
|
P.Next();
|
|
|
|
y := P.ParseExpressionList();
|
|
|
|
// create list manually because NewExpr checks for type expressions
|
|
|
|
z := AST.NewExpr(pos, Scanner.COMMA, nil, y);
|
|
|
|
z.x = x.y;
|
|
|
|
x.y = z;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// normal argument list
|
|
|
|
x.y = P.ParseExpressionList();
|
2008-10-17 00:30:42 -06:00
|
|
|
}
|
2008-10-23 18:56:54 -06:00
|
|
|
P.expr_lev--;
|
2008-10-10 17:03:32 -06:00
|
|
|
}
|
|
|
|
P.Expect(Scanner.RPAREN);
|
|
|
|
|
|
|
|
P.Ecart();
|
2008-10-14 19:14:01 -06:00
|
|
|
return x;
|
2008-10-10 17:03:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 17:44:03 -06:00
|
|
|
func (P *Parser) ParseCompositeElements() *AST.Expr {
|
2008-10-18 21:20:30 -06:00
|
|
|
x := P.ParseExpression(0);
|
2008-10-16 13:16:50 -06:00
|
|
|
if P.tok == Scanner.COMMA {
|
|
|
|
pos := P.pos;
|
|
|
|
P.Next();
|
2008-10-20 16:03:40 -06:00
|
|
|
|
|
|
|
// first element determines mode
|
|
|
|
singles := true;
|
|
|
|
if x.tok == Scanner.COLON {
|
|
|
|
singles = false;
|
2008-10-16 13:16:50 -06:00
|
|
|
}
|
2008-10-20 16:03:40 -06:00
|
|
|
|
|
|
|
for first := true; P.tok != Scanner.RBRACE && P.tok != Scanner.EOF; {
|
|
|
|
y := P.ParseExpression(0);
|
2008-10-18 21:20:30 -06:00
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
if singles {
|
|
|
|
if y.tok == Scanner.COLON {
|
|
|
|
P.Error(y.x.pos, "single value expected; found pair");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if y.tok != Scanner.COLON {
|
|
|
|
P.Error(y.pos, "key:value pair expected; found single value");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if first {
|
|
|
|
x = P.NewExpr(pos, Scanner.COMMA, x, y);
|
|
|
|
} else {
|
|
|
|
x.y = P.NewExpr(pos, Scanner.COMMA, x.y, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
if P.tok == Scanner.COMMA {
|
|
|
|
pos = P.pos;
|
|
|
|
P.Next();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2008-10-16 13:16:50 -06:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseCompositeLit(t *AST.Type) *AST.Expr {
|
2008-10-10 17:03:32 -06:00
|
|
|
P.Trace("CompositeLit");
|
2008-10-17 00:30:42 -06:00
|
|
|
|
2008-10-17 17:19:31 -06:00
|
|
|
x := P.NewExpr(P.pos, Scanner.LBRACE, nil, nil);
|
2008-10-17 00:30:42 -06:00
|
|
|
x.t = t;
|
2008-10-17 17:19:31 -06:00
|
|
|
P.Expect(Scanner.LBRACE);
|
|
|
|
if P.tok != Scanner.RBRACE {
|
2008-10-20 17:44:03 -06:00
|
|
|
x.y = P.ParseCompositeElements();
|
2008-10-17 17:19:31 -06:00
|
|
|
}
|
2008-10-10 17:03:32 -06:00
|
|
|
P.Expect(Scanner.RBRACE);
|
2008-10-14 19:14:01 -06:00
|
|
|
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Ecart();
|
2008-10-17 00:30:42 -06:00
|
|
|
return x;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParsePrimaryExpr() *AST.Expr {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Trace("PrimaryExpr");
|
|
|
|
|
2008-09-23 17:40:12 -06:00
|
|
|
x := P.ParseOperand();
|
2008-10-09 19:03:16 -06:00
|
|
|
for {
|
2008-09-18 17:58:37 -06:00
|
|
|
switch P.tok {
|
2008-09-19 13:12:28 -06:00
|
|
|
case Scanner.PERIOD: x = P.ParseSelectorOrTypeGuard(x);
|
2008-10-14 19:14:01 -06:00
|
|
|
case Scanner.LBRACK: x = P.ParseIndex(x);
|
2008-09-18 17:58:37 -06:00
|
|
|
case Scanner.LPAREN: x = P.ParseCall(x);
|
2008-10-10 17:03:32 -06:00
|
|
|
case Scanner.LBRACE:
|
2008-10-17 00:30:42 -06:00
|
|
|
// assume a composite literal only if x could be a type
|
2008-10-23 18:56:54 -06:00
|
|
|
// and if we are not inside control clause (expr_lev >= 0)
|
2008-10-17 00:30:42 -06:00
|
|
|
// (composites inside control clauses must be parenthesized)
|
2008-10-20 16:03:40 -06:00
|
|
|
var t *AST.Type;
|
2008-10-23 18:56:54 -06:00
|
|
|
if P.expr_lev >= 0 {
|
2008-10-17 17:19:31 -06:00
|
|
|
t = ExprType(x);
|
2008-10-17 00:30:42 -06:00
|
|
|
}
|
|
|
|
if t != nil {
|
2008-10-15 18:06:28 -06:00
|
|
|
x = P.ParseCompositeLit(t);
|
2008-10-10 17:03:32 -06:00
|
|
|
} else {
|
|
|
|
goto exit;
|
|
|
|
}
|
2008-10-09 19:03:16 -06:00
|
|
|
default: goto exit;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
}
|
2008-10-17 17:19:31 -06:00
|
|
|
|
2008-10-09 19:03:16 -06:00
|
|
|
exit:
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Ecart();
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseUnaryExpr() *AST.Expr {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Trace("UnaryExpr");
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
x := AST.BadExpr;
|
2008-09-18 17:58:37 -06:00
|
|
|
switch P.tok {
|
2008-10-17 00:30:42 -06:00
|
|
|
case Scanner.ADD, Scanner.SUB, Scanner.MUL, Scanner.NOT, Scanner.XOR, Scanner.ARROW, Scanner.AND:
|
|
|
|
pos, tok := P.pos, P.tok;
|
|
|
|
P.Next();
|
|
|
|
y := P.ParseUnaryExpr();
|
|
|
|
if tok == Scanner.MUL && y.tok == Scanner.TYPE {
|
|
|
|
// pointer type
|
2008-10-20 16:03:40 -06:00
|
|
|
t := AST.NewType(pos, Scanner.MUL);
|
2008-10-17 00:30:42 -06:00
|
|
|
t.elt = y.t;
|
2008-10-20 16:03:40 -06:00
|
|
|
x = AST.NewTypeExpr(t);
|
2008-10-17 00:30:42 -06:00
|
|
|
} else {
|
|
|
|
x = P.NewExpr(pos, tok, nil, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
x = P.ParsePrimaryExpr();
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
P.Ecart();
|
2008-09-22 19:26:12 -06:00
|
|
|
return x;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseBinaryExpr(prec1 int) *AST.Expr {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Trace("BinaryExpr");
|
|
|
|
|
2008-09-23 17:40:12 -06:00
|
|
|
x := P.ParseUnaryExpr();
|
2008-09-25 16:14:26 -06:00
|
|
|
for prec := Scanner.Precedence(P.tok); prec >= prec1; prec-- {
|
|
|
|
for Scanner.Precedence(P.tok) == prec {
|
2008-09-22 19:26:12 -06:00
|
|
|
pos, tok := P.pos, P.tok;
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Next();
|
2008-09-23 17:40:12 -06:00
|
|
|
y := P.ParseBinaryExpr(prec + 1);
|
2008-10-17 00:30:42 -06:00
|
|
|
x = P.NewExpr(pos, tok, x, y);
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
P.Ecart();
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseExpression(prec int) *AST.Expr {
|
2008-09-23 17:40:12 -06:00
|
|
|
P.Trace("Expression");
|
2008-09-18 17:58:37 -06:00
|
|
|
indent := P.indent;
|
2008-10-18 21:20:30 -06:00
|
|
|
|
|
|
|
if prec < 0 {
|
|
|
|
panic("precedence must be >= 0");
|
|
|
|
}
|
|
|
|
x := P.NoType(P.ParseBinaryExpr(prec));
|
2008-10-17 00:30:42 -06:00
|
|
|
|
2008-09-18 17:58:37 -06:00
|
|
|
if indent != P.indent {
|
|
|
|
panic("imbalanced tracing code (Expression)");
|
|
|
|
}
|
|
|
|
P.Ecart();
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Statements
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseSimpleStat() *AST.Stat {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Trace("SimpleStat");
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
s := AST.BadStat;
|
2008-10-15 12:48:18 -06:00
|
|
|
x := P.ParseExpressionList();
|
2008-09-18 17:58:37 -06:00
|
|
|
|
|
|
|
switch P.tok {
|
|
|
|
case Scanner.COLON:
|
|
|
|
// label declaration
|
2008-10-20 16:03:40 -06:00
|
|
|
s = AST.NewStat(P.pos, Scanner.COLON);
|
2008-10-16 13:16:50 -06:00
|
|
|
s.expr = x;
|
|
|
|
if x.len() != 1 {
|
|
|
|
P.Error(x.pos, "illegal label declaration");
|
2008-09-25 16:14:26 -06:00
|
|
|
}
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Next(); // consume ":"
|
2008-10-10 17:03:32 -06:00
|
|
|
P.opt_semi = true;
|
2008-09-18 17:58:37 -06:00
|
|
|
|
2008-09-23 17:40:12 -06:00
|
|
|
case
|
|
|
|
Scanner.DEFINE, Scanner.ASSIGN, Scanner.ADD_ASSIGN,
|
|
|
|
Scanner.SUB_ASSIGN, Scanner.MUL_ASSIGN, Scanner.QUO_ASSIGN,
|
|
|
|
Scanner.REM_ASSIGN, Scanner.AND_ASSIGN, Scanner.OR_ASSIGN,
|
|
|
|
Scanner.XOR_ASSIGN, Scanner.SHL_ASSIGN, Scanner.SHR_ASSIGN:
|
2008-10-20 11:01:34 -06:00
|
|
|
// assignment
|
|
|
|
pos, tok := P.pos, P.tok;
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Next();
|
2008-10-20 11:01:34 -06:00
|
|
|
y := P.ParseExpressionList();
|
|
|
|
if xl, yl := x.len(), y.len(); xl > 1 && yl > 1 && xl != yl {
|
2008-10-16 13:16:50 -06:00
|
|
|
P.Error(x.pos, "arity of lhs doesn't match rhs");
|
|
|
|
}
|
2008-10-20 16:03:40 -06:00
|
|
|
s = AST.NewStat(x.pos, Scanner.EXPRSTAT);
|
|
|
|
s.expr = AST.NewExpr(pos, tok, x, y);
|
2008-10-14 19:14:01 -06:00
|
|
|
|
2008-09-18 17:58:37 -06:00
|
|
|
default:
|
2008-10-16 13:16:50 -06:00
|
|
|
var pos, tok int;
|
2008-09-18 17:58:37 -06:00
|
|
|
if P.tok == Scanner.INC || P.tok == Scanner.DEC {
|
2008-10-16 13:16:50 -06:00
|
|
|
pos, tok = P.pos, P.tok;
|
|
|
|
P.Next();
|
2008-09-23 17:40:12 -06:00
|
|
|
} else {
|
2008-10-17 17:19:31 -06:00
|
|
|
pos, tok = x.pos, Scanner.EXPRSTAT;
|
2008-10-16 13:16:50 -06:00
|
|
|
}
|
2008-10-20 16:03:40 -06:00
|
|
|
s = AST.NewStat(pos, tok);
|
2008-10-16 13:16:50 -06:00
|
|
|
s.expr = x;
|
|
|
|
if x.len() != 1 {
|
|
|
|
P.Error(x.pos, "only one expression allowed");
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
P.Ecart();
|
2008-10-14 19:14:01 -06:00
|
|
|
return s;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseGoStat() *AST.Stat {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Trace("GoStat");
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
s := AST.NewStat(P.pos, Scanner.GO);
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Expect(Scanner.GO);
|
2008-10-18 21:20:30 -06:00
|
|
|
s.expr = P.ParseExpression(1);
|
2008-09-18 17:58:37 -06:00
|
|
|
|
|
|
|
P.Ecart();
|
2008-10-14 19:14:01 -06:00
|
|
|
return s;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseReturnStat() *AST.Stat {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Trace("ReturnStat");
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
s := AST.NewStat(P.pos, Scanner.RETURN);
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Expect(Scanner.RETURN);
|
|
|
|
if P.tok != Scanner.SEMICOLON && P.tok != Scanner.RBRACE {
|
2008-10-14 19:14:01 -06:00
|
|
|
s.expr = P.ParseExpressionList();
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
P.Ecart();
|
2008-10-14 19:14:01 -06:00
|
|
|
return s;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseControlFlowStat(tok int) *AST.Stat {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Trace("ControlFlowStat");
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
s := AST.NewStat(P.pos, tok);
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Expect(tok);
|
2008-10-16 11:16:59 -06:00
|
|
|
if tok != Scanner.FALLTHROUGH && P.tok == Scanner.IDENT {
|
2008-10-14 19:14:01 -06:00
|
|
|
s.expr = P.ParseIdent();
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
P.Ecart();
|
2008-10-14 19:14:01 -06:00
|
|
|
return s;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseControlClause(keyword int) *AST.Stat {
|
2008-10-15 12:48:18 -06:00
|
|
|
P.Trace("ControlClause");
|
2008-09-18 17:58:37 -06:00
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
s := AST.NewStat(P.pos, keyword);
|
2008-09-24 16:50:28 -06:00
|
|
|
P.Expect(keyword);
|
2008-09-18 17:58:37 -06:00
|
|
|
if P.tok != Scanner.LBRACE {
|
2008-10-10 17:03:32 -06:00
|
|
|
prev_lev := P.expr_lev;
|
2008-10-23 18:56:54 -06:00
|
|
|
P.expr_lev = -1;
|
2008-09-18 17:58:37 -06:00
|
|
|
if P.tok != Scanner.SEMICOLON {
|
2008-10-14 19:14:01 -06:00
|
|
|
s.init = P.ParseSimpleStat();
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
if P.tok == Scanner.SEMICOLON {
|
|
|
|
P.Next();
|
2008-09-25 12:50:34 -06:00
|
|
|
if P.tok != Scanner.SEMICOLON && P.tok != Scanner.LBRACE {
|
2008-10-18 21:20:30 -06:00
|
|
|
s.expr = P.ParseExpression(1);
|
2008-09-25 12:50:34 -06:00
|
|
|
}
|
2008-09-24 16:50:28 -06:00
|
|
|
if keyword == Scanner.FOR {
|
|
|
|
P.Expect(Scanner.SEMICOLON);
|
|
|
|
if P.tok != Scanner.LBRACE {
|
2008-10-14 19:14:01 -06:00
|
|
|
s.post = P.ParseSimpleStat();
|
2008-09-24 16:50:28 -06:00
|
|
|
}
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2008-09-25 12:50:34 -06:00
|
|
|
} else {
|
2008-10-14 19:14:01 -06:00
|
|
|
if s.init != nil { // guard in case of errors
|
|
|
|
s.expr, s.init = s.init.expr, nil;
|
|
|
|
}
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2008-10-10 17:03:32 -06:00
|
|
|
P.expr_lev = prev_lev;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2008-09-24 16:50:28 -06:00
|
|
|
|
|
|
|
P.Ecart();
|
2008-10-14 19:14:01 -06:00
|
|
|
return s;
|
2008-09-24 16:50:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseIfStat() *AST.Stat {
|
2008-09-24 16:50:28 -06:00
|
|
|
P.Trace("IfStat");
|
|
|
|
|
2008-10-14 19:14:01 -06:00
|
|
|
s := P.ParseControlClause(Scanner.IF);
|
|
|
|
s.block = P.ParseBlock();
|
2008-09-18 17:58:37 -06:00
|
|
|
if P.tok == Scanner.ELSE {
|
|
|
|
P.Next();
|
2008-10-20 16:03:40 -06:00
|
|
|
s1 := AST.BadStat;
|
2008-10-18 17:42:25 -06:00
|
|
|
if P.sixg {
|
|
|
|
s1 = P.ParseStatement();
|
2008-10-17 17:19:31 -06:00
|
|
|
if s1 != nil {
|
|
|
|
// not the empty statement
|
|
|
|
if s1.tok != Scanner.LBRACE {
|
|
|
|
// wrap in a block if we don't have one
|
2008-10-20 16:03:40 -06:00
|
|
|
b := AST.NewStat(P.pos, Scanner.LBRACE);
|
|
|
|
b.block = AST.NewList();
|
2008-10-17 17:19:31 -06:00
|
|
|
b.block.Add(s1);
|
|
|
|
s1 = b;
|
|
|
|
}
|
|
|
|
s.post = s1;
|
2008-10-16 11:16:59 -06:00
|
|
|
}
|
2008-10-18 17:42:25 -06:00
|
|
|
} else if P.tok == Scanner.IF {
|
|
|
|
s1 = P.ParseIfStat();
|
|
|
|
} else {
|
2008-10-20 16:03:40 -06:00
|
|
|
s1 = AST.NewStat(P.pos, Scanner.LBRACE);
|
2008-10-18 17:42:25 -06:00
|
|
|
s1.block = P.ParseBlock();
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2008-10-18 17:42:25 -06:00
|
|
|
s.post = s1;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
P.Ecart();
|
2008-10-14 19:14:01 -06:00
|
|
|
return s;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseForStat() *AST.Stat {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Trace("ForStat");
|
|
|
|
|
2008-10-14 19:14:01 -06:00
|
|
|
s := P.ParseControlClause(Scanner.FOR);
|
|
|
|
s.block = P.ParseBlock();
|
2008-09-18 17:58:37 -06:00
|
|
|
|
|
|
|
P.Ecart();
|
2008-10-14 19:14:01 -06:00
|
|
|
return s;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseCase() *AST.Stat {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Trace("Case");
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
s := AST.NewStat(P.pos, P.tok);
|
2008-09-18 17:58:37 -06:00
|
|
|
if P.tok == Scanner.CASE {
|
|
|
|
P.Next();
|
2008-10-14 19:14:01 -06:00
|
|
|
s.expr = P.ParseExpressionList();
|
2008-09-18 17:58:37 -06:00
|
|
|
} else {
|
|
|
|
P.Expect(Scanner.DEFAULT);
|
|
|
|
}
|
|
|
|
P.Expect(Scanner.COLON);
|
|
|
|
|
|
|
|
P.Ecart();
|
2008-10-14 19:14:01 -06:00
|
|
|
return s;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseCaseClause() *AST.Stat {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Trace("CaseClause");
|
2008-09-24 16:50:28 -06:00
|
|
|
|
2008-10-14 19:14:01 -06:00
|
|
|
s := P.ParseCase();
|
2008-10-09 19:03:16 -06:00
|
|
|
if P.tok != Scanner.CASE && P.tok != Scanner.DEFAULT && P.tok != Scanner.RBRACE {
|
2008-10-14 19:14:01 -06:00
|
|
|
s.block = P.ParseStatementList();
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
P.Ecart();
|
2008-10-14 19:14:01 -06:00
|
|
|
return s;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseSwitchStat() *AST.Stat {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Trace("SwitchStat");
|
|
|
|
|
2008-10-14 19:14:01 -06:00
|
|
|
s := P.ParseControlClause(Scanner.SWITCH);
|
2008-10-20 16:03:40 -06:00
|
|
|
s.block = AST.NewList();
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Expect(Scanner.LBRACE);
|
2008-10-09 19:03:16 -06:00
|
|
|
for P.tok != Scanner.RBRACE && P.tok != Scanner.EOF {
|
2008-10-14 19:14:01 -06:00
|
|
|
s.block.Add(P.ParseCaseClause());
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
P.Expect(Scanner.RBRACE);
|
2008-10-10 17:03:32 -06:00
|
|
|
P.opt_semi = true;
|
2008-09-24 16:50:28 -06:00
|
|
|
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Ecart();
|
2008-10-14 19:14:01 -06:00
|
|
|
return s;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseCommCase() *AST.Stat {
|
2008-10-14 19:14:01 -06:00
|
|
|
P.Trace("CommCase");
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
s := AST.NewStat(P.pos, Scanner.CASE);
|
2008-10-14 19:14:01 -06:00
|
|
|
if P.tok == Scanner.CASE {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Next();
|
2008-10-23 18:56:54 -06:00
|
|
|
x := P.ParseExpression(1);
|
2008-10-14 19:14:01 -06:00
|
|
|
if P.tok == Scanner.ASSIGN || P.tok == Scanner.DEFINE {
|
2008-10-23 18:56:54 -06:00
|
|
|
pos, tok := P.pos, P.tok;
|
2008-10-14 19:14:01 -06:00
|
|
|
P.Next();
|
2008-10-26 22:32:30 -06:00
|
|
|
if P.tok == Scanner.ARROW {
|
|
|
|
y := P.ParseExpression(1);
|
|
|
|
x = AST.NewExpr(pos, tok, x, y);
|
|
|
|
} else {
|
|
|
|
P.Expect(Scanner.ARROW); // use Expect() error handling
|
|
|
|
}
|
2008-10-14 19:14:01 -06:00
|
|
|
}
|
2008-10-23 18:56:54 -06:00
|
|
|
s.expr = x;
|
2008-10-14 19:14:01 -06:00
|
|
|
} else {
|
|
|
|
P.Expect(Scanner.DEFAULT);
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2008-10-14 19:14:01 -06:00
|
|
|
P.Expect(Scanner.COLON);
|
|
|
|
|
|
|
|
P.Ecart();
|
|
|
|
return s;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseCommClause() *AST.Stat {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Trace("CommClause");
|
|
|
|
|
2008-10-14 19:14:01 -06:00
|
|
|
s := P.ParseCommCase();
|
2008-09-18 17:58:37 -06:00
|
|
|
if P.tok != Scanner.CASE && P.tok != Scanner.DEFAULT && P.tok != Scanner.RBRACE {
|
2008-10-14 19:14:01 -06:00
|
|
|
s.block = P.ParseStatementList();
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
P.Ecart();
|
2008-10-14 19:14:01 -06:00
|
|
|
return s;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseSelectStat() *AST.Stat {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Trace("SelectStat");
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
s := AST.NewStat(P.pos, Scanner.SELECT);
|
|
|
|
s.block = AST.NewList();
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Expect(Scanner.SELECT);
|
|
|
|
P.Expect(Scanner.LBRACE);
|
|
|
|
for P.tok != Scanner.RBRACE && P.tok != Scanner.EOF {
|
2008-10-14 19:14:01 -06:00
|
|
|
s.block.Add(P.ParseCommClause());
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2008-10-09 19:03:16 -06:00
|
|
|
P.Expect(Scanner.RBRACE);
|
2008-10-10 17:03:32 -06:00
|
|
|
P.opt_semi = true;
|
2008-09-18 17:58:37 -06:00
|
|
|
|
|
|
|
P.Ecart();
|
2008-10-14 19:14:01 -06:00
|
|
|
return s;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseRangeStat() *AST.Stat {
|
2008-10-09 19:03:16 -06:00
|
|
|
P.Trace("RangeStat");
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
s := AST.NewStat(P.pos, Scanner.RANGE);
|
2008-10-09 19:03:16 -06:00
|
|
|
P.Expect(Scanner.RANGE);
|
|
|
|
P.ParseIdentList();
|
|
|
|
P.Expect(Scanner.DEFINE);
|
2008-10-18 21:20:30 -06:00
|
|
|
s.expr = P.ParseExpression(1);
|
2008-10-14 19:14:01 -06:00
|
|
|
s.block = P.ParseBlock();
|
2008-10-09 19:03:16 -06:00
|
|
|
|
2008-10-14 19:14:01 -06:00
|
|
|
P.Ecart();
|
|
|
|
return s;
|
2008-10-09 19:03:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseStatement() *AST.Stat {
|
2008-10-09 19:03:16 -06:00
|
|
|
P.Trace("Statement");
|
2008-09-18 17:58:37 -06:00
|
|
|
indent := P.indent;
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
s := AST.BadStat;
|
2008-09-18 17:58:37 -06:00
|
|
|
switch P.tok {
|
2008-09-24 16:50:28 -06:00
|
|
|
case Scanner.CONST, Scanner.TYPE, Scanner.VAR:
|
2008-10-20 16:03:40 -06:00
|
|
|
s = AST.NewStat(P.pos, P.tok);
|
2008-10-14 19:14:01 -06:00
|
|
|
s.decl = P.ParseDeclaration();
|
2008-09-18 17:58:37 -06:00
|
|
|
case Scanner.FUNC:
|
2008-10-16 11:16:59 -06:00
|
|
|
// for now we do not allow local function declarations,
|
|
|
|
// instead we assume this starts a function literal
|
2008-09-18 17:58:37 -06:00
|
|
|
fallthrough;
|
2008-10-10 17:03:32 -06:00
|
|
|
case
|
|
|
|
// only the tokens that are legal top-level expression starts
|
|
|
|
Scanner.IDENT, Scanner.INT, Scanner.FLOAT, Scanner.STRING, Scanner.LPAREN, // operand
|
|
|
|
Scanner.LBRACK, Scanner.STRUCT, // composite type
|
|
|
|
Scanner.MUL, Scanner.AND, Scanner.ARROW: // unary
|
2008-10-14 19:14:01 -06:00
|
|
|
s = P.ParseSimpleStat();
|
2008-09-18 17:58:37 -06:00
|
|
|
case Scanner.GO:
|
2008-10-14 19:14:01 -06:00
|
|
|
s = P.ParseGoStat();
|
2008-09-18 17:58:37 -06:00
|
|
|
case Scanner.RETURN:
|
2008-10-14 19:14:01 -06:00
|
|
|
s = P.ParseReturnStat();
|
2008-10-16 11:16:59 -06:00
|
|
|
case Scanner.BREAK, Scanner.CONTINUE, Scanner.GOTO, Scanner.FALLTHROUGH:
|
2008-10-14 19:14:01 -06:00
|
|
|
s = P.ParseControlFlowStat(P.tok);
|
2008-09-18 17:58:37 -06:00
|
|
|
case Scanner.LBRACE:
|
2008-10-20 16:03:40 -06:00
|
|
|
s = AST.NewStat(P.pos, Scanner.LBRACE);
|
2008-10-14 19:14:01 -06:00
|
|
|
s.block = P.ParseBlock();
|
2008-09-18 17:58:37 -06:00
|
|
|
case Scanner.IF:
|
2008-10-14 19:14:01 -06:00
|
|
|
s = P.ParseIfStat();
|
2008-09-18 17:58:37 -06:00
|
|
|
case Scanner.FOR:
|
2008-10-14 19:14:01 -06:00
|
|
|
s = P.ParseForStat();
|
2008-09-18 17:58:37 -06:00
|
|
|
case Scanner.SWITCH:
|
2008-10-14 19:14:01 -06:00
|
|
|
s = P.ParseSwitchStat();
|
2008-09-18 17:58:37 -06:00
|
|
|
case Scanner.RANGE:
|
2008-10-14 19:14:01 -06:00
|
|
|
s = P.ParseRangeStat();
|
2008-09-18 17:58:37 -06:00
|
|
|
case Scanner.SELECT:
|
2008-10-14 19:14:01 -06:00
|
|
|
s = P.ParseSelectStat();
|
2008-09-18 17:58:37 -06:00
|
|
|
default:
|
2008-10-17 17:19:31 -06:00
|
|
|
// empty statement
|
|
|
|
s = nil;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if indent != P.indent {
|
|
|
|
panic("imbalanced tracing code (Statement)");
|
|
|
|
}
|
|
|
|
P.Ecart();
|
2008-10-14 19:14:01 -06:00
|
|
|
return s;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Declarations
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseImportSpec() *AST.Decl {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Trace("ImportSpec");
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
d := AST.NewDecl(P.pos, Scanner.IMPORT, false);
|
2008-09-18 17:58:37 -06:00
|
|
|
if P.tok == Scanner.PERIOD {
|
|
|
|
P.Error(P.pos, `"import ." not yet handled properly`);
|
|
|
|
P.Next();
|
|
|
|
} else if P.tok == Scanner.IDENT {
|
2008-10-14 19:14:01 -06:00
|
|
|
d.ident = P.ParseIdent();
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if P.tok == Scanner.STRING {
|
|
|
|
// TODO eventually the scanner should strip the quotes
|
2008-10-20 16:03:40 -06:00
|
|
|
d.val = AST.NewLit(P.pos, Scanner.STRING, P.val);
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Next();
|
|
|
|
} else {
|
|
|
|
P.Expect(Scanner.STRING); // use Expect() error handling
|
|
|
|
}
|
|
|
|
|
|
|
|
P.Ecart();
|
2008-10-14 19:14:01 -06:00
|
|
|
return d;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseConstSpec(exported bool) *AST.Decl {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Trace("ConstSpec");
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
d := AST.NewDecl(P.pos, Scanner.CONST, exported);
|
2008-10-14 19:14:01 -06:00
|
|
|
d.ident = P.ParseIdent();
|
|
|
|
d.typ = P.TryType();
|
2008-09-18 17:58:37 -06:00
|
|
|
if P.tok == Scanner.ASSIGN {
|
|
|
|
P.Next();
|
2008-10-18 21:20:30 -06:00
|
|
|
d.val = P.ParseExpression(1);
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
P.Ecart();
|
2008-10-14 19:14:01 -06:00
|
|
|
return d;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseTypeSpec(exported bool) *AST.Decl {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Trace("TypeSpec");
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
d := AST.NewDecl(P.pos, Scanner.TYPE, exported);
|
2008-10-14 19:14:01 -06:00
|
|
|
d.ident = P.ParseIdent();
|
|
|
|
d.typ = P.ParseType();
|
2008-10-10 17:03:32 -06:00
|
|
|
P.opt_semi = true;
|
2008-09-18 17:58:37 -06:00
|
|
|
|
|
|
|
P.Ecart();
|
2008-10-14 19:14:01 -06:00
|
|
|
return d;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseVarSpec(exported bool) *AST.Decl {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Trace("VarSpec");
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
d := AST.NewDecl(P.pos, Scanner.VAR, exported);
|
2008-10-16 13:16:50 -06:00
|
|
|
d.ident = P.ParseIdentList();
|
2008-09-18 17:58:37 -06:00
|
|
|
if P.tok == Scanner.ASSIGN {
|
|
|
|
P.Next();
|
2008-10-16 13:16:50 -06:00
|
|
|
d.val = P.ParseExpressionList();
|
2008-09-18 17:58:37 -06:00
|
|
|
} else {
|
2008-10-16 13:16:50 -06:00
|
|
|
d.typ = P.ParseVarType();
|
2008-09-18 17:58:37 -06:00
|
|
|
if P.tok == Scanner.ASSIGN {
|
|
|
|
P.Next();
|
2008-10-16 13:16:50 -06:00
|
|
|
d.val = P.ParseExpressionList();
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
P.Ecart();
|
2008-10-14 19:14:01 -06:00
|
|
|
return d;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-17 17:19:31 -06:00
|
|
|
// TODO replace this by using function pointers derived from methods
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseSpec(exported bool, keyword int) *AST.Decl {
|
2008-09-18 17:58:37 -06:00
|
|
|
switch keyword {
|
2008-09-25 16:14:26 -06:00
|
|
|
case Scanner.IMPORT: return P.ParseImportSpec();
|
|
|
|
case Scanner.CONST: return P.ParseConstSpec(exported);
|
|
|
|
case Scanner.TYPE: return P.ParseTypeSpec(exported);
|
|
|
|
case Scanner.VAR: return P.ParseVarSpec(exported);
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2008-09-25 16:14:26 -06:00
|
|
|
panic("UNREACHABLE");
|
2008-10-14 19:14:01 -06:00
|
|
|
return nil;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseDecl(exported bool, keyword int) *AST.Decl {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Trace("Decl");
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
d := AST.BadDecl;
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Expect(keyword);
|
|
|
|
if P.tok == Scanner.LPAREN {
|
|
|
|
P.Next();
|
2008-10-20 16:03:40 -06:00
|
|
|
d = AST.NewDecl(P.pos, keyword, exported);
|
|
|
|
d.list = AST.NewList();
|
2008-10-09 19:03:16 -06:00
|
|
|
for P.tok != Scanner.RPAREN && P.tok != Scanner.EOF {
|
2008-10-14 19:14:01 -06:00
|
|
|
d.list.Add(P.ParseSpec(exported, keyword));
|
2008-10-09 19:03:16 -06:00
|
|
|
if P.tok == Scanner.SEMICOLON {
|
|
|
|
P.Next();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
2008-10-09 19:03:16 -06:00
|
|
|
P.Expect(Scanner.RPAREN);
|
2008-10-10 17:03:32 -06:00
|
|
|
P.opt_semi = true;
|
2008-10-09 19:03:16 -06:00
|
|
|
|
2008-09-18 17:58:37 -06:00
|
|
|
} else {
|
2008-10-14 19:14:01 -06:00
|
|
|
d = P.ParseSpec(exported, keyword);
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
P.Ecart();
|
2008-10-14 19:14:01 -06:00
|
|
|
return d;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-19 13:12:28 -06:00
|
|
|
// Function declarations
|
|
|
|
//
|
|
|
|
// 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)
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseFunctionDecl(exported bool) *AST.Decl {
|
2008-10-07 18:57:19 -06:00
|
|
|
P.Trace("FunctionDecl");
|
2008-09-18 17:58:37 -06:00
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
d := AST.NewDecl(P.pos, Scanner.FUNC, exported);
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Expect(Scanner.FUNC);
|
2008-10-16 13:16:50 -06:00
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
var recv *AST.Type;
|
2008-09-19 13:12:28 -06:00
|
|
|
if P.tok == Scanner.LPAREN {
|
2008-09-25 12:50:34 -06:00
|
|
|
pos := P.pos;
|
2008-10-24 15:04:54 -06:00
|
|
|
recv = P.ParseParameters(true);
|
2008-10-16 13:16:50 -06:00
|
|
|
if recv.nfields() != 1 {
|
2008-10-15 18:06:28 -06:00
|
|
|
P.Error(pos, "must have exactly one receiver");
|
2008-09-19 13:12:28 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-14 19:14:01 -06:00
|
|
|
d.ident = P.ParseIdent();
|
|
|
|
d.typ = P.ParseFunctionType();
|
2008-10-16 13:16:50 -06:00
|
|
|
d.typ.key = recv;
|
|
|
|
|
2008-10-07 18:57:19 -06:00
|
|
|
if P.tok == Scanner.LBRACE {
|
2008-10-10 17:03:32 -06:00
|
|
|
P.scope_lev++;
|
2008-10-14 19:14:01 -06:00
|
|
|
d.list = P.ParseBlock();
|
2008-10-10 17:03:32 -06:00
|
|
|
P.scope_lev--;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
P.Ecart();
|
2008-10-14 19:14:01 -06:00
|
|
|
return d;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseExportDecl() *AST.Decl {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Trace("ExportDecl");
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
d := AST.NewDecl(P.pos, Scanner.EXPORT, false);
|
2008-10-16 20:24:33 -06:00
|
|
|
d.ident = P.ParseIdentList();
|
2008-10-17 17:19:31 -06:00
|
|
|
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Ecart();
|
2008-10-16 20:24:33 -06:00
|
|
|
return d;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseDeclaration() *AST.Decl {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Trace("Declaration");
|
|
|
|
indent := P.indent;
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
d := AST.BadDecl;
|
2008-09-18 17:58:37 -06:00
|
|
|
exported := false;
|
|
|
|
if P.tok == Scanner.EXPORT {
|
2008-10-10 17:03:32 -06:00
|
|
|
if P.scope_lev == 0 {
|
2008-09-18 17:58:37 -06:00
|
|
|
exported = true;
|
|
|
|
} else {
|
|
|
|
P.Error(P.pos, "local declarations cannot be exported");
|
|
|
|
}
|
|
|
|
P.Next();
|
|
|
|
}
|
|
|
|
|
|
|
|
switch P.tok {
|
|
|
|
case Scanner.CONST, Scanner.TYPE, Scanner.VAR:
|
2008-10-14 19:14:01 -06:00
|
|
|
d = P.ParseDecl(exported, P.tok);
|
2008-09-18 17:58:37 -06:00
|
|
|
case Scanner.FUNC:
|
2008-10-14 19:14:01 -06:00
|
|
|
d = P.ParseFunctionDecl(exported);
|
2008-09-18 17:58:37 -06:00
|
|
|
case Scanner.EXPORT:
|
|
|
|
if exported {
|
|
|
|
P.Error(P.pos, "cannot mark export declaration for export");
|
|
|
|
}
|
|
|
|
P.Next();
|
2008-10-16 20:24:33 -06:00
|
|
|
d = P.ParseExportDecl();
|
2008-09-18 17:58:37 -06:00
|
|
|
default:
|
|
|
|
if exported && (P.tok == Scanner.IDENT || P.tok == Scanner.LPAREN) {
|
2008-10-16 20:24:33 -06:00
|
|
|
d = P.ParseExportDecl();
|
2008-09-18 17:58:37 -06:00
|
|
|
} else {
|
|
|
|
P.Error(P.pos, "declaration expected");
|
|
|
|
P.Next(); // make progress
|
|
|
|
}
|
|
|
|
}
|
2008-10-07 18:57:19 -06:00
|
|
|
|
2008-09-18 17:58:37 -06:00
|
|
|
if indent != P.indent {
|
|
|
|
panic("imbalanced tracing code (Declaration)");
|
|
|
|
}
|
|
|
|
P.Ecart();
|
2008-10-14 19:14:01 -06:00
|
|
|
return d;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Program
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
func (P *Parser) ParseProgram() *AST.Program {
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Trace("Program");
|
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
p := AST.NewProgram(P.pos);
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Expect(Scanner.PACKAGE);
|
2008-10-14 19:14:01 -06:00
|
|
|
p.ident = P.ParseIdent();
|
2008-09-18 17:58:37 -06:00
|
|
|
|
2008-10-20 16:03:40 -06:00
|
|
|
p.decls = AST.NewList();
|
2008-10-10 17:03:32 -06:00
|
|
|
for P.tok == Scanner.IMPORT {
|
2008-10-14 19:14:01 -06:00
|
|
|
p.decls.Add(P.ParseDecl(false, Scanner.IMPORT));
|
2008-10-10 17:03:32 -06:00
|
|
|
P.OptSemicolon();
|
|
|
|
}
|
2008-10-26 22:32:30 -06:00
|
|
|
|
|
|
|
if !P.deps {
|
|
|
|
for P.tok != Scanner.EOF {
|
|
|
|
p.decls.Add(P.ParseDeclaration());
|
|
|
|
P.OptSemicolon();
|
|
|
|
}
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|
|
|
|
|
2008-10-17 00:30:42 -06:00
|
|
|
p.comments = P.comments;
|
|
|
|
|
2008-09-18 17:58:37 -06:00
|
|
|
P.Ecart();
|
2008-10-14 19:14:01 -06:00
|
|
|
return p;
|
2008-09-18 17:58:37 -06:00
|
|
|
}
|