Integrate error messages on the REPL

This commit is contained in:
Demian Ferreiro 2013-03-03 22:08:27 -03:00
parent 55c99dfaec
commit 0affb4f936
2 changed files with 34 additions and 16 deletions

View File

@ -1,6 +1,6 @@
// Generated by CoffeeScript 1.5.0 // Generated by CoffeeScript 1.5.0
(function() { (function() {
var CoffeeScript, addMultilineHandler, merge, nodeREPL, replDefaults, vm; var CoffeeScript, CompilerError, addMultilineHandler, merge, nodeREPL, replDefaults, vm;
vm = require('vm'); vm = require('vm');
@ -8,25 +8,30 @@
CoffeeScript = require('./coffee-script'); CoffeeScript = require('./coffee-script');
CompilerError = require('./error').CompilerError;
merge = require('./helpers').merge; merge = require('./helpers').merge;
replDefaults = { replDefaults = {
prompt: 'coffee> ', prompt: 'coffee> ',
"eval": function(input, context, filename, cb) { "eval": function(input, context, filename, cb) {
var js; var Assign, Block, Literal, Value, ast, js, _ref;
input = input.replace(/\uFF00/g, '\n'); input = input.replace(/\uFF00/g, '\n');
input = input.replace(/(^|[\r\n]+)(\s*)##?(?:[^#\r\n][^\r\n]*|)($|[\r\n])/, '$1$2$3'); input = input.replace(/^\(([\s\S]*)\n\)$/m, '$1');
if (/^\s*$/.test(input)) { _ref = require('./nodes'), Block = _ref.Block, Assign = _ref.Assign, Value = _ref.Value, Literal = _ref.Literal;
return cb(null);
}
try { try {
js = CoffeeScript.compile("_=(" + input + "\n)", { ast = CoffeeScript.nodes(input);
filename: filename, ast = new Block([new Assign(new Value(new Literal('_')), ast, '=')]);
js = ast.compile({
bare: true bare: true
}); });
} catch (err) { } catch (err) {
if (err instanceof CompilerError) {
console.log(err.prettyMessage(filename, input, true));
} else {
cb(err); cb(err);
} }
}
return cb(null, vm.runInContext(js, context, filename)); return cb(null, vm.runInContext(js, context, filename));
} }
}; };

View File

@ -1,21 +1,34 @@
vm = require 'vm' vm = require 'vm'
nodeREPL = require 'repl' nodeREPL = require 'repl'
CoffeeScript = require './coffee-script' CoffeeScript = require './coffee-script'
{CompilerError} = require './error'
{merge} = require './helpers' {merge} = require './helpers'
replDefaults = replDefaults =
prompt: 'coffee> ', prompt: 'coffee> ',
eval: (input, context, filename, cb) -> eval: (input, context, filename, cb) ->
# XXX: multiline hack # XXX: multiline hack.
input = input.replace /\uFF00/g, '\n' input = input.replace /\uFF00/g, '\n'
# strip single-line comments # Node's REPL sends the input ending with a newline and then wrapped in
input = input.replace /(^|[\r\n]+)(\s*)##?(?:[^#\r\n][^\r\n]*|)($|[\r\n])/, '$1$2$3' # parens. Unwrap all that.
# empty command input = input.replace /^\(([\s\S]*)\n\)$/m, '$1'
return cb null if /^\s*$/.test input
# Require AST nodes to do some AST manipulation.
{Block, Assign, Value, Literal} = require './nodes'
# TODO: fix #1829: pass in-scope vars and avoid accidentally shadowing them by omitting those declarations # TODO: fix #1829: pass in-scope vars and avoid accidentally shadowing them by omitting those declarations
try try
js = CoffeeScript.compile "_=(#{input}\n)", {filename, bare: yes} # Generate the AST of the clean input.
ast = CoffeeScript.nodes input
# Add assignment to `_` variable to force the input to be an expression.
ast = new Block [
new Assign (new Value new Literal '_'), ast, '='
]
js = ast.compile bare: yes
catch err catch err
if err instanceof CompilerError
console.log err.prettyMessage filename, input, yes
else
cb err cb err
cb null, vm.runInContext(js, context, filename) cb null, vm.runInContext(js, context, filename)