node.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. /*
  2. Package ast declares types representing a JavaScript AST.
  3. Warning
  4. The parser and AST interfaces are still works-in-progress (particularly where
  5. node types are concerned) and may change in the future.
  6. */
  7. package ast
  8. import (
  9. "github.com/dop251/goja/file"
  10. "github.com/dop251/goja/token"
  11. "github.com/dop251/goja/unistring"
  12. )
  13. type PropertyKind string
  14. const (
  15. PropertyKindValue PropertyKind = "value"
  16. PropertyKindGet PropertyKind = "get"
  17. PropertyKindSet PropertyKind = "set"
  18. PropertyKindMethod PropertyKind = "method"
  19. )
  20. // All nodes implement the Node interface.
  21. type Node interface {
  22. Idx0() file.Idx // The index of the first character belonging to the node
  23. Idx1() file.Idx // The index of the first character immediately after the node
  24. }
  25. // ========== //
  26. // Expression //
  27. // ========== //
  28. type (
  29. // All expression nodes implement the Expression interface.
  30. Expression interface {
  31. Node
  32. _expressionNode()
  33. }
  34. BindingTarget interface {
  35. Expression
  36. _bindingTarget()
  37. }
  38. Binding struct {
  39. Target BindingTarget
  40. Initializer Expression
  41. }
  42. Pattern interface {
  43. BindingTarget
  44. _pattern()
  45. }
  46. ArrayLiteral struct {
  47. LeftBracket file.Idx
  48. RightBracket file.Idx
  49. Value []Expression
  50. }
  51. ArrayPattern struct {
  52. LeftBracket file.Idx
  53. RightBracket file.Idx
  54. Elements []Expression
  55. Rest Expression
  56. }
  57. AssignExpression struct {
  58. Operator token.Token
  59. Left Expression
  60. Right Expression
  61. }
  62. BadExpression struct {
  63. From file.Idx
  64. To file.Idx
  65. }
  66. BinaryExpression struct {
  67. Operator token.Token
  68. Left Expression
  69. Right Expression
  70. Comparison bool
  71. }
  72. BooleanLiteral struct {
  73. Idx file.Idx
  74. Literal string
  75. Value bool
  76. }
  77. BracketExpression struct {
  78. Left Expression
  79. Member Expression
  80. LeftBracket file.Idx
  81. RightBracket file.Idx
  82. }
  83. CallExpression struct {
  84. Callee Expression
  85. LeftParenthesis file.Idx
  86. ArgumentList []Expression
  87. RightParenthesis file.Idx
  88. }
  89. ConditionalExpression struct {
  90. Test Expression
  91. Consequent Expression
  92. Alternate Expression
  93. }
  94. DotExpression struct {
  95. Left Expression
  96. Identifier Identifier
  97. }
  98. FunctionLiteral struct {
  99. Function file.Idx
  100. Name *Identifier
  101. ParameterList *ParameterList
  102. Body *BlockStatement
  103. Source string
  104. DeclarationList []*VariableDeclaration
  105. }
  106. Identifier struct {
  107. Name unistring.String
  108. Idx file.Idx
  109. }
  110. NewExpression struct {
  111. New file.Idx
  112. Callee Expression
  113. LeftParenthesis file.Idx
  114. ArgumentList []Expression
  115. RightParenthesis file.Idx
  116. }
  117. NullLiteral struct {
  118. Idx file.Idx
  119. Literal string
  120. }
  121. NumberLiteral struct {
  122. Idx file.Idx
  123. Literal string
  124. Value interface{}
  125. }
  126. ObjectLiteral struct {
  127. LeftBrace file.Idx
  128. RightBrace file.Idx
  129. Value []Property
  130. }
  131. ObjectPattern struct {
  132. LeftBrace file.Idx
  133. RightBrace file.Idx
  134. Properties []Property
  135. Rest Expression
  136. }
  137. ParameterList struct {
  138. Opening file.Idx
  139. List []*Binding
  140. Rest Expression
  141. Closing file.Idx
  142. }
  143. Property interface {
  144. Expression
  145. _property()
  146. }
  147. PropertyShort struct {
  148. Name Identifier
  149. Initializer Expression
  150. }
  151. PropertyKeyed struct {
  152. Key Expression
  153. Kind PropertyKind
  154. Value Expression
  155. }
  156. SpreadElement struct {
  157. Expression
  158. }
  159. RegExpLiteral struct {
  160. Idx file.Idx
  161. Literal string
  162. Pattern string
  163. Flags string
  164. }
  165. SequenceExpression struct {
  166. Sequence []Expression
  167. }
  168. StringLiteral struct {
  169. Idx file.Idx
  170. Literal string
  171. Value unistring.String
  172. }
  173. ThisExpression struct {
  174. Idx file.Idx
  175. }
  176. UnaryExpression struct {
  177. Operator token.Token
  178. Idx file.Idx // If a prefix operation
  179. Operand Expression
  180. Postfix bool
  181. }
  182. MetaProperty struct {
  183. Meta, Property *Identifier
  184. Idx file.Idx
  185. }
  186. )
  187. // _expressionNode
  188. func (*ArrayLiteral) _expressionNode() {}
  189. func (*AssignExpression) _expressionNode() {}
  190. func (*BadExpression) _expressionNode() {}
  191. func (*BinaryExpression) _expressionNode() {}
  192. func (*BooleanLiteral) _expressionNode() {}
  193. func (*BracketExpression) _expressionNode() {}
  194. func (*CallExpression) _expressionNode() {}
  195. func (*ConditionalExpression) _expressionNode() {}
  196. func (*DotExpression) _expressionNode() {}
  197. func (*FunctionLiteral) _expressionNode() {}
  198. func (*Identifier) _expressionNode() {}
  199. func (*NewExpression) _expressionNode() {}
  200. func (*NullLiteral) _expressionNode() {}
  201. func (*NumberLiteral) _expressionNode() {}
  202. func (*ObjectLiteral) _expressionNode() {}
  203. func (*RegExpLiteral) _expressionNode() {}
  204. func (*SequenceExpression) _expressionNode() {}
  205. func (*StringLiteral) _expressionNode() {}
  206. func (*ThisExpression) _expressionNode() {}
  207. func (*UnaryExpression) _expressionNode() {}
  208. func (*MetaProperty) _expressionNode() {}
  209. func (*ObjectPattern) _expressionNode() {}
  210. func (*ArrayPattern) _expressionNode() {}
  211. func (*Binding) _expressionNode() {}
  212. func (*PropertyShort) _expressionNode() {}
  213. func (*PropertyKeyed) _expressionNode() {}
  214. // ========= //
  215. // Statement //
  216. // ========= //
  217. type (
  218. // All statement nodes implement the Statement interface.
  219. Statement interface {
  220. Node
  221. _statementNode()
  222. }
  223. BadStatement struct {
  224. From file.Idx
  225. To file.Idx
  226. }
  227. BlockStatement struct {
  228. LeftBrace file.Idx
  229. List []Statement
  230. RightBrace file.Idx
  231. }
  232. BranchStatement struct {
  233. Idx file.Idx
  234. Token token.Token
  235. Label *Identifier
  236. }
  237. CaseStatement struct {
  238. Case file.Idx
  239. Test Expression
  240. Consequent []Statement
  241. }
  242. CatchStatement struct {
  243. Catch file.Idx
  244. Parameter BindingTarget
  245. Body *BlockStatement
  246. }
  247. DebuggerStatement struct {
  248. Debugger file.Idx
  249. }
  250. DoWhileStatement struct {
  251. Do file.Idx
  252. Test Expression
  253. Body Statement
  254. }
  255. EmptyStatement struct {
  256. Semicolon file.Idx
  257. }
  258. ExpressionStatement struct {
  259. Expression Expression
  260. }
  261. ForInStatement struct {
  262. For file.Idx
  263. Into ForInto
  264. Source Expression
  265. Body Statement
  266. }
  267. ForOfStatement struct {
  268. For file.Idx
  269. Into ForInto
  270. Source Expression
  271. Body Statement
  272. }
  273. ForStatement struct {
  274. For file.Idx
  275. Initializer ForLoopInitializer
  276. Update Expression
  277. Test Expression
  278. Body Statement
  279. }
  280. IfStatement struct {
  281. If file.Idx
  282. Test Expression
  283. Consequent Statement
  284. Alternate Statement
  285. }
  286. LabelledStatement struct {
  287. Label *Identifier
  288. Colon file.Idx
  289. Statement Statement
  290. }
  291. ReturnStatement struct {
  292. Return file.Idx
  293. Argument Expression
  294. }
  295. SwitchStatement struct {
  296. Switch file.Idx
  297. Discriminant Expression
  298. Default int
  299. Body []*CaseStatement
  300. }
  301. ThrowStatement struct {
  302. Throw file.Idx
  303. Argument Expression
  304. }
  305. TryStatement struct {
  306. Try file.Idx
  307. Body *BlockStatement
  308. Catch *CatchStatement
  309. Finally *BlockStatement
  310. }
  311. VariableStatement struct {
  312. Var file.Idx
  313. List []*Binding
  314. }
  315. LexicalDeclaration struct {
  316. Idx file.Idx
  317. Token token.Token
  318. List []*Binding
  319. }
  320. WhileStatement struct {
  321. While file.Idx
  322. Test Expression
  323. Body Statement
  324. }
  325. WithStatement struct {
  326. With file.Idx
  327. Object Expression
  328. Body Statement
  329. }
  330. FunctionDeclaration struct {
  331. Function *FunctionLiteral
  332. }
  333. )
  334. // _statementNode
  335. func (*BadStatement) _statementNode() {}
  336. func (*BlockStatement) _statementNode() {}
  337. func (*BranchStatement) _statementNode() {}
  338. func (*CaseStatement) _statementNode() {}
  339. func (*CatchStatement) _statementNode() {}
  340. func (*DebuggerStatement) _statementNode() {}
  341. func (*DoWhileStatement) _statementNode() {}
  342. func (*EmptyStatement) _statementNode() {}
  343. func (*ExpressionStatement) _statementNode() {}
  344. func (*ForInStatement) _statementNode() {}
  345. func (*ForOfStatement) _statementNode() {}
  346. func (*ForStatement) _statementNode() {}
  347. func (*IfStatement) _statementNode() {}
  348. func (*LabelledStatement) _statementNode() {}
  349. func (*ReturnStatement) _statementNode() {}
  350. func (*SwitchStatement) _statementNode() {}
  351. func (*ThrowStatement) _statementNode() {}
  352. func (*TryStatement) _statementNode() {}
  353. func (*VariableStatement) _statementNode() {}
  354. func (*WhileStatement) _statementNode() {}
  355. func (*WithStatement) _statementNode() {}
  356. func (*LexicalDeclaration) _statementNode() {}
  357. func (*FunctionDeclaration) _statementNode() {}
  358. // =========== //
  359. // Declaration //
  360. // =========== //
  361. type (
  362. VariableDeclaration struct {
  363. Var file.Idx
  364. List []*Binding
  365. }
  366. )
  367. type (
  368. ForLoopInitializer interface {
  369. _forLoopInitializer()
  370. }
  371. ForLoopInitializerExpression struct {
  372. Expression Expression
  373. }
  374. ForLoopInitializerVarDeclList struct {
  375. Var file.Idx
  376. List []*Binding
  377. }
  378. ForLoopInitializerLexicalDecl struct {
  379. LexicalDeclaration LexicalDeclaration
  380. }
  381. ForInto interface {
  382. _forInto()
  383. }
  384. ForIntoVar struct {
  385. Binding *Binding
  386. }
  387. ForDeclaration struct {
  388. Idx file.Idx
  389. IsConst bool
  390. Target BindingTarget
  391. }
  392. ForIntoExpression struct {
  393. Expression Expression
  394. }
  395. )
  396. func (*ForLoopInitializerExpression) _forLoopInitializer() {}
  397. func (*ForLoopInitializerVarDeclList) _forLoopInitializer() {}
  398. func (*ForLoopInitializerLexicalDecl) _forLoopInitializer() {}
  399. func (*ForIntoVar) _forInto() {}
  400. func (*ForDeclaration) _forInto() {}
  401. func (*ForIntoExpression) _forInto() {}
  402. func (*ArrayPattern) _pattern() {}
  403. func (*ArrayPattern) _bindingTarget() {}
  404. func (*ObjectPattern) _pattern() {}
  405. func (*ObjectPattern) _bindingTarget() {}
  406. func (*BadExpression) _bindingTarget() {}
  407. func (*PropertyShort) _property() {}
  408. func (*PropertyKeyed) _property() {}
  409. func (*SpreadElement) _property() {}
  410. func (*Identifier) _bindingTarget() {}
  411. // ==== //
  412. // Node //
  413. // ==== //
  414. type Program struct {
  415. Body []Statement
  416. DeclarationList []*VariableDeclaration
  417. File *file.File
  418. }
  419. // ==== //
  420. // Idx0 //
  421. // ==== //
  422. func (self *ArrayLiteral) Idx0() file.Idx { return self.LeftBracket }
  423. func (self *ArrayPattern) Idx0() file.Idx { return self.LeftBracket }
  424. func (self *ObjectPattern) Idx0() file.Idx { return self.LeftBrace }
  425. func (self *AssignExpression) Idx0() file.Idx { return self.Left.Idx0() }
  426. func (self *BadExpression) Idx0() file.Idx { return self.From }
  427. func (self *BinaryExpression) Idx0() file.Idx { return self.Left.Idx0() }
  428. func (self *BooleanLiteral) Idx0() file.Idx { return self.Idx }
  429. func (self *BracketExpression) Idx0() file.Idx { return self.Left.Idx0() }
  430. func (self *CallExpression) Idx0() file.Idx { return self.Callee.Idx0() }
  431. func (self *ConditionalExpression) Idx0() file.Idx { return self.Test.Idx0() }
  432. func (self *DotExpression) Idx0() file.Idx { return self.Left.Idx0() }
  433. func (self *FunctionLiteral) Idx0() file.Idx { return self.Function }
  434. func (self *Identifier) Idx0() file.Idx { return self.Idx }
  435. func (self *NewExpression) Idx0() file.Idx { return self.New }
  436. func (self *NullLiteral) Idx0() file.Idx { return self.Idx }
  437. func (self *NumberLiteral) Idx0() file.Idx { return self.Idx }
  438. func (self *ObjectLiteral) Idx0() file.Idx { return self.LeftBrace }
  439. func (self *RegExpLiteral) Idx0() file.Idx { return self.Idx }
  440. func (self *SequenceExpression) Idx0() file.Idx { return self.Sequence[0].Idx0() }
  441. func (self *StringLiteral) Idx0() file.Idx { return self.Idx }
  442. func (self *ThisExpression) Idx0() file.Idx { return self.Idx }
  443. func (self *UnaryExpression) Idx0() file.Idx { return self.Idx }
  444. func (self *MetaProperty) Idx0() file.Idx { return self.Idx }
  445. func (self *BadStatement) Idx0() file.Idx { return self.From }
  446. func (self *BlockStatement) Idx0() file.Idx { return self.LeftBrace }
  447. func (self *BranchStatement) Idx0() file.Idx { return self.Idx }
  448. func (self *CaseStatement) Idx0() file.Idx { return self.Case }
  449. func (self *CatchStatement) Idx0() file.Idx { return self.Catch }
  450. func (self *DebuggerStatement) Idx0() file.Idx { return self.Debugger }
  451. func (self *DoWhileStatement) Idx0() file.Idx { return self.Do }
  452. func (self *EmptyStatement) Idx0() file.Idx { return self.Semicolon }
  453. func (self *ExpressionStatement) Idx0() file.Idx { return self.Expression.Idx0() }
  454. func (self *ForInStatement) Idx0() file.Idx { return self.For }
  455. func (self *ForOfStatement) Idx0() file.Idx { return self.For }
  456. func (self *ForStatement) Idx0() file.Idx { return self.For }
  457. func (self *IfStatement) Idx0() file.Idx { return self.If }
  458. func (self *LabelledStatement) Idx0() file.Idx { return self.Label.Idx0() }
  459. func (self *Program) Idx0() file.Idx { return self.Body[0].Idx0() }
  460. func (self *ReturnStatement) Idx0() file.Idx { return self.Return }
  461. func (self *SwitchStatement) Idx0() file.Idx { return self.Switch }
  462. func (self *ThrowStatement) Idx0() file.Idx { return self.Throw }
  463. func (self *TryStatement) Idx0() file.Idx { return self.Try }
  464. func (self *VariableStatement) Idx0() file.Idx { return self.Var }
  465. func (self *WhileStatement) Idx0() file.Idx { return self.While }
  466. func (self *WithStatement) Idx0() file.Idx { return self.With }
  467. func (self *LexicalDeclaration) Idx0() file.Idx { return self.Idx }
  468. func (self *FunctionDeclaration) Idx0() file.Idx { return self.Function.Idx0() }
  469. func (self *Binding) Idx0() file.Idx { return self.Target.Idx0() }
  470. func (self *ForLoopInitializerVarDeclList) Idx0() file.Idx { return self.List[0].Idx0() }
  471. func (self *PropertyShort) Idx0() file.Idx { return self.Name.Idx }
  472. func (self *PropertyKeyed) Idx0() file.Idx { return self.Key.Idx0() }
  473. // ==== //
  474. // Idx1 //
  475. // ==== //
  476. func (self *ArrayLiteral) Idx1() file.Idx { return self.RightBracket + 1 }
  477. func (self *ArrayPattern) Idx1() file.Idx { return self.RightBracket + 1 }
  478. func (self *AssignExpression) Idx1() file.Idx { return self.Right.Idx1() }
  479. func (self *BadExpression) Idx1() file.Idx { return self.To }
  480. func (self *BinaryExpression) Idx1() file.Idx { return self.Right.Idx1() }
  481. func (self *BooleanLiteral) Idx1() file.Idx { return file.Idx(int(self.Idx) + len(self.Literal)) }
  482. func (self *BracketExpression) Idx1() file.Idx { return self.RightBracket + 1 }
  483. func (self *CallExpression) Idx1() file.Idx { return self.RightParenthesis + 1 }
  484. func (self *ConditionalExpression) Idx1() file.Idx { return self.Test.Idx1() }
  485. func (self *DotExpression) Idx1() file.Idx { return self.Identifier.Idx1() }
  486. func (self *FunctionLiteral) Idx1() file.Idx { return self.Body.Idx1() }
  487. func (self *Identifier) Idx1() file.Idx { return file.Idx(int(self.Idx) + len(self.Name)) }
  488. func (self *NewExpression) Idx1() file.Idx { return self.RightParenthesis + 1 }
  489. func (self *NullLiteral) Idx1() file.Idx { return file.Idx(int(self.Idx) + 4) } // "null"
  490. func (self *NumberLiteral) Idx1() file.Idx { return file.Idx(int(self.Idx) + len(self.Literal)) }
  491. func (self *ObjectLiteral) Idx1() file.Idx { return self.RightBrace + 1 }
  492. func (self *ObjectPattern) Idx1() file.Idx { return self.RightBrace + 1 }
  493. func (self *RegExpLiteral) Idx1() file.Idx { return file.Idx(int(self.Idx) + len(self.Literal)) }
  494. func (self *SequenceExpression) Idx1() file.Idx { return self.Sequence[len(self.Sequence)-1].Idx1() }
  495. func (self *StringLiteral) Idx1() file.Idx { return file.Idx(int(self.Idx) + len(self.Literal)) }
  496. func (self *ThisExpression) Idx1() file.Idx { return self.Idx + 4 }
  497. func (self *UnaryExpression) Idx1() file.Idx {
  498. if self.Postfix {
  499. return self.Operand.Idx1() + 2 // ++ --
  500. }
  501. return self.Operand.Idx1()
  502. }
  503. func (self *MetaProperty) Idx1() file.Idx {
  504. return self.Property.Idx1()
  505. }
  506. func (self *BadStatement) Idx1() file.Idx { return self.To }
  507. func (self *BlockStatement) Idx1() file.Idx { return self.RightBrace + 1 }
  508. func (self *BranchStatement) Idx1() file.Idx { return self.Idx }
  509. func (self *CaseStatement) Idx1() file.Idx { return self.Consequent[len(self.Consequent)-1].Idx1() }
  510. func (self *CatchStatement) Idx1() file.Idx { return self.Body.Idx1() }
  511. func (self *DebuggerStatement) Idx1() file.Idx { return self.Debugger + 8 }
  512. func (self *DoWhileStatement) Idx1() file.Idx { return self.Test.Idx1() }
  513. func (self *EmptyStatement) Idx1() file.Idx { return self.Semicolon + 1 }
  514. func (self *ExpressionStatement) Idx1() file.Idx { return self.Expression.Idx1() }
  515. func (self *ForInStatement) Idx1() file.Idx { return self.Body.Idx1() }
  516. func (self *ForOfStatement) Idx1() file.Idx { return self.Body.Idx1() }
  517. func (self *ForStatement) Idx1() file.Idx { return self.Body.Idx1() }
  518. func (self *IfStatement) Idx1() file.Idx {
  519. if self.Alternate != nil {
  520. return self.Alternate.Idx1()
  521. }
  522. return self.Consequent.Idx1()
  523. }
  524. func (self *LabelledStatement) Idx1() file.Idx { return self.Colon + 1 }
  525. func (self *Program) Idx1() file.Idx { return self.Body[len(self.Body)-1].Idx1() }
  526. func (self *ReturnStatement) Idx1() file.Idx { return self.Return + 6 }
  527. func (self *SwitchStatement) Idx1() file.Idx { return self.Body[len(self.Body)-1].Idx1() }
  528. func (self *ThrowStatement) Idx1() file.Idx { return self.Argument.Idx1() }
  529. func (self *TryStatement) Idx1() file.Idx {
  530. if self.Finally != nil {
  531. return self.Finally.Idx1()
  532. }
  533. if self.Catch != nil {
  534. return self.Catch.Idx1()
  535. }
  536. return self.Body.Idx1()
  537. }
  538. func (self *VariableStatement) Idx1() file.Idx { return self.List[len(self.List)-1].Idx1() }
  539. func (self *WhileStatement) Idx1() file.Idx { return self.Body.Idx1() }
  540. func (self *WithStatement) Idx1() file.Idx { return self.Body.Idx1() }
  541. func (self *LexicalDeclaration) Idx1() file.Idx { return self.List[len(self.List)-1].Idx1() }
  542. func (self *FunctionDeclaration) Idx1() file.Idx { return self.Function.Idx1() }
  543. func (self *Binding) Idx1() file.Idx {
  544. if self.Initializer != nil {
  545. return self.Initializer.Idx1()
  546. }
  547. return self.Target.Idx1()
  548. }
  549. func (self *ForLoopInitializerVarDeclList) Idx1() file.Idx { return self.List[len(self.List)-1].Idx1() }
  550. func (self *PropertyShort) Idx1() file.Idx {
  551. if self.Initializer != nil {
  552. return self.Initializer.Idx1()
  553. }
  554. return self.Name.Idx1()
  555. }
  556. func (self *PropertyKeyed) Idx1() file.Idx { return self.Value.Idx1() }