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

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

Issue 2039163002: Add SpanScanner.within(). (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 | « CHANGELOG.md ('k') | lib/src/span_scanner.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2016, 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 import 'package:source_span/source_span.dart';
6
7 import 'exception.dart';
8 import 'line_scanner.dart';
9 import 'span_scanner.dart';
10 import 'string_scanner.dart';
11 import 'utils.dart';
12
13 /// A [SpanScanner] that scans within an existing [FileSpan].
14 ///
15 /// This re-implements chunks of [SpanScanner] rather than using a dummy span or
16 /// inheritance because scanning is often a performance-critical operation, so
17 /// it's important to avoid adding extra overhead when relative scanning isn't
18 /// needed.
19 class RelativeSpanScanner extends StringScanner implements SpanScanner {
20 /// The source of the scanner.
21 ///
22 /// This caches line break information and is used to generate [Span]s.
23 final SourceFile _sourceFile;
24
25 /// The start location of the span within which this scanner is scanning.
26 ///
27 /// This is used to convert between span-relative and file-relative fields.
28 final FileLocation _startLocation;
29
30 int get line => _sourceFile.getLine(_startLocation.offset + position) -
31 _startLocation.line;
32
33 int get column {
34 var line = _sourceFile.getLine(_startLocation.offset + position);
35 var column = _sourceFile.getColumn(_startLocation.offset + position,
36 line: line);
37 return line == _startLocation.line
38 ? column - _startLocation.column
39 : column;
40 }
41
42 LineScannerState get state => new _SpanScannerState(this, position);
43
44 set state(LineScannerState state) {
45 if (state is! _SpanScannerState ||
46 !identical((state as _SpanScannerState)._scanner, this)) {
47 throw new ArgumentError("The given LineScannerState was not returned by "
48 "this LineScanner.");
49 }
50
51 this.position = state.position;
52 }
53
54 FileSpan get lastSpan => _lastSpan;
55 FileSpan _lastSpan;
56
57 FileLocation get location =>
58 _sourceFile.location(_startLocation.offset + position);
59
60 FileSpan get emptySpan => location.pointSpan();
61
62 RelativeSpanScanner(FileSpan span)
63 : _sourceFile = span.file,
64 _startLocation = span.start,
65 super(span.text, sourceUrl: span.sourceUrl);
66
67 FileSpan spanFrom(LineScannerState startState, [LineScannerState endState]) {
68 var endPosition = endState == null ? position : endState.position;
69 return _sourceFile.span(
70 _startLocation.offset + startState.position,
71 _startLocation.offset + endPosition);
72 }
73
74 bool matches(Pattern pattern) {
75 if (!super.matches(pattern)) {
76 _lastSpan = null;
77 return false;
78 }
79
80 _lastSpan = _sourceFile.span(
81 _startLocation.offset + position,
82 _startLocation.offset + lastMatch.end);
83 return true;
84 }
85
86 void error(String message, {Match match, int position, int length}) {
87 validateErrorArgs(string, match, position, length);
88
89 if (match == null && position == null && length == null) match = lastMatch;
90 if (position == null) {
91 position = match == null ? this.position : match.start;
92 }
93 if (length == null) length = match == null ? 1 : match.end - match.start;
94
95 var span = _sourceFile.span(
96 _startLocation.offset + position,
97 _startLocation.offset + position + length);
98 throw new StringScannerException(message, span, string);
99 }
100 }
101
102 /// A class representing the state of a [SpanScanner].
103 class _SpanScannerState implements LineScannerState {
104 /// The [SpanScanner] that created this.
105 final RelativeSpanScanner _scanner;
106
107 final int position;
108 int get line => _scanner._sourceFile.getLine(position);
109 int get column => _scanner._sourceFile.getColumn(position);
110
111 _SpanScannerState(this._scanner, this.position);
112 }
OLDNEW
« no previous file with comments | « CHANGELOG.md ('k') | lib/src/span_scanner.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698