OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 library org_dartlang_compiler_util; | 5 library org_dartlang_compiler_util; |
6 | 6 |
7 import 'util_implementation.dart'; | 7 import 'util_implementation.dart'; |
8 import 'characters.dart'; | 8 import 'characters.dart'; |
9 | 9 |
10 part 'link.dart'; | 10 part 'link.dart'; |
(...skipping 19 matching lines...) Expand all Loading... |
30 class SpannableAssertionFailure { | 30 class SpannableAssertionFailure { |
31 final Spannable node; | 31 final Spannable node; |
32 final String message; | 32 final String message; |
33 SpannableAssertionFailure(this.node, this.message); | 33 SpannableAssertionFailure(this.node, this.message); |
34 | 34 |
35 String toString() => 'Compiler crashed: $message.'; | 35 String toString() => 'Compiler crashed: $message.'; |
36 } | 36 } |
37 | 37 |
38 /// Writes the characters of [string] on [buffer]. The characters | 38 /// Writes the characters of [string] on [buffer]. The characters |
39 /// are escaped as suitable for JavaScript and JSON. [buffer] is | 39 /// are escaped as suitable for JavaScript and JSON. [buffer] is |
40 /// anything which supports [:add:] and [:addCharCode:], for example, | 40 /// anything which supports [:write:] and [:writeCharCode:], for example, |
41 /// [StringBuffer]. Note that JS supports \xnn and \unnnn whereas JSON only | 41 /// [StringBuffer]. Note that JS supports \xnn and \unnnn whereas JSON only |
42 /// supports the \unnnn notation. Therefore we use the \unnnn notation. | 42 /// supports the \unnnn notation. Therefore we use the \unnnn notation. |
43 void writeJsonEscapedCharsOn(String string, buffer) { | 43 void writeJsonEscapedCharsOn(String string, buffer) { |
44 void addCodeUnitEscaped(var buffer, int code) { | 44 void addCodeUnitEscaped(var buffer, int code) { |
45 assert(code < 0x10000); | 45 assert(code < 0x10000); |
46 buffer.add(r'\u'); | 46 buffer.write(r'\u'); |
47 if (code < 0x1000) { | 47 if (code < 0x1000) { |
48 buffer.add('0'); | 48 buffer.write('0'); |
49 if (code < 0x100) { | 49 if (code < 0x100) { |
50 buffer.add('0'); | 50 buffer.write('0'); |
51 if (code < 0x10) { | 51 if (code < 0x10) { |
52 buffer.add('0'); | 52 buffer.write('0'); |
53 } | 53 } |
54 } | 54 } |
55 } | 55 } |
56 buffer.add(code.toRadixString(16)); | 56 buffer.write(code.toRadixString(16)); |
57 } | 57 } |
58 | 58 |
59 void writeEscapedOn(String string, var buffer) { | 59 void writeEscapedOn(String string, var buffer) { |
60 for (int i = 0; i < string.length; i++) { | 60 for (int i = 0; i < string.length; i++) { |
61 int code = string.codeUnitAt(i); | 61 int code = string.codeUnitAt(i); |
62 if (code == $DQ) { | 62 if (code == $DQ) { |
63 buffer.add(r'\"'); | 63 buffer.write(r'\"'); |
64 } else if (code == $TAB) { | 64 } else if (code == $TAB) { |
65 buffer.add(r'\t'); | 65 buffer.write(r'\t'); |
66 } else if (code == $LF) { | 66 } else if (code == $LF) { |
67 buffer.add(r'\n'); | 67 buffer.write(r'\n'); |
68 } else if (code == $CR) { | 68 } else if (code == $CR) { |
69 buffer.add(r'\r'); | 69 buffer.write(r'\r'); |
70 } else if (code == $DEL) { | 70 } else if (code == $DEL) { |
71 addCodeUnitEscaped(buffer, $DEL); | 71 addCodeUnitEscaped(buffer, $DEL); |
72 } else if (code == $LS) { | 72 } else if (code == $LS) { |
73 // This Unicode line terminator and $PS are invalid in JS string | 73 // This Unicode line terminator and $PS are invalid in JS string |
74 // literals. | 74 // literals. |
75 addCodeUnitEscaped(buffer, $LS); // 0x2028. | 75 addCodeUnitEscaped(buffer, $LS); // 0x2028. |
76 } else if (code == $PS) { | 76 } else if (code == $PS) { |
77 addCodeUnitEscaped(buffer, $PS); // 0x2029. | 77 addCodeUnitEscaped(buffer, $PS); // 0x2029. |
78 } else if (code == $BACKSLASH) { | 78 } else if (code == $BACKSLASH) { |
79 buffer.add(r'\\'); | 79 buffer.write(r'\\'); |
80 } else { | 80 } else { |
81 if (code < 0x20) { | 81 if (code < 0x20) { |
82 addCodeUnitEscaped(buffer, code); | 82 addCodeUnitEscaped(buffer, code); |
83 // We emit DEL (ASCII 0x7f) as an escape because it would be confusing | 83 // We emit DEL (ASCII 0x7f) as an escape because it would be confusing |
84 // to have it unescaped in a string literal. We also escape | 84 // to have it unescaped in a string literal. We also escape |
85 // everything above 0x7f because that means we don't have to worry | 85 // everything above 0x7f because that means we don't have to worry |
86 // about whether the web server serves it up as Latin1 or UTF-8. | 86 // about whether the web server serves it up as Latin1 or UTF-8. |
87 } else if (code < 0x7f) { | 87 } else if (code < 0x7f) { |
88 buffer.addCharCode(code); | 88 buffer.writeCharCode(code); |
89 } else { | 89 } else { |
90 // This will output surrogate pairs in the form \udxxx\udyyy, rather | 90 // This will output surrogate pairs in the form \udxxx\udyyy, rather |
91 // than the more logical \u{zzzzzz}. This should work in JavaScript | 91 // than the more logical \u{zzzzzz}. This should work in JavaScript |
92 // (especially old UCS-2 based implementations) and is the only | 92 // (especially old UCS-2 based implementations) and is the only |
93 // format that is allowed in JSON. | 93 // format that is allowed in JSON. |
94 addCodeUnitEscaped(buffer, code); | 94 addCodeUnitEscaped(buffer, code); |
95 } | 95 } |
96 } | 96 } |
97 } | 97 } |
98 } | 98 } |
99 | 99 |
100 for (int i = 0; i < string.length; i++) { | 100 for (int i = 0; i < string.length; i++) { |
101 int code = string.codeUnitAt(i); | 101 int code = string.codeUnitAt(i); |
102 if (code < 0x20 || code == $DEL || code == $DQ || code == $LS || | 102 if (code < 0x20 || code == $DEL || code == $DQ || code == $LS || |
103 code == $PS || code == $BACKSLASH || code >= 0x80) { | 103 code == $PS || code == $BACKSLASH || code >= 0x80) { |
104 writeEscapedOn(string, buffer); | 104 writeEscapedOn(string, buffer); |
105 return; | 105 return; |
106 } | 106 } |
107 } | 107 } |
108 buffer.add(string); | 108 buffer.write(string); |
109 } | 109 } |
OLD | NEW |