Chromium Code Reviews| 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 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 322 } | 322 } |
| 323 | 323 |
| 324 /// Creates a literal js string from [value]. | 324 /// Creates a literal js string from [value]. |
| 325 /// | 325 /// |
| 326 /// Note that this function only puts quotes around [value]. It does not do | 326 /// Note that this function only puts quotes around [value]. It does not do |
| 327 /// any escaping, so use only when you can guarantee that [value] does not | 327 /// any escaping, so use only when you can guarantee that [value] does not |
| 328 /// contain newlines or backslashes. For escaping the string use | 328 /// contain newlines or backslashes. For escaping the string use |
| 329 /// [escapedString]. | 329 /// [escapedString]. |
| 330 LiteralString string(String value) => new LiteralString('"$value"'); | 330 LiteralString string(String value) => new LiteralString('"$value"'); |
| 331 | 331 |
| 332 /// Creates an instance of [LiteralString] from [value]. | |
| 333 /// | |
| 334 /// Does not add quotes or do any escaping. | |
| 335 LiteralString stringPart(String value) => new LiteralString(value); | |
| 336 | |
| 337 StringConcatenation concatenateStrings(Iterable<Literal> parts, | |
| 338 {addQuotes: false}) { | |
| 339 List<Literal> _parts; | |
| 340 if (addQuotes) { | |
| 341 _parts = <Literal>[stringPart('"')] | |
|
sra1
2015/05/27 20:30:15
This stringPart can be shared between begining and
herhut
2015/06/01 12:09:43
I share them locally for now.
| |
| 342 ..addAll(parts) | |
| 343 ..add(stringPart('"')); | |
| 344 } else { | |
| 345 _parts = new List.from(parts, growable: false); | |
| 346 } | |
| 347 return new StringConcatenation(_parts); | |
| 348 } | |
| 349 | |
| 350 Iterable<Literal> joinLiterals(Iterable<Literal> list, Literal separator) { | |
| 351 return new _InterleaveIterable(list, separator); | |
| 352 } | |
| 353 | |
| 332 LiteralNumber number(num value) => new LiteralNumber('$value'); | 354 LiteralNumber number(num value) => new LiteralNumber('$value'); |
| 333 | 355 |
| 334 LiteralBool boolean(bool value) => new LiteralBool(value); | 356 LiteralBool boolean(bool value) => new LiteralBool(value); |
| 335 | 357 |
| 336 ArrayInitializer numArray(Iterable<int> list) => | 358 ArrayInitializer numArray(Iterable<int> list) => |
| 337 new ArrayInitializer(list.map(number).toList()); | 359 new ArrayInitializer(list.map(number).toList()); |
| 338 | 360 |
| 339 ArrayInitializer stringArray(Iterable<String> list) => | 361 ArrayInitializer stringArray(Iterable<String> list) => |
| 340 new ArrayInitializer(list.map(string).toList()); | 362 new ArrayInitializer(list.map(string).toList()); |
| 341 | 363 |
| 342 Comment comment(String text) => new Comment(text); | 364 Comment comment(String text) => new Comment(text); |
| 343 | 365 |
| 344 Call propertyCall(Expression receiver, | 366 Call propertyCall(Expression receiver, |
| 345 String fieldName, | 367 String fieldName, |
| 346 List<Expression> arguments) { | 368 List<Expression> arguments) { |
| 347 return new Call(new PropertyAccess.field(receiver, fieldName), arguments); | 369 return new Call(new PropertyAccess.field(receiver, fieldName), arguments); |
| 348 } | 370 } |
| 349 } | 371 } |
| 350 | 372 |
| 351 LiteralString string(String value) => js.string(value); | 373 LiteralString string(String value) => js.string(value); |
| 374 LiteralString stringPart(String value) => js.stringPart(value); | |
| 375 Iterable<Literal> joinLiterals(Iterable<Literal> list, Literal separator) { | |
| 376 return js.joinLiterals(list, separator); | |
| 377 } | |
| 352 LiteralNumber number(num value) => js.number(value); | 378 LiteralNumber number(num value) => js.number(value); |
| 353 ArrayInitializer numArray(Iterable<int> list) => js.numArray(list); | 379 ArrayInitializer numArray(Iterable<int> list) => js.numArray(list); |
| 354 ArrayInitializer stringArray(Iterable<String> list) => js.stringArray(list); | 380 ArrayInitializer stringArray(Iterable<String> list) => js.stringArray(list); |
| 355 Call propertyCall(Expression receiver, | 381 Call propertyCall(Expression receiver, |
| 356 String fieldName, | 382 String fieldName, |
| 357 List<Expression> arguments) { | 383 List<Expression> arguments) { |
| 358 return js.propertyCall(receiver, fieldName, arguments); | 384 return js.propertyCall(receiver, fieldName, arguments); |
| 359 } | 385 } |
| 360 | 386 |
| 361 class MiniJsParserError { | 387 class MiniJsParserError { |
| (...skipping 403 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 765 new InterpolatedExpression(nameOrPosition); | 791 new InterpolatedExpression(nameOrPosition); |
| 766 interpolatedValues.add(expression); | 792 interpolatedValues.add(expression); |
| 767 return expression; | 793 return expression; |
| 768 } else { | 794 } else { |
| 769 error("Expected primary expression"); | 795 error("Expected primary expression"); |
| 770 return null; | 796 return null; |
| 771 } | 797 } |
| 772 } | 798 } |
| 773 | 799 |
| 774 Expression parseFunctionExpression() { | 800 Expression parseFunctionExpression() { |
| 775 String last = lastToken; | |
| 776 if (lastCategory == ALPHA || lastCategory == HASH) { | 801 if (lastCategory == ALPHA || lastCategory == HASH) { |
| 777 Declaration name = parseVariableDeclaration(); | 802 Declaration name = parseVariableDeclaration(); |
| 778 return new NamedFunction(name, parseFun()); | 803 return new NamedFunction(name, parseFun()); |
| 779 } | 804 } |
| 780 return parseFun(); | 805 return parseFun(); |
| 781 } | 806 } |
| 782 | 807 |
| 783 Expression parseFun() { | 808 Expression parseFun() { |
| 784 List<Parameter> params = <Parameter>[]; | 809 List<Parameter> params = <Parameter>[]; |
| 785 | 810 |
| (...skipping 479 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1265 | 1290 |
| 1266 Statement parseFunctionDeclaration() { | 1291 Statement parseFunctionDeclaration() { |
| 1267 Declaration name = parseVariableDeclaration(); | 1292 Declaration name = parseVariableDeclaration(); |
| 1268 Expression fun = parseFun(); | 1293 Expression fun = parseFun(); |
| 1269 return new FunctionDeclaration(name, fun); | 1294 return new FunctionDeclaration(name, fun); |
| 1270 } | 1295 } |
| 1271 | 1296 |
| 1272 Statement parseTry() { | 1297 Statement parseTry() { |
| 1273 expectCategory(LBRACE); | 1298 expectCategory(LBRACE); |
| 1274 Block body = parseBlock(); | 1299 Block body = parseBlock(); |
| 1275 String token = lastToken; | |
| 1276 Catch catchPart = null; | 1300 Catch catchPart = null; |
| 1277 if (acceptString('catch')) catchPart = parseCatch(); | 1301 if (acceptString('catch')) catchPart = parseCatch(); |
| 1278 Block finallyPart = null; | 1302 Block finallyPart = null; |
| 1279 if (acceptString('finally')) { | 1303 if (acceptString('finally')) { |
| 1280 expectCategory(LBRACE); | 1304 expectCategory(LBRACE); |
| 1281 finallyPart = parseBlock(); | 1305 finallyPart = parseBlock(); |
| 1282 } else { | 1306 } else { |
| 1283 if (catchPart == null) error("expected 'finally'"); | 1307 if (catchPart == null) error("expected 'finally'"); |
| 1284 } | 1308 } |
| 1285 return new Try(body, catchPart, finallyPart); | 1309 return new Try(body, catchPart, finallyPart); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1341 | 1365 |
| 1342 Catch parseCatch() { | 1366 Catch parseCatch() { |
| 1343 expectCategory(LPAREN); | 1367 expectCategory(LPAREN); |
| 1344 Declaration errorName = parseVariableDeclaration(); | 1368 Declaration errorName = parseVariableDeclaration(); |
| 1345 expectCategory(RPAREN); | 1369 expectCategory(RPAREN); |
| 1346 expectCategory(LBRACE); | 1370 expectCategory(LBRACE); |
| 1347 Block body = parseBlock(); | 1371 Block body = parseBlock(); |
| 1348 return new Catch(errorName, body); | 1372 return new Catch(errorName, body); |
| 1349 } | 1373 } |
| 1350 } | 1374 } |
| 1375 | |
| 1376 class _InterleaveIterator implements Iterator<Node> { | |
| 1377 Iterator<Node> source; | |
| 1378 Node separator; | |
| 1379 bool isNextSeparator = false; | |
| 1380 bool isInitialized = false; | |
| 1381 | |
| 1382 _InterleaveIterator(this.source, this.separator); | |
| 1383 | |
| 1384 bool moveNext() { | |
| 1385 if (!isInitialized) { | |
| 1386 isInitialized = true; | |
| 1387 return source.moveNext(); | |
| 1388 } else if (isNextSeparator) { | |
| 1389 isNextSeparator = false; | |
| 1390 return true; | |
| 1391 } else { | |
| 1392 return isNextSeparator = source.moveNext(); | |
| 1393 } | |
| 1394 } | |
| 1395 | |
| 1396 Node get current { | |
| 1397 if (isNextSeparator) return separator._clone(); | |
| 1398 return source.current; | |
| 1399 } | |
| 1400 } | |
| 1401 | |
| 1402 class _InterleaveIterable extends IterableBase { | |
| 1403 Iterable<Node> source; | |
| 1404 Node separator; | |
| 1405 | |
| 1406 _InterleaveIterable(this.source, this.separator); | |
| 1407 | |
| 1408 Iterator<Node> get iterator { | |
| 1409 return new _InterleaveIterator(source.iterator, separator); | |
| 1410 } | |
| 1411 } | |
| OLD | NEW |