Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(125)

Side by Side Diff: pkg/analyzer/lib/src/dart/scanner/scanner.dart

Issue 2794653002: fasta tokens implement analyzer token API (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « pkg/analyzer/lib/src/dart/ast/token.dart ('k') | pkg/analyzer/lib/src/fasta/token_utils.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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';
11 import 'package:analyzer/src/generated/source.dart'; 11 import 'package:analyzer/src/generated/source.dart';
12 import 'package:front_end/src/fasta/scanner.dart' as fasta; 12 import 'package:front_end/src/fasta/scanner.dart' as fasta;
13 import 'package:front_end/src/fasta/scanner/abstract_scanner.dart'
14 show fastaSupportsGenericMethodComments;
13 import 'package:front_end/src/fasta/scanner/precedence.dart' as fasta; 15 import 'package:front_end/src/fasta/scanner/precedence.dart' as fasta;
14 import 'package:front_end/src/scanner/errors.dart' show translateErrorToken; 16 import 'package:front_end/src/scanner/errors.dart' show translateErrorToken;
15 import 'package:front_end/src/scanner/scanner.dart' as fe; 17 import 'package:front_end/src/scanner/scanner.dart' as fe;
16 import 'package:front_end/src/scanner/token.dart' show Token; 18 import 'package:front_end/src/scanner/token.dart' show Token;
17 19
18 export 'package:analyzer/src/dart/error/syntactic_errors.dart'; 20 export 'package:analyzer/src/dart/error/syntactic_errors.dart';
19 export 'package:front_end/src/scanner/scanner.dart' show KeywordState; 21 export 'package:front_end/src/scanner/scanner.dart' show KeywordState;
20 22
21 /** 23 /**
22 * The class `Scanner` implements a scanner for Dart code. 24 * The class `Scanner` implements a scanner for Dart code.
(...skipping 11 matching lines...) Expand all
34 */ 36 */
35 final Source source; 37 final Source source;
36 38
37 /** 39 /**
38 * The error listener that will be informed of any errors that are found 40 * The error listener that will be informed of any errors that are found
39 * during the scan. 41 * during the scan.
40 */ 42 */
41 final AnalysisErrorListener _errorListener; 43 final AnalysisErrorListener _errorListener;
42 44
43 /** 45 /**
44 * A flag indicating whether the [Scanner] factory method
45 * will return a fasta based scanner or an analyzer based scanner.
46 */
47 static bool useFasta = false;
48
49 /**
50 * Initialize a newly created scanner to scan characters from the given 46 * Initialize a newly created scanner to scan characters from the given
51 * [source]. The given character [reader] will be used to read the characters 47 * [source]. The given character [reader] will be used to read the characters
52 * in the source. The given [_errorListener] will be informed of any errors 48 * in the source. The given [_errorListener] will be informed of any errors
53 * that are found. 49 * that are found.
54 */ 50 */
55 factory Scanner(Source source, CharacterReader reader, 51 factory Scanner(Source source, CharacterReader reader,
56 AnalysisErrorListener errorListener) => 52 AnalysisErrorListener errorListener) =>
57 useFasta 53 fe.Scanner.useFasta
58 ? new _Scanner2(source, reader.getContents(), errorListener) 54 ? new _Scanner2(source, reader.getContents(), errorListener)
59 : new Scanner._(source, reader, errorListener); 55 : new Scanner._(source, reader, errorListener);
60 56
61 Scanner._(this.source, CharacterReader reader, this._errorListener) 57 Scanner._(this.source, CharacterReader reader, this._errorListener)
62 : super.create(reader); 58 : super.create(reader);
63 59
64 @override 60 @override
65 void reportError( 61 void reportError(
66 ScannerErrorCode errorCode, int offset, List<Object> arguments) { 62 ScannerErrorCode errorCode, int offset, List<Object> arguments) {
67 _errorListener 63 _errorListener
(...skipping 18 matching lines...) Expand all
86 * during the scan. 82 * during the scan.
87 */ 83 */
88 final AnalysisErrorListener _errorListener; 84 final AnalysisErrorListener _errorListener;
89 85
90 /** 86 /**
91 * The flag specifying whether documentation comments should be parsed. 87 * The flag specifying whether documentation comments should be parsed.
92 */ 88 */
93 bool _preserveComments = true; 89 bool _preserveComments = true;
94 90
95 @override 91 @override
96 List<int> lineStarts; 92 final List<int> lineStarts = new List<int>();
Brian Wilkerson 2017/03/31 20:39:05 nit: "<int>[]" is shorter.
danrubel 2017/04/04 12:45:41 Good idea. Done.
97 93
98 @override 94 @override
99 Token firstToken; 95 Token firstToken;
100 96
101 @override 97 @override
102 bool scanGenericMethodComments = false; 98 bool scanGenericMethodComments = false;
103 99
104 @override 100 @override
105 bool scanLazyAssignmentOperators = false; 101 bool scanLazyAssignmentOperators = false;
106 102
107 _Scanner2(this.source, this._contents, this._errorListener); 103 _Scanner2(this.source, this._contents, this._errorListener) {
104 lineStarts.add(0);
105 }
108 106
109 @override 107 @override
110 void appendToken(Token token) { 108 void appendToken(Token token) {
111 throw 'unsupported operation'; 109 throw 'unsupported operation';
112 } 110 }
113 111
114 @override 112 @override
115 int bigSwitch(int next) { 113 int bigSwitch(int next) {
116 throw 'unsupported operation'; 114 throw 'unsupported operation';
117 } 115 }
(...skipping 22 matching lines...) Expand all
140 138
141 @override 139 @override
142 Token get tail { 140 Token get tail {
143 throw 'unsupported operation'; 141 throw 'unsupported operation';
144 } 142 }
145 143
146 @override 144 @override
147 Token tokenize() { 145 Token tokenize() {
148 // Note: Fasta always supports lazy assignment operators (`&&=` and `||=`), 146 // Note: Fasta always supports lazy assignment operators (`&&=` and `||=`),
149 // so we can ignore the `scanLazyAssignmentOperators` flag. 147 // so we can ignore the `scanLazyAssignmentOperators` flag.
150 if (scanGenericMethodComments) { 148 if (scanGenericMethodComments && !fastaSupportsGenericMethodComments) {
151 // Fasta doesn't support generic method comments. 149 // Fasta doesn't support generic method comments.
152 // TODO(danrubel): remove this once fasts support has been added. 150 // TODO(danrubel): remove this once fasts support has been added.
153 throw 'No generic method comment support in Fasta'; 151 throw 'No generic method comment support in Fasta';
154 } 152 }
155 fasta.ScannerResult result = 153 fasta.ScannerResult result =
156 fasta.scanString(_contents, includeComments: _preserveComments); 154 fasta.scanString(_contents, includeComments: _preserveComments);
157 // fasta pretends there is an additional line at EOF 155 // fasta pretends there is an additional line at EOF
158 lineStarts = result.lineStarts.sublist(0, result.lineStarts.length - 1); 156 lineStarts
157 .addAll(result.lineStarts.sublist(1, result.lineStarts.length - 1));
159 fasta.Token token = result.tokens; 158 fasta.Token token = result.tokens;
160 // The default recovery strategy used by scanString 159 // The default recovery strategy used by scanString
161 // places all error tokens at the head of the stream. 160 // places all error tokens at the head of the stream.
162 while (token.info == fasta.BAD_INPUT_INFO) { 161 while (token.info == fasta.BAD_INPUT_INFO) {
163 translateErrorToken(token, reportError); 162 translateErrorToken(token, reportError);
164 token = token.next; 163 token = token.next;
165 } 164 }
166 firstToken = token; 165 firstToken = token;
167 return firstToken; 166 return firstToken;
168 } 167 }
169 168
170 @override 169 @override
171 set preserveComments(bool preserveComments) { 170 set preserveComments(bool preserveComments) {
172 this._preserveComments = preserveComments; 171 this._preserveComments = preserveComments;
173 } 172 }
174 } 173 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/dart/ast/token.dart ('k') | pkg/analyzer/lib/src/fasta/token_utils.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698