Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10)

Side by Side Diff: lib/src/js/builder.dart

Issue 1484263002: Use destructuring assignments for named parameters (#180) (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: Destructure function params directly (no more opts in most cases) Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 793 matching lines...) Expand 10 before | Expand all | Expand 10 after
804 return null; 804 return null;
805 } 805 }
806 } 806 }
807 807
808 InterpolatedExpression parseInterpolatedExpression() { 808 InterpolatedExpression parseInterpolatedExpression() {
809 var expression = new InterpolatedExpression(parseHash()); 809 var expression = new InterpolatedExpression(parseHash());
810 interpolatedValues.add(expression); 810 interpolatedValues.add(expression);
811 return expression; 811 return expression;
812 } 812 }
813 813
814 InterpolatedIdentifier parseInterpolatedIdentifier() {
815 var id = new InterpolatedIdentifier(parseHash());
816 interpolatedValues.add(id);
817 return id;
818 }
819
820 Identifier parseIdentifier() {
821 if (acceptCategory(HASH)) {
822 return parseInterpolatedIdentifier();
823 } else {
824 var id = new Identifier(lastToken);
825 expectCategory(ALPHA);
826 return id;
827 }
828 }
829
814 /** 830 /**
815 * CoverParenthesizedExpressionAndArrowParameterList[Yield] : 831 * CoverParenthesizedExpressionAndArrowParameterList[Yield] :
816 * ( Expression ) 832 * ( Expression )
817 * ( ) 833 * ( )
818 * ( ... BindingIdentifier ) 834 * ( ... BindingIdentifier )
819 * ( Expression , ... BindingIdentifier ) 835 * ( Expression , ... BindingIdentifier )
820 */ 836 */
821 Expression parseExpressionOrArrowFunction() { 837 Expression parseExpressionOrArrowFunction() {
822 if (acceptCategory(RPAREN)) { 838 if (acceptCategory(RPAREN)) {
823 expectCategory(ARROW); 839 expectCategory(ARROW);
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after
1129 Expression parseExpression() { 1145 Expression parseExpression() {
1130 Expression expression = parseAssignment(); 1146 Expression expression = parseAssignment();
1131 while (acceptCategory(COMMA)) { 1147 while (acceptCategory(COMMA)) {
1132 Expression right = parseAssignment(); 1148 Expression right = parseAssignment();
1133 expression = new Binary(',', expression, right); 1149 expression = new Binary(',', expression, right);
1134 } 1150 }
1135 return expression; 1151 return expression;
1136 } 1152 }
1137 1153
1138 /** Parse a variable declaration list, with `var` or `let` [keyword] */ 1154 /** Parse a variable declaration list, with `var` or `let` [keyword] */
1139 VariableDeclarationList parseVariableDeclarationList(String keyword) { 1155 VariableDeclarationList parseVariableDeclarationList(String keyword) =>
1140 // Supports one form for interpolated variable declaration: 1156 finishVariableDeclarationList(keyword);
Jennifer Messerly 2015/12/01 02:10:27 why introduce this redirect? If they aren't differ
ochafik 2015/12/02 20:05:46 Done.
1141 // let # = ...
1142 if (acceptCategory(HASH)) {
1143 var name = new InterpolatedIdentifier(parseHash());
1144 interpolatedValues.add(name);
1145 1157
1146 Expression initializer = acceptString("=") ? parseAssignment() : null; 1158 VariableDeclarationList finishVariableDeclarationList(String keyword) {
1147 return new VariableDeclarationList(keyword,
1148 [new VariableInitialization(name, initializer)]);
1149 }
1150
1151 String firstVariable = lastToken;
1152 expectCategory(ALPHA);
1153 return finishVariableDeclarationList(keyword, firstVariable);
1154 }
1155
1156 VariableDeclarationList finishVariableDeclarationList(
1157 String keyword, String firstVariable) {
1158 var initialization = []; 1159 var initialization = [];
1159 1160
1160 void declare(String variable) { 1161 do {
1162 var declarator;
1163 switch (lastCategory) {
Jennifer Messerly 2015/12/01 02:10:27 this pattern occurs in a couple of places, pull it
ochafik 2015/12/02 20:05:46 +1, meet parseVariableBinding()
1164 case ALPHA:
1165 case HASH:
1166 declarator = parseIdentifier();
1167 break;
1168 case LBRACE:
1169 case LSQUARE:
1170 declarator = parseDestructuring();
1171 break;
1172 default:
1173 throw new StateError('Unexpected token $lastToken: ${categoryToString( lastCategory)}');
Jennifer Messerly 2015/12/01 02:10:27 long line. FYI, to prevent large churn here we exc
ochafik 2015/12/02 20:05:46 Done.
1174 }
1161 Expression initializer = null; 1175 Expression initializer = null;
1162 if (acceptString("=")) { 1176 if (acceptString("=")) {
1163 initializer = parseAssignment(); 1177 initializer = parseAssignment();
1164 } 1178 }
1165 var declaration = new Identifier(variable); 1179 initialization.add(new VariableInitialization(declarator, initializer));
1166 initialization.add(new VariableInitialization(declaration, initializer)); 1180 } while (acceptCategory(COMMA));
1181
1182 return new VariableDeclarationList(keyword, initialization);
1183 }
1184
1185 Destructuring parseDestructuring() {
Jennifer Messerly 2015/12/01 02:10:27 It'd be worth adding a link to where this is defin
ochafik 2015/12/02 20:05:46 Done.
1186 if (acceptCategory(LBRACE)) {
1187 return parseObjectDestructuring();
1188 } else {
1189 expectCategory(LSQUARE);
1190 return parseArrayDestructuring();
1167 } 1191 }
1192 }
1168 1193
1169 declare(firstVariable); 1194 ArrayDestructuring parseArrayDestructuring() {
1170 while (acceptCategory(COMMA)) { 1195 var variables = <DestructuredVariable>[];
1171 String variable = lastToken; 1196 do {
1172 expectCategory(ALPHA); 1197 var name;
1173 declare(variable); 1198 var structure;
1174 } 1199 var defaultValue;
1175 return new VariableDeclarationList(keyword, initialization); 1200
1201 switch (lastCategory) {
1202 case ALPHA:
1203 case HASH:
1204 name = parseIdentifier();
1205 break;
1206 case LBRACE:
1207 case LSQUARE:
1208 structure = parseDestructuring();
1209 break;
1210 default:
1211 throw new StateError('Unexpected token $lastToken: ${categoryToString( lastCategory)}');
1212 }
1213 if (acceptString("=")) {
1214 defaultValue = parseExpression();
1215 }
1216 variables.add(new DestructuredVariable(
1217 name: name, structure: structure, defaultValue: defaultValue));
1218 } while (acceptCategory(COMMA));
1219
1220 expectCategory(RSQUARE);
1221 return new ArrayDestructuring(variables);
1222 }
1223
1224 ObjectDestructuring parseObjectDestructuring() {
1225 var variables = <DestructuredVariable>[];
1226 do {
1227 var name = parseIdentifier();
1228 var structure;
1229 var defaultValue;
1230
1231 if (acceptCategory(COLON)) {
Jennifer Messerly 2015/12/01 02:10:27 Hmmm, normally we'd do parseAssignment, like the o
ochafik 2015/12/02 20:05:46 Hehe, tried that, but... the grammar is a bit sens
1232 structure = parseDestructuring();
1233 } else if (acceptString("=")) {
1234 defaultValue = parseExpression();
1235 }
1236 variables.add(new DestructuredVariable(
1237 name: name, structure: structure, defaultValue: defaultValue));
1238 } while (acceptCategory(COMMA));
1239
1240 expectCategory(RBRACE);
1241 return new ObjectDestructuring(variables);
1176 } 1242 }
1177 1243
1178 Expression parseVarDeclarationOrExpression() { 1244 Expression parseVarDeclarationOrExpression() {
1179 var keyword = acceptVarOrLet(); 1245 var keyword = acceptVarOrLet();
1180 if (keyword != null) { 1246 if (keyword != null) {
1181 return parseVariableDeclarationList(keyword); 1247 return parseVariableDeclarationList(keyword);
1182 } else { 1248 } else {
1183 return parseExpression(); 1249 return parseExpression();
1184 } 1250 }
1185 } 1251 }
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after
1387 body); 1453 body);
1388 } else if (acceptString('of')) { 1454 } else if (acceptString('of')) {
1389 Expression iterableExpression = parseAssignment(); 1455 Expression iterableExpression = parseAssignment();
1390 expectCategory(RPAREN); 1456 expectCategory(RPAREN);
1391 Statement body = parseStatement(); 1457 Statement body = parseStatement();
1392 return new ForOf( 1458 return new ForOf(
1393 _createVariableDeclarationList(keyword, identifier), 1459 _createVariableDeclarationList(keyword, identifier),
1394 iterableExpression, 1460 iterableExpression,
1395 body); 1461 body);
1396 } 1462 }
1397 var declarations = finishVariableDeclarationList(keyword, identifier); 1463 var declarations = finishVariableDeclarationList(keyword);
1398 expectCategory(SEMICOLON); 1464 expectCategory(SEMICOLON);
1399 return finishFor(declarations); 1465 return finishFor(declarations);
1400 } 1466 }
1401 1467
1402 Expression init = parseExpression(); 1468 Expression init = parseExpression();
1403 expectCategory(SEMICOLON); 1469 expectCategory(SEMICOLON);
1404 return finishFor(init); 1470 return finishFor(init);
1405 } 1471 }
1406 1472
1407 static VariableDeclarationList _createVariableDeclarationList( 1473 static VariableDeclarationList _createVariableDeclarationList(
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
1491 expectCategory(LPAREN); 1557 expectCategory(LPAREN);
1492 String identifier = lastToken; 1558 String identifier = lastToken;
1493 expectCategory(ALPHA); 1559 expectCategory(ALPHA);
1494 expectCategory(RPAREN); 1560 expectCategory(RPAREN);
1495 expectCategory(LBRACE); 1561 expectCategory(LBRACE);
1496 Block body = parseBlock(); 1562 Block body = parseBlock();
1497 return new Catch(new Identifier(identifier), body); 1563 return new Catch(new Identifier(identifier), body);
1498 } 1564 }
1499 1565
1500 ClassExpression parseClass() { 1566 ClassExpression parseClass() {
1501 Identifier name; 1567 Identifier name = parseIdentifier();
1502 if (acceptCategory(HASH)) {
1503 var interpolatedName = new InterpolatedIdentifier(parseHash());
1504 interpolatedValues.add(interpolatedName);
1505 name = interpolatedName;
1506 } else {
1507 name = new Identifier(lastToken);
1508 expectCategory(ALPHA);
1509 }
1510 Expression heritage = null; 1568 Expression heritage = null;
1511 if (acceptString('extends')) { 1569 if (acceptString('extends')) {
1512 heritage = parseLeftHandSide(); 1570 heritage = parseLeftHandSide();
1513 } 1571 }
1514 expectCategory(LBRACE); 1572 expectCategory(LBRACE);
1515 var methods = new List<Method>(); 1573 var methods = new List<Method>();
1516 while (lastCategory != RBRACE) { 1574 while (lastCategory != RBRACE) {
1517 methods.add(parseMethodOrProperty(onlyMethods: true)); 1575 methods.add(parseMethodOrProperty(onlyMethods: true));
1518 } 1576 }
1519 expectCategory(RBRACE); 1577 expectCategory(RBRACE);
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
1583 expectCategory(RSQUARE); 1641 expectCategory(RSQUARE);
1584 return expr; 1642 return expr;
1585 } else if (acceptCategory(HASH)) { 1643 } else if (acceptCategory(HASH)) {
1586 return parseInterpolatedExpression(); 1644 return parseInterpolatedExpression();
1587 } else { 1645 } else {
1588 error('Expected property name'); 1646 error('Expected property name');
1589 return null; 1647 return null;
1590 } 1648 }
1591 } 1649 }
1592 } 1650 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698