From 9c3040b7049ffdc913605a7c5e92adc056893855 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Mon, 15 Feb 2010 18:09:01 -0500 Subject: [PATCH] moving print_tokens into the actual coffee-script module, so we can use it separately from the command-line --- lib/coffee_script/coffee-script.js | 13 +++++++++++++ lib/coffee_script/command_line.js | 12 ++---------- src/coffee-script.coffee | 8 +++++++- src/command_line.coffee | 8 +------- 4 files changed, 23 insertions(+), 18 deletions(-) diff --git a/lib/coffee_script/coffee-script.js b/lib/coffee_script/coffee-script.js index 58652a20..dfa1ef89 100644 --- a/lib/coffee_script/coffee-script.js +++ b/lib/coffee_script/coffee-script.js @@ -48,6 +48,19 @@ 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 strings.join(' '); + }; //---------- Below this line is obsolete, for the Ruby compiler. ---------------- // The path to the CoffeeScript executable. compiler = function compiler() { diff --git a/lib/coffee_script/command_line.js b/lib/coffee_script/command_line.js index 3df7573b..15f1bfe9 100644 --- a/lib/coffee_script/command_line.js +++ b/lib/coffee_script/command_line.js @@ -1,5 +1,5 @@ (function(){ - var BANNER, SWITCHES, WATCH_INTERVAL, coffee, compile, compile_scripts, lint, option_parser, options, optparse, parse_options, path, posix, sources, tokenize, usage, version, write_js; + var BANNER, SWITCHES, WATCH_INTERVAL, coffee, compile, compile_scripts, lint, option_parser, options, optparse, parse_options, path, posix, sources, usage, version, write_js; posix = require('posix'); path = require('path'); coffee = require('coffee-script'); @@ -69,7 +69,7 @@ return posix.cat(source).addCallback(function(code) { var js; if (opts.tokens) { - puts(tokenize(code)); + puts(coffee.print_tokens(coffee.tokenize(code))); } else if (opts.tree) { puts(coffee.tree(code).toString()); } else { @@ -97,14 +97,6 @@ return posix.write(fd, js); }); }; - // Pretty-print the token stream. - 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). lint = function lint(js) { var jsl; diff --git a/src/coffee-script.coffee b/src/coffee-script.coffee index 21ebcabc..5297b11c 100644 --- a/src/coffee-script.coffee +++ b/src/coffee-script.coffee @@ -1,7 +1,7 @@ # Set up for both the browser and the server. if process? process.mixin require './nodes' - path: require('path') + path: require 'path' lexer: new (require('./lexer').Lexer)() parser: require('./parser').parser else @@ -42,6 +42,12 @@ 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') + ']' + strings.join(' ') + #---------- Below this line is obsolete, for the Ruby compiler. ---------------- diff --git a/src/command_line.coffee b/src/command_line.coffee index 5fd71eb9..43d4b107 100644 --- a/src/command_line.coffee +++ b/src/command_line.coffee @@ -68,7 +68,7 @@ compile_scripts: -> return unless source: sources.shift() opts: options posix.cat(source).addCallback (code) -> - if opts.tokens then puts tokenize(code) + if opts.tokens then puts coffee.print_tokens coffee.tokenize code else if opts.tree then puts coffee.tree(code).toString() else js: coffee.compile code @@ -86,12 +86,6 @@ 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. -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). lint: (js) -> jsl: process.createChildProcess('jsl', ['-nologo', '-stdin'])