OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library string_scanner.line_scanner; | 5 library string_scanner.line_scanner; |
6 | 6 |
7 import 'string_scanner.dart'; | 7 import 'string_scanner.dart'; |
8 | 8 |
9 /// A subclass of [StringScanner] that tracks line and column information. | 9 /// A subclass of [StringScanner] that tracks line and column information. |
10 class LineScanner extends StringScanner { | 10 class LineScanner extends StringScanner { |
(...skipping 22 matching lines...) Expand all Loading... |
33 super.position = state.position; | 33 super.position = state.position; |
34 _line = state.line; | 34 _line = state.line; |
35 _column = state.column; | 35 _column = state.column; |
36 } | 36 } |
37 | 37 |
38 set position(int newPosition) { | 38 set position(int newPosition) { |
39 var oldPosition = position; | 39 var oldPosition = position; |
40 super.position = newPosition; | 40 super.position = newPosition; |
41 | 41 |
42 if (newPosition > oldPosition) { | 42 if (newPosition > oldPosition) { |
43 var newlines = "\n".allMatches(string.substring(oldPosition, newPosition)) | 43 var newlines = |
44 .toList(); | 44 "\n".allMatches(string.substring(oldPosition, newPosition)).toList(); |
45 _line += newlines.length; | 45 _line += newlines.length; |
46 if (newlines.isEmpty) { | 46 if (newlines.isEmpty) { |
47 _column += newPosition - oldPosition; | 47 _column += newPosition - oldPosition; |
48 } else { | 48 } else { |
49 _column = newPosition - newlines.last.end; | 49 _column = newPosition - newlines.last.end; |
50 } | 50 } |
51 } else { | 51 } else { |
52 var newlines = "\n".allMatches(string.substring(newPosition, oldPosition)) | 52 var newlines = |
53 .toList(); | 53 "\n".allMatches(string.substring(newPosition, oldPosition)).toList(); |
54 _line -= newlines.length; | 54 _line -= newlines.length; |
55 if (newlines.isEmpty) { | 55 if (newlines.isEmpty) { |
56 _column -= oldPosition - newPosition; | 56 _column -= oldPosition - newPosition; |
57 } else { | 57 } else { |
58 _column = newPosition - string.lastIndexOf("\n", newPosition) - 1; | 58 _column = newPosition - string.lastIndexOf("\n", newPosition) - 1; |
59 } | 59 } |
60 } | 60 } |
61 } | 61 } |
62 | 62 |
63 LineScanner(String string, {sourceUrl, int position}) | 63 LineScanner(String string, {sourceUrl, int position}) |
64 : super(string, sourceUrl: sourceUrl, position: position); | 64 : super(string, sourceUrl: sourceUrl, position: position); |
65 | 65 |
66 int readChar() { | 66 int readChar() { |
67 var char = super.readChar(); | 67 var char = super.readChar(); |
68 if (char == 0xA) { | 68 if (char == 0xA) { |
69 _line += 1; | 69 _line += 1; |
70 _column = 0; | 70 _column = 0; |
71 } else { | 71 } else { |
72 _column += 1; | 72 _column += 1; |
73 } | 73 } |
74 return char; | 74 return char; |
75 } | 75 } |
76 | 76 |
77 bool scan(Pattern pattern) { | 77 bool scan(Pattern pattern) { |
78 var oldPosition = position; | |
79 if (!super.scan(pattern)) return false; | 78 if (!super.scan(pattern)) return false; |
80 | 79 |
81 var newlines = "\n".allMatches(lastMatch[0]).toList(); | 80 var newlines = "\n".allMatches(lastMatch[0]).toList(); |
82 _line += newlines.length; | 81 _line += newlines.length; |
83 if (newlines.isEmpty) { | 82 if (newlines.isEmpty) { |
84 _column += lastMatch[0].length; | 83 _column += lastMatch[0].length; |
85 } else { | 84 } else { |
86 _column = lastMatch[0].length - newlines.last.end; | 85 _column = lastMatch[0].length - newlines.last.end; |
87 } | 86 } |
88 | 87 |
(...skipping 10 matching lines...) Expand all Loading... |
99 final int position; | 98 final int position; |
100 | 99 |
101 /// The zero-based line number of the scanner in this state. | 100 /// The zero-based line number of the scanner in this state. |
102 final int line; | 101 final int line; |
103 | 102 |
104 /// The zero-based column number of the scanner in this state. | 103 /// The zero-based column number of the scanner in this state. |
105 final int column; | 104 final int column; |
106 | 105 |
107 LineScannerState._(this._scanner, this.position, this.line, this.column); | 106 LineScannerState._(this._scanner, this.position, this.line, this.column); |
108 } | 107 } |
OLD | NEW |