Improved lexer error messages

This commit is contained in:
Demian Ferreiro
2013-02-25 14:41:34 -03:00
parent c39723c053
commit 25091fb2a0
10 changed files with 123 additions and 27 deletions

View File

@@ -1,6 +1,6 @@
// Generated by CoffeeScript 1.5.0
(function() {
var BANNER, CoffeeScript, EventEmitter, SWITCHES, coffee_exts, compileJoin, compileOptions, compilePath, compileScript, compileStdio, exec, exists, forkNode, fs, helpers, hidden, joinTimeout, lint, notSources, optionParser, optparse, opts, outputPath, parseOptions, path, printLine, printTokens, printWarn, removeSource, sourceCode, sources, spawn, timeLog, unwatchDir, usage, version, wait, watch, watchDir, watchers, writeJs, _ref,
var BANNER, CoffeeScript, CompilerError, EventEmitter, SWITCHES, coffee_exts, compileJoin, compileOptions, compilePath, compileScript, compileStdio, exec, exists, forkNode, fs, helpers, hidden, joinTimeout, lint, notSources, optionParser, optparse, opts, outputPath, parseOptions, path, printLine, printTokens, printWarn, removeSource, sourceCode, sources, spawn, timeLog, unwatchDir, usage, version, wait, watch, watchDir, watchers, writeJs, _ref,
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
fs = require('fs');
@@ -13,6 +13,8 @@
CoffeeScript = require('./coffee-script');
CompilerError = require('./error').CompilerError;
_ref = require('child_process'), spawn = _ref.spawn, exec = _ref.exec;
EventEmitter = require('events').EventEmitter;
@@ -159,7 +161,7 @@
};
compileScript = function(file, input, base) {
var o, options, t, task;
var message, o, options, t, task;
o = opts;
options = compileOptions(file);
try {
@@ -194,11 +196,15 @@
if (CoffeeScript.listeners('failure').length) {
return;
}
message = err instanceof CompilerError ? err.prettyMessage(file || '[stdin]', input) : err.stack || ("ERROR: " + err);
if (o.watch) {
return printLine(err.message + '\x07');
if (o.watch) {
return printLine(message + '\x07');
}
} else {
printWarn(message);
return process.exit(1);
}
printWarn(err instanceof Error && err.stack || ("ERROR: " + err));
return process.exit(1);
}
};

View File

@@ -0,0 +1,44 @@
// Generated by CoffeeScript 1.5.0
(function() {
var CompilerError, repeat,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
repeat = require('./helpers').repeat;
exports.CompilerError = CompilerError = (function(_super) {
__extends(CompilerError, _super);
CompilerError.prototype.name = 'CompilerError';
function CompilerError(message, startLine, startColumn, endLine, endColumn) {
this.message = message;
this.startLine = startLine;
this.startColumn = startColumn;
this.endLine = endLine != null ? endLine : this.startLine;
this.endColumn = endColumn != null ? endColumn : this.startColumn;
if (typeof Error.captureStackTrace === "function") {
Error.captureStackTrace(this, CompilerError);
}
}
CompilerError.prototype.prettyMessage = function(fileName, code) {
var errorLength, errorLine, marker, message;
message = "" + fileName + ":" + this.startLine + ":" + this.startColumn + ": " + this.message;
if (this.startLine === this.endLine) {
errorLine = code.split('\n')[this.startLine - 1];
errorLength = this.endColumn - this.startColumn + 1;
marker = (repeat(' ', this.startColumn - 1)) + (repeat('^', errorLength));
message += "\n" + errorLine + "\n" + marker;
} else {
void 0;
}
return message;
};
return CompilerError;
})(Error);
}).call(this);

View File

@@ -12,6 +12,10 @@
return literal === string.substr(string.length - len - (back || 0), len);
};
exports.repeat = function(string, n) {
return (Array(n + 1)).join(string);
};
exports.compact = function(array) {
var item, _i, _len, _results;
_results = [];

View File

@@ -1,12 +1,14 @@
// Generated by CoffeeScript 1.5.0
(function() {
var BOM, BOOL, CALLABLE, CODE, COFFEE_ALIASES, COFFEE_ALIAS_MAP, COFFEE_KEYWORDS, COMMENT, COMPARE, COMPOUND_ASSIGN, HEREDOC, HEREDOC_ILLEGAL, HEREDOC_INDENT, HEREGEX, HEREGEX_OMIT, IDENTIFIER, INDEXABLE, INVERSES, JSTOKEN, JS_FORBIDDEN, JS_KEYWORDS, LINE_BREAK, LINE_CONTINUER, LITERATE, LOGIC, Lexer, MATH, MULTILINER, MULTI_DENT, NOT_REGEX, NOT_SPACED_REGEX, NUMBER, OPERATOR, REGEX, RELATION, RESERVED, Rewriter, SHIFT, SIMPLESTR, STRICT_PROSCRIBED, TRAILING_SPACES, UNARY, WHITESPACE, compact, count, key, last, locationDataToString, starts, _ref, _ref1,
var BOM, BOOL, CALLABLE, CODE, COFFEE_ALIASES, COFFEE_ALIAS_MAP, COFFEE_KEYWORDS, COMMENT, COMPARE, COMPOUND_ASSIGN, CompilerError, HEREDOC, HEREDOC_ILLEGAL, HEREDOC_INDENT, HEREGEX, HEREGEX_OMIT, IDENTIFIER, INDEXABLE, INVERSES, JSTOKEN, JS_FORBIDDEN, JS_KEYWORDS, LINE_BREAK, LINE_CONTINUER, LITERATE, LOGIC, Lexer, MATH, MULTILINER, MULTI_DENT, NOT_REGEX, NOT_SPACED_REGEX, NUMBER, OPERATOR, REGEX, RELATION, RESERVED, Rewriter, SHIFT, SIMPLESTR, STRICT_PROSCRIBED, TRAILING_SPACES, UNARY, WHITESPACE, compact, count, key, last, locationDataToString, starts, _ref, _ref1,
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
_ref = require('./rewriter'), Rewriter = _ref.Rewriter, INVERSES = _ref.INVERSES;
_ref1 = require('./helpers'), count = _ref1.count, starts = _ref1.starts, compact = _ref1.compact, last = _ref1.last, locationDataToString = _ref1.locationDataToString;
CompilerError = require('./error').CompilerError;
exports.Lexer = Lexer = (function() {
function Lexer() {}
@@ -786,7 +788,7 @@
};
Lexer.prototype.error = function(message) {
throw SyntaxError("" + message + " on line " + (this.chunkLine + 1));
throw new CompilerError(message, this.chunkLine + 1, this.chunkColumn + 1);
};
return Lexer;