Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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 compiler; | 5 library compiler; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'src/apiimpl.dart'; | 8 import 'src/apiimpl.dart'; |
| 9 | 9 |
| 10 // Unless explicitly allowed, passing [:null:] for any argument to the | 10 // Unless explicitly allowed, passing [:null:] for any argument to the |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 58 * [:null:]. If [uri] is not [:null:], neither are [begin] and | 58 * [:null:]. If [uri] is not [:null:], neither are [begin] and |
| 59 * [end]. [uri] indicates the compilation unit from where the | 59 * [end]. [uri] indicates the compilation unit from where the |
| 60 * diagnostic originates. [begin] and [end] are zero-based character | 60 * diagnostic originates. [begin] and [end] are zero-based character |
| 61 * offsets from the beginning of the compilaton unit. [message] is the | 61 * offsets from the beginning of the compilaton unit. [message] is the |
| 62 * diagnostic message, and [kind] indicates indicates what kind of | 62 * diagnostic message, and [kind] indicates indicates what kind of |
| 63 * diagnostic it is. | 63 * diagnostic it is. |
| 64 */ | 64 */ |
| 65 typedef void DiagnosticHandler(Uri uri, int begin, int end, | 65 typedef void DiagnosticHandler(Uri uri, int begin, int end, |
| 66 String message, Diagnostic kind); | 66 String message, Diagnostic kind); |
| 67 | 67 |
| 68 /// Information resulting from the compilation. | |
| 69 class CompilationResult { | |
| 70 /// `true` if the compilation succeeded. | |
|
floitsch
2015/01/05 10:39:33
Specify that "succeeded" means "valid program" and
Johnni Winther
2015/01/05 13:54:10
Done.
| |
| 71 final bool isSuccess; | |
| 72 | |
| 73 /// The compiler object used for the compilation. | |
| 74 /// | |
| 75 /// Note: The type of [compiler] is implementation dependent and may vary. | |
| 76 /// Use only for debugging and testing. | |
| 77 final compiler; | |
| 78 | |
| 79 /// The compilation resulted in this exception. | |
| 80 final exception; | |
| 81 | |
| 82 /// The compilation resulted in an exception with this stack trace. | |
| 83 final trace; | |
| 84 | |
| 85 CompilationResult(this.compiler, {this.isSuccess: true}) | |
| 86 : exception = null, trace = null; | |
| 87 | |
| 88 CompilationResult.error(this.exception, this.trace) | |
| 89 : compiler = null, isSuccess = false; | |
| 90 } | |
| 91 | |
| 68 /** | 92 /** |
| 69 * Returns a future that completes to a non-null String when [script] | 93 * Returns a future that completes to a non-null String when [script] |
| 70 * has been successfully compiled. | 94 * has been successfully compiled. |
| 71 * | 95 * |
| 72 * The compiler output is obtained by providing an [outputProvider]. | 96 * The compiler output is obtained by providing an [outputProvider]. |
| 73 * | 97 * |
| 74 * If the compilation fails, the future's value will be [:null:] and | 98 * If the compilation fails, the future's value will be [:null:] and |
| 75 * [handler] will have been invoked at least once with [:kind == | 99 * [handler] will have been invoked at least once with [:kind == |
| 76 * Diagnostic.ERROR:] or [:kind == Diagnostic.CRASH:]. | 100 * Diagnostic.ERROR:] or [:kind == Diagnostic.CRASH:]. |
| 77 * | 101 * |
| 78 * Deprecated: if no [outputProvider] is given, the future completes | 102 * Deprecated: if no [outputProvider] is given, the future completes |
| 79 * to the compiled script. This behavior will be removed in the future | 103 * to the compiled script. This behavior will be removed in the future |
| 80 * as the compiler may create multiple files to support lazy loading | 104 * as the compiler may create multiple files to support lazy loading |
| 81 * of libraries. | 105 * of libraries. |
| 82 */ | 106 */ |
| 83 Future<String> compile(Uri script, | 107 Future<CompilationResult> compile( |
| 84 Uri libraryRoot, | 108 Uri script, |
| 85 Uri packageRoot, | 109 Uri libraryRoot, |
| 86 CompilerInputProvider inputProvider, | 110 Uri packageRoot, |
| 87 DiagnosticHandler handler, | 111 CompilerInputProvider inputProvider, |
| 88 [List<String> options = const [], | 112 DiagnosticHandler handler, |
| 89 CompilerOutputProvider outputProvider, | 113 [List<String> options = const [], |
| 90 Map<String, dynamic> environment = const {}]) { | 114 CompilerOutputProvider outputProvider, |
| 115 Map<String, dynamic> environment = const {}]) { | |
| 91 if (!libraryRoot.path.endsWith("/")) { | 116 if (!libraryRoot.path.endsWith("/")) { |
| 92 throw new ArgumentError("libraryRoot must end with a /"); | 117 throw new ArgumentError("libraryRoot must end with a /"); |
| 93 } | 118 } |
| 94 if (packageRoot != null && !packageRoot.path.endsWith("/")) { | 119 if (packageRoot != null && !packageRoot.path.endsWith("/")) { |
| 95 throw new ArgumentError("packageRoot must end with a /"); | 120 throw new ArgumentError("packageRoot must end with a /"); |
| 96 } | 121 } |
| 97 // TODO(ahe): Consider completing the future with an exception if | 122 // TODO(ahe): Consider completing the future with an exception if |
| 98 // code is null. | 123 // code is null. |
| 99 Compiler compiler = new Compiler(inputProvider, | 124 Compiler compiler = new Compiler(inputProvider, |
| 100 outputProvider, | 125 outputProvider, |
| 101 handler, | 126 handler, |
| 102 libraryRoot, | 127 libraryRoot, |
| 103 packageRoot, | 128 packageRoot, |
| 104 options, | 129 options, |
| 105 environment); | 130 environment); |
| 106 // TODO(ahe): Use the value of the future (which signals success or failure). | 131 return compiler.run(script).then((bool success) { |
| 107 return compiler.run(script).then((_) { | 132 return new CompilationResult(compiler, isSuccess: success); |
| 108 String code = compiler.assembledCode; | |
| 109 if (code != null && outputProvider != null) { | |
| 110 code = ''; // Non-null signals success. | |
| 111 } | |
| 112 return code; | |
| 113 }); | 133 }); |
| 114 } | 134 } |
| 115 | 135 |
| 116 /** | 136 /** |
| 117 * Kind of diagnostics that the compiler can report. | 137 * Kind of diagnostics that the compiler can report. |
| 118 */ | 138 */ |
| 119 class Diagnostic { | 139 class Diagnostic { |
| 120 /** | 140 /** |
| 121 * An error as identified by the "Dart Programming Language | 141 * An error as identified by the "Dart Programming Language |
| 122 * Specification" [http://www.dartlang.org/docs/spec/]. | 142 * Specification" [http://www.dartlang.org/docs/spec/]. |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 178 final String name; | 198 final String name; |
| 179 | 199 |
| 180 /** | 200 /** |
| 181 * This constructor is not private to support user-defined | 201 * This constructor is not private to support user-defined |
| 182 * diagnostic kinds. | 202 * diagnostic kinds. |
| 183 */ | 203 */ |
| 184 const Diagnostic(this.ordinal, this.name); | 204 const Diagnostic(this.ordinal, this.name); |
| 185 | 205 |
| 186 String toString() => name; | 206 String toString() => name; |
| 187 } | 207 } |
| OLD | NEW |