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 compiler; | |
6 | |
7 import 'dart:async'; | |
8 import 'src/apiimpl.dart'; | |
9 | |
10 // Unless explicitly allowed, passing [:null:] for any argument to the | |
11 // methods of library will result in an Error being thrown. | |
12 | |
13 /** | |
14 * Returns a future that completes to the source corresponding to [uri]. | |
15 * If an exception occurs, the future completes with this exception. | |
16 * | |
17 * The source can be represented either as a [:List<int>:] of UTF-8 bytes or as | |
18 * a [String]. | |
19 * | |
20 * The following text is non-normative: | |
21 * | |
22 * It is recommended to return a UTF-8 encoded list of bytes because the scanner | |
23 * is more efficient in this case. In either case, the data structure is | |
24 * expected to hold a zero element at the last position. If this is not the | |
25 * case, the entire data structure is copied before scanning. | |
26 */ | |
27 typedef Future/*<String | List<int>>*/ CompilerInputProvider(Uri uri); | |
28 | |
29 /// Deprecated, please use [CompilerInputProvider] instead. | |
30 typedef Future<String> ReadStringFromUri(Uri uri); | |
31 | |
32 /** | |
33 * Returns an [EventSink] that will serve as compiler output for the given | |
34 * component. | |
35 * | |
36 * Components are identified by [name] and [extension]. By convention, | |
37 * the empty string [:"":] will represent the main script | |
38 * (corresponding to the script parameter of [compile]) even if the | |
39 * main script is a library. For libraries that are compiled | |
40 * separately, the library name is used. | |
41 * | |
42 * At least the following extensions can be expected: | |
43 * | |
44 * * "js" for JavaScript output. | |
45 * * "js.map" for source maps. | |
46 * * "dart" for Dart output. | |
47 * * "dart.map" for source maps. | |
48 * | |
49 * As more features are added to the compiler, new names and | |
50 * extensions may be introduced. | |
51 */ | |
52 typedef EventSink<String> CompilerOutputProvider(String name, | |
53 String extension); | |
54 | |
55 /** | |
56 * Invoked by the compiler to report diagnostics. If [uri] is | |
57 * [:null:], so are [begin] and [end]. No other arguments may be | |
58 * [:null:]. If [uri] is not [:null:], neither are [begin] and | |
59 * [end]. [uri] indicates the compilation unit from where the | |
60 * diagnostic originates. [begin] and [end] are zero-based character | |
61 * offsets from the beginning of the compilaton unit. [message] is the | |
62 * diagnostic message, and [kind] indicates indicates what kind of | |
63 * diagnostic it is. | |
64 */ | |
65 typedef void DiagnosticHandler(Uri uri, int begin, int end, | |
66 String message, Diagnostic kind); | |
67 | |
68 /** | |
69 * Returns a future that completes to a non-null String when [script] | |
70 * has been successfully compiled. | |
71 * | |
72 * The compiler output is obtained by providing an [outputProvider]. | |
73 * | |
74 * If the compilation fails, the future's value will be [:null:] and | |
75 * [handler] will have been invoked at least once with [:kind == | |
76 * Diagnostic.ERROR:] or [:kind == Diagnostic.CRASH:]. | |
77 * | |
78 * Deprecated: if no [outputProvider] is given, the future completes | |
79 * to the compiled script. This behavior will be removed in the future | |
80 * as the compiler may create multiple files to support lazy loading | |
81 * of libraries. | |
82 */ | |
83 Future<String> compile(Uri script, | |
84 Uri libraryRoot, | |
85 Uri packageRoot, | |
86 CompilerInputProvider inputProvider, | |
87 DiagnosticHandler handler, | |
88 [List<String> options = const [], | |
89 CompilerOutputProvider outputProvider, | |
90 Map<String, dynamic> environment = const {}]) { | |
91 if (!libraryRoot.path.endsWith("/")) { | |
92 throw new ArgumentError("libraryRoot must end with a /"); | |
93 } | |
94 if (packageRoot != null && !packageRoot.path.endsWith("/")) { | |
95 throw new ArgumentError("packageRoot must end with a /"); | |
96 } | |
97 // TODO(ahe): Consider completing the future with an exception if | |
98 // code is null. | |
99 Compiler compiler = new Compiler(inputProvider, | |
100 outputProvider, | |
101 handler, | |
102 libraryRoot, | |
103 packageRoot, | |
104 options, | |
105 environment); | |
106 // TODO(ahe): Use the value of the future (which signals success or failure). | |
107 return compiler.run(script).then((_) { | |
108 String code = compiler.assembledCode; | |
109 if (code != null && outputProvider != null) { | |
110 code = ''; // Non-null signals success. | |
111 } | |
112 return code; | |
113 }); | |
114 } | |
115 | |
116 /** | |
117 * Kind of diagnostics that the compiler can report. | |
118 */ | |
119 class Diagnostic { | |
120 /** | |
121 * An error as identified by the "Dart Programming Language | |
122 * Specification" [http://www.dartlang.org/docs/spec/]. | |
123 * | |
124 * Note: the compiler may still produce an executable result after | |
125 * reporting a compilation error. The specification says: | |
126 * | |
127 * "A compile-time error must be reported by a Dart compiler before | |
128 * the erroneous code is executed." and "If a compile-time error | |
129 * occurs within the code of a running isolate A, A is immediately | |
130 * suspended." | |
131 * | |
132 * This means that the compiler can generate code that when executed | |
133 * terminates execution. | |
134 */ | |
135 static const Diagnostic ERROR = const Diagnostic(1, 'error'); | |
136 | |
137 /** | |
138 * A warning as identified by the "Dart Programming Language | |
139 * Specification" [http://www.dartlang.org/docs/spec/]. | |
140 */ | |
141 static const Diagnostic WARNING = const Diagnostic(2, 'warning'); | |
142 | |
143 /** | |
144 * Any other warning that is not covered by [WARNING]. | |
145 */ | |
146 static const Diagnostic HINT = const Diagnostic(4, 'hint'); | |
147 | |
148 /** | |
149 * Additional information about the preceding non-info diagnostic from the | |
150 * compiler. | |
151 * | |
152 * For example, consider a duplicated definition. The compiler first emits a | |
153 * message about the duplicated definition, then emits an info message about | |
154 * the location of the existing definition. | |
155 */ | |
156 static const Diagnostic INFO = const Diagnostic(8, 'info'); | |
157 | |
158 /** | |
159 * Informational messages that shouldn't be printed unless | |
160 * explicitly requested by the user of a compiler. | |
161 */ | |
162 static const Diagnostic VERBOSE_INFO = const Diagnostic(16, 'verbose info'); | |
163 | |
164 /** | |
165 * An internal error in the compiler. | |
166 */ | |
167 static const Diagnostic CRASH = const Diagnostic(32, 'crash'); | |
168 | |
169 /** | |
170 * An [int] representation of this kind. The ordinals are designed | |
171 * to be used as bitsets. | |
172 */ | |
173 final int ordinal; | |
174 | |
175 /** | |
176 * The name of this kind. | |
177 */ | |
178 final String name; | |
179 | |
180 /** | |
181 * This constructor is not private to support user-defined | |
182 * diagnostic kinds. | |
183 */ | |
184 const Diagnostic(this.ordinal, this.name); | |
185 | |
186 String toString() => name; | |
187 } | |
OLD | NEW |