| 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 library string_scanner.string_scanner; |
| 6 | 6 |
| 7 import 'package:source_maps/source_maps.dart'; | 7 import 'package:source_span/source_span.dart'; |
| 8 | 8 |
| 9 import 'exception.dart'; | 9 import 'exception.dart'; |
| 10 import 'utils.dart'; | 10 import 'utils.dart'; |
| 11 | 11 |
| 12 /// When compiled to JS, forward slashes are always escaped in [RegExp.pattern]. | 12 /// When compiled to JS, forward slashes are always escaped in [RegExp.pattern]. |
| 13 /// | 13 /// |
| 14 /// See issue 17998. | 14 /// See issue 17998. |
| 15 final _slashAutoEscape = new RegExp("/").pattern == "\\/"; | 15 final _slashAutoEscape = new RegExp("/").pattern == "\\/"; |
| 16 | 16 |
| 17 /// A class that scans through a string using [Pattern]s. | 17 /// A class that scans through a string using [Pattern]s. |
| (...skipping 27 matching lines...) Expand all Loading... |
| 45 /// The portion of the string that hasn't yet been scanned. | 45 /// The portion of the string that hasn't yet been scanned. |
| 46 String get rest => string.substring(position); | 46 String get rest => string.substring(position); |
| 47 | 47 |
| 48 /// Whether the scanner has completely consumed [string]. | 48 /// Whether the scanner has completely consumed [string]. |
| 49 bool get isDone => position == string.length; | 49 bool get isDone => position == string.length; |
| 50 | 50 |
| 51 /// Creates a new [StringScanner] that starts scanning from [position]. | 51 /// Creates a new [StringScanner] that starts scanning from [position]. |
| 52 /// | 52 /// |
| 53 /// [position] defaults to 0, the beginning of the string. [sourceUrl] is the | 53 /// [position] defaults to 0, the beginning of the string. [sourceUrl] is the |
| 54 /// URL of the source of the string being scanned, if available. It can be | 54 /// URL of the source of the string being scanned, if available. It can be |
| 55 /// either a [String] or a [Uri]. | 55 /// a [String], a [Uri], or `null`. |
| 56 StringScanner(this.string, {sourceUrl, int position}) | 56 StringScanner(this.string, {sourceUrl, int position}) |
| 57 : sourceUrl = sourceUrl is String ? Uri.parse(sourceUrl) : sourceUrl { | 57 : sourceUrl = sourceUrl is String ? Uri.parse(sourceUrl) : sourceUrl { |
| 58 if (position != null) this.position = position; | 58 if (position != null) this.position = position; |
| 59 } | 59 } |
| 60 | 60 |
| 61 /// Consumes a single character and returns its character code. | 61 /// Consumes a single character and returns its character code. |
| 62 /// | 62 /// |
| 63 /// This throws a [FormatException] if the string has been fully consumed. It | 63 /// This throws a [FormatException] if the string has been fully consumed. It |
| 64 /// doesn't affect [lastMatch]. | 64 /// doesn't affect [lastMatch]. |
| 65 int readChar() { | 65 int readChar() { |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 /// It's an error to pass [match] at the same time as [position] or [length]. | 146 /// It's an error to pass [match] at the same time as [position] or [length]. |
| 147 void error(String message, {Match match, int position, int length}) { | 147 void error(String message, {Match match, int position, int length}) { |
| 148 validateErrorArgs(string, match, position, length); | 148 validateErrorArgs(string, match, position, length); |
| 149 | 149 |
| 150 if (match == null && position == null && length == null) match = lastMatch; | 150 if (match == null && position == null && length == null) match = lastMatch; |
| 151 if (position == null) { | 151 if (position == null) { |
| 152 position = match == null ? this.position : match.start; | 152 position = match == null ? this.position : match.start; |
| 153 } | 153 } |
| 154 if (length == null) length = match == null ? 1 : match.end - match.start; | 154 if (length == null) length = match == null ? 1 : match.end - match.start; |
| 155 | 155 |
| 156 var url = sourceUrl == null ? null : sourceUrl.toString(); | 156 var sourceFile = new SourceFile(string, url: sourceUrl); |
| 157 var sourceFile = new SourceFile.text(url, string); | |
| 158 var span = sourceFile.span(position, position + length); | 157 var span = sourceFile.span(position, position + length); |
| 159 throw new StringScannerException(message, string, sourceUrl, span); | 158 throw new StringScannerException(message, span, string); |
| 160 } | 159 } |
| 161 | 160 |
| 162 // TODO(nweiz): Make this handle long lines more gracefully. | 161 // TODO(nweiz): Make this handle long lines more gracefully. |
| 163 /// Throws a [FormatException] describing that [name] is expected at the | 162 /// Throws a [FormatException] describing that [name] is expected at the |
| 164 /// current position in the string. | 163 /// current position in the string. |
| 165 void _fail(String name) { | 164 void _fail(String name) { |
| 166 error("expected $name.", position: this.position, length: 0); | 165 error("expected $name.", position: this.position, length: 0); |
| 167 } | 166 } |
| 168 } | 167 } |
| OLD | NEW |