diff --git a/lib/cake.js b/lib/cake.js index 73274bdb..cc3eb923 100755 --- a/lib/cake.js +++ b/lib/cake.js @@ -77,4 +77,4 @@ }); }); }; -})(); \ No newline at end of file +})(); diff --git a/lib/coffee-script.js b/lib/coffee-script.js index 098d3b72..bd797098 100644 --- a/lib/coffee-script.js +++ b/lib/coffee-script.js @@ -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(' ')); - }; -})(); \ No newline at end of file +})(); diff --git a/lib/command_line.js b/lib/command_line.js index fe23e186..006ba41b 100644 --- a/lib/command_line.js +++ b/lib/command_line.js @@ -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 } : {}; }; -})(); \ No newline at end of file +})(); diff --git a/lib/grammar.js b/lib/grammar.js index f7cb2196..6adef6db 100644 --- a/lib/grammar.js +++ b/lib/grammar.js @@ -561,4 +561,4 @@ }, { debug: false }); -})(); \ No newline at end of file +})(); diff --git a/lib/lexer.js b/lib/lexer.js index 53ad4fee..d0506c86 100644 --- a/lib/lexer.js +++ b/lib/lexer.js @@ -402,4 +402,4 @@ lex.prototype.close_indentation = function close_indentation() { return this.outdent_token(this.indent); }; -})(); \ No newline at end of file +})(); diff --git a/lib/narwhal.js b/lib/narwhal.js index e5633864..604fdd41 100755 --- a/lib/narwhal.js +++ b/lib/narwhal.js @@ -41,4 +41,4 @@ return factories[topId] = factories[topId] || this.reload(topId, path); }; require.loader.loaders.unshift([".coffee", loader]); -})(); \ No newline at end of file +})(); diff --git a/lib/nodes.js b/lib/nodes.js index 0b405b65..8d41f859 100644 --- a/lib/nodes.js +++ b/lib/nodes.js @@ -1325,4 +1325,4 @@ return if_part + ' : ' + else_part; } })); -})(); \ No newline at end of file +})(); diff --git a/lib/optparse.js b/lib/optparse.js index 987a9761..94faa4bd 100755 --- a/lib/optparse.js +++ b/lib/optparse.js @@ -108,4 +108,4 @@ } return builder.join(''); }; -})(); \ No newline at end of file +})(); diff --git a/lib/repl.js b/lib/repl.js index 18caaef0..344ff766 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -29,4 +29,4 @@ process.stdio.addListener('data', readline); process.stdio.open(); print(prompt); -})(); \ No newline at end of file +})(); diff --git a/lib/rewriter.js b/lib/rewriter.js index 47763e4d..ca9c76a9 100644 --- a/lib/rewriter.js +++ b/lib/rewriter.js @@ -380,4 +380,4 @@ }); })(this)); }; -})(); \ No newline at end of file +})(); diff --git a/lib/scope.js b/lib/scope.js index 694327d6..86591e51 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -111,4 +111,4 @@ Scope.prototype.compiled_assignments = function compiled_assignments() { return this.assigned_variables().join(', '); }; -})(); \ No newline at end of file +})(); diff --git a/src/coffee-script.coffee b/src/coffee-script.coffee index 36500bec..628ecb4d 100644 --- a/src/coffee-script.coffee +++ b/src/coffee-script.coffee @@ -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(' ') diff --git a/src/command_line.coffee b/src/command_line.coffee index bc9c6a29..55e4a887 100644 --- a/src/command_line.coffee +++ b/src/command_line.coffee @@ -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