From 02ac3edebfcf8130c610cbec64d20b8401137a9d Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Sat, 13 Feb 2010 09:59:13 -0500 Subject: [PATCH] Nicer-looking --tokenize, more in line with what Ruby's doing --- lib/coffee_script/command_line.js | 10 +++++++++- src/command_line.coffee | 8 +++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/coffee_script/command_line.js b/lib/coffee_script/command_line.js index 51700d8f..5ccc7f4d 100644 --- a/lib/coffee_script/command_line.js +++ b/lib/coffee_script/command_line.js @@ -55,7 +55,7 @@ return posix.cat(source).addCallback(function(code) { var js; if (opts.tokens) { - puts(coffee.tokenize(code).join(' ')); + puts(exports.tokenize(code)); } else if (opts.tree) { puts(coffee.tree(code).toString()); } else { @@ -83,6 +83,14 @@ return posix.write(fd, js); }); }; + // Pretty-print the token stream. + exports.tokenize = function tokenize(code) { + var strings; + strings = coffee.tokenize(code).map(function(token) { + return '[' + token[0] + ' ' + token[1].toString().replace(/\n/, '\\n') + ']'; + }); + return strings.join(' '); + }; // Pipe compiled JS through JSLint (requires a working 'jsl' command). exports.lint = function lint(js) { var jsl; diff --git a/src/command_line.coffee b/src/command_line.coffee index 69a6e4c2..33b495bf 100644 --- a/src/command_line.coffee +++ b/src/command_line.coffee @@ -63,7 +63,7 @@ exports.compile_scripts: -> return unless source: @sources.shift() opts: @options posix.cat(source).addCallback (code) -> - if opts.tokens then puts coffee.tokenize(code).join(' ') + if opts.tokens then puts exports.tokenize(code) else if opts.tree then puts coffee.tree(code).toString() else js: coffee.compile code @@ -81,6 +81,12 @@ exports.write_js: (source, js) -> posix.open(js_path, process.O_CREAT | process.O_WRONLY | process.O_TRUNC, parseInt('0755', 8)).addCallback (fd) -> posix.write(fd, js) +# Pretty-print the token stream. +exports.tokenize: (code) -> + strings: coffee.tokenize(code).map (token) -> + '[' + token[0] + ' ' + token[1].toString().replace(/\n/, '\\n') + ']' + strings.join(' ') + # Pipe compiled JS through JSLint (requires a working 'jsl' command). exports.lint: (js) -> jsl: process.createChildProcess('jsl', ['-nologo', '-stdin'])