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 "\\n"; | |
|
floitsch
2014/02/28 12:58:15
use raw strings.
case "\n": return r"\n";
sigurdm
2014/03/03 13:18:49
Done.
| |
| 56 case "\\" : return "\\\\"; | |
| 57 case "\"" : return "\\\""; | |
| 58 case "\0" : return "\\0"; | |
| 59 case "\b" : return "\\b"; | |
| 60 case "\t" : return "\\t"; | |
| 61 case "\f" : return "\\f"; | |
| 62 case "\v" : return "\\v"; | |
| 63 } | |
| 64 }); | |
| 65 return new LiteralString('"$escaped"'); | |
|
floitsch
2014/02/28 12:58:15
LiteralString result = string(escaped);
// We don'
sigurdm
2014/03/03 13:18:49
Done.
sigurdm
2014/03/03 13:18:49
Done.
| |
| 66 } | |
| 67 | |
| 68 /// Creates a litteral js string from [value]. | |
| 69 /// | |
| 70 /// Note that this function only puts quotes around [value]. It does not do | |
| 71 /// any escaping, so use only when you can guarantee that [value] does not | |
| 72 /// contain newlines or backslashes. For escaping the string use | |
| 73 /// [escapedString]. | |
| 48 LiteralString string(String value) => new LiteralString('"$value"'); | 74 LiteralString string(String value) => new LiteralString('"$value"'); |
| 49 | 75 |
| 50 LiteralNumber number(num value) => new LiteralNumber('$value'); | 76 LiteralNumber number(num value) => new LiteralNumber('$value'); |
| 51 | 77 |
| 52 If if_(condition, thenPart, [elsePart]) { | 78 If if_(condition, thenPart, [elsePart]) { |
| 53 condition = toExpression(condition); | 79 condition = toExpression(condition); |
| 54 return (elsePart == null) | 80 return (elsePart == null) |
| 55 ? new If.noElse(condition, toStatement(thenPart)) | 81 ? new If.noElse(condition, toStatement(thenPart)) |
| 56 : new If(condition, toStatement(thenPart), toStatement(elsePart)); | 82 : new If(condition, toStatement(thenPart), toStatement(elsePart)); |
| 57 } | 83 } |
| (...skipping 735 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 793 } | 819 } |
| 794 | 820 |
| 795 Node visitLiteralNull(LiteralNull node) { | 821 Node visitLiteralNull(LiteralNull node) { |
| 796 return node; | 822 return node; |
| 797 } | 823 } |
| 798 | 824 |
| 799 Node visitLiteralBool(LiteralBool node) { | 825 Node visitLiteralBool(LiteralBool node) { |
| 800 return node; | 826 return node; |
| 801 } | 827 } |
| 802 } | 828 } |
| OLD | NEW |