statement.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  1. package parser
  2. import (
  3. "github.com/robertkrimen/otto/ast"
  4. "github.com/robertkrimen/otto/token"
  5. )
  6. func (p *parser) parseBlockStatement() *ast.BlockStatement {
  7. node := &ast.BlockStatement{}
  8. // Find comments before the leading brace
  9. if p.mode&StoreComments != 0 {
  10. p.comments.CommentMap.AddComments(node, p.comments.FetchAll(), ast.LEADING)
  11. p.comments.Unset()
  12. }
  13. node.LeftBrace = p.expect(token.LEFT_BRACE)
  14. node.List = p.parseStatementList()
  15. if p.mode&StoreComments != 0 {
  16. p.comments.Unset()
  17. p.comments.CommentMap.AddComments(node, p.comments.FetchAll(), ast.FINAL)
  18. p.comments.AfterBlock()
  19. }
  20. node.RightBrace = p.expect(token.RIGHT_BRACE)
  21. // Find comments after the trailing brace
  22. if p.mode&StoreComments != 0 {
  23. p.comments.ResetLineBreak()
  24. p.comments.CommentMap.AddComments(node, p.comments.Fetch(), ast.TRAILING)
  25. }
  26. return node
  27. }
  28. func (p *parser) parseEmptyStatement() ast.Statement {
  29. idx := p.expect(token.SEMICOLON)
  30. return &ast.EmptyStatement{Semicolon: idx}
  31. }
  32. func (p *parser) parseStatementList() (list []ast.Statement) { //nolint:nonamedreturns
  33. for p.token != token.RIGHT_BRACE && p.token != token.EOF {
  34. statement := p.parseStatement()
  35. list = append(list, statement)
  36. }
  37. return list
  38. }
  39. func (p *parser) parseStatement() ast.Statement {
  40. if p.token == token.EOF {
  41. p.errorUnexpectedToken(p.token)
  42. return &ast.BadStatement{From: p.idx, To: p.idx + 1}
  43. }
  44. if p.mode&StoreComments != 0 {
  45. p.comments.ResetLineBreak()
  46. }
  47. switch p.token {
  48. case token.SEMICOLON:
  49. return p.parseEmptyStatement()
  50. case token.LEFT_BRACE:
  51. return p.parseBlockStatement()
  52. case token.IF:
  53. return p.parseIfStatement()
  54. case token.DO:
  55. statement := p.parseDoWhileStatement()
  56. p.comments.PostProcessNode(statement)
  57. return statement
  58. case token.WHILE:
  59. return p.parseWhileStatement()
  60. case token.FOR:
  61. return p.parseForOrForInStatement()
  62. case token.BREAK:
  63. return p.parseBreakStatement()
  64. case token.CONTINUE:
  65. return p.parseContinueStatement()
  66. case token.DEBUGGER:
  67. return p.parseDebuggerStatement()
  68. case token.WITH:
  69. return p.parseWithStatement()
  70. case token.VAR:
  71. return p.parseVariableStatement()
  72. case token.FUNCTION:
  73. return p.parseFunctionStatement()
  74. case token.SWITCH:
  75. return p.parseSwitchStatement()
  76. case token.RETURN:
  77. return p.parseReturnStatement()
  78. case token.THROW:
  79. return p.parseThrowStatement()
  80. case token.TRY:
  81. return p.parseTryStatement()
  82. }
  83. var comments []*ast.Comment
  84. if p.mode&StoreComments != 0 {
  85. comments = p.comments.FetchAll()
  86. }
  87. expression := p.parseExpression()
  88. if identifier, isIdentifier := expression.(*ast.Identifier); isIdentifier && p.token == token.COLON {
  89. // LabelledStatement
  90. colon := p.idx
  91. if p.mode&StoreComments != 0 {
  92. p.comments.Unset()
  93. }
  94. p.next() // :
  95. label := identifier.Name
  96. for _, value := range p.scope.labels {
  97. if label == value {
  98. p.error(identifier.Idx0(), "Label '%s' already exists", label)
  99. }
  100. }
  101. var labelComments []*ast.Comment
  102. if p.mode&StoreComments != 0 {
  103. labelComments = p.comments.FetchAll()
  104. }
  105. p.scope.labels = append(p.scope.labels, label) // Push the label
  106. statement := p.parseStatement()
  107. p.scope.labels = p.scope.labels[:len(p.scope.labels)-1] // Pop the label
  108. exp := &ast.LabelledStatement{
  109. Label: identifier,
  110. Colon: colon,
  111. Statement: statement,
  112. }
  113. if p.mode&StoreComments != 0 {
  114. p.comments.CommentMap.AddComments(exp, labelComments, ast.LEADING)
  115. }
  116. return exp
  117. }
  118. p.optionalSemicolon()
  119. statement := &ast.ExpressionStatement{
  120. Expression: expression,
  121. }
  122. if p.mode&StoreComments != 0 {
  123. p.comments.CommentMap.AddComments(statement, comments, ast.LEADING)
  124. }
  125. return statement
  126. }
  127. func (p *parser) parseTryStatement() ast.Statement {
  128. var tryComments []*ast.Comment
  129. if p.mode&StoreComments != 0 {
  130. tryComments = p.comments.FetchAll()
  131. }
  132. node := &ast.TryStatement{
  133. Try: p.expect(token.TRY),
  134. Body: p.parseBlockStatement(),
  135. }
  136. if p.mode&StoreComments != 0 {
  137. p.comments.CommentMap.AddComments(node, tryComments, ast.LEADING)
  138. p.comments.CommentMap.AddComments(node.Body, p.comments.FetchAll(), ast.TRAILING)
  139. }
  140. if p.token == token.CATCH {
  141. catch := p.idx
  142. if p.mode&StoreComments != 0 {
  143. p.comments.Unset()
  144. }
  145. p.next()
  146. p.expect(token.LEFT_PARENTHESIS)
  147. if p.token != token.IDENTIFIER {
  148. p.expect(token.IDENTIFIER)
  149. p.nextStatement()
  150. return &ast.BadStatement{From: catch, To: p.idx}
  151. }
  152. identifier := p.parseIdentifier()
  153. p.expect(token.RIGHT_PARENTHESIS)
  154. node.Catch = &ast.CatchStatement{
  155. Catch: catch,
  156. Parameter: identifier,
  157. Body: p.parseBlockStatement(),
  158. }
  159. if p.mode&StoreComments != 0 {
  160. p.comments.CommentMap.AddComments(node.Catch.Body, p.comments.FetchAll(), ast.TRAILING)
  161. }
  162. }
  163. if p.token == token.FINALLY {
  164. if p.mode&StoreComments != 0 {
  165. p.comments.Unset()
  166. }
  167. p.next()
  168. if p.mode&StoreComments != 0 {
  169. tryComments = p.comments.FetchAll()
  170. }
  171. node.Finally = p.parseBlockStatement()
  172. if p.mode&StoreComments != 0 {
  173. p.comments.CommentMap.AddComments(node.Finally, tryComments, ast.LEADING)
  174. }
  175. }
  176. if node.Catch == nil && node.Finally == nil {
  177. p.error(node.Try, "Missing catch or finally after try")
  178. return &ast.BadStatement{From: node.Try, To: node.Body.Idx1()}
  179. }
  180. return node
  181. }
  182. func (p *parser) parseFunctionParameterList() *ast.ParameterList {
  183. opening := p.expect(token.LEFT_PARENTHESIS)
  184. if p.mode&StoreComments != 0 {
  185. p.comments.Unset()
  186. }
  187. var list []*ast.Identifier
  188. for p.token != token.RIGHT_PARENTHESIS && p.token != token.EOF {
  189. if p.token != token.IDENTIFIER {
  190. p.expect(token.IDENTIFIER)
  191. } else {
  192. identifier := p.parseIdentifier()
  193. list = append(list, identifier)
  194. }
  195. if p.token != token.RIGHT_PARENTHESIS {
  196. if p.mode&StoreComments != 0 {
  197. p.comments.Unset()
  198. }
  199. p.expect(token.COMMA)
  200. }
  201. }
  202. closing := p.expect(token.RIGHT_PARENTHESIS)
  203. return &ast.ParameterList{
  204. Opening: opening,
  205. List: list,
  206. Closing: closing,
  207. }
  208. }
  209. func (p *parser) parseFunctionStatement() *ast.FunctionStatement {
  210. var comments []*ast.Comment
  211. if p.mode&StoreComments != 0 {
  212. comments = p.comments.FetchAll()
  213. }
  214. function := &ast.FunctionStatement{
  215. Function: p.parseFunction(true),
  216. }
  217. if p.mode&StoreComments != 0 {
  218. p.comments.CommentMap.AddComments(function, comments, ast.LEADING)
  219. }
  220. return function
  221. }
  222. func (p *parser) parseFunction(declaration bool) *ast.FunctionLiteral {
  223. node := &ast.FunctionLiteral{
  224. Function: p.expect(token.FUNCTION),
  225. }
  226. var name *ast.Identifier
  227. if p.token == token.IDENTIFIER {
  228. name = p.parseIdentifier()
  229. if declaration {
  230. p.scope.declare(&ast.FunctionDeclaration{
  231. Function: node,
  232. })
  233. }
  234. } else if declaration {
  235. // Use expect error handling
  236. p.expect(token.IDENTIFIER)
  237. }
  238. if p.mode&StoreComments != 0 {
  239. p.comments.Unset()
  240. }
  241. node.Name = name
  242. node.ParameterList = p.parseFunctionParameterList()
  243. p.parseFunctionBlock(node)
  244. node.Source = p.slice(node.Idx0(), node.Idx1())
  245. return node
  246. }
  247. func (p *parser) parseFunctionBlock(node *ast.FunctionLiteral) {
  248. p.openScope()
  249. inFunction := p.scope.inFunction
  250. p.scope.inFunction = true
  251. defer func() {
  252. p.scope.inFunction = inFunction
  253. p.closeScope()
  254. }()
  255. node.Body = p.parseBlockStatement()
  256. node.DeclarationList = p.scope.declarationList
  257. }
  258. func (p *parser) parseDebuggerStatement() ast.Statement {
  259. idx := p.expect(token.DEBUGGER)
  260. node := &ast.DebuggerStatement{
  261. Debugger: idx,
  262. }
  263. if p.mode&StoreComments != 0 {
  264. p.comments.CommentMap.AddComments(node, p.comments.FetchAll(), ast.TRAILING)
  265. }
  266. p.semicolon()
  267. return node
  268. }
  269. func (p *parser) parseReturnStatement() ast.Statement {
  270. idx := p.expect(token.RETURN)
  271. var comments []*ast.Comment
  272. if p.mode&StoreComments != 0 {
  273. comments = p.comments.FetchAll()
  274. }
  275. if !p.scope.inFunction {
  276. p.error(idx, "Illegal return statement")
  277. p.nextStatement()
  278. return &ast.BadStatement{From: idx, To: p.idx}
  279. }
  280. node := &ast.ReturnStatement{
  281. Return: idx,
  282. }
  283. if !p.implicitSemicolon && p.token != token.SEMICOLON && p.token != token.RIGHT_BRACE && p.token != token.EOF {
  284. node.Argument = p.parseExpression()
  285. }
  286. if p.mode&StoreComments != 0 {
  287. p.comments.CommentMap.AddComments(node, comments, ast.LEADING)
  288. }
  289. p.semicolon()
  290. return node
  291. }
  292. func (p *parser) parseThrowStatement() ast.Statement {
  293. var comments []*ast.Comment
  294. if p.mode&StoreComments != 0 {
  295. comments = p.comments.FetchAll()
  296. }
  297. idx := p.expect(token.THROW)
  298. if p.implicitSemicolon {
  299. if p.chr == -1 { // Hackish
  300. p.error(idx, "Unexpected end of input")
  301. } else {
  302. p.error(idx, "Illegal newline after throw")
  303. }
  304. p.nextStatement()
  305. return &ast.BadStatement{From: idx, To: p.idx}
  306. }
  307. node := &ast.ThrowStatement{
  308. Throw: idx,
  309. Argument: p.parseExpression(),
  310. }
  311. if p.mode&StoreComments != 0 {
  312. p.comments.CommentMap.AddComments(node, comments, ast.LEADING)
  313. }
  314. p.semicolon()
  315. return node
  316. }
  317. func (p *parser) parseSwitchStatement() ast.Statement {
  318. var comments []*ast.Comment
  319. if p.mode&StoreComments != 0 {
  320. comments = p.comments.FetchAll()
  321. }
  322. idx := p.expect(token.SWITCH)
  323. if p.mode&StoreComments != 0 {
  324. comments = append(comments, p.comments.FetchAll()...)
  325. }
  326. p.expect(token.LEFT_PARENTHESIS)
  327. node := &ast.SwitchStatement{
  328. Switch: idx,
  329. Discriminant: p.parseExpression(),
  330. Default: -1,
  331. }
  332. p.expect(token.RIGHT_PARENTHESIS)
  333. if p.mode&StoreComments != 0 {
  334. comments = append(comments, p.comments.FetchAll()...)
  335. }
  336. p.expect(token.LEFT_BRACE)
  337. inSwitch := p.scope.inSwitch
  338. p.scope.inSwitch = true
  339. defer func() {
  340. p.scope.inSwitch = inSwitch
  341. }()
  342. for index := 0; p.token != token.EOF; index++ {
  343. if p.token == token.RIGHT_BRACE {
  344. node.RightBrace = p.idx
  345. p.next()
  346. break
  347. }
  348. clause := p.parseCaseStatement()
  349. if clause.Test == nil {
  350. if node.Default != -1 {
  351. p.error(clause.Case, "Already saw a default in switch")
  352. }
  353. node.Default = index
  354. }
  355. node.Body = append(node.Body, clause)
  356. }
  357. if p.mode&StoreComments != 0 {
  358. p.comments.CommentMap.AddComments(node, comments, ast.LEADING)
  359. }
  360. return node
  361. }
  362. func (p *parser) parseWithStatement() ast.Statement {
  363. var comments []*ast.Comment
  364. if p.mode&StoreComments != 0 {
  365. comments = p.comments.FetchAll()
  366. }
  367. idx := p.expect(token.WITH)
  368. var withComments []*ast.Comment
  369. if p.mode&StoreComments != 0 {
  370. withComments = p.comments.FetchAll()
  371. }
  372. p.expect(token.LEFT_PARENTHESIS)
  373. node := &ast.WithStatement{
  374. With: idx,
  375. Object: p.parseExpression(),
  376. }
  377. p.expect(token.RIGHT_PARENTHESIS)
  378. if p.mode&StoreComments != 0 {
  379. p.comments.CommentMap.AddComments(node, comments, ast.LEADING)
  380. p.comments.CommentMap.AddComments(node, withComments, ast.WITH)
  381. }
  382. node.Body = p.parseStatement()
  383. return node
  384. }
  385. func (p *parser) parseCaseStatement() *ast.CaseStatement {
  386. node := &ast.CaseStatement{
  387. Case: p.idx,
  388. }
  389. var comments []*ast.Comment
  390. if p.mode&StoreComments != 0 {
  391. comments = p.comments.FetchAll()
  392. p.comments.Unset()
  393. }
  394. if p.token == token.DEFAULT {
  395. p.next()
  396. } else {
  397. p.expect(token.CASE)
  398. node.Test = p.parseExpression()
  399. }
  400. if p.mode&StoreComments != 0 {
  401. p.comments.Unset()
  402. }
  403. p.expect(token.COLON)
  404. for {
  405. if p.token == token.EOF ||
  406. p.token == token.RIGHT_BRACE ||
  407. p.token == token.CASE ||
  408. p.token == token.DEFAULT {
  409. break
  410. }
  411. consequent := p.parseStatement()
  412. node.Consequent = append(node.Consequent, consequent)
  413. }
  414. // Link the comments to the case statement
  415. if p.mode&StoreComments != 0 {
  416. p.comments.CommentMap.AddComments(node, comments, ast.LEADING)
  417. }
  418. return node
  419. }
  420. func (p *parser) parseIterationStatement() ast.Statement {
  421. inIteration := p.scope.inIteration
  422. p.scope.inIteration = true
  423. defer func() {
  424. p.scope.inIteration = inIteration
  425. }()
  426. return p.parseStatement()
  427. }
  428. func (p *parser) parseForIn(into ast.Expression) *ast.ForInStatement {
  429. // Already have consumed "<into> in"
  430. source := p.parseExpression()
  431. p.expect(token.RIGHT_PARENTHESIS)
  432. body := p.parseIterationStatement()
  433. forin := &ast.ForInStatement{
  434. Into: into,
  435. Source: source,
  436. Body: body,
  437. }
  438. return forin
  439. }
  440. func (p *parser) parseFor(initializer ast.Expression) *ast.ForStatement {
  441. // Already have consumed "<initializer> ;"
  442. var test, update ast.Expression
  443. if p.token != token.SEMICOLON {
  444. test = p.parseExpression()
  445. }
  446. if p.mode&StoreComments != 0 {
  447. p.comments.Unset()
  448. }
  449. p.expect(token.SEMICOLON)
  450. if p.token != token.RIGHT_PARENTHESIS {
  451. update = p.parseExpression()
  452. }
  453. p.expect(token.RIGHT_PARENTHESIS)
  454. body := p.parseIterationStatement()
  455. forstatement := &ast.ForStatement{
  456. Initializer: initializer,
  457. Test: test,
  458. Update: update,
  459. Body: body,
  460. }
  461. return forstatement
  462. }
  463. func (p *parser) parseForOrForInStatement() ast.Statement {
  464. var comments []*ast.Comment
  465. if p.mode&StoreComments != 0 {
  466. comments = p.comments.FetchAll()
  467. }
  468. idx := p.expect(token.FOR)
  469. var forComments []*ast.Comment
  470. if p.mode&StoreComments != 0 {
  471. forComments = p.comments.FetchAll()
  472. }
  473. p.expect(token.LEFT_PARENTHESIS)
  474. var left []ast.Expression
  475. forIn := false
  476. if p.token != token.SEMICOLON {
  477. allowIn := p.scope.allowIn
  478. p.scope.allowIn = false
  479. if p.token == token.VAR {
  480. tokenIdx := p.idx
  481. var varComments []*ast.Comment
  482. if p.mode&StoreComments != 0 {
  483. varComments = p.comments.FetchAll()
  484. p.comments.Unset()
  485. }
  486. p.next()
  487. list := p.parseVariableDeclarationList(tokenIdx)
  488. if len(list) == 1 && p.token == token.IN {
  489. if p.mode&StoreComments != 0 {
  490. p.comments.Unset()
  491. }
  492. p.next() // in
  493. forIn = true
  494. left = []ast.Expression{list[0]} // There is only one declaration
  495. } else {
  496. left = list
  497. }
  498. if p.mode&StoreComments != 0 {
  499. p.comments.CommentMap.AddComments(left[0], varComments, ast.LEADING)
  500. }
  501. } else {
  502. left = append(left, p.parseExpression())
  503. if p.token == token.IN {
  504. p.next()
  505. forIn = true
  506. }
  507. }
  508. p.scope.allowIn = allowIn
  509. }
  510. if forIn {
  511. switch left[0].(type) {
  512. case *ast.Identifier, *ast.DotExpression, *ast.BracketExpression, *ast.VariableExpression:
  513. // These are all acceptable
  514. default:
  515. p.error(idx, "Invalid left-hand side in for-in")
  516. p.nextStatement()
  517. return &ast.BadStatement{From: idx, To: p.idx}
  518. }
  519. forin := p.parseForIn(left[0])
  520. forin.For = idx
  521. if p.mode&StoreComments != 0 {
  522. p.comments.CommentMap.AddComments(forin, comments, ast.LEADING)
  523. p.comments.CommentMap.AddComments(forin, forComments, ast.FOR)
  524. }
  525. return forin
  526. }
  527. if p.mode&StoreComments != 0 {
  528. p.comments.Unset()
  529. }
  530. p.expect(token.SEMICOLON)
  531. initializer := &ast.SequenceExpression{Sequence: left}
  532. forstatement := p.parseFor(initializer)
  533. forstatement.For = idx
  534. if p.mode&StoreComments != 0 {
  535. p.comments.CommentMap.AddComments(forstatement, comments, ast.LEADING)
  536. p.comments.CommentMap.AddComments(forstatement, forComments, ast.FOR)
  537. }
  538. return forstatement
  539. }
  540. func (p *parser) parseVariableStatement() *ast.VariableStatement {
  541. var comments []*ast.Comment
  542. if p.mode&StoreComments != 0 {
  543. comments = p.comments.FetchAll()
  544. }
  545. idx := p.expect(token.VAR)
  546. list := p.parseVariableDeclarationList(idx)
  547. statement := &ast.VariableStatement{
  548. Var: idx,
  549. List: list,
  550. }
  551. if p.mode&StoreComments != 0 {
  552. p.comments.CommentMap.AddComments(statement, comments, ast.LEADING)
  553. p.comments.Unset()
  554. }
  555. p.semicolon()
  556. return statement
  557. }
  558. func (p *parser) parseDoWhileStatement() ast.Statement {
  559. inIteration := p.scope.inIteration
  560. p.scope.inIteration = true
  561. defer func() {
  562. p.scope.inIteration = inIteration
  563. }()
  564. var comments []*ast.Comment
  565. if p.mode&StoreComments != 0 {
  566. comments = p.comments.FetchAll()
  567. }
  568. idx := p.expect(token.DO)
  569. var doComments []*ast.Comment
  570. if p.mode&StoreComments != 0 {
  571. doComments = p.comments.FetchAll()
  572. }
  573. node := &ast.DoWhileStatement{Do: idx}
  574. if p.token == token.LEFT_BRACE {
  575. node.Body = p.parseBlockStatement()
  576. } else {
  577. node.Body = p.parseStatement()
  578. }
  579. p.expect(token.WHILE)
  580. var whileComments []*ast.Comment
  581. if p.mode&StoreComments != 0 {
  582. whileComments = p.comments.FetchAll()
  583. }
  584. p.expect(token.LEFT_PARENTHESIS)
  585. node.Test = p.parseExpression()
  586. node.RightParenthesis = p.expect(token.RIGHT_PARENTHESIS)
  587. p.implicitSemicolon = true
  588. p.optionalSemicolon()
  589. if p.mode&StoreComments != 0 {
  590. p.comments.CommentMap.AddComments(node, comments, ast.LEADING)
  591. p.comments.CommentMap.AddComments(node, doComments, ast.DO)
  592. p.comments.CommentMap.AddComments(node, whileComments, ast.WHILE)
  593. }
  594. return node
  595. }
  596. func (p *parser) parseWhileStatement() ast.Statement {
  597. var comments []*ast.Comment
  598. if p.mode&StoreComments != 0 {
  599. comments = p.comments.FetchAll()
  600. }
  601. idx := p.expect(token.WHILE)
  602. var whileComments []*ast.Comment
  603. if p.mode&StoreComments != 0 {
  604. whileComments = p.comments.FetchAll()
  605. }
  606. p.expect(token.LEFT_PARENTHESIS)
  607. node := &ast.WhileStatement{
  608. While: idx,
  609. Test: p.parseExpression(),
  610. }
  611. p.expect(token.RIGHT_PARENTHESIS)
  612. node.Body = p.parseIterationStatement()
  613. if p.mode&StoreComments != 0 {
  614. p.comments.CommentMap.AddComments(node, comments, ast.LEADING)
  615. p.comments.CommentMap.AddComments(node, whileComments, ast.WHILE)
  616. }
  617. return node
  618. }
  619. func (p *parser) parseIfStatement() ast.Statement {
  620. var comments []*ast.Comment
  621. if p.mode&StoreComments != 0 {
  622. comments = p.comments.FetchAll()
  623. }
  624. pos := p.expect(token.IF)
  625. var ifComments []*ast.Comment
  626. if p.mode&StoreComments != 0 {
  627. ifComments = p.comments.FetchAll()
  628. }
  629. p.expect(token.LEFT_PARENTHESIS)
  630. node := &ast.IfStatement{
  631. If: pos,
  632. Test: p.parseExpression(),
  633. }
  634. p.expect(token.RIGHT_PARENTHESIS)
  635. if p.token == token.LEFT_BRACE {
  636. node.Consequent = p.parseBlockStatement()
  637. } else {
  638. node.Consequent = p.parseStatement()
  639. }
  640. if p.token == token.ELSE {
  641. p.next()
  642. node.Alternate = p.parseStatement()
  643. }
  644. if p.mode&StoreComments != 0 {
  645. p.comments.CommentMap.AddComments(node, comments, ast.LEADING)
  646. p.comments.CommentMap.AddComments(node, ifComments, ast.IF)
  647. }
  648. return node
  649. }
  650. func (p *parser) parseSourceElement() ast.Statement {
  651. statement := p.parseStatement()
  652. return statement
  653. }
  654. func (p *parser) parseSourceElements() []ast.Statement {
  655. body := []ast.Statement(nil)
  656. for {
  657. if p.token != token.STRING {
  658. break
  659. }
  660. body = append(body, p.parseSourceElement())
  661. }
  662. for p.token != token.EOF {
  663. body = append(body, p.parseSourceElement())
  664. }
  665. return body
  666. }
  667. func (p *parser) parseProgram() *ast.Program {
  668. p.openScope()
  669. defer p.closeScope()
  670. return &ast.Program{
  671. Body: p.parseSourceElements(),
  672. DeclarationList: p.scope.declarationList,
  673. File: p.file,
  674. }
  675. }
  676. func (p *parser) parseBreakStatement() ast.Statement {
  677. var comments []*ast.Comment
  678. if p.mode&StoreComments != 0 {
  679. comments = p.comments.FetchAll()
  680. }
  681. idx := p.expect(token.BREAK)
  682. semicolon := p.implicitSemicolon
  683. if p.token == token.SEMICOLON {
  684. semicolon = true
  685. p.next()
  686. }
  687. if semicolon || p.token == token.RIGHT_BRACE {
  688. p.implicitSemicolon = false
  689. if !p.scope.inIteration && !p.scope.inSwitch {
  690. goto illegal
  691. }
  692. breakStatement := &ast.BranchStatement{
  693. Idx: idx,
  694. Token: token.BREAK,
  695. }
  696. if p.mode&StoreComments != 0 {
  697. p.comments.CommentMap.AddComments(breakStatement, comments, ast.LEADING)
  698. p.comments.CommentMap.AddComments(breakStatement, p.comments.FetchAll(), ast.TRAILING)
  699. }
  700. return breakStatement
  701. }
  702. if p.token == token.IDENTIFIER {
  703. identifier := p.parseIdentifier()
  704. if !p.scope.hasLabel(identifier.Name) {
  705. p.error(idx, "Undefined label '%s'", identifier.Name)
  706. return &ast.BadStatement{From: idx, To: identifier.Idx1()}
  707. }
  708. p.semicolon()
  709. breakStatement := &ast.BranchStatement{
  710. Idx: idx,
  711. Token: token.BREAK,
  712. Label: identifier,
  713. }
  714. if p.mode&StoreComments != 0 {
  715. p.comments.CommentMap.AddComments(breakStatement, comments, ast.LEADING)
  716. }
  717. return breakStatement
  718. }
  719. p.expect(token.IDENTIFIER)
  720. illegal:
  721. p.error(idx, "Illegal break statement")
  722. p.nextStatement()
  723. return &ast.BadStatement{From: idx, To: p.idx}
  724. }
  725. func (p *parser) parseContinueStatement() ast.Statement {
  726. idx := p.expect(token.CONTINUE)
  727. semicolon := p.implicitSemicolon
  728. if p.token == token.SEMICOLON {
  729. semicolon = true
  730. p.next()
  731. }
  732. if semicolon || p.token == token.RIGHT_BRACE {
  733. p.implicitSemicolon = false
  734. if !p.scope.inIteration {
  735. goto illegal
  736. }
  737. return &ast.BranchStatement{
  738. Idx: idx,
  739. Token: token.CONTINUE,
  740. }
  741. }
  742. if p.token == token.IDENTIFIER {
  743. identifier := p.parseIdentifier()
  744. if !p.scope.hasLabel(identifier.Name) {
  745. p.error(idx, "Undefined label '%s'", identifier.Name)
  746. return &ast.BadStatement{From: idx, To: identifier.Idx1()}
  747. }
  748. if !p.scope.inIteration {
  749. goto illegal
  750. }
  751. p.semicolon()
  752. return &ast.BranchStatement{
  753. Idx: idx,
  754. Token: token.CONTINUE,
  755. Label: identifier,
  756. }
  757. }
  758. p.expect(token.IDENTIFIER)
  759. illegal:
  760. p.error(idx, "Illegal continue statement")
  761. p.nextStatement()
  762. return &ast.BadStatement{From: idx, To: p.idx}
  763. }
  764. // Find the next statement after an error (recover).
  765. func (p *parser) nextStatement() {
  766. for {
  767. switch p.token {
  768. case token.BREAK, token.CONTINUE,
  769. token.FOR, token.IF, token.RETURN, token.SWITCH,
  770. token.VAR, token.DO, token.TRY, token.WITH,
  771. token.WHILE, token.THROW, token.CATCH, token.FINALLY:
  772. // Return only if parser made some progress since last
  773. // sync or if it has not reached 10 next calls without
  774. // progress. Otherwise consume at least one token to
  775. // avoid an endless parser loop
  776. if p.idx == p.recover.idx && p.recover.count < 10 {
  777. p.recover.count++
  778. return
  779. }
  780. if p.idx > p.recover.idx {
  781. p.recover.idx = p.idx
  782. p.recover.count = 0
  783. return
  784. }
  785. // Reaching here indicates a parser bug, likely an
  786. // incorrect token list in this function, but it only
  787. // leads to skipping of possibly correct code if a
  788. // previous error is present, and thus is preferred
  789. // over a non-terminating parse.
  790. case token.EOF:
  791. return
  792. }
  793. p.next()
  794. }
  795. }