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 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 readFromUri(resourceUri).then(UTF8.decode); |
| 37 } |
| 38 |
| 39 Future<List<int>> readFromUri(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>> call(Uri resourceUri) => readFromUri(resourceUri); |
52 } | 58 } |
53 | 59 |
54 class FormattingDiagnosticHandler { | 60 class FormattingDiagnosticHandler { |
55 final SourceFileProvider provider; | 61 final SourceFileProvider provider; |
56 bool showWarnings = true; | 62 bool showWarnings = true; |
57 bool showHints = true; | 63 bool showHints = true; |
58 bool verbose = false; | 64 bool verbose = false; |
59 bool isAborting = false; | 65 bool isAborting = false; |
60 bool enableColors = false; | 66 bool enableColors = false; |
61 bool throwOnError = false; | 67 bool throwOnError = false; |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 if (fatal && throwOnError) { | 138 if (fatal && throwOnError) { |
133 isAborting = true; | 139 isAborting = true; |
134 throw new AbortLeg(message); | 140 throw new AbortLeg(message); |
135 } | 141 } |
136 } | 142 } |
137 | 143 |
138 void call(Uri uri, int begin, int end, String message, api.Diagnostic kind) { | 144 void call(Uri uri, int begin, int end, String message, api.Diagnostic kind) { |
139 return diagnosticHandler(uri, begin, end, message, kind); | 145 return diagnosticHandler(uri, begin, end, message, kind); |
140 } | 146 } |
141 } | 147 } |
OLD | NEW |