1
0
mirror of https://github.com/golang/go synced 2024-11-21 21:34:40 -07:00

go/ast: change embedded token.Position fields to named fields

This is in preparation for a different position representation.
It also resolves situations where a node would be printed as
it's node position simply because the embedded token.Position
has a String method.

R=r
CC=golang-dev
https://golang.org/cl/2991041
This commit is contained in:
Robert Griesemer 2010-11-09 11:26:54 -08:00
parent 5928e1d4dc
commit 56bfe95e7f
4 changed files with 154 additions and 117 deletions

View File

@ -174,7 +174,7 @@ func (f *File) saveExport(x interface{}, context string) {
name := strings.TrimSpace(string(c.Text[9:]))
if name == "" {
error(c.Position, "export missing name")
error(c.Pos(), "export missing name")
}
f.ExpFunc = append(f.ExpFunc, &ExpFunc{

View File

@ -80,7 +80,7 @@ func genSnippet(d *ast.GenDecl, id *ast.Ident) *Snippet {
}
// only use the spec containing the id for the snippet
dd := &ast.GenDecl{d.Doc, d.Position, d.Tok, d.Lparen, []ast.Spec{s}, d.Rparen}
dd := &ast.GenDecl{d.Doc, d.Pos(), d.Tok, d.Lparen, []ast.Spec{s}, d.Rparen}
return newSnippet(dd, id)
}

View File

@ -65,11 +65,16 @@ type Decl interface {
// A Comment node represents a single //-style or /*-style comment.
type Comment struct {
token.Position // beginning position of the comment
Slash token.Position // position of "/" starting the comment
Text []byte // comment text (excluding '\n' for //-style comments)
}
func (c *Comment) Pos() token.Position {
return c.Slash
}
// A CommentGroup represents a sequence of comments
// with no other tokens and no empty lines between.
//
@ -135,12 +140,12 @@ type (
// created.
//
BadExpr struct {
token.Position // beginning position of bad expression
Begin token.Position // beginning position of bad expression
}
// An Ident node represents an identifier.
Ident struct {
token.Position // identifier position
NamePos token.Position // identifier position
Name string // identifier name
Obj *Object // denoted object; or nil
}
@ -149,13 +154,13 @@ type (
// parameter list or the "..." length in an array type.
//
Ellipsis struct {
token.Position // position of "..."
Ellipsis token.Position // position of "..."
Elt Expr // ellipsis element type (parameter lists only)
}
// A BasicLit node represents a literal of basic type.
BasicLit struct {
token.Position // literal position
ValuePos token.Position // literal position
Kind token.Token // token.INT, token.FLOAT, token.IMAG, token.CHAR, or token.STRING
Value []byte // literal string; e.g. 42, 0x7f, 3.14, 1e-9, 2.4i, 'a', '\x7f', "foo" or `\m\n\o`
}
@ -176,7 +181,7 @@ type (
// A ParenExpr node represents a parenthesized expression.
ParenExpr struct {
token.Position // position of "("
Lparen token.Position // position of "("
X Expr // parenthesized expression
Rparen token.Position // position of ")"
}
@ -221,7 +226,7 @@ type (
// Semantically it could be a unary "*" expression, or a pointer type.
//
StarExpr struct {
token.Position // position of "*"
Star token.Position // position of "*"
X Expr // operand
}
@ -229,7 +234,7 @@ type (
// Unary "*" expressions are represented via StarExpr nodes.
//
UnaryExpr struct {
token.Position // position of Op
OpPos token.Position // position of Op
Op token.Token // operator
X Expr // operand
}
@ -271,14 +276,14 @@ const (
type (
// An ArrayType node represents an array or slice type.
ArrayType struct {
token.Position // position of "["
Lbrack token.Position // position of "["
Len Expr // Ellipsis node for [...]T array types, nil for slice types
Elt Expr // element type
}
// A StructType node represents a struct type.
StructType struct {
token.Position // position of "struct" keyword
Struct token.Position // position of "struct" keyword
Fields *FieldList // list of field declarations
Incomplete bool // true if (source) fields are missing in the Fields list
}
@ -287,37 +292,40 @@ type (
// A FuncType node represents a function type.
FuncType struct {
token.Position // position of "func" keyword
Func token.Position // position of "func" keyword
Params *FieldList // (incoming) parameters
Results *FieldList // (outgoing) results
}
// An InterfaceType node represents an interface type.
InterfaceType struct {
token.Position // position of "interface" keyword
Interface token.Position // position of "interface" keyword
Methods *FieldList // list of methods
Incomplete bool // true if (source) methods are missing in the Methods list
}
// A MapType node represents a map type.
MapType struct {
token.Position // position of "map" keyword
Map token.Position // position of "map" keyword
Key Expr
Value Expr
}
// A ChanType node represents a channel type.
ChanType struct {
token.Position // position of "chan" keyword or "<-" (whichever comes first)
Begin token.Position // position of "chan" keyword or "<-" (whichever comes first)
Dir ChanDir // channel direction
Value Expr // value type
}
)
// Pos() implementations for expression/type where the position
// corresponds to the position of a sub-node.
// Pos() implementations for expression/type nodes.
//
func (x *BadExpr) Pos() token.Position { return x.Begin }
func (x *Ident) Pos() token.Position { return x.NamePos }
func (x *Ellipsis) Pos() token.Position { return x.Ellipsis }
func (x *BasicLit) Pos() token.Position { return x.ValuePos }
func (x *FuncLit) Pos() token.Position { return x.Type.Pos() }
func (x *CompositeLit) Pos() token.Position {
if x.Type != nil {
@ -325,13 +333,22 @@ func (x *CompositeLit) Pos() token.Position {
}
return x.Lbrace
}
func (x *ParenExpr) Pos() token.Position { return x.Lparen }
func (x *SelectorExpr) Pos() token.Position { return x.X.Pos() }
func (x *IndexExpr) Pos() token.Position { return x.X.Pos() }
func (x *SliceExpr) Pos() token.Position { return x.X.Pos() }
func (x *TypeAssertExpr) Pos() token.Position { return x.X.Pos() }
func (x *CallExpr) Pos() token.Position { return x.Fun.Pos() }
func (x *StarExpr) Pos() token.Position { return x.Star }
func (x *UnaryExpr) Pos() token.Position { return x.OpPos }
func (x *BinaryExpr) Pos() token.Position { return x.X.Pos() }
func (x *KeyValueExpr) Pos() token.Position { return x.Key.Pos() }
func (x *ArrayType) Pos() token.Position { return x.Lbrack }
func (x *StructType) Pos() token.Position { return x.Struct }
func (x *FuncType) Pos() token.Position { return x.Func }
func (x *InterfaceType) Pos() token.Position { return x.Interface }
func (x *MapType) Pos() token.Position { return x.Map }
func (x *ChanType) Pos() token.Position { return x.Begin }
// exprNode() ensures that only expression/type nodes can be
@ -408,7 +425,7 @@ type (
// created.
//
BadStmt struct {
token.Position // beginning position of bad statement
Begin token.Position // beginning position of bad statement
}
// A DeclStmt node represents a declaration in a statement list.
@ -421,7 +438,7 @@ type (
// of the immediately preceeding semicolon.
//
EmptyStmt struct {
token.Position // position of preceeding ";"
Semicolon token.Position // position of preceeding ";"
}
// A LabeledStmt node represents a labeled statement.
@ -455,19 +472,19 @@ type (
// A GoStmt node represents a go statement.
GoStmt struct {
token.Position // position of "go" keyword
Go token.Position // position of "go" keyword
Call *CallExpr
}
// A DeferStmt node represents a defer statement.
DeferStmt struct {
token.Position // position of "defer" keyword
Defer token.Position // position of "defer" keyword
Call *CallExpr
}
// A ReturnStmt node represents a return statement.
ReturnStmt struct {
token.Position // position of "return" keyword
Return token.Position // position of "return" keyword
Results []Expr
}
@ -475,21 +492,21 @@ type (
// or fallthrough statement.
//
BranchStmt struct {
token.Position // position of Tok
TokPos token.Position // position of Tok
Tok token.Token // keyword token (BREAK, CONTINUE, GOTO, FALLTHROUGH)
Label *Ident
}
// A BlockStmt node represents a braced statement list.
BlockStmt struct {
token.Position // position of "{"
Lbrace token.Position // position of "{"
List []Stmt
Rbrace token.Position // position of "}"
}
// An IfStmt node represents an if statement.
IfStmt struct {
token.Position // position of "if" keyword
If token.Position // position of "if" keyword
Init Stmt
Cond Expr
Body *BlockStmt
@ -498,7 +515,7 @@ type (
// A CaseClause represents a case of an expression switch statement.
CaseClause struct {
token.Position // position of "case" or "default" keyword
Case token.Position // position of "case" or "default" keyword
Values []Expr // nil means default case
Colon token.Position // position of ":"
Body []Stmt // statement list; or nil
@ -506,7 +523,7 @@ type (
// A SwitchStmt node represents an expression switch statement.
SwitchStmt struct {
token.Position // position of "switch" keyword
Switch token.Position // position of "switch" keyword
Init Stmt
Tag Expr
Body *BlockStmt // CaseClauses only
@ -514,7 +531,7 @@ type (
// A TypeCaseClause represents a case of a type switch statement.
TypeCaseClause struct {
token.Position // position of "case" or "default" keyword
Case token.Position // position of "case" or "default" keyword
Types []Expr // nil means default case
Colon token.Position // position of ":"
Body []Stmt // statement list; or nil
@ -522,7 +539,7 @@ type (
// An TypeSwitchStmt node represents a type switch statement.
TypeSwitchStmt struct {
token.Position // position of "switch" keyword
Switch token.Position // position of "switch" keyword
Init Stmt
Assign Stmt // x := y.(type)
Body *BlockStmt // TypeCaseClauses only
@ -530,7 +547,7 @@ type (
// A CommClause node represents a case of a select statement.
CommClause struct {
token.Position // position of "case" or "default" keyword
Case token.Position // position of "case" or "default" keyword
Tok token.Token // ASSIGN or DEFINE (valid only if Lhs != nil)
Lhs, Rhs Expr // Rhs == nil means default case
Colon token.Position // position of ":"
@ -539,13 +556,13 @@ type (
// An SelectStmt node represents a select statement.
SelectStmt struct {
token.Position // position of "select" keyword
Select token.Position // position of "select" keyword
Body *BlockStmt // CommClauses only
}
// A ForStmt represents a for statement.
ForStmt struct {
token.Position // position of "for" keyword
For token.Position // position of "for" keyword
Init Stmt
Cond Expr
Post Stmt
@ -554,7 +571,7 @@ type (
// A RangeStmt represents a for statement with a range clause.
RangeStmt struct {
token.Position // position of "for" keyword
For token.Position // position of "for" keyword
Key, Value Expr // Value may be nil
TokPos token.Position // position of Tok
Tok token.Token // ASSIGN, DEFINE
@ -564,14 +581,29 @@ type (
)
// Pos() implementations for statement nodes where the position
// corresponds to the position of a sub-node.
// Pos() implementations for statement nodes.
//
func (s *BadStmt) Pos() token.Position { return s.Begin }
func (s *DeclStmt) Pos() token.Position { return s.Decl.Pos() }
func (s *EmptyStmt) Pos() token.Position { return s.Semicolon }
func (s *LabeledStmt) Pos() token.Position { return s.Label.Pos() }
func (s *ExprStmt) Pos() token.Position { return s.X.Pos() }
func (s *IncDecStmt) Pos() token.Position { return s.X.Pos() }
func (s *AssignStmt) Pos() token.Position { return s.Lhs[0].Pos() }
func (s *GoStmt) Pos() token.Position { return s.Go }
func (s *DeferStmt) Pos() token.Position { return s.Defer }
func (s *ReturnStmt) Pos() token.Position { return s.Return }
func (s *BranchStmt) Pos() token.Position { return s.TokPos }
func (s *BlockStmt) Pos() token.Position { return s.Lbrace }
func (s *IfStmt) Pos() token.Position { return s.If }
func (s *CaseClause) Pos() token.Position { return s.Case }
func (s *SwitchStmt) Pos() token.Position { return s.Switch }
func (s *TypeCaseClause) Pos() token.Position { return s.Case }
func (s *TypeSwitchStmt) Pos() token.Position { return s.Switch }
func (s *CommClause) Pos() token.Position { return s.Case }
func (s *SelectStmt) Pos() token.Position { return s.Select }
func (s *ForStmt) Pos() token.Position { return s.For }
func (s *RangeStmt) Pos() token.Position { return s.For }
// stmtNode() ensures that only statement nodes can be
@ -650,7 +682,6 @@ func (s *ImportSpec) Pos() token.Position {
}
return s.Path.Pos()
}
func (s *ValueSpec) Pos() token.Position { return s.Names[0].Pos() }
func (s *TypeSpec) Pos() token.Position { return s.Name.Pos() }
@ -671,7 +702,7 @@ type (
// created.
//
BadDecl struct {
token.Position // beginning position of bad declaration
Begin token.Position // beginning position of bad declaration
}
// A GenDecl node (generic declaration node) represents an import,
@ -687,7 +718,7 @@ type (
//
GenDecl struct {
Doc *CommentGroup // associated documentation; or nil
token.Position // position of Tok
TokPos token.Position // position of Tok
Tok token.Token // IMPORT, CONST, TYPE, VAR
Lparen token.Position // position of '(', if any
Specs []Spec
@ -705,7 +736,10 @@ type (
)
// The position of a FuncDecl node is the position of its function type.
// Pos implementations for declaration nodes.
//
func (d *BadDecl) Pos() token.Position { return d.Begin }
func (d *GenDecl) Pos() token.Position { return d.TokPos }
func (d *FuncDecl) Pos() token.Position { return d.Type.Pos() }
@ -728,13 +762,16 @@ func (d *FuncDecl) declNode() {}
//
type File struct {
Doc *CommentGroup // associated documentation; or nil
token.Position // position of "package" keyword
Package token.Position // position of "package" keyword
Name *Ident // package name
Decls []Decl // top-level declarations
Comments []*CommentGroup // list of all comments in the source file
}
func (f *File) Pos() token.Position { return f.Package }
// A Package node represents a set of source files
// collectively building a Go package.
//

View File

@ -51,7 +51,7 @@ func CheckPackage(pkg *ast.Package, importer Importer) os.Error {
//
func CheckFile(file *ast.File, importer Importer) os.Error {
// create a single-file dummy package
pkg := &ast.Package{file.Name.Name, nil, map[string]*ast.File{file.Name.Position.Filename: file}}
pkg := &ast.Package{file.Name.Name, nil, map[string]*ast.File{file.Name.NamePos.Filename: file}}
return CheckPackage(pkg, importer)
}