OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 string_scanner.string_scanner; | 5 import 'package:charcode/charcode.dart'; |
6 | |
7 import 'package:source_span/source_span.dart'; | 6 import 'package:source_span/source_span.dart'; |
8 | 7 |
9 import 'exception.dart'; | 8 import 'exception.dart'; |
10 import 'utils.dart'; | 9 import 'utils.dart'; |
11 | 10 |
12 /// When compiled to JS, forward slashes are always escaped in [RegExp.pattern]. | 11 /// When compiled to JS, forward slashes are always escaped in [RegExp.pattern]. |
13 /// | 12 /// |
14 /// See issue 17998. | 13 /// See issue 17998. |
15 final _slashAutoEscape = new RegExp("/").pattern == "\\/"; | 14 final _slashAutoEscape = new RegExp("/").pattern == "\\/"; |
16 | 15 |
17 /// A class that scans through a string using [Pattern]s. | 16 /// A class that scans through a string using [Pattern]s. |
18 class StringScanner { | 17 class StringScanner { |
19 /// The URL of the source of the string being scanned. | 18 /// The URL of the source of the string being scanned. |
20 /// | 19 /// |
21 /// This is used for error reporting. It may be `null`, indicating that the | 20 /// This is used for error reporting. It may be `null`, indicating that the |
22 /// source URL is unknown or unavailable. | 21 /// source URL is unknown or unavailable. |
23 final Uri sourceUrl; | 22 final Uri sourceUrl; |
24 | 23 |
25 /// The string being scanned through. | 24 /// The string being scanned through. |
26 final String string; | 25 final String string; |
27 | 26 |
28 /// The current position of the scanner in the string, in characters. | 27 /// The current position of the scanner in the string, in characters. |
29 int get position => _position; | 28 int get position => _position; |
30 set position(int position) { | 29 set position(int position) { |
31 if (position < 0 || position > string.length) { | 30 if (position < 0 || position > string.length) { |
32 throw new ArgumentError("Invalid position $position"); | 31 throw new ArgumentError("Invalid position $position"); |
33 } | 32 } |
34 | 33 |
35 _position = position; | 34 _position = position; |
| 35 _lastMatch = null; |
36 } | 36 } |
37 int _position = 0; | 37 int _position = 0; |
38 | 38 |
39 /// The data about the previous match made by the scanner. | 39 /// The data about the previous match made by the scanner. |
40 /// | 40 /// |
41 /// If the last match failed, this will be `null`. | 41 /// If the last match failed, this will be `null`. |
42 Match get lastMatch => _lastMatch; | 42 Match get lastMatch { |
| 43 // Lazily unset [_lastMatch] so that we avoid extra assignments in |
| 44 // character-by-character methods that are used in core loops. |
| 45 if (_position != _lastMatchPosition) _lastMatch = null; |
| 46 return _lastMatch; |
| 47 } |
43 Match _lastMatch; | 48 Match _lastMatch; |
| 49 int _lastMatchPosition; |
44 | 50 |
45 /// The portion of the string that hasn't yet been scanned. | 51 /// The portion of the string that hasn't yet been scanned. |
46 String get rest => string.substring(position); | 52 String get rest => string.substring(position); |
47 | 53 |
48 /// Whether the scanner has completely consumed [string]. | 54 /// Whether the scanner has completely consumed [string]. |
49 bool get isDone => position == string.length; | 55 bool get isDone => position == string.length; |
50 | 56 |
51 /// Creates a new [StringScanner] that starts scanning from [position]. | 57 /// Creates a new [StringScanner] that starts scanning from [position]. |
52 /// | 58 /// |
53 /// [position] defaults to 0, the beginning of the string. [sourceUrl] is the | 59 /// [position] defaults to 0, the beginning of the string. [sourceUrl] is the |
(...skipping 20 matching lines...) Expand all Loading... |
74 /// | 80 /// |
75 /// This returns `null` if [offset] points outside the string. It doesn't | 81 /// This returns `null` if [offset] points outside the string. It doesn't |
76 /// affect [lastMatch]. | 82 /// affect [lastMatch]. |
77 int peekChar([int offset]) { | 83 int peekChar([int offset]) { |
78 if (offset == null) offset = 0; | 84 if (offset == null) offset = 0; |
79 var index = position + offset; | 85 var index = position + offset; |
80 if (index < 0 || index >= string.length) return null; | 86 if (index < 0 || index >= string.length) return null; |
81 return string.codeUnitAt(index); | 87 return string.codeUnitAt(index); |
82 } | 88 } |
83 | 89 |
| 90 /// If the next character in the string is [character], consumes it. |
| 91 /// |
| 92 /// Returns whether or not [character] was consumed. |
| 93 bool scanChar(int character) { |
| 94 if (isDone) return false; |
| 95 if (string.codeUnitAt(_position) != character) return false; |
| 96 _position++; |
| 97 return true; |
| 98 } |
| 99 |
| 100 /// If the next character in the string is [character], consumes it. |
| 101 /// |
| 102 /// If [character] could not be consumed, throws a [FormatException] |
| 103 /// describing the position of the failure. [name] is used in this error as |
| 104 /// the expected name of the character being matched; if it's `null`, the |
| 105 /// character itself is used instead. |
| 106 void expectChar(int character, {String name}) { |
| 107 if (scanChar(character)) return; |
| 108 |
| 109 if (name == null) { |
| 110 if (character == $backslash) { |
| 111 name = r'"\"'; |
| 112 } else if (character == $double_quote) { |
| 113 name = r'"\""'; |
| 114 } else { |
| 115 name = '"${new String.fromCharCode(character)}"'; |
| 116 } |
| 117 } |
| 118 |
| 119 _fail(name); |
| 120 } |
| 121 |
84 /// If [pattern] matches at the current position of the string, scans forward | 122 /// If [pattern] matches at the current position of the string, scans forward |
85 /// until the end of the match. | 123 /// until the end of the match. |
86 /// | 124 /// |
87 /// Returns whether or not [pattern] matched. | 125 /// Returns whether or not [pattern] matched. |
88 bool scan(Pattern pattern) { | 126 bool scan(Pattern pattern) { |
89 var success = matches(pattern); | 127 var success = matches(pattern); |
90 if (success) _position = _lastMatch.end; | 128 if (success) { |
| 129 _position = _lastMatch.end; |
| 130 _lastMatchPosition = _position; |
| 131 } |
91 return success; | 132 return success; |
92 } | 133 } |
93 | 134 |
94 /// If [pattern] matches at the current position of the string, scans forward | 135 /// If [pattern] matches at the current position of the string, scans forward |
95 /// until the end of the match. | 136 /// until the end of the match. |
96 /// | 137 /// |
97 /// If [pattern] did not match, throws a [FormatException] describing the | 138 /// If [pattern] did not match, throws a [FormatException] describing the |
98 /// position of the failure. [name] is used in this error as the expected name | 139 /// position of the failure. [name] is used in this error as the expected name |
99 /// of the pattern being matched; if it's `null`, the pattern itself is used | 140 /// of the pattern being matched; if it's `null`, the pattern itself is used |
100 /// instead. | 141 /// instead. |
(...skipping 20 matching lines...) Expand all Loading... |
121 if (isDone) return; | 162 if (isDone) return; |
122 _fail("no more input"); | 163 _fail("no more input"); |
123 } | 164 } |
124 | 165 |
125 /// Returns whether or not [pattern] matches at the current position of the | 166 /// Returns whether or not [pattern] matches at the current position of the |
126 /// string. | 167 /// string. |
127 /// | 168 /// |
128 /// This doesn't move the scan pointer forward. | 169 /// This doesn't move the scan pointer forward. |
129 bool matches(Pattern pattern) { | 170 bool matches(Pattern pattern) { |
130 _lastMatch = pattern.matchAsPrefix(string, position); | 171 _lastMatch = pattern.matchAsPrefix(string, position); |
| 172 _lastMatchPosition = _position; |
131 return _lastMatch != null; | 173 return _lastMatch != null; |
132 } | 174 } |
133 | 175 |
134 /// Returns the substring of [string] between [start] and [end]. | 176 /// Returns the substring of [string] between [start] and [end]. |
135 /// | 177 /// |
136 /// Unlike [String.substring], [end] defaults to [position] rather than the | 178 /// Unlike [String.substring], [end] defaults to [position] rather than the |
137 /// end of the string. | 179 /// end of the string. |
138 String substring(int start, [int end]) { | 180 String substring(int start, [int end]) { |
139 if (end == null) end = position; | 181 if (end == null) end = position; |
140 return string.substring(start, end); | 182 return string.substring(start, end); |
141 } | 183 } |
142 | 184 |
143 /// Throws a [FormatException] with [message] as well as a detailed | 185 /// Throws a [FormatException] with [message] as well as a detailed |
144 /// description of the location of the error in the string. | 186 /// description of the location of the error in the string. |
145 /// | 187 /// |
146 /// [match] is the match information for the span of the string with which the | 188 /// [match] is the match information for the span of the string with which the |
147 /// error is associated. This should be a match returned by this scanner's | 189 /// error is associated. This should be a match returned by this scanner's |
148 /// [lastMatch] property. By default, the error is associated with the last | 190 /// [lastMatch] property. By default, the error is associated with the last |
149 /// match. | 191 /// match. |
150 /// | 192 /// |
151 /// If [position] and/or [length] are passed, they are used as the error span | 193 /// If [position] and/or [length] are passed, they are used as the error span |
152 /// instead. If only [length] is passed, [position] defaults to the current | 194 /// instead. If only [length] is passed, [position] defaults to the current |
153 /// position; if only [position] is passed, [length] defaults to 1. | 195 /// position; if only [position] is passed, [length] defaults to 0. |
154 /// | 196 /// |
155 /// It's an error to pass [match] at the same time as [position] or [length]. | 197 /// It's an error to pass [match] at the same time as [position] or [length]. |
156 void error(String message, {Match match, int position, int length}) { | 198 void error(String message, {Match match, int position, int length}) { |
157 validateErrorArgs(string, match, position, length); | 199 validateErrorArgs(string, match, position, length); |
158 | 200 |
159 if (match == null && position == null && length == null) match = lastMatch; | 201 if (match == null && position == null && length == null) match = lastMatch; |
160 if (position == null) { | 202 if (position == null) { |
161 position = match == null ? this.position : match.start; | 203 position = match == null ? this.position : match.start; |
162 } | 204 } |
163 if (length == null) length = match == null ? 1 : match.end - match.start; | 205 if (length == null) length = match == null ? 0 : match.end - match.start; |
164 | 206 |
165 var sourceFile = new SourceFile(string, url: sourceUrl); | 207 var sourceFile = new SourceFile.fromString(string, url: sourceUrl); |
166 var span = sourceFile.span(position, position + length); | 208 var span = sourceFile.span(position, position + length); |
167 throw new StringScannerException(message, span, string); | 209 throw new StringScannerException(message, span, string); |
168 } | 210 } |
169 | 211 |
170 // TODO(nweiz): Make this handle long lines more gracefully. | 212 // TODO(nweiz): Make this handle long lines more gracefully. |
171 /// Throws a [FormatException] describing that [name] is expected at the | 213 /// Throws a [FormatException] describing that [name] is expected at the |
172 /// current position in the string. | 214 /// current position in the string. |
173 void _fail(String name) { | 215 void _fail(String name) { |
174 error("expected $name.", position: this.position, length: 0); | 216 error("expected $name.", position: this.position, length: 0); |
175 } | 217 } |
176 } | 218 } |
OLD | NEW |