-
Notifications
You must be signed in to change notification settings - Fork 547
/
ast_gop.go
156 lines (121 loc) · 4.34 KB
/
ast_gop.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
/*
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 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
}
// Pos returns position of first character belonging to the node.
func (p *ForPhrase) Pos() token.Pos { return p.For }
// End returns position of first character immediately after the node.
func (p *ForPhrase) End() token.Pos { return p.X.End() }
func (p *ForPhrase) exprNode() {}
// ComprehensionExpr represents
// `[vexpr for k1, v1 <- listOrMap1, cond1 ...]` or
// `{vexpr for k1, v1 <- listOrMap1, cond1 ...}` or
// `{kexpr: vexpr for k1, v1 <- listOrMap1, cond1 ...}`
type ComprehensionExpr struct {
Lpos token.Pos // position of "[" or "{"
Tok token.Token // token.LBRACK '[' or token.LBRACE '{'
Elt Expr // *KeyValueExpr or Expr
Fors []ForPhrase
Rpos token.Pos // position of "]" or "}"
}
// Pos - position of first character belonging to the node
func (p *ComprehensionExpr) Pos() token.Pos {
return p.Lpos
}
// End - position of first character immediately after the node
func (p *ComprehensionExpr) End() token.Pos {
return p.Rpos + 1
}
func (*ComprehensionExpr) 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() {}
// -----------------------------------------------------------------------------