2009-04-28 20:11: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-04-23 22:53:01 -06:00
|
|
|
|
2009-05-20 18:09:59 -06:00
|
|
|
// Format file for printing AST nodes.
|
2009-05-01 20:32:41 -06:00
|
|
|
|
2009-05-20 18:09:59 -06:00
|
|
|
ast "ast";
|
|
|
|
token "token";
|
2009-04-28 20:11:37 -06:00
|
|
|
|
2009-05-20 18:09:59 -06:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Basic types, support rules
|
2009-04-28 20:11:37 -06:00
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
array =
|
|
|
|
*;
|
|
|
|
|
2009-06-01 20:13:44 -06:00
|
|
|
ptr =
|
2009-05-01 20:32:41 -06:00
|
|
|
*;
|
|
|
|
|
|
|
|
string =
|
|
|
|
"%s";
|
|
|
|
|
|
|
|
char =
|
|
|
|
"%c";
|
|
|
|
|
|
|
|
bytes =
|
|
|
|
{*};
|
|
|
|
|
2009-05-13 16:18:05 -06:00
|
|
|
empty =
|
|
|
|
;
|
2009-05-01 20:32:41 -06:00
|
|
|
|
2009-05-06 17:28:18 -06:00
|
|
|
exists =
|
2009-05-13 16:18:05 -06:00
|
|
|
*:empty;
|
2009-04-28 20:11:37 -06:00
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.Expr =
|
|
|
|
*;
|
2009-04-28 20:11:37 -06:00
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.Stmt =
|
2009-04-25 17:36:17 -06:00
|
|
|
*;
|
2009-04-23 22:53:01 -06:00
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.Decl =
|
2009-04-25 17:36:17 -06:00
|
|
|
*;
|
2009-04-23 22:53:01 -06:00
|
|
|
|
2009-04-28 20:11:37 -06:00
|
|
|
// ----------------------------------------------------------------------------
|
2009-05-20 18:09:59 -06:00
|
|
|
// Tokens and comments
|
|
|
|
|
|
|
|
token.Token =
|
2009-06-02 19:03:47 -06:00
|
|
|
@:string;
|
2009-04-23 22:53:01 -06:00
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.Comment =
|
2009-06-01 20:13:44 -06:00
|
|
|
// TODO this doesn't indent properly after //-style comments because
|
|
|
|
// the '\n'-char is printed as part of the comment - need to
|
|
|
|
// address this
|
2009-05-15 19:52:59 -06:00
|
|
|
Text:string [Text:isMultiLineComment "\n"];
|
2009-04-24 18:22:58 -06:00
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.Comments =
|
2009-04-28 20:11:37 -06:00
|
|
|
{*};
|
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Expressions & Types
|
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.Field =
|
2009-05-06 17:28:18 -06:00
|
|
|
[Names:exists {Names / ", "} " "] Type;
|
2009-04-28 20:11:37 -06:00
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.BadExpr =
|
2009-04-28 20:11:37 -06:00
|
|
|
"BAD EXPR";
|
2009-04-24 18:22:58 -06:00
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.Ident =
|
2009-04-25 17:36:17 -06:00
|
|
|
Value;
|
2009-04-24 18:22:58 -06:00
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.Ellipsis =
|
2009-04-28 20:11:37 -06:00
|
|
|
"...";
|
2009-04-24 18:22:58 -06:00
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.IntLit =
|
|
|
|
Value:string;
|
2009-04-28 20:11:37 -06:00
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.FloatLit =
|
|
|
|
Value:string;
|
2009-04-28 20:11:37 -06:00
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.CharLit =
|
|
|
|
Value:string;
|
2009-04-28 20:11:37 -06:00
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.StringLit =
|
|
|
|
Value:string;
|
2009-04-28 20:11:37 -06:00
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.StringList =
|
|
|
|
{Strings / "\n"};
|
2009-04-28 20:11:37 -06:00
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.FuncLit =
|
2009-06-02 19:03:47 -06:00
|
|
|
Type " " Body @:clearOptSemi; // no optional ; after a func literal body
|
2009-04-28 20:11:37 -06:00
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.CompositeLit =
|
|
|
|
Type "{" {Elts / ", "} "}";
|
2009-04-28 20:11:37 -06:00
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.ParenExpr =
|
2009-04-28 20:11:37 -06:00
|
|
|
"(" X ")";
|
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.SelectorExpr =
|
2009-04-28 20:11:37 -06:00
|
|
|
X "." Sel;
|
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.IndexExpr =
|
2009-05-13 16:18:05 -06:00
|
|
|
X "[" Index [":" End] "]";
|
2009-04-28 20:11:37 -06:00
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.TypeAssertExpr =
|
2009-04-28 20:11:37 -06:00
|
|
|
X ".(" Type ")";
|
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.CallExpr =
|
|
|
|
Fun "(" {Args / ", "} ")";
|
2009-04-28 20:11:37 -06:00
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.StarExpr =
|
2009-04-28 20:11:37 -06:00
|
|
|
"*" X;
|
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.UnaryExpr =
|
2009-04-28 20:11:37 -06:00
|
|
|
Op X;
|
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.BinaryExpr =
|
|
|
|
X " " Op " " Y;
|
2009-04-28 20:11:37 -06:00
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.KeyValueExpr =
|
2009-04-28 20:11:37 -06:00
|
|
|
Key ": " Value;
|
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.ArrayType =
|
2009-05-13 16:18:05 -06:00
|
|
|
"[" [Len] "]" Elt;
|
2009-04-28 20:11:37 -06:00
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.StructType =
|
2009-05-06 17:28:18 -06:00
|
|
|
"struct"
|
|
|
|
[Lbrace:isValidPos " {"]
|
|
|
|
[ Fields:exists
|
2009-06-01 20:13:44 -06:00
|
|
|
( "\t" >> "\n"
|
2009-05-06 17:28:18 -06:00
|
|
|
{Fields / ";\n"}
|
2009-06-01 20:13:44 -06:00
|
|
|
) "\n"
|
2009-05-01 20:32:41 -06:00
|
|
|
]
|
2009-05-06 17:28:18 -06:00
|
|
|
[Rbrace:isValidPos "}"];
|
2009-04-24 18:22:58 -06:00
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
signature =
|
2009-05-06 17:28:18 -06:00
|
|
|
"(" {Params / ", "} ")" [Results:exists " (" {Results / ", "} ")"];
|
2009-05-01 20:32:41 -06:00
|
|
|
|
|
|
|
funcSignature =
|
|
|
|
*:signature;
|
|
|
|
|
|
|
|
ast.FuncType =
|
2009-06-02 19:03:47 -06:00
|
|
|
[Position:isValidPos "func"] @:signature;
|
2009-04-28 20:11:37 -06:00
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.InterfaceType =
|
2009-05-06 17:28:18 -06:00
|
|
|
"interface"
|
|
|
|
[Lbrace:isValidPos " {"]
|
|
|
|
[ Methods:exists
|
2009-06-01 20:13:44 -06:00
|
|
|
( "\t" >> "\n"
|
2009-05-06 17:28:18 -06:00
|
|
|
{Methods / ";\n"}
|
2009-06-01 20:13:44 -06:00
|
|
|
) "\n"
|
2009-05-01 20:32:41 -06:00
|
|
|
]
|
2009-05-06 17:28:18 -06:00
|
|
|
[Rbrace:isValidPos "}"];
|
2009-04-28 20:11:37 -06:00
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.MapType =
|
2009-04-28 20:11:37 -06:00
|
|
|
"map[" Key "]" Value;
|
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.ChanType =
|
2009-05-06 17:28:18 -06:00
|
|
|
( Dir:isSend Dir:isRecv
|
|
|
|
"chan "
|
|
|
|
| Dir:isSend
|
|
|
|
"chan <- "
|
|
|
|
| "<-chan "
|
|
|
|
)
|
|
|
|
Value;
|
2009-04-28 20:11:37 -06:00
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Statements
|
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.BadStmt =
|
2009-04-28 20:11:37 -06:00
|
|
|
"BAD STMT";
|
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.DeclStmt =
|
2009-04-28 20:11:37 -06:00
|
|
|
Decl;
|
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.EmptyStmt =
|
2009-04-28 20:11:37 -06:00
|
|
|
;
|
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.LabeledStmt =
|
2009-04-28 20:11:37 -06:00
|
|
|
Label ":\t" Stmt;
|
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.ExprStmt =
|
2009-04-28 20:11:37 -06:00
|
|
|
X;
|
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.IncDecStmt =
|
2009-04-28 20:11:37 -06:00
|
|
|
X Tok;
|
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.AssignStmt =
|
|
|
|
{Lhs / ", "} " " Tok " " {Rhs / ", "};
|
2009-04-28 20:11:37 -06:00
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.GoStmt =
|
2009-04-28 20:11:37 -06:00
|
|
|
"go " Call;
|
|
|
|
|
2009-05-06 17:28:18 -06:00
|
|
|
ast.DeferStmt =
|
|
|
|
"defer " Call;
|
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.ReturnStmt =
|
|
|
|
"return" {" " Results / ","};
|
2009-04-28 20:11:37 -06:00
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.BranchStmt =
|
2009-04-28 20:11:37 -06:00
|
|
|
Tok [" " Label];
|
2009-04-24 18:22:58 -06:00
|
|
|
|
2009-06-01 20:13:44 -06:00
|
|
|
stmtList =
|
2009-06-02 19:03:47 -06:00
|
|
|
{@ / @:optSemi "\n"};
|
2009-06-01 20:13:44 -06:00
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
blockStmt = // like ast.BlockStmt but w/o indentation
|
|
|
|
"{"
|
2009-05-06 17:28:18 -06:00
|
|
|
[List:exists
|
2009-05-01 20:32:41 -06:00
|
|
|
"\n"
|
2009-06-01 20:13:44 -06:00
|
|
|
List:stmtList
|
2009-05-01 20:32:41 -06:00
|
|
|
"\n"
|
|
|
|
]
|
2009-06-02 19:03:47 -06:00
|
|
|
"}" @:setOptSemi;
|
2009-04-28 20:11:37 -06:00
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
blockStmtPtr =
|
|
|
|
*:blockStmt;
|
2009-04-28 20:11:37 -06:00
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.BlockStmt =
|
|
|
|
"{"
|
2009-05-06 17:28:18 -06:00
|
|
|
[List:exists
|
2009-06-01 20:13:44 -06:00
|
|
|
( "\t" >> "\n"
|
|
|
|
List:stmtList
|
|
|
|
) "\n"
|
2009-05-01 20:32:41 -06:00
|
|
|
]
|
2009-06-02 19:03:47 -06:00
|
|
|
"}" @:setOptSemi;
|
2009-04-24 18:22:58 -06:00
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.IfStmt =
|
|
|
|
"if " [Init "; "] [Cond " "] Body [" else " Else];
|
|
|
|
|
|
|
|
ast.CaseClause =
|
2009-05-06 17:28:18 -06:00
|
|
|
( Values:exists "case " {Values / ", "}
|
|
|
|
| "default"
|
2009-05-01 20:32:41 -06:00
|
|
|
)
|
|
|
|
":"
|
2009-05-06 17:28:18 -06:00
|
|
|
[Body:exists
|
2009-06-01 20:13:44 -06:00
|
|
|
( "\t" >> "\n"
|
|
|
|
Body:stmtList
|
|
|
|
)
|
2009-05-01 20:32:41 -06:00
|
|
|
];
|
|
|
|
|
|
|
|
ast.SwitchStmt =
|
|
|
|
"switch " [Init "; "] [Tag " "]
|
|
|
|
Body:blockStmtPtr;
|
|
|
|
|
|
|
|
ast.TypeCaseClause =
|
2009-05-06 17:28:18 -06:00
|
|
|
( Type:exists "case " Type
|
2009-05-01 20:32:41 -06:00
|
|
|
| "default"
|
|
|
|
)
|
|
|
|
":"
|
2009-05-06 17:28:18 -06:00
|
|
|
[Body:exists
|
2009-06-01 20:13:44 -06:00
|
|
|
( "\t" >> "\n"
|
|
|
|
Body:stmtList
|
|
|
|
)
|
2009-05-01 20:32:41 -06:00
|
|
|
];
|
|
|
|
|
|
|
|
ast.TypeSwitchStmt =
|
|
|
|
"switch " Assign " "
|
|
|
|
Body:blockStmtPtr;
|
|
|
|
|
|
|
|
ast.CommClause =
|
2009-05-06 17:28:18 -06:00
|
|
|
( "case " [Lhs " " Tok " "] Rhs
|
|
|
|
| "default"
|
|
|
|
)
|
|
|
|
":"
|
|
|
|
[Body:exists
|
2009-06-01 20:13:44 -06:00
|
|
|
( "\t" >> "\n"
|
|
|
|
Body:stmtList
|
|
|
|
)
|
2009-05-06 17:28:18 -06:00
|
|
|
];
|
2009-05-01 20:32:41 -06:00
|
|
|
|
|
|
|
ast.SelectStmt =
|
|
|
|
"select "
|
|
|
|
Body:blockStmtPtr;
|
|
|
|
|
|
|
|
ast.ForStmt =
|
|
|
|
"for "
|
2009-05-06 17:28:18 -06:00
|
|
|
[ (Init:exists | Post:exists)
|
2009-05-01 20:32:41 -06:00
|
|
|
[Init] "; " [Cond] "; " [Post " "]
|
|
|
|
| Cond " "
|
|
|
|
]
|
|
|
|
Body;
|
|
|
|
|
|
|
|
ast.RangeStmt =
|
|
|
|
"for " Key [", " Value] " " Tok " range " X
|
|
|
|
" "
|
|
|
|
Body;
|
2009-04-28 20:11:37 -06:00
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Declarations
|
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.Spec =
|
2009-04-28 20:11:37 -06:00
|
|
|
*;
|
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.ImportSpec =
|
|
|
|
Doc
|
|
|
|
[Name] "\t" {Path};
|
2009-04-28 20:11:37 -06:00
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.ValueSpec =
|
2009-05-06 17:28:18 -06:00
|
|
|
{Names / ", "} [" " Type] [Values:exists " = " {Values / ", "}];
|
2009-04-28 20:11:37 -06:00
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.TypeSpec =
|
|
|
|
Name " " // TODO using "\t" instead of " " screws up struct field alignment
|
|
|
|
Type;
|
2009-04-28 20:11:37 -06:00
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.BadDecl =
|
2009-04-28 20:11:37 -06:00
|
|
|
"BAD DECL";
|
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.GenDecl =
|
2009-04-28 20:11:37 -06:00
|
|
|
Doc
|
2009-05-06 17:28:18 -06:00
|
|
|
Tok " "
|
2009-06-01 20:13:44 -06:00
|
|
|
( Lparen:isValidPos "("
|
|
|
|
[Specs:exists
|
|
|
|
( "\t" >> "\n"
|
|
|
|
{Specs / ";\n"}
|
|
|
|
) "\n"
|
|
|
|
]
|
2009-06-02 19:03:47 -06:00
|
|
|
")" @:setOptSemi
|
2009-05-15 19:52:59 -06:00
|
|
|
| {Specs / ";\n"}
|
|
|
|
);
|
2009-05-01 20:32:41 -06:00
|
|
|
|
|
|
|
ast.FuncDecl =
|
|
|
|
"func " ["(" Recv ") "] Name Type:funcSignature
|
|
|
|
[" " Body]
|
|
|
|
"\n";
|
|
|
|
|
2009-04-28 20:11:37 -06:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Program
|
|
|
|
|
2009-05-01 20:32:41 -06:00
|
|
|
ast.Program =
|
2009-04-28 20:11:37 -06:00
|
|
|
Doc
|
|
|
|
"package " Name "\n\n"
|
2009-05-01 20:32:41 -06:00
|
|
|
{Decls / "\n\n"};
|