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 analyzer.src.dart.scanner.scanner; | 5 library analyzer.src.dart.scanner.scanner; |
6 | 6 |
7 import 'package:analyzer/error/error.dart'; | 7 import 'package:analyzer/error/error.dart'; |
8 import 'package:analyzer/error/listener.dart'; | 8 import 'package:analyzer/error/listener.dart'; |
9 import 'package:analyzer/src/dart/error/syntactic_errors.dart'; | 9 import 'package:analyzer/src/dart/error/syntactic_errors.dart'; |
10 import 'package:analyzer/src/dart/scanner/reader.dart'; | 10 import 'package:analyzer/src/dart/scanner/reader.dart'; |
(...skipping 30 matching lines...) Expand all Loading... |
41 | 41 |
42 /** | 42 /** |
43 * Initialize a newly created scanner to scan characters from the given | 43 * Initialize a newly created scanner to scan characters from the given |
44 * [source]. The given character [reader] will be used to read the characters | 44 * [source]. The given character [reader] will be used to read the characters |
45 * in the source. The given [_errorListener] will be informed of any errors | 45 * in the source. The given [_errorListener] will be informed of any errors |
46 * that are found. | 46 * that are found. |
47 */ | 47 */ |
48 factory Scanner(Source source, CharacterReader reader, | 48 factory Scanner(Source source, CharacterReader reader, |
49 AnalysisErrorListener errorListener) => | 49 AnalysisErrorListener errorListener) => |
50 fe.Scanner.useFasta | 50 fe.Scanner.useFasta |
51 ? new _Scanner2(source, reader.getContents(), errorListener) | 51 ? new _Scanner2( |
| 52 source, reader.getContents(), reader.offset, errorListener) |
52 : new Scanner._(source, reader, errorListener); | 53 : new Scanner._(source, reader, errorListener); |
53 | 54 |
54 Scanner._(this.source, CharacterReader reader, this._errorListener) | 55 Scanner._(this.source, CharacterReader reader, this._errorListener) |
55 : super.create(reader); | 56 : super.create(reader); |
56 | 57 |
57 @override | 58 @override |
58 void reportError( | 59 void reportError( |
59 ScannerErrorCode errorCode, int offset, List<Object> arguments) { | 60 ScannerErrorCode errorCode, int offset, List<Object> arguments) { |
60 _errorListener | 61 _errorListener |
61 .onError(new AnalysisError(source, offset, 1, errorCode, arguments)); | 62 .onError(new AnalysisError(source, offset, 1, errorCode, arguments)); |
62 } | 63 } |
63 } | 64 } |
64 | 65 |
65 /** | 66 /** |
66 * Replacement scanner based on fasta. | 67 * Replacement scanner based on fasta. |
67 */ | 68 */ |
68 class _Scanner2 implements Scanner { | 69 class _Scanner2 implements Scanner { |
69 @override | 70 @override |
70 final Source source; | 71 final Source source; |
71 | 72 |
72 /** | 73 /** |
73 * The text to be scanned. | 74 * The text to be scanned. |
74 */ | 75 */ |
75 final String _contents; | 76 final String _contents; |
76 | 77 |
77 /** | 78 /** |
| 79 * The offset of the first character from the reader. |
| 80 */ |
| 81 final int _readerOffset; |
| 82 |
| 83 /** |
78 * The error listener that will be informed of any errors that are found | 84 * The error listener that will be informed of any errors that are found |
79 * during the scan. | 85 * during the scan. |
80 */ | 86 */ |
81 final AnalysisErrorListener _errorListener; | 87 final AnalysisErrorListener _errorListener; |
82 | 88 |
83 /** | 89 /** |
84 * The flag specifying whether documentation comments should be parsed. | 90 * The flag specifying whether documentation comments should be parsed. |
85 */ | 91 */ |
86 bool _preserveComments = true; | 92 bool _preserveComments = true; |
87 | 93 |
88 @override | 94 @override |
89 final List<int> lineStarts = <int>[]; | 95 final List<int> lineStarts = <int>[]; |
90 | 96 |
91 @override | 97 @override |
92 Token firstToken; | 98 Token firstToken; |
93 | 99 |
94 @override | 100 @override |
95 bool scanGenericMethodComments = false; | 101 bool scanGenericMethodComments = false; |
96 | 102 |
97 @override | 103 @override |
98 bool scanLazyAssignmentOperators = false; | 104 bool scanLazyAssignmentOperators = false; |
99 | 105 |
100 _Scanner2(this.source, this._contents, this._errorListener) { | 106 _Scanner2( |
| 107 this.source, this._contents, this._readerOffset, this._errorListener) { |
101 lineStarts.add(0); | 108 lineStarts.add(0); |
102 } | 109 } |
103 | 110 |
104 @override | 111 @override |
105 void appendToken(Token token) { | 112 void appendToken(Token token) { |
106 throw 'unsupported operation'; | 113 throw 'unsupported operation'; |
107 } | 114 } |
108 | 115 |
109 @override | 116 @override |
110 int bigSwitch(int next) { | 117 int bigSwitch(int next) { |
(...skipping 12 matching lines...) Expand all Loading... |
123 | 130 |
124 @override | 131 @override |
125 void reportError( | 132 void reportError( |
126 ScannerErrorCode errorCode, int offset, List<Object> arguments) { | 133 ScannerErrorCode errorCode, int offset, List<Object> arguments) { |
127 _errorListener | 134 _errorListener |
128 .onError(new AnalysisError(source, offset, 1, errorCode, arguments)); | 135 .onError(new AnalysisError(source, offset, 1, errorCode, arguments)); |
129 } | 136 } |
130 | 137 |
131 @override | 138 @override |
132 void setSourceStart(int line, int column) { | 139 void setSourceStart(int line, int column) { |
133 throw 'unsupported operation'; | 140 lineStarts.removeAt(0); |
| 141 int offset = _readerOffset; |
| 142 if (line < 1 || column < 1 || offset < 0 || (line + column - 2) >= offset) { |
| 143 return; |
| 144 } |
| 145 for (int i = 2; i < line; i++) { |
| 146 lineStarts.add(1); |
| 147 } |
| 148 lineStarts.add(offset - column + 1); |
134 } | 149 } |
135 | 150 |
136 @override | 151 @override |
137 Token get tail { | 152 Token get tail { |
138 throw 'unsupported operation'; | 153 throw 'unsupported operation'; |
139 } | 154 } |
140 | 155 |
141 @override | 156 @override |
142 Token tokenize() { | 157 Token tokenize() { |
143 // Note: Fasta always supports lazy assignment operators (`&&=` and `||=`), | |
144 // so we can ignore the `scanLazyAssignmentOperators` flag. | |
145 if (scanGenericMethodComments) { | |
146 // Fasta doesn't support generic method comments. | |
147 // TODO(danrubel): remove this once fasts support has been added. | |
148 throw 'No generic method comment support in Fasta'; | |
149 } | |
150 fasta.ScannerResult result = fasta.scanString(_contents, | 158 fasta.ScannerResult result = fasta.scanString(_contents, |
151 includeComments: _preserveComments, | 159 includeComments: _preserveComments, |
| 160 scanGenericMethodComments: scanGenericMethodComments, |
152 scanLazyAssignmentOperators: scanLazyAssignmentOperators); | 161 scanLazyAssignmentOperators: scanLazyAssignmentOperators); |
153 // fasta pretends there is an additional line at EOF | 162 // fasta pretends there is an additional line at EOF |
154 lineStarts | 163 lineStarts |
155 .addAll(result.lineStarts.sublist(1, result.lineStarts.length - 1)); | 164 .addAll(result.lineStarts.sublist(1, result.lineStarts.length - 1)); |
156 fasta.Token token = result.tokens; | 165 fasta.Token token = result.tokens; |
157 // The default recovery strategy used by scanString | 166 // The default recovery strategy used by scanString |
158 // places all error tokens at the head of the stream. | 167 // places all error tokens at the head of the stream. |
159 while (token.type == TokenType.BAD_INPUT) { | 168 while (token.type == TokenType.BAD_INPUT) { |
160 translateErrorToken(token, reportError); | 169 translateErrorToken(token, reportError); |
161 token = token.next; | 170 token = token.next; |
162 } | 171 } |
163 firstToken = token; | 172 firstToken = token; |
| 173 // Update all token offsets based upon the reader's starting offset |
| 174 if (_readerOffset != -1) { |
| 175 final int delta = _readerOffset + 1; |
| 176 do { |
| 177 token.offset += delta; |
| 178 token = token.next; |
| 179 } while (!token.isEof); |
| 180 } |
164 return firstToken; | 181 return firstToken; |
165 } | 182 } |
166 | 183 |
167 @override | 184 @override |
168 set preserveComments(bool preserveComments) { | 185 set preserveComments(bool preserveComments) { |
169 this._preserveComments = preserveComments; | 186 this._preserveComments = preserveComments; |
170 } | 187 } |
171 } | 188 } |
OLD | NEW |