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

Side by Side Diff: pkg/analyzer/lib/src/dart/analysis/file_state.dart

Issue 2745163004: Optionally use Fasta with the new analysis driver. (Closed)
Patch Set: Created 3 years, 9 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 | « no previous file | pkg/analyzer/test/src/dart/analysis/driver_test.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) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, 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 import 'dart:convert'; 5 import 'dart:convert';
6 import 'dart:typed_data'; 6 import 'dart:typed_data';
7 7
8 import 'package:analyzer/dart/ast/ast.dart'; 8 import 'package:analyzer/dart/ast/ast.dart';
9 import 'package:analyzer/dart/ast/token.dart'; 9 import 'package:analyzer/dart/ast/token.dart';
10 import 'package:analyzer/error/listener.dart'; 10 import 'package:analyzer/error/listener.dart';
(...skipping 10 matching lines...) Expand all
21 import 'package:analyzer/src/generated/source.dart'; 21 import 'package:analyzer/src/generated/source.dart';
22 import 'package:analyzer/src/generated/utilities_dart.dart'; 22 import 'package:analyzer/src/generated/utilities_dart.dart';
23 import 'package:analyzer/src/source/source_resource.dart'; 23 import 'package:analyzer/src/source/source_resource.dart';
24 import 'package:analyzer/src/summary/api_signature.dart'; 24 import 'package:analyzer/src/summary/api_signature.dart';
25 import 'package:analyzer/src/summary/format.dart'; 25 import 'package:analyzer/src/summary/format.dart';
26 import 'package:analyzer/src/summary/idl.dart'; 26 import 'package:analyzer/src/summary/idl.dart';
27 import 'package:analyzer/src/summary/name_filter.dart'; 27 import 'package:analyzer/src/summary/name_filter.dart';
28 import 'package:analyzer/src/summary/summarize_ast.dart'; 28 import 'package:analyzer/src/summary/summarize_ast.dart';
29 import 'package:convert/convert.dart'; 29 import 'package:convert/convert.dart';
30 import 'package:crypto/crypto.dart'; 30 import 'package:crypto/crypto.dart';
31 import 'package:front_end/src/fasta/analyzer/ast_builder.dart' as fasta;
32 import 'package:front_end/src/fasta/analyzer/element_store.dart' as fasta;
33 import 'package:front_end/src/fasta/analyzer/mock_element.dart' as fasta;
34 import 'package:front_end/src/fasta/builder/builder.dart' as fasta;
35 import 'package:front_end/src/fasta/builder/scope.dart' as fasta;
36 import 'package:front_end/src/fasta/parser/parser.dart' as fasta;
37 import 'package:front_end/src/fasta/scanner.dart' as fasta;
31 import 'package:meta/meta.dart'; 38 import 'package:meta/meta.dart';
32 39
33 /** 40 /**
34 * [FileContentOverlay] is used to temporary override content of files. 41 * [FileContentOverlay] is used to temporary override content of files.
35 */ 42 */
36 class FileContentOverlay { 43 class FileContentOverlay {
37 final _map = <String, String>{}; 44 final _map = <String, String>{};
38 45
39 /** 46 /**
40 * Return the content of the file with the given [path], or `null` the 47 * Return the content of the file with the given [path], or `null` the
(...skipping 20 matching lines...) Expand all
61 /** 68 /**
62 * Information about a file being analyzed, explicitly or implicitly. 69 * Information about a file being analyzed, explicitly or implicitly.
63 * 70 *
64 * It provides a consistent view on its properties. 71 * It provides a consistent view on its properties.
65 * 72 *
66 * The properties are not guaranteed to represent the most recent state 73 * The properties are not guaranteed to represent the most recent state
67 * of the file system. To update the file to the most recent state, [refresh] 74 * of the file system. To update the file to the most recent state, [refresh]
68 * should be called. 75 * should be called.
69 */ 76 */
70 class FileState { 77 class FileState {
78 static const bool USE_FASTA_PARSER = false;
79
71 final FileSystemState _fsState; 80 final FileSystemState _fsState;
72 81
73 /** 82 /**
74 * The absolute path of the file. 83 * The absolute path of the file.
75 */ 84 */
76 final String path; 85 final String path;
77 86
78 /** 87 /**
79 * The absolute URI of the file. 88 * The absolute URI of the file.
80 */ 89 */
81 final Uri uri; 90 final Uri uri;
82 91
83 /** 92 /**
84 * The [Source] of the file with the [uri]. 93 * The [Source] of the file with the [uri].
85 */ 94 */
86 Source source; 95 Source source;
87 96
88 bool _exists; 97 bool _exists;
98 List<int> _contentBytes;
89 String _content; 99 String _content;
90 String _contentHash; 100 String _contentHash;
91 LineInfo _lineInfo; 101 LineInfo _lineInfo;
92 Set<String> _definedTopLevelNames; 102 Set<String> _definedTopLevelNames;
93 Set<String> _definedClassMemberNames; 103 Set<String> _definedClassMemberNames;
94 Set<String> _referencedNames; 104 Set<String> _referencedNames;
95 UnlinkedUnit _unlinked; 105 UnlinkedUnit _unlinked;
96 List<int> _apiSignature; 106 List<int> _apiSignature;
97 107
98 List<FileState> _importedFiles; 108 List<FileState> _importedFiles;
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after
338 bool operator ==(Object other) { 348 bool operator ==(Object other) {
339 return other is FileState && other.uri == uri; 349 return other is FileState && other.uri == uri;
340 } 350 }
341 351
342 /** 352 /**
343 * Return a new parsed unresolved [CompilationUnit]. 353 * Return a new parsed unresolved [CompilationUnit].
344 */ 354 */
345 CompilationUnit parse(AnalysisErrorListener errorListener) { 355 CompilationUnit parse(AnalysisErrorListener errorListener) {
346 AnalysisOptions analysisOptions = _fsState._analysisOptions; 356 AnalysisOptions analysisOptions = _fsState._analysisOptions;
347 357
348 CharSequenceReader reader = new CharSequenceReader(content); 358 if (USE_FASTA_PARSER) {
349 Scanner scanner = new Scanner(source, reader, errorListener); 359 try {
350 scanner.scanGenericMethodComments = analysisOptions.strongMode; 360 fasta.ScannerResult scanResult = fasta.scan(_contentBytes);
351 Token token = scanner.tokenize();
352 LineInfo lineInfo = new LineInfo(scanner.lineStarts);
353 361
354 Parser parser = new Parser(source, errorListener); 362 var listener = new fasta.AstBuilder(null, null,
355 parser.enableAssertInitializer = analysisOptions.enableAssertInitializer; 363 new _FastaElementStoreProxy(), new _FastaEmptyScope(), uri);
356 parser.parseGenericMethodComments = analysisOptions.strongMode; 364 var parser = new fasta.Parser(listener);
357 CompilationUnit unit = parser.parseCompilationUnit(token); 365 parser.parseUnit(scanResult.tokens);
358 unit.lineInfo = lineInfo; 366 var unit = listener.pop() as CompilationUnit;
359 return unit; 367
368 LineInfo lineInfo = new LineInfo(scanResult.lineStarts);
369 unit.lineInfo = lineInfo;
370 return unit;
371 } catch (e, st) {
372 print(e);
373 print(st);
374 rethrow;
375 }
376 } else {
377 CharSequenceReader reader = new CharSequenceReader(content);
378 Scanner scanner = new Scanner(source, reader, errorListener);
379 scanner.scanGenericMethodComments = analysisOptions.strongMode;
380 Token token = scanner.tokenize();
381 LineInfo lineInfo = new LineInfo(scanner.lineStarts);
382
383 Parser parser = new Parser(source, errorListener);
384 parser.enableAssertInitializer = analysisOptions.enableAssertInitializer;
385 parser.parseGenericMethodComments = analysisOptions.strongMode;
386 CompilationUnit unit = parser.parseCompilationUnit(token);
387 unit.lineInfo = lineInfo;
388 return unit;
389 }
360 } 390 }
361 391
362 /** 392 /**
363 * Read the file content and ensure that all of the file properties are 393 * Read the file content and ensure that all of the file properties are
364 * consistent with the read content, including API signature. 394 * consistent with the read content, including API signature.
365 * 395 *
366 * Return `true` if the API signature changed since the last refresh. 396 * Return `true` if the API signature changed since the last refresh.
367 */ 397 */
368 bool refresh() { 398 bool refresh() {
369 // Read the content. 399 // Read the content.
370 try { 400 try {
371 _content = _fsState._contentOverlay[path]; 401 _content = _fsState._contentOverlay[path];
372 _content ??= _fsState._resourceProvider.getFile(path).readAsStringSync(); 402 _content ??= _fsState._resourceProvider.getFile(path).readAsStringSync();
373 _exists = true; 403 _exists = true;
374 } catch (_) { 404 } catch (_) {
375 _content = ''; 405 _content = '';
376 _exists = false; 406 _exists = false;
377 } 407 }
378 408
409 if (USE_FASTA_PARSER) {
410 var bytes = UTF8.encode(_content);
411 _contentBytes = new Uint8List(bytes.length + 1);
412 _contentBytes.setRange(0, bytes.length, bytes);
413 _contentBytes[_contentBytes.length - 1] = 0;
414 }
415
379 // Compute the content hash. 416 // Compute the content hash.
380 List<int> contentBytes = UTF8.encode(_content); 417 List<int> contentBytes = UTF8.encode(_content);
381 { 418 {
382 List<int> hashBytes = md5.convert(contentBytes).bytes; 419 List<int> hashBytes = md5.convert(contentBytes).bytes;
383 _contentHash = hex.encode(hashBytes); 420 _contentHash = hex.encode(hashBytes);
384 } 421 }
385 422
386 // Prepare the unlinked bundle key. 423 // Prepare the unlinked bundle key.
387 String unlinkedKey; 424 String unlinkedKey;
388 { 425 {
(...skipping 344 matching lines...) Expand 10 before | Expand all | Expand 10 after
733 .where((f) => f._transitiveFiles == null) 770 .where((f) => f._transitiveFiles == null)
734 .toSet(); 771 .toSet();
735 } 772 }
736 773
737 Set<FileState> get filesWithoutTransitiveSignature { 774 Set<FileState> get filesWithoutTransitiveSignature {
738 return state._uriToFile.values 775 return state._uriToFile.values
739 .where((f) => f._transitiveSignature == null) 776 .where((f) => f._transitiveSignature == null)
740 .toSet(); 777 .toSet();
741 } 778 }
742 } 779 }
780
781 class _FastaElementProxy implements fasta.KernelClassElement {
782 @override
783 final fasta.KernelInterfaceType rawType = new _FastaInterfaceTypeProxy();
784
785 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
786 }
787
788 class _FastaElementStoreProxy implements fasta.ElementStore {
789 final _elements = <fasta.Builder, _FastaElementProxy>{};
790
791 @override
792 _FastaElementProxy operator [](fasta.Builder builder) =>
793 _elements.putIfAbsent(builder, () => new _FastaElementProxy());
794
795 @override
796 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
797 }
798
799 class _FastaEmptyScope extends fasta.Scope {
800 _FastaEmptyScope() : super({}, null);
801 }
802
803 class _FastaInterfaceTypeProxy implements fasta.KernelInterfaceType {
804 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
805 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analyzer/test/src/dart/analysis/driver_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698