added the ability to print the parse tree

This commit is contained in:
Jeremy Ashkenas
2010-02-11 23:11:05 -05:00
parent 950d1199c2
commit 7c01bba4f4
6 changed files with 92 additions and 10 deletions

View File

@@ -4,7 +4,7 @@
posix = require('posix');
coffee = require('coffee-script');
BANNER = "coffee compiles CoffeeScript source files into JavaScript.\n\nUsage:\n coffee path/to/script.coffee";
SWITCHES = [['-i', '--interactive', 'run an interactive CoffeeScript REPL'], ['-r', '--run', 'compile and run a CoffeeScript'], ['-o', '--output [DIR]', 'set the directory for compiled JavaScript'], ['-w', '--watch', 'watch scripts for changes, and recompile'], ['-p', '--print', 'print the compiled JavaScript to stdout'], ['-l', '--lint', 'pipe the compiled JavaScript through JSLint'], ['-e', '--eval', 'compile a cli scriptlet or read from stdin'], ['-t', '--tokens', 'print the tokens that the lexer produces'], ['-n', '--no-wrap', 'raw output, no function safety wrapper'], ['-g', '--globals', 'attach all top-level variables as globals'], ['-v', '--version', 'display CoffeeScript version'], ['-h', '--help', 'display this help message']];
SWITCHES = [['-i', '--interactive', 'run an interactive CoffeeScript REPL'], ['-r', '--run', 'compile and run a CoffeeScript'], ['-o', '--output [DIR]', 'set the directory for compiled JavaScript'], ['-w', '--watch', 'watch scripts for changes, and recompile'], ['-p', '--print', 'print the compiled JavaScript to stdout'], ['-l', '--lint', 'pipe the compiled JavaScript through JSLint'], ['-e', '--eval', 'compile a cli scriptlet or read from stdin'], ['-t', '--tokens', 'print the tokens that the lexer produces'], ['--tree', 'print the parse tree that Jison produces'], ['-n', '--no-wrap', 'raw output, no function safety wrapper'], ['-g', '--globals', 'attach all top-level variables as globals'], ['-v', '--version', 'display CoffeeScript version'], ['-h', '--help', 'display this help message']];
WATCH_INTERVAL = 0.5;
// The CommandLine handles all of the functionality of the `coffee` utility.
exports.run = function run() {
@@ -53,16 +53,17 @@
}
opts = this.options;
return posix.cat(source).addCallback(function(code) {
var js;
if (opts.tokens) {
return puts(coffee.tokenize(code).join(' '));
}
js = coffee.compile(code);
if (opts.tree) {
return puts(coffee.tree(code).toString());
}
if (opts.run) {
return eval(js);
return eval(coffee.compile(code));
}
if (opts.print) {
return puts(js);
return puts(coffee.compile(code));
}
return exports.compile_scripts();
});
@@ -98,6 +99,9 @@
oparser.add('tokens', function() {
return opts.tokens = true;
});
oparser.add('tree', function() {
return opts.tree = true;
});
oparser.add('help', (function(__this) {
var __func = function() {
return this.usage();