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

Improved parser error messages

This commit is contained in:
Demian Ferreiro 2013-02-25 15:09:42 -03:00
parent 25091fb2a0
commit caacd892cc
4 changed files with 28 additions and 8 deletions

View file

@ -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);

View file

@ -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;

View file

@ -10,6 +10,7 @@ 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.
@ -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

View file

@ -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