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 class JsBuilder { | 10 class JsBuilder { |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 38 Expression parseForeignJS(String source, [var expression]) { | 38 Expression parseForeignJS(String source, [var expression]) { |
| 39 // We can parse simple JS with the mini parser. At the moment we can't | 39 // We can parse simple JS with the mini parser. At the moment we can't |
| 40 // handle JSON literals and function literals, both of which contain "{". | 40 // handle JSON literals and function literals, both of which contain "{". |
| 41 if (source.contains("{") || source.startsWith("throw ")) { | 41 if (source.contains("{") || source.startsWith("throw ")) { |
| 42 assert(expression == null); | 42 assert(expression == null); |
| 43 return new LiteralExpression(source); | 43 return new LiteralExpression(source); |
| 44 } | 44 } |
| 45 return call(source, expression); | 45 return call(source, expression); |
| 46 } | 46 } |
| 47 | 47 |
| 48 /// Creates a litteral js string from [value]. | |
| 49 LiteralString escapedString(String value) { | |
| 50 // Do not escape unicode characters and ' because they are allowed in the | |
| 51 // string literal anyway. | |
| 52 String escaped = | |
| 53 value.replaceAllMapped(new RegExp('\n|"|\\|\0|\b|\t|\v'), (match) { | |
| 54 switch (match.group(0)) { | |
| 55 case "\n" : return r"\n"; | |
| 56 case "\\" : return r"\\"; | |
| 57 case "\"" : return r'\"'; | |
| 58 case "\0" : return r"\0"; | |
| 59 case "\b" : return r"\b"; | |
| 60 case "\t" : return r"\t"; | |
| 61 case "\f" : return r"\f"; | |
| 62 case "\v" : return r"\v"; | |
| 63 } | |
| 64 }); | |
| 65 LiteralString result = string(escaped); | |
| 66 // We don't escape ' under the assumption that the string is wrapped | |
| 67 // into ". Verify that assumption. | |
| 68 assert(result.value.codeUnitAt(0) == '"'.codeUnitAt(0)); | |
|
floitsch
2014/03/06 15:25:02
since this is an assert.
assert(result.value[0] ==
| |
| 69 return result; | |
| 70 } | |
| 71 | |
| 72 /// Creates a litteral js string from [value]. | |
| 73 /// | |
| 74 /// Note that this function only puts quotes around [value]. It does not do | |
| 75 /// any escaping, so use only when you can guarantee that [value] does not | |
| 76 /// contain newlines or backslashes. For escaping the string use | |
| 77 /// [escapedString]. | |
| 48 LiteralString string(String value) => new LiteralString('"$value"'); | 78 LiteralString string(String value) => new LiteralString('"$value"'); |
| 49 | 79 |
| 50 LiteralNumber number(num value) => new LiteralNumber('$value'); | 80 LiteralNumber number(num value) => new LiteralNumber('$value'); |
| 51 | 81 |
| 52 If if_(condition, thenPart, [elsePart]) { | 82 If if_(condition, thenPart, [elsePart]) { |
| 53 condition = toExpression(condition); | 83 condition = toExpression(condition); |
| 54 return (elsePart == null) | 84 return (elsePart == null) |
| 55 ? new If.noElse(condition, toStatement(thenPart)) | 85 ? new If.noElse(condition, toStatement(thenPart)) |
| 56 : new If(condition, toStatement(thenPart), toStatement(elsePart)); | 86 : new If(condition, toStatement(thenPart), toStatement(elsePart)); |
| 57 } | 87 } |
| (...skipping 735 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 793 } | 823 } |
| 794 | 824 |
| 795 Node visitLiteralNull(LiteralNull node) { | 825 Node visitLiteralNull(LiteralNull node) { |
| 796 return node; | 826 return node; |
| 797 } | 827 } |
| 798 | 828 |
| 799 Node visitLiteralBool(LiteralBool node) { | 829 Node visitLiteralBool(LiteralBool node) { |
| 800 return node; | 830 return node; |
| 801 } | 831 } |
| 802 } | 832 } |
| OLD | NEW |