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

View File

@ -1,21 +1,34 @@
vm = require 'vm'
nodeREPL = require 'repl'
CoffeeScript = require './coffee-script'
{CompilerError} = require './error'
{merge} = require './helpers'
replDefaults =
prompt: 'coffee> ',
eval: (input, context, filename, cb) ->
# XXX: multiline hack
# XXX: multiline hack.
input = input.replace /\uFF00/g, '\n'
# strip single-line comments
input = input.replace /(^|[\r\n]+)(\s*)##?(?:[^#\r\n][^\r\n]*|)($|[\r\n])/, '$1$2$3'
# empty command
return cb null if /^\s*$/.test input
# Node's REPL sends the input ending with a newline and then wrapped in
# parens. Unwrap all that.
input = input.replace /^\(([\s\S]*)\n\)$/m, '$1'
# 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
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
if err instanceof CompilerError
console.log err.prettyMessage filename, input, yes
else
cb err
cb null, vm.runInContext(js, context, filename)