1
0
mirror of https://github.com/golang/go synced 2024-11-22 03:14:41 -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:])) name := strings.TrimSpace(string(c.Text[9:]))
if name == "" { if name == "" {
error(c.Position, "export missing name") error(c.Pos(), "export missing name")
} }
f.ExpFunc = append(f.ExpFunc, &ExpFunc{ 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 // 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) return newSnippet(dd, id)
} }

View File

@ -65,8 +65,13 @@ type Decl interface {
// A Comment node represents a single //-style or /*-style comment. // A Comment node represents a single //-style or /*-style comment.
type Comment struct { 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) Text []byte // comment text (excluding '\n' for //-style comments)
}
func (c *Comment) Pos() token.Position {
return c.Slash
} }
@ -135,29 +140,29 @@ type (
// created. // created.
// //
BadExpr struct { BadExpr struct {
token.Position // beginning position of bad expression Begin token.Position // beginning position of bad expression
} }
// An Ident node represents an identifier. // An Ident node represents an identifier.
Ident struct { Ident struct {
token.Position // identifier position NamePos token.Position // identifier position
Name string // identifier name Name string // identifier name
Obj *Object // denoted object; or nil Obj *Object // denoted object; or nil
} }
// An Ellipsis node stands for the "..." type in a // An Ellipsis node stands for the "..." type in a
// parameter list or the "..." length in an array type. // parameter list or the "..." length in an array type.
// //
Ellipsis struct { Ellipsis struct {
token.Position // position of "..." Ellipsis token.Position // position of "..."
Elt Expr // ellipsis element type (parameter lists only) Elt Expr // ellipsis element type (parameter lists only)
} }
// A BasicLit node represents a literal of basic type. // A BasicLit node represents a literal of basic type.
BasicLit struct { 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 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` Value []byte // literal string; e.g. 42, 0x7f, 3.14, 1e-9, 2.4i, 'a', '\x7f', "foo" or `\m\n\o`
} }
// A FuncLit node represents a function literal. // A FuncLit node represents a function literal.
@ -176,9 +181,9 @@ type (
// A ParenExpr node represents a parenthesized expression. // A ParenExpr node represents a parenthesized expression.
ParenExpr struct { ParenExpr struct {
token.Position // position of "(" Lparen token.Position // position of "("
X Expr // parenthesized expression X Expr // parenthesized expression
Rparen token.Position // position of ")" Rparen token.Position // position of ")"
} }
// A SelectorExpr node represents an expression followed by a selector. // A SelectorExpr node represents an expression followed by a selector.
@ -221,17 +226,17 @@ type (
// Semantically it could be a unary "*" expression, or a pointer type. // Semantically it could be a unary "*" expression, or a pointer type.
// //
StarExpr struct { StarExpr struct {
token.Position // position of "*" Star token.Position // position of "*"
X Expr // operand X Expr // operand
} }
// A UnaryExpr node represents a unary expression. // A UnaryExpr node represents a unary expression.
// Unary "*" expressions are represented via StarExpr nodes. // Unary "*" expressions are represented via StarExpr nodes.
// //
UnaryExpr struct { UnaryExpr struct {
token.Position // position of Op OpPos token.Position // position of Op
Op token.Token // operator Op token.Token // operator
X Expr // operand X Expr // operand
} }
// A BinaryExpr node represents a binary expression. // A BinaryExpr node represents a binary expression.
@ -271,67 +276,79 @@ const (
type ( type (
// An ArrayType node represents an array or slice type. // An ArrayType node represents an array or slice type.
ArrayType struct { ArrayType struct {
token.Position // position of "[" Lbrack token.Position // position of "["
Len Expr // Ellipsis node for [...]T array types, nil for slice types Len Expr // Ellipsis node for [...]T array types, nil for slice types
Elt Expr // element type Elt Expr // element type
} }
// A StructType node represents a struct type. // A StructType node represents a struct type.
StructType struct { StructType struct {
token.Position // position of "struct" keyword Struct token.Position // position of "struct" keyword
Fields *FieldList // list of field declarations Fields *FieldList // list of field declarations
Incomplete bool // true if (source) fields are missing in the Fields list Incomplete bool // true if (source) fields are missing in the Fields list
} }
// Pointer types are represented via StarExpr nodes. // Pointer types are represented via StarExpr nodes.
// A FuncType node represents a function type. // A FuncType node represents a function type.
FuncType struct { FuncType struct {
token.Position // position of "func" keyword Func token.Position // position of "func" keyword
Params *FieldList // (incoming) parameters Params *FieldList // (incoming) parameters
Results *FieldList // (outgoing) results Results *FieldList // (outgoing) results
} }
// An InterfaceType node represents an interface type. // An InterfaceType node represents an interface type.
InterfaceType struct { InterfaceType struct {
token.Position // position of "interface" keyword Interface token.Position // position of "interface" keyword
Methods *FieldList // list of methods Methods *FieldList // list of methods
Incomplete bool // true if (source) methods are missing in the Methods list Incomplete bool // true if (source) methods are missing in the Methods list
} }
// A MapType node represents a map type. // A MapType node represents a map type.
MapType struct { MapType struct {
token.Position // position of "map" keyword Map token.Position // position of "map" keyword
Key Expr Key Expr
Value Expr Value Expr
} }
// A ChanType node represents a channel type. // A ChanType node represents a channel type.
ChanType struct { 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 Dir ChanDir // channel direction
Value Expr // value type Value Expr // value type
} }
) )
// Pos() implementations for expression/type where the position // Pos() implementations for expression/type nodes.
// corresponds to the position of a sub-node.
// //
func (x *FuncLit) Pos() token.Position { return x.Type.Pos() } 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 { func (x *CompositeLit) Pos() token.Position {
if x.Type != nil { if x.Type != nil {
return x.Type.Pos() return x.Type.Pos()
} }
return x.Lbrace return x.Lbrace
} }
func (x *ParenExpr) Pos() token.Position { return x.Lparen }
func (x *SelectorExpr) Pos() token.Position { return x.X.Pos() } func (x *SelectorExpr) Pos() token.Position { return x.X.Pos() }
func (x *IndexExpr) 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 *SliceExpr) Pos() token.Position { return x.X.Pos() }
func (x *TypeAssertExpr) 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 *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 *BinaryExpr) Pos() token.Position { return x.X.Pos() }
func (x *KeyValueExpr) Pos() token.Position { return x.Key.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 // exprNode() ensures that only expression/type nodes can be
@ -408,7 +425,7 @@ type (
// created. // created.
// //
BadStmt struct { 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. // A DeclStmt node represents a declaration in a statement list.
@ -421,7 +438,7 @@ type (
// of the immediately preceeding semicolon. // of the immediately preceeding semicolon.
// //
EmptyStmt struct { EmptyStmt struct {
token.Position // position of preceeding ";" Semicolon token.Position // position of preceeding ";"
} }
// A LabeledStmt node represents a labeled statement. // A LabeledStmt node represents a labeled statement.
@ -455,123 +472,138 @@ type (
// A GoStmt node represents a go statement. // A GoStmt node represents a go statement.
GoStmt struct { GoStmt struct {
token.Position // position of "go" keyword Go token.Position // position of "go" keyword
Call *CallExpr Call *CallExpr
} }
// A DeferStmt node represents a defer statement. // A DeferStmt node represents a defer statement.
DeferStmt struct { DeferStmt struct {
token.Position // position of "defer" keyword Defer token.Position // position of "defer" keyword
Call *CallExpr Call *CallExpr
} }
// A ReturnStmt node represents a return statement. // A ReturnStmt node represents a return statement.
ReturnStmt struct { ReturnStmt struct {
token.Position // position of "return" keyword Return token.Position // position of "return" keyword
Results []Expr Results []Expr
} }
// A BranchStmt node represents a break, continue, goto, // A BranchStmt node represents a break, continue, goto,
// or fallthrough statement. // or fallthrough statement.
// //
BranchStmt struct { BranchStmt struct {
token.Position // position of Tok TokPos token.Position // position of Tok
Tok token.Token // keyword token (BREAK, CONTINUE, GOTO, FALLTHROUGH) Tok token.Token // keyword token (BREAK, CONTINUE, GOTO, FALLTHROUGH)
Label *Ident Label *Ident
} }
// A BlockStmt node represents a braced statement list. // A BlockStmt node represents a braced statement list.
BlockStmt struct { BlockStmt struct {
token.Position // position of "{" Lbrace token.Position // position of "{"
List []Stmt List []Stmt
Rbrace token.Position // position of "}" Rbrace token.Position // position of "}"
} }
// An IfStmt node represents an if statement. // An IfStmt node represents an if statement.
IfStmt struct { IfStmt struct {
token.Position // position of "if" keyword If token.Position // position of "if" keyword
Init Stmt Init Stmt
Cond Expr Cond Expr
Body *BlockStmt Body *BlockStmt
Else Stmt Else Stmt
} }
// A CaseClause represents a case of an expression switch statement. // A CaseClause represents a case of an expression switch statement.
CaseClause struct { 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 Values []Expr // nil means default case
Colon token.Position // position of ":" Colon token.Position // position of ":"
Body []Stmt // statement list; or nil Body []Stmt // statement list; or nil
} }
// A SwitchStmt node represents an expression switch statement. // A SwitchStmt node represents an expression switch statement.
SwitchStmt struct { SwitchStmt struct {
token.Position // position of "switch" keyword Switch token.Position // position of "switch" keyword
Init Stmt Init Stmt
Tag Expr Tag Expr
Body *BlockStmt // CaseClauses only Body *BlockStmt // CaseClauses only
} }
// A TypeCaseClause represents a case of a type switch statement. // A TypeCaseClause represents a case of a type switch statement.
TypeCaseClause struct { 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 Types []Expr // nil means default case
Colon token.Position // position of ":" Colon token.Position // position of ":"
Body []Stmt // statement list; or nil Body []Stmt // statement list; or nil
} }
// An TypeSwitchStmt node represents a type switch statement. // An TypeSwitchStmt node represents a type switch statement.
TypeSwitchStmt struct { TypeSwitchStmt struct {
token.Position // position of "switch" keyword Switch token.Position // position of "switch" keyword
Init Stmt Init Stmt
Assign Stmt // x := y.(type) Assign Stmt // x := y.(type)
Body *BlockStmt // TypeCaseClauses only Body *BlockStmt // TypeCaseClauses only
} }
// A CommClause node represents a case of a select statement. // A CommClause node represents a case of a select statement.
CommClause struct { 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) Tok token.Token // ASSIGN or DEFINE (valid only if Lhs != nil)
Lhs, Rhs Expr // Rhs == nil means default case Lhs, Rhs Expr // Rhs == nil means default case
Colon token.Position // position of ":" Colon token.Position // position of ":"
Body []Stmt // statement list; or nil Body []Stmt // statement list; or nil
} }
// An SelectStmt node represents a select statement. // An SelectStmt node represents a select statement.
SelectStmt struct { SelectStmt struct {
token.Position // position of "select" keyword Select token.Position // position of "select" keyword
Body *BlockStmt // CommClauses only Body *BlockStmt // CommClauses only
} }
// A ForStmt represents a for statement. // A ForStmt represents a for statement.
ForStmt struct { ForStmt struct {
token.Position // position of "for" keyword For token.Position // position of "for" keyword
Init Stmt Init Stmt
Cond Expr Cond Expr
Post Stmt Post Stmt
Body *BlockStmt Body *BlockStmt
} }
// A RangeStmt represents a for statement with a range clause. // A RangeStmt represents a for statement with a range clause.
RangeStmt struct { RangeStmt struct {
token.Position // position of "for" keyword For token.Position // position of "for" keyword
Key, Value Expr // Value may be nil Key, Value Expr // Value may be nil
TokPos token.Position // position of Tok TokPos token.Position // position of Tok
Tok token.Token // ASSIGN, DEFINE Tok token.Token // ASSIGN, DEFINE
X Expr // value to range over X Expr // value to range over
Body *BlockStmt Body *BlockStmt
} }
) )
// Pos() implementations for statement nodes where the position // Pos() implementations for statement nodes.
// corresponds to the position of a sub-node.
// //
func (s *DeclStmt) Pos() token.Position { return s.Decl.Pos() } func (s *BadStmt) Pos() token.Position { return s.Begin }
func (s *LabeledStmt) Pos() token.Position { return s.Label.Pos() } func (s *DeclStmt) Pos() token.Position { return s.Decl.Pos() }
func (s *ExprStmt) Pos() token.Position { return s.X.Pos() } func (s *EmptyStmt) Pos() token.Position { return s.Semicolon }
func (s *IncDecStmt) Pos() token.Position { return s.X.Pos() } func (s *LabeledStmt) Pos() token.Position { return s.Label.Pos() }
func (s *AssignStmt) Pos() token.Position { return s.Lhs[0].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 // stmtNode() ensures that only statement nodes can be
@ -650,7 +682,6 @@ func (s *ImportSpec) Pos() token.Position {
} }
return s.Path.Pos() return s.Path.Pos()
} }
func (s *ValueSpec) Pos() token.Position { return s.Names[0].Pos() } func (s *ValueSpec) Pos() token.Position { return s.Names[0].Pos() }
func (s *TypeSpec) Pos() token.Position { return s.Name.Pos() } func (s *TypeSpec) Pos() token.Position { return s.Name.Pos() }
@ -671,7 +702,7 @@ type (
// created. // created.
// //
BadDecl struct { 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, // A GenDecl node (generic declaration node) represents an import,
@ -686,12 +717,12 @@ type (
// token.VAR *ValueSpec // token.VAR *ValueSpec
// //
GenDecl struct { GenDecl struct {
Doc *CommentGroup // associated documentation; or nil Doc *CommentGroup // associated documentation; or nil
token.Position // position of Tok TokPos token.Position // position of Tok
Tok token.Token // IMPORT, CONST, TYPE, VAR Tok token.Token // IMPORT, CONST, TYPE, VAR
Lparen token.Position // position of '(', if any Lparen token.Position // position of '(', if any
Specs []Spec Specs []Spec
Rparen token.Position // position of ')', if any Rparen token.Position // position of ')', if any
} }
// A FuncDecl node represents a function declaration. // A FuncDecl node represents a function declaration.
@ -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() } func (d *FuncDecl) Pos() token.Position { return d.Type.Pos() }
@ -727,14 +761,17 @@ func (d *FuncDecl) declNode() {}
// via Doc and Comment fields. // via Doc and Comment fields.
// //
type File struct { type File struct {
Doc *CommentGroup // associated documentation; or nil Doc *CommentGroup // associated documentation; or nil
token.Position // position of "package" keyword Package token.Position // position of "package" keyword
Name *Ident // package name Name *Ident // package name
Decls []Decl // top-level declarations Decls []Decl // top-level declarations
Comments []*CommentGroup // list of all comments in the source file 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 // A Package node represents a set of source files
// collectively building a Go package. // 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 { func CheckFile(file *ast.File, importer Importer) os.Error {
// create a single-file dummy package // 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) return CheckPackage(pkg, importer)
} }