OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library string_scanner.string_scanner; |
| 6 |
| 7 import 'package:source_span/source_span.dart'; |
| 8 |
| 9 import 'exception.dart'; |
| 10 import 'utils.dart'; |
| 11 |
| 12 /// When compiled to JS, forward slashes are always escaped in [RegExp.pattern]. |
| 13 /// |
| 14 /// See issue 17998. |
| 15 final _slashAutoEscape = new RegExp("/").pattern == "\\/"; |
| 16 |
| 17 /// A class that scans through a string using [Pattern]s. |
| 18 class StringScanner { |
| 19 /// The URL of the source of the string being scanned. |
| 20 /// |
| 21 /// This is used for error reporting. It may be `null`, indicating that the |
| 22 /// source URL is unknown or unavailable. |
| 23 final Uri sourceUrl; |
| 24 |
| 25 /// The string being scanned through. |
| 26 final String string; |
| 27 |
| 28 /// The current position of the scanner in the string, in characters. |
| 29 int get position => _position; |
| 30 set position(int position) { |
| 31 if (position < 0 || position > string.length) { |
| 32 throw new ArgumentError("Invalid position $position"); |
| 33 } |
| 34 |
| 35 _position = position; |
| 36 } |
| 37 int _position = 0; |
| 38 |
| 39 /// The data about the previous match made by the scanner. |
| 40 /// |
| 41 /// If the last match failed, this will be `null`. |
| 42 Match get lastMatch => _lastMatch; |
| 43 Match _lastMatch; |
| 44 |
| 45 /// The portion of the string that hasn't yet been scanned. |
| 46 String get rest => string.substring(position); |
| 47 |
| 48 /// Whether the scanner has completely consumed [string]. |
| 49 bool get isDone => position == string.length; |
| 50 |
| 51 /// Creates a new [StringScanner] that starts scanning from [position]. |
| 52 /// |
| 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 |
| 55 /// a [String], a [Uri], or `null`. |
| 56 StringScanner(this.string, {sourceUrl, int position}) |
| 57 : sourceUrl = sourceUrl is String ? Uri.parse(sourceUrl) : sourceUrl { |
| 58 if (position != null) this.position = position; |
| 59 } |
| 60 |
| 61 /// Consumes a single character and returns its character code. |
| 62 /// |
| 63 /// This throws a [FormatException] if the string has been fully consumed. It |
| 64 /// doesn't affect [lastMatch]. |
| 65 int readChar() { |
| 66 if (isDone) _fail("more input"); |
| 67 return string.codeUnitAt(_position++); |
| 68 } |
| 69 |
| 70 /// Returns the character code of the character [offset] away from [position]. |
| 71 /// |
| 72 /// [offset] defaults to zero, and may be negative to inspect already-consumed |
| 73 /// characters. |
| 74 /// |
| 75 /// This returns `null` if [offset] points outside the string. It doesn't |
| 76 /// affect [lastMatch]. |
| 77 int peekChar([int offset]) { |
| 78 if (offset == null) offset = 0; |
| 79 var index = position + offset; |
| 80 if (index < 0 || index >= string.length) return null; |
| 81 return string.codeUnitAt(index); |
| 82 } |
| 83 |
| 84 /// If [pattern] matches at the current position of the string, scans forward |
| 85 /// until the end of the match. |
| 86 /// |
| 87 /// Returns whether or not [pattern] matched. |
| 88 bool scan(Pattern pattern) { |
| 89 var success = matches(pattern); |
| 90 if (success) _position = _lastMatch.end; |
| 91 return success; |
| 92 } |
| 93 |
| 94 /// If [pattern] matches at the current position of the string, scans forward |
| 95 /// until the end of the match. |
| 96 /// |
| 97 /// 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 |
| 99 /// of the pattern being matched; if it's `null`, the pattern itself is used |
| 100 /// instead. |
| 101 void expect(Pattern pattern, {String name}) { |
| 102 if (scan(pattern)) return; |
| 103 |
| 104 if (name == null) { |
| 105 if (pattern is RegExp) { |
| 106 var source = pattern.pattern; |
| 107 if (!_slashAutoEscape) source = source.replaceAll("/", "\\/"); |
| 108 name = "/$source/"; |
| 109 } else { |
| 110 name = |
| 111 pattern.toString().replaceAll("\\", "\\\\").replaceAll('"', '\\"'); |
| 112 name = '"$name"'; |
| 113 } |
| 114 } |
| 115 _fail(name); |
| 116 } |
| 117 |
| 118 /// If the string has not been fully consumed, this throws a |
| 119 /// [FormatException]. |
| 120 void expectDone() { |
| 121 if (isDone) return; |
| 122 _fail("no more input"); |
| 123 } |
| 124 |
| 125 /// Returns whether or not [pattern] matches at the current position of the |
| 126 /// string. |
| 127 /// |
| 128 /// This doesn't move the scan pointer forward. |
| 129 bool matches(Pattern pattern) { |
| 130 _lastMatch = pattern.matchAsPrefix(string, position); |
| 131 return _lastMatch != null; |
| 132 } |
| 133 |
| 134 /// Returns the substring of [string] between [start] and [end]. |
| 135 /// |
| 136 /// Unlike [String.substring], [end] defaults to [position] rather than the |
| 137 /// end of the string. |
| 138 String substring(int start, [int end]) { |
| 139 if (end == null) end = position; |
| 140 return string.substring(start, end); |
| 141 } |
| 142 |
| 143 /// Throws a [FormatException] with [message] as well as a detailed |
| 144 /// description of the location of the error in the string. |
| 145 /// |
| 146 /// [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 |
| 148 /// [lastMatch] property. By default, the error is associated with the last |
| 149 /// match. |
| 150 /// |
| 151 /// 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 |
| 153 /// position; if only [position] is passed, [length] defaults to 1. |
| 154 /// |
| 155 /// 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}) { |
| 157 validateErrorArgs(string, match, position, length); |
| 158 |
| 159 if (match == null && position == null && length == null) match = lastMatch; |
| 160 if (position == null) { |
| 161 position = match == null ? this.position : match.start; |
| 162 } |
| 163 if (length == null) length = match == null ? 1 : match.end - match.start; |
| 164 |
| 165 var sourceFile = new SourceFile(string, url: sourceUrl); |
| 166 var span = sourceFile.span(position, position + length); |
| 167 throw new StringScannerException(message, span, string); |
| 168 } |
| 169 |
| 170 // TODO(nweiz): Make this handle long lines more gracefully. |
| 171 /// Throws a [FormatException] describing that [name] is expected at the |
| 172 /// current position in the string. |
| 173 void _fail(String name) { |
| 174 error("expected $name.", position: this.position, length: 0); |
| 175 } |
| 176 } |
OLD | NEW |