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; | 8 part of js; |
| 9 | 9 |
| 10 Expression js(String source) { | |
|
ahe
2013/04/05 13:47:20
You could also have JsBuilder implement Function.
| |
| 11 return new MiniJsParser(source).expression(); | |
| 12 } | |
| 13 | |
| 10 class JsBuilder { | 14 class JsBuilder { |
| 11 const JsBuilder(); | 15 const JsBuilder(); |
| 12 | 16 |
| 13 Expression operator [](String source) { | |
| 14 return new MiniJsParser(source).expression(); | |
| 15 } | |
| 16 | |
| 17 // TODO(ahe): Remove this method. | |
| 18 Binary equals(Expression left, Expression right) { | |
| 19 return new Binary('==', left, right); | |
| 20 } | |
| 21 | |
| 22 // TODO(ahe): Remove this method. | |
| 23 Binary strictEquals(Expression left, Expression right) { | |
| 24 return new Binary('===', left, right); | |
| 25 } | |
| 26 | |
| 27 LiteralString string(String value) => new LiteralString('"$value"'); | 17 LiteralString string(String value) => new LiteralString('"$value"'); |
| 28 | 18 |
| 29 If if_(condition, thenPart, [elsePart]) { | 19 If if_(condition, thenPart, [elsePart]) { |
| 30 condition = toExpression(condition); | 20 condition = toExpression(condition); |
| 31 return (elsePart == null) | 21 return (elsePart == null) |
| 32 ? new If.noElse(condition, toStatement(thenPart)) | 22 ? new If.noElse(condition, toStatement(thenPart)) |
| 33 : new If(condition, toStatement(thenPart), toStatement(elsePart)); | 23 : new If(condition, toStatement(thenPart), toStatement(elsePart)); |
| 34 } | 24 } |
| 35 | 25 |
| 36 Return return_([value]) { | 26 Return return_([value]) { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 85 return statement.toStatement(); | 75 return statement.toStatement(); |
| 86 } else { | 76 } else { |
| 87 throw new ArgumentError('statement'); | 77 throw new ArgumentError('statement'); |
| 88 } | 78 } |
| 89 } | 79 } |
| 90 | 80 |
| 91 Expression toExpression(expression) { | 81 Expression toExpression(expression) { |
| 92 if (expression is Expression) { | 82 if (expression is Expression) { |
| 93 return expression; | 83 return expression; |
| 94 } else if (expression is String) { | 84 } else if (expression is String) { |
| 95 return this[expression]; | 85 return js(expression); |
| 96 } else if (expression is num) { | 86 } else if (expression is num) { |
| 97 return new LiteralNumber('$expression'); | 87 return new LiteralNumber('$expression'); |
| 98 } else if (expression is bool) { | 88 } else if (expression is bool) { |
| 99 return new LiteralBool(expression); | 89 return new LiteralBool(expression); |
| 100 } else if (expression is Map) { | 90 } else if (expression is Map) { |
| 101 if (!expression.isEmpty) { | 91 if (!expression.isEmpty) { |
| 102 throw new ArgumentError('expression should be an empty Map'); | 92 throw new ArgumentError('expression should be an empty Map'); |
| 103 } | 93 } |
| 104 return new ObjectInitializer([]); | 94 return new ObjectInitializer([]); |
| 105 } else { | 95 } else { |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 127 | 117 |
| 128 Try try_(body, {catchPart, finallyPart}) { | 118 Try try_(body, {catchPart, finallyPart}) { |
| 129 if (catchPart != null) catchPart = toStatement(catchPart); | 119 if (catchPart != null) catchPart = toStatement(catchPart); |
| 130 if (finallyPart != null) finallyPart = toStatement(finallyPart); | 120 if (finallyPart != null) finallyPart = toStatement(finallyPart); |
| 131 return new Try(toStatement(body), catchPart, finallyPart); | 121 return new Try(toStatement(body), catchPart, finallyPart); |
| 132 } | 122 } |
| 133 | 123 |
| 134 Comment comment(String text) => new Comment(text); | 124 Comment comment(String text) => new Comment(text); |
| 135 } | 125 } |
| 136 | 126 |
| 137 const JsBuilder js = const JsBuilder(); | 127 const JsBuilder jsBuilder = const JsBuilder(); |
| 138 | 128 |
| 139 LiteralString string(String value) => js.string(value); | 129 LiteralString string(String value) => jsBuilder.string(value); |
| 140 | 130 |
| 141 class MiniJsParserError { | 131 class MiniJsParserError { |
| 142 MiniJsParserError(this.parser, this.message) { } | 132 MiniJsParserError(this.parser, this.message) { } |
| 143 | 133 |
| 144 MiniJsParser parser; | 134 MiniJsParser parser; |
| 145 String message; | 135 String message; |
| 146 | 136 |
| 147 String toString() { | 137 String toString() { |
| 148 var codes = new List.filled(parser.lastPosition, charCodes.$SPACE); | 138 var codes = new List.filled(parser.lastPosition, charCodes.$SPACE); |
| 149 var spaces = new String.fromCharCodes(codes); | 139 var spaces = new String.fromCharCodes(codes); |
| (...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 548 | 538 |
| 549 Expression expression() { | 539 Expression expression() { |
| 550 Expression expression = parseVarDeclarationOrExpression(); | 540 Expression expression = parseVarDeclarationOrExpression(); |
| 551 if (lastCategory != NONE || position != src.length) { | 541 if (lastCategory != NONE || position != src.length) { |
| 552 throw new MiniJsParserError( | 542 throw new MiniJsParserError( |
| 553 this, "Unparsed junk: ${categoryToString(lastCategory)}"); | 543 this, "Unparsed junk: ${categoryToString(lastCategory)}"); |
| 554 } | 544 } |
| 555 return expression; | 545 return expression; |
| 556 } | 546 } |
| 557 } | 547 } |
| OLD | NEW |