| 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:io' show FileSystemException; | |
| 10 | |
| 11 import 'dart:typed_data' show Uint8List; | 9 import 'dart:typed_data' show Uint8List; |
| 12 | 10 |
| 13 import 'package:kernel/ast.dart' show Program; | 11 import 'package:kernel/ast.dart' show Program; |
| 14 | 12 |
| 15 import 'package:kernel/class_hierarchy.dart' show ClassHierarchy; | 13 import 'package:kernel/class_hierarchy.dart' show ClassHierarchy; |
| 16 | 14 |
| 17 import 'package:kernel/core_types.dart' show CoreTypes; | 15 import 'package:kernel/core_types.dart' show CoreTypes; |
| 18 | 16 |
| 19 import '../builder/builder.dart' show Builder, ClassBuilder, LibraryBuilder; | 17 import '../builder/builder.dart' show Builder, ClassBuilder, LibraryBuilder; |
| 20 | 18 |
| 21 import '../compiler_context.dart' show CompilerContext; | 19 import '../compiler_context.dart' show CompilerContext; |
| 22 | 20 |
| 23 import '../errors.dart' show inputError; | 21 import '../errors.dart' show inputError; |
| 24 | 22 |
| 25 import '../export.dart' show Export; | 23 import '../export.dart' show Export; |
| 26 | 24 |
| 27 import '../loader.dart' show Loader; | 25 import '../loader.dart' show Loader; |
| 28 | 26 |
| 29 import '../parser/class_member_parser.dart' show ClassMemberParser; | 27 import '../parser/class_member_parser.dart' show ClassMemberParser; |
| 30 | 28 |
| 31 import '../scanner.dart' show ErrorToken, ScannerResult, Token, scan; | 29 import '../scanner.dart' show ErrorToken, ScannerResult, Token, scan; |
| 32 | 30 |
| 33 import '../scanner/io.dart' show readBytesFromFile; | 31 import '../io.dart' show readBytesFromFile; |
| 34 | 32 |
| 35 import '../target_implementation.dart' show TargetImplementation; | 33 import '../target_implementation.dart' show TargetImplementation; |
| 36 | 34 |
| 37 import 'diet_listener.dart' show DietListener; | 35 import 'diet_listener.dart' show DietListener; |
| 38 | 36 |
| 39 import 'diet_parser.dart' show DietParser; | 37 import 'diet_parser.dart' show DietParser; |
| 40 | 38 |
| 41 import 'outline_builder.dart' show OutlineBuilder; | 39 import 'outline_builder.dart' show OutlineBuilder; |
| 42 | 40 |
| 43 import 'source_class_builder.dart' show SourceClassBuilder; | 41 import 'source_class_builder.dart' show SourceClassBuilder; |
| 44 | 42 |
| 45 import 'source_library_builder.dart' show SourceLibraryBuilder; | 43 import 'source_library_builder.dart' show SourceLibraryBuilder; |
| 46 | 44 |
| 47 class SourceLoader<L> extends Loader<L> { | 45 class SourceLoader<L> extends Loader<L> { |
| 48 final Map<Uri, List<int>> sourceBytes = <Uri, List<int>>{}; | 46 final Map<Uri, List<int>> sourceBytes = <Uri, List<int>>{}; |
| 49 final bool excludeSource = CompilerContext.current.options.excludeSource; | 47 final bool excludeSource = CompilerContext.current.options.excludeSource; |
| 50 | 48 |
| 51 // Used when building directly to kernel. | 49 // Used when building directly to kernel. |
| 52 ClassHierarchy hierarchy; | 50 ClassHierarchy hierarchy; |
| 53 CoreTypes coreTypes; | 51 CoreTypes coreTypes; |
| 54 | 52 |
| 55 SourceLoader(TargetImplementation target) : super(target); | 53 SourceLoader(TargetImplementation target) : super(target); |
| 56 | 54 |
| 57 Future<Token> tokenize(SourceLibraryBuilder library, | 55 Future<Token> tokenize(SourceLibraryBuilder library, |
| 58 {bool suppressLexicalErrors: false}) async { | 56 {bool suppressLexicalErrors: false}) async { |
| 59 Uri uri = library.fileUri; | 57 Uri uri = library.fileUri; |
| 60 if (uri == null || uri.scheme != "file") { | 58 if (uri == null || uri.scheme != "file") { |
| 61 return inputError(library.uri, -1, "Not found: ${library.uri}."); | 59 return inputError(library.uri, -1, "Not found: ${library.uri}."); |
| 62 } | 60 } |
| 63 try { | 61 List<int> bytes = sourceBytes[uri]; |
| 64 List<int> bytes = sourceBytes[uri]; | 62 if (bytes == null) { |
| 65 if (bytes == null) { | 63 bytes = sourceBytes[uri] = await readBytesFromFile(uri); |
| 66 bytes = sourceBytes[uri] = await readBytesFromFile(uri); | 64 } |
| 65 byteCount += bytes.length - 1; |
| 66 ScannerResult result = scan(bytes); |
| 67 Token token = result.tokens; |
| 68 if (!suppressLexicalErrors) { |
| 69 List<int> source = getSource(bytes); |
| 70 target.addSourceInformation(library.fileUri, result.lineStarts, source); |
| 71 } |
| 72 while (token is ErrorToken) { |
| 73 if (!suppressLexicalErrors) { |
| 74 ErrorToken error = token; |
| 75 library.addCompileTimeError(token.charOffset, error.assertionMessage, |
| 76 fileUri: uri); |
| 67 } | 77 } |
| 68 byteCount += bytes.length - 1; | 78 token = token.next; |
| 69 ScannerResult result = scan(bytes); | |
| 70 Token token = result.tokens; | |
| 71 if (!suppressLexicalErrors) { | |
| 72 List<int> source = getSource(bytes); | |
| 73 target.addSourceInformation(library.fileUri, result.lineStarts, source); | |
| 74 } | |
| 75 while (token is ErrorToken) { | |
| 76 if (!suppressLexicalErrors) { | |
| 77 ErrorToken error = token; | |
| 78 library.addCompileTimeError(token.charOffset, error.assertionMessage, | |
| 79 fileUri: uri); | |
| 80 } | |
| 81 token = token.next; | |
| 82 } | |
| 83 return token; | |
| 84 } on FileSystemException catch (e) { | |
| 85 String message = e.message; | |
| 86 String osMessage = e.osError?.message; | |
| 87 if (osMessage != null && osMessage.isNotEmpty) { | |
| 88 message = osMessage; | |
| 89 } | |
| 90 return inputError(uri, -1, message); | |
| 91 } | 79 } |
| 80 return token; |
| 92 } | 81 } |
| 93 | 82 |
| 94 List<int> getSource(List<int> bytes) { | 83 List<int> getSource(List<int> bytes) { |
| 95 if (excludeSource) return const <int>[]; | 84 if (excludeSource) return const <int>[]; |
| 96 | 85 |
| 97 // bytes is 0-terminated. We don't want that included. | 86 // bytes is 0-terminated. We don't want that included. |
| 98 if (bytes is Uint8List) { | 87 if (bytes is Uint8List) { |
| 99 return new Uint8List.view( | 88 return new Uint8List.view( |
| 100 bytes.buffer, bytes.offsetInBytes, bytes.length - 1); | 89 bytes.buffer, bytes.offsetInBytes, bytes.length - 1); |
| 101 } | 90 } |
| (...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 362 void checkOverrides(List<SourceClassBuilder> sourceClasses) { | 351 void checkOverrides(List<SourceClassBuilder> sourceClasses) { |
| 363 assert(hierarchy != null); | 352 assert(hierarchy != null); |
| 364 for (SourceClassBuilder builder in sourceClasses) { | 353 for (SourceClassBuilder builder in sourceClasses) { |
| 365 builder.checkOverrides(hierarchy); | 354 builder.checkOverrides(hierarchy); |
| 366 } | 355 } |
| 367 ticker.logMs("Checked overrides"); | 356 ticker.logMs("Checked overrides"); |
| 368 } | 357 } |
| 369 | 358 |
| 370 List<Uri> getDependencies() => sourceBytes.keys.toList(); | 359 List<Uri> getDependencies() => sourceBytes.keys.toList(); |
| 371 } | 360 } |
| OLD | NEW |