| 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 'dart:uri'; | 8 import 'dart:uri'; |
| 9 import 'implementation/apiimpl.dart'; | 9 import 'implementation/apiimpl.dart'; |
| 10 | 10 |
| 11 // Unless explicitly allowed, passing [:null:] for any argument to the | 11 // Unless explicitly allowed, passing [:null:] for any argument to the |
| 12 // methods of library will result in an Error being thrown. | 12 // methods of library will result in an Error being thrown. |
| 13 | 13 |
| 14 /** | 14 /** |
| 15 * Returns a future that completes to the source corresponding to | 15 * Returns a future that completes to the source corresponding to |
| 16 * [uri]. If an exception occurs, the future completes with this | 16 * [uri]. If an exception occurs, the future completes with this |
| 17 * exception. | 17 * exception. |
| 18 */ | 18 */ |
| 19 typedef Future<String> CompilerInputProvider(Uri uri); |
| 20 |
| 21 /// Deprecated, please use [CompilerInputProvider] instead. |
| 19 typedef Future<String> ReadStringFromUri(Uri uri); | 22 typedef Future<String> ReadStringFromUri(Uri uri); |
| 20 | 23 |
| 21 /** | 24 /** |
| 25 * Returns a [Sink] that will serve as compiler output for the given |
| 26 * component. |
| 27 * |
| 28 * Components are identified by [name] and [extension]. By convention, |
| 29 * the empty string [:"":] will represent the main script |
| 30 * (corresponding to the script parameter of [compile]) even if the |
| 31 * main script is a library. For libraries that are compiled |
| 32 * separately, the library name is used. |
| 33 * |
| 34 * At least the following extensions can be expected: |
| 35 * |
| 36 * * "js" for JavaScript output. |
| 37 * * "js.map" for source maps. |
| 38 * * "dart" for Dart output. |
| 39 * * "dart.map" for source maps. |
| 40 * |
| 41 * As more features are added to the compiler, new names and |
| 42 * extensions may be introduced. |
| 43 */ |
| 44 typedef Sink CompilerOutputProvider(String name, String extension); |
| 45 |
| 46 /** |
| 22 * Invoked by the compiler to report diagnostics. If [uri] is | 47 * Invoked by the compiler to report diagnostics. If [uri] is |
| 23 * [:null:], so are [begin] and [end]. No other arguments may be | 48 * [:null:], so are [begin] and [end]. No other arguments may be |
| 24 * [:null:]. If [uri] is not [:null:], neither are [begin] and | 49 * [:null:]. If [uri] is not [:null:], neither are [begin] and |
| 25 * [end]. [uri] indicates the compilation unit from where the | 50 * [end]. [uri] indicates the compilation unit from where the |
| 26 * diagnostic originates. [begin] and [end] are zero-based character | 51 * diagnostic originates. [begin] and [end] are zero-based character |
| 27 * offsets from the beginning of the compilaton unit. [message] is the | 52 * offsets from the beginning of the compilaton unit. [message] is the |
| 28 * diagnostic message, and [kind] indicates indicates what kind of | 53 * diagnostic message, and [kind] indicates indicates what kind of |
| 29 * diagnostic it is. | 54 * diagnostic it is. |
| 30 */ | 55 */ |
| 31 typedef void DiagnosticHandler(Uri uri, int begin, int end, | 56 typedef void DiagnosticHandler(Uri uri, int begin, int end, |
| 32 String message, Diagnostic kind); | 57 String message, Diagnostic kind); |
| 33 | 58 |
| 34 /** | 59 /** |
| 35 * Returns a future that completes to [script] compiled to JavaScript. If | 60 * Returns a future that completes to a non-null String when [script] |
| 36 * the compilation fails, the future's value will be [:null:] and | 61 * has been successfully compiled. |
| 62 * |
| 63 * The compiler output is obtained by providing an [outputProvider]. |
| 64 * |
| 65 * If the compilation fails, the future's value will be [:null:] and |
| 37 * [handler] will have been invoked at least once with [:kind == | 66 * [handler] will have been invoked at least once with [:kind == |
| 38 * Diagnostic.ERROR:] or [:kind == Diagnostic.CRASH:]. | 67 * Diagnostic.ERROR:] or [:kind == Diagnostic.CRASH:]. |
| 68 * |
| 69 * Deprecated: if no [outputProvider] is given, the future completes |
| 70 * to the compiled script. This behavior will be removed in the future |
| 71 * as the compiler may create multiple files to support lazy loading |
| 72 * of libraries. |
| 39 */ | 73 */ |
| 40 Future<String> compile(Uri script, | 74 Future<String> compile(Uri script, |
| 41 Uri libraryRoot, | 75 Uri libraryRoot, |
| 42 Uri packageRoot, | 76 Uri packageRoot, |
| 43 ReadStringFromUri provider, | 77 CompilerInputProvider inputProvider, |
| 44 DiagnosticHandler handler, | 78 DiagnosticHandler handler, |
| 45 [List<String> options = const []]) { | 79 [List<String> options = const [], |
| 80 CompilerOutputProvider outputProvider]) { |
| 46 if (!libraryRoot.path.endsWith("/")) { | 81 if (!libraryRoot.path.endsWith("/")) { |
| 47 throw new ArgumentError("libraryRoot must end with a /"); | 82 throw new ArgumentError("libraryRoot must end with a /"); |
| 48 } | 83 } |
| 49 if (packageRoot != null && !packageRoot.path.endsWith("/")) { | 84 if (packageRoot != null && !packageRoot.path.endsWith("/")) { |
| 50 throw new ArgumentError("packageRoot must end with a /"); | 85 throw new ArgumentError("packageRoot must end with a /"); |
| 51 } | 86 } |
| 52 // TODO(ahe): Consider completing the future with an exception if | 87 // TODO(ahe): Consider completing the future with an exception if |
| 53 // code is null. | 88 // code is null. |
| 54 Compiler compiler = new Compiler(provider, handler, libraryRoot, packageRoot, | 89 Compiler compiler = new Compiler(inputProvider, |
| 90 outputProvider, |
| 91 handler, |
| 92 libraryRoot, |
| 93 packageRoot, |
| 55 options); | 94 options); |
| 56 compiler.run(script); | 95 compiler.run(script); |
| 57 String code = compiler.assembledCode; | 96 String code = compiler.assembledCode; |
| 97 if (code != null && outputProvider != null) { |
| 98 String outputType = 'js'; |
| 99 if (options.contains('--output-type=dart')) { |
| 100 outputType = 'dart'; |
| 101 } |
| 102 outputProvider('', outputType) |
| 103 ..add(code) |
| 104 ..close(); |
| 105 code = ''; // Non-null signals success. |
| 106 } |
| 58 return new Future.immediate(code); | 107 return new Future.immediate(code); |
| 59 } | 108 } |
| 60 | 109 |
| 61 /** | 110 /** |
| 62 * Kind of diagnostics that the compiler can report. | 111 * Kind of diagnostics that the compiler can report. |
| 63 */ | 112 */ |
| 64 class Diagnostic { | 113 class Diagnostic { |
| 65 /** | 114 /** |
| 66 * An error as identified by the "Dart Programming Language | 115 * An error as identified by the "Dart Programming Language |
| 67 * Specification" [http://www.dartlang.org/docs/spec/]. | 116 * Specification" [http://www.dartlang.org/docs/spec/]. |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 final String name; | 167 final String name; |
| 119 | 168 |
| 120 /** | 169 /** |
| 121 * This constructor is not private to support user-defined | 170 * This constructor is not private to support user-defined |
| 122 * diagnostic kinds. | 171 * diagnostic kinds. |
| 123 */ | 172 */ |
| 124 const Diagnostic(this.ordinal, this.name); | 173 const Diagnostic(this.ordinal, this.name); |
| 125 | 174 |
| 126 String toString() => name; | 175 String toString() => name; |
| 127 } | 176 } |
| OLD | NEW |