LCOV - code coverage report
Current view: top level - string_scanner-1.0.2/lib/src - span_scanner.dart (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 0 27 0.0 %
Date: 2017-10-10 20:17:03 Functions: 0 0 -

          Line data    Source code
       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             : import 'package:source_span/source_span.dart';
       6             : 
       7             : import 'eager_span_scanner.dart';
       8             : import 'exception.dart';
       9             : import 'line_scanner.dart';
      10             : import 'relative_span_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           0 :   int get line => _sourceFile.getLine(position);
      23           0 :   int get column => _sourceFile.getColumn(position);
      24             : 
      25           0 :   LineScannerState get state => new _SpanScannerState(this, position);
      26             : 
      27             :   set state(LineScannerState state) {
      28           0 :     if (state is! _SpanScannerState ||
      29           0 :         !identical((state as _SpanScannerState)._scanner, this)) {
      30           0 :       throw new ArgumentError("The given LineScannerState was not returned by "
      31             :           "this LineScanner.");
      32             :     }
      33             : 
      34           0 :     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 {
      42           0 :     if (lastMatch == null) _lastSpan = null;
      43           0 :     return _lastSpan;
      44             :   }
      45             :   FileSpan _lastSpan;
      46             : 
      47             :   /// The current location of the scanner.
      48           0 :   FileLocation get location => _sourceFile.location(position);
      49             : 
      50             :   /// Returns an empty span at the current location.
      51           0 :   FileSpan get emptySpan => location.pointSpan();
      52             : 
      53             :   /// Creates a new [SpanScanner] that starts scanning from [position].
      54             :   ///
      55             :   /// [sourceUrl] is used as [SourceLocation.sourceUrl] for the returned
      56             :   /// [FileSpan]s as well as for error reporting. It can be a [String], a
      57             :   /// [Uri], or `null`.
      58             :   SpanScanner(String string, {sourceUrl, int position})
      59           0 :       : _sourceFile = new SourceFile.fromString(string, url: sourceUrl),
      60           0 :         super(string, sourceUrl: sourceUrl, position: position);
      61             : 
      62             :   /// Creates a new [SpanScanner] that eagerly computes line and column numbers.
      63             :   ///
      64             :   /// In general [new SpanScanner] will be more efficient, since it avoids extra
      65             :   /// computation on every scan. However, eager scanning can be useful for
      66             :   /// situations where the normal course of parsing frequently involves
      67             :   /// accessing the current line and column numbers.
      68             :   ///
      69             :   /// Note that *only* the `line` and `column` fields on the `SpanScanner`
      70             :   /// itself and its `LineScannerState` are eagerly computed. To limit their
      71             :   /// memory footprint, returned spans and locations will still lazily compute
      72             :   /// their line and column numbers.
      73             :   factory SpanScanner.eager(String string, {sourceUrl, int position}) =
      74             :       EagerSpanScanner;
      75             : 
      76             :   /// Creates a new [SpanScanner] that scans within [span].
      77             :   ///
      78             :   /// This scans through [span.text], but emits new spans from [span.file] in
      79             :   /// their appropriate relative positions. The [string] field contains only
      80             :   /// [span.text], and [position], [line], and [column] are all relative to the
      81             :   /// span.
      82             :   factory SpanScanner.within(FileSpan span) = RelativeSpanScanner;
      83             : 
      84             :   /// Creates a [FileSpan] representing the source range between [startState]
      85             :   /// and the current position.
      86             :   FileSpan spanFrom(LineScannerState startState, [LineScannerState endState]) {
      87           0 :     var endPosition = endState == null ? position : endState.position;
      88           0 :     return _sourceFile.span(startState.position, endPosition);
      89             :   }
      90             : 
      91             :   bool matches(Pattern pattern) {
      92           0 :     if (!super.matches(pattern)) {
      93           0 :       _lastSpan = null;
      94             :       return false;
      95             :     }
      96             : 
      97           0 :     _lastSpan = _sourceFile.span(position, lastMatch.end);
      98             :     return true;
      99             :   }
     100             : 
     101             :   void error(String message, {Match match, int position, int length}) {
     102           0 :     validateErrorArgs(string, match, position, length);
     103             : 
     104           0 :     if (match == null && position == null && length == null) match = lastMatch;
     105             :     if (position == null) {
     106           0 :       position = match == null ? this.position : match.start;
     107             :     }
     108           0 :     if (length == null) length = match == null ? 0 : match.end - match.start;
     109             : 
     110           0 :     var span = _sourceFile.span(position, position + length);
     111           0 :     throw new StringScannerException(message, span, string);
     112             :   }
     113             : }
     114             : 
     115             : /// A class representing the state of a [SpanScanner].
     116             : class _SpanScannerState implements LineScannerState {
     117             :   /// The [SpanScanner] that created this.
     118             :   final SpanScanner _scanner;
     119             : 
     120             :   final int position;
     121           0 :   int get line => _scanner._sourceFile.getLine(position);
     122           0 :   int get column => _scanner._sourceFile.getColumn(position);
     123             : 
     124           0 :   _SpanScannerState(this._scanner, this.position);
     125             : }

Generated by: LCOV version 1.13