1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

Add CompilationError.fromLocationData

This commit is contained in:
Demian Ferreiro 2013-02-26 23:03:33 -03:00
parent 1db89d1589
commit 5115fcb162
7 changed files with 16 additions and 12 deletions

View file

@ -169,11 +169,10 @@
parser.yy = require('./nodes'); parser.yy = require('./nodes');
parser.yy.parseError = function(message, _arg) { parser.yy.parseError = function(message, _arg) {
var first_column, first_line, last_column, last_line, token, _ref; var token;
token = _arg.token; token = _arg.token;
message = "unexpected " + token; message = "unexpected " + token;
_ref = parser.lexer.yylloc, first_line = _ref.first_line, first_column = _ref.first_column, last_line = _ref.last_line, last_column = _ref.last_column; throw CompilerError.fromLocationData(message, parser.lexer.yylloc);
throw new CompilerError(message, first_line, first_column, last_line, last_column);
}; };
}).call(this); }).call(this);

View file

@ -23,6 +23,12 @@
} }
} }
CompilerError.fromLocationData = function(message, _arg) {
var first_column, first_line, last_column, last_line;
first_line = _arg.first_line, first_column = _arg.first_column, last_line = _arg.last_line, last_column = _arg.last_column;
return new CompilerError(message, first_line, first_column, last_line, last_column);
};
CompilerError.prototype.prettyMessage = function(fileName, code) { CompilerError.prototype.prettyMessage = function(fileName, code) {
var errorLength, errorLine, marker, message; var errorLength, errorLine, marker, message;
message = "" + fileName + ":" + (this.startLine + 1) + ":" + (this.startColumn + 1) + ": error: " + this.message; message = "" + fileName + ":" + (this.startLine + 1) + ":" + (this.startColumn + 1) + ": error: " + this.message;

View file

@ -199,9 +199,7 @@
}; };
Base.prototype.error = function(message) { Base.prototype.error = function(message) {
var first_column, first_line, last_column, last_line, _ref2; throw CompilerError.fromLocationData(message, this.locationData);
_ref2 = this.locationData, first_line = _ref2.first_line, first_column = _ref2.first_column, last_line = _ref2.last_line, last_column = _ref2.last_column;
throw new CompilerError(message, first_line, first_column, last_line, last_column);
}; };
return Base; return Base;

View file

@ -140,10 +140,8 @@ parser.yy = require './nodes'
parser.yy.parseError = (message, {token}) -> parser.yy.parseError = (message, {token}) ->
# Disregard Jison's message, it contains redundant line numer information. # Disregard Jison's message, it contains redundant line numer information.
message = "unexpected #{token}" message = "unexpected #{token}"
# The second argument has a `loc` property, which should have the location # The second argument has a `loc` property, which should have the location
# data for this token. Unfortunately, Jison seems to send an outdated `loc` # data for this token. Unfortunately, Jison seems to send an outdated `loc`
# (from the previous token), so we take the location information directly # (from the previous token), so we take the location information directly
# from the lexer. # from the lexer.
{first_line, first_column, last_line, last_column} = parser.lexer.yylloc throw CompilerError.fromLocationData message, parser.lexer.yylloc
throw new CompilerError message, first_line, first_column, last_line, last_column

View file

@ -10,6 +10,10 @@ exports.CompilerError = class CompilerError extends Error
# Add a stack trace in V8. # Add a stack trace in V8.
Error.captureStackTrace? @, CompilerError Error.captureStackTrace? @, CompilerError
# Creates a CompilerError from a given locationData.
@fromLocationData = (message, {first_line, first_column, last_line, last_column}) ->
new CompilerError message, first_line, first_column, last_line, last_column
# Creates a nice error message like, following the "standard" format # Creates a nice error message like, following the "standard" format
# <filename>:<line>:<col>: <message> plus the line with the error and a marker # <filename>:<line>:<col>: <message> plus the line with the error and a marker
# showing where the error is. # showing where the error is.

View file

@ -13,7 +13,7 @@ exports.ends = (string, literal, back) ->
# Repeat a string `n` times. # Repeat a string `n` times.
exports.repeat = (str, n) -> exports.repeat = (str, n) ->
# Use clever algorithm to have O(lon(n)) string concatenation operations # Use clever algorithm to have O(log(n)) string concatenation operations.
res = '' res = ''
while n > 0 while n > 0
res += str if n & 1 res += str if n & 1

View file

@ -163,8 +163,7 @@ exports.Base = class Base
# Throw a compiler error associated with this node's location. # Throw a compiler error associated with this node's location.
error: (message) -> error: (message) ->
{first_line, first_column, last_line, last_column} = @locationData throw CompilerError.fromLocationData message, @locationData
throw new CompilerError message, first_line, first_column, last_line, last_column
#### Block #### Block