Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(119)

Side by Side Diff: pkg/compiler/lib/src/source_file_provider.dart

Issue 1235563003: Add interfaces for a new compiler API. (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Rebased Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 import 'dart:math' as math; 10 import 'dart:math' as math;
11 11
12 import '../compiler.dart' as api show Diagnostic, DiagnosticHandler; 12 import '../compiler.dart' as api show Diagnostic, DiagnosticHandler;
13 import '../compiler_new.dart' as api show CompilerInput, CompilerDiagnostics;
13 import 'dart2js.dart' show AbortLeg; 14 import 'dart2js.dart' show AbortLeg;
14 import 'colors.dart' as colors; 15 import 'colors.dart' as colors;
15 import 'io/source_file.dart'; 16 import 'io/source_file.dart';
16 import 'filenames.dart'; 17 import 'filenames.dart';
17 import 'util/uri_extras.dart'; 18 import 'util/uri_extras.dart';
18 import 'dart:typed_data'; 19 import 'dart:typed_data';
20 import '../compiler_new.dart';
19 21
20 List<int> readAll(String filename) { 22 List<int> readAll(String filename) {
21 var file = (new File(filename)).openSync(); 23 var file = (new File(filename)).openSync();
22 var length = file.lengthSync(); 24 var length = file.lengthSync();
23 // +1 to have a 0 terminated list, see [Scanner]. 25 // +1 to have a 0 terminated list, see [Scanner].
24 var buffer = new Uint8List(length + 1); 26 var buffer = new Uint8List(length + 1);
25 file.readIntoSync(buffer, 0, length); 27 file.readIntoSync(buffer, 0, length);
26 file.closeSync(); 28 file.closeSync();
27 return buffer; 29 return buffer;
28 } 30 }
29 31
30 abstract class SourceFileProvider { 32 abstract class SourceFileProvider implements CompilerInput {
31 bool isWindows = (Platform.operatingSystem == 'windows'); 33 bool isWindows = (Platform.operatingSystem == 'windows');
32 Uri cwd = currentDirectory; 34 Uri cwd = currentDirectory;
33 Map<Uri, SourceFile> sourceFiles = <Uri, SourceFile>{}; 35 Map<Uri, SourceFile> sourceFiles = <Uri, SourceFile>{};
34 int dartCharactersRead = 0; 36 int dartCharactersRead = 0;
35 37
36 Future<String> readStringFromUri(Uri resourceUri) { 38 Future<String> readStringFromUri(Uri resourceUri) {
37 return readUtf8BytesFromUri(resourceUri).then(UTF8.decode); 39 return readUtf8BytesFromUri(resourceUri).then(UTF8.decode);
38 } 40 }
39 41
40 Future<List<int>> readUtf8BytesFromUri(Uri resourceUri) { 42 Future<List<int>> readUtf8BytesFromUri(Uri resourceUri) {
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 offset += contentPart.length; 95 offset += contentPart.length;
94 } 96 }
95 dartCharactersRead += totalLength; 97 dartCharactersRead += totalLength;
96 sourceFiles[resourceUri] = 98 sourceFiles[resourceUri] =
97 new CachingUtf8BytesSourceFile( 99 new CachingUtf8BytesSourceFile(
98 resourceUri, resourceUri.toString(), result); 100 resourceUri, resourceUri.toString(), result);
99 return result; 101 return result;
100 }); 102 });
101 } 103 }
102 104
105 // TODO(johnniwinther): Remove this when no longer needed for the old compiler
106 // API.
103 Future/*<List<int> | String>*/ call(Uri resourceUri); 107 Future/*<List<int> | String>*/ call(Uri resourceUri);
104 108
105 relativizeUri(Uri uri) => relativize(cwd, uri, isWindows); 109 relativizeUri(Uri uri) => relativize(cwd, uri, isWindows);
106 110
107 SourceFile getSourceFile(Uri resourceUri) { 111 SourceFile getSourceFile(Uri resourceUri) {
108 return sourceFiles[resourceUri]; 112 return sourceFiles[resourceUri];
109 } 113 }
110 } 114 }
111 115
112 class CompilerSourceFileProvider extends SourceFileProvider { 116 class CompilerSourceFileProvider extends SourceFileProvider {
113 Future<List<int>> call(Uri resourceUri) => readUtf8BytesFromUri(resourceUri); 117 // TODO(johnniwinther): Remove this when no longer needed for the old compiler
118 // API.
119 Future<List<int>> call(Uri resourceUri) => readFromUri(resourceUri);
120
121 @override
122 Future readFromUri(Uri uri) => readUtf8BytesFromUri(uri);
114 } 123 }
115 124
116 class FormattingDiagnosticHandler { 125 class FormattingDiagnosticHandler implements CompilerDiagnostics {
117 final SourceFileProvider provider; 126 final SourceFileProvider provider;
118 bool showWarnings = true; 127 bool showWarnings = true;
119 bool showHints = true; 128 bool showHints = true;
120 bool verbose = false; 129 bool verbose = false;
121 bool isAborting = false; 130 bool isAborting = false;
122 bool enableColors = false; 131 bool enableColors = false;
123 bool throwOnError = false; 132 bool throwOnError = false;
124 int throwOnErrorCount = 0; 133 int throwOnErrorCount = 0;
125 api.Diagnostic lastKind = null; 134 api.Diagnostic lastKind = null;
126 int fatalCount = 0; 135 int fatalCount = 0;
(...skipping 26 matching lines...) Expand all
153 return 'Hint: $message'; 162 return 'Hint: $message';
154 case api.Diagnostic.CRASH: 163 case api.Diagnostic.CRASH:
155 return 'Internal Error: $message'; 164 return 'Internal Error: $message';
156 case api.Diagnostic.INFO: 165 case api.Diagnostic.INFO:
157 case api.Diagnostic.VERBOSE_INFO: 166 case api.Diagnostic.VERBOSE_INFO:
158 return 'Info: $message'; 167 return 'Info: $message';
159 } 168 }
160 throw 'Unexpected diagnostic kind: $kind (${kind.ordinal})'; 169 throw 'Unexpected diagnostic kind: $kind (${kind.ordinal})';
161 } 170 }
162 171
163 void diagnosticHandler(Uri uri, int begin, int end, String message, 172 @override
164 api.Diagnostic kind) { 173 void report(Uri uri, int begin, int end, String message,
174 api.Diagnostic kind) {
165 // TODO(ahe): Remove this when source map is handled differently. 175 // TODO(ahe): Remove this when source map is handled differently.
166 if (identical(kind.name, 'source map')) return; 176 if (identical(kind.name, 'source map')) return;
167 177
168 if (isAborting) return; 178 if (isAborting) return;
169 isAborting = (kind == api.Diagnostic.CRASH); 179 isAborting = (kind == api.Diagnostic.CRASH);
170 180
171 bool fatal = (kind.ordinal & FATAL) != 0; 181 bool fatal = (kind.ordinal & FATAL) != 0;
172 bool isInfo = (kind.ordinal & INFO) != 0; 182 bool isInfo = (kind.ordinal & INFO) != 0;
173 if (isInfo && uri == null && kind != api.Diagnostic.INFO) { 183 if (isInfo && uri == null && kind != api.Diagnostic.INFO) {
174 info(message, kind); 184 info(message, kind);
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 print('${provider.relativizeUri(uri)}$position:\n' 226 print('${provider.relativizeUri(uri)}$position:\n'
217 '${color(message)}'); 227 '${color(message)}');
218 } 228 }
219 } 229 }
220 if (fatal && ++fatalCount >= throwOnErrorCount && throwOnError) { 230 if (fatal && ++fatalCount >= throwOnErrorCount && throwOnError) {
221 isAborting = true; 231 isAborting = true;
222 throw new AbortLeg(message); 232 throw new AbortLeg(message);
223 } 233 }
224 } 234 }
225 235
236 // TODO(johnniwinther): Remove this when no longer needed for the old compiler
237 // API.
226 void call(Uri uri, int begin, int end, String message, api.Diagnostic kind) { 238 void call(Uri uri, int begin, int end, String message, api.Diagnostic kind) {
227 return diagnosticHandler(uri, begin, end, message, kind); 239 return report(uri, begin, end, message, kind);
228 } 240 }
229 } 241 }
230 242
231 typedef void MessageCallback(String message); 243 typedef void MessageCallback(String message);
232 244
233 class RandomAccessFileOutputProvider { 245 class RandomAccessFileOutputProvider {
234 final Uri out; 246 final Uri out;
235 final Uri sourceMapOut; 247 final Uri sourceMapOut;
236 final MessageCallback onInfo; 248 final MessageCallback onInfo;
237 final MessageCallback onFailure; 249 final MessageCallback onFailure;
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
325 var onAdd, onClose; 337 var onAdd, onClose;
326 338
327 EventSinkWrapper(this.onAdd, this.onClose); 339 EventSinkWrapper(this.onAdd, this.onClose);
328 340
329 void add(String data) => onAdd(data); 341 void add(String data) => onAdd(data);
330 342
331 void addError(error, [StackTrace stackTrace]) => throw error; 343 void addError(error, [StackTrace stackTrace]) => throw error;
332 344
333 void close() => onClose(); 345 void close() => onClose();
334 } 346 }
OLDNEW
« no previous file with comments | « pkg/compiler/lib/src/old_to_new_api.dart ('k') | pkg/dart2js_incremental/lib/caching_compiler.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698