moving print_tokens (the pretty printer) from coffee_script to command_line

This commit is contained in:
Jeremy Ashkenas 2010-02-24 18:56:32 -05:00
parent b26e577244
commit 2a46e13d33
13 changed files with 49 additions and 46 deletions

View File

@ -77,4 +77,4 @@
});
});
};
})();
})();

View File

@ -45,17 +45,4 @@
exports.tree = function tree(code) {
return parser.parse(lexer.tokenize(code));
};
// Pretty-print a token stream.
exports.print_tokens = function print_tokens(tokens) {
var _a, _b, _c, strings, token;
strings = (function() {
_a = []; _b = tokens;
for (_c = 0; _c < _b.length; _c++) {
token = _b[_c];
_a.push('[' + token[0] + ' ' + token[1].toString().replace(/\n/, '\\n') + ']');
}
return _a;
}).call(this);
return puts(strings.join(' '));
};
})();
})();

View File

@ -1,11 +1,11 @@
(function(){
var BANNER, SWITCHES, coffee, compile_options, compile_script, compile_scripts, compile_stdio, fs, lint, option_parser, options, optparse, parse_options, path, sources, usage, version, watch_scripts, write_js;
var BANNER, SWITCHES, coffee, compile_options, compile_script, compile_scripts, compile_stdio, fs, lint, option_parser, options, optparse, parse_options, path, print_tokens, sources, usage, version, watch_scripts, write_js;
fs = require('fs');
path = require('path');
coffee = require('coffee-script');
optparse = require('optparse');
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'], ['-s', '--stdio', 'listen for and compile scripts over stdio'], ['-e', '--eval', 'compile a string from the command line'], ['-t', '--tokens', 'print the tokens that the lexer produces'], ['-tr', '--tree', 'print the parse tree that Jison produces'], ['-n', '--no-wrap', 'compile without the top-level function wrapper'], ['-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'], ['-s', '--stdio', 'listen for and compile scripts over stdio'], ['-e', '--eval', 'compile a string from the command line'], ['-n', '--no-wrap', 'compile without the top-level function wrapper'], ['-t', '--tokens', 'print the tokens that the lexer produces'], ['-tr', '--tree', 'print the parse tree that Jison produces'], ['-v', '--version', 'display CoffeeScript version'], ['-h', '--help', 'display this help message']];
options = {};
sources = [];
option_parser = null;
@ -73,26 +73,27 @@
// Compile a single source script, containing the given code, according to the
// requested options. Both compile_scripts and watch_scripts share this method.
compile_script = function compile_script(source, code) {
var js;
var js, o;
o = options;
try {
if (options.tokens) {
return coffee.print_tokens(coffee.tokenize(code));
} else if (options.tree) {
if (o.tokens) {
return print_tokens(coffee.tokenize(code));
} else if (o.tree) {
return puts(coffee.tree(code).toString());
} else {
js = coffee.compile(code, compile_options());
if (options.run) {
if (o.run) {
return eval(js);
} else if (options.lint) {
} else if (o.lint) {
return lint(js);
} else if (options.print || options.eval) {
} else if (o.print || o.eval) {
return puts(js);
} else {
return write_js(source, js);
}
}
} catch (err) {
if (options.watch) {
if (o.watch) {
return puts(err.message);
} else {
throw err;
@ -162,6 +163,19 @@
jsl.write(js);
return jsl.close();
};
// Pretty-print a token stream.
print_tokens = function print_tokens(tokens) {
var _a, _b, _c, strings, token;
strings = (function() {
_a = []; _b = tokens;
for (_c = 0; _c < _b.length; _c++) {
token = _b[_c];
_a.push('[' + token[0] + ' ' + token[1].toString().replace(/\n/, '\\n') + ']');
}
return _a;
}).call(this);
return puts(strings.join(' '));
};
// Use OptionParser for all the options.
parse_options = function parse_options() {
option_parser = new optparse.OptionParser(SWITCHES, BANNER);
@ -174,4 +188,4 @@
no_wrap: true
} : {};
};
})();
})();

View File

@ -561,4 +561,4 @@
}, {
debug: false
});
})();
})();

View File

@ -402,4 +402,4 @@
lex.prototype.close_indentation = function close_indentation() {
return this.outdent_token(this.indent);
};
})();
})();

View File

@ -41,4 +41,4 @@
return factories[topId] = factories[topId] || this.reload(topId, path);
};
require.loader.loaders.unshift([".coffee", loader]);
})();
})();

View File

@ -1325,4 +1325,4 @@
return if_part + ' : ' + else_part;
}
}));
})();
})();

View File

@ -108,4 +108,4 @@
}
return builder.join('');
};
})();
})();

View File

@ -29,4 +29,4 @@
process.stdio.addListener('data', readline);
process.stdio.open();
print(prompt);
})();
})();

View File

@ -380,4 +380,4 @@
});
})(this));
};
})();
})();

View File

@ -111,4 +111,4 @@
Scope.prototype.compiled_assignments = function compiled_assignments() {
return this.assigned_variables().join(', ');
};
})();
})();

View File

@ -38,8 +38,3 @@ exports.tokenize: (code) ->
exports.tree: (code) ->
parser.parse lexer.tokenize code
# Pretty-print a token stream.
exports.print_tokens: (tokens) ->
strings: for token in tokens
'[' + token[0] + ' ' + token[1].toString().replace(/\n/, '\\n') + ']'
puts strings.join(' ')

View File

@ -69,17 +69,18 @@ compile_scripts: ->
# Compile a single source script, containing the given code, according to the
# requested options. Both compile_scripts and watch_scripts share this method.
compile_script: (source, code) ->
o: options
try
if options.tokens then coffee.print_tokens coffee.tokenize code
else if options.tree then puts coffee.tree(code).toString()
if o.tokens then print_tokens coffee.tokenize code
else if o.tree then puts coffee.tree(code).toString()
else
js: coffee.compile code, compile_options()
if options.run then eval js
else if options.lint then lint js
else if options.print or options.eval then puts js
else write_js source, js
if o.run then eval js
else if o.lint then lint js
else if o.print or o.eval then puts js
else write_js source, js
catch err
if options.watch then puts err.message else throw err
if o.watch then puts err.message else throw err
# Listen for and compile scripts over stdio.
compile_stdio: ->
@ -116,6 +117,12 @@ lint: (js) ->
jsl.write js
jsl.close()
# Pretty-print a token stream.
print_tokens: (tokens) ->
strings: for token in tokens
'[' + token[0] + ' ' + token[1].toString().replace(/\n/, '\\n') + ']'
puts strings.join(' ')
# Use OptionParser for all the options.
parse_options: ->
option_parser: new optparse.OptionParser SWITCHES, BANNER