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

Side by Side Diff: observatory_pub_packages/string_scanner/src/span_scanner.dart

Issue 816693004: Add observatory_pub_packages snapshot to third_party (Closed) Base URL: http://dart.googlecode.com/svn/third_party/
Patch Set: Created 6 years 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 | Annotate | Revision Log
OLDNEW
(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.span_scanner;
6
7 import 'package:source_span/source_span.dart';
8
9 import 'exception.dart';
10 import 'line_scanner.dart';
11 import 'string_scanner.dart';
12 import 'utils.dart';
13
14 /// A subclass of [LineScanner] that exposes matched ranges as source map
15 /// [Span]s.
16 class SpanScanner extends StringScanner implements LineScanner {
17 /// The source of the scanner.
18 ///
19 /// This caches line break information and is used to generate [Span]s.
20 final SourceFile _sourceFile;
21
22 int get line => _sourceFile.getLine(position);
23 int get column => _sourceFile.getColumn(position);
24
25 LineScannerState get state => new _SpanScannerState(this, position);
26
27 set state(LineScannerState state) {
28 if (state is! _SpanScannerState ||
29 !identical((state as _SpanScannerState)._scanner, this)) {
30 throw new ArgumentError("The given LineScannerState was not returned by "
31 "this LineScanner.");
32 }
33
34 this.position = state.position;
35 }
36
37 /// The [FileSpan] for [lastMatch].
38 ///
39 /// This is the span for the entire match. There's no way to get spans for
40 /// subgroups since [Match] exposes no information about their positions.
41 FileSpan get lastSpan => _lastSpan;
42 FileSpan _lastSpan;
43
44 /// The current location of the scanner.
45 FileLocation get location => _sourceFile.location(position);
46
47 /// Returns an empty span at the current location.
48 FileSpan get emptySpan => location.pointSpan();
49
50 /// Creates a new [SpanScanner] that starts scanning from [position].
51 ///
52 /// [sourceUrl] is used as [SourceLocation.sourceUrl] for the returned
53 /// [FileSpan]s as well as for error reporting. It can be a [String], a
54 /// [Uri], or `null`.
55 SpanScanner(String string, {sourceUrl, int position})
56 : _sourceFile = new SourceFile(string, url: sourceUrl),
57 super(string, sourceUrl: sourceUrl, position: position);
58
59 /// Creates a [FileSpan] representing the source range between [startState]
60 /// and the current position.
61 FileSpan spanFrom(LineScannerState startState, [LineScannerState endState]) {
62 var endPosition = endState == null ? position : endState.position;
63 return _sourceFile.span(startState.position, endPosition);
64 }
65
66 bool matches(Pattern pattern) {
67 if (!super.matches(pattern)) {
68 _lastSpan = null;
69 return false;
70 }
71
72 _lastSpan = _sourceFile.span(position, lastMatch.end);
73 return true;
74 }
75
76 void error(String message, {Match match, int position, int length}) {
77 validateErrorArgs(string, match, position, length);
78
79 if (match == null && position == null && length == null) match = lastMatch;
80 if (position == null) {
81 position = match == null ? this.position : match.start;
82 }
83 if (length == null) length = match == null ? 1 : match.end - match.start;
84
85 var span = _sourceFile.span(position, position + length);
86 throw new StringScannerException(message, span, string);
87 }
88 }
89
90 /// A class representing the state of a [SpanScanner].
91 class _SpanScannerState implements LineScannerState {
92 /// The [SpanScanner] that created this.
93 final SpanScanner _scanner;
94
95 final int position;
96 int get line => _scanner._sourceFile.getLine(position);
97 int get column => _scanner._sourceFile.getColumn(position);
98
99 _SpanScannerState(this._scanner, this.position);
100 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698