| 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('dart2js'); | 5 #library('dart2js'); |
| 6 | 6 |
| 7 #import('dart:io'); | 7 #import('dart:io'); |
| 8 #import('dart:uri'); | 8 #import('dart:uri'); |
| 9 #import('dart:utf'); | 9 #import('dart:utf'); |
| 10 | 10 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 HandleOption handle; | 23 HandleOption handle; |
| 24 | 24 |
| 25 OptionHandler(this.pattern, this.handle); | 25 OptionHandler(this.pattern, this.handle); |
| 26 } | 26 } |
| 27 | 27 |
| 28 String extractParameter(String argument) { | 28 String extractParameter(String argument) { |
| 29 return argument.substring(argument.indexOf('=') + 1); | 29 return argument.substring(argument.indexOf('=') + 1); |
| 30 } | 30 } |
| 31 | 31 |
| 32 void parseCommandLine(List<OptionHandler> handlers, List<String> argv) { | 32 void parseCommandLine(List<OptionHandler> handlers, List<String> argv) { |
| 33 // TODO(ahe): Use ../../args/args.dart for parsing options instead. |
| 33 var patterns = <String>[]; | 34 var patterns = <String>[]; |
| 34 for (OptionHandler handler in handlers) { | 35 for (OptionHandler handler in handlers) { |
| 35 patterns.add(handler.pattern); | 36 patterns.add(handler.pattern); |
| 36 } | 37 } |
| 37 var pattern = new RegExp('^(${Strings.join(patterns, ")|(")})\$'); | 38 var pattern = new RegExp('^(${Strings.join(patterns, ")|(")})\$'); |
| 38 OUTER: for (String argument in argv) { | 39 OUTER: for (String argument in argv) { |
| 39 Match match = pattern.firstMatch(argument); | 40 Match match = pattern.firstMatch(argument); |
| 40 assert(match.groupCount() == handlers.length); | 41 assert(match.groupCount() == handlers.length); |
| 41 for (int i = 0; i < handlers.length; i++) { | 42 for (int i = 0; i < handlers.length; i++) { |
| 42 if (match[i + 1] !== null) { | 43 if (match[i + 1] !== null) { |
| 43 handlers[i].handle(argument); | 44 handlers[i].handle(argument); |
| 44 continue OUTER; | 45 continue OUTER; |
| 45 } | 46 } |
| 46 } | 47 } |
| 47 throw 'Internal error: "$argument" did not match'; | 48 throw 'Internal error: "$argument" did not match'; |
| 48 } | 49 } |
| 49 } | 50 } |
| 50 | 51 |
| 51 void compile(List<String> argv) { | 52 void compile(List<String> argv) { |
| 52 Uri cwd = getCurrentDirectory(); | 53 Uri cwd = getCurrentDirectory(); |
| 53 bool throwOnError = false; | 54 bool throwOnError = false; |
| 54 bool showWarnings = true; | 55 bool showWarnings = true; |
| 55 bool verbose = false; | 56 bool verbose = false; |
| 56 Uri libraryRoot = cwd; | 57 Uri libraryRoot = cwd; |
| 57 Uri out = cwd.resolve('out.js'); | 58 Uri out = cwd.resolve('out.js'); |
| 58 List<String> options = new List<String>(); | 59 List<String> options = new List<String>(); |
| 59 bool explicitOut = false; | 60 bool explicitOut = false; |
| 61 bool wantHelp = false; |
| 60 | 62 |
| 61 passThrough(String argument) => options.add(argument); | 63 passThrough(String argument) => options.add(argument); |
| 62 | 64 |
| 63 List<String> arguments = <String>[]; | 65 List<String> arguments = <String>[]; |
| 64 List<OptionHandler> handlers = <OptionHandler>[ | 66 List<OptionHandler> handlers = <OptionHandler>[ |
| 65 new OptionHandler('--throw-on-error', (_) => throwOnError = true), | 67 new OptionHandler('--throw-on-error', (_) => throwOnError = true), |
| 66 new OptionHandler('--suppress-warnings', (_) => showWarnings = false), | 68 new OptionHandler('--suppress-warnings', (_) => showWarnings = false), |
| 67 new OptionHandler('--verbose', (_) => verbose = true), | 69 new OptionHandler('--verbose|-v', (_) => verbose = true), |
| 68 new OptionHandler('--library-root=.*', (String argument) { | 70 new OptionHandler('--library-root=.+', (String argument) { |
| 69 String path = nativeToUriPath(extractParameter(argument)); | 71 String path = nativeToUriPath(extractParameter(argument)); |
| 70 if (!path.endsWith("/")) path = "$path/"; | 72 if (!path.endsWith("/")) path = "$path/"; |
| 71 libraryRoot = cwd.resolve(path); | 73 libraryRoot = cwd.resolve(path); |
| 72 }), | 74 }), |
| 73 new OptionHandler('--out=.*', (String argument) { | 75 new OptionHandler('--out=.+|-o.+', (String argument) { |
| 74 explicitOut = true; | 76 explicitOut = true; |
| 75 out = cwd.resolve(nativeToUriPath(extractParameter(argument))); | 77 out = cwd.resolve(nativeToUriPath(extractParameter(argument))); |
| 76 }), | 78 }), |
| 77 new OptionHandler('--allow-mock-compilation', passThrough), | 79 new OptionHandler('--allow-mock-compilation', passThrough), |
| 78 new OptionHandler('--no-colors', (_) => colors.enabled = false), | 80 new OptionHandler('--no-colors', (_) => colors.enabled = false), |
| 79 new OptionHandler('--enable-checked-mode|--checked|-c', | 81 new OptionHandler('--enable[_-]checked[_-]mode|--checked|-c', |
| 80 (_) => passThrough('--enable-checked-mode')), | 82 (_) => passThrough('--enable-checked-mode')), |
| 81 new OptionHandler('--help', (_) => helpAndExit()), | 83 new OptionHandler(@'--help|-h|/\?|/h', (_) => wantHelp = true), |
| 82 // The following two options must come last. | 84 // The following two options must come last. |
| 83 new OptionHandler('-.*', (String argument) { | 85 new OptionHandler('-.*', (String argument) { |
| 84 helpAndFail('Error: Unknown option "$argument".'); | 86 helpAndFail('Error: Unknown option "$argument".'); |
| 85 }), | 87 }), |
| 86 new OptionHandler('.*', (String argument) { | 88 new OptionHandler('.*', (String argument) { |
| 87 arguments.add(nativeToUriPath(argument)); | 89 arguments.add(nativeToUriPath(argument)); |
| 88 }) | 90 }) |
| 89 ]; | 91 ]; |
| 90 | 92 |
| 91 parseCommandLine(handlers, argv); | 93 parseCommandLine(handlers, argv); |
| 92 | 94 |
| 95 if (wantHelp) helpAndExit(verbose); |
| 96 |
| 93 if (arguments.isEmpty()) { | 97 if (arguments.isEmpty()) { |
| 94 helpAndFail('Error: No Dart file specified.'); | 98 helpAndFail('Error: No Dart file specified.'); |
| 95 } | 99 } |
| 96 if (arguments.length > 1) { | 100 if (arguments.length > 1) { |
| 97 var extra = arguments.getRange(1, arguments.length - 1); | 101 var extra = arguments.getRange(1, arguments.length - 1); |
| 98 helpAndFail('Error: Extra arguments: ${Strings.join(extra, " ")}'); | 102 helpAndFail('Error: Extra arguments: ${Strings.join(extra, " ")}'); |
| 99 } | 103 } |
| 100 | 104 |
| 101 Map<String, SourceFile> sourceFiles = <SourceFile>{}; | 105 Map<String, SourceFile> sourceFiles = <SourceFile>{}; |
| 102 int dartBytesRead = 0; | 106 int dartBytesRead = 0; |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 List<String> argv = ['--library-root=${options.script}$root']; | 191 List<String> argv = ['--library-root=${options.script}$root']; |
| 188 argv.addAll(options.arguments); | 192 argv.addAll(options.arguments); |
| 189 compile(argv); | 193 compile(argv); |
| 190 } | 194 } |
| 191 | 195 |
| 192 void help() { | 196 void help() { |
| 193 // This message should be no longer than 22 lines. The default | 197 // This message should be no longer than 22 lines. The default |
| 194 // terminal size normally 80x24. Two lines are used for the prompts | 198 // terminal size normally 80x24. Two lines are used for the prompts |
| 195 // before and after running the compiler. | 199 // before and after running the compiler. |
| 196 print(''' | 200 print(''' |
| 197 Usage: dart2js [OPTIONS] DARTFILE | 201 Usage: dart2js [options] dartfile |
| 198 | 202 |
| 199 Compiles Dart to JavaScript. | 203 Compiles Dart to JavaScript. |
| 200 | 204 |
| 201 Common options: | 205 Common options: |
| 202 --help Display this message. | 206 -o<file> Generate the output into <file>. |
| 203 --out=FILE Save the output to FILE (default: out.js). | 207 -c Insert runtime type checks and enable assertions (checked mode). |
| 204 --checked Turn on checked mode in generated JavaScript code.'''); | 208 -h Display this message (add -v for information about all options).'''); |
| 205 } | 209 } |
| 206 | 210 |
| 207 void helpAndExit() { | 211 void verboseHelp() { |
| 208 help(); | 212 print(''' |
| 213 Usage: dart2js [options] dartfile |
| 214 |
| 215 Compiles Dart to JavaScript. |
| 216 |
| 217 Supported options: |
| 218 -o<file>, --out=<file> |
| 219 Generate the output into <file>. |
| 220 |
| 221 -c, --enable-checked-mode, --checked |
| 222 Insert runtime type checks and enable assertions (checked mode). |
| 223 |
| 224 -h, /h, /?, --help |
| 225 Display this message (add -v for information about all options). |
| 226 |
| 227 -v, --verbose |
| 228 Display verbose information. |
| 229 |
| 230 --suppress-warnings |
| 231 Do not display any warnings. |
| 232 |
| 233 --no-colors |
| 234 Do not add colors to diagnostic messages. |
| 235 |
| 236 The following options are only used for compiler development and may |
| 237 be removed in a future version: |
| 238 --throw-on-error |
| 239 Throw an exception if a compile-time error is detected. |
| 240 |
| 241 --library-root=<directory> |
| 242 Where to find the Dart platform libraries. |
| 243 |
| 244 --allow-mock-compilation |
| 245 Do not generate a call to main if either of the following |
| 246 libraries are used: dart:dom, dart:html dart:io.'''); |
| 247 } |
| 248 |
| 249 void helpAndExit(bool verbose) { |
| 250 if (verbose) { |
| 251 verboseHelp(); |
| 252 } else { |
| 253 help(); |
| 254 } |
| 209 exit(0); | 255 exit(0); |
| 210 } | 256 } |
| 211 | 257 |
| 212 void helpAndFail(String message) { | 258 void helpAndFail(String message) { |
| 213 help(); | 259 help(); |
| 214 print(''); | 260 print(''); |
| 215 fail(message); | 261 fail(message); |
| 216 } | 262 } |
| 217 | 263 |
| 218 void main() { | 264 void main() { |
| 219 try { | 265 try { |
| 220 compilerMain(new Options()); | 266 compilerMain(new Options()); |
| 221 } catch (var exception, var trace) { | 267 } catch (var exception, var trace) { |
| 222 try { | 268 try { |
| 223 print('Internal error: $exception'); | 269 print('Internal error: $exception'); |
| 224 } catch (var ignored) { | 270 } catch (var ignored) { |
| 225 print('Internal error: error while printing exception'); | 271 print('Internal error: error while printing exception'); |
| 226 } | 272 } |
| 227 try { | 273 try { |
| 228 print(trace); | 274 print(trace); |
| 229 } finally { | 275 } finally { |
| 230 exit(253); // 253 is recognized as a crash by our test scripts. | 276 exit(253); // 253 is recognized as a crash by our test scripts. |
| 231 } | 277 } |
| 232 } | 278 } |
| 233 } | 279 } |
| OLD | NEW |