OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 library source_file_provider; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'dart:uri'; |
| 9 import 'dart:io'; |
| 10 import 'dart:utf'; |
| 11 |
| 12 import '../compiler.dart' as api show Diagnostic; |
| 13 import 'dart2js.dart' show AbortLeg; |
| 14 import 'colors.dart' as colors; |
| 15 import 'source_file.dart'; |
| 16 import 'filenames.dart'; |
| 17 import 'util/uri_extras.dart'; |
| 18 |
| 19 String readAll(String filename) { |
| 20 var file = (new File(filename)).openSync(FileMode.READ); |
| 21 var length = file.lengthSync(); |
| 22 var buffer = new List<int>.fixedLength(length); |
| 23 var bytes = file.readListSync(buffer, 0, length); |
| 24 file.closeSync(); |
| 25 return new String.fromCharCodes(new Utf8Decoder(buffer).decodeRest()); |
| 26 } |
| 27 |
| 28 class SourceFileProvider { |
| 29 bool isWindows = (Platform.operatingSystem == 'windows'); |
| 30 Uri cwd = getCurrentDirectory(); |
| 31 Map<String, SourceFile> sourceFiles = <String, SourceFile>{}; |
| 32 int dartBytesRead = 0; |
| 33 |
| 34 Future<String> readStringFromUri(Uri resourceUri) { |
| 35 if (resourceUri.scheme != 'file') { |
| 36 throw new ArgumentError(resourceUri); |
| 37 } |
| 38 String source; |
| 39 try { |
| 40 source = readAll(uriPathToNative(resourceUri.path)); |
| 41 } on FileIOException catch (ex) { |
| 42 throw 'Error: Cannot read "${relativize(cwd, resourceUri, isWindows)}" ' |
| 43 '(${ex.osError}).'; |
| 44 } |
| 45 dartBytesRead += source.length; |
| 46 sourceFiles[resourceUri.toString()] = |
| 47 new SourceFile(relativize(cwd, resourceUri, isWindows), source); |
| 48 return new Future.immediate(source); |
| 49 } |
| 50 } |
| 51 |
| 52 class FormattingDiagnosticHandler { |
| 53 final SourceFileProvider provider; |
| 54 bool showWarnings = true; |
| 55 bool verbose = false; |
| 56 bool isAborting = false; |
| 57 bool enableColors = false; |
| 58 bool throwOnError = false; |
| 59 |
| 60 final int FATAL = api.Diagnostic.CRASH.ordinal | api.Diagnostic.ERROR.ordinal; |
| 61 final int INFO = |
| 62 api.Diagnostic.INFO.ordinal | api.Diagnostic.VERBOSE_INFO.ordinal; |
| 63 |
| 64 FormattingDiagnosticHandler(SourceFileProvider this.provider); |
| 65 |
| 66 void info(var message, [api.Diagnostic kind = api.Diagnostic.VERBOSE_INFO]) { |
| 67 if (!verbose && identical(kind, api.Diagnostic.VERBOSE_INFO)) return; |
| 68 if (enableColors) { |
| 69 print('${colors.green("info:")} $message'); |
| 70 } else { |
| 71 print('info: $message'); |
| 72 } |
| 73 } |
| 74 |
| 75 void diagnosticHandler(Uri uri, int begin, int end, String message, |
| 76 api.Diagnostic kind) { |
| 77 // TODO(ahe): Remove this when source map is handled differently. |
| 78 if (identical(kind.name, 'source map')) return; |
| 79 |
| 80 if (isAborting) return; |
| 81 isAborting = identical(kind, api.Diagnostic.CRASH); |
| 82 bool fatal = (kind.ordinal & FATAL) != 0; |
| 83 bool isInfo = (kind.ordinal & INFO) != 0; |
| 84 if (isInfo && uri == null && !identical(kind, api.Diagnostic.INFO)) { |
| 85 info(message, kind); |
| 86 return; |
| 87 } |
| 88 var color; |
| 89 if (!enableColors) { |
| 90 color = (x) => x; |
| 91 } else if (identical(kind, api.Diagnostic.ERROR)) { |
| 92 color = colors.red; |
| 93 } else if (identical(kind, api.Diagnostic.WARNING)) { |
| 94 color = colors.magenta; |
| 95 } else if (identical(kind, api.Diagnostic.LINT)) { |
| 96 color = colors.magenta; |
| 97 } else if (identical(kind, api.Diagnostic.CRASH)) { |
| 98 color = colors.red; |
| 99 } else if (identical(kind, api.Diagnostic.INFO)) { |
| 100 color = colors.green; |
| 101 } else { |
| 102 throw 'Unknown kind: $kind (${kind.ordinal})'; |
| 103 } |
| 104 if (uri == null) { |
| 105 assert(fatal); |
| 106 print(color(message)); |
| 107 } else if (fatal || showWarnings) { |
| 108 SourceFile file = provider.sourceFiles[uri.toString()]; |
| 109 if (file == null) { |
| 110 throw '$uri: file is null'; |
| 111 } |
| 112 print(file.getLocationMessage(color(message), begin, end, true, color)); |
| 113 } |
| 114 if (fatal && throwOnError) { |
| 115 isAborting = true; |
| 116 throw new AbortLeg(message); |
| 117 } |
| 118 } |
| 119 } |
| 120 |
| 121 |
OLD | NEW |