OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, 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 /// New Compiler API. This API is under construction, use only internally or |
| 6 /// in unittests. |
| 7 |
| 8 library compiler_new; |
| 9 |
| 10 import 'dart:async'; |
| 11 import 'src/apiimpl.dart'; |
| 12 import 'compiler.dart' show Diagnostic, PackagesDiscoveryProvider; |
| 13 export 'compiler.dart' show Diagnostic, PackagesDiscoveryProvider; |
| 14 |
| 15 // Unless explicitly allowed, passing `null` for any argument to the |
| 16 // methods of library will result in an Error being thrown. |
| 17 |
| 18 /// Interface for providing the compiler with input. That is, Dart source files, |
| 19 /// package config files, etc. |
| 20 abstract class CompilerInput { |
| 21 /// Returns a future that completes to the source corresponding to [uri]. |
| 22 /// If an exception occurs, the future completes with this exception. |
| 23 /// |
| 24 /// The source can be represented either as a [:List<int>:] of UTF-8 bytes or |
| 25 /// as a [String]. |
| 26 /// |
| 27 /// The following text is non-normative: |
| 28 /// |
| 29 /// It is recommended to return a UTF-8 encoded list of bytes because the |
| 30 /// scanner is more efficient in this case. In either case, the data structure |
| 31 /// is expected to hold a zero element at the last position. If this is not |
| 32 /// the case, the entire data structure is copied before scanning. |
| 33 Future/*<String | List<int>>*/ readFromUri(Uri uri); |
| 34 } |
| 35 |
| 36 /// Interface for producing output from the compiler. That is, JavaScript target |
| 37 /// files, source map files, dump info files, etc. |
| 38 abstract class CompilerOutput { |
| 39 /// Returns an [EventSink] that will serve as compiler output for the given |
| 40 /// component. |
| 41 /// |
| 42 /// Components are identified by [name] and [extension]. By convention, |
| 43 /// the empty string [:"":] will represent the main script |
| 44 /// (corresponding to the script parameter of [compile]) even if the |
| 45 /// main script is a library. For libraries that are compiled |
| 46 /// separately, the library name is used. |
| 47 /// |
| 48 /// At least the following extensions can be expected: |
| 49 /// |
| 50 /// * "js" for JavaScript output. |
| 51 /// * "js.map" for source maps. |
| 52 /// * "dart" for Dart output. |
| 53 /// * "dart.map" for source maps. |
| 54 /// |
| 55 /// As more features are added to the compiler, new names and |
| 56 /// extensions may be introduced. |
| 57 EventSink<String> createEventSink(String name, String extension); |
| 58 } |
| 59 |
| 60 /// Interface for receiving diagnostic message from the compiler. That is, |
| 61 /// errors, warnings, hints, etc. |
| 62 abstract class CompilerDiagnostics { |
| 63 /// Invoked by the compiler to report diagnostics. If [uri] is `null`, so are |
| 64 /// [begin] and [end]. No other arguments may be `null`. If [uri] is not |
| 65 /// `null`, neither are [begin] and [end]. [uri] indicates the compilation |
| 66 /// unit from where the diagnostic originates. [begin] and [end] are |
| 67 /// zero-based character offsets from the beginning of the compilation unit. |
| 68 /// [message] is the diagnostic message, and [kind] indicates indicates what |
| 69 /// kind of diagnostic it is. |
| 70 void report(Uri uri, int begin, int end, String message, Diagnostic kind); |
| 71 } |
| 72 |
| 73 /// Information resulting from the compilation. |
| 74 class CompilationResult { |
| 75 /// `true` if the compilation succeeded, that is, compilation didn't fail due |
| 76 /// to compile-time errors and/or internal errors. |
| 77 final bool isSuccess; |
| 78 |
| 79 /// The compiler object used for the compilation. |
| 80 /// |
| 81 /// Note: The type of [compiler] is implementation dependent and may vary. |
| 82 /// Use only for debugging and testing. |
| 83 final compiler; |
| 84 |
| 85 CompilationResult(this.compiler, {this.isSuccess: true}); |
| 86 } |
| 87 |
| 88 /// Object for passing options to the compiler. |
| 89 class CompilerOptions { |
| 90 final Uri entryPoint; |
| 91 final Uri libraryRoot; |
| 92 final Uri packageRoot; |
| 93 final Uri packageConfig; |
| 94 final PackagesDiscoveryProvider packagesDiscoveryProvider; |
| 95 final List<String> options; |
| 96 final Map<String, dynamic> environment; |
| 97 |
| 98 /// Creates an option object for the compiler. |
| 99 // TODO(johnniwinther): Expand comment when [options] are explicit as named |
| 100 // arguments. |
| 101 factory CompilerOptions( |
| 102 {Uri entryPoint, |
| 103 Uri libraryRoot, |
| 104 Uri packageRoot, |
| 105 Uri packageConfig, |
| 106 PackagesDiscoveryProvider packagesDiscoveryProvider, |
| 107 List<String> options: const <String>[], |
| 108 Map<String, dynamic> environment: const <String, dynamic>{}}) { |
| 109 if (entryPoint == null) { |
| 110 throw new ArgumentError("entryPoint must be non-null"); |
| 111 } |
| 112 if (!libraryRoot.path.endsWith("/")) { |
| 113 throw new ArgumentError("libraryRoot must end with a /"); |
| 114 } |
| 115 if (packageRoot != null && !packageRoot.path.endsWith("/")) { |
| 116 throw new ArgumentError("packageRoot must end with a /"); |
| 117 } |
| 118 return new CompilerOptions._( |
| 119 entryPoint, |
| 120 libraryRoot, |
| 121 packageRoot, |
| 122 packageConfig, |
| 123 packagesDiscoveryProvider, |
| 124 options, |
| 125 environment); |
| 126 } |
| 127 |
| 128 CompilerOptions._( |
| 129 this.entryPoint, |
| 130 this.libraryRoot, |
| 131 this.packageRoot, |
| 132 this.packageConfig, |
| 133 this.packagesDiscoveryProvider, |
| 134 this.options, |
| 135 this.environment); |
| 136 } |
| 137 |
| 138 /// Returns a future that completes to a [CompilationResult] when the Dart |
| 139 /// sources in [options] have been compiled. |
| 140 /// |
| 141 /// The generated compiler output is obtained by providing a [compilerOutput]. |
| 142 /// |
| 143 /// If the compilation fails, the future's `CompilationResult.isSuccess` is |
| 144 /// `false` and [CompilerDiagnostics.report] on [compilerDiagnostics] |
| 145 /// is invoked at least once with `kind == Diagnostic.ERROR` or |
| 146 /// `kind == Diagnostic.CRASH`. |
| 147 Future<CompilationResult> compile( |
| 148 CompilerOptions compilerOptions, |
| 149 CompilerInput compilerInput, |
| 150 CompilerDiagnostics compilerDiagnostics, |
| 151 CompilerOutput compilerOutput) { |
| 152 |
| 153 if (compilerOptions == null) { |
| 154 throw new ArgumentError("compilerOptions must be non-null"); |
| 155 } |
| 156 if (compilerInput == null) { |
| 157 throw new ArgumentError("compilerInput must be non-null"); |
| 158 } |
| 159 if (compilerDiagnostics == null) { |
| 160 throw new ArgumentError("compilerDiagnostics must be non-null"); |
| 161 } |
| 162 if (compilerOutput == null) { |
| 163 throw new ArgumentError("compilerOutput must be non-null"); |
| 164 } |
| 165 |
| 166 Compiler compiler = new Compiler( |
| 167 compilerInput, |
| 168 compilerOutput, |
| 169 compilerDiagnostics, |
| 170 compilerOptions.libraryRoot, |
| 171 compilerOptions.packageRoot, |
| 172 compilerOptions.options, |
| 173 compilerOptions.environment, |
| 174 compilerOptions.packageConfig, |
| 175 compilerOptions.packagesDiscoveryProvider); |
| 176 return compiler.run(compilerOptions.entryPoint).then((bool success) { |
| 177 return new CompilationResult(compiler, isSuccess: success); |
| 178 }); |
| 179 } |
OLD | NEW |