Index: src/pkg/go/printer/testdata/parser.go |
=================================================================== |
--- a/src/pkg/go/printer/testdata/parser.go |
+++ b/src/pkg/go/printer/testdata/parser.go |
@@ -16,7 +16,6 @@ |
"go/token" |
) |
- |
// The mode parameter to the Parse* functions is a set of flags (or 0). |
// They control the amount of source code parsed and other optional |
// parser functionality. |
@@ -29,7 +28,6 @@ |
DeclarationErrors // report declaration errors |
) |
- |
// The parser structure holds the parser's internal state. |
type parser struct { |
file *token.File |
@@ -66,7 +64,6 @@ |
targetStack [][]*ast.Ident // stack of unresolved labels |
} |
- |
// scannerMode returns the scanner mode bits given the parser's mode bits. |
func scannerMode(mode uint) uint { |
var m uint = scanner.InsertSemis |
@@ -76,7 +73,6 @@ |
return m |
} |
- |
func (p *parser) init(fset *token.FileSet, filename string, src []byte, mode uint) { |
p.file = fset.AddFile(filename, fset.Base(), len(src)) |
p.scanner.Init(p.file, src, p, scannerMode(mode)) |
@@ -95,7 +91,6 @@ |
p.openLabelScope() |
} |
- |
// ---------------------------------------------------------------------------- |
// Scoping support |
@@ -103,18 +98,15 @@ |
p.topScope = ast.NewScope(p.topScope) |
} |
- |
func (p *parser) closeScope() { |
p.topScope = p.topScope.Outer |
} |
- |
func (p *parser) openLabelScope() { |
p.labelScope = ast.NewScope(p.labelScope) |
p.targetStack = append(p.targetStack, nil) |
} |
- |
func (p *parser) closeLabelScope() { |
// resolve labels |
n := len(p.targetStack) - 1 |
@@ -130,7 +122,6 @@ |
p.labelScope = p.labelScope.Outer |
} |
- |
func (p *parser) declare(decl interface{}, scope *ast.Scope, kind ast.ObjKind, idents ...*ast.Ident) { |
for _, ident := range idents { |
assert(ident.Obj == nil, "identifier already declared or resolved") |
@@ -151,7 +142,6 @@ |
} |
} |
- |
func (p *parser) shortVarDecl(idents []*ast.Ident) { |
// Go spec: A short variable declaration may redeclare variables |
// provided they were originally declared in the same block with |
@@ -177,13 +167,11 @@ |
} |
} |
- |
// The unresolved object is a sentinel to mark identifiers that have been added |
// to the list of unresolved identifiers. The sentinel is only used for verifying |
// internal consistency. |
var unresolved = new(ast.Object) |
- |
func (p *parser) resolve(x ast.Expr) { |
// nothing to do if x is not an identifier or the blank identifier |
ident, _ := x.(*ast.Ident) |
@@ -209,7 +197,6 @@ |
p.unresolved = append(p.unresolved, ident) |
} |
- |
// ---------------------------------------------------------------------------- |
// Parsing support |
@@ -227,21 +214,18 @@ |
fmt.Println(a...) |
} |
- |
func trace(p *parser, msg string) *parser { |
p.printTrace(msg, "(") |
p.indent++ |
return p |
} |
- |
// Usage pattern: defer un(trace(p, "...")); |
func un(p *parser) { |
p.indent-- |
p.printTrace(")") |
} |
- |
// Advance to the next token. |
func (p *parser) next0() { |
// Because of one-token look-ahead, print the previous token |
@@ -283,7 +267,6 @@ |
return |
} |
- |
// Consume a group of adjacent comments, add it to the parser's |
// comments list, and return it together with the line at which |
// the last comment in the group ends. An empty line or non-comment |
@@ -305,7 +288,6 @@ |
return |
} |
- |
// Advance to the next non-comment token. In the process, collect |
// any comment groups encountered, and remember the last lead and |
// and line comments. |
@@ -356,12 +338,10 @@ |
} |
} |
- |
func (p *parser) error(pos token.Pos, msg string) { |
p.Error(p.file.Position(pos), msg) |
} |
- |
func (p *parser) errorExpected(pos token.Pos, msg string) { |
msg = "expected " + msg |
if pos == p.pos { |
@@ -379,7 +359,6 @@ |
p.error(pos, msg) |
} |
- |
func (p *parser) expect(tok token.Token) token.Pos { |
pos := p.pos |
if p.tok != tok { |
@@ -389,21 +368,18 @@ |
return pos |
} |
- |
func (p *parser) expectSemi() { |
if p.tok != token.RPAREN && p.tok != token.RBRACE { |
p.expect(token.SEMICOLON) |
} |
} |
- |
func assert(cond bool, msg string) { |
if !cond { |
panic("go/parser internal error: " + msg) |
} |
} |
- |
// ---------------------------------------------------------------------------- |
// Identifiers |
@@ -419,7 +395,6 @@ |
return &ast.Ident{pos, name, nil} |
} |
- |
func (p *parser) parseIdentList() (list []*ast.Ident) { |
if p.trace { |
defer un(trace(p, "IdentList")) |
@@ -434,7 +409,6 @@ |
return |
} |
- |
// ---------------------------------------------------------------------------- |
// Common productions |
@@ -453,7 +427,6 @@ |
return |
} |
- |
func (p *parser) parseLhsList() []ast.Expr { |
list := p.parseExprList(true) |
switch p.tok { |
@@ -477,12 +450,10 @@ |
return list |
} |
- |
func (p *parser) parseRhsList() []ast.Expr { |
return p.parseExprList(false) |
} |
- |
// ---------------------------------------------------------------------------- |
// Types |
@@ -503,7 +474,6 @@ |
return typ |
} |
- |
// If the result is an identifier, it is not resolved. |
func (p *parser) parseTypeName() ast.Expr { |
if p.trace { |
@@ -524,7 +494,6 @@ |
return ident |
} |
- |
func (p *parser) parseArrayType(ellipsisOk bool) ast.Expr { |
if p.trace { |
defer un(trace(p, "ArrayType")) |
@@ -544,7 +513,6 @@ |
return &ast.ArrayType{lbrack, len, elt} |
} |
- |
func (p *parser) makeIdentList(list []ast.Expr) []*ast.Ident { |
idents := make([]*ast.Ident, len(list)) |
for i, x := range list { |
@@ -559,7 +527,6 @@ |
return idents |
} |
- |
func (p *parser) parseFieldDecl(scope *ast.Scope) *ast.Field { |
if p.trace { |
defer un(trace(p, "FieldDecl")) |
@@ -601,7 +568,6 @@ |
return field |
} |
- |
func (p *parser) parseStructType() *ast.StructType { |
if p.trace { |
defer un(trace(p, "StructType")) |
@@ -623,7 +589,6 @@ |
return &ast.StructType{pos, &ast.FieldList{lbrace, list, rbrace}, false} |
} |
- |
func (p *parser) parsePointerType() *ast.StarExpr { |
if p.trace { |
defer un(trace(p, "PointerType")) |
@@ -635,7 +600,6 @@ |
return &ast.StarExpr{star, base} |
} |
- |
func (p *parser) tryVarType(isParam bool) ast.Expr { |
if isParam && p.tok == token.ELLIPSIS { |
pos := p.pos |
@@ -653,7 +617,6 @@ |
return p.tryIdentOrType(false) |
} |
- |
func (p *parser) parseVarType(isParam bool) ast.Expr { |
typ := p.tryVarType(isParam) |
if typ == nil { |
@@ -665,7 +628,6 @@ |
return typ |
} |
- |
func (p *parser) parseVarList(isParam bool) (list []ast.Expr, typ ast.Expr) { |
if p.trace { |
defer un(trace(p, "VarList")) |
@@ -693,7 +655,6 @@ |
return |
} |
- |
func (p *parser) parseParameterList(scope *ast.Scope, ellipsisOk bool) (params []*ast.Field) { |
if p.trace { |
defer un(trace(p, "ParameterList")) |
@@ -738,7 +699,6 @@ |
return |
} |
- |
func (p *parser) parseParameters(scope *ast.Scope, ellipsisOk bool) *ast.FieldList { |
if p.trace { |
defer un(trace(p, "Parameters")) |
@@ -754,7 +714,6 @@ |
return &ast.FieldList{lparen, params, rparen} |
} |
- |
func (p *parser) parseResult(scope *ast.Scope) *ast.FieldList { |
if p.trace { |
defer un(trace(p, "Result")) |
@@ -774,7 +733,6 @@ |
return nil |
} |
- |
func (p *parser) parseSignature(scope *ast.Scope) (params, results *ast.FieldList) { |
if p.trace { |
defer un(trace(p, "Signature")) |
@@ -786,7 +744,6 @@ |
return |
} |
- |
func (p *parser) parseFuncType() (*ast.FuncType, *ast.Scope) { |
if p.trace { |
defer un(trace(p, "FuncType")) |
@@ -799,7 +756,6 @@ |
return &ast.FuncType{pos, params, results}, scope |
} |
- |
func (p *parser) parseMethodSpec(scope *ast.Scope) *ast.Field { |
if p.trace { |
defer un(trace(p, "MethodSpec")) |
@@ -827,7 +783,6 @@ |
return spec |
} |
- |
func (p *parser) parseInterfaceType() *ast.InterfaceType { |
if p.trace { |
defer un(trace(p, "InterfaceType")) |
@@ -846,7 +801,6 @@ |
return &ast.InterfaceType{pos, &ast.FieldList{lbrace, list, rbrace}, false} |
} |
- |
func (p *parser) parseMapType() *ast.MapType { |
if p.trace { |
defer un(trace(p, "MapType")) |
@@ -861,7 +815,6 @@ |
return &ast.MapType{pos, key, value} |
} |
- |
func (p *parser) parseChanType() *ast.ChanType { |
if p.trace { |
defer un(trace(p, "ChanType")) |
@@ -885,7 +838,6 @@ |
return &ast.ChanType{pos, dir, value} |
} |
- |
// If the result is an identifier, it is not resolved. |
func (p *parser) tryIdentOrType(ellipsisOk bool) ast.Expr { |
switch p.tok { |
@@ -918,7 +870,6 @@ |
return nil |
} |
- |
func (p *parser) tryType() ast.Expr { |
typ := p.tryIdentOrType(false) |
if typ != nil { |
@@ -927,7 +878,6 @@ |
return typ |
} |
- |
// ---------------------------------------------------------------------------- |
// Blocks |
@@ -943,7 +893,6 @@ |
return |
} |
- |
func (p *parser) parseBody(scope *ast.Scope) *ast.BlockStmt { |
if p.trace { |
defer un(trace(p, "Body")) |
@@ -960,7 +909,6 @@ |
return &ast.BlockStmt{lbrace, list, rbrace} |
} |
- |
func (p *parser) parseBlockStmt() *ast.BlockStmt { |
if p.trace { |
defer un(trace(p, "BlockStmt")) |
@@ -975,7 +923,6 @@ |
return &ast.BlockStmt{lbrace, list, rbrace} |
} |
- |
// ---------------------------------------------------------------------------- |
// Expressions |
@@ -997,7 +944,6 @@ |
return &ast.FuncLit{typ, body} |
} |
- |
// parseOperand may return an expression or a raw type (incl. array |
// types of the form [...]T. Callers must verify the result. |
// If lhs is set and the result is an identifier, it is not resolved. |
@@ -1047,7 +993,6 @@ |
return &ast.BadExpr{pos, p.pos} |
} |
- |
func (p *parser) parseSelector(x ast.Expr) ast.Expr { |
if p.trace { |
defer un(trace(p, "Selector")) |
@@ -1058,7 +1003,6 @@ |
return &ast.SelectorExpr{x, sel} |
} |
- |
func (p *parser) parseTypeAssertion(x ast.Expr) ast.Expr { |
if p.trace { |
defer un(trace(p, "TypeAssertion")) |
@@ -1077,7 +1021,6 @@ |
return &ast.TypeAssertExpr{x, typ} |
} |
- |
func (p *parser) parseIndexOrSlice(x ast.Expr) ast.Expr { |
if p.trace { |
defer un(trace(p, "IndexOrSlice")) |
@@ -1106,7 +1049,6 @@ |
return &ast.IndexExpr{x, lbrack, low, rbrack} |
} |
- |
func (p *parser) parseCallOrConversion(fun ast.Expr) *ast.CallExpr { |
if p.trace { |
defer un(trace(p, "CallOrConversion")) |
@@ -1133,7 +1075,6 @@ |
return &ast.CallExpr{fun, lparen, list, ellipsis, rparen} |
} |
- |
func (p *parser) parseElement(keyOk bool) ast.Expr { |
if p.trace { |
defer un(trace(p, "Element")) |
@@ -1156,7 +1097,6 @@ |
return x |
} |
- |
func (p *parser) parseElementList() (list []ast.Expr) { |
if p.trace { |
defer un(trace(p, "ElementList")) |
@@ -1173,7 +1113,6 @@ |
return |
} |
- |
func (p *parser) parseLiteralValue(typ ast.Expr) ast.Expr { |
if p.trace { |
defer un(trace(p, "LiteralValue")) |
@@ -1190,7 +1129,6 @@ |
return &ast.CompositeLit{typ, lbrace, elts, rbrace} |
} |
- |
// checkExpr checks that x is an expression (and not a type). |
func (p *parser) checkExpr(x ast.Expr) ast.Expr { |
switch t := unparen(x).(type) { |
@@ -1227,7 +1165,6 @@ |
return x |
} |
- |
// isTypeName returns true iff x is a (qualified) TypeName. |
func isTypeName(x ast.Expr) bool { |
switch t := x.(type) { |
@@ -1242,7 +1179,6 @@ |
return true |
} |
- |
// isLiteralType returns true iff x is a legal composite literal type. |
func isLiteralType(x ast.Expr) bool { |
switch t := x.(type) { |
@@ -1260,7 +1196,6 @@ |
return true |
} |
- |
// If x is of the form *T, deref returns T, otherwise it returns x. |
func deref(x ast.Expr) ast.Expr { |
if p, isPtr := x.(*ast.StarExpr); isPtr { |
@@ -1269,7 +1204,6 @@ |
return x |
} |
- |
// If x is of the form (T), unparen returns unparen(T), otherwise it returns x. |
func unparen(x ast.Expr) ast.Expr { |
if p, isParen := x.(*ast.ParenExpr); isParen { |
@@ -1278,7 +1212,6 @@ |
return x |
} |
- |
// checkExprOrType checks that x is an expression or a type |
// (and not a raw type such as [...]T). |
// |
@@ -1303,7 +1236,6 @@ |
return x |
} |
- |
// If lhs is set and the result is an identifier, it is not resolved. |
func (p *parser) parsePrimaryExpr(lhs bool) ast.Expr { |
if p.trace { |
@@ -1358,7 +1290,6 @@ |
return x |
} |
- |
// If lhs is set and the result is an identifier, it is not resolved. |
func (p *parser) parseUnaryExpr(lhs bool) ast.Expr { |
if p.trace { |
@@ -1396,7 +1327,6 @@ |
return p.parsePrimaryExpr(lhs) |
} |
- |
// If lhs is set and the result is an identifier, it is not resolved. |
func (p *parser) parseBinaryExpr(lhs bool, prec1 int) ast.Expr { |
if p.trace { |
@@ -1420,7 +1350,6 @@ |
return x |
} |
- |
// If lhs is set and the result is an identifier, it is not resolved. |
// TODO(gri): parseExpr may return a type or even a raw type ([..]int) - |
// should reject when a type/raw type is obviously not allowed |
@@ -1432,12 +1361,10 @@ |
return p.parseBinaryExpr(lhs, token.LowestPrec+1) |
} |
- |
func (p *parser) parseRhs() ast.Expr { |
return p.parseExpr(false) |
} |
- |
// ---------------------------------------------------------------------------- |
// Statements |
@@ -1500,7 +1427,6 @@ |
return &ast.ExprStmt{x[0]} |
} |
- |
func (p *parser) parseCallExpr() *ast.CallExpr { |
x := p.parseRhs() |
if call, isCall := x.(*ast.CallExpr); isCall { |
@@ -1510,7 +1436,6 @@ |
return nil |
} |
- |
func (p *parser) parseGoStmt() ast.Stmt { |
if p.trace { |
defer un(trace(p, "GoStmt")) |
@@ -1526,7 +1451,6 @@ |
return &ast.GoStmt{pos, call} |
} |
- |
func (p *parser) parseDeferStmt() ast.Stmt { |
if p.trace { |
defer un(trace(p, "DeferStmt")) |
@@ -1542,7 +1466,6 @@ |
return &ast.DeferStmt{pos, call} |
} |
- |
func (p *parser) parseReturnStmt() *ast.ReturnStmt { |
if p.trace { |
defer un(trace(p, "ReturnStmt")) |
@@ -1559,7 +1482,6 @@ |
return &ast.ReturnStmt{pos, x} |
} |
- |
func (p *parser) parseBranchStmt(tok token.Token) *ast.BranchStmt { |
if p.trace { |
defer un(trace(p, "BranchStmt")) |
@@ -1578,7 +1500,6 @@ |
return &ast.BranchStmt{pos, tok, label} |
} |
- |
func (p *parser) makeExpr(s ast.Stmt) ast.Expr { |
if s == nil { |
return nil |
@@ -1590,7 +1511,6 @@ |
return &ast.BadExpr{s.Pos(), s.End()} |
} |
- |
func (p *parser) parseIfStmt() *ast.IfStmt { |
if p.trace { |
defer un(trace(p, "IfStmt")) |
@@ -1633,7 +1553,6 @@ |
return &ast.IfStmt{pos, s, x, body, else_} |
} |
- |
func (p *parser) parseTypeList() (list []ast.Expr) { |
if p.trace { |
defer un(trace(p, "TypeList")) |
@@ -1648,7 +1567,6 @@ |
return |
} |
- |
func (p *parser) parseCaseClause(exprSwitch bool) *ast.CaseClause { |
if p.trace { |
defer un(trace(p, "CaseClause")) |
@@ -1675,7 +1593,6 @@ |
return &ast.CaseClause{pos, list, colon, body} |
} |
- |
func isExprSwitch(s ast.Stmt) bool { |
if s == nil { |
return true |
@@ -1689,7 +1606,6 @@ |
return false |
} |
- |
func (p *parser) parseSwitchStmt() ast.Stmt { |
if p.trace { |
defer un(trace(p, "SwitchStmt")) |
@@ -1735,7 +1651,6 @@ |
return &ast.TypeSwitchStmt{pos, s1, s2, body} |
} |
- |
func (p *parser) parseCommClause() *ast.CommClause { |
if p.trace { |
defer un(trace(p, "CommClause")) |
@@ -1801,7 +1716,6 @@ |
return &ast.CommClause{pos, comm, colon, body} |
} |
- |
func (p *parser) parseSelectStmt() *ast.SelectStmt { |
if p.trace { |
defer un(trace(p, "SelectStmt")) |
@@ -1820,7 +1734,6 @@ |
return &ast.SelectStmt{pos, body} |
} |
- |
func (p *parser) parseForStmt() ast.Stmt { |
if p.trace { |
defer un(trace(p, "ForStmt")) |
@@ -1890,7 +1803,6 @@ |
return &ast.ForStmt{pos, s1, p.makeExpr(s2), s3, body} |
} |
- |
func (p *parser) parseStmt() (s ast.Stmt) { |
if p.trace { |
defer un(trace(p, "Statement")) |
@@ -1947,13 +1859,11 @@ |
return |
} |
- |
// ---------------------------------------------------------------------------- |
// Declarations |
type parseSpecFunction func(p *parser, doc *ast.CommentGroup, iota int) ast.Spec |
- |
func parseImportSpec(p *parser, doc *ast.CommentGroup, _ int) ast.Spec { |
if p.trace { |
defer un(trace(p, "ImportSpec")) |
@@ -1984,7 +1894,6 @@ |
return spec |
} |
- |
func parseConstSpec(p *parser, doc *ast.CommentGroup, iota int) ast.Spec { |
if p.trace { |
defer un(trace(p, "ConstSpec")) |
@@ -2009,7 +1918,6 @@ |
return spec |
} |
- |
func parseTypeSpec(p *parser, doc *ast.CommentGroup, _ int) ast.Spec { |
if p.trace { |
defer un(trace(p, "TypeSpec")) |
@@ -2031,7 +1939,6 @@ |
return spec |
} |
- |
func parseVarSpec(p *parser, doc *ast.CommentGroup, _ int) ast.Spec { |
if p.trace { |
defer un(trace(p, "VarSpec")) |
@@ -2056,7 +1963,6 @@ |
return spec |
} |
- |
func (p *parser) parseGenDecl(keyword token.Token, f parseSpecFunction) *ast.GenDecl { |
if p.trace { |
defer un(trace(p, "GenDecl("+keyword.String()+")")) |
@@ -2081,7 +1987,6 @@ |
return &ast.GenDecl{doc, pos, keyword, lparen, list, rparen} |
} |
- |
func (p *parser) parseReceiver(scope *ast.Scope) *ast.FieldList { |
if p.trace { |
defer un(trace(p, "Receiver")) |
@@ -2109,7 +2014,6 @@ |
return par |
} |
- |
func (p *parser) parseFuncDecl() *ast.FuncDecl { |
if p.trace { |
defer un(trace(p, "FunctionDecl")) |
@@ -2150,7 +2054,6 @@ |
return decl |
} |
- |
func (p *parser) parseDecl() ast.Decl { |
if p.trace { |
defer un(trace(p, "Declaration")) |
@@ -2181,7 +2084,6 @@ |
return p.parseGenDecl(p.tok, f) |
} |
- |
func (p *parser) parseDeclList() (list []ast.Decl) { |
if p.trace { |
defer un(trace(p, "DeclList")) |
@@ -2194,7 +2096,6 @@ |
return |
} |
- |
// ---------------------------------------------------------------------------- |
// Source files |