-
Notifications
You must be signed in to change notification settings - Fork 546
/
ast_ql.go
428 lines (325 loc) · 12.1 KB
/
ast_ql.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*
Copyright 2020 The GoPlus Authors (goplus.org)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package ast
import (
"github.com/goplus/gop/token"
)
// ----------------------------------------------------------------------------
/*
// A Scope maintains the set of named language entities declared
// in the scope and a link to the immediately surrounding (outer)
// scope.
type Scope = ast.Scope
// A Package node represents a set of source files collectively building a Go+ package.
type Package = ast.Package
// A File node represents a Go+ source file.
type File = ast.File
// Expr - All expression nodes implement the Expr interface.
type Expr = ast.Expr
// Stmt - All statement nodes implement the Stmt interface.
type Stmt = ast.Stmt
// ----------------------------------------------------------------------------
// Declarations
// A Spec node represents a single (non-parenthesized) import,
// constant, type, or variable declaration.
type (
// The Spec type stands for any of *ImportSpec, *ValueSpec, and *TypeSpec.
Spec = ast.Spec
// An ImportSpec node represents a single package import.
ImportSpec = ast.ImportSpec
// A ValueSpec node represents a constant or variable declaration
// (ConstSpec or VarSpec production).
ValueSpec = ast.ValueSpec
// A TypeSpec node represents a type declaration (TypeSpec production).
TypeSpec = ast.TypeSpec
)
// A declaration is represented by one of the following declaration nodes.
type (
// A GenDecl node (generic declaration node) represents an import,
// constant, type or variable declaration. A valid Lparen position
// (Lparen.IsValid()) indicates a parenthesized declaration.
//
// Relationship between Tok value and Specs element type:
//
// token.IMPORT *ImportSpec
// token.CONST *ValueSpec
// token.TYPE *TypeSpec
// token.VAR *ValueSpec
//
GenDecl = ast.GenDecl
// A FuncDecl node represents a function declaration.
FuncDecl = ast.FuncDecl
)
// ----------------------------------------------------------------------------
// Comments
// A CommentGroup represents a sequence of comments
// with no other tokens and no empty lines between.
type CommentGroup = ast.CommentGroup
// ----------------------------------------------------------------------------
// Statements
// A statement is represented by a tree consisting of one
// or more of the following concrete statement nodes.
type (
// A BadStmt node is a placeholder for statements containing
// syntax errors for which no correct statement nodes can be
// created.
BadStmt = ast.BadStmt
// A DeclStmt node represents a declaration in a statement list.
DeclStmt = ast.DeclStmt
// An EmptyStmt node represents an empty statement.
// The "position" of the empty statement is the position
// of the immediately following (explicit or implicit) semicolon.
EmptyStmt = ast.EmptyStmt
// A LabeledStmt node represents a labeled statement.
LabeledStmt = ast.LabeledStmt
// An ExprStmt node represents a (stand-alone) expression
// in a statement list.
ExprStmt = ast.ExprStmt
// A SendStmt node represents a send statement.
SendStmt = ast.SendStmt
// An IncDecStmt node represents an increment or decrement statement.
IncDecStmt = ast.IncDecStmt
// An AssignStmt node represents an assignment or
// a short variable declaration.
AssignStmt = ast.AssignStmt
// A GoStmt node represents a go statement.
GoStmt = ast.GoStmt
// A DeferStmt node represents a defer statement.
DeferStmt = ast.DeferStmt
// A ReturnStmt node represents a return statement.
ReturnStmt = ast.ReturnStmt
// A BranchStmt node represents a break, continue, goto,
// or fallthrough statement.
BranchStmt = ast.BranchStmt
// A BlockStmt node represents a braced statement list.
BlockStmt = ast.BlockStmt
// An IfStmt node represents an if statement.
IfStmt = ast.IfStmt
// A CaseClause represents a case of an expression or type switch statement.
CaseClause = ast.CaseClause
// A SwitchStmt node represents an expression switch statement.
SwitchStmt = ast.SwitchStmt
// A TypeSwitchStmt node represents a type switch statement.
TypeSwitchStmt = ast.TypeSwitchStmt
// A CommClause node represents a case of a select statement.
CommClause = ast.CommClause
// A SelectStmt node represents a select statement.
SelectStmt = ast.SelectStmt
// A ForStmt represents a for statement.
ForStmt = ast.ForStmt
// A RangeStmt represents a for statement with a range clause.
RangeStmt = ast.RangeStmt
)
// ----------------------------------------------------------------------------
// Expressions and types
// A Field represents a Field declaration list in a struct type,
// a method list in an interface type, or a parameter/result declaration
// in a signature.
// Field.Names is nil for unnamed parameters (parameter lists which only contain types)
// and embedded struct fields. In the latter case, the field name is the type name.
//
type Field = ast.Field
// A FieldList represents a list of Fields, enclosed by parentheses or braces.
type FieldList = ast.FieldList
// An expression is represented by a tree consisting of one
// or more of the following concrete expression nodes.
//
type (
// A BadExpr node is a placeholder for expressions containing
// syntax errors for which no correct expression nodes can be
// created.
BadExpr = ast.BadExpr
// An Ident node represents an identifier.
Ident = ast.Ident
// An Ellipsis node stands for the "..." type in a
// parameter list or the "..." length in an array type.
Ellipsis = ast.Ellipsis
// A BasicLit node represents a literal of basic type.
BasicLit = ast.BasicLit
// A FuncLit node represents a function literal.
FuncLit = ast.FuncLit
// A CompositeLit node represents a composite literal.
CompositeLit = ast.CompositeLit
// A ParenExpr node represents a parenthesized expression.
ParenExpr = ast.ParenExpr
// A SelectorExpr node represents an expression followed by a selector.
SelectorExpr = ast.SelectorExpr
// An IndexExpr node represents an expression followed by an index.
IndexExpr = ast.IndexExpr
// A SliceExpr node represents an expression followed by slice indices.
SliceExpr = ast.SliceExpr
// A TypeAssertExpr node represents an expression followed by a
// type assertion.
TypeAssertExpr = ast.TypeAssertExpr
// A CallExpr node represents an expression followed by an argument list.
CallExpr = ast.CallExpr
// A StarExpr node represents an expression of the form "*" Expression.
// Semantically it could be a unary "*" expression, or a pointer type.
StarExpr = ast.StarExpr
// A UnaryExpr node represents a unary expression.
// Unary "*" expressions are represented via StarExpr nodes.
UnaryExpr = ast.UnaryExpr
// A BinaryExpr node represents a binary expression.
BinaryExpr = ast.BinaryExpr
// A KeyValueExpr node represents (key : value) pairs
// in composite literals.
KeyValueExpr = ast.KeyValueExpr
)
// ChanDir - the direction of a channel type is indicated by a bit
// mask including one or both of the following constants.
type ChanDir = ast.ChanDir
const (
// SEND flag
SEND = ast.SEND
// RECV flag
RECV = ast.RECV
)
// A type is represented by a tree consisting of one
// or more of the following type-specific expression
// nodes.
type (
// An ArrayType node represents an array or slice type.
ArrayType = ast.ArrayType
// A StructType node represents a struct type.
StructType = ast.StructType
// Pointer types are represented via StarExpr nodes.
// A FuncType node represents a function type.
FuncType = ast.FuncType
// An InterfaceType node represents an interface type.
InterfaceType = ast.InterfaceType
// A MapType node represents a map type.
MapType = ast.MapType
// A ChanType node represents a channel type.
ChanType = ast.ChanType
)
*/
// -----------------------------------------------------------------------------
// A SliceLit node represents a slice literal.
type SliceLit struct {
Lbrack token.Pos // position of "["
Elts []Expr // list of composite elements; or nil
Rbrack token.Pos // position of "]"
Incomplete bool // true if (source) expressions are missing in the Elts list
}
// Pos - position of first character belonging to the node
func (p *SliceLit) Pos() token.Pos {
return p.Lbrack
}
// End - position of first character immediately after the node
func (p *SliceLit) End() token.Pos {
return p.Rbrack + 1
}
func (*SliceLit) exprNode() {}
// -----------------------------------------------------------------------------
// TernaryExpr represents `cond ? expr1 : expr2`
type TernaryExpr struct {
Cond Expr
Question token.Pos
X Expr
Colon token.Pos
Y Expr
}
// Pos - position of first character belonging to the node
func (p *TernaryExpr) Pos() token.Pos {
return p.Cond.Pos()
}
// End - position of first character immediately after the node
func (p *TernaryExpr) End() token.Pos {
return p.Y.End()
}
func (*TernaryExpr) exprNode() {}
// -----------------------------------------------------------------------------
// ErrWrapExpr represents `expr!`, `expr?` or `expr? defaultValue`
type ErrWrapExpr struct {
X Expr
Tok token.Token // ! or ?
TokPos token.Pos
Default Expr // can be nil
}
// Pos - position of first character belonging to the node
func (p *ErrWrapExpr) Pos() token.Pos {
return p.X.Pos()
}
// End - position of first character immediately after the node
func (p *ErrWrapExpr) End() token.Pos {
if p.Default != nil {
return p.Default.End()
}
return p.TokPos + 1
}
func (*ErrWrapExpr) exprNode() {}
// -----------------------------------------------------------------------------
// ForPhrase represents `for k, v <- listOrMap`
type ForPhrase struct {
For token.Pos // position of "for" keyword
Key, Value *Ident // Key may be nil
TokPos token.Pos // position of "<-" operator
X Expr // value to range over, must be list or map
Cond Expr // value filter, can be nil
}
func (p *ForPhrase) Pos() token.Pos {
return p.For
}
func (p *ForPhrase) End() token.Pos {
return p.X.End()
}
func (p *ForPhrase) exprNode() {}
// ListComprehensionExpr represents `[expr for k1, v1 <- listOrMap1, cond1 ...]`
type ListComprehensionExpr struct {
Lbrack token.Pos // position of "["
Elt Expr
Fors []ForPhrase
Rbrack token.Pos // position of "]"
}
// Pos - position of first character belonging to the node
func (p *ListComprehensionExpr) Pos() token.Pos {
return p.Lbrack
}
// End - position of first character immediately after the node
func (p *ListComprehensionExpr) End() token.Pos {
return p.Rbrack + 1
}
func (*ListComprehensionExpr) exprNode() {}
// -----------------------------------------------------------------------------
// MapComprehensionExpr represents `{kexpr: vexpr for k1, v1 <- listOrMap1, cond1 ...}`
type MapComprehensionExpr struct {
Lbrace token.Pos // position of "{"
Elt *KeyValueExpr
Fors []ForPhrase
Rbrace token.Pos // position of "}"
}
// Pos - position of first character belonging to the node
func (p *MapComprehensionExpr) Pos() token.Pos {
return p.Lbrace
}
// End - position of first character immediately after the node
func (p *MapComprehensionExpr) End() token.Pos {
return p.Rbrace + 1
}
func (*MapComprehensionExpr) exprNode() {}
// -----------------------------------------------------------------------------
// A ForPhraseStmt represents a for statement with a for <- clause.
type ForPhraseStmt struct {
ForPhrase
Body *BlockStmt
}
// Pos - position of first character belonging to the node
func (p *ForPhraseStmt) Pos() token.Pos {
return p.For
}
// End - position of first character immediately after the node
func (p *ForPhraseStmt) End() token.Pos {
return p.Body.End()
}
func (*ForPhraseStmt) stmtNode() {}
// -----------------------------------------------------------------------------