OLD | NEW |
(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.line_scanner; |
| 6 |
| 7 import 'package:charcode/ascii.dart'; |
| 8 |
| 9 import 'string_scanner.dart'; |
| 10 |
| 11 // Note that much of this code is duplicated in eager_span_scanner.dart. |
| 12 |
| 13 /// A regular expression matching newlines across platforms. |
| 14 final _newlineRegExp = new RegExp(r"\r\n?|\n"); |
| 15 |
| 16 /// A subclass of [StringScanner] that tracks line and column information. |
| 17 class LineScanner extends StringScanner { |
| 18 /// The scanner's current (zero-based) line number. |
| 19 int get line => _line; |
| 20 int _line = 0; |
| 21 |
| 22 /// The scanner's current (zero-based) column number. |
| 23 int get column => _column; |
| 24 int _column = 0; |
| 25 |
| 26 /// The scanner's state, including line and column information. |
| 27 /// |
| 28 /// This can be used to efficiently save and restore the state of the scanner |
| 29 /// when backtracking. A given [LineScannerState] is only valid for the |
| 30 /// [LineScanner] that created it. |
| 31 LineScannerState get state => |
| 32 new LineScannerState._(this, position, line, column); |
| 33 |
| 34 /// Whether the current position is between a CR character and an LF |
| 35 /// charactet. |
| 36 bool get _betweenCRLF => peekChar(-1) == $cr && peekChar() == $lf; |
| 37 |
| 38 set state(LineScannerState state) { |
| 39 if (!identical(state._scanner, this)) { |
| 40 throw new ArgumentError("The given LineScannerState was not returned by " |
| 41 "this LineScanner."); |
| 42 } |
| 43 |
| 44 super.position = state.position; |
| 45 _line = state.line; |
| 46 _column = state.column; |
| 47 } |
| 48 |
| 49 set position(int newPosition) { |
| 50 var oldPosition = position; |
| 51 super.position = newPosition; |
| 52 |
| 53 if (newPosition > oldPosition) { |
| 54 var newlines = _newlinesIn(string.substring(oldPosition, newPosition)); |
| 55 _line += newlines.length; |
| 56 if (newlines.isEmpty) { |
| 57 _column += newPosition - oldPosition; |
| 58 } else { |
| 59 _column = newPosition - newlines.last.end; |
| 60 } |
| 61 } else { |
| 62 var newlines = _newlinesIn(string.substring(newPosition, oldPosition)); |
| 63 if (_betweenCRLF) newlines.removeLast(); |
| 64 |
| 65 _line -= newlines.length; |
| 66 if (newlines.isEmpty) { |
| 67 _column -= oldPosition - newPosition; |
| 68 } else { |
| 69 _column = newPosition - |
| 70 string.lastIndexOf(_newlineRegExp, newPosition) - 1; |
| 71 } |
| 72 } |
| 73 } |
| 74 |
| 75 LineScanner(String string, {sourceUrl, int position}) |
| 76 : super(string, sourceUrl: sourceUrl, position: position); |
| 77 |
| 78 int readChar() { |
| 79 var char = super.readChar(); |
| 80 if (char == $lf || (char == $cr && peekChar() != $lf)) { |
| 81 _line += 1; |
| 82 _column = 0; |
| 83 } else { |
| 84 _column += 1; |
| 85 } |
| 86 return char; |
| 87 } |
| 88 |
| 89 bool scan(Pattern pattern) { |
| 90 if (!super.scan(pattern)) return false; |
| 91 |
| 92 var newlines = _newlinesIn(lastMatch[0]); |
| 93 _line += newlines.length; |
| 94 if (newlines.isEmpty) { |
| 95 _column += lastMatch[0].length; |
| 96 } else { |
| 97 _column = lastMatch[0].length - newlines.last.end; |
| 98 } |
| 99 |
| 100 return true; |
| 101 } |
| 102 |
| 103 /// Returns a list of [Match]es describing all the newlines in [text], which |
| 104 /// is assumed to end at [position]. |
| 105 List<Match> _newlinesIn(String text) { |
| 106 var newlines = _newlineRegExp.allMatches(text).toList(); |
| 107 if (_betweenCRLF) newlines.removeLast(); |
| 108 return newlines; |
| 109 } |
| 110 } |
| 111 |
| 112 /// A class representing the state of a [LineScanner]. |
| 113 class LineScannerState { |
| 114 /// The [LineScanner] that created this. |
| 115 final LineScanner _scanner; |
| 116 |
| 117 /// The position of the scanner in this state. |
| 118 final int position; |
| 119 |
| 120 /// The zero-based line number of the scanner in this state. |
| 121 final int line; |
| 122 |
| 123 /// The zero-based column number of the scanner in this state. |
| 124 final int column; |
| 125 |
| 126 LineScannerState._(this._scanner, this.position, this.line, this.column); |
| 127 } |
OLD | NEW |