| 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 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 | 177 |
| 178 js('{#:1, #:2}', [s, 'bye']) --> {hello: 1, bye: 2} | 178 js('{#:1, #:2}', [s, 'bye']) --> {hello: 1, bye: 2} |
| 179 | 179 |
| 180 | 180 |
| 181 What is not implemented: | 181 What is not implemented: |
| 182 | 182 |
| 183 - Array initializers and object initializers could support splicing. In the | 183 - Array initializers and object initializers could support splicing. In the |
| 184 array case, we would need some way to know if an ArrayInitializer argument | 184 array case, we would need some way to know if an ArrayInitializer argument |
| 185 should be splice or is intended as a single value. | 185 should be splice or is intended as a single value. |
| 186 | 186 |
| 187 - There are no placeholders in definition contexts: | |
| 188 | |
| 189 function #(){} | |
| 190 var # = 1; | |
| 191 | |
| 192 */ | 187 */ |
| 193 const JsBuilder js = const JsBuilder(); | 188 const JsBuilder js = const JsBuilder(); |
| 194 | 189 |
| 195 | 190 |
| 196 class JsBuilder { | 191 class JsBuilder { |
| 197 const JsBuilder(); | 192 const JsBuilder(); |
| 198 | 193 |
| 199 /** | 194 /** |
| 200 * Parses a bit of JavaScript, and returns an expression. | 195 * Parses a bit of JavaScript, and returns an expression. |
| 201 * | 196 * |
| (...skipping 557 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 759 interpolatedValues.add(expression); | 754 interpolatedValues.add(expression); |
| 760 return expression; | 755 return expression; |
| 761 } else { | 756 } else { |
| 762 error("Expected primary expression"); | 757 error("Expected primary expression"); |
| 763 return null; | 758 return null; |
| 764 } | 759 } |
| 765 } | 760 } |
| 766 | 761 |
| 767 Expression parseFunctionExpression() { | 762 Expression parseFunctionExpression() { |
| 768 String last = lastToken; | 763 String last = lastToken; |
| 769 if (acceptCategory(ALPHA)) { | 764 if (lastCategory == ALPHA || lastCategory == HASH) { |
| 770 String functionName = last; | 765 Declaration name = parseVariableDeclaration(); |
| 771 return new NamedFunction(new VariableDeclaration(functionName), | 766 return new NamedFunction(name, parseFun()); |
| 772 parseFun()); | |
| 773 } | 767 } |
| 774 return parseFun(); | 768 return parseFun(); |
| 775 } | 769 } |
| 776 | 770 |
| 777 Expression parseFun() { | 771 Expression parseFun() { |
| 778 List<Parameter> params = <Parameter>[]; | 772 List<Parameter> params = <Parameter>[]; |
| 779 | 773 |
| 780 expectCategory(LPAREN); | 774 expectCategory(LPAREN); |
| 781 if (!acceptCategory(RPAREN)) { | 775 if (!acceptCategory(RPAREN)) { |
| 782 for (;;) { | 776 for (;;) { |
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1008 Expression parseExpression() { | 1002 Expression parseExpression() { |
| 1009 Expression expression = parseAssignment(); | 1003 Expression expression = parseAssignment(); |
| 1010 while (acceptCategory(COMMA)) { | 1004 while (acceptCategory(COMMA)) { |
| 1011 Expression right = parseAssignment(); | 1005 Expression right = parseAssignment(); |
| 1012 expression = new Binary(',', expression, right); | 1006 expression = new Binary(',', expression, right); |
| 1013 } | 1007 } |
| 1014 return expression; | 1008 return expression; |
| 1015 } | 1009 } |
| 1016 | 1010 |
| 1017 VariableDeclarationList parseVariableDeclarationList() { | 1011 VariableDeclarationList parseVariableDeclarationList() { |
| 1018 String firstVariable = lastToken; | 1012 Declaration firstVariable = parseVariableDeclaration(); |
| 1019 expectCategory(ALPHA); | |
| 1020 return finishVariableDeclarationList(firstVariable); | 1013 return finishVariableDeclarationList(firstVariable); |
| 1021 } | 1014 } |
| 1022 | 1015 |
| 1023 VariableDeclarationList finishVariableDeclarationList(String firstVariable) { | 1016 VariableDeclarationList finishVariableDeclarationList( |
| 1017 Declaration firstVariable) { |
| 1024 var initialization = []; | 1018 var initialization = []; |
| 1025 | 1019 |
| 1026 void declare(String variable) { | 1020 void declare(Declaration declaration) { |
| 1027 Expression initializer = null; | 1021 Expression initializer = null; |
| 1028 if (acceptString("=")) { | 1022 if (acceptString("=")) { |
| 1029 initializer = parseAssignment(); | 1023 initializer = parseAssignment(); |
| 1030 } | 1024 } |
| 1031 var declaration = new VariableDeclaration(variable); | |
| 1032 initialization.add(new VariableInitialization(declaration, initializer)); | 1025 initialization.add(new VariableInitialization(declaration, initializer)); |
| 1033 } | 1026 } |
| 1034 | 1027 |
| 1035 declare(firstVariable); | 1028 declare(firstVariable); |
| 1036 while (acceptCategory(COMMA)) { | 1029 while (acceptCategory(COMMA)) { |
| 1037 String variable = lastToken; | 1030 Declaration variable = parseVariableDeclaration(); |
| 1038 expectCategory(ALPHA); | |
| 1039 declare(variable); | 1031 declare(variable); |
| 1040 } | 1032 } |
| 1041 return new VariableDeclarationList(initialization); | 1033 return new VariableDeclarationList(initialization); |
| 1042 } | 1034 } |
| 1043 | 1035 |
| 1044 Expression parseVarDeclarationOrExpression() { | 1036 Expression parseVarDeclarationOrExpression() { |
| 1045 if (acceptString("var")) { | 1037 if (acceptString("var")) { |
| 1046 return parseVariableDeclarationList(); | 1038 return parseVariableDeclarationList(); |
| 1047 } else { | 1039 } else { |
| 1048 return parseExpression(); | 1040 return parseExpression(); |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1217 Statement body = parseStatement(); | 1209 Statement body = parseStatement(); |
| 1218 return new For(init, condition, update, body); | 1210 return new For(init, condition, update, body); |
| 1219 } | 1211 } |
| 1220 | 1212 |
| 1221 expectCategory(LPAREN); | 1213 expectCategory(LPAREN); |
| 1222 if (acceptCategory(SEMICOLON)) { | 1214 if (acceptCategory(SEMICOLON)) { |
| 1223 return finishFor(null); | 1215 return finishFor(null); |
| 1224 } | 1216 } |
| 1225 | 1217 |
| 1226 if (acceptString('var')) { | 1218 if (acceptString('var')) { |
| 1227 String identifier = lastToken; | 1219 Declaration declaration = parseVariableDeclaration(); |
| 1228 expectCategory(ALPHA); | |
| 1229 if (acceptString('in')) { | 1220 if (acceptString('in')) { |
| 1230 Expression objectExpression = parseExpression(); | 1221 Expression objectExpression = parseExpression(); |
| 1231 expectCategory(RPAREN); | 1222 expectCategory(RPAREN); |
| 1232 Statement body = parseStatement(); | 1223 Statement body = parseStatement(); |
| 1233 return new ForIn( | 1224 return new ForIn( |
| 1234 new VariableDeclarationList([ | 1225 new VariableDeclarationList([ |
| 1235 new VariableInitialization( | 1226 new VariableInitialization(declaration, null)]), |
| 1236 new VariableDeclaration(identifier), null)]), | |
| 1237 objectExpression, | 1227 objectExpression, |
| 1238 body); | 1228 body); |
| 1239 } | 1229 } |
| 1240 Expression declarations = finishVariableDeclarationList(identifier); | 1230 Expression declarations = finishVariableDeclarationList(declaration); |
| 1241 expectCategory(SEMICOLON); | 1231 expectCategory(SEMICOLON); |
| 1242 return finishFor(declarations); | 1232 return finishFor(declarations); |
| 1243 } | 1233 } |
| 1244 | 1234 |
| 1245 Expression init = parseExpression(); | 1235 Expression init = parseExpression(); |
| 1246 expectCategory(SEMICOLON); | 1236 expectCategory(SEMICOLON); |
| 1247 return finishFor(init); | 1237 return finishFor(init); |
| 1248 } | 1238 } |
| 1249 | 1239 |
| 1240 Declaration parseVariableDeclaration() { |
| 1241 if (acceptCategory(HASH)) { |
| 1242 var nameOrPosition = parseHash(); |
| 1243 InterpolatedDeclaration declaration = |
| 1244 new InterpolatedDeclaration(nameOrPosition); |
| 1245 interpolatedValues.add(declaration); |
| 1246 return declaration; |
| 1247 } else { |
| 1248 String token = lastToken; |
| 1249 expectCategory(ALPHA); |
| 1250 return new VariableDeclaration(token); |
| 1251 } |
| 1252 } |
| 1253 |
| 1250 Statement parseFunctionDeclaration() { | 1254 Statement parseFunctionDeclaration() { |
| 1251 String name = lastToken; | 1255 Declaration name = parseVariableDeclaration(); |
| 1252 expectCategory(ALPHA); | |
| 1253 Expression fun = parseFun(); | 1256 Expression fun = parseFun(); |
| 1254 return new FunctionDeclaration(new VariableDeclaration(name), fun); | 1257 return new FunctionDeclaration(name, fun); |
| 1255 } | 1258 } |
| 1256 | 1259 |
| 1257 Statement parseTry() { | 1260 Statement parseTry() { |
| 1258 expectCategory(LBRACE); | 1261 expectCategory(LBRACE); |
| 1259 Block body = parseBlock(); | 1262 Block body = parseBlock(); |
| 1260 String token = lastToken; | 1263 String token = lastToken; |
| 1261 Catch catchPart = null; | 1264 Catch catchPart = null; |
| 1262 if (acceptString('catch')) catchPart = parseCatch(); | 1265 if (acceptString('catch')) catchPart = parseCatch(); |
| 1263 Block finallyPart = null; | 1266 Block finallyPart = null; |
| 1264 if (acceptString('finally')) { | 1267 if (acceptString('finally')) { |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1319 List<SwitchClause> clauses = new List<SwitchClause>(); | 1322 List<SwitchClause> clauses = new List<SwitchClause>(); |
| 1320 while(lastCategory != RBRACE) { | 1323 while(lastCategory != RBRACE) { |
| 1321 clauses.add(parseSwitchClause()); | 1324 clauses.add(parseSwitchClause()); |
| 1322 } | 1325 } |
| 1323 expectCategory(RBRACE); | 1326 expectCategory(RBRACE); |
| 1324 return new Switch(key, clauses); | 1327 return new Switch(key, clauses); |
| 1325 } | 1328 } |
| 1326 | 1329 |
| 1327 Catch parseCatch() { | 1330 Catch parseCatch() { |
| 1328 expectCategory(LPAREN); | 1331 expectCategory(LPAREN); |
| 1329 String identifier = lastToken; | 1332 Declaration errorName = parseVariableDeclaration(); |
| 1330 expectCategory(ALPHA); | |
| 1331 expectCategory(RPAREN); | 1333 expectCategory(RPAREN); |
| 1332 expectCategory(LBRACE); | 1334 expectCategory(LBRACE); |
| 1333 Block body = parseBlock(); | 1335 Block body = parseBlock(); |
| 1334 return new Catch(new VariableDeclaration(identifier), body); | 1336 return new Catch(errorName, body); |
| 1335 } | 1337 } |
| 1336 } | 1338 } |
| OLD | NEW |