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

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: 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
« no previous file with comments | « lib/src/codegen/js_codegen.dart ('k') | lib/src/js/nodes.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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:
1141 // let # = ...
1142 if (acceptCategory(HASH)) {
1143 var name = new InterpolatedIdentifier(parseHash());
1144 interpolatedValues.add(name);
1145
1146 Expression initializer = acceptString("=") ? parseAssignment() : null;
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 = []; 1156 var initialization = [];
1159 1157
1160 void declare(String variable) { 1158 do {
1161 Expression initializer = null; 1159 var declarator = parseVariableBinding();
1162 if (acceptString("=")) { 1160 var initializer = acceptString("=") ? parseAssignment() : null;
1163 initializer = parseAssignment(); 1161 initialization.add(new VariableInitialization(declarator, initializer));
1164 } 1162 } while (acceptCategory(COMMA));
1165 var declaration = new Identifier(variable);
1166 initialization.add(new VariableInitialization(declaration, initializer));
1167 }
1168 1163
1169 declare(firstVariable);
1170 while (acceptCategory(COMMA)) {
1171 String variable = lastToken;
1172 expectCategory(ALPHA);
1173 declare(variable);
1174 }
1175 return new VariableDeclarationList(keyword, initialization); 1164 return new VariableDeclarationList(keyword, initialization);
1176 } 1165 }
1177 1166
1167 VariableBinding parseVariableBinding() {
1168 switch (lastCategory) {
1169 case ALPHA:
1170 case HASH:
1171 return parseIdentifier();
1172 case LBRACE:
1173 case LSQUARE:
1174 return parseBindingPattern();
1175 default:
1176 error('Unexpected token $lastToken: ${categoryToString(lastCategory)}');
1177 return null;
1178 }
1179 }
1180
1181 /// Note: this doesn't deal with general-case destructuring yet, it just
1182 /// supports it in variable initialization.
1183 /// See ES6 spec:
1184 /// http://www.ecma-international.org/ecma-262/6.0/#sec-destructuring-binding- patterns
1185 /// http://www.ecma-international.org/ecma-262/6.0/#sec-destructuring-assignme nt
1186 /// TODO(ochafik): Support destructuring in LeftHandSideExpression.
1187 BindingPattern parseBindingPattern() {
1188 if (acceptCategory(LBRACE)) {
1189 return parseObjectBindingPattern();
1190 } else {
1191 expectCategory(LSQUARE);
1192 return parseArrayBindingPattern();
1193 }
1194 }
1195
1196 ArrayBindingPattern parseArrayBindingPattern() {
1197 var variables = <DestructuredVariable>[];
1198 do {
1199 var name;
1200 var structure;
1201 var defaultValue;
1202
1203 var declarator = parseVariableBinding();
1204 if (declarator is Identifier) name = declarator;
1205 else if (declarator is BindingPattern) structure = declarator;
1206 else error("Unexpected LHS: $declarator");
1207
1208 if (acceptString("=")) {
1209 defaultValue = parseExpression();
1210 }
1211 variables.add(new DestructuredVariable(
1212 name: name, structure: structure, defaultValue: defaultValue));
1213 } while (acceptCategory(COMMA));
1214
1215 expectCategory(RSQUARE);
1216 return new ArrayBindingPattern(variables);
1217 }
1218
1219 ObjectBindingPattern parseObjectBindingPattern() {
1220 var variables = <DestructuredVariable>[];
1221 do {
1222 var name = parseIdentifier();
1223 var structure;
1224 var defaultValue;
1225
1226 if (acceptCategory(COLON)) {
1227 structure = parseBindingPattern();
1228 } else if (acceptString("=")) {
1229 defaultValue = parseExpression();
1230 }
1231 variables.add(new DestructuredVariable(
1232 name: name, structure: structure, defaultValue: defaultValue));
1233 } while (acceptCategory(COMMA));
1234
1235 expectCategory(RBRACE);
1236 return new ObjectBindingPattern(variables);
1237 }
1238
1178 Expression parseVarDeclarationOrExpression() { 1239 Expression parseVarDeclarationOrExpression() {
1179 var keyword = acceptVarLetOrConst(); 1240 var keyword = acceptVarLetOrConst();
1180 if (keyword != null) { 1241 if (keyword != null) {
1181 return parseVariableDeclarationList(keyword); 1242 return parseVariableDeclarationList(keyword);
1182 } else { 1243 } else {
1183 return parseExpression(); 1244 return parseExpression();
1184 } 1245 }
1185 } 1246 }
1186 1247
1187 /** Accepts a `var` or `let` keyword. If neither is found, returns null. */ 1248 /** Accepts a `var` or `let` keyword. If neither is found, returns null. */
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after
1388 body); 1449 body);
1389 } else if (acceptString('of')) { 1450 } else if (acceptString('of')) {
1390 Expression iterableExpression = parseAssignment(); 1451 Expression iterableExpression = parseAssignment();
1391 expectCategory(RPAREN); 1452 expectCategory(RPAREN);
1392 Statement body = parseStatement(); 1453 Statement body = parseStatement();
1393 return new ForOf( 1454 return new ForOf(
1394 _createVariableDeclarationList(keyword, identifier), 1455 _createVariableDeclarationList(keyword, identifier),
1395 iterableExpression, 1456 iterableExpression,
1396 body); 1457 body);
1397 } 1458 }
1398 var declarations = finishVariableDeclarationList(keyword, identifier); 1459 var declarations = parseVariableDeclarationList(keyword);
1399 expectCategory(SEMICOLON); 1460 expectCategory(SEMICOLON);
1400 return finishFor(declarations); 1461 return finishFor(declarations);
1401 } 1462 }
1402 1463
1403 Expression init = parseExpression(); 1464 Expression init = parseExpression();
1404 expectCategory(SEMICOLON); 1465 expectCategory(SEMICOLON);
1405 return finishFor(init); 1466 return finishFor(init);
1406 } 1467 }
1407 1468
1408 static VariableDeclarationList _createVariableDeclarationList( 1469 static VariableDeclarationList _createVariableDeclarationList(
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
1492 expectCategory(LPAREN); 1553 expectCategory(LPAREN);
1493 String identifier = lastToken; 1554 String identifier = lastToken;
1494 expectCategory(ALPHA); 1555 expectCategory(ALPHA);
1495 expectCategory(RPAREN); 1556 expectCategory(RPAREN);
1496 expectCategory(LBRACE); 1557 expectCategory(LBRACE);
1497 Block body = parseBlock(); 1558 Block body = parseBlock();
1498 return new Catch(new Identifier(identifier), body); 1559 return new Catch(new Identifier(identifier), body);
1499 } 1560 }
1500 1561
1501 ClassExpression parseClass() { 1562 ClassExpression parseClass() {
1502 Identifier name; 1563 Identifier name = parseIdentifier();
1503 if (acceptCategory(HASH)) {
1504 var interpolatedName = new InterpolatedIdentifier(parseHash());
1505 interpolatedValues.add(interpolatedName);
1506 name = interpolatedName;
1507 } else {
1508 name = new Identifier(lastToken);
1509 expectCategory(ALPHA);
1510 }
1511 Expression heritage = null; 1564 Expression heritage = null;
1512 if (acceptString('extends')) { 1565 if (acceptString('extends')) {
1513 heritage = parseLeftHandSide(); 1566 heritage = parseConditional();
1514 } 1567 }
1515 expectCategory(LBRACE); 1568 expectCategory(LBRACE);
1516 var methods = new List<Method>(); 1569 var methods = new List<Method>();
1517 while (lastCategory != RBRACE) { 1570 while (lastCategory != RBRACE) {
1518 methods.add(parseMethodOrProperty(onlyMethods: true)); 1571 methods.add(parseMethodOrProperty(onlyMethods: true));
1519 } 1572 }
1520 expectCategory(RBRACE); 1573 expectCategory(RBRACE);
1521 return new ClassExpression(name, heritage, methods); 1574 return new ClassExpression(name, heritage, methods);
1522 } 1575 }
1523 1576
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
1584 expectCategory(RSQUARE); 1637 expectCategory(RSQUARE);
1585 return expr; 1638 return expr;
1586 } else if (acceptCategory(HASH)) { 1639 } else if (acceptCategory(HASH)) {
1587 return parseInterpolatedExpression(); 1640 return parseInterpolatedExpression();
1588 } else { 1641 } else {
1589 error('Expected property name'); 1642 error('Expected property name');
1590 return null; 1643 return null;
1591 } 1644 }
1592 } 1645 }
1593 } 1646 }
OLDNEW
« no previous file with comments | « lib/src/codegen/js_codegen.dart ('k') | lib/src/js/nodes.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698