Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(214)

Side by Side Diff: lib/src/string_scanner.dart

Issue 2041813002: Add StringScanner.scanChar() and .expectChar(). (Closed) Base URL: git@github.com:dart-lang/string_scanner@master
Patch Set: Code review changes Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/src/line_scanner.dart ('k') | pubspec.yaml » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 import 'package:charcode/charcode.dart';
5 import 'package:source_span/source_span.dart'; 6 import 'package:source_span/source_span.dart';
6 7
7 import 'exception.dart'; 8 import 'exception.dart';
8 import 'utils.dart'; 9 import 'utils.dart';
9 10
10 /// 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].
11 /// 12 ///
12 /// See issue 17998. 13 /// See issue 17998.
13 final _slashAutoEscape = new RegExp("/").pattern == "\\/"; 14 final _slashAutoEscape = new RegExp("/").pattern == "\\/";
14 15
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 /// 73 ///
73 /// This returns `null` if [offset] points outside the string. It doesn't 74 /// This returns `null` if [offset] points outside the string. It doesn't
74 /// affect [lastMatch]. 75 /// affect [lastMatch].
75 int peekChar([int offset]) { 76 int peekChar([int offset]) {
76 if (offset == null) offset = 0; 77 if (offset == null) offset = 0;
77 var index = position + offset; 78 var index = position + offset;
78 if (index < 0 || index >= string.length) return null; 79 if (index < 0 || index >= string.length) return null;
79 return string.codeUnitAt(index); 80 return string.codeUnitAt(index);
80 } 81 }
81 82
83 /// If the next character in the string is [character], consumes it.
84 ///
85 /// Returns whether or not [character] was consumed.
86 bool scanChar(int character) {
87 if (isDone) return false;
88 if (string.codeUnitAt(_position) != character) return false;
89 _position++;
90 return true;
91 }
92
93 /// If the next character in the string is [character], consumes it.
94 ///
95 /// If [character] could not be consumed, throws a [FormatException]
96 /// describing the position of the failure. [name] is used in this error as
97 /// the expected name of the character being matched; if it's `null`, the
98 /// character itself is used instead.
99 void expectChar(int character, {String name}) {
100 if (scanChar(character)) return;
101
102 if (name == null) {
103 if (character == $backslash) {
104 name = r'"\"';
105 } else if (character == $double_quote) {
106 name = r'"\""';
107 } else {
108 name = '"${new String.fromCharCode(character)}"';
109 }
110 }
111
112 _fail('Expected $name.');
113 }
114
82 /// If [pattern] matches at the current position of the string, scans forward 115 /// If [pattern] matches at the current position of the string, scans forward
83 /// until the end of the match. 116 /// until the end of the match.
84 /// 117 ///
85 /// Returns whether or not [pattern] matched. 118 /// Returns whether or not [pattern] matched.
86 bool scan(Pattern pattern) { 119 bool scan(Pattern pattern) {
87 var success = matches(pattern); 120 var success = matches(pattern);
88 if (success) _position = _lastMatch.end; 121 if (success) _position = _lastMatch.end;
89 return success; 122 return success;
90 } 123 }
91 124
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 throw new StringScannerException(message, span, string); 198 throw new StringScannerException(message, span, string);
166 } 199 }
167 200
168 // TODO(nweiz): Make this handle long lines more gracefully. 201 // TODO(nweiz): Make this handle long lines more gracefully.
169 /// Throws a [FormatException] describing that [name] is expected at the 202 /// Throws a [FormatException] describing that [name] is expected at the
170 /// current position in the string. 203 /// current position in the string.
171 void _fail(String name) { 204 void _fail(String name) {
172 error("expected $name.", position: this.position, length: 0); 205 error("expected $name.", position: this.position, length: 0);
173 } 206 }
174 } 207 }
OLDNEW
« no previous file with comments | « lib/src/line_scanner.dart ('k') | pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698