Skip to content

Commit

Permalink
Experimental fix for ohmjs#21
Browse files Browse the repository at this point in the history
  • Loading branch information
njjewers committed Nov 13, 2016
1 parent ee7b930 commit 6b46617
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 24 deletions.
10 changes: 5 additions & 5 deletions dist/ohm.min.js

Large diffs are not rendered by default.

15 changes: 1 addition & 14 deletions src/MatchResult.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,6 @@ var nodes = require('./nodes');
var util = require('./util');
var Interval = require('./Interval');

// --------------------------------------------------------------------
// Private stuff
// --------------------------------------------------------------------

// Create a short error message for an error that occurred during matching.
function getShortMatchErrorMessage(pos, source, detail) {
var errorInfo = util.getLineAndColumn(source, pos);
return 'Line ' + errorInfo.lineNum + ', col ' + errorInfo.colNum + ': ' + detail;
}

// ----------------- MatchFailure -----------------

function MatchResult(state) {
Expand Down Expand Up @@ -147,10 +137,7 @@ function MatchFailure(state) {
return 'match failed at position ' + this.getRightmostFailurePosition();
}
var detail = 'expected ' + this.getExpectedText();
return getShortMatchErrorMessage(
this.getRightmostFailurePosition(),
this.state.inputStream.source,
detail);
return util.getShortLineAndColumnMessage(this.state.inputStream.source, this.getRightmostFailurePosition()) + detail;
});
}
inherits(MatchFailure, MatchResult);
Expand Down
38 changes: 34 additions & 4 deletions src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// --------------------------------------------------------------------

var Namespace = require('./Namespace');
var util = require('./util');

// --------------------------------------------------------------------
// Private stuff
Expand Down Expand Up @@ -34,11 +35,40 @@ function intervalSourcesDontMatch() {

function grammarSyntaxError(matchFailure) {
var e = new Error();
Object.defineProperty(e, 'message', {get: function() { return matchFailure.message; }});
Object.defineProperty(e, 'shortMessage', {get: function() {
return 'Expected ' + matchFailure.getExpectedText();
}});

e.interval = matchFailure.getInterval();

var message;
var shortMessage;

var source = matchFailure.state.inputStream.source;

// Iterate through the failures, in an attempt to produce a more relevant error message
var reason = null;

var failures = matchFailure.getRightmostFailures();
for(var i = 0; i < failures.length; i++) {
var failure = failures[i];
if (failure.getPExpr().ruleName === 'escapeChar'){
// Failure to match an escape sequence, therefore the grammar contains an invalid escape sequence.
var escapeSequence = source.substr(matchFailure.getRightmostFailurePosition(),2);
var reason = "Invalid escape sequence \"" + escapeSequence + "\"";
break;
}
}

if (reason) {
// Use the more relevant reason an the error message
message = util.getLineAndColumnMessage(source, matchFailure.getRightmostFailurePosition()) + reason;
shortMessage = util.getShortLineAndColumnMessage(source, matchFailure.getRightmostFailurePosition()) + reason;
} else {
// Use the default parse failure message for the error. This is usually sufficient.
message = matchFailure.message;
shortMessage = 'Expected ' + matchFailure.getExpectedText();
}

Object.defineProperty(e, 'message', {get: function() { return message; }});
Object.defineProperty(e, 'shortMessage', {get: function() { return shortMessage; }});
return e;
}

Expand Down
5 changes: 5 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,8 @@ exports.getLineAndColumnMessage = function(str, offset /* ...ranges */) {
}
return sb.contents();
};

exports.getShortLineAndColumnMessage = function(str, offset){
var errorInfo = exports.getLineAndColumn(str, offset);
return 'Line ' + errorInfo.lineNum + ', col ' + errorInfo.colNum + ': ';
}
9 changes: 8 additions & 1 deletion visualizer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
var grammar;
var err;

try {
ohm.grammar(source);
} catch (ex) {
err = ex;
}

/*
if (matchResult.succeeded()) {
var ns = {};
try {
Expand All @@ -43,7 +50,7 @@
shortMessage: matchResult.shortMessage,
interval: matchResult.getInterval()
};
}
}*/
return {
matchResult: matchResult,
grammar: grammar,
Expand Down

0 comments on commit 6b46617

Please sign in to comment.