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:
parent
25091fb2a0
commit
caacd892cc
4 changed files with 28 additions and 8 deletions
|
@ -1,6 +1,6 @@
|
||||||
// Generated by CoffeeScript 1.5.0
|
// Generated by CoffeeScript 1.5.0
|
||||||
(function() {
|
(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; },
|
__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;
|
__hasProp = {}.hasOwnProperty;
|
||||||
|
|
||||||
|
@ -12,6 +12,8 @@
|
||||||
|
|
||||||
parser = require('./parser').parser;
|
parser = require('./parser').parser;
|
||||||
|
|
||||||
|
CompilerError = require('./error').CompilerError;
|
||||||
|
|
||||||
vm = require('vm');
|
vm = require('vm');
|
||||||
|
|
||||||
extensions = ['.coffee', '.litcoffee'];
|
extensions = ['.coffee', '.litcoffee'];
|
||||||
|
@ -173,4 +175,12 @@
|
||||||
|
|
||||||
parser.yy = require('./nodes');
|
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);
|
}).call(this);
|
||||||
|
|
|
@ -25,7 +25,7 @@
|
||||||
|
|
||||||
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 + ":" + this.startColumn + ": " + this.message;
|
message = "" + fileName + ":" + this.startLine + ":" + this.startColumn + ": error: " + this.message;
|
||||||
if (this.startLine === this.endLine) {
|
if (this.startLine === this.endLine) {
|
||||||
errorLine = code.split('\n')[this.startLine - 1];
|
errorLine = code.split('\n')[this.startLine - 1];
|
||||||
errorLength = this.endColumn - this.startColumn + 1;
|
errorLength = this.endColumn - this.startColumn + 1;
|
||||||
|
|
|
@ -6,11 +6,12 @@
|
||||||
# If included on a webpage, it will automatically sniff out, compile, and
|
# If included on a webpage, it will automatically sniff out, compile, and
|
||||||
# execute all scripts present in `text/coffeescript` tags.
|
# execute all scripts present in `text/coffeescript` tags.
|
||||||
|
|
||||||
fs = require 'fs'
|
fs = require 'fs'
|
||||||
path = require 'path'
|
path = require 'path'
|
||||||
{Lexer} = require './lexer'
|
{Lexer} = require './lexer'
|
||||||
{parser} = require './parser'
|
{parser} = require './parser'
|
||||||
vm = require 'vm'
|
{CompilerError} = require './error'
|
||||||
|
vm = require 'vm'
|
||||||
|
|
||||||
# The file extensions that are considered to be CoffeeScript.
|
# The file extensions that are considered to be CoffeeScript.
|
||||||
extensions = ['.coffee', '.litcoffee']
|
extensions = ['.coffee', '.litcoffee']
|
||||||
|
@ -135,4 +136,13 @@ parser.lexer =
|
||||||
upcomingInput: ->
|
upcomingInput: ->
|
||||||
""
|
""
|
||||||
|
|
||||||
|
# Make all the AST nodes visible to the parser.
|
||||||
parser.yy = require './nodes'
|
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
|
|
@ -15,7 +15,7 @@ exports.CompilerError = class CompilerError extends Error
|
||||||
# showing where the error is.
|
# showing where the error is.
|
||||||
# TODO: tests
|
# TODO: tests
|
||||||
prettyMessage: (fileName, code) ->
|
prettyMessage: (fileName, code) ->
|
||||||
message = "#{fileName}:#{@startLine}:#{@startColumn}: #{@message}"
|
message = "#{fileName}:#{@startLine}:#{@startColumn}: error: #{@message}"
|
||||||
if @startLine is @endLine
|
if @startLine is @endLine
|
||||||
errorLine = code.split('\n')[@startLine - 1]
|
errorLine = code.split('\n')[@startLine - 1]
|
||||||
errorLength = @endColumn - @startColumn + 1
|
errorLength = @endColumn - @startColumn + 1
|
||||||
|
|
Loading…
Reference in a new issue