OLD | NEW |
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 library fasta.source_loader; | 5 library fasta.source_loader; |
6 | 6 |
7 import 'dart:async' show Future; | 7 import 'dart:async' show Future; |
8 | 8 |
9 import 'dart:typed_data' show Uint8List; | 9 import 'dart:typed_data' show Uint8List; |
10 | 10 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 import 'outline_builder.dart' show OutlineBuilder; | 68 import 'outline_builder.dart' show OutlineBuilder; |
69 | 69 |
70 import 'source_class_builder.dart' show SourceClassBuilder; | 70 import 'source_class_builder.dart' show SourceClassBuilder; |
71 | 71 |
72 import 'source_library_builder.dart' show SourceLibraryBuilder; | 72 import 'source_library_builder.dart' show SourceLibraryBuilder; |
73 | 73 |
74 class SourceLoader<L> extends Loader<L> { | 74 class SourceLoader<L> extends Loader<L> { |
75 /// The [FileSystem] which should be used to access files. | 75 /// The [FileSystem] which should be used to access files. |
76 final FileSystem fileSystem; | 76 final FileSystem fileSystem; |
77 | 77 |
| 78 /// Whether comments should be scanned and parsed. |
| 79 final bool includeComments; |
| 80 |
78 final Map<Uri, List<int>> sourceBytes = <Uri, List<int>>{}; | 81 final Map<Uri, List<int>> sourceBytes = <Uri, List<int>>{}; |
79 | 82 |
80 final bool excludeSource = CompilerContext.current.options.excludeSource; | 83 final bool excludeSource = CompilerContext.current.options.excludeSource; |
81 | 84 |
82 // Used when building directly to kernel. | 85 // Used when building directly to kernel. |
83 ClassHierarchy hierarchy; | 86 ClassHierarchy hierarchy; |
84 CoreTypes coreTypes; | 87 CoreTypes coreTypes; |
85 | 88 |
86 TypeInferenceEngine typeInferenceEngine; | 89 TypeInferenceEngine typeInferenceEngine; |
87 | 90 |
88 Instrumentation instrumentation; | 91 Instrumentation instrumentation; |
89 | 92 |
90 SourceLoader(this.fileSystem, KernelTarget target) : super(target); | 93 SourceLoader(this.fileSystem, this.includeComments, KernelTarget target) |
| 94 : super(target); |
91 | 95 |
92 Future<Token> tokenize(SourceLibraryBuilder library, | 96 Future<Token> tokenize(SourceLibraryBuilder library, |
93 {bool suppressLexicalErrors: false}) async { | 97 {bool suppressLexicalErrors: false}) async { |
94 Uri uri = library.fileUri; | 98 Uri uri = library.fileUri; |
95 // TODO(sigmund): source-loader shouldn't check schemes, but defer to the | 99 // TODO(sigmund): source-loader shouldn't check schemes, but defer to the |
96 // underlying file system to decide whether it is supported. | 100 // underlying file system to decide whether it is supported. |
97 if (uri == null || uri.scheme != "file" && uri.scheme != "multi-root") { | 101 if (uri == null || uri.scheme != "file" && uri.scheme != "multi-root") { |
98 return deprecated_inputError( | 102 return deprecated_inputError( |
99 library.uri, -1, "Not found: ${library.uri}."); | 103 library.uri, -1, "Not found: ${library.uri}."); |
100 } | 104 } |
101 | 105 |
102 // Get the library text from the cache, or read from the file system. | 106 // Get the library text from the cache, or read from the file system. |
103 List<int> bytes = sourceBytes[uri]; | 107 List<int> bytes = sourceBytes[uri]; |
104 if (bytes == null) { | 108 if (bytes == null) { |
105 try { | 109 try { |
106 List<int> rawBytes = await fileSystem.entityForUri(uri).readAsBytes(); | 110 List<int> rawBytes = await fileSystem.entityForUri(uri).readAsBytes(); |
107 Uint8List zeroTerminatedBytes = new Uint8List(rawBytes.length + 1); | 111 Uint8List zeroTerminatedBytes = new Uint8List(rawBytes.length + 1); |
108 zeroTerminatedBytes.setRange(0, rawBytes.length, rawBytes); | 112 zeroTerminatedBytes.setRange(0, rawBytes.length, rawBytes); |
109 bytes = zeroTerminatedBytes; | 113 bytes = zeroTerminatedBytes; |
110 sourceBytes[uri] = bytes; | 114 sourceBytes[uri] = bytes; |
111 } on FileSystemException catch (e) { | 115 } on FileSystemException catch (e) { |
112 return deprecated_inputError(uri, -1, e.message); | 116 return deprecated_inputError(uri, -1, e.message); |
113 } | 117 } |
114 } | 118 } |
115 | 119 |
116 byteCount += bytes.length - 1; | 120 byteCount += bytes.length - 1; |
117 ScannerResult result = scan(bytes); | 121 ScannerResult result = scan(bytes, includeComments: includeComments); |
118 Token token = result.tokens; | 122 Token token = result.tokens; |
119 if (!suppressLexicalErrors) { | 123 if (!suppressLexicalErrors) { |
120 List<int> source = getSource(bytes); | 124 List<int> source = getSource(bytes); |
121 target.addSourceInformation(library.fileUri, result.lineStarts, source); | 125 target.addSourceInformation(library.fileUri, result.lineStarts, source); |
122 } | 126 } |
123 while (token is ErrorToken) { | 127 while (token is ErrorToken) { |
124 if (!suppressLexicalErrors) { | 128 if (!suppressLexicalErrors) { |
125 ErrorToken error = token; | 129 ErrorToken error = token; |
126 library.addCompileTimeError( | 130 library.addCompileTimeError( |
127 templateUnspecified.withArguments(error.assertionMessage), | 131 templateUnspecified.withArguments(error.assertionMessage), |
(...skipping 401 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
529 Expression throwCompileConstantError(Expression error) { | 533 Expression throwCompileConstantError(Expression error) { |
530 return target.backendTarget.throwCompileConstantError(coreTypes, error); | 534 return target.backendTarget.throwCompileConstantError(coreTypes, error); |
531 } | 535 } |
532 | 536 |
533 Expression buildCompileTimeError(Message message, int offset, Uri uri) { | 537 Expression buildCompileTimeError(Message message, int offset, Uri uri) { |
534 String text = target.context | 538 String text = target.context |
535 .format(message.withLocation(uri, offset), Severity.error); | 539 .format(message.withLocation(uri, offset), Severity.error); |
536 return target.backendTarget.buildCompileTimeError(coreTypes, text, offset); | 540 return target.backendTarget.buildCompileTimeError(coreTypes, text, offset); |
537 } | 541 } |
538 } | 542 } |
OLD | NEW |