github.com/goplus/gop@v1.2.6/ast/ast_gop.go (about) 1 /* 2 * Copyright (c) 2021 The GoPlus Authors (goplus.org). All rights reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package ast 18 19 import ( 20 "github.com/goplus/gop/token" 21 ) 22 23 // ----------------------------------------------------------------------------- 24 25 // OverloadFuncDecl node represents an overload function declaration: 26 // 27 // `func name = (overloadFuncs)` 28 // `func (T).nameOrOp = (overloadFuncs)` 29 // 30 // here overloadFunc represents 31 // 32 // `func(params) {...}` 33 // `funcName` 34 // `(*T).methodName` 35 type OverloadFuncDecl struct { 36 Doc *CommentGroup // associated documentation; or nil 37 Func token.Pos // position of "func" keyword 38 Recv *FieldList // receiver (methods); or nil (functions) 39 Name *Ident // function/method name 40 Assign token.Pos // position of token "=" 41 Lparen token.Pos // position of "(" 42 Funcs []Expr // overload functions. here `Expr` can be *FuncLit, *Ident or *SelectorExpr 43 Rparen token.Pos // position of ")" 44 Operator bool // is operator or not 45 IsClass bool // recv set by class 46 } 47 48 // Pos - position of first character belonging to the node. 49 func (p *OverloadFuncDecl) Pos() token.Pos { 50 return p.Func 51 } 52 53 // End - position of first character immediately after the node. 54 func (p *OverloadFuncDecl) End() token.Pos { 55 return p.Rparen + 1 56 } 57 58 func (*OverloadFuncDecl) declNode() {} 59 60 // ----------------------------------------------------------------------------- 61 62 // A BasicLit node represents a literal of basic type. 63 type BasicLit struct { 64 ValuePos token.Pos // literal position 65 Kind token.Token // token.INT, token.FLOAT, token.IMAG, token.RAT, token.CHAR, token.STRING, token.CSTRING 66 Value string // literal string; e.g. 42, 0x7f, 3.14, 1e-9, 2.4i, 3r, 'a', '\x7f', "foo" or `\m\n\o` 67 Extra *StringLitEx // optional (only available when Kind == token.STRING) 68 } 69 70 type StringLitEx struct { 71 Parts []any // can be (val string) or (xval Expr) 72 } 73 74 // NextPartPos - position of first character of next part. 75 // pos - position of this part (not including quote character). 76 func NextPartPos(pos token.Pos, part any) (nextPos token.Pos) { 77 switch v := part.(type) { 78 case string: // normal string literal or end with "$$" 79 return pos + token.Pos(len(v)) 80 case Expr: 81 return v.End() 82 } 83 panic("NextPartPos: unexpected parameters") 84 } 85 86 // ----------------------------------------------------------------------------- 87 88 // A EnvExpr node represents a ${name} expression. 89 type EnvExpr struct { 90 TokPos token.Pos // position of "$" 91 Lbrace token.Pos // position of "{" 92 Name *Ident // name 93 Rbrace token.Pos // position of "}" 94 } 95 96 // Pos - position of first character belonging to the node. 97 func (p *EnvExpr) Pos() token.Pos { 98 return p.TokPos 99 } 100 101 // End - position of first character immediately after the node. 102 func (p *EnvExpr) End() token.Pos { 103 if p.Rbrace != token.NoPos { 104 return p.Rbrace 105 } 106 return p.Name.End() 107 } 108 109 // HasBrace checks is this EnvExpr ${name} or $name. 110 func (p *EnvExpr) HasBrace() bool { 111 return p.Rbrace != token.NoPos 112 } 113 114 func (*EnvExpr) exprNode() {} 115 116 // ----------------------------------------------------------------------------- 117 118 // A SliceLit node represents a slice literal. 119 type SliceLit struct { 120 Lbrack token.Pos // position of "[" 121 Elts []Expr // list of slice elements; or nil 122 Rbrack token.Pos // position of "]" 123 Incomplete bool // true if (source) expressions are missing in the Elts list 124 } 125 126 // Pos - position of first character belonging to the node. 127 func (p *SliceLit) Pos() token.Pos { 128 return p.Lbrack 129 } 130 131 // End - position of first character immediately after the node. 132 func (p *SliceLit) End() token.Pos { 133 return p.Rbrack + 1 134 } 135 136 func (*SliceLit) exprNode() {} 137 138 // ----------------------------------------------------------------------------- 139 140 // A MatrixLit node represents a matrix literal. 141 type MatrixLit struct { 142 Lbrack token.Pos // position of "[" 143 Elts [][]Expr // list of matrix elements 144 Rbrack token.Pos // position of "]" 145 Incomplete bool // true if (source) expressions are missing in the Elts list 146 } 147 148 // Pos - position of first character belonging to the node. 149 func (p *MatrixLit) Pos() token.Pos { 150 return p.Lbrack 151 } 152 153 // End - position of first character immediately after the node. 154 func (p *MatrixLit) End() token.Pos { 155 return p.Rbrack + 1 156 } 157 158 func (*MatrixLit) exprNode() {} 159 160 // ----------------------------------------------------------------------------- 161 162 // A ElemEllipsis node represents a matrix row elements. 163 type ElemEllipsis struct { 164 Elt Expr // ellipsis element 165 Ellipsis token.Pos // position of "..." 166 } 167 168 // Pos - position of first character belonging to the node. 169 func (p *ElemEllipsis) Pos() token.Pos { 170 return p.Elt.Pos() 171 } 172 173 // End - position of first character immediately after the node. 174 func (p *ElemEllipsis) End() token.Pos { 175 return p.Ellipsis + 3 176 } 177 178 func (*ElemEllipsis) exprNode() {} 179 180 // ----------------------------------------------------------------------------- 181 182 // ErrWrapExpr represents `expr!`, `expr?` or `expr?: defaultValue`. 183 type ErrWrapExpr struct { 184 X Expr 185 Tok token.Token // ! or ? 186 TokPos token.Pos 187 Default Expr // can be nil 188 } 189 190 // Pos - position of first character belonging to the node. 191 func (p *ErrWrapExpr) Pos() token.Pos { 192 return p.X.Pos() 193 } 194 195 // End - position of first character immediately after the node. 196 func (p *ErrWrapExpr) End() token.Pos { 197 if p.Default != nil { 198 return p.Default.End() 199 } 200 return p.TokPos + 1 201 } 202 203 func (*ErrWrapExpr) exprNode() {} 204 205 // ----------------------------------------------------------------------------- 206 207 // LambdaExpr represents one of the following expressions: 208 // 209 // `(x, y, ...) => exprOrExprTuple` 210 // `x => exprOrExprTuple` 211 // `=> exprOrExprTuple` 212 // 213 // here exprOrExprTuple represents 214 // 215 // `expr` 216 // `(expr1, expr2, ...)` 217 type LambdaExpr struct { 218 First token.Pos 219 Lhs []*Ident 220 Rarrow token.Pos 221 Rhs []Expr 222 Last token.Pos 223 LhsHasParen bool 224 RhsHasParen bool 225 } 226 227 // LambdaExpr2 represents one of the following expressions: 228 // 229 // `(x, y, ...) => { ... }` 230 // `x => { ... }` 231 // `=> { ... }` 232 type LambdaExpr2 struct { 233 First token.Pos 234 Lhs []*Ident 235 Rarrow token.Pos 236 Body *BlockStmt 237 LhsHasParen bool 238 } 239 240 // Pos - position of first character belonging to the node. 241 func (p *LambdaExpr) Pos() token.Pos { 242 return p.First 243 } 244 245 // End - position of first character immediately after the node. 246 func (p *LambdaExpr) End() token.Pos { 247 return p.Last 248 } 249 250 // Pos - position of first character belonging to the node. 251 func (p *LambdaExpr2) Pos() token.Pos { 252 return p.First 253 } 254 255 // End - position of first character immediately after the node. 256 func (p *LambdaExpr2) End() token.Pos { 257 return p.Body.End() 258 } 259 260 func (*LambdaExpr) exprNode() {} 261 func (*LambdaExpr2) exprNode() {} 262 263 // ----------------------------------------------------------------------------- 264 265 // ForPhrase represents `for k, v <- container if init; cond` phrase. 266 type ForPhrase struct { 267 For token.Pos // position of "for" keyword 268 Key, Value *Ident // Key may be nil 269 TokPos token.Pos // position of "<-" operator 270 X Expr // value to range over 271 IfPos token.Pos // position of if or comma; or NoPos 272 Init Stmt // initialization statement; or nil 273 Cond Expr // value filter, can be nil 274 } 275 276 // Pos returns position of first character belonging to the node. 277 func (p *ForPhrase) Pos() token.Pos { return p.For } 278 279 // End returns position of first character immediately after the node. 280 func (p *ForPhrase) End() token.Pos { 281 if p.Cond != nil { 282 return p.Cond.End() 283 } 284 return p.X.End() 285 } 286 287 func (p *ForPhrase) exprNode() {} 288 289 // ComprehensionExpr represents one of the following expressions: 290 // 291 // `[vexpr for k1, v1 <- container1, cond1 ...]` or 292 // `{vexpr for k1, v1 <- container1, cond1 ...}` or 293 // `{kexpr: vexpr for k1, v1 <- container1, cond1 ...}` or 294 // `{for k1, v1 <- container1, cond1 ...}` or 295 type ComprehensionExpr struct { 296 Lpos token.Pos // position of "[" or "{" 297 Tok token.Token // token.LBRACK '[' or token.LBRACE '{' 298 Elt Expr // *KeyValueExpr or Expr or nil 299 Fors []*ForPhrase 300 Rpos token.Pos // position of "]" or "}" 301 } 302 303 // Pos - position of first character belonging to the node. 304 func (p *ComprehensionExpr) Pos() token.Pos { 305 return p.Lpos 306 } 307 308 // End - position of first character immediately after the node. 309 func (p *ComprehensionExpr) End() token.Pos { 310 return p.Rpos + 1 311 } 312 313 func (*ComprehensionExpr) exprNode() {} 314 315 // ----------------------------------------------------------------------------- 316 317 // A ForPhraseStmt represents a for statement with a for <- clause. 318 type ForPhraseStmt struct { 319 *ForPhrase 320 Body *BlockStmt 321 } 322 323 // Pos - position of first character belonging to the node. 324 func (p *ForPhraseStmt) Pos() token.Pos { 325 return p.For 326 } 327 328 // End - position of first character immediately after the node. 329 func (p *ForPhraseStmt) End() token.Pos { 330 return p.Body.End() 331 } 332 333 func (*ForPhraseStmt) stmtNode() {} 334 335 // ----------------------------------------------------------------------------- 336 337 // A RangeExpr node represents a range expression. 338 type RangeExpr struct { 339 First Expr // start of composite elements; or nil 340 To token.Pos // position of ":" 341 Last Expr // end of composite elements 342 Colon2 token.Pos // position of ":" or token.NoPos 343 Expr3 Expr // step (or max) of composite elements; or nil 344 } 345 346 // Pos - position of first character belonging to the node. 347 func (p *RangeExpr) Pos() token.Pos { 348 if p.First != nil { 349 return p.First.Pos() 350 } 351 return p.To 352 } 353 354 // End - position of first character immediately after the node. 355 func (p *RangeExpr) End() token.Pos { 356 if p.Expr3 != nil { 357 return p.Expr3.End() 358 } 359 if p.Colon2 != token.NoPos { 360 return p.Colon2 + 1 361 } 362 if p.Last != nil { 363 return p.Last.End() 364 } 365 return p.To + 1 366 } 367 368 func (*RangeExpr) exprNode() {} 369 370 // -----------------------------------------------------------------------------