expr.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. package parser
  2. import (
  3. "strings"
  4. "github.com/d5/tengo/v2/token"
  5. )
  6. // Expr represents an expression node in the AST.
  7. type Expr interface {
  8. Node
  9. exprNode()
  10. }
  11. // ArrayLit represents an array literal.
  12. type ArrayLit struct {
  13. Elements []Expr
  14. LBrack Pos
  15. RBrack Pos
  16. }
  17. func (e *ArrayLit) exprNode() {}
  18. // Pos returns the position of first character belonging to the node.
  19. func (e *ArrayLit) Pos() Pos {
  20. return e.LBrack
  21. }
  22. // End returns the position of first character immediately after the node.
  23. func (e *ArrayLit) End() Pos {
  24. return e.RBrack + 1
  25. }
  26. func (e *ArrayLit) String() string {
  27. var elements []string
  28. for _, m := range e.Elements {
  29. elements = append(elements, m.String())
  30. }
  31. return "[" + strings.Join(elements, ", ") + "]"
  32. }
  33. // BadExpr represents a bad expression.
  34. type BadExpr struct {
  35. From Pos
  36. To Pos
  37. }
  38. func (e *BadExpr) exprNode() {}
  39. // Pos returns the position of first character belonging to the node.
  40. func (e *BadExpr) Pos() Pos {
  41. return e.From
  42. }
  43. // End returns the position of first character immediately after the node.
  44. func (e *BadExpr) End() Pos {
  45. return e.To
  46. }
  47. func (e *BadExpr) String() string {
  48. return "<bad expression>"
  49. }
  50. // BinaryExpr represents a binary operator expression.
  51. type BinaryExpr struct {
  52. LHS Expr
  53. RHS Expr
  54. Token token.Token
  55. TokenPos Pos
  56. }
  57. func (e *BinaryExpr) exprNode() {}
  58. // Pos returns the position of first character belonging to the node.
  59. func (e *BinaryExpr) Pos() Pos {
  60. return e.LHS.Pos()
  61. }
  62. // End returns the position of first character immediately after the node.
  63. func (e *BinaryExpr) End() Pos {
  64. return e.RHS.End()
  65. }
  66. func (e *BinaryExpr) String() string {
  67. return "(" + e.LHS.String() + " " + e.Token.String() +
  68. " " + e.RHS.String() + ")"
  69. }
  70. // BoolLit represents a boolean literal.
  71. type BoolLit struct {
  72. Value bool
  73. ValuePos Pos
  74. Literal string
  75. }
  76. func (e *BoolLit) exprNode() {}
  77. // Pos returns the position of first character belonging to the node.
  78. func (e *BoolLit) Pos() Pos {
  79. return e.ValuePos
  80. }
  81. // End returns the position of first character immediately after the node.
  82. func (e *BoolLit) End() Pos {
  83. return Pos(int(e.ValuePos) + len(e.Literal))
  84. }
  85. func (e *BoolLit) String() string {
  86. return e.Literal
  87. }
  88. // CallExpr represents a function call expression.
  89. type CallExpr struct {
  90. Func Expr
  91. LParen Pos
  92. Args []Expr
  93. Ellipsis Pos
  94. RParen Pos
  95. }
  96. func (e *CallExpr) exprNode() {}
  97. // Pos returns the position of first character belonging to the node.
  98. func (e *CallExpr) Pos() Pos {
  99. return e.Func.Pos()
  100. }
  101. // End returns the position of first character immediately after the node.
  102. func (e *CallExpr) End() Pos {
  103. return e.RParen + 1
  104. }
  105. func (e *CallExpr) String() string {
  106. var args []string
  107. for _, e := range e.Args {
  108. args = append(args, e.String())
  109. }
  110. if len(args) > 0 && e.Ellipsis.IsValid() {
  111. args[len(args)-1] = args[len(args)-1] + "..."
  112. }
  113. return e.Func.String() + "(" + strings.Join(args, ", ") + ")"
  114. }
  115. // CharLit represents a character literal.
  116. type CharLit struct {
  117. Value rune
  118. ValuePos Pos
  119. Literal string
  120. }
  121. func (e *CharLit) exprNode() {}
  122. // Pos returns the position of first character belonging to the node.
  123. func (e *CharLit) Pos() Pos {
  124. return e.ValuePos
  125. }
  126. // End returns the position of first character immediately after the node.
  127. func (e *CharLit) End() Pos {
  128. return Pos(int(e.ValuePos) + len(e.Literal))
  129. }
  130. func (e *CharLit) String() string {
  131. return e.Literal
  132. }
  133. // CondExpr represents a ternary conditional expression.
  134. type CondExpr struct {
  135. Cond Expr
  136. True Expr
  137. False Expr
  138. QuestionPos Pos
  139. ColonPos Pos
  140. }
  141. func (e *CondExpr) exprNode() {}
  142. // Pos returns the position of first character belonging to the node.
  143. func (e *CondExpr) Pos() Pos {
  144. return e.Cond.Pos()
  145. }
  146. // End returns the position of first character immediately after the node.
  147. func (e *CondExpr) End() Pos {
  148. return e.False.End()
  149. }
  150. func (e *CondExpr) String() string {
  151. return "(" + e.Cond.String() + " ? " + e.True.String() +
  152. " : " + e.False.String() + ")"
  153. }
  154. // ErrorExpr represents an error expression
  155. type ErrorExpr struct {
  156. Expr Expr
  157. ErrorPos Pos
  158. LParen Pos
  159. RParen Pos
  160. }
  161. func (e *ErrorExpr) exprNode() {}
  162. // Pos returns the position of first character belonging to the node.
  163. func (e *ErrorExpr) Pos() Pos {
  164. return e.ErrorPos
  165. }
  166. // End returns the position of first character immediately after the node.
  167. func (e *ErrorExpr) End() Pos {
  168. return e.RParen
  169. }
  170. func (e *ErrorExpr) String() string {
  171. return "error(" + e.Expr.String() + ")"
  172. }
  173. // FloatLit represents a floating point literal.
  174. type FloatLit struct {
  175. Value float64
  176. ValuePos Pos
  177. Literal string
  178. }
  179. func (e *FloatLit) exprNode() {}
  180. // Pos returns the position of first character belonging to the node.
  181. func (e *FloatLit) Pos() Pos {
  182. return e.ValuePos
  183. }
  184. // End returns the position of first character immediately after the node.
  185. func (e *FloatLit) End() Pos {
  186. return Pos(int(e.ValuePos) + len(e.Literal))
  187. }
  188. func (e *FloatLit) String() string {
  189. return e.Literal
  190. }
  191. // FuncLit represents a function literal.
  192. type FuncLit struct {
  193. Type *FuncType
  194. Body *BlockStmt
  195. }
  196. func (e *FuncLit) exprNode() {}
  197. // Pos returns the position of first character belonging to the node.
  198. func (e *FuncLit) Pos() Pos {
  199. return e.Type.Pos()
  200. }
  201. // End returns the position of first character immediately after the node.
  202. func (e *FuncLit) End() Pos {
  203. return e.Body.End()
  204. }
  205. func (e *FuncLit) String() string {
  206. return "func" + e.Type.Params.String() + " " + e.Body.String()
  207. }
  208. // FuncType represents a function type definition.
  209. type FuncType struct {
  210. FuncPos Pos
  211. Params *IdentList
  212. }
  213. func (e *FuncType) exprNode() {}
  214. // Pos returns the position of first character belonging to the node.
  215. func (e *FuncType) Pos() Pos {
  216. return e.FuncPos
  217. }
  218. // End returns the position of first character immediately after the node.
  219. func (e *FuncType) End() Pos {
  220. return e.Params.End()
  221. }
  222. func (e *FuncType) String() string {
  223. return "func" + e.Params.String()
  224. }
  225. // Ident represents an identifier.
  226. type Ident struct {
  227. Name string
  228. NamePos Pos
  229. }
  230. func (e *Ident) exprNode() {}
  231. // Pos returns the position of first character belonging to the node.
  232. func (e *Ident) Pos() Pos {
  233. return e.NamePos
  234. }
  235. // End returns the position of first character immediately after the node.
  236. func (e *Ident) End() Pos {
  237. return Pos(int(e.NamePos) + len(e.Name))
  238. }
  239. func (e *Ident) String() string {
  240. if e != nil {
  241. return e.Name
  242. }
  243. return nullRep
  244. }
  245. // ImmutableExpr represents an immutable expression
  246. type ImmutableExpr struct {
  247. Expr Expr
  248. ErrorPos Pos
  249. LParen Pos
  250. RParen Pos
  251. }
  252. func (e *ImmutableExpr) exprNode() {}
  253. // Pos returns the position of first character belonging to the node.
  254. func (e *ImmutableExpr) Pos() Pos {
  255. return e.ErrorPos
  256. }
  257. // End returns the position of first character immediately after the node.
  258. func (e *ImmutableExpr) End() Pos {
  259. return e.RParen
  260. }
  261. func (e *ImmutableExpr) String() string {
  262. return "immutable(" + e.Expr.String() + ")"
  263. }
  264. // ImportExpr represents an import expression
  265. type ImportExpr struct {
  266. ModuleName string
  267. Token token.Token
  268. TokenPos Pos
  269. }
  270. func (e *ImportExpr) exprNode() {}
  271. // Pos returns the position of first character belonging to the node.
  272. func (e *ImportExpr) Pos() Pos {
  273. return e.TokenPos
  274. }
  275. // End returns the position of first character immediately after the node.
  276. func (e *ImportExpr) End() Pos {
  277. // import("moduleName")
  278. return Pos(int(e.TokenPos) + 10 + len(e.ModuleName))
  279. }
  280. func (e *ImportExpr) String() string {
  281. return `import("` + e.ModuleName + `")"`
  282. }
  283. // IndexExpr represents an index expression.
  284. type IndexExpr struct {
  285. Expr Expr
  286. LBrack Pos
  287. Index Expr
  288. RBrack Pos
  289. }
  290. func (e *IndexExpr) exprNode() {}
  291. // Pos returns the position of first character belonging to the node.
  292. func (e *IndexExpr) Pos() Pos {
  293. return e.Expr.Pos()
  294. }
  295. // End returns the position of first character immediately after the node.
  296. func (e *IndexExpr) End() Pos {
  297. return e.RBrack + 1
  298. }
  299. func (e *IndexExpr) String() string {
  300. var index string
  301. if e.Index != nil {
  302. index = e.Index.String()
  303. }
  304. return e.Expr.String() + "[" + index + "]"
  305. }
  306. // IntLit represents an integer literal.
  307. type IntLit struct {
  308. Value int64
  309. ValuePos Pos
  310. Literal string
  311. }
  312. func (e *IntLit) exprNode() {}
  313. // Pos returns the position of first character belonging to the node.
  314. func (e *IntLit) Pos() Pos {
  315. return e.ValuePos
  316. }
  317. // End returns the position of first character immediately after the node.
  318. func (e *IntLit) End() Pos {
  319. return Pos(int(e.ValuePos) + len(e.Literal))
  320. }
  321. func (e *IntLit) String() string {
  322. return e.Literal
  323. }
  324. // MapElementLit represents a map element.
  325. type MapElementLit struct {
  326. Key string
  327. KeyPos Pos
  328. ColonPos Pos
  329. Value Expr
  330. }
  331. func (e *MapElementLit) exprNode() {}
  332. // Pos returns the position of first character belonging to the node.
  333. func (e *MapElementLit) Pos() Pos {
  334. return e.KeyPos
  335. }
  336. // End returns the position of first character immediately after the node.
  337. func (e *MapElementLit) End() Pos {
  338. return e.Value.End()
  339. }
  340. func (e *MapElementLit) String() string {
  341. return e.Key + ": " + e.Value.String()
  342. }
  343. // MapLit represents a map literal.
  344. type MapLit struct {
  345. LBrace Pos
  346. Elements []*MapElementLit
  347. RBrace Pos
  348. }
  349. func (e *MapLit) exprNode() {}
  350. // Pos returns the position of first character belonging to the node.
  351. func (e *MapLit) Pos() Pos {
  352. return e.LBrace
  353. }
  354. // End returns the position of first character immediately after the node.
  355. func (e *MapLit) End() Pos {
  356. return e.RBrace + 1
  357. }
  358. func (e *MapLit) String() string {
  359. var elements []string
  360. for _, m := range e.Elements {
  361. elements = append(elements, m.String())
  362. }
  363. return "{" + strings.Join(elements, ", ") + "}"
  364. }
  365. // ParenExpr represents a parenthesis wrapped expression.
  366. type ParenExpr struct {
  367. Expr Expr
  368. LParen Pos
  369. RParen Pos
  370. }
  371. func (e *ParenExpr) exprNode() {}
  372. // Pos returns the position of first character belonging to the node.
  373. func (e *ParenExpr) Pos() Pos {
  374. return e.LParen
  375. }
  376. // End returns the position of first character immediately after the node.
  377. func (e *ParenExpr) End() Pos {
  378. return e.RParen + 1
  379. }
  380. func (e *ParenExpr) String() string {
  381. return "(" + e.Expr.String() + ")"
  382. }
  383. // SelectorExpr represents a selector expression.
  384. type SelectorExpr struct {
  385. Expr Expr
  386. Sel Expr
  387. }
  388. func (e *SelectorExpr) exprNode() {}
  389. // Pos returns the position of first character belonging to the node.
  390. func (e *SelectorExpr) Pos() Pos {
  391. return e.Expr.Pos()
  392. }
  393. // End returns the position of first character immediately after the node.
  394. func (e *SelectorExpr) End() Pos {
  395. return e.Sel.End()
  396. }
  397. func (e *SelectorExpr) String() string {
  398. return e.Expr.String() + "." + e.Sel.String()
  399. }
  400. // SliceExpr represents a slice expression.
  401. type SliceExpr struct {
  402. Expr Expr
  403. LBrack Pos
  404. Low Expr
  405. High Expr
  406. RBrack Pos
  407. }
  408. func (e *SliceExpr) exprNode() {}
  409. // Pos returns the position of first character belonging to the node.
  410. func (e *SliceExpr) Pos() Pos {
  411. return e.Expr.Pos()
  412. }
  413. // End returns the position of first character immediately after the node.
  414. func (e *SliceExpr) End() Pos {
  415. return e.RBrack + 1
  416. }
  417. func (e *SliceExpr) String() string {
  418. var low, high string
  419. if e.Low != nil {
  420. low = e.Low.String()
  421. }
  422. if e.High != nil {
  423. high = e.High.String()
  424. }
  425. return e.Expr.String() + "[" + low + ":" + high + "]"
  426. }
  427. // StringLit represents a string literal.
  428. type StringLit struct {
  429. Value string
  430. ValuePos Pos
  431. Literal string
  432. }
  433. func (e *StringLit) exprNode() {}
  434. // Pos returns the position of first character belonging to the node.
  435. func (e *StringLit) Pos() Pos {
  436. return e.ValuePos
  437. }
  438. // End returns the position of first character immediately after the node.
  439. func (e *StringLit) End() Pos {
  440. return Pos(int(e.ValuePos) + len(e.Literal))
  441. }
  442. func (e *StringLit) String() string {
  443. return e.Literal
  444. }
  445. // UnaryExpr represents an unary operator expression.
  446. type UnaryExpr struct {
  447. Expr Expr
  448. Token token.Token
  449. TokenPos Pos
  450. }
  451. func (e *UnaryExpr) exprNode() {}
  452. // Pos returns the position of first character belonging to the node.
  453. func (e *UnaryExpr) Pos() Pos {
  454. return e.Expr.Pos()
  455. }
  456. // End returns the position of first character immediately after the node.
  457. func (e *UnaryExpr) End() Pos {
  458. return e.Expr.End()
  459. }
  460. func (e *UnaryExpr) String() string {
  461. return "(" + e.Token.String() + e.Expr.String() + ")"
  462. }
  463. // UndefinedLit represents an undefined literal.
  464. type UndefinedLit struct {
  465. TokenPos Pos
  466. }
  467. func (e *UndefinedLit) exprNode() {}
  468. // Pos returns the position of first character belonging to the node.
  469. func (e *UndefinedLit) Pos() Pos {
  470. return e.TokenPos
  471. }
  472. // End returns the position of first character immediately after the node.
  473. func (e *UndefinedLit) End() Pos {
  474. return e.TokenPos + 9 // len(undefined) == 9
  475. }
  476. func (e *UndefinedLit) String() string {
  477. return "undefined"
  478. }