github.com/nuvolaris/goja@v0.0.0-20230825100449-967811910c6d/parser/expression.go (about) 1 package parser 2 3 import ( 4 "strings" 5 6 "github.com/nuvolaris/goja/ast" 7 "github.com/nuvolaris/goja/file" 8 "github.com/nuvolaris/goja/token" 9 "github.com/nuvolaris/goja/unistring" 10 ) 11 12 func (self *_parser) parseIdentifier() *ast.Identifier { 13 literal := self.parsedLiteral 14 idx := self.idx 15 self.next() 16 return &ast.Identifier{ 17 Name: literal, 18 Idx: idx, 19 } 20 } 21 22 func (self *_parser) parsePrimaryExpression() ast.Expression { 23 literal, parsedLiteral := self.literal, self.parsedLiteral 24 idx := self.idx 25 switch self.token { 26 case token.IDENTIFIER: 27 self.next() 28 return &ast.Identifier{ 29 Name: parsedLiteral, 30 Idx: idx, 31 } 32 case token.NULL: 33 self.next() 34 return &ast.NullLiteral{ 35 Idx: idx, 36 Literal: literal, 37 } 38 case token.BOOLEAN: 39 self.next() 40 value := false 41 switch parsedLiteral { 42 case "true": 43 value = true 44 case "false": 45 value = false 46 default: 47 self.error(idx, "Illegal boolean literal") 48 } 49 return &ast.BooleanLiteral{ 50 Idx: idx, 51 Literal: literal, 52 Value: value, 53 } 54 case token.STRING: 55 self.next() 56 return &ast.StringLiteral{ 57 Idx: idx, 58 Literal: literal, 59 Value: parsedLiteral, 60 } 61 case token.NUMBER: 62 self.next() 63 value, err := parseNumberLiteral(literal) 64 if err != nil { 65 self.error(idx, err.Error()) 66 value = 0 67 } 68 return &ast.NumberLiteral{ 69 Idx: idx, 70 Literal: literal, 71 Value: value, 72 } 73 case token.SLASH, token.QUOTIENT_ASSIGN: 74 return self.parseRegExpLiteral() 75 case token.LEFT_BRACE: 76 return self.parseObjectLiteral() 77 case token.LEFT_BRACKET: 78 return self.parseArrayLiteral() 79 case token.LEFT_PARENTHESIS: 80 return self.parseParenthesisedExpression() 81 case token.BACKTICK: 82 return self.parseTemplateLiteral(false) 83 case token.THIS: 84 self.next() 85 return &ast.ThisExpression{ 86 Idx: idx, 87 } 88 case token.SUPER: 89 return self.parseSuperProperty() 90 case token.ASYNC: 91 if f := self.parseMaybeAsyncFunction(false); f != nil { 92 return f 93 } 94 case token.FUNCTION: 95 return self.parseFunction(false, false, idx) 96 case token.CLASS: 97 return self.parseClass(false) 98 } 99 100 if self.isBindingId(self.token) { 101 self.next() 102 return &ast.Identifier{ 103 Name: parsedLiteral, 104 Idx: idx, 105 } 106 } 107 108 self.errorUnexpectedToken(self.token) 109 self.nextStatement() 110 return &ast.BadExpression{From: idx, To: self.idx} 111 } 112 113 func (self *_parser) parseSuperProperty() ast.Expression { 114 idx := self.idx 115 self.next() 116 switch self.token { 117 case token.PERIOD: 118 self.next() 119 if !token.IsId(self.token) { 120 self.expect(token.IDENTIFIER) 121 self.nextStatement() 122 return &ast.BadExpression{From: idx, To: self.idx} 123 } 124 idIdx := self.idx 125 parsedLiteral := self.parsedLiteral 126 self.next() 127 return &ast.DotExpression{ 128 Left: &ast.SuperExpression{ 129 Idx: idx, 130 }, 131 Identifier: ast.Identifier{ 132 Name: parsedLiteral, 133 Idx: idIdx, 134 }, 135 } 136 case token.LEFT_BRACKET: 137 return self.parseBracketMember(&ast.SuperExpression{ 138 Idx: idx, 139 }) 140 case token.LEFT_PARENTHESIS: 141 return self.parseCallExpression(&ast.SuperExpression{ 142 Idx: idx, 143 }) 144 default: 145 self.error(idx, "'super' keyword unexpected here") 146 self.nextStatement() 147 return &ast.BadExpression{From: idx, To: self.idx} 148 } 149 } 150 151 func (self *_parser) reinterpretSequenceAsArrowFuncParams(list []ast.Expression) *ast.ParameterList { 152 firstRestIdx := -1 153 params := make([]*ast.Binding, 0, len(list)) 154 for i, item := range list { 155 if _, ok := item.(*ast.SpreadElement); ok { 156 if firstRestIdx == -1 { 157 firstRestIdx = i 158 continue 159 } 160 } 161 if firstRestIdx != -1 { 162 self.error(list[firstRestIdx].Idx0(), "Rest parameter must be last formal parameter") 163 return &ast.ParameterList{} 164 } 165 params = append(params, self.reinterpretAsBinding(item)) 166 } 167 var rest ast.Expression 168 if firstRestIdx != -1 { 169 rest = self.reinterpretAsBindingRestElement(list[firstRestIdx]) 170 } 171 return &ast.ParameterList{ 172 List: params, 173 Rest: rest, 174 } 175 } 176 177 func (self *_parser) parseParenthesisedExpression() ast.Expression { 178 opening := self.idx 179 self.expect(token.LEFT_PARENTHESIS) 180 var list []ast.Expression 181 if self.token != token.RIGHT_PARENTHESIS { 182 for { 183 if self.token == token.ELLIPSIS { 184 start := self.idx 185 self.errorUnexpectedToken(token.ELLIPSIS) 186 self.next() 187 expr := self.parseAssignmentExpression() 188 list = append(list, &ast.BadExpression{ 189 From: start, 190 To: expr.Idx1(), 191 }) 192 } else { 193 list = append(list, self.parseAssignmentExpression()) 194 } 195 if self.token != token.COMMA { 196 break 197 } 198 self.next() 199 if self.token == token.RIGHT_PARENTHESIS { 200 self.errorUnexpectedToken(token.RIGHT_PARENTHESIS) 201 break 202 } 203 } 204 } 205 self.expect(token.RIGHT_PARENTHESIS) 206 if len(list) == 1 && len(self.errors) == 0 { 207 return list[0] 208 } 209 if len(list) == 0 { 210 self.errorUnexpectedToken(token.RIGHT_PARENTHESIS) 211 return &ast.BadExpression{ 212 From: opening, 213 To: self.idx, 214 } 215 } 216 return &ast.SequenceExpression{ 217 Sequence: list, 218 } 219 } 220 221 func (self *_parser) parseRegExpLiteral() *ast.RegExpLiteral { 222 223 offset := self.chrOffset - 1 // Opening slash already gotten 224 if self.token == token.QUOTIENT_ASSIGN { 225 offset -= 1 // = 226 } 227 idx := self.idxOf(offset) 228 229 pattern, _, err := self.scanString(offset, false) 230 endOffset := self.chrOffset 231 232 if err == "" { 233 pattern = pattern[1 : len(pattern)-1] 234 } 235 236 flags := "" 237 if !isLineTerminator(self.chr) && !isLineWhiteSpace(self.chr) { 238 self.next() 239 240 if self.token == token.IDENTIFIER { // gim 241 242 flags = self.literal 243 self.next() 244 endOffset = self.chrOffset - 1 245 } 246 } else { 247 self.next() 248 } 249 250 literal := self.str[offset:endOffset] 251 252 return &ast.RegExpLiteral{ 253 Idx: idx, 254 Literal: literal, 255 Pattern: pattern, 256 Flags: flags, 257 } 258 } 259 260 func (self *_parser) isBindingId(tok token.Token) bool { 261 if tok == token.IDENTIFIER { 262 return true 263 } 264 265 if tok == token.AWAIT { 266 return !self.scope.allowAwait 267 } 268 if tok == token.YIELD { 269 return !self.scope.allowYield 270 } 271 272 if token.IsUnreservedWord(tok) { 273 return true 274 } 275 return false 276 } 277 278 func (self *_parser) tokenToBindingId() { 279 if self.isBindingId(self.token) { 280 self.token = token.IDENTIFIER 281 } 282 } 283 284 func (self *_parser) parseBindingTarget() (target ast.BindingTarget) { 285 self.tokenToBindingId() 286 switch self.token { 287 case token.IDENTIFIER: 288 target = &ast.Identifier{ 289 Name: self.parsedLiteral, 290 Idx: self.idx, 291 } 292 self.next() 293 case token.LEFT_BRACKET: 294 target = self.parseArrayBindingPattern() 295 case token.LEFT_BRACE: 296 target = self.parseObjectBindingPattern() 297 default: 298 idx := self.expect(token.IDENTIFIER) 299 self.nextStatement() 300 target = &ast.BadExpression{From: idx, To: self.idx} 301 } 302 303 return 304 } 305 306 func (self *_parser) parseVariableDeclaration(declarationList *[]*ast.Binding) *ast.Binding { 307 node := &ast.Binding{ 308 Target: self.parseBindingTarget(), 309 } 310 311 if declarationList != nil { 312 *declarationList = append(*declarationList, node) 313 } 314 315 if self.token == token.ASSIGN { 316 self.next() 317 node.Initializer = self.parseAssignmentExpression() 318 } 319 320 return node 321 } 322 323 func (self *_parser) parseVariableDeclarationList() (declarationList []*ast.Binding) { 324 for { 325 self.parseVariableDeclaration(&declarationList) 326 if self.token != token.COMMA { 327 break 328 } 329 self.next() 330 } 331 return 332 } 333 334 func (self *_parser) parseVarDeclarationList(var_ file.Idx) []*ast.Binding { 335 declarationList := self.parseVariableDeclarationList() 336 337 self.scope.declare(&ast.VariableDeclaration{ 338 Var: var_, 339 List: declarationList, 340 }) 341 342 return declarationList 343 } 344 345 func (self *_parser) parseObjectPropertyKey() (string, unistring.String, ast.Expression, token.Token) { 346 if self.token == token.LEFT_BRACKET { 347 self.next() 348 expr := self.parseAssignmentExpression() 349 self.expect(token.RIGHT_BRACKET) 350 return "", "", expr, token.ILLEGAL 351 } 352 idx, tkn, literal, parsedLiteral := self.idx, self.token, self.literal, self.parsedLiteral 353 var value ast.Expression 354 self.next() 355 switch tkn { 356 case token.IDENTIFIER, token.STRING, token.KEYWORD, token.ESCAPED_RESERVED_WORD: 357 value = &ast.StringLiteral{ 358 Idx: idx, 359 Literal: literal, 360 Value: parsedLiteral, 361 } 362 case token.NUMBER: 363 num, err := parseNumberLiteral(literal) 364 if err != nil { 365 self.error(idx, err.Error()) 366 } else { 367 value = &ast.NumberLiteral{ 368 Idx: idx, 369 Literal: literal, 370 Value: num, 371 } 372 } 373 case token.PRIVATE_IDENTIFIER: 374 value = &ast.PrivateIdentifier{ 375 Identifier: ast.Identifier{ 376 Idx: idx, 377 Name: parsedLiteral, 378 }, 379 } 380 default: 381 // null, false, class, etc. 382 if token.IsId(tkn) { 383 value = &ast.StringLiteral{ 384 Idx: idx, 385 Literal: literal, 386 Value: unistring.String(literal), 387 } 388 } else { 389 self.errorUnexpectedToken(tkn) 390 } 391 } 392 return literal, parsedLiteral, value, tkn 393 } 394 395 func (self *_parser) parseObjectProperty() ast.Property { 396 if self.token == token.ELLIPSIS { 397 self.next() 398 return &ast.SpreadElement{ 399 Expression: self.parseAssignmentExpression(), 400 } 401 } 402 keyStartIdx := self.idx 403 generator := false 404 if self.token == token.MULTIPLY { 405 generator = true 406 self.next() 407 } 408 literal, parsedLiteral, value, tkn := self.parseObjectPropertyKey() 409 if value == nil { 410 return nil 411 } 412 if token.IsId(tkn) || tkn == token.STRING || tkn == token.NUMBER || tkn == token.ILLEGAL { 413 if generator { 414 return &ast.PropertyKeyed{ 415 Key: value, 416 Kind: ast.PropertyKindMethod, 417 Value: self.parseMethodDefinition(keyStartIdx, ast.PropertyKindMethod, true, false), 418 Computed: tkn == token.ILLEGAL, 419 } 420 } 421 switch { 422 case self.token == token.LEFT_PARENTHESIS: 423 return &ast.PropertyKeyed{ 424 Key: value, 425 Kind: ast.PropertyKindMethod, 426 Value: self.parseMethodDefinition(keyStartIdx, ast.PropertyKindMethod, false, false), 427 Computed: tkn == token.ILLEGAL, 428 } 429 case self.token == token.COMMA || self.token == token.RIGHT_BRACE || self.token == token.ASSIGN: // shorthand property 430 if self.isBindingId(tkn) { 431 var initializer ast.Expression 432 if self.token == token.ASSIGN { 433 // allow the initializer syntax here in case the object literal 434 // needs to be reinterpreted as an assignment pattern, enforce later if it doesn't. 435 self.next() 436 initializer = self.parseAssignmentExpression() 437 } 438 return &ast.PropertyShort{ 439 Name: ast.Identifier{ 440 Name: parsedLiteral, 441 Idx: value.Idx0(), 442 }, 443 Initializer: initializer, 444 } 445 } else { 446 self.errorUnexpectedToken(self.token) 447 } 448 case (literal == "get" || literal == "set" || tkn == token.ASYNC) && self.token != token.COLON: 449 _, _, keyValue, tkn1 := self.parseObjectPropertyKey() 450 if keyValue == nil { 451 return nil 452 } 453 454 var kind ast.PropertyKind 455 var async bool 456 if tkn == token.ASYNC { 457 async = true 458 kind = ast.PropertyKindMethod 459 } else if literal == "get" { 460 kind = ast.PropertyKindGet 461 } else { 462 kind = ast.PropertyKindSet 463 } 464 465 return &ast.PropertyKeyed{ 466 Key: keyValue, 467 Kind: kind, 468 Value: self.parseMethodDefinition(keyStartIdx, kind, false, async), 469 Computed: tkn1 == token.ILLEGAL, 470 } 471 } 472 } 473 474 self.expect(token.COLON) 475 return &ast.PropertyKeyed{ 476 Key: value, 477 Kind: ast.PropertyKindValue, 478 Value: self.parseAssignmentExpression(), 479 Computed: tkn == token.ILLEGAL, 480 } 481 } 482 483 func (self *_parser) parseMethodDefinition(keyStartIdx file.Idx, kind ast.PropertyKind, generator, async bool) *ast.FunctionLiteral { 484 idx1 := self.idx 485 if generator != self.scope.allowYield { 486 self.scope.allowYield = generator 487 defer func() { 488 self.scope.allowYield = !generator 489 }() 490 } 491 if async != self.scope.allowAwait { 492 self.scope.allowAwait = async 493 defer func() { 494 self.scope.allowAwait = !async 495 }() 496 } 497 parameterList := self.parseFunctionParameterList() 498 switch kind { 499 case ast.PropertyKindGet: 500 if len(parameterList.List) > 0 || parameterList.Rest != nil { 501 self.error(idx1, "Getter must not have any formal parameters.") 502 } 503 case ast.PropertyKindSet: 504 if len(parameterList.List) != 1 || parameterList.Rest != nil { 505 self.error(idx1, "Setter must have exactly one formal parameter.") 506 } 507 } 508 node := &ast.FunctionLiteral{ 509 Function: keyStartIdx, 510 ParameterList: parameterList, 511 Generator: generator, 512 Async: async, 513 } 514 node.Body, node.DeclarationList = self.parseFunctionBlock(async, async, generator) 515 node.Source = self.slice(keyStartIdx, node.Body.Idx1()) 516 return node 517 } 518 519 func (self *_parser) parseObjectLiteral() *ast.ObjectLiteral { 520 var value []ast.Property 521 idx0 := self.expect(token.LEFT_BRACE) 522 for self.token != token.RIGHT_BRACE && self.token != token.EOF { 523 property := self.parseObjectProperty() 524 if property != nil { 525 value = append(value, property) 526 } 527 if self.token != token.RIGHT_BRACE { 528 self.expect(token.COMMA) 529 } else { 530 break 531 } 532 } 533 idx1 := self.expect(token.RIGHT_BRACE) 534 535 return &ast.ObjectLiteral{ 536 LeftBrace: idx0, 537 RightBrace: idx1, 538 Value: value, 539 } 540 } 541 542 func (self *_parser) parseArrayLiteral() *ast.ArrayLiteral { 543 544 idx0 := self.expect(token.LEFT_BRACKET) 545 var value []ast.Expression 546 for self.token != token.RIGHT_BRACKET && self.token != token.EOF { 547 if self.token == token.COMMA { 548 self.next() 549 value = append(value, nil) 550 continue 551 } 552 if self.token == token.ELLIPSIS { 553 self.next() 554 value = append(value, &ast.SpreadElement{ 555 Expression: self.parseAssignmentExpression(), 556 }) 557 } else { 558 value = append(value, self.parseAssignmentExpression()) 559 } 560 if self.token != token.RIGHT_BRACKET { 561 self.expect(token.COMMA) 562 } 563 } 564 idx1 := self.expect(token.RIGHT_BRACKET) 565 566 return &ast.ArrayLiteral{ 567 LeftBracket: idx0, 568 RightBracket: idx1, 569 Value: value, 570 } 571 } 572 573 func (self *_parser) parseTemplateLiteral(tagged bool) *ast.TemplateLiteral { 574 res := &ast.TemplateLiteral{ 575 OpenQuote: self.idx, 576 } 577 for { 578 start := self.offset 579 literal, parsed, finished, parseErr, err := self.parseTemplateCharacters() 580 if err != "" { 581 self.error(self.offset, err) 582 } 583 res.Elements = append(res.Elements, &ast.TemplateElement{ 584 Idx: self.idxOf(start), 585 Literal: literal, 586 Parsed: parsed, 587 Valid: parseErr == "", 588 }) 589 if !tagged && parseErr != "" { 590 self.error(self.offset, parseErr) 591 } 592 end := self.chrOffset - 1 593 self.next() 594 if finished { 595 res.CloseQuote = self.idxOf(end) 596 break 597 } 598 expr := self.parseExpression() 599 res.Expressions = append(res.Expressions, expr) 600 if self.token != token.RIGHT_BRACE { 601 self.errorUnexpectedToken(self.token) 602 } 603 } 604 return res 605 } 606 607 func (self *_parser) parseTaggedTemplateLiteral(tag ast.Expression) *ast.TemplateLiteral { 608 l := self.parseTemplateLiteral(true) 609 l.Tag = tag 610 return l 611 } 612 613 func (self *_parser) parseArgumentList() (argumentList []ast.Expression, idx0, idx1 file.Idx) { 614 idx0 = self.expect(token.LEFT_PARENTHESIS) 615 for self.token != token.RIGHT_PARENTHESIS { 616 var item ast.Expression 617 if self.token == token.ELLIPSIS { 618 self.next() 619 item = &ast.SpreadElement{ 620 Expression: self.parseAssignmentExpression(), 621 } 622 } else { 623 item = self.parseAssignmentExpression() 624 } 625 argumentList = append(argumentList, item) 626 if self.token != token.COMMA { 627 break 628 } 629 self.next() 630 } 631 idx1 = self.expect(token.RIGHT_PARENTHESIS) 632 return 633 } 634 635 func (self *_parser) parseCallExpression(left ast.Expression) ast.Expression { 636 argumentList, idx0, idx1 := self.parseArgumentList() 637 return &ast.CallExpression{ 638 Callee: left, 639 LeftParenthesis: idx0, 640 ArgumentList: argumentList, 641 RightParenthesis: idx1, 642 } 643 } 644 645 func (self *_parser) parseDotMember(left ast.Expression) ast.Expression { 646 period := self.idx 647 self.next() 648 649 literal := self.parsedLiteral 650 idx := self.idx 651 652 if self.token == token.PRIVATE_IDENTIFIER { 653 self.next() 654 return &ast.PrivateDotExpression{ 655 Left: left, 656 Identifier: ast.PrivateIdentifier{ 657 Identifier: ast.Identifier{ 658 Idx: idx, 659 Name: literal, 660 }, 661 }, 662 } 663 } 664 665 if !token.IsId(self.token) { 666 self.expect(token.IDENTIFIER) 667 self.nextStatement() 668 return &ast.BadExpression{From: period, To: self.idx} 669 } 670 671 self.next() 672 673 return &ast.DotExpression{ 674 Left: left, 675 Identifier: ast.Identifier{ 676 Idx: idx, 677 Name: literal, 678 }, 679 } 680 } 681 682 func (self *_parser) parseBracketMember(left ast.Expression) ast.Expression { 683 idx0 := self.expect(token.LEFT_BRACKET) 684 member := self.parseExpression() 685 idx1 := self.expect(token.RIGHT_BRACKET) 686 return &ast.BracketExpression{ 687 LeftBracket: idx0, 688 Left: left, 689 Member: member, 690 RightBracket: idx1, 691 } 692 } 693 694 func (self *_parser) parseNewExpression() ast.Expression { 695 idx := self.expect(token.NEW) 696 if self.token == token.PERIOD { 697 self.next() 698 if self.literal == "target" { 699 return &ast.MetaProperty{ 700 Meta: &ast.Identifier{ 701 Name: unistring.String(token.NEW.String()), 702 Idx: idx, 703 }, 704 Property: self.parseIdentifier(), 705 } 706 } 707 self.errorUnexpectedToken(token.IDENTIFIER) 708 } 709 callee := self.parseLeftHandSideExpression() 710 if bad, ok := callee.(*ast.BadExpression); ok { 711 bad.From = idx 712 return bad 713 } 714 node := &ast.NewExpression{ 715 New: idx, 716 Callee: callee, 717 } 718 if self.token == token.LEFT_PARENTHESIS { 719 argumentList, idx0, idx1 := self.parseArgumentList() 720 node.ArgumentList = argumentList 721 node.LeftParenthesis = idx0 722 node.RightParenthesis = idx1 723 } 724 return node 725 } 726 727 func (self *_parser) parseLeftHandSideExpression() ast.Expression { 728 729 var left ast.Expression 730 if self.token == token.NEW { 731 left = self.parseNewExpression() 732 } else { 733 left = self.parsePrimaryExpression() 734 } 735 L: 736 for { 737 switch self.token { 738 case token.PERIOD: 739 left = self.parseDotMember(left) 740 case token.LEFT_BRACKET: 741 left = self.parseBracketMember(left) 742 case token.BACKTICK: 743 left = self.parseTaggedTemplateLiteral(left) 744 default: 745 break L 746 } 747 } 748 749 return left 750 } 751 752 func (self *_parser) parseLeftHandSideExpressionAllowCall() ast.Expression { 753 754 allowIn := self.scope.allowIn 755 self.scope.allowIn = true 756 defer func() { 757 self.scope.allowIn = allowIn 758 }() 759 760 var left ast.Expression 761 start := self.idx 762 if self.token == token.NEW { 763 left = self.parseNewExpression() 764 } else { 765 left = self.parsePrimaryExpression() 766 } 767 768 optionalChain := false 769 L: 770 for { 771 switch self.token { 772 case token.PERIOD: 773 left = self.parseDotMember(left) 774 case token.LEFT_BRACKET: 775 left = self.parseBracketMember(left) 776 case token.LEFT_PARENTHESIS: 777 left = self.parseCallExpression(left) 778 case token.BACKTICK: 779 if optionalChain { 780 self.error(self.idx, "Invalid template literal on optional chain") 781 self.nextStatement() 782 return &ast.BadExpression{From: start, To: self.idx} 783 } 784 left = self.parseTaggedTemplateLiteral(left) 785 case token.QUESTION_DOT: 786 optionalChain = true 787 left = &ast.Optional{Expression: left} 788 789 switch self.peek() { 790 case token.LEFT_BRACKET, token.LEFT_PARENTHESIS, token.BACKTICK: 791 self.next() 792 default: 793 left = self.parseDotMember(left) 794 } 795 default: 796 break L 797 } 798 } 799 800 if optionalChain { 801 left = &ast.OptionalChain{Expression: left} 802 } 803 return left 804 } 805 806 func (self *_parser) parseUpdateExpression() ast.Expression { 807 switch self.token { 808 case token.INCREMENT, token.DECREMENT: 809 tkn := self.token 810 idx := self.idx 811 self.next() 812 operand := self.parseUnaryExpression() 813 switch operand.(type) { 814 case *ast.Identifier, *ast.DotExpression, *ast.PrivateDotExpression, *ast.BracketExpression: 815 default: 816 self.error(idx, "Invalid left-hand side in assignment") 817 self.nextStatement() 818 return &ast.BadExpression{From: idx, To: self.idx} 819 } 820 return &ast.UnaryExpression{ 821 Operator: tkn, 822 Idx: idx, 823 Operand: operand, 824 } 825 default: 826 operand := self.parseLeftHandSideExpressionAllowCall() 827 if self.token == token.INCREMENT || self.token == token.DECREMENT { 828 // Make sure there is no line terminator here 829 if self.implicitSemicolon { 830 return operand 831 } 832 tkn := self.token 833 idx := self.idx 834 self.next() 835 switch operand.(type) { 836 case *ast.Identifier, *ast.DotExpression, *ast.PrivateDotExpression, *ast.BracketExpression: 837 default: 838 self.error(idx, "Invalid left-hand side in assignment") 839 self.nextStatement() 840 return &ast.BadExpression{From: idx, To: self.idx} 841 } 842 return &ast.UnaryExpression{ 843 Operator: tkn, 844 Idx: idx, 845 Operand: operand, 846 Postfix: true, 847 } 848 } 849 return operand 850 } 851 } 852 853 func (self *_parser) parseUnaryExpression() ast.Expression { 854 855 switch self.token { 856 case token.PLUS, token.MINUS, token.NOT, token.BITWISE_NOT: 857 fallthrough 858 case token.DELETE, token.VOID, token.TYPEOF: 859 tkn := self.token 860 idx := self.idx 861 self.next() 862 return &ast.UnaryExpression{ 863 Operator: tkn, 864 Idx: idx, 865 Operand: self.parseUnaryExpression(), 866 } 867 case token.AWAIT: 868 if self.scope.allowAwait { 869 idx := self.idx 870 self.next() 871 if !self.scope.inAsync { 872 self.errorUnexpectedToken(token.AWAIT) 873 return &ast.BadExpression{ 874 From: idx, 875 To: self.idx, 876 } 877 } 878 if self.scope.inFuncParams { 879 self.error(idx, "Illegal await-expression in formal parameters of async function") 880 } 881 return &ast.AwaitExpression{ 882 Await: idx, 883 Argument: self.parseUnaryExpression(), 884 } 885 } 886 } 887 888 return self.parseUpdateExpression() 889 } 890 891 func (self *_parser) parseExponentiationExpression() ast.Expression { 892 parenthesis := self.token == token.LEFT_PARENTHESIS 893 894 left := self.parseUnaryExpression() 895 896 if self.token == token.EXPONENT { 897 if !parenthesis { 898 if u, isUnary := left.(*ast.UnaryExpression); isUnary && u.Operator != token.INCREMENT && u.Operator != token.DECREMENT { 899 self.error(self.idx, "Unary operator used immediately before exponentiation expression. Parenthesis must be used to disambiguate operator precedence") 900 } 901 } 902 for { 903 self.next() 904 left = &ast.BinaryExpression{ 905 Operator: token.EXPONENT, 906 Left: left, 907 Right: self.parseExponentiationExpression(), 908 } 909 if self.token != token.EXPONENT { 910 break 911 } 912 } 913 } 914 915 return left 916 } 917 918 func (self *_parser) parseMultiplicativeExpression() ast.Expression { 919 left := self.parseExponentiationExpression() 920 921 for self.token == token.MULTIPLY || self.token == token.SLASH || 922 self.token == token.REMAINDER { 923 tkn := self.token 924 self.next() 925 left = &ast.BinaryExpression{ 926 Operator: tkn, 927 Left: left, 928 Right: self.parseExponentiationExpression(), 929 } 930 } 931 932 return left 933 } 934 935 func (self *_parser) parseAdditiveExpression() ast.Expression { 936 left := self.parseMultiplicativeExpression() 937 938 for self.token == token.PLUS || self.token == token.MINUS { 939 tkn := self.token 940 self.next() 941 left = &ast.BinaryExpression{ 942 Operator: tkn, 943 Left: left, 944 Right: self.parseMultiplicativeExpression(), 945 } 946 } 947 948 return left 949 } 950 951 func (self *_parser) parseShiftExpression() ast.Expression { 952 left := self.parseAdditiveExpression() 953 954 for self.token == token.SHIFT_LEFT || self.token == token.SHIFT_RIGHT || 955 self.token == token.UNSIGNED_SHIFT_RIGHT { 956 tkn := self.token 957 self.next() 958 left = &ast.BinaryExpression{ 959 Operator: tkn, 960 Left: left, 961 Right: self.parseAdditiveExpression(), 962 } 963 } 964 965 return left 966 } 967 968 func (self *_parser) parseRelationalExpression() ast.Expression { 969 if self.scope.allowIn && self.token == token.PRIVATE_IDENTIFIER { 970 left := &ast.PrivateIdentifier{ 971 Identifier: ast.Identifier{ 972 Idx: self.idx, 973 Name: self.parsedLiteral, 974 }, 975 } 976 self.next() 977 if self.token == token.IN { 978 self.next() 979 return &ast.BinaryExpression{ 980 Operator: self.token, 981 Left: left, 982 Right: self.parseShiftExpression(), 983 } 984 } 985 return left 986 } 987 left := self.parseShiftExpression() 988 989 allowIn := self.scope.allowIn 990 self.scope.allowIn = true 991 defer func() { 992 self.scope.allowIn = allowIn 993 }() 994 995 switch self.token { 996 case token.LESS, token.LESS_OR_EQUAL, token.GREATER, token.GREATER_OR_EQUAL: 997 tkn := self.token 998 self.next() 999 return &ast.BinaryExpression{ 1000 Operator: tkn, 1001 Left: left, 1002 Right: self.parseRelationalExpression(), 1003 Comparison: true, 1004 } 1005 case token.INSTANCEOF: 1006 tkn := self.token 1007 self.next() 1008 return &ast.BinaryExpression{ 1009 Operator: tkn, 1010 Left: left, 1011 Right: self.parseRelationalExpression(), 1012 } 1013 case token.IN: 1014 if !allowIn { 1015 return left 1016 } 1017 tkn := self.token 1018 self.next() 1019 return &ast.BinaryExpression{ 1020 Operator: tkn, 1021 Left: left, 1022 Right: self.parseRelationalExpression(), 1023 } 1024 } 1025 1026 return left 1027 } 1028 1029 func (self *_parser) parseEqualityExpression() ast.Expression { 1030 left := self.parseRelationalExpression() 1031 1032 for self.token == token.EQUAL || self.token == token.NOT_EQUAL || 1033 self.token == token.STRICT_EQUAL || self.token == token.STRICT_NOT_EQUAL { 1034 tkn := self.token 1035 self.next() 1036 left = &ast.BinaryExpression{ 1037 Operator: tkn, 1038 Left: left, 1039 Right: self.parseRelationalExpression(), 1040 Comparison: true, 1041 } 1042 } 1043 1044 return left 1045 } 1046 1047 func (self *_parser) parseBitwiseAndExpression() ast.Expression { 1048 left := self.parseEqualityExpression() 1049 1050 for self.token == token.AND { 1051 tkn := self.token 1052 self.next() 1053 left = &ast.BinaryExpression{ 1054 Operator: tkn, 1055 Left: left, 1056 Right: self.parseEqualityExpression(), 1057 } 1058 } 1059 1060 return left 1061 } 1062 1063 func (self *_parser) parseBitwiseExclusiveOrExpression() ast.Expression { 1064 left := self.parseBitwiseAndExpression() 1065 1066 for self.token == token.EXCLUSIVE_OR { 1067 tkn := self.token 1068 self.next() 1069 left = &ast.BinaryExpression{ 1070 Operator: tkn, 1071 Left: left, 1072 Right: self.parseBitwiseAndExpression(), 1073 } 1074 } 1075 1076 return left 1077 } 1078 1079 func (self *_parser) parseBitwiseOrExpression() ast.Expression { 1080 left := self.parseBitwiseExclusiveOrExpression() 1081 1082 for self.token == token.OR { 1083 tkn := self.token 1084 self.next() 1085 left = &ast.BinaryExpression{ 1086 Operator: tkn, 1087 Left: left, 1088 Right: self.parseBitwiseExclusiveOrExpression(), 1089 } 1090 } 1091 1092 return left 1093 } 1094 1095 func (self *_parser) parseLogicalAndExpression() ast.Expression { 1096 left := self.parseBitwiseOrExpression() 1097 1098 for self.token == token.LOGICAL_AND { 1099 tkn := self.token 1100 self.next() 1101 left = &ast.BinaryExpression{ 1102 Operator: tkn, 1103 Left: left, 1104 Right: self.parseBitwiseOrExpression(), 1105 } 1106 } 1107 1108 return left 1109 } 1110 1111 func isLogicalAndExpr(expr ast.Expression) bool { 1112 if bexp, ok := expr.(*ast.BinaryExpression); ok && bexp.Operator == token.LOGICAL_AND { 1113 return true 1114 } 1115 return false 1116 } 1117 1118 func (self *_parser) parseLogicalOrExpression() ast.Expression { 1119 var idx file.Idx 1120 parenthesis := self.token == token.LEFT_PARENTHESIS 1121 left := self.parseLogicalAndExpression() 1122 1123 if self.token == token.LOGICAL_OR || !parenthesis && isLogicalAndExpr(left) { 1124 for { 1125 switch self.token { 1126 case token.LOGICAL_OR: 1127 self.next() 1128 left = &ast.BinaryExpression{ 1129 Operator: token.LOGICAL_OR, 1130 Left: left, 1131 Right: self.parseLogicalAndExpression(), 1132 } 1133 case token.COALESCE: 1134 idx = self.idx 1135 goto mixed 1136 default: 1137 return left 1138 } 1139 } 1140 } else { 1141 for { 1142 switch self.token { 1143 case token.COALESCE: 1144 idx = self.idx 1145 self.next() 1146 1147 parenthesis := self.token == token.LEFT_PARENTHESIS 1148 right := self.parseLogicalAndExpression() 1149 if !parenthesis && isLogicalAndExpr(right) { 1150 goto mixed 1151 } 1152 1153 left = &ast.BinaryExpression{ 1154 Operator: token.COALESCE, 1155 Left: left, 1156 Right: right, 1157 } 1158 case token.LOGICAL_OR: 1159 idx = self.idx 1160 goto mixed 1161 default: 1162 return left 1163 } 1164 } 1165 } 1166 1167 mixed: 1168 self.error(idx, "Logical expressions and coalesce expressions cannot be mixed. Wrap either by parentheses") 1169 return left 1170 } 1171 1172 func (self *_parser) parseConditionalExpression() ast.Expression { 1173 left := self.parseLogicalOrExpression() 1174 1175 if self.token == token.QUESTION_MARK { 1176 self.next() 1177 allowIn := self.scope.allowIn 1178 self.scope.allowIn = true 1179 consequent := self.parseAssignmentExpression() 1180 self.scope.allowIn = allowIn 1181 self.expect(token.COLON) 1182 return &ast.ConditionalExpression{ 1183 Test: left, 1184 Consequent: consequent, 1185 Alternate: self.parseAssignmentExpression(), 1186 } 1187 } 1188 1189 return left 1190 } 1191 1192 func (self *_parser) parseArrowFunction(start file.Idx, paramList *ast.ParameterList, async bool) ast.Expression { 1193 self.expect(token.ARROW) 1194 node := &ast.ArrowFunctionLiteral{ 1195 Start: start, 1196 ParameterList: paramList, 1197 Async: async, 1198 } 1199 node.Body, node.DeclarationList = self.parseArrowFunctionBody(async) 1200 node.Source = self.slice(start, node.Body.Idx1()) 1201 return node 1202 } 1203 1204 func (self *_parser) parseSingleArgArrowFunction(start file.Idx, async bool) ast.Expression { 1205 if async != self.scope.allowAwait { 1206 self.scope.allowAwait = async 1207 defer func() { 1208 self.scope.allowAwait = !async 1209 }() 1210 } 1211 self.tokenToBindingId() 1212 if self.token != token.IDENTIFIER { 1213 self.errorUnexpectedToken(self.token) 1214 self.next() 1215 return &ast.BadExpression{ 1216 From: start, 1217 To: self.idx, 1218 } 1219 } 1220 1221 id := self.parseIdentifier() 1222 1223 paramList := &ast.ParameterList{ 1224 Opening: id.Idx, 1225 Closing: id.Idx1(), 1226 List: []*ast.Binding{{ 1227 Target: id, 1228 }}, 1229 } 1230 1231 return self.parseArrowFunction(start, paramList, async) 1232 } 1233 1234 func (self *_parser) parseAssignmentExpression() ast.Expression { 1235 start := self.idx 1236 parenthesis := false 1237 async := false 1238 var state parserState 1239 switch self.token { 1240 case token.LEFT_PARENTHESIS: 1241 self.mark(&state) 1242 parenthesis = true 1243 case token.ASYNC: 1244 tok := self.peek() 1245 if self.isBindingId(tok) { 1246 // async x => ... 1247 self.next() 1248 return self.parseSingleArgArrowFunction(start, true) 1249 } else if tok == token.LEFT_PARENTHESIS { 1250 self.mark(&state) 1251 async = true 1252 } 1253 case token.YIELD: 1254 if self.scope.allowYield { 1255 return self.parseYieldExpression() 1256 } 1257 fallthrough 1258 default: 1259 self.tokenToBindingId() 1260 } 1261 left := self.parseConditionalExpression() 1262 var operator token.Token 1263 switch self.token { 1264 case token.ASSIGN: 1265 operator = self.token 1266 case token.ADD_ASSIGN: 1267 operator = token.PLUS 1268 case token.SUBTRACT_ASSIGN: 1269 operator = token.MINUS 1270 case token.MULTIPLY_ASSIGN: 1271 operator = token.MULTIPLY 1272 case token.EXPONENT_ASSIGN: 1273 operator = token.EXPONENT 1274 case token.QUOTIENT_ASSIGN: 1275 operator = token.SLASH 1276 case token.REMAINDER_ASSIGN: 1277 operator = token.REMAINDER 1278 case token.AND_ASSIGN: 1279 operator = token.AND 1280 case token.OR_ASSIGN: 1281 operator = token.OR 1282 case token.EXCLUSIVE_OR_ASSIGN: 1283 operator = token.EXCLUSIVE_OR 1284 case token.SHIFT_LEFT_ASSIGN: 1285 operator = token.SHIFT_LEFT 1286 case token.SHIFT_RIGHT_ASSIGN: 1287 operator = token.SHIFT_RIGHT 1288 case token.UNSIGNED_SHIFT_RIGHT_ASSIGN: 1289 operator = token.UNSIGNED_SHIFT_RIGHT 1290 case token.ARROW: 1291 var paramList *ast.ParameterList 1292 if id, ok := left.(*ast.Identifier); ok { 1293 paramList = &ast.ParameterList{ 1294 Opening: id.Idx, 1295 Closing: id.Idx1() - 1, 1296 List: []*ast.Binding{{ 1297 Target: id, 1298 }}, 1299 } 1300 } else if parenthesis { 1301 if seq, ok := left.(*ast.SequenceExpression); ok && len(self.errors) == 0 { 1302 paramList = self.reinterpretSequenceAsArrowFuncParams(seq.Sequence) 1303 } else { 1304 self.restore(&state) 1305 paramList = self.parseFunctionParameterList() 1306 } 1307 } else if async { 1308 // async (x, y) => ... 1309 if !self.scope.allowAwait { 1310 self.scope.allowAwait = true 1311 defer func() { 1312 self.scope.allowAwait = false 1313 }() 1314 } 1315 if _, ok := left.(*ast.CallExpression); ok { 1316 self.restore(&state) 1317 self.next() // skip "async" 1318 paramList = self.parseFunctionParameterList() 1319 } 1320 } 1321 if paramList == nil { 1322 self.error(left.Idx0(), "Malformed arrow function parameter list") 1323 return &ast.BadExpression{From: left.Idx0(), To: left.Idx1()} 1324 } 1325 return self.parseArrowFunction(start, paramList, async) 1326 } 1327 1328 if operator != 0 { 1329 idx := self.idx 1330 self.next() 1331 ok := false 1332 switch l := left.(type) { 1333 case *ast.Identifier, *ast.DotExpression, *ast.PrivateDotExpression, *ast.BracketExpression: 1334 ok = true 1335 case *ast.ArrayLiteral: 1336 if !parenthesis && operator == token.ASSIGN { 1337 left = self.reinterpretAsArrayAssignmentPattern(l) 1338 ok = true 1339 } 1340 case *ast.ObjectLiteral: 1341 if !parenthesis && operator == token.ASSIGN { 1342 left = self.reinterpretAsObjectAssignmentPattern(l) 1343 ok = true 1344 } 1345 } 1346 if ok { 1347 return &ast.AssignExpression{ 1348 Left: left, 1349 Operator: operator, 1350 Right: self.parseAssignmentExpression(), 1351 } 1352 } 1353 self.error(left.Idx0(), "Invalid left-hand side in assignment") 1354 self.nextStatement() 1355 return &ast.BadExpression{From: idx, To: self.idx} 1356 } 1357 1358 return left 1359 } 1360 1361 func (self *_parser) parseYieldExpression() ast.Expression { 1362 idx := self.expect(token.YIELD) 1363 1364 if self.scope.inFuncParams { 1365 self.error(idx, "Yield expression not allowed in formal parameter") 1366 } 1367 1368 node := &ast.YieldExpression{ 1369 Yield: idx, 1370 } 1371 1372 if !self.implicitSemicolon && self.token == token.MULTIPLY { 1373 node.Delegate = true 1374 self.next() 1375 } 1376 1377 if !self.implicitSemicolon && self.token != token.SEMICOLON && self.token != token.RIGHT_BRACE && self.token != token.EOF { 1378 var state parserState 1379 self.mark(&state) 1380 expr := self.parseAssignmentExpression() 1381 if _, bad := expr.(*ast.BadExpression); bad { 1382 expr = nil 1383 self.restore(&state) 1384 } 1385 node.Argument = expr 1386 } 1387 1388 return node 1389 } 1390 1391 func (self *_parser) parseExpression() ast.Expression { 1392 left := self.parseAssignmentExpression() 1393 1394 if self.token == token.COMMA { 1395 sequence := []ast.Expression{left} 1396 for { 1397 if self.token != token.COMMA { 1398 break 1399 } 1400 self.next() 1401 sequence = append(sequence, self.parseAssignmentExpression()) 1402 } 1403 return &ast.SequenceExpression{ 1404 Sequence: sequence, 1405 } 1406 } 1407 1408 return left 1409 } 1410 1411 func (self *_parser) checkComma(from, to file.Idx) { 1412 if pos := strings.IndexByte(self.str[int(from)-self.base:int(to)-self.base], ','); pos >= 0 { 1413 self.error(from+file.Idx(pos), "Comma is not allowed here") 1414 } 1415 } 1416 1417 func (self *_parser) reinterpretAsArrayAssignmentPattern(left *ast.ArrayLiteral) ast.Expression { 1418 value := left.Value 1419 var rest ast.Expression 1420 for i, item := range value { 1421 if spread, ok := item.(*ast.SpreadElement); ok { 1422 if i != len(value)-1 { 1423 self.error(item.Idx0(), "Rest element must be last element") 1424 return &ast.BadExpression{From: left.Idx0(), To: left.Idx1()} 1425 } 1426 self.checkComma(spread.Expression.Idx1(), left.RightBracket) 1427 rest = self.reinterpretAsDestructAssignTarget(spread.Expression) 1428 value = value[:len(value)-1] 1429 } else { 1430 value[i] = self.reinterpretAsAssignmentElement(item) 1431 } 1432 } 1433 return &ast.ArrayPattern{ 1434 LeftBracket: left.LeftBracket, 1435 RightBracket: left.RightBracket, 1436 Elements: value, 1437 Rest: rest, 1438 } 1439 } 1440 1441 func (self *_parser) reinterpretArrayAssignPatternAsBinding(pattern *ast.ArrayPattern) *ast.ArrayPattern { 1442 for i, item := range pattern.Elements { 1443 pattern.Elements[i] = self.reinterpretAsDestructBindingTarget(item) 1444 } 1445 if pattern.Rest != nil { 1446 pattern.Rest = self.reinterpretAsDestructBindingTarget(pattern.Rest) 1447 } 1448 return pattern 1449 } 1450 1451 func (self *_parser) reinterpretAsArrayBindingPattern(left *ast.ArrayLiteral) ast.BindingTarget { 1452 value := left.Value 1453 var rest ast.Expression 1454 for i, item := range value { 1455 if spread, ok := item.(*ast.SpreadElement); ok { 1456 if i != len(value)-1 { 1457 self.error(item.Idx0(), "Rest element must be last element") 1458 return &ast.BadExpression{From: left.Idx0(), To: left.Idx1()} 1459 } 1460 self.checkComma(spread.Expression.Idx1(), left.RightBracket) 1461 rest = self.reinterpretAsDestructBindingTarget(spread.Expression) 1462 value = value[:len(value)-1] 1463 } else { 1464 value[i] = self.reinterpretAsBindingElement(item) 1465 } 1466 } 1467 return &ast.ArrayPattern{ 1468 LeftBracket: left.LeftBracket, 1469 RightBracket: left.RightBracket, 1470 Elements: value, 1471 Rest: rest, 1472 } 1473 } 1474 1475 func (self *_parser) parseArrayBindingPattern() ast.BindingTarget { 1476 return self.reinterpretAsArrayBindingPattern(self.parseArrayLiteral()) 1477 } 1478 1479 func (self *_parser) parseObjectBindingPattern() ast.BindingTarget { 1480 return self.reinterpretAsObjectBindingPattern(self.parseObjectLiteral()) 1481 } 1482 1483 func (self *_parser) reinterpretArrayObjectPatternAsBinding(pattern *ast.ObjectPattern) *ast.ObjectPattern { 1484 for _, prop := range pattern.Properties { 1485 if keyed, ok := prop.(*ast.PropertyKeyed); ok { 1486 keyed.Value = self.reinterpretAsBindingElement(keyed.Value) 1487 } 1488 } 1489 if pattern.Rest != nil { 1490 pattern.Rest = self.reinterpretAsBindingRestElement(pattern.Rest) 1491 } 1492 return pattern 1493 } 1494 1495 func (self *_parser) reinterpretAsObjectBindingPattern(expr *ast.ObjectLiteral) ast.BindingTarget { 1496 var rest ast.Expression 1497 value := expr.Value 1498 for i, prop := range value { 1499 ok := false 1500 switch prop := prop.(type) { 1501 case *ast.PropertyKeyed: 1502 if prop.Kind == ast.PropertyKindValue { 1503 prop.Value = self.reinterpretAsBindingElement(prop.Value) 1504 ok = true 1505 } 1506 case *ast.PropertyShort: 1507 ok = true 1508 case *ast.SpreadElement: 1509 if i != len(expr.Value)-1 { 1510 self.error(prop.Idx0(), "Rest element must be last element") 1511 return &ast.BadExpression{From: expr.Idx0(), To: expr.Idx1()} 1512 } 1513 // TODO make sure there is no trailing comma 1514 rest = self.reinterpretAsBindingRestElement(prop.Expression) 1515 value = value[:i] 1516 ok = true 1517 } 1518 if !ok { 1519 self.error(prop.Idx0(), "Invalid destructuring binding target") 1520 return &ast.BadExpression{From: expr.Idx0(), To: expr.Idx1()} 1521 } 1522 } 1523 return &ast.ObjectPattern{ 1524 LeftBrace: expr.LeftBrace, 1525 RightBrace: expr.RightBrace, 1526 Properties: value, 1527 Rest: rest, 1528 } 1529 } 1530 1531 func (self *_parser) reinterpretAsObjectAssignmentPattern(l *ast.ObjectLiteral) ast.Expression { 1532 var rest ast.Expression 1533 value := l.Value 1534 for i, prop := range value { 1535 ok := false 1536 switch prop := prop.(type) { 1537 case *ast.PropertyKeyed: 1538 if prop.Kind == ast.PropertyKindValue { 1539 prop.Value = self.reinterpretAsAssignmentElement(prop.Value) 1540 ok = true 1541 } 1542 case *ast.PropertyShort: 1543 ok = true 1544 case *ast.SpreadElement: 1545 if i != len(l.Value)-1 { 1546 self.error(prop.Idx0(), "Rest element must be last element") 1547 return &ast.BadExpression{From: l.Idx0(), To: l.Idx1()} 1548 } 1549 // TODO make sure there is no trailing comma 1550 rest = prop.Expression 1551 value = value[:i] 1552 ok = true 1553 } 1554 if !ok { 1555 self.error(prop.Idx0(), "Invalid destructuring assignment target") 1556 return &ast.BadExpression{From: l.Idx0(), To: l.Idx1()} 1557 } 1558 } 1559 return &ast.ObjectPattern{ 1560 LeftBrace: l.LeftBrace, 1561 RightBrace: l.RightBrace, 1562 Properties: value, 1563 Rest: rest, 1564 } 1565 } 1566 1567 func (self *_parser) reinterpretAsAssignmentElement(expr ast.Expression) ast.Expression { 1568 switch expr := expr.(type) { 1569 case *ast.AssignExpression: 1570 if expr.Operator == token.ASSIGN { 1571 expr.Left = self.reinterpretAsDestructAssignTarget(expr.Left) 1572 return expr 1573 } else { 1574 self.error(expr.Idx0(), "Invalid destructuring assignment target") 1575 return &ast.BadExpression{From: expr.Idx0(), To: expr.Idx1()} 1576 } 1577 default: 1578 return self.reinterpretAsDestructAssignTarget(expr) 1579 } 1580 } 1581 1582 func (self *_parser) reinterpretAsBindingElement(expr ast.Expression) ast.Expression { 1583 switch expr := expr.(type) { 1584 case *ast.AssignExpression: 1585 if expr.Operator == token.ASSIGN { 1586 expr.Left = self.reinterpretAsDestructBindingTarget(expr.Left) 1587 return expr 1588 } else { 1589 self.error(expr.Idx0(), "Invalid destructuring assignment target") 1590 return &ast.BadExpression{From: expr.Idx0(), To: expr.Idx1()} 1591 } 1592 default: 1593 return self.reinterpretAsDestructBindingTarget(expr) 1594 } 1595 } 1596 1597 func (self *_parser) reinterpretAsBinding(expr ast.Expression) *ast.Binding { 1598 switch expr := expr.(type) { 1599 case *ast.AssignExpression: 1600 if expr.Operator == token.ASSIGN { 1601 return &ast.Binding{ 1602 Target: self.reinterpretAsDestructBindingTarget(expr.Left), 1603 Initializer: expr.Right, 1604 } 1605 } else { 1606 self.error(expr.Idx0(), "Invalid destructuring assignment target") 1607 return &ast.Binding{ 1608 Target: &ast.BadExpression{From: expr.Idx0(), To: expr.Idx1()}, 1609 } 1610 } 1611 default: 1612 return &ast.Binding{ 1613 Target: self.reinterpretAsDestructBindingTarget(expr), 1614 } 1615 } 1616 } 1617 1618 func (self *_parser) reinterpretAsDestructAssignTarget(item ast.Expression) ast.Expression { 1619 switch item := item.(type) { 1620 case nil: 1621 return nil 1622 case *ast.ArrayLiteral: 1623 return self.reinterpretAsArrayAssignmentPattern(item) 1624 case *ast.ObjectLiteral: 1625 return self.reinterpretAsObjectAssignmentPattern(item) 1626 case ast.Pattern, *ast.Identifier, *ast.DotExpression, *ast.PrivateDotExpression, *ast.BracketExpression: 1627 return item 1628 } 1629 self.error(item.Idx0(), "Invalid destructuring assignment target") 1630 return &ast.BadExpression{From: item.Idx0(), To: item.Idx1()} 1631 } 1632 1633 func (self *_parser) reinterpretAsDestructBindingTarget(item ast.Expression) ast.BindingTarget { 1634 switch item := item.(type) { 1635 case nil: 1636 return nil 1637 case *ast.ArrayPattern: 1638 return self.reinterpretArrayAssignPatternAsBinding(item) 1639 case *ast.ObjectPattern: 1640 return self.reinterpretArrayObjectPatternAsBinding(item) 1641 case *ast.ArrayLiteral: 1642 return self.reinterpretAsArrayBindingPattern(item) 1643 case *ast.ObjectLiteral: 1644 return self.reinterpretAsObjectBindingPattern(item) 1645 case *ast.Identifier: 1646 if !self.scope.allowAwait || item.Name != "await" { 1647 return item 1648 } 1649 } 1650 self.error(item.Idx0(), "Invalid destructuring binding target") 1651 return &ast.BadExpression{From: item.Idx0(), To: item.Idx1()} 1652 } 1653 1654 func (self *_parser) reinterpretAsBindingRestElement(expr ast.Expression) ast.Expression { 1655 if _, ok := expr.(*ast.Identifier); ok { 1656 return expr 1657 } 1658 self.error(expr.Idx0(), "Invalid binding rest") 1659 return &ast.BadExpression{From: expr.Idx0(), To: expr.Idx1()} 1660 }