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 | 9 |
9 part 'link.dart'; | 10 part 'link.dart'; |
10 | 11 |
11 /** | 12 /** |
12 * Tagging interface for classes from which source spans can be generated. | 13 * Tagging interface for classes from which source spans can be generated. |
13 */ | 14 */ |
14 // TODO(johnniwinther): Find a better name. | 15 // TODO(johnniwinther): Find a better name. |
15 // TODO(ahe): How about "Bolt"? | 16 // TODO(ahe): How about "Bolt"? |
16 abstract class Spannable {} | 17 abstract class Spannable {} |
17 | 18 |
18 class SpannableAssertionFailure { | 19 class SpannableAssertionFailure { |
19 final Spannable node; | 20 final Spannable node; |
20 final String message; | 21 final String message; |
21 SpannableAssertionFailure(this.node, this.message); | 22 SpannableAssertionFailure(this.node, this.message); |
22 | 23 |
23 String toString() => 'compiler crashed.'; | 24 String toString() => 'compiler crashed.'; |
24 } | 25 } |
| 26 |
| 27 /// Writes the characters of [iterator] on [buffer]. The characters |
| 28 /// are escaped as suitable for JavaScript and JSON. [buffer] is |
| 29 /// anything which supports [:add:] and [:addCharCode:], for example, |
| 30 /// [StringBuffer]. |
| 31 void writeJsonEscapedCharsOn(Iterator<int> iterator, buffer, onError(code)) { |
| 32 while (iterator.hasNext) { |
| 33 int code = iterator.next(); |
| 34 if (identical(code, $SQ)) { |
| 35 buffer.add(r"\'"); |
| 36 } else if (identical(code, $LF)) { |
| 37 buffer.add(r'\n'); |
| 38 } else if (identical(code, $CR)) { |
| 39 buffer.add(r'\r'); |
| 40 } else if (identical(code, $LS)) { |
| 41 // This Unicode line terminator and $PS are invalid in JS string |
| 42 // literals. |
| 43 buffer.add(r'\u2028'); |
| 44 } else if (identical(code, $PS)) { |
| 45 buffer.add(r'\u2029'); |
| 46 } else if (identical(code, $BACKSLASH)) { |
| 47 buffer.add(r'\\'); |
| 48 } else { |
| 49 if (code > 0xffff) { |
| 50 if (onError != null) onError(code); |
| 51 throw 'Unhandled non-BMP character: ${code.toRadixString(16)}'; |
| 52 } |
| 53 // TODO(lrn): Consider whether all codes above 0x7f really need to |
| 54 // be escaped. We build a Dart string here, so it should be a literal |
| 55 // stage that converts it to, e.g., UTF-8 for a JS interpreter. |
| 56 if (code < 0x20) { |
| 57 buffer.add(r'\x'); |
| 58 if (code < 0x10) buffer.add('0'); |
| 59 buffer.add(code.toRadixString(16)); |
| 60 } else if (code >= 0x80) { |
| 61 if (code < 0x100) { |
| 62 buffer.add(r'\x'); |
| 63 } else { |
| 64 buffer.add(r'\u'); |
| 65 if (code < 0x1000) { |
| 66 buffer.add('0'); |
| 67 } |
| 68 } |
| 69 buffer.add(code.toRadixString(16)); |
| 70 } else { |
| 71 buffer.addCharCode(code); |
| 72 } |
| 73 } |
| 74 } |
| 75 } |
OLD | NEW |