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 (
...
@@ -17,13 +17,6 @@ import (
type
Position
scanner
.
Location
type
Position
scanner
.
Location
// TODO try to get rid of these
type
(
Block
struct
;
Signature
struct
;
)
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// Interfaces
// Interfaces
//
//
...
@@ -43,12 +36,13 @@ type (
...
@@ -43,12 +36,13 @@ type (
// TODO: For comment positioning only the byte position and not
// TODO: For comment positioning only the byte position and not
// a complete Position field is needed. May be able to trim node
// 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
(
type
(
ExprVisitor
interface
;
ExprVisitor
interface
;
St
a
tVisitor
interface
;
St
m
tVisitor
interface
;
DeclVisitor
interface
;
DeclVisitor
interface
;
)
)
...
@@ -65,12 +59,12 @@ type Expr interface {
...
@@ -65,12 +59,12 @@ type Expr interface {
}
}
// All statement nodes implement the St
a
t interface.
// All statement nodes implement the St
m
t interface.
type
St
a
t
interface
{
type
St
m
t
interface
{
// For a (dynamic) node type X, calling Visit with a statement
// For a (dynamic) node type X, calling Visit with a statement
// visitor v invokes the node-specific DoX function of the visitor.
// 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 returns the (beginning) position of the statement.
Pos
()
Position
;
Pos
()
Position
;
...
@@ -109,6 +103,25 @@ type Comments []*Comment
...
@@ -109,6 +103,25 @@ type Comments []*Comment
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// Expressions and types
// 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
// An expression is represented by a tree consisting of one
// or more of the following concrete expression nodes.
// or more of the following concrete expression nodes.
//
//
...
@@ -127,28 +140,55 @@ type (
...
@@ -127,28 +140,55 @@ type (
Lit
[]
byte
;
// identifier string (e.g. foobar)
Lit
[]
byte
;
// identifier string (e.g. foobar)
};
};
// A BasicLit node represents a basic literal.
// An Ellipsis node stands for the "..." type in a
BasicLit
struct
{
// 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
Pos_
Position
;
// literal string position
Tok
int
;
// literal token (INT, FLOAT, CHAR, STRING)
Lit
[]
byte
;
// literal string; e.g. 42 or 0x7f
Lit
[]
byte
;
// literal string
};
};
// 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
{
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.
// A FunctionLit node represents a function literal.
FunctionLit
struct
{
FunctionLit
struct
{
Func
Position
;
// position of "func" keyword
Type
*
FunctionType
;
// function type
Typ
*
Signature
;
// function signature
Body
*
BlockStmt
;
// function body
Body
*
Block
;
// function body
};
};
// A CompositeLit node represents a composite literal.
// A CompositeLit node represents a composite literal.
CompositeLit
struct
{
CompositeLit
struct
{
Typ
Expr
;
// literal type
Typ
e
Expr
;
// literal type
Lbrace
Position
;
// position of "{"
Lbrace
Position
;
// position of "{"
Elts
[]
Expr
;
// list of composite elements
Elts
[]
Expr
;
// list of composite elements
Rbrace
Position
;
// position of "}"
Rbrace
Position
;
// position of "}"
...
@@ -161,33 +201,33 @@ type (
...
@@ -161,33 +201,33 @@ type (
Rparen
Position
;
// position of ")"
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
{
SelectorExpr
struct
{
X
Expr
;
//
primary
expression
X
Expr
;
// expression
Sel
*
Ident
;
// field selector
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
{
IndexExpr
struct
{
X
Expr
;
//
primary
expression
X
Expr
;
// expression
Index
Expr
;
// index 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
{
SliceExpr
struct
{
X
Expr
;
//
primary
expression
X
Expr
;
// expression
Begin
,
End
Expr
;
// slice range
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.
// type assertion.
//
//
TypeAssertExpr
struct
{
TypeAssertExpr
struct
{
X
Expr
;
//
primary
expression
X
Expr
;
// expression
Typ
Expr
;
// asserted type
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
{
CallExpr
struct
{
Fun
Expr
;
// function expression
Fun
Expr
;
// function expression
Lparen
Position
;
// position of "("
Lparen
Position
;
// position of "("
...
@@ -236,17 +276,10 @@ const (
...
@@ -236,17 +276,10 @@ const (
// nodes.
// nodes.
//
//
type
(
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.
// An ArrayType node represents an array type.
ArrayType
struct
{
ArrayType
struct
{
Lbrack
Position
;
// position of "["
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
Elt
Expr
;
// element type
};
};
...
@@ -256,16 +289,6 @@ type (
...
@@ -256,16 +289,6 @@ type (
Elt
Expr
;
// element 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.
// A StructType node represents a struct type.
StructType
struct
{
StructType
struct
{
Struct
,
Lbrace
Position
;
// positions of "struct" keyword, "{"
Struct
,
Lbrace
Position
;
// positions of "struct" keyword, "{"
...
@@ -273,20 +296,13 @@ type (
...
@@ -273,20 +296,13 @@ type (
Rbrace
Position
;
// position of "}"
Rbrace
Position
;
// position of "}"
};
};
// Note: pointer types are represented via StarExpr nodes.
// 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
;
};
// A FunctionType node represents a function type.
// A FunctionType node represents a function type.
FunctionType
struct
{
FunctionType
struct
{
Func
Position
;
// position of "func" keyword
Func
Position
;
// position of "func" keyword
Sig
*
Signature
;
Params
[]
*
Field
;
// (incoming) parameters
Results
[]
*
Field
;
// (outgoing) results
};
};
// An InterfaceType node represents an interface type.
// An InterfaceType node represents an interface type.
...
@@ -306,7 +322,7 @@ type (
...
@@ -306,7 +322,7 @@ type (
// A ChannelType node represents a channel type.
// A ChannelType node represents a channel type.
ChannelType
struct
{
ChannelType
struct
{
Pos_
Position
;
// position of "chan" keyword or "<-" (whichever comes first)
Pos_
Position
;
// position of "chan" keyword or "<-" (whichever comes first)
Dir
ChanDir
;
Dir
ChanDir
;
// channel direction
Value
Expr
;
// value type
Value
Expr
;
// value type
};
};
)
)
...
@@ -316,10 +332,13 @@ type (
...
@@ -316,10 +332,13 @@ type (
//
//
func
(
x
*
BadExpr
)
Pos
()
Position
{
return
x
.
Pos_
;
}
func
(
x
*
BadExpr
)
Pos
()
Position
{
return
x
.
Pos_
;
}
func
(
x
*
Ident
)
Pos
()
Position
{
return
x
.
Pos_
;
}
func
(
x
*
Ident
)
Pos
()
Position
{
return
x
.
Pos_
;
}
func
(
x
*
BasicLit
)
Pos
()
Position
{
return
x
.
Pos_
;
}
func
(
x
*
IntLit
)
Pos
()
Position
{
return
x
.
Pos_
;
}
func
(
x
*
StringLit
)
Pos
()
Position
{
return
x
.
Strings
[
0
]
.
Pos
();
}
func
(
x
*
FloatLit
)
Pos
()
Position
{
return
x
.
Pos_
;
}
func
(
x
*
FunctionLit
)
Pos
()
Position
{
return
x
.
Func
;
}
func
(
x
*
CharLit
)
Pos
()
Position
{
return
x
.
Pos_
;
}
func
(
x
*
CompositeLit
)
Pos
()
Position
{
return
x
.
Typ
.
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
*
ParenExpr
)
Pos
()
Position
{
return
x
.
Lparen
;
}
func
(
x
*
SelectorExpr
)
Pos
()
Position
{
return
x
.
X
.
Pos
();
}
func
(
x
*
SelectorExpr
)
Pos
()
Position
{
return
x
.
X
.
Pos
();
}
func
(
x
*
IndexExpr
)
Pos
()
Position
{
return
x
.
X
.
Pos
();
}
func
(
x
*
IndexExpr
)
Pos
()
Position
{
return
x
.
X
.
Pos
();
}
...
@@ -349,8 +368,11 @@ type ExprVisitor interface {
...
@@ -349,8 +368,11 @@ type ExprVisitor interface {
// Expressions
// Expressions
DoBadExpr
(
x
*
BadExpr
);
DoBadExpr
(
x
*
BadExpr
);
DoIdent
(
x
*
Ident
);
DoIdent
(
x
*
Ident
);
DoBasicLit
(
x
*
BasicLit
);
DoIntLit
(
x
*
IntLit
);
DoFloatLit
(
x
*
FloatLit
);
DoCharLit
(
x
*
CharLit
);
DoStringLit
(
x
*
StringLit
);
DoStringLit
(
x
*
StringLit
);
DoStringList
(
x
*
StringList
);
DoFunctionLit
(
x
*
FunctionLit
);
DoFunctionLit
(
x
*
FunctionLit
);
DoCompositeLit
(
x
*
CompositeLit
);
DoCompositeLit
(
x
*
CompositeLit
);
DoParenExpr
(
x
*
ParenExpr
);
DoParenExpr
(
x
*
ParenExpr
);
...
@@ -379,8 +401,12 @@ type ExprVisitor interface {
...
@@ -379,8 +401,12 @@ type ExprVisitor interface {
//
//
func
(
x
*
BadExpr
)
Visit
(
v
ExprVisitor
)
{
v
.
DoBadExpr
(
x
);
}
func
(
x
*
BadExpr
)
Visit
(
v
ExprVisitor
)
{
v
.
DoBadExpr
(
x
);
}
func
(
x
*
Ident
)
Visit
(
v
ExprVisitor
)
{
v
.
DoIdent
(
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
*
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
*
FunctionLit
)
Visit
(
v
ExprVisitor
)
{
v
.
DoFunctionLit
(
x
);
}
func
(
x
*
CompositeLit
)
Visit
(
v
ExprVisitor
)
{
v
.
DoCompositeLit
(
x
);
}
func
(
x
*
CompositeLit
)
Visit
(
v
ExprVisitor
)
{
v
.
DoCompositeLit
(
x
);
}
func
(
x
*
ParenExpr
)
Visit
(
v
ExprVisitor
)
{
v
.
DoParenExpr
(
x
);
}
func
(
x
*
ParenExpr
)
Visit
(
v
ExprVisitor
)
{
v
.
DoParenExpr
(
x
);
}
...
@@ -393,7 +419,6 @@ func (x *StarExpr) Visit(v ExprVisitor) { v.DoStarExpr(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
*
UnaryExpr
)
Visit
(
v
ExprVisitor
)
{
v
.
DoUnaryExpr
(
x
);
}
func
(
x
*
BinaryExpr
)
Visit
(
v
ExprVisitor
)
{
v
.
DoBinaryExpr
(
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
*
ArrayType
)
Visit
(
v
ExprVisitor
)
{
v
.
DoArrayType
(
x
);
}
func
(
x
*
SliceType
)
Visit
(
v
ExprVisitor
)
{
v
.
DoSliceType
(
x
);
}
func
(
x
*
SliceType
)
Visit
(
v
ExprVisitor
)
{
v
.
DoSliceType
(
x
);
}
func
(
x
*
StructType
)
Visit
(
v
ExprVisitor
)
{
v
.
DoStructType
(
x
);
}
func
(
x
*
StructType
)
Visit
(
v
ExprVisitor
)
{
v
.
DoStructType
(
x
);
}
...
@@ -403,22 +428,6 @@ func (x *MapType) Visit(v ExprVisitor) { v.DoMapType(x); }
...
@@ -403,22 +428,6 @@ func (x *MapType) Visit(v ExprVisitor) { v.DoMapType(x); }
func
(
x
*
ChannelType
)
Visit
(
v
ExprVisitor
)
{
v
.
DoChannelType
(
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
// Statements
...
@@ -426,236 +435,244 @@ type Block struct {
...
@@ -426,236 +435,244 @@ type Block struct {
// or more of the following concrete statement nodes.
// or more of the following concrete statement nodes.
//
//
type
(
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
// syntax errors for which no correct statement nodes can be
// created.
// created.
//
//
BadSt
a
t
struct
{
BadSt
m
t
struct
{
Pos_
Position
;
// beginning position of bad statement
Pos_
Position
;
// beginning position of bad statement
};
};
// A DeclSt
a
t node represents a declaration in a statement list.
// A DeclSt
m
t node represents a declaration in a statement list.
DeclSt
a
t
struct
{
DeclSt
m
t
struct
{
Decl
Decl
;
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
// The "position" of the empty statement is the position
// of the immediately preceeding semicolon.
// of the immediately preceeding semicolon.
//
//
EmptySt
a
t
struct
{
EmptySt
m
t
struct
{
Semicolon
Position
;
// position of preceeding ";"
Semicolon
Position
;
// position of preceeding ";"
};
};
// A LabeledSt
a
t node represents a labeled statement.
// A LabeledSt
m
t node represents a labeled statement.
LabeledSt
a
t
struct
{
LabeledSt
m
t
struct
{
Label
*
Ident
;
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.
// in a statement list.
//
//
ExprSt
a
t
struct
{
ExprSt
m
t
struct
{
X
Expr
;
// expression
X
Expr
;
// expression
};
};
// An IncDecSt
a
t node represents an increment or decrement statement.
// An IncDecSt
m
t node represents an increment or decrement statement.
IncDecSt
a
t
struct
{
IncDecSt
m
t
struct
{
X
Expr
;
X
Expr
;
Tok
int
;
// INC or DEC
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.
// a short variable declaration.
Assign
mentSta
t
struct
{
Assign
Stm
t
struct
{
Lhs
[]
Expr
;
Lhs
[]
Expr
;
Pos_
Position
;
// token position
Pos_
Position
;
// token position
Tok
int
;
// assignment token, DEFINE
Tok
int
;
// assignment token, DEFINE
Rhs
[]
Expr
;
Rhs
[]
Expr
;
};
};
// A GoSt
a
t node represents a go statement.
// A GoSt
m
t node represents a go statement.
GoSt
a
t
struct
{
GoSt
m
t
struct
{
Go
Position
;
// position of "go" keyword
Go
Position
;
// position of "go" keyword
Call
Expr
;
Call
*
Call
Expr
;
};
};
// A DeferSt
a
t node represents a defer statement.
// A DeferSt
m
t node represents a defer statement.
DeferSt
a
t
struct
{
DeferSt
m
t
struct
{
Defer
Position
;
// position of "defer" keyword
Defer
Position
;
// position of "defer" keyword
Call
Expr
;
Call
*
Call
Expr
;
};
};
// A ReturnSt
a
t node represents a return statement.
// A ReturnSt
m
t node represents a return statement.
ReturnSt
a
t
struct
{
ReturnSt
m
t
struct
{
Return
Position
;
// position of "return" keyword
Return
Position
;
// position of "return" keyword
Results
[]
Expr
;
Results
[]
Expr
;
};
};
// A
ControlFlowSta
t node represents a break, continue, goto,
// A
BranchStm
t node represents a break, continue, goto,
// or fallthrough statement.
// or fallthrough statement.
//
//
ControlFlowSta
t
struct
{
BranchStm
t
struct
{
Pos_
Position
;
// position of keyword
Pos_
Position
;
// position of keyword
Tok
int
;
// keyword token (BREAK, CONTINUE, GOTO, FALLTHROUGH)
Tok
int
;
// keyword token (BREAK, CONTINUE, GOTO, FALLTHROUGH)
Label
*
Ident
;
Label
*
Ident
;
};
};
// A CompositeStat node represents a braced statement list.
// A BlockStmt node represents a braced statement list.
CompositeStat
struct
{
BlockStmt
struct
{
Body
*
Block
;
Lbrace
Position
;
List
[]
Stmt
;
Rbrace
Position
;
};
};
// An IfSt
a
t node represents an if statement.
// An IfSt
m
t node represents an if statement.
IfSt
a
t
struct
{
IfSt
m
t
struct
{
If
Position
;
// position of "if" keyword
If
Position
;
// position of "if" keyword
Init
St
a
t
;
Init
St
m
t
;
Cond
Expr
;
Cond
Expr
;
Body
*
Block
;
Body
*
Block
Stmt
;
Else
St
a
t
;
Else
St
m
t
;
};
};
// A CaseClause represents a case of an expression switch statement.
// A CaseClause represents a case of an expression switch statement.
CaseClause
struct
{
CaseClause
struct
{
Case
Position
;
// position of "case" or "default" keyword
Case
Position
;
// position of "case" or "default" keyword
Values
[]
Expr
;
// nil means default case
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.
// A SwitchSt
m
t node represents an expression switch statement.
SwitchSt
a
t
struct
{
SwitchSt
m
t
struct
{
Switch
Position
;
// position of "switch" keyword
Switch
Position
;
// position of "switch" keyword
Init
St
a
t
;
Init
St
m
t
;
Tag
Expr
;
Tag
Expr
;
Body
*
Block
;
// CaseClauses only
Body
*
Block
Stmt
;
// 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
{
Case
Position
;
// position of "case" or "default" keyword
Case
Position
;
// position of "case" or "default" keyword
Typ
Expr
;
// nil means default case
Type
Expr
;
// nil means default case
Body
*
Block
;
Colon
Position
;
// position of ":"
Body
[]
Stmt
;
// statement list; or nil
};
};
// An TypeSwitchSt
a
t node represents a type switch statement.
// An TypeSwitchSt
m
t node represents a type switch statement.
TypeSwitchSt
a
t
struct
{
TypeSwitchSt
m
t
struct
{
Switch
Position
;
// position of "switch" keyword
Switch
Position
;
// position of "switch" keyword
Init
St
a
t
;
Init
St
m
t
;
Assign
St
a
t
;
// x := y.(type)
Assign
St
m
t
;
// x := y.(type)
Body
*
Block
;
// TypeCaseClauses only
Body
*
Block
Stmt
;
// 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
{
Case
Position
;
// position of "case" or "default" keyword
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
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.
// An SelectSt
m
t node represents a select statement.
SelectSt
a
t
struct
{
SelectSt
m
t
struct
{
Select
Position
;
// position of "select" keyword
Select
Position
;
// position of "select" keyword
Body
*
Block
;
// CommClauses only
Body
*
Block
Stmt
;
// CommClauses only
};
};
// A ForSt
a
t represents a for statement.
// A ForSt
m
t represents a for statement.
ForSt
a
t
struct
{
ForSt
m
t
struct
{
For
Position
;
// position of "for" keyword
For
Position
;
// position of "for" keyword
Init
St
a
t
;
Init
St
m
t
;
Cond
Expr
;
Cond
Expr
;
Post
St
a
t
;
Post
St
m
t
;
Body
*
Block
;
Body
*
Block
Stmt
;
};
};
// A RangeSt
a
t represents a for statement with a range clause.
// A RangeSt
m
t represents a for statement with a range clause.
RangeSt
a
t
struct
{
RangeSt
m
t
struct
{
For
Position
;
// position of "for" keyword
For
Position
;
// position of "for" keyword
Range
Stat
;
Key
,
Value
Expr
;
// Value may be nil
Body
*
Block
;
Pos_
Position
;
// token position
Tok
int
;
// ASSIGN or DEFINE
X
Expr
;
// value to range over
Body
*
BlockStmt
;
};
};
)
)
// Pos() implementations for all statement nodes.
// Pos() implementations for all statement nodes.
//
//
func
(
s
*
BadSt
a
t
)
Pos
()
Position
{
return
s
.
Pos_
;
}
func
(
s
*
BadSt
m
t
)
Pos
()
Position
{
return
s
.
Pos_
;
}
func
(
s
*
DeclSt
a
t
)
Pos
()
Position
{
return
s
.
Decl
.
Pos
();
}
func
(
s
*
DeclSt
m
t
)
Pos
()
Position
{
return
s
.
Decl
.
Pos
();
}
func
(
s
*
EmptySt
a
t
)
Pos
()
Position
{
return
s
.
Semicolon
;
}
func
(
s
*
EmptySt
m
t
)
Pos
()
Position
{
return
s
.
Semicolon
;
}
func
(
s
*
LabeledSt
a
t
)
Pos
()
Position
{
return
s
.
Label
.
Pos
();
}
func
(
s
*
LabeledSt
m
t
)
Pos
()
Position
{
return
s
.
Label
.
Pos
();
}
func
(
s
*
ExprSt
a
t
)
Pos
()
Position
{
return
s
.
X
.
Pos
();
}
func
(
s
*
ExprSt
m
t
)
Pos
()
Position
{
return
s
.
X
.
Pos
();
}
func
(
s
*
IncDecSt
a
t
)
Pos
()
Position
{
return
s
.
X
.
Pos
();
}
func
(
s
*
IncDecSt
m
t
)
Pos
()
Position
{
return
s
.
X
.
Pos
();
}
func
(
s
*
Assign
mentSta
t
)
Pos
()
Position
{
return
s
.
Lhs
[
0
]
.
Pos
();
}
func
(
s
*
Assign
Stm
t
)
Pos
()
Position
{
return
s
.
Lhs
[
0
]
.
Pos
();
}
func
(
s
*
GoSt
a
t
)
Pos
()
Position
{
return
s
.
Go
;
}
func
(
s
*
GoSt
m
t
)
Pos
()
Position
{
return
s
.
Go
;
}
func
(
s
*
DeferSt
a
t
)
Pos
()
Position
{
return
s
.
Defer
;
}
func
(
s
*
DeferSt
m
t
)
Pos
()
Position
{
return
s
.
Defer
;
}
func
(
s
*
ReturnSt
a
t
)
Pos
()
Position
{
return
s
.
Return
;
}
func
(
s
*
ReturnSt
m
t
)
Pos
()
Position
{
return
s
.
Return
;
}
func
(
s
*
ControlFlowSta
t
)
Pos
()
Position
{
return
s
.
Pos_
;
}
func
(
s
*
BranchStm
t
)
Pos
()
Position
{
return
s
.
Pos_
;
}
func
(
s
*
CompositeStat
)
Pos
()
Position
{
return
s
.
Body
.
Pos_
;
}
func
(
s
*
BlockStmt
)
Pos
()
Position
{
return
s
.
Lbrace
;
}
func
(
s
*
IfSt
a
t
)
Pos
()
Position
{
return
s
.
If
;
}
func
(
s
*
IfSt
m
t
)
Pos
()
Position
{
return
s
.
If
;
}
func
(
s
*
CaseClause
)
Pos
()
Position
{
return
s
.
Case
;
}
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
*
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
*
CommClause
)
Pos
()
Position
{
return
s
.
Case
;
}
func
(
s
*
SelectSt
a
t
)
Pos
()
Position
{
return
s
.
Select
;
}
func
(
s
*
SelectSt
m
t
)
Pos
()
Position
{
return
s
.
Select
;
}
func
(
s
*
ForSt
a
t
)
Pos
()
Position
{
return
s
.
For
;
}
func
(
s
*
ForSt
m
t
)
Pos
()
Position
{
return
s
.
For
;
}
func
(
s
*
RangeSt
a
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
// All statement nodes implement a Visit method which takes
// a St
a
tVisitor as argument. For a given node x of type X, and
// a St
m
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
// 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).
// result in a call of v.DoX(x) (through a double-dispatch).
//
//
type
St
a
tVisitor
interface
{
type
St
m
tVisitor
interface
{
DoBadSt
at
(
s
*
BadSta
t
);
DoBadSt
mt
(
s
*
BadStm
t
);
DoDeclSt
at
(
s
*
DeclSta
t
);
DoDeclSt
mt
(
s
*
DeclStm
t
);
DoEmptySt
at
(
s
*
EmptySta
t
);
DoEmptySt
mt
(
s
*
EmptyStm
t
);
DoLabeledSt
at
(
s
*
LabeledSta
t
);
DoLabeledSt
mt
(
s
*
LabeledStm
t
);
DoExprSt
at
(
s
*
ExprSta
t
);
DoExprSt
mt
(
s
*
ExprStm
t
);
DoIncDecSt
at
(
s
*
IncDecSta
t
);
DoIncDecSt
mt
(
s
*
IncDecStm
t
);
DoAssign
mentStat
(
s
*
AssignmentSta
t
);
DoAssign
Stmt
(
s
*
AssignStm
t
);
DoGoSt
at
(
s
*
GoSta
t
);
DoGoSt
mt
(
s
*
GoStm
t
);
DoDeferSt
at
(
s
*
DeferSta
t
);
DoDeferSt
mt
(
s
*
DeferStm
t
);
DoReturnSt
at
(
s
*
ReturnSta
t
);
DoReturnSt
mt
(
s
*
ReturnStm
t
);
Do
ControlFlowStat
(
s
*
ControlFlowSta
t
);
Do
BranchStmt
(
s
*
BranchStm
t
);
Do
CompositeStat
(
s
*
CompositeSta
t
);
Do
BlockStmt
(
s
*
BlockStm
t
);
DoIfSt
at
(
s
*
IfSta
t
);
DoIfSt
mt
(
s
*
IfStm
t
);
DoCaseClause
(
s
*
CaseClause
);
DoCaseClause
(
s
*
CaseClause
);
DoSwitchSt
at
(
s
*
SwitchSta
t
);
DoSwitchSt
mt
(
s
*
SwitchStm
t
);
DoTypeCaseClause
(
s
*
TypeCaseClause
);
DoTypeCaseClause
(
s
*
TypeCaseClause
);
DoTypeSwitchSt
at
(
s
*
TypeSwitchSta
t
);
DoTypeSwitchSt
mt
(
s
*
TypeSwitchStm
t
);
DoCommClause
(
s
*
CommClause
);
DoCommClause
(
s
*
CommClause
);
DoSelectSt
at
(
s
*
SelectSta
t
);
DoSelectSt
mt
(
s
*
SelectStm
t
);
DoForSt
at
(
s
*
ForSta
t
);
DoForSt
mt
(
s
*
ForStm
t
);
DoRangeSt
at
(
s
*
RangeSta
t
);
DoRangeSt
mt
(
s
*
RangeStm
t
);
}
}
// Visit() implementations for all statement nodes.
// Visit() implementations for all statement nodes.
//
//
func
(
s
*
BadSt
at
)
Visit
(
v
StatVisitor
)
{
v
.
DoBadSta
t
(
s
);
}
func
(
s
*
BadSt
mt
)
Visit
(
v
StmtVisitor
)
{
v
.
DoBadStm
t
(
s
);
}
func
(
s
*
DeclSt
at
)
Visit
(
v
StatVisitor
)
{
v
.
DoDeclSta
t
(
s
);
}
func
(
s
*
DeclSt
mt
)
Visit
(
v
StmtVisitor
)
{
v
.
DoDeclStm
t
(
s
);
}
func
(
s
*
EmptySt
at
)
Visit
(
v
StatVisitor
)
{
v
.
DoEmptySta
t
(
s
);
}
func
(
s
*
EmptySt
mt
)
Visit
(
v
StmtVisitor
)
{
v
.
DoEmptyStm
t
(
s
);
}
func
(
s
*
LabeledSt
at
)
Visit
(
v
StatVisitor
)
{
v
.
DoLabeledSta
t
(
s
);
}
func
(
s
*
LabeledSt
mt
)
Visit
(
v
StmtVisitor
)
{
v
.
DoLabeledStm
t
(
s
);
}
func
(
s
*
ExprSt
at
)
Visit
(
v
StatVisitor
)
{
v
.
DoExprSta
t
(
s
);
}
func
(
s
*
ExprSt
mt
)
Visit
(
v
StmtVisitor
)
{
v
.
DoExprStm
t
(
s
);
}
func
(
s
*
IncDecSt
at
)
Visit
(
v
StatVisitor
)
{
v
.
DoIncDecSta
t
(
s
);
}
func
(
s
*
IncDecSt
mt
)
Visit
(
v
StmtVisitor
)
{
v
.
DoIncDecStm
t
(
s
);
}
func
(
s
*
Assign
mentStat
)
Visit
(
v
StatVisitor
)
{
v
.
DoAssignmentSta
t
(
s
);
}
func
(
s
*
Assign
Stmt
)
Visit
(
v
StmtVisitor
)
{
v
.
DoAssignStm
t
(
s
);
}
func
(
s
*
GoSt
at
)
Visit
(
v
StatVisitor
)
{
v
.
DoGoSta
t
(
s
);
}
func
(
s
*
GoSt
mt
)
Visit
(
v
StmtVisitor
)
{
v
.
DoGoStm
t
(
s
);
}
func
(
s
*
DeferSt
at
)
Visit
(
v
StatVisitor
)
{
v
.
DoDeferSta
t
(
s
);
}
func
(
s
*
DeferSt
mt
)
Visit
(
v
StmtVisitor
)
{
v
.
DoDeferStm
t
(
s
);
}
func
(
s
*
ReturnSt
at
)
Visit
(
v
StatVisitor
)
{
v
.
DoReturnSta
t
(
s
);
}
func
(
s
*
ReturnSt
mt
)
Visit
(
v
StmtVisitor
)
{
v
.
DoReturnStm
t
(
s
);
}
func
(
s
*
ControlFlowStat
)
Visit
(
v
StatVisitor
)
{
v
.
DoControlFlowSta
t
(
s
);
}
func
(
s
*
BranchStmt
)
Visit
(
v
StmtVisitor
)
{
v
.
DoBranchStm
t
(
s
);
}
func
(
s
*
CompositeStat
)
Visit
(
v
StatVisitor
)
{
v
.
DoCompositeSta
t
(
s
);
}
func
(
s
*
BlockStmt
)
Visit
(
v
StmtVisitor
)
{
v
.
DoBlockStm
t
(
s
);
}
func
(
s
*
IfSt
at
)
Visit
(
v
StatVisitor
)
{
v
.
DoIfSta
t
(
s
);
}
func
(
s
*
IfSt
mt
)
Visit
(
v
StmtVisitor
)
{
v
.
DoIfStm
t
(
s
);
}
func
(
s
*
CaseClause
)
Visit
(
v
St
a
tVisitor
)
{
v
.
DoCaseClause
(
s
);
}
func
(
s
*
CaseClause
)
Visit
(
v
St
m
tVisitor
)
{
v
.
DoCaseClause
(
s
);
}
func
(
s
*
SwitchSt
at
)
Visit
(
v
StatVisitor
)
{
v
.
DoSwitchSta
t
(
s
);
}
func
(
s
*
SwitchSt
mt
)
Visit
(
v
StmtVisitor
)
{
v
.
DoSwitchStm
t
(
s
);
}
func
(
s
*
TypeCaseClause
)
Visit
(
v
St
a
tVisitor
)
{
v
.
DoTypeCaseClause
(
s
);
}
func
(
s
*
TypeCaseClause
)
Visit
(
v
St
m
tVisitor
)
{
v
.
DoTypeCaseClause
(
s
);
}
func
(
s
*
TypeSwitchSt
at
)
Visit
(
v
StatVisitor
)
{
v
.
DoTypeSwitchSta
t
(
s
);
}
func
(
s
*
TypeSwitchSt
mt
)
Visit
(
v
StmtVisitor
)
{
v
.
DoTypeSwitchStm
t
(
s
);
}
func
(
s
*
CommClause
)
Visit
(
v
St
a
tVisitor
)
{
v
.
DoCommClause
(
s
);
}
func
(
s
*
CommClause
)
Visit
(
v
St
m
tVisitor
)
{
v
.
DoCommClause
(
s
);
}
func
(
s
*
SelectSt
at
)
Visit
(
v
StatVisitor
)
{
v
.
DoSelectSta
t
(
s
);
}
func
(
s
*
SelectSt
mt
)
Visit
(
v
StmtVisitor
)
{
v
.
DoSelectStm
t
(
s
);
}
func
(
s
*
ForSt
at
)
Visit
(
v
StatVisitor
)
{
v
.
DoForSta
t
(
s
);
}
func
(
s
*
ForSt
mt
)
Visit
(
v
StmtVisitor
)
{
v
.
DoForStm
t
(
s
);
}
func
(
s
*
RangeSt
at
)
Visit
(
v
StatVisitor
)
{
v
.
DoRangeSta
t
(
s
);
}
func
(
s
*
RangeSt
mt
)
Visit
(
v
StmtVisitor
)
{
v
.
DoRangeStm
t
(
s
);
}
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
...
@@ -673,46 +690,45 @@ type (
...
@@ -673,46 +690,45 @@ type (
};
};
ImportDecl
struct
{
ImportDecl
struct
{
Doc
Comments
;
// associated documentation
Doc
Comments
;
// associated documentation
; or nil
Import
Position
;
// position of "import" keyword
Import
Position
;
// position of "import" keyword
Name
*
Ident
;
// local package name or nil
Name
*
Ident
;
// local package name or nil
Path
*
StringLit
;
// package path
Path
[]
*
StringLit
;
// package path
};
};
ConstDecl
struct
{
ConstDecl
struct
{
Doc
Comments
;
// associated documentation
Doc
Comments
;
// associated documentation
; or nil
Const
Position
;
// position of "const" keyword
Const
Position
;
// position of "const" keyword
Names
[]
*
Ident
;
Names
[]
*
Ident
;
Typ
Expr
;
// constant type or nil
Typ
e
Expr
;
// constant type or nil
Values
[]
Expr
;
Values
[]
Expr
;
};
};
TypeDecl
struct
{
TypeDecl
struct
{
Doc
Comments
;
// associated documentation
Doc
Comments
;
// associated documentation
; or nil
Type
Position
;
// position of "type" keyword
Pos_
Position
;
// position of "type" keyword
Name
*
Ident
;
Name
*
Ident
;
Typ
Expr
;
Typ
e
Expr
;
};
};
VarDecl
struct
{
VarDecl
struct
{
Doc
Comments
;
// associated documentation
Doc
Comments
;
// associated documentation
; or nil
Var
Position
;
// position of "var" keyword
Var
Position
;
// position of "var" keyword
Names
[]
*
Ident
;
Names
[]
*
Ident
;
Typ
Expr
;
// variable type or nil
Typ
e
Expr
;
// variable type or nil
Values
[]
Expr
;
Values
[]
Expr
;
};
};
FuncDecl
struct
{
FuncDecl
struct
{
Doc
Comments
;
// associated documentation
Doc
Comments
;
// associated documentation; or nil
Func
Position
;
// position of "func" keyword
Recv
*
Field
;
// receiver (methods) or nil (functions)
Recv
*
Field
;
// receiver (methods) or nil (functions)
Name
*
Ident
;
// function/method name
Name
*
Ident
;
// function/method name
Sig
*
Signature
;
//
parameters and results
Type
*
FunctionType
;
// position of Func keyword,
parameters and results
Body
*
Block
;
// function body or nil (forward declaration)
Body
*
Block
Stmt
;
// function body or nil (forward declaration)
};
};
DeclList
struct
{
DeclList
struct
{
Doc
Comments
;
// associated documentation
Doc
Comments
;
// associated documentation
; or nil
Pos_
Position
;
// position of token
Pos_
Position
;
// position of token
Tok
int
;
// IMPORT, CONST, VAR, TYPE
Tok
int
;
// IMPORT, CONST, VAR, TYPE
Lparen
Position
;
// position of '('
Lparen
Position
;
// position of '('
...
@@ -727,9 +743,9 @@ type (
...
@@ -727,9 +743,9 @@ type (
func
(
d
*
BadDecl
)
Pos
()
Position
{
return
d
.
Pos_
;
}
func
(
d
*
BadDecl
)
Pos
()
Position
{
return
d
.
Pos_
;
}
func
(
d
*
ImportDecl
)
Pos
()
Position
{
return
d
.
Import
;
}
func
(
d
*
ImportDecl
)
Pos
()
Position
{
return
d
.
Import
;
}
func
(
d
*
ConstDecl
)
Pos
()
Position
{
return
d
.
Const
;
}
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
*
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
;
}
func
(
d
*
DeclList
)
Pos
()
Position
{
return
d
.
Lparen
;
}
...
@@ -765,7 +781,7 @@ func (d *DeclList) Visit(v DeclVisitor) { v.DoDeclList(d); }
...
@@ -765,7 +781,7 @@ func (d *DeclList) Visit(v DeclVisitor) { v.DoDeclList(d); }
// A Package node represents the root node of an AST.
// A Package node represents the root node of an AST.
type
Package
struct
{
type
Package
struct
{
Doc
Comments
;
// associated documentation
Doc
Comments
;
// associated documentation
; or nil
Package
Position
;
// position of "package" keyword
Package
Position
;
// position of "package" keyword
Name
*
Ident
;
// package name
Name
*
Ident
;
// package name
Decls
[]
Decl
;
// top-level declarations
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