Permalink
Browse files

[lib] better failing engine

  • Loading branch information...
1 parent 35a2fc5 commit 4a6dcc14b69d1b360bdb69b016838ef601535d4e @indutny indutny committed Nov 23, 2011
Showing with 66 additions and 64 deletions.
  1. +2 −2 lib/ometajs/api.js
  2. +24 −22 lib/ometajs/core.js
  3. +39 −39 lib/ometajs/ometa/parsers.js
  4. +1 −1 src/bs-ometa-compiler.ometajs
View
@@ -12,7 +12,7 @@ var ometajs = require('../ometajs'),
// #### @i {Number}
//
function compilationError(m, i) {
- throw objectThatDelegatesTo(fail, {errorPos: i});
+ throw objectThatDelegatesTo(fail(), {errorPos: i});
};
//
@@ -21,7 +21,7 @@ function compilationError(m, i) {
// #### @i {Number}
//
function translationError(m, i) {
- throw fail;
+ throw fail();
};
//
View
@@ -43,7 +43,9 @@ var ometajs = require('../ometajs'),
//
// failure exception
//
-var fail = exports.fail = { toString: function () { return 'match failed' } };
+var fail = exports.fail = function () {
+ return new SyntaxError('match failed');
+};
//
// ### function OMInputStream(hd, tl)
@@ -109,13 +111,13 @@ OMInputStreamEnd.prototype = objectThatDelegatesTo(OMInputStream.prototype);
// ### function head ()
// Not implemented
//
-OMInputStreamEnd.prototype.head = function() { throw fail };
+OMInputStreamEnd.prototype.head = function() { throw fail() };
//
// ### function tail ()
// Not implemented
//
-OMInputStreamEnd.prototype.tail = function() { throw fail };
+OMInputStreamEnd.prototype.tail = function() { throw fail() };
//
// ### function ListOMInputStream (lst, idx)
@@ -205,12 +207,12 @@ var OMeta = exports.OMeta = {
this.input = origInput
var ans = this[rule].call(this)
if (this.input == sentinel)
- throw fail
+ throw fail()
memoRec.ans = ans
memoRec.nextInput = this.input
}
catch (f) {
- if (f != fail)
+ if (!(f instanceof SyntaxError))
throw f
break
}
@@ -219,7 +221,7 @@ var OMeta = exports.OMeta = {
}
else if (memoRec instanceof Failer) {
memoRec.used = true
- throw fail
+ throw fail()
}
this.input = memoRec.nextInput
return memoRec.ans
@@ -264,18 +266,18 @@ var OMeta = exports.OMeta = {
_pred: function(b) {
if (b) return true;
- throw fail;
+ throw fail();
},
_not: function(x) {
var origInput = this.input
try { x.call(this) }
catch (f) {
- if (f != fail)
+ if (!(f instanceof SyntaxError))
throw f
this.input = origInput
return true
}
- throw fail
+ throw fail();
},
_lookahead: function(x) {
var origInput = this.input,
@@ -288,10 +290,10 @@ var OMeta = exports.OMeta = {
for (var idx = 0; idx < arguments.length; idx++)
try { this.input = origInput; return arguments[idx].call(this) }
catch (f) {
- if (f != fail)
+ if (!(f instanceof SyntaxError))
throw f
}
- throw fail
+ throw fail();
},
_xor: function(ruleName) {
var origInput = this.input, idx = 1, newInput, ans
@@ -304,7 +306,7 @@ var OMeta = exports.OMeta = {
newInput = this.input
}
catch (f) {
- if (f != fail)
+ if (!(f instanceof SyntaxError))
throw f
}
idx++
@@ -314,25 +316,25 @@ var OMeta = exports.OMeta = {
return ans
}
else
- throw fail
+ throw fail();
},
disableXORs: function() {
this._xor = function(ruleName) {
var origInput = this.input
for (var idx = 1; idx < arguments.length; idx++)
try { this.input = origInput; return arguments[idx].call(this) }
catch (f) {
- if (f != fail)
+ if (!(f instanceof SyntaxError))
throw f
}
- throw fail
+ throw fail()
}
},
_opt: function(x) {
var origInput = this.input, ans
try { ans = x.call(this) }
catch (f) {
- if (f != fail)
+ if (!(f instanceof SyntaxError))
throw f
this.input = origInput
}
@@ -344,7 +346,7 @@ var OMeta = exports.OMeta = {
var origInput = this.input
try { ans.push(x.call(this)) }
catch (f) {
- if (f != fail)
+ if (!(f instanceof SyntaxError))
throw f
this.input = origInput
break
@@ -356,7 +358,7 @@ var OMeta = exports.OMeta = {
_form: function(x) {
var v = this._apply("anything")
if (!isSequenceable(v))
- throw fail
+ throw fail()
var origInput = this.input
this.input = makeListOMInputStream(v, 0);
var r = x.call(this)
@@ -395,7 +397,7 @@ var OMeta = exports.OMeta = {
break
}
catch (f) {
- if (f != fail)
+ if (!(f instanceof SyntaxError))
throw f
// if this (failed) part's mode is "1" or "+", we're not done yet
allDone = allDone && (arguments[idx] == "*" || arguments[idx] == "?")
@@ -406,7 +408,7 @@ var OMeta = exports.OMeta = {
if (allDone)
return ans
else
- throw fail
+ throw fail()
}
}
},
@@ -443,7 +445,7 @@ var OMeta = exports.OMeta = {
var wanted = this._apply("anything")
if (wanted === this._apply("anything"))
return wanted
- throw fail
+ throw fail()
},
"true": function() {
var r = this._apply("anything")
@@ -566,7 +568,7 @@ var OMeta = exports.OMeta = {
m.initialize()
try { return realArgs.length == 1 ? m._apply.call(m, realArgs[0]) : m._applyWithArgs.apply(m, realArgs) }
catch (f) {
- if (f == fail && matchFailed != undefined) {
+ if (f instanceof SyntaxError && matchFailed != undefined) {
var input = m.input
if (input.idx != undefined) {
while (input.tl != undefined && input.tl.idx != undefined)
Oops, something went wrong.

0 comments on commit 4a6dcc1

Please sign in to comment.