mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
moving print_tokens (the pretty printer) from coffee_script to command_line
This commit is contained in:
parent
b26e577244
commit
2a46e13d33
13 changed files with 49 additions and 46 deletions
|
@ -77,4 +77,4 @@
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
})();
|
})();
|
||||||
|
|
|
@ -45,17 +45,4 @@
|
||||||
exports.tree = function tree(code) {
|
exports.tree = function tree(code) {
|
||||||
return parser.parse(lexer.tokenize(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(' '));
|
|
||||||
};
|
|
||||||
})();
|
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
(function(){
|
(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');
|
fs = require('fs');
|
||||||
path = require('path');
|
path = require('path');
|
||||||
coffee = require('coffee-script');
|
coffee = require('coffee-script');
|
||||||
optparse = require('optparse');
|
optparse = require('optparse');
|
||||||
BANNER = "coffee compiles CoffeeScript source files into JavaScript.\n\nUsage:\n coffee path/to/script.coffee";
|
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 = {};
|
options = {};
|
||||||
sources = [];
|
sources = [];
|
||||||
option_parser = null;
|
option_parser = null;
|
||||||
|
@ -73,26 +73,27 @@
|
||||||
// Compile a single source script, containing the given code, according to the
|
// Compile a single source script, containing the given code, according to the
|
||||||
// requested options. Both compile_scripts and watch_scripts share this method.
|
// requested options. Both compile_scripts and watch_scripts share this method.
|
||||||
compile_script = function compile_script(source, code) {
|
compile_script = function compile_script(source, code) {
|
||||||
var js;
|
var js, o;
|
||||||
|
o = options;
|
||||||
try {
|
try {
|
||||||
if (options.tokens) {
|
if (o.tokens) {
|
||||||
return coffee.print_tokens(coffee.tokenize(code));
|
return print_tokens(coffee.tokenize(code));
|
||||||
} else if (options.tree) {
|
} else if (o.tree) {
|
||||||
return puts(coffee.tree(code).toString());
|
return puts(coffee.tree(code).toString());
|
||||||
} else {
|
} else {
|
||||||
js = coffee.compile(code, compile_options());
|
js = coffee.compile(code, compile_options());
|
||||||
if (options.run) {
|
if (o.run) {
|
||||||
return eval(js);
|
return eval(js);
|
||||||
} else if (options.lint) {
|
} else if (o.lint) {
|
||||||
return lint(js);
|
return lint(js);
|
||||||
} else if (options.print || options.eval) {
|
} else if (o.print || o.eval) {
|
||||||
return puts(js);
|
return puts(js);
|
||||||
} else {
|
} else {
|
||||||
return write_js(source, js);
|
return write_js(source, js);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (options.watch) {
|
if (o.watch) {
|
||||||
return puts(err.message);
|
return puts(err.message);
|
||||||
} else {
|
} else {
|
||||||
throw err;
|
throw err;
|
||||||
|
@ -162,6 +163,19 @@
|
||||||
jsl.write(js);
|
jsl.write(js);
|
||||||
return jsl.close();
|
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.
|
// Use OptionParser for all the options.
|
||||||
parse_options = function parse_options() {
|
parse_options = function parse_options() {
|
||||||
option_parser = new optparse.OptionParser(SWITCHES, BANNER);
|
option_parser = new optparse.OptionParser(SWITCHES, BANNER);
|
||||||
|
@ -174,4 +188,4 @@
|
||||||
no_wrap: true
|
no_wrap: true
|
||||||
} : {};
|
} : {};
|
||||||
};
|
};
|
||||||
})();
|
})();
|
||||||
|
|
|
@ -561,4 +561,4 @@
|
||||||
}, {
|
}, {
|
||||||
debug: false
|
debug: false
|
||||||
});
|
});
|
||||||
})();
|
})();
|
||||||
|
|
|
@ -402,4 +402,4 @@
|
||||||
lex.prototype.close_indentation = function close_indentation() {
|
lex.prototype.close_indentation = function close_indentation() {
|
||||||
return this.outdent_token(this.indent);
|
return this.outdent_token(this.indent);
|
||||||
};
|
};
|
||||||
})();
|
})();
|
||||||
|
|
|
@ -41,4 +41,4 @@
|
||||||
return factories[topId] = factories[topId] || this.reload(topId, path);
|
return factories[topId] = factories[topId] || this.reload(topId, path);
|
||||||
};
|
};
|
||||||
require.loader.loaders.unshift([".coffee", loader]);
|
require.loader.loaders.unshift([".coffee", loader]);
|
||||||
})();
|
})();
|
||||||
|
|
|
@ -1325,4 +1325,4 @@
|
||||||
return if_part + ' : ' + else_part;
|
return if_part + ' : ' + else_part;
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
})();
|
})();
|
||||||
|
|
|
@ -108,4 +108,4 @@
|
||||||
}
|
}
|
||||||
return builder.join('');
|
return builder.join('');
|
||||||
};
|
};
|
||||||
})();
|
})();
|
||||||
|
|
|
@ -29,4 +29,4 @@
|
||||||
process.stdio.addListener('data', readline);
|
process.stdio.addListener('data', readline);
|
||||||
process.stdio.open();
|
process.stdio.open();
|
||||||
print(prompt);
|
print(prompt);
|
||||||
})();
|
})();
|
||||||
|
|
|
@ -380,4 +380,4 @@
|
||||||
});
|
});
|
||||||
})(this));
|
})(this));
|
||||||
};
|
};
|
||||||
})();
|
})();
|
||||||
|
|
|
@ -111,4 +111,4 @@
|
||||||
Scope.prototype.compiled_assignments = function compiled_assignments() {
|
Scope.prototype.compiled_assignments = function compiled_assignments() {
|
||||||
return this.assigned_variables().join(', ');
|
return this.assigned_variables().join(', ');
|
||||||
};
|
};
|
||||||
})();
|
})();
|
||||||
|
|
|
@ -38,8 +38,3 @@ exports.tokenize: (code) ->
|
||||||
exports.tree: (code) ->
|
exports.tree: (code) ->
|
||||||
parser.parse lexer.tokenize 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(' ')
|
|
||||||
|
|
|
@ -69,17 +69,18 @@ compile_scripts: ->
|
||||||
# Compile a single source script, containing the given code, according to the
|
# Compile a single source script, containing the given code, according to the
|
||||||
# requested options. Both compile_scripts and watch_scripts share this method.
|
# requested options. Both compile_scripts and watch_scripts share this method.
|
||||||
compile_script: (source, code) ->
|
compile_script: (source, code) ->
|
||||||
|
o: options
|
||||||
try
|
try
|
||||||
if options.tokens then coffee.print_tokens coffee.tokenize code
|
if o.tokens then print_tokens coffee.tokenize code
|
||||||
else if options.tree then puts coffee.tree(code).toString()
|
else if o.tree then puts coffee.tree(code).toString()
|
||||||
else
|
else
|
||||||
js: coffee.compile code, compile_options()
|
js: coffee.compile code, compile_options()
|
||||||
if options.run then eval js
|
if o.run then eval js
|
||||||
else if options.lint then lint js
|
else if o.lint then lint js
|
||||||
else if options.print or options.eval then puts js
|
else if o.print or o.eval then puts js
|
||||||
else write_js source, js
|
else write_js source, js
|
||||||
catch err
|
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.
|
# Listen for and compile scripts over stdio.
|
||||||
compile_stdio: ->
|
compile_stdio: ->
|
||||||
|
@ -116,6 +117,12 @@ lint: (js) ->
|
||||||
jsl.write js
|
jsl.write js
|
||||||
jsl.close()
|
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.
|
# Use OptionParser for all the options.
|
||||||
parse_options: ->
|
parse_options: ->
|
||||||
option_parser: new optparse.OptionParser SWITCHES, BANNER
|
option_parser: new optparse.OptionParser SWITCHES, BANNER
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue