Line data Source code
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 0 : int get line => _sourceFile.getLine(_startLocation.offset + position) -
31 0 : _startLocation.line;
32 :
33 : int get column {
34 0 : var line = _sourceFile.getLine(_startLocation.offset + position);
35 0 : var column = _sourceFile.getColumn(_startLocation.offset + position,
36 : line: line);
37 0 : return line == _startLocation.line
38 0 : ? column - _startLocation.column
39 : : column;
40 : }
41 :
42 0 : LineScannerState get state => new _SpanScannerState(this, position);
43 :
44 : set state(LineScannerState state) {
45 0 : if (state is! _SpanScannerState ||
46 0 : !identical((state as _SpanScannerState)._scanner, this)) {
47 0 : throw new ArgumentError("The given LineScannerState was not returned by "
48 : "this LineScanner.");
49 : }
50 :
51 0 : this.position = state.position;
52 : }
53 :
54 0 : FileSpan get lastSpan => _lastSpan;
55 : FileSpan _lastSpan;
56 :
57 : FileLocation get location =>
58 0 : _sourceFile.location(_startLocation.offset + position);
59 :
60 0 : FileSpan get emptySpan => location.pointSpan();
61 :
62 : RelativeSpanScanner(FileSpan span)
63 0 : : _sourceFile = span.file,
64 0 : _startLocation = span.start,
65 0 : super(span.text, sourceUrl: span.sourceUrl);
66 :
67 : FileSpan spanFrom(LineScannerState startState, [LineScannerState endState]) {
68 0 : var endPosition = endState == null ? position : endState.position;
69 0 : return _sourceFile.span(
70 0 : _startLocation.offset + startState.position,
71 0 : _startLocation.offset + endPosition);
72 : }
73 :
74 : bool matches(Pattern pattern) {
75 0 : if (!super.matches(pattern)) {
76 0 : _lastSpan = null;
77 : return false;
78 : }
79 :
80 0 : _lastSpan = _sourceFile.span(
81 0 : _startLocation.offset + position,
82 0 : _startLocation.offset + lastMatch.end);
83 : return true;
84 : }
85 :
86 : void error(String message, {Match match, int position, int length}) {
87 0 : validateErrorArgs(string, match, position, length);
88 :
89 0 : if (match == null && position == null && length == null) match = lastMatch;
90 : if (position == null) {
91 0 : position = match == null ? this.position : match.start;
92 : }
93 0 : if (length == null) length = match == null ? 1 : match.end - match.start;
94 :
95 0 : var span = _sourceFile.span(
96 0 : _startLocation.offset + position,
97 0 : _startLocation.offset + position + length);
98 0 : 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 0 : int get line => _scanner._sourceFile.getLine(position);
109 0 : int get column => _scanner._sourceFile.getColumn(position);
110 :
111 0 : _SpanScannerState(this._scanner, this.position);
112 : }
|