OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 source_file_provider; | 5 library source_file_provider; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:convert'; | 8 import 'dart:convert'; |
9 import 'dart:io'; | 9 import 'dart:io'; |
10 | 10 |
11 import '../compiler.dart' as api show Diagnostic; | 11 import '../compiler.dart' as api show Diagnostic; |
12 import 'dart2js.dart' show AbortLeg; | 12 import 'dart2js.dart' show AbortLeg; |
13 import 'colors.dart' as colors; | 13 import 'colors.dart' as colors; |
14 import 'source_file.dart'; | 14 import 'source_file.dart'; |
15 import 'filenames.dart'; | 15 import 'filenames.dart'; |
16 import 'util/uri_extras.dart'; | 16 import 'util/uri_extras.dart'; |
| 17 import 'dart:typed_data'; |
17 | 18 |
18 String readAll(String filename) { | 19 List<int> readAll(String filename) { |
19 var file = (new File(filename)).openSync(); | 20 var file = (new File(filename)).openSync(); |
20 var length = file.lengthSync(); | 21 var length = file.lengthSync(); |
21 var buffer = new List<int>(length); | 22 // +1 to have a 0 terminated list, see [Scanner]. |
| 23 var buffer = new Uint8List(length + 1); |
22 var bytes = file.readIntoSync(buffer, 0, length); | 24 var bytes = file.readIntoSync(buffer, 0, length); |
23 file.closeSync(); | 25 file.closeSync(); |
24 return UTF8.decode(buffer); | 26 return buffer; |
25 } | 27 } |
26 | 28 |
27 class SourceFileProvider { | 29 abstract class SourceFileProvider { |
28 bool isWindows = (Platform.operatingSystem == 'windows'); | 30 bool isWindows = (Platform.operatingSystem == 'windows'); |
29 Uri cwd = currentDirectory; | 31 Uri cwd = currentDirectory; |
30 Map<String, SourceFile> sourceFiles = <String, SourceFile>{}; | 32 Map<String, SourceFile> sourceFiles = <String, SourceFile>{}; |
31 int dartCharactersRead = 0; | 33 int dartCharactersRead = 0; |
32 | 34 |
33 Future<String> readStringFromUri(Uri resourceUri) { | 35 Future<String> readStringFromUri(Uri resourceUri) { |
| 36 return readUtf8BytesFromUri(resourceUri).then(UTF8.decode); |
| 37 } |
| 38 |
| 39 Future<List<int>> readUtf8BytesFromUri(Uri resourceUri) { |
34 if (resourceUri.scheme != 'file') { | 40 if (resourceUri.scheme != 'file') { |
35 throw new ArgumentError("Unknown scheme in uri '$resourceUri'"); | 41 throw new ArgumentError("Unknown scheme in uri '$resourceUri'"); |
36 } | 42 } |
37 String source; | 43 List<int> source; |
38 try { | 44 try { |
39 source = readAll(uriPathToNative(resourceUri.path)); | 45 source = readAll(uriPathToNative(resourceUri.path)); |
40 } on FileException catch (ex) { | 46 } on FileException catch (ex) { |
41 return new Future.error( | 47 return new Future.error( |
42 "Error reading '${relativize(cwd, resourceUri, isWindows)}' " | 48 "Error reading '${relativize(cwd, resourceUri, isWindows)}' " |
43 "(${ex.osError})"); | 49 "(${ex.osError})"); |
44 } | 50 } |
45 dartCharactersRead += source.length; | 51 dartCharactersRead += source.length; |
46 sourceFiles[resourceUri.toString()] = new SourceFile( | 52 sourceFiles[resourceUri.toString()] = new Utf8BytesSourceFile( |
47 relativize(cwd, resourceUri, isWindows), source); | 53 relativize(cwd, resourceUri, isWindows), source); |
48 return new Future.value(source); | 54 return new Future.value(source); |
49 } | 55 } |
50 | 56 |
51 Future<String> call(Uri resourceUri) => readStringFromUri(resourceUri); | 57 Future/*<List<int> | String>*/ call(Uri resourceUri); |
| 58 } |
| 59 |
| 60 class CompilerSourceFileProvider extends SourceFileProvider { |
| 61 Future<List<int>> call(Uri resourceUri) => readUtf8BytesFromUri(resourceUri); |
52 } | 62 } |
53 | 63 |
54 class FormattingDiagnosticHandler { | 64 class FormattingDiagnosticHandler { |
55 final SourceFileProvider provider; | 65 final SourceFileProvider provider; |
56 bool showWarnings = true; | 66 bool showWarnings = true; |
57 bool showHints = true; | 67 bool showHints = true; |
58 bool verbose = false; | 68 bool verbose = false; |
59 bool isAborting = false; | 69 bool isAborting = false; |
60 bool enableColors = false; | 70 bool enableColors = false; |
61 bool throwOnError = false; | 71 bool throwOnError = false; |
62 api.Diagnostic lastKind = null; | 72 api.Diagnostic lastKind = null; |
63 | 73 |
64 final int FATAL = api.Diagnostic.CRASH.ordinal | api.Diagnostic.ERROR.ordinal; | 74 final int FATAL = api.Diagnostic.CRASH.ordinal | api.Diagnostic.ERROR.ordinal; |
65 final int INFO = | 75 final int INFO = |
66 api.Diagnostic.INFO.ordinal | api.Diagnostic.VERBOSE_INFO.ordinal; | 76 api.Diagnostic.INFO.ordinal | api.Diagnostic.VERBOSE_INFO.ordinal; |
67 | 77 |
68 FormattingDiagnosticHandler([SourceFileProvider provider]) | 78 FormattingDiagnosticHandler([SourceFileProvider provider]) |
69 : this.provider = | 79 : this.provider = |
70 (provider == null) ? new SourceFileProvider() : provider; | 80 (provider == null) ? new CompilerSourceFileProvider() : provider; |
71 | 81 |
72 void info(var message, [api.Diagnostic kind = api.Diagnostic.VERBOSE_INFO]) { | 82 void info(var message, [api.Diagnostic kind = api.Diagnostic.VERBOSE_INFO]) { |
73 if (!verbose && kind == api.Diagnostic.VERBOSE_INFO) return; | 83 if (!verbose && kind == api.Diagnostic.VERBOSE_INFO) return; |
74 if (enableColors) { | 84 if (enableColors) { |
75 print('${colors.green("info:")} $message'); | 85 print('${colors.green("info:")} $message'); |
76 } else { | 86 } else { |
77 print('info: $message'); | 87 print('info: $message'); |
78 } | 88 } |
79 } | 89 } |
80 | 90 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 if (fatal && throwOnError) { | 142 if (fatal && throwOnError) { |
133 isAborting = true; | 143 isAborting = true; |
134 throw new AbortLeg(message); | 144 throw new AbortLeg(message); |
135 } | 145 } |
136 } | 146 } |
137 | 147 |
138 void call(Uri uri, int begin, int end, String message, api.Diagnostic kind) { | 148 void call(Uri uri, int begin, int end, String message, api.Diagnostic kind) { |
139 return diagnosticHandler(uri, begin, end, message, kind); | 149 return diagnosticHandler(uri, begin, end, message, kind); |
140 } | 150 } |
141 } | 151 } |
OLD | NEW |