| 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; | 9 import 'dart:io' show FileSystemException; |
| 10 | 10 |
| 11 import 'dart:typed_data' show Uint8List; |
| 12 |
| 11 import '../scanner/io.dart' show readBytesFromFile; | 13 import '../scanner/io.dart' show readBytesFromFile; |
| 12 | 14 |
| 13 import '../scanner.dart' show ErrorToken, ScannerResult, Token, scan; | 15 import '../scanner.dart' show ErrorToken, ScannerResult, Token, scan; |
| 14 | 16 |
| 15 import '../parser/class_member_parser.dart' show ClassMemberParser; | 17 import '../parser/class_member_parser.dart' show ClassMemberParser; |
| 16 | 18 |
| 17 import 'package:kernel/ast.dart' show Program; | 19 import 'package:kernel/ast.dart' show Program; |
| 18 | 20 |
| 19 import 'package:kernel/class_hierarchy.dart' show ClassHierarchy; | 21 import 'package:kernel/class_hierarchy.dart' show ClassHierarchy; |
| 20 | 22 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 33 import '../loader.dart' show Loader; | 35 import '../loader.dart' show Loader; |
| 34 | 36 |
| 35 import '../target_implementation.dart' show TargetImplementation; | 37 import '../target_implementation.dart' show TargetImplementation; |
| 36 | 38 |
| 37 import 'diet_listener.dart' show DietListener; | 39 import 'diet_listener.dart' show DietListener; |
| 38 | 40 |
| 39 import 'diet_parser.dart' show DietParser; | 41 import 'diet_parser.dart' show DietParser; |
| 40 | 42 |
| 41 import 'source_library_builder.dart' show SourceLibraryBuilder; | 43 import 'source_library_builder.dart' show SourceLibraryBuilder; |
| 42 | 44 |
| 45 import '../compiler_context.dart' show CompilerContext; |
| 46 |
| 43 class SourceLoader<L> extends Loader<L> { | 47 class SourceLoader<L> extends Loader<L> { |
| 44 final Map<Uri, List<int>> sourceBytes = <Uri, List<int>>{}; | 48 final Map<Uri, List<int>> sourceBytes = <Uri, List<int>>{}; |
| 49 final includeSource = CompilerContext.current.options.includeSource; |
| 45 | 50 |
| 46 // Used when building directly to kernel. | 51 // Used when building directly to kernel. |
| 47 ClassHierarchy hierarchy; | 52 ClassHierarchy hierarchy; |
| 48 CoreTypes coreTypes; | 53 CoreTypes coreTypes; |
| 49 | 54 |
| 50 SourceLoader(TargetImplementation target) : super(target); | 55 SourceLoader(TargetImplementation target) : super(target); |
| 51 | 56 |
| 52 Future<Token> tokenize(SourceLibraryBuilder library, | 57 Future<Token> tokenize(SourceLibraryBuilder library, |
| 53 {bool suppressLexicalErrors: false}) async { | 58 {bool suppressLexicalErrors: false}) async { |
| 54 Uri uri = library.fileUri; | 59 Uri uri = library.fileUri; |
| 55 if (uri == null || uri.scheme != "file") { | 60 if (uri == null || uri.scheme != "file") { |
| 56 return inputError(library.uri, -1, "Not found: ${library.uri}."); | 61 return inputError(library.uri, -1, "Not found: ${library.uri}."); |
| 57 } | 62 } |
| 58 try { | 63 try { |
| 59 List<int> bytes = sourceBytes[uri]; | 64 List<int> bytes = sourceBytes[uri]; |
| 60 if (bytes == null) { | 65 if (bytes == null) { |
| 61 bytes = sourceBytes[uri] = await readBytesFromFile(uri); | 66 bytes = sourceBytes[uri] = await readBytesFromFile(uri); |
| 62 } | 67 } |
| 63 byteCount += bytes.length - 1; | 68 byteCount += bytes.length - 1; |
| 64 ScannerResult result = scan(bytes); | 69 ScannerResult result = scan(bytes); |
| 65 Token token = result.tokens; | 70 Token token = result.tokens; |
| 66 if (!suppressLexicalErrors) { | 71 if (!suppressLexicalErrors) { |
| 67 target.addLineStarts(library.fileUri, result.lineStarts); | 72 List<int> source = getSource(bytes); |
| 73 target.addSourceInformation(library.fileUri, result.lineStarts, source); |
| 68 } | 74 } |
| 69 while (token is ErrorToken) { | 75 while (token is ErrorToken) { |
| 70 if (!suppressLexicalErrors) { | 76 if (!suppressLexicalErrors) { |
| 71 ErrorToken error = token; | 77 ErrorToken error = token; |
| 72 printUnexpected(uri, token.charOffset, error.assertionMessage); | 78 printUnexpected(uri, token.charOffset, error.assertionMessage); |
| 73 } | 79 } |
| 74 token = token.next; | 80 token = token.next; |
| 75 } | 81 } |
| 76 return token; | 82 return token; |
| 77 } on FileSystemException catch (e) { | 83 } on FileSystemException catch (e) { |
| 78 String message = e.message; | 84 String message = e.message; |
| 79 String osMessage = e.osError?.message; | 85 String osMessage = e.osError?.message; |
| 80 if (osMessage != null && osMessage.isNotEmpty) { | 86 if (osMessage != null && osMessage.isNotEmpty) { |
| 81 message = osMessage; | 87 message = osMessage; |
| 82 } | 88 } |
| 83 return inputError(uri, -1, message); | 89 return inputError(uri, -1, message); |
| 84 } | 90 } |
| 85 } | 91 } |
| 86 | 92 |
| 93 List<int> getSource(List<int> bytes) { |
| 94 if (!includeSource) return const <int>[]; |
| 95 |
| 96 // bytes is 0-terminated. We don't want that included. |
| 97 if (bytes is Uint8List) { |
| 98 return new Uint8List.view( |
| 99 bytes.buffer, bytes.offsetInBytes, bytes.length - 1); |
| 100 } |
| 101 return bytes.sublist(0, bytes.length - 1); |
| 102 } |
| 103 |
| 87 Future<Null> buildOutline(SourceLibraryBuilder library) async { | 104 Future<Null> buildOutline(SourceLibraryBuilder library) async { |
| 88 Token tokens = await tokenize(library); | 105 Token tokens = await tokenize(library); |
| 89 if (tokens == null) return; | 106 if (tokens == null) return; |
| 90 OutlineBuilder listener = new OutlineBuilder(library); | 107 OutlineBuilder listener = new OutlineBuilder(library); |
| 91 new ClassMemberParser(listener).parseUnit(tokens); | 108 new ClassMemberParser(listener).parseUnit(tokens); |
| 92 } | 109 } |
| 93 | 110 |
| 94 Future<Null> buildBody(LibraryBuilder library) async { | 111 Future<Null> buildBody(LibraryBuilder library) async { |
| 95 if (library is SourceLibraryBuilder) { | 112 if (library is SourceLibraryBuilder) { |
| 96 // We tokenize source files twice to keep memory usage low. This is the | 113 // We tokenize source files twice to keep memory usage low. This is the |
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 337 | 354 |
| 338 void computeHierarchy(Program program) { | 355 void computeHierarchy(Program program) { |
| 339 hierarchy = new ClassHierarchy(program); | 356 hierarchy = new ClassHierarchy(program); |
| 340 ticker.logMs("Computed class hierarchy"); | 357 ticker.logMs("Computed class hierarchy"); |
| 341 coreTypes = new CoreTypes(program); | 358 coreTypes = new CoreTypes(program); |
| 342 ticker.logMs("Computed core types"); | 359 ticker.logMs("Computed core types"); |
| 343 } | 360 } |
| 344 | 361 |
| 345 List<Uri> getDependencies() => sourceBytes.keys.toList(); | 362 List<Uri> getDependencies() => sourceBytes.keys.toList(); |
| 346 } | 363 } |
| OLD | NEW |