| 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) == '"'); | |
| 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]. | |
| 78 LiteralString string(String value) => new LiteralString('"$value"'); | 48 LiteralString string(String value) => new LiteralString('"$value"'); |
| 79 | 49 |
| 80 LiteralNumber number(num value) => new LiteralNumber('$value'); | 50 LiteralNumber number(num value) => new LiteralNumber('$value'); |
| 81 | 51 |
| 82 If if_(condition, thenPart, [elsePart]) { | 52 If if_(condition, thenPart, [elsePart]) { |
| 83 condition = toExpression(condition); | 53 condition = toExpression(condition); |
| 84 return (elsePart == null) | 54 return (elsePart == null) |
| 85 ? new If.noElse(condition, toStatement(thenPart)) | 55 ? new If.noElse(condition, toStatement(thenPart)) |
| 86 : new If(condition, toStatement(thenPart), toStatement(elsePart)); | 56 : new If(condition, toStatement(thenPart), toStatement(elsePart)); |
| 87 } | 57 } |
| (...skipping 735 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 823 } | 793 } |
| 824 | 794 |
| 825 Node visitLiteralNull(LiteralNull node) { | 795 Node visitLiteralNull(LiteralNull node) { |
| 826 return node; | 796 return node; |
| 827 } | 797 } |
| 828 | 798 |
| 829 Node visitLiteralBool(LiteralBool node) { | 799 Node visitLiteralBool(LiteralBool node) { |
| 830 return node; | 800 return node; |
| 831 } | 801 } |
| 832 } | 802 } |
| OLD | NEW |