Debian Codegraph @

parser/statement.go

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