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 dart2js.scanner.string; | 5 library dart2js.scanner.string; |
6 | 6 |
7 import '../io/source_file.dart' show SourceFile; | 7 import 'array_based_scanner.dart' show |
8 import '../tokens/precedence.dart' show PrecedenceInfo; | 8 ArrayBasedScanner; |
9 import '../tokens/token.dart' show StringToken, Token; | 9 |
10 import 'array_based_scanner.dart' show ArrayBasedScanner; | 10 import 'precedence.dart' show |
| 11 PrecedenceInfo; |
| 12 |
| 13 import 'token.dart' show |
| 14 StringToken, |
| 15 Token; |
11 | 16 |
12 /** | 17 /** |
13 * Scanner that reads from a String and creates tokens that points to | 18 * Scanner that reads from a String and creates tokens that points to |
14 * substrings. | 19 * substrings. |
15 */ | 20 */ |
16 class StringScanner extends ArrayBasedScanner { | 21 class StringScanner extends ArrayBasedScanner { |
17 /** The file content. */ | 22 /** The file content. */ |
18 String string; | 23 String string; |
19 | 24 |
20 /** The current offset in [string]. */ | 25 /** The current offset in [string]. */ |
21 int scanOffset = -1; | 26 int scanOffset = -1; |
22 | 27 |
23 StringScanner(SourceFile file, {bool includeComments: false}) | 28 StringScanner(String string, {bool includeComments: false}) |
24 : string = file.slowText(), | 29 : string = ensureZeroTermination(string), |
25 super(file, includeComments) { | 30 super(includeComments); |
26 ensureZeroTermination(); | |
27 } | |
28 | 31 |
29 StringScanner.fromString(this.string, {bool includeComments: false}) | 32 static String ensureZeroTermination(String string) { |
30 : super(null, includeComments) { | 33 return (string.isEmpty || string.codeUnitAt(string.length - 1) != 0) |
31 ensureZeroTermination(); | 34 // TODO(lry): abort instead of copying the array, or warn? |
32 } | 35 ? string + '\x00' |
33 | 36 : string; |
34 void ensureZeroTermination() { | |
35 if (string.isEmpty || string.codeUnitAt(string.length - 1) != 0) { | |
36 // TODO(lry): abort instead of copying the array, or warn? | |
37 string = string + '\x00'; | |
38 } | |
39 } | 37 } |
40 | 38 |
41 int advance() => string.codeUnitAt(++scanOffset); | 39 int advance() => string.codeUnitAt(++scanOffset); |
42 int peek() => string.codeUnitAt(scanOffset + 1); | 40 int peek() => string.codeUnitAt(scanOffset + 1); |
43 | 41 |
44 int get stringOffset => scanOffset; | 42 int get stringOffset => scanOffset; |
45 | 43 |
46 int currentAsUnicode(int next) => next; | 44 int currentAsUnicode(int next) => next; |
47 | 45 |
48 void handleUnicode(int startScanOffset) {} | 46 void handleUnicode(int startScanOffset) {} |
49 | 47 |
50 Token firstToken() => tokens.next; | 48 Token firstToken() => tokens.next; |
51 Token previousToken() => tail; | 49 Token previousToken() => tail; |
52 | 50 |
53 void appendSubstringToken(PrecedenceInfo info, int start, bool asciiOnly, | 51 void appendSubstringToken(PrecedenceInfo info, int start, bool asciiOnly, |
54 [int extraOffset = 0]) { | 52 [int extraOffset = 0]) { |
55 tail.next = new StringToken.fromSubstring( | 53 tail.next = new StringToken.fromSubstring( |
56 info, string, start, scanOffset + extraOffset, tokenStart, | 54 info, string, start, scanOffset + extraOffset, tokenStart, |
57 canonicalize: true); | 55 canonicalize: true); |
58 tail = tail.next; | 56 tail = tail.next; |
59 } | 57 } |
60 | 58 |
61 bool atEndOfFile() => scanOffset >= string.length - 1; | 59 bool atEndOfFile() => scanOffset >= string.length - 1; |
62 } | 60 } |
OLD | NEW |