diff --git a/lib/coffee-script/coffee-script.js b/lib/coffee-script/coffee-script.js index 64684dfd..cc8929d7 100644 --- a/lib/coffee-script/coffee-script.js +++ b/lib/coffee-script/coffee-script.js @@ -1,6 +1,6 @@ // Generated by CoffeeScript 1.5.0 (function() { - var Lexer, compile, ext, extensions, fs, lexer, loadFile, parser, path, vm, _i, _len, + var CompilerError, Lexer, compile, ext, extensions, fs, lexer, loadFile, parser, path, vm, _i, _len, __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; }, __hasProp = {}.hasOwnProperty; @@ -12,6 +12,8 @@ parser = require('./parser').parser; + CompilerError = require('./error').CompilerError; + vm = require('vm'); extensions = ['.coffee', '.litcoffee']; @@ -173,4 +175,12 @@ parser.yy = require('./nodes'); + parser.yy.parseError = function(message, _arg) { + var first_column, first_line, last_column, last_line, loc, token; + loc = _arg.loc, token = _arg.token; + message = "unexpected " + token; + first_line = loc.first_line, first_column = loc.first_column, last_line = loc.last_line, last_column = loc.last_column; + throw new CompilerError(message, first_line + 1, first_column + 1, last_line + 1, last_column + 1); + }; + }).call(this); diff --git a/lib/coffee-script/error.js b/lib/coffee-script/error.js index bf2602e0..c0a64d3f 100644 --- a/lib/coffee-script/error.js +++ b/lib/coffee-script/error.js @@ -25,7 +25,7 @@ CompilerError.prototype.prettyMessage = function(fileName, code) { var errorLength, errorLine, marker, message; - message = "" + fileName + ":" + this.startLine + ":" + this.startColumn + ": " + this.message; + message = "" + fileName + ":" + this.startLine + ":" + this.startColumn + ": error: " + this.message; if (this.startLine === this.endLine) { errorLine = code.split('\n')[this.startLine - 1]; errorLength = this.endColumn - this.startColumn + 1; diff --git a/src/coffee-script.coffee b/src/coffee-script.coffee index b9ee1df2..359a86b1 100644 --- a/src/coffee-script.coffee +++ b/src/coffee-script.coffee @@ -6,11 +6,12 @@ # If included on a webpage, it will automatically sniff out, compile, and # execute all scripts present in `text/coffeescript` tags. -fs = require 'fs' -path = require 'path' -{Lexer} = require './lexer' -{parser} = require './parser' -vm = require 'vm' +fs = require 'fs' +path = require 'path' +{Lexer} = require './lexer' +{parser} = require './parser' +{CompilerError} = require './error' +vm = require 'vm' # The file extensions that are considered to be CoffeeScript. extensions = ['.coffee', '.litcoffee'] @@ -135,4 +136,13 @@ parser.lexer = upcomingInput: -> "" +# Make all the AST nodes visible to the parser. parser.yy = require './nodes' + +# Override Jison's default error handling function. +parser.yy.parseError = (message, {loc, token}) -> + # Disregard Jison's message, it contains redundant line numer information. + message = "unexpected #{token}" + + {first_line, first_column, last_line, last_column} = loc + throw new CompilerError message, first_line + 1, first_column + 1, last_line + 1, last_column + 1 \ No newline at end of file diff --git a/src/error.coffee b/src/error.coffee index 1d131606..9fde0033 100644 --- a/src/error.coffee +++ b/src/error.coffee @@ -15,7 +15,7 @@ exports.CompilerError = class CompilerError extends Error # showing where the error is. # TODO: tests prettyMessage: (fileName, code) -> - message = "#{fileName}:#{@startLine}:#{@startColumn}: #{@message}" + message = "#{fileName}:#{@startLine}:#{@startColumn}: error: #{@message}" if @startLine is @endLine errorLine = code.split('\n')[@startLine - 1] errorLength = @endColumn - @startColumn + 1