Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
bafd8c39
Commit
bafd8c39
authored
Mar 25, 2009
by
Robert Griesemer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
AST for Go programs
R=rsc,r DELTA=309 (67 added, 51 deleted, 191 changed) OCL=26611 CL=26745
parent
21d03496
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
236 additions
and
220 deletions
+236
-220
usr/gri/pretty/ast.go
usr/gri/pretty/ast.go
+236
-220
No files found.
usr/gri/pretty/ast.go
View file @
bafd8c39
...
...
@@ -17,13 +17,6 @@ import (
type
Position
scanner
.
Location
// TODO try to get rid of these
type
(
Block
struct
;
Signature
struct
;
)
// ----------------------------------------------------------------------------
// Interfaces
//
...
...
@@ -43,12 +36,13 @@ type (
// TODO: For comment positioning only the byte position and not
// a complete Position field is needed. May be able to trim node
// sizes a bit.
// sizes a bit. Then, embed Position field so we can get rid of
// most of the Pos() methods.
type
(
ExprVisitor
interface
;
St
a
tVisitor
interface
;
St
m
tVisitor
interface
;
DeclVisitor
interface
;
)
...
...
@@ -65,12 +59,12 @@ type Expr interface {
}
// All statement nodes implement the St
a
t interface.
type
St
a
t
interface
{
// All statement nodes implement the St
m
t interface.
type
St
m
t
interface
{
// For a (dynamic) node type X, calling Visit with a statement
// visitor v invokes the node-specific DoX function of the visitor.
//
Visit
(
v
St
a
tVisitor
);
Visit
(
v
St
m
tVisitor
);
// Pos returns the (beginning) position of the statement.
Pos
()
Position
;
...
...
@@ -109,6 +103,25 @@ type Comments []*Comment
// ----------------------------------------------------------------------------
// Expressions and types
// Support types.
type
(
Ident
struct
;
StringLit
struct
;
FunctionType
struct
;
BlockStmt
struct
;
// A Field represents a Field declaration list in a struct type,
// a method in an interface type, or a parameter/result declaration
// in a signature.
Field
struct
{
Doc
Comments
;
// associated documentation; or nil
Names
[]
*
Ident
;
// field/method/parameter names; nil if anonymous field
Type
Expr
;
// field/method/parameter type
Tag
[]
*
StringLit
;
// field tag; nil if no tag
};
)
// An expression is represented by a tree consisting of one
// or more of the following concrete expression nodes.
//
...
...
@@ -127,28 +140,55 @@ type (
Lit
[]
byte
;
// identifier string (e.g. foobar)
};
// A BasicLit node represents a basic literal.
BasicLit
struct
{
// An Ellipsis node stands for the "..." type in a
// parameter list or the "..." length in an array type.
//
Ellipsis
struct
{
Pos_
Position
;
// position of "..."
};
// An IntLit node represents an integer literal.
IntLit
struct
{
Pos_
Position
;
// literal string position
Tok
int
;
// literal token (INT, FLOAT, CHAR, STRING)
Lit
[]
byte
;
// literal string
Lit
[]
byte
;
// literal string; e.g. 42 or 0x7f
};
// A StringLit node represents a sequence of string literals.
// A FloatLit node represents a floating-point literal.
FloatLit
struct
{
Pos_
Position
;
// literal string position
Lit
[]
byte
;
// literal string; e.g. 3.14 or 1e-9
};
// A CharLit node represents a character literal.
CharLit
struct
{
Pos_
Position
;
// literal string position
Lit
[]
byte
;
// literal string, including quotes; e.g. 'a' or '\x7f'
};
// A StringLit node represents a string literal.
StringLit
struct
{
Strings
[]
*
BasicLit
;
// sequence of strings
Pos_
Position
;
// literal string position
Lit
[]
byte
;
// literal string, including quotes; e.g. "foo" or `\m\n\o`
};
// A StringList node represents a sequence of adjacent string literals.
// A single string literal (common case) is represented by a StringLit
// node; StringList nodes are used only if there are two or more string
// literals in a sequence.
//
StringList
struct
{
Strings
[]
*
StringLit
;
// list of strings, len(Strings) > 1
};
// A FunctionLit node represents a function literal.
FunctionLit
struct
{
Func
Position
;
// position of "func" keyword
Typ
*
Signature
;
// function signature
Body
*
Block
;
// function body
Type
*
FunctionType
;
// function type
Body
*
BlockStmt
;
// function body
};
// A CompositeLit node represents a composite literal.
CompositeLit
struct
{
Typ
Expr
;
// literal type
Typ
e
Expr
;
// literal type
Lbrace
Position
;
// position of "{"
Elts
[]
Expr
;
// list of composite elements
Rbrace
Position
;
// position of "}"
...
...
@@ -161,33 +201,33 @@ type (
Rparen
Position
;
// position of ")"
};
// A SelectorExpr node represents a
primary
expression followed by a selector.
// A SelectorExpr node represents a
n
expression followed by a selector.
SelectorExpr
struct
{
X
Expr
;
//
primary
expression
X
Expr
;
// expression
Sel
*
Ident
;
// field selector
};
// An IndexExpr node represents a
primary
expression followed by an index.
// An IndexExpr node represents a
n
expression followed by an index.
IndexExpr
struct
{
X
Expr
;
//
primary
expression
X
Expr
;
// expression
Index
Expr
;
// index expression
};
// A SliceExpr node represents a
primary
expression followed by a slice.
// A SliceExpr node represents a
n
expression followed by a slice.
SliceExpr
struct
{
X
Expr
;
//
primary
expression
X
Expr
;
// expression
Begin
,
End
Expr
;
// slice range
};
// A TypeAssertExpr node represents a
primary
expression followed by a
// A TypeAssertExpr node represents a
n
expression followed by a
// type assertion.
//
TypeAssertExpr
struct
{
X
Expr
;
//
primary
expression
Typ
Expr
;
// asserted type
X
Expr
;
// expression
Typ
e
Expr
;
// asserted type
};
// A CallExpr node represents a
primary
expression followed by an argument list.
// A CallExpr node represents a
n
expression followed by an argument list.
CallExpr
struct
{
Fun
Expr
;
// function expression
Lparen
Position
;
// position of "("
...
...
@@ -236,17 +276,10 @@ const (
// nodes.
//
type
(
// An Ellipsis node stands for the "..." type in a
// parameter list or the "..." length in an array type.
//
Ellipsis
struct
{
// neither a type nor an expression
Pos_
Position
;
// position of "..."
};
// An ArrayType node represents an array type.
ArrayType
struct
{
Lbrack
Position
;
// position of "["
Len
Expr
;
// an Ellipsis node for [...]T array types
Len
Expr
;
//
possibly
an Ellipsis node for [...]T array types
Elt
Expr
;
// element type
};
...
...
@@ -256,16 +289,6 @@ type (
Elt
Expr
;
// element type
};
// A Field represents a Field declaration list in a struct type,
// a method in an interface type, or a parameter declaration in
// a signature.
Field
struct
{
Doc
Comments
;
// associated documentation (struct types only)
Names
[]
*
Ident
;
// field/method/parameter names; nil if anonymous field
Typ
Expr
;
// field/method/parameter type
Tag
Expr
;
// field tag; nil if no tag
};
// A StructType node represents a struct type.
StructType
struct
{
Struct
,
Lbrace
Position
;
// positions of "struct" keyword, "{"
...
...
@@ -273,20 +296,13 @@ type (
Rbrace
Position
;
// position of "}"
};
// Note: pointer types are represented via StarExpr nodes.
// A signature node represents the parameter and result
// sections of a function type only.
//
Signature
struct
{
Params
[]
*
Field
;
Result
[]
*
Field
;
};
// Pointer types are represented via StarExpr nodes.
// A FunctionType node represents a function type.
FunctionType
struct
{
Func
Position
;
// position of "func" keyword
Sig
*
Signature
;
Params
[]
*
Field
;
// (incoming) parameters
Results
[]
*
Field
;
// (outgoing) results
};
// An InterfaceType node represents an interface type.
...
...
@@ -306,7 +322,7 @@ type (
// A ChannelType node represents a channel type.
ChannelType
struct
{
Pos_
Position
;
// position of "chan" keyword or "<-" (whichever comes first)
Dir
ChanDir
;
Dir
ChanDir
;
// channel direction
Value
Expr
;
// value type
};
)
...
...
@@ -316,10 +332,13 @@ type (
//
func
(
x
*
BadExpr
)
Pos
()
Position
{
return
x
.
Pos_
;
}
func
(
x
*
Ident
)
Pos
()
Position
{
return
x
.
Pos_
;
}
func
(
x
*
BasicLit
)
Pos
()
Position
{
return
x
.
Pos_
;
}
func
(
x
*
StringLit
)
Pos
()
Position
{
return
x
.
Strings
[
0
]
.
Pos
();
}
func
(
x
*
FunctionLit
)
Pos
()
Position
{
return
x
.
Func
;
}
func
(
x
*
CompositeLit
)
Pos
()
Position
{
return
x
.
Typ
.
Pos
();
}
func
(
x
*
IntLit
)
Pos
()
Position
{
return
x
.
Pos_
;
}
func
(
x
*
FloatLit
)
Pos
()
Position
{
return
x
.
Pos_
;
}
func
(
x
*
CharLit
)
Pos
()
Position
{
return
x
.
Pos_
;
}
func
(
x
*
StringLit
)
Pos
()
Position
{
return
x
.
Pos_
;
}
func
(
x
*
StringList
)
Pos
()
Position
{
return
x
.
Strings
[
0
]
.
Pos
();
}
func
(
x
*
FunctionLit
)
Pos
()
Position
{
return
x
.
Type
.
Func
;
}
func
(
x
*
CompositeLit
)
Pos
()
Position
{
return
x
.
Type
.
Pos
();
}
func
(
x
*
ParenExpr
)
Pos
()
Position
{
return
x
.
Lparen
;
}
func
(
x
*
SelectorExpr
)
Pos
()
Position
{
return
x
.
X
.
Pos
();
}
func
(
x
*
IndexExpr
)
Pos
()
Position
{
return
x
.
X
.
Pos
();
}
...
...
@@ -349,8 +368,11 @@ type ExprVisitor interface {
// Expressions
DoBadExpr
(
x
*
BadExpr
);
DoIdent
(
x
*
Ident
);
DoBasicLit
(
x
*
BasicLit
);
DoIntLit
(
x
*
IntLit
);
DoFloatLit
(
x
*
FloatLit
);
DoCharLit
(
x
*
CharLit
);
DoStringLit
(
x
*
StringLit
);
DoStringList
(
x
*
StringList
);
DoFunctionLit
(
x
*
FunctionLit
);
DoCompositeLit
(
x
*
CompositeLit
);
DoParenExpr
(
x
*
ParenExpr
);
...
...
@@ -379,8 +401,12 @@ type ExprVisitor interface {
//
func
(
x
*
BadExpr
)
Visit
(
v
ExprVisitor
)
{
v
.
DoBadExpr
(
x
);
}
func
(
x
*
Ident
)
Visit
(
v
ExprVisitor
)
{
v
.
DoIdent
(
x
);
}
func
(
x
*
BasicLit
)
Visit
(
v
ExprVisitor
)
{
v
.
DoBasicLit
(
x
);
}
func
(
x
*
Ellipsis
)
Visit
(
v
ExprVisitor
)
{
v
.
DoEllipsis
(
x
);
}
func
(
x
*
IntLit
)
Visit
(
v
ExprVisitor
)
{
v
.
DoIntLit
(
x
);
}
func
(
x
*
FloatLit
)
Visit
(
v
ExprVisitor
)
{
v
.
DoFloatLit
(
x
);
}
func
(
x
*
CharLit
)
Visit
(
v
ExprVisitor
)
{
v
.
DoCharLit
(
x
);
}
func
(
x
*
StringLit
)
Visit
(
v
ExprVisitor
)
{
v
.
DoStringLit
(
x
);
}
func
(
x
*
StringList
)
Visit
(
v
ExprVisitor
)
{
v
.
DoStringList
(
x
);
}
func
(
x
*
FunctionLit
)
Visit
(
v
ExprVisitor
)
{
v
.
DoFunctionLit
(
x
);
}
func
(
x
*
CompositeLit
)
Visit
(
v
ExprVisitor
)
{
v
.
DoCompositeLit
(
x
);
}
func
(
x
*
ParenExpr
)
Visit
(
v
ExprVisitor
)
{
v
.
DoParenExpr
(
x
);
}
...
...
@@ -393,7 +419,6 @@ func (x *StarExpr) Visit(v ExprVisitor) { v.DoStarExpr(x); }
func
(
x
*
UnaryExpr
)
Visit
(
v
ExprVisitor
)
{
v
.
DoUnaryExpr
(
x
);
}
func
(
x
*
BinaryExpr
)
Visit
(
v
ExprVisitor
)
{
v
.
DoBinaryExpr
(
x
);
}
func
(
x
*
Ellipsis
)
Visit
(
v
ExprVisitor
)
{
v
.
DoEllipsis
(
x
);
}
func
(
x
*
ArrayType
)
Visit
(
v
ExprVisitor
)
{
v
.
DoArrayType
(
x
);
}
func
(
x
*
SliceType
)
Visit
(
v
ExprVisitor
)
{
v
.
DoSliceType
(
x
);
}
func
(
x
*
StructType
)
Visit
(
v
ExprVisitor
)
{
v
.
DoStructType
(
x
);
}
...
...
@@ -403,22 +428,6 @@ func (x *MapType) Visit(v ExprVisitor) { v.DoMapType(x); }
func
(
x
*
ChannelType
)
Visit
(
v
ExprVisitor
)
{
v
.
DoChannelType
(
x
);
}
// ----------------------------------------------------------------------------
// Blocks
// A Block represents syntactic constructs of the form:
//
// "{" StatementList "}"
// ":" StatementList
//
type
Block
struct
{
Pos_
Position
;
Tok
int
;
List
[]
Stat
;
Rparen
Position
;
// position of closing "}" if present
}
// ----------------------------------------------------------------------------
// Statements
...
...
@@ -426,236 +435,244 @@ type Block struct {
// or more of the following concrete statement nodes.
//
type
(
// A BadSt
a
t node is a placeholder for statements containing
// A BadSt
m
t node is a placeholder for statements containing
// syntax errors for which no correct statement nodes can be
// created.
//
BadSt
a
t
struct
{
BadSt
m
t
struct
{
Pos_
Position
;
// beginning position of bad statement
};
// A DeclSt
a
t node represents a declaration in a statement list.
DeclSt
a
t
struct
{
// A DeclSt
m
t node represents a declaration in a statement list.
DeclSt
m
t
struct
{
Decl
Decl
;
};
// An EmptySt
a
t node represents an empty statement.
// An EmptySt
m
t node represents an empty statement.
// The "position" of the empty statement is the position
// of the immediately preceeding semicolon.
//
EmptySt
a
t
struct
{
EmptySt
m
t
struct
{
Semicolon
Position
;
// position of preceeding ";"
};
// A LabeledSt
a
t node represents a labeled statement.
LabeledSt
a
t
struct
{
// A LabeledSt
m
t node represents a labeled statement.
LabeledSt
m
t
struct
{
Label
*
Ident
;
St
at
Sta
t
;
St
mt
Stm
t
;
};
// An ExprSt
a
t node represents a (stand-alone) expression
// An ExprSt
m
t node represents a (stand-alone) expression
// in a statement list.
//
ExprSt
a
t
struct
{
ExprSt
m
t
struct
{
X
Expr
;
// expression
};
// An IncDecSt
a
t node represents an increment or decrement statement.
IncDecSt
a
t
struct
{
// An IncDecSt
m
t node represents an increment or decrement statement.
IncDecSt
m
t
struct
{
X
Expr
;
Tok
int
;
// INC or DEC
};
// An Assign
mentSta
t node represents an assignment or
// An Assign
Stm
t node represents an assignment or
// a short variable declaration.
Assign
mentSta
t
struct
{
Assign
Stm
t
struct
{
Lhs
[]
Expr
;
Pos_
Position
;
// token position
Tok
int
;
// assignment token, DEFINE
Rhs
[]
Expr
;
};
// A GoSt
a
t node represents a go statement.
GoSt
a
t
struct
{
// A GoSt
m
t node represents a go statement.
GoSt
m
t
struct
{
Go
Position
;
// position of "go" keyword
Call
Expr
;
Call
*
Call
Expr
;
};
// A DeferSt
a
t node represents a defer statement.
DeferSt
a
t
struct
{
// A DeferSt
m
t node represents a defer statement.
DeferSt
m
t
struct
{
Defer
Position
;
// position of "defer" keyword
Call
Expr
;
Call
*
Call
Expr
;
};
// A ReturnSt
a
t node represents a return statement.
ReturnSt
a
t
struct
{
// A ReturnSt
m
t node represents a return statement.
ReturnSt
m
t
struct
{
Return
Position
;
// position of "return" keyword
Results
[]
Expr
;
};
// A
ControlFlowSta
t node represents a break, continue, goto,
// A
BranchStm
t node represents a break, continue, goto,
// or fallthrough statement.
//
ControlFlowSta
t
struct
{
BranchStm
t
struct
{
Pos_
Position
;
// position of keyword
Tok
int
;
// keyword token (BREAK, CONTINUE, GOTO, FALLTHROUGH)
Label
*
Ident
;
};
// A CompositeStat node represents a braced statement list.
CompositeStat
struct
{
Body
*
Block
;
// A BlockStmt node represents a braced statement list.
BlockStmt
struct
{
Lbrace
Position
;
List
[]
Stmt
;
Rbrace
Position
;
};
// An IfSt
a
t node represents an if statement.
IfSt
a
t
struct
{
// An IfSt
m
t node represents an if statement.
IfSt
m
t
struct
{
If
Position
;
// position of "if" keyword
Init
St
a
t
;
Init
St
m
t
;
Cond
Expr
;
Body
*
Block
;
Else
St
a
t
;
Body
*
Block
Stmt
;
Else
St
m
t
;
};
// A CaseClause represents a case of an expression switch statement.
CaseClause
struct
{
Case
Position
;
// position of "case" or "default" keyword
Values
[]
Expr
;
// nil means default case
Body
*
Block
;
Colon
Position
;
// position of ":"
Body
[]
Stmt
;
// statement list; or nil
};
// A SwitchSt
a
t node represents an expression switch statement.
SwitchSt
a
t
struct
{
// A SwitchSt
m
t node represents an expression switch statement.
SwitchSt
m
t
struct
{
Switch
Position
;
// position of "switch" keyword
Init
St
a
t
;
Init
St
m
t
;
Tag
Expr
;
Body
*
Block
;
// CaseClauses only
Body
*
Block
Stmt
;
// CaseClauses only
};
// A TypeCaseClause represents a case of a type switch statement.
TypeCaseClause
struct
{
Case
Position
;
// position of "case" or "default" keyword
Typ
Expr
;
// nil means default case
Body
*
Block
;
Type
Expr
;
// nil means default case
Colon
Position
;
// position of ":"
Body
[]
Stmt
;
// statement list; or nil
};
// An TypeSwitchSt
a
t node represents a type switch statement.
TypeSwitchSt
a
t
struct
{
// An TypeSwitchSt
m
t node represents a type switch statement.
TypeSwitchSt
m
t
struct
{
Switch
Position
;
// position of "switch" keyword
Init
St
a
t
;
Assign
St
a
t
;
// x := y.(type)
Body
*
Block
;
// TypeCaseClauses only
Init
St
m
t
;
Assign
St
m
t
;
// x := y.(type)
Body
*
Block
Stmt
;
// TypeCaseClauses only
};
// A CommClause node represents a case of a select statement.
CommClause
struct
{
Case
Position
;
// position of "case" or "default" keyword
Tok
int
;
// ASSIGN
,
DEFINE (valid only if Lhs != nil)
Tok
int
;
// ASSIGN
or
DEFINE (valid only if Lhs != nil)
Lhs
,
Rhs
Expr
;
// Rhs == nil means default case
Body
*
Block
;
Colon
Position
;
// position of ":"
Body
[]
Stmt
;
// statement list; or nil
};
// An SelectSt
a
t node represents a select statement.
SelectSt
a
t
struct
{
// An SelectSt
m
t node represents a select statement.
SelectSt
m
t
struct
{
Select
Position
;
// position of "select" keyword
Body
*
Block
;
// CommClauses only
Body
*
Block
Stmt
;
// CommClauses only
};
// A ForSt
a
t represents a for statement.
ForSt
a
t
struct
{
// A ForSt
m
t represents a for statement.
ForSt
m
t
struct
{
For
Position
;
// position of "for" keyword
Init
St
a
t
;
Init
St
m
t
;
Cond
Expr
;
Post
St
a
t
;
Body
*
Block
;
Post
St
m
t
;
Body
*
Block
Stmt
;
};
// A RangeSt
a
t represents a for statement with a range clause.
RangeSt
a
t
struct
{
// A RangeSt
m
t represents a for statement with a range clause.
RangeSt
m
t
struct
{
For
Position
;
// position of "for" keyword
Range
Stat
;
Body
*
Block
;
Key
,
Value
Expr
;
// Value may be nil
Pos_
Position
;
// token position
Tok
int
;
// ASSIGN or DEFINE
X
Expr
;
// value to range over
Body
*
BlockStmt
;
};
)
// Pos() implementations for all statement nodes.
//
func
(
s
*
BadSt
a
t
)
Pos
()
Position
{
return
s
.
Pos_
;
}
func
(
s
*
DeclSt
a
t
)
Pos
()
Position
{
return
s
.
Decl
.
Pos
();
}
func
(
s
*
EmptySt
a
t
)
Pos
()
Position
{
return
s
.
Semicolon
;
}
func
(
s
*
LabeledSt
a
t
)
Pos
()
Position
{
return
s
.
Label
.
Pos
();
}
func
(
s
*
ExprSt
a
t
)
Pos
()
Position
{
return
s
.
X
.
Pos
();
}
func
(
s
*
IncDecSt
a
t
)
Pos
()
Position
{
return
s
.
X
.
Pos
();
}
func
(
s
*
Assign
mentSta
t
)
Pos
()
Position
{
return
s
.
Lhs
[
0
]
.
Pos
();
}
func
(
s
*
GoSt
a
t
)
Pos
()
Position
{
return
s
.
Go
;
}
func
(
s
*
DeferSt
a
t
)
Pos
()
Position
{
return
s
.
Defer
;
}
func
(
s
*
ReturnSt
a
t
)
Pos
()
Position
{
return
s
.
Return
;
}
func
(
s
*
ControlFlowSta
t
)
Pos
()
Position
{
return
s
.
Pos_
;
}
func
(
s
*
CompositeStat
)
Pos
()
Position
{
return
s
.
Body
.
Pos_
;
}
func
(
s
*
IfSt
a
t
)
Pos
()
Position
{
return
s
.
If
;
}
func
(
s
*
BadSt
m
t
)
Pos
()
Position
{
return
s
.
Pos_
;
}
func
(
s
*
DeclSt
m
t
)
Pos
()
Position
{
return
s
.
Decl
.
Pos
();
}
func
(
s
*
EmptySt
m
t
)
Pos
()
Position
{
return
s
.
Semicolon
;
}
func
(
s
*
LabeledSt
m
t
)
Pos
()
Position
{
return
s
.
Label
.
Pos
();
}
func
(
s
*
ExprSt
m
t
)
Pos
()
Position
{
return
s
.
X
.
Pos
();
}
func
(
s
*
IncDecSt
m
t
)
Pos
()
Position
{
return
s
.
X
.
Pos
();
}
func
(
s
*
Assign
Stm
t
)
Pos
()
Position
{
return
s
.
Lhs
[
0
]
.
Pos
();
}
func
(
s
*
GoSt
m
t
)
Pos
()
Position
{
return
s
.
Go
;
}
func
(
s
*
DeferSt
m
t
)
Pos
()
Position
{
return
s
.
Defer
;
}
func
(
s
*
ReturnSt
m
t
)
Pos
()
Position
{
return
s
.
Return
;
}
func
(
s
*
BranchStm
t
)
Pos
()
Position
{
return
s
.
Pos_
;
}
func
(
s
*
BlockStmt
)
Pos
()
Position
{
return
s
.
Lbrace
;
}
func
(
s
*
IfSt
m
t
)
Pos
()
Position
{
return
s
.
If
;
}
func
(
s
*
CaseClause
)
Pos
()
Position
{
return
s
.
Case
;
}
func
(
s
*
SwitchSt
a
t
)
Pos
()
Position
{
return
s
.
Switch
;
}
func
(
s
*
SwitchSt
m
t
)
Pos
()
Position
{
return
s
.
Switch
;
}
func
(
s
*
TypeCaseClause
)
Pos
()
Position
{
return
s
.
Case
;
}
func
(
s
*
TypeSwitchSt
a
t
)
Pos
()
Position
{
return
s
.
Switch
;
}
func
(
s
*
TypeSwitchSt
m
t
)
Pos
()
Position
{
return
s
.
Switch
;
}
func
(
s
*
CommClause
)
Pos
()
Position
{
return
s
.
Case
;
}
func
(
s
*
SelectSt
a
t
)
Pos
()
Position
{
return
s
.
Select
;
}
func
(
s
*
ForSt
a
t
)
Pos
()
Position
{
return
s
.
For
;
}
func
(
s
*
RangeSt
a
t
)
Pos
()
Position
{
return
s
.
For
;
}
func
(
s
*
SelectSt
m
t
)
Pos
()
Position
{
return
s
.
Select
;
}
func
(
s
*
ForSt
m
t
)
Pos
()
Position
{
return
s
.
For
;
}
func
(
s
*
RangeSt
m
t
)
Pos
()
Position
{
return
s
.
For
;
}
// All statement nodes implement a Visit method which takes
// a St
a
tVisitor as argument. For a given node x of type X, and
// an implementation v of a St
a
tVisitor, calling x.Visit(v) will
// a St
m
tVisitor as argument. For a given node x of type X, and
// an implementation v of a St
m
tVisitor, calling x.Visit(v) will
// result in a call of v.DoX(x) (through a double-dispatch).
//
type
St
a
tVisitor
interface
{
DoBadSt
at
(
s
*
BadSta
t
);
DoDeclSt
at
(
s
*
DeclSta
t
);
DoEmptySt
at
(
s
*
EmptySta
t
);
DoLabeledSt
at
(
s
*
LabeledSta
t
);
DoExprSt
at
(
s
*
ExprSta
t
);
DoIncDecSt
at
(
s
*
IncDecSta
t
);
DoAssign
mentStat
(
s
*
AssignmentSta
t
);
DoGoSt
at
(
s
*
GoSta
t
);
DoDeferSt
at
(
s
*
DeferSta
t
);
DoReturnSt
at
(
s
*
ReturnSta
t
);
Do
ControlFlowStat
(
s
*
ControlFlowSta
t
);
Do
CompositeStat
(
s
*
CompositeSta
t
);
DoIfSt
at
(
s
*
IfSta
t
);
type
St
m
tVisitor
interface
{
DoBadSt
mt
(
s
*
BadStm
t
);
DoDeclSt
mt
(
s
*
DeclStm
t
);
DoEmptySt
mt
(
s
*
EmptyStm
t
);
DoLabeledSt
mt
(
s
*
LabeledStm
t
);
DoExprSt
mt
(
s
*
ExprStm
t
);
DoIncDecSt
mt
(
s
*
IncDecStm
t
);
DoAssign
Stmt
(
s
*
AssignStm
t
);
DoGoSt
mt
(
s
*
GoStm
t
);
DoDeferSt
mt
(
s
*
DeferStm
t
);
DoReturnSt
mt
(
s
*
ReturnStm
t
);
Do
BranchStmt
(
s
*
BranchStm
t
);
Do
BlockStmt
(
s
*
BlockStm
t
);
DoIfSt
mt
(
s
*
IfStm
t
);
DoCaseClause
(
s
*
CaseClause
);
DoSwitchSt
at
(
s
*
SwitchSta
t
);
DoSwitchSt
mt
(
s
*
SwitchStm
t
);
DoTypeCaseClause
(
s
*
TypeCaseClause
);
DoTypeSwitchSt
at
(
s
*
TypeSwitchSta
t
);
DoTypeSwitchSt
mt
(
s
*
TypeSwitchStm
t
);
DoCommClause
(
s
*
CommClause
);
DoSelectSt
at
(
s
*
SelectSta
t
);
DoForSt
at
(
s
*
ForSta
t
);
DoRangeSt
at
(
s
*
RangeSta
t
);
DoSelectSt
mt
(
s
*
SelectStm
t
);
DoForSt
mt
(
s
*
ForStm
t
);
DoRangeSt
mt
(
s
*
RangeStm
t
);
}
// Visit() implementations for all statement nodes.
//
func
(
s
*
BadSt
at
)
Visit
(
v
StatVisitor
)
{
v
.
DoBadSta
t
(
s
);
}
func
(
s
*
DeclSt
at
)
Visit
(
v
StatVisitor
)
{
v
.
DoDeclSta
t
(
s
);
}
func
(
s
*
EmptySt
at
)
Visit
(
v
StatVisitor
)
{
v
.
DoEmptySta
t
(
s
);
}
func
(
s
*
LabeledSt
at
)
Visit
(
v
StatVisitor
)
{
v
.
DoLabeledSta
t
(
s
);
}
func
(
s
*
ExprSt
at
)
Visit
(
v
StatVisitor
)
{
v
.
DoExprSta
t
(
s
);
}
func
(
s
*
IncDecSt
at
)
Visit
(
v
StatVisitor
)
{
v
.
DoIncDecSta
t
(
s
);
}
func
(
s
*
Assign
mentStat
)
Visit
(
v
StatVisitor
)
{
v
.
DoAssignmentSta
t
(
s
);
}
func
(
s
*
GoSt
at
)
Visit
(
v
StatVisitor
)
{
v
.
DoGoSta
t
(
s
);
}
func
(
s
*
DeferSt
at
)
Visit
(
v
StatVisitor
)
{
v
.
DoDeferSta
t
(
s
);
}
func
(
s
*
ReturnSt
at
)
Visit
(
v
StatVisitor
)
{
v
.
DoReturnSta
t
(
s
);
}
func
(
s
*
ControlFlowStat
)
Visit
(
v
StatVisitor
)
{
v
.
DoControlFlowSta
t
(
s
);
}
func
(
s
*
CompositeStat
)
Visit
(
v
StatVisitor
)
{
v
.
DoCompositeSta
t
(
s
);
}
func
(
s
*
IfSt
at
)
Visit
(
v
StatVisitor
)
{
v
.
DoIfSta
t
(
s
);
}
func
(
s
*
CaseClause
)
Visit
(
v
St
a
tVisitor
)
{
v
.
DoCaseClause
(
s
);
}
func
(
s
*
SwitchSt
at
)
Visit
(
v
StatVisitor
)
{
v
.
DoSwitchSta
t
(
s
);
}
func
(
s
*
TypeCaseClause
)
Visit
(
v
St
a
tVisitor
)
{
v
.
DoTypeCaseClause
(
s
);
}
func
(
s
*
TypeSwitchSt
at
)
Visit
(
v
StatVisitor
)
{
v
.
DoTypeSwitchSta
t
(
s
);
}
func
(
s
*
CommClause
)
Visit
(
v
St
a
tVisitor
)
{
v
.
DoCommClause
(
s
);
}
func
(
s
*
SelectSt
at
)
Visit
(
v
StatVisitor
)
{
v
.
DoSelectSta
t
(
s
);
}
func
(
s
*
ForSt
at
)
Visit
(
v
StatVisitor
)
{
v
.
DoForSta
t
(
s
);
}
func
(
s
*
RangeSt
at
)
Visit
(
v
StatVisitor
)
{
v
.
DoRangeSta
t
(
s
);
}
func
(
s
*
BadSt
mt
)
Visit
(
v
StmtVisitor
)
{
v
.
DoBadStm
t
(
s
);
}
func
(
s
*
DeclSt
mt
)
Visit
(
v
StmtVisitor
)
{
v
.
DoDeclStm
t
(
s
);
}
func
(
s
*
EmptySt
mt
)
Visit
(
v
StmtVisitor
)
{
v
.
DoEmptyStm
t
(
s
);
}
func
(
s
*
LabeledSt
mt
)
Visit
(
v
StmtVisitor
)
{
v
.
DoLabeledStm
t
(
s
);
}
func
(
s
*
ExprSt
mt
)
Visit
(
v
StmtVisitor
)
{
v
.
DoExprStm
t
(
s
);
}
func
(
s
*
IncDecSt
mt
)
Visit
(
v
StmtVisitor
)
{
v
.
DoIncDecStm
t
(
s
);
}
func
(
s
*
Assign
Stmt
)
Visit
(
v
StmtVisitor
)
{
v
.
DoAssignStm
t
(
s
);
}
func
(
s
*
GoSt
mt
)
Visit
(
v
StmtVisitor
)
{
v
.
DoGoStm
t
(
s
);
}
func
(
s
*
DeferSt
mt
)
Visit
(
v
StmtVisitor
)
{
v
.
DoDeferStm
t
(
s
);
}
func
(
s
*
ReturnSt
mt
)
Visit
(
v
StmtVisitor
)
{
v
.
DoReturnStm
t
(
s
);
}
func
(
s
*
BranchStmt
)
Visit
(
v
StmtVisitor
)
{
v
.
DoBranchStm
t
(
s
);
}
func
(
s
*
BlockStmt
)
Visit
(
v
StmtVisitor
)
{
v
.
DoBlockStm
t
(
s
);
}
func
(
s
*
IfSt
mt
)
Visit
(
v
StmtVisitor
)
{
v
.
DoIfStm
t
(
s
);
}
func
(
s
*
CaseClause
)
Visit
(
v
St
m
tVisitor
)
{
v
.
DoCaseClause
(
s
);
}
func
(
s
*
SwitchSt
mt
)
Visit
(
v
StmtVisitor
)
{
v
.
DoSwitchStm
t
(
s
);
}
func
(
s
*
TypeCaseClause
)
Visit
(
v
St
m
tVisitor
)
{
v
.
DoTypeCaseClause
(
s
);
}
func
(
s
*
TypeSwitchSt
mt
)
Visit
(
v
StmtVisitor
)
{
v
.
DoTypeSwitchStm
t
(
s
);
}
func
(
s
*
CommClause
)
Visit
(
v
St
m
tVisitor
)
{
v
.
DoCommClause
(
s
);
}
func
(
s
*
SelectSt
mt
)
Visit
(
v
StmtVisitor
)
{
v
.
DoSelectStm
t
(
s
);
}
func
(
s
*
ForSt
mt
)
Visit
(
v
StmtVisitor
)
{
v
.
DoForStm
t
(
s
);
}
func
(
s
*
RangeSt
mt
)
Visit
(
v
StmtVisitor
)
{
v
.
DoRangeStm
t
(
s
);
}
// ----------------------------------------------------------------------------
...
...
@@ -673,46 +690,45 @@ type (
};
ImportDecl
struct
{
Doc
Comments
;
// associated documentation
Doc
Comments
;
// associated documentation
; or nil
Import
Position
;
// position of "import" keyword
Name
*
Ident
;
// local package name or nil
Path
*
StringLit
;
// package path
Path
[]
*
StringLit
;
// package path
};
ConstDecl
struct
{
Doc
Comments
;
// associated documentation
Doc
Comments
;
// associated documentation
; or nil
Const
Position
;
// position of "const" keyword
Names
[]
*
Ident
;
Typ
Expr
;
// constant type or nil
Typ
e
Expr
;
// constant type or nil
Values
[]
Expr
;
};
TypeDecl
struct
{
Doc
Comments
;
// associated documentation
Type
Position
;
// position of "type" keyword
Doc
Comments
;
// associated documentation
; or nil
Pos_
Position
;
// position of "type" keyword
Name
*
Ident
;
Typ
Expr
;
Typ
e
Expr
;
};
VarDecl
struct
{
Doc
Comments
;
// associated documentation
Doc
Comments
;
// associated documentation
; or nil
Var
Position
;
// position of "var" keyword
Names
[]
*
Ident
;
Typ
Expr
;
// variable type or nil
Typ
e
Expr
;
// variable type or nil
Values
[]
Expr
;
};
FuncDecl
struct
{
Doc
Comments
;
// associated documentation
Func
Position
;
// position of "func" keyword
Doc
Comments
;
// associated documentation; or nil
Recv
*
Field
;
// receiver (methods) or nil (functions)
Name
*
Ident
;
// function/method name
Sig
*
Signature
;
//
parameters and results
Body
*
Block
;
// function body or nil (forward declaration)
Type
*
FunctionType
;
// position of Func keyword,
parameters and results
Body
*
Block
Stmt
;
// function body or nil (forward declaration)
};
DeclList
struct
{
Doc
Comments
;
// associated documentation
Doc
Comments
;
// associated documentation
; or nil
Pos_
Position
;
// position of token
Tok
int
;
// IMPORT, CONST, VAR, TYPE
Lparen
Position
;
// position of '('
...
...
@@ -727,9 +743,9 @@ type (
func
(
d
*
BadDecl
)
Pos
()
Position
{
return
d
.
Pos_
;
}
func
(
d
*
ImportDecl
)
Pos
()
Position
{
return
d
.
Import
;
}
func
(
d
*
ConstDecl
)
Pos
()
Position
{
return
d
.
Const
;
}
func
(
d
*
TypeDecl
)
Pos
()
Position
{
return
d
.
Type
;
}
func
(
d
*
TypeDecl
)
Pos
()
Position
{
return
d
.
Pos_
;
}
func
(
d
*
VarDecl
)
Pos
()
Position
{
return
d
.
Var
;
}
func
(
d
*
FuncDecl
)
Pos
()
Position
{
return
d
.
Func
;
}
func
(
d
*
FuncDecl
)
Pos
()
Position
{
return
d
.
Type
.
Func
;
}
func
(
d
*
DeclList
)
Pos
()
Position
{
return
d
.
Lparen
;
}
...
...
@@ -765,7 +781,7 @@ func (d *DeclList) Visit(v DeclVisitor) { v.DoDeclList(d); }
// A Package node represents the root node of an AST.
type
Package
struct
{
Doc
Comments
;
// associated documentation
Doc
Comments
;
// associated documentation
; or nil
Package
Position
;
// position of "package" keyword
Name
*
Ident
;
// package name
Decls
[]
Decl
;
// top-level declarations
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment