| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 // Utilities for building JS ASTs at runtime. Contains a builder class | 5 // Utilities for building JS ASTs at runtime. Contains a builder class |
| 6 // and a parser that parses part of the language. | 6 // and a parser that parses part of the language. |
| 7 | 7 |
| 8 part of js_ast; | 8 part of js_ast; |
| 9 | 9 |
| 10 | 10 |
| (...skipping 1134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1145 Expression parseExpression() { | 1145 Expression parseExpression() { |
| 1146 Expression expression = parseAssignment(); | 1146 Expression expression = parseAssignment(); |
| 1147 while (acceptCategory(COMMA)) { | 1147 while (acceptCategory(COMMA)) { |
| 1148 Expression right = parseAssignment(); | 1148 Expression right = parseAssignment(); |
| 1149 expression = new Binary(',', expression, right); | 1149 expression = new Binary(',', expression, right); |
| 1150 } | 1150 } |
| 1151 return expression; | 1151 return expression; |
| 1152 } | 1152 } |
| 1153 | 1153 |
| 1154 /** Parse a variable declaration list, with `var` or `let` [keyword] */ | 1154 /** Parse a variable declaration list, with `var` or `let` [keyword] */ |
| 1155 VariableDeclarationList parseVariableDeclarationList(String keyword) { | 1155 VariableDeclarationList parseVariableDeclarationList( |
| 1156 String keyword, [String firstIdentifier]) { |
| 1156 var initialization = []; | 1157 var initialization = []; |
| 1157 | 1158 |
| 1158 do { | 1159 do { |
| 1159 var declarator = parseVariableBinding(); | 1160 var declarator; |
| 1161 if (firstIdentifier != null) { |
| 1162 declarator = new Identifier(firstIdentifier); |
| 1163 firstIdentifier = null; |
| 1164 } else { |
| 1165 declarator = parseVariableBinding(); |
| 1166 } |
| 1167 |
| 1160 var initializer = acceptString("=") ? parseAssignment() : null; | 1168 var initializer = acceptString("=") ? parseAssignment() : null; |
| 1161 initialization.add(new VariableInitialization(declarator, initializer)); | 1169 initialization.add(new VariableInitialization(declarator, initializer)); |
| 1162 } while (acceptCategory(COMMA)); | 1170 } while (acceptCategory(COMMA)); |
| 1163 | 1171 |
| 1164 return new VariableDeclarationList(keyword, initialization); | 1172 return new VariableDeclarationList(keyword, initialization); |
| 1165 } | 1173 } |
| 1166 | 1174 |
| 1167 VariableBinding parseVariableBinding() { | 1175 VariableBinding parseVariableBinding() { |
| 1168 switch (lastCategory) { | 1176 switch (lastCategory) { |
| 1169 case ALPHA: | 1177 case ALPHA: |
| (...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1449 body); | 1457 body); |
| 1450 } else if (acceptString('of')) { | 1458 } else if (acceptString('of')) { |
| 1451 Expression iterableExpression = parseAssignment(); | 1459 Expression iterableExpression = parseAssignment(); |
| 1452 expectCategory(RPAREN); | 1460 expectCategory(RPAREN); |
| 1453 Statement body = parseStatement(); | 1461 Statement body = parseStatement(); |
| 1454 return new ForOf( | 1462 return new ForOf( |
| 1455 _createVariableDeclarationList(keyword, identifier), | 1463 _createVariableDeclarationList(keyword, identifier), |
| 1456 iterableExpression, | 1464 iterableExpression, |
| 1457 body); | 1465 body); |
| 1458 } | 1466 } |
| 1459 var declarations = parseVariableDeclarationList(keyword); | 1467 var declarations = parseVariableDeclarationList(keyword, identifier); |
| 1460 expectCategory(SEMICOLON); | 1468 expectCategory(SEMICOLON); |
| 1461 return finishFor(declarations); | 1469 return finishFor(declarations); |
| 1462 } | 1470 } |
| 1463 | 1471 |
| 1464 Expression init = parseExpression(); | 1472 Expression init = parseExpression(); |
| 1465 expectCategory(SEMICOLON); | 1473 expectCategory(SEMICOLON); |
| 1466 return finishFor(init); | 1474 return finishFor(init); |
| 1467 } | 1475 } |
| 1468 | 1476 |
| 1469 static VariableDeclarationList _createVariableDeclarationList( | 1477 static VariableDeclarationList _createVariableDeclarationList( |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1581 * are supported: | 1589 * are supported: |
| 1582 * | 1590 * |
| 1583 * - getter/setter names: `get #() { ... }` | 1591 * - getter/setter names: `get #() { ... }` |
| 1584 * - method names: `#() { ... }` | 1592 * - method names: `#() { ... }` |
| 1585 * - property names: `#: ...` | 1593 * - property names: `#: ...` |
| 1586 * - entire methods: `#` | 1594 * - entire methods: `#` |
| 1587 */ | 1595 */ |
| 1588 Property parseMethodOrProperty({bool onlyMethods: false}) { | 1596 Property parseMethodOrProperty({bool onlyMethods: false}) { |
| 1589 bool isStatic = acceptString('static'); | 1597 bool isStatic = acceptString('static'); |
| 1590 | 1598 |
| 1591 bool isGetter = false; | 1599 bool isGetter = lastToken == 'get'; |
| 1592 bool isSetter = false; | 1600 bool isSetter = lastToken == 'set'; |
| 1593 Expression name = null; | 1601 Expression name = null; |
| 1594 bool propertyNameIsIdentifier = lastCategory == ALPHA; | 1602 if (isGetter || isSetter) { |
| 1603 var token = lastToken; |
| 1604 getToken(); |
| 1605 if (lastCategory == COLON) { |
| 1606 // That wasn't a accessor but the 'get' or 'set' property: retropedal. |
| 1607 isGetter = isSetter = false; |
| 1608 name = new LiteralString('"$token"'); |
| 1609 } |
| 1610 } |
| 1595 if (acceptCategory(HASH)) { | 1611 if (acceptCategory(HASH)) { |
| 1596 if (lastCategory != LPAREN && (onlyMethods || lastCategory != COLON)) { | 1612 if (lastCategory != LPAREN && (onlyMethods || lastCategory != COLON)) { |
| 1597 // Interpolated method | 1613 // Interpolated method |
| 1598 var member = new InterpolatedMethod(parseHash()); | 1614 var member = new InterpolatedMethod(parseHash()); |
| 1599 interpolatedValues.add(member); | 1615 interpolatedValues.add(member); |
| 1600 return member; | 1616 return member; |
| 1601 } | 1617 } |
| 1602 name = parseInterpolatedExpression(); | 1618 name = parseInterpolatedExpression(); |
| 1603 } else { | 1619 } else { |
| 1604 name = parsePropertyName(); | 1620 name ??= parsePropertyName(); |
| 1605 } | |
| 1606 | |
| 1607 // Allow get or set to be followed by another property name. | |
| 1608 if (propertyNameIsIdentifier && | |
| 1609 (lastCategory == ALPHA || lastCategory == HASH)) { | |
| 1610 LiteralString p = name; | |
| 1611 isGetter = p.value == '"get"'; | |
| 1612 isSetter = p.value == '"set"'; | |
| 1613 if (isGetter || isSetter) { | |
| 1614 name = parsePropertyName(); | |
| 1615 } | |
| 1616 } | 1621 } |
| 1617 | 1622 |
| 1618 if (!onlyMethods && acceptCategory(COLON)) { | 1623 if (!onlyMethods && acceptCategory(COLON)) { |
| 1619 Expression value = parseAssignment(); | 1624 Expression value = parseAssignment(); |
| 1620 return new Property(name, value); | 1625 return new Property(name, value); |
| 1621 } else { | 1626 } else { |
| 1622 var fun = parseFun(); | 1627 var fun = parseFun(); |
| 1623 return new Method(name, fun, | 1628 return new Method(name, fun, |
| 1624 isGetter: isGetter, isSetter: isSetter, isStatic: isStatic); | 1629 isGetter: isGetter, isSetter: isSetter, isStatic: isStatic); |
| 1625 } | 1630 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 1637 expectCategory(RSQUARE); | 1642 expectCategory(RSQUARE); |
| 1638 return expr; | 1643 return expr; |
| 1639 } else if (acceptCategory(HASH)) { | 1644 } else if (acceptCategory(HASH)) { |
| 1640 return parseInterpolatedExpression(); | 1645 return parseInterpolatedExpression(); |
| 1641 } else { | 1646 } else { |
| 1642 error('Expected property name'); | 1647 error('Expected property name'); |
| 1643 return null; | 1648 return null; |
| 1644 } | 1649 } |
| 1645 } | 1650 } |
| 1646 } | 1651 } |
| OLD | NEW |