Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[lib] better failing engine

  • Loading branch information...
commit 4a6dcc14b69d1b360bdb69b016838ef601535d4e 1 parent 35a2fc5
@indutny indutny authored
View
4 lib/ometajs/api.js
@@ -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
46 lib/ometajs/core.js
@@ -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,7 +316,7 @@ var OMeta = exports.OMeta = {
return ans
}
else
- throw fail
+ throw fail();
},
disableXORs: function() {
this._xor = function(ruleName) {
@@ -322,17 +324,17 @@ var OMeta = exports.OMeta = {
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)
View
78 lib/ometajs/ometa/parsers.js
@@ -78,7 +78,7 @@ var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator;
case "_":
return "_";
default:
- throw fail;
+ throw fail();
}
}.call(this);
});
@@ -161,7 +161,7 @@ var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator;
return [ "number", n ];
}.call(this);
default:
- throw fail;
+ throw fail();
}
}.call(this);
}, function() {
@@ -180,7 +180,7 @@ var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator;
case "e":
return "e";
default:
- throw fail;
+ throw fail();
}
})).call(this);
this._opt(function() {
@@ -191,7 +191,7 @@ var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator;
case "+":
return "+";
default:
- throw fail;
+ throw fail();
}
}.call(this);
});
@@ -247,7 +247,7 @@ var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator;
return this._apply("hexDigit");
}.call(this);
default:
- throw fail;
+ throw fail();
}
}.call(this);
}, function() {
@@ -295,7 +295,7 @@ var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator;
return [ "string", cs.join("") ];
}.call(this);
default:
- throw fail;
+ throw fail();
}
}.call(this);
}, function() {
@@ -334,7 +334,7 @@ var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator;
return [ "string", cs.join("") ];
}.call(this);
default:
- throw fail;
+ throw fail();
}
}.call(this);
}, function() {
@@ -346,7 +346,7 @@ var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator;
case "`":
return "`";
default:
- throw fail;
+ throw fail();
}
})).call(this);
n = this._apply("iName");
@@ -366,7 +366,7 @@ var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator;
case "=":
return "/=";
default:
- throw fail;
+ throw fail();
}
}.call(this);
}, function() {
@@ -384,14 +384,14 @@ var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator;
case "=":
return "||=";
default:
- throw fail;
+ throw fail();
}
}.call(this);
}, function() {
return "||";
});
default:
- throw fail;
+ throw fail();
}
}.call(this);
case "-":
@@ -403,7 +403,7 @@ var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator;
case "-":
return "--";
default:
- throw fail;
+ throw fail();
}
}.call(this);
}, function() {
@@ -422,14 +422,14 @@ var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator;
case "=":
return "!==";
default:
- throw fail;
+ throw fail();
}
}.call(this);
}, function() {
return "!=";
});
default:
- throw fail;
+ throw fail();
}
}.call(this);
}, function() {
@@ -444,7 +444,7 @@ var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator;
case "=":
return "%=";
default:
- throw fail;
+ throw fail();
}
}.call(this);
}, function() {
@@ -459,7 +459,7 @@ var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator;
case "+":
return "++";
default:
- throw fail;
+ throw fail();
}
}.call(this);
}, function() {
@@ -480,14 +480,14 @@ var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator;
case "=":
return "===";
default:
- throw fail;
+ throw fail();
}
}.call(this);
}, function() {
return "==";
});
default:
- throw fail;
+ throw fail();
}
}.call(this);
}, function() {
@@ -500,7 +500,7 @@ var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator;
case "=":
return "<=";
default:
- throw fail;
+ throw fail();
}
}.call(this);
}, function() {
@@ -513,7 +513,7 @@ var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator;
case "=":
return ">=";
default:
- throw fail;
+ throw fail();
}
}.call(this);
}, function() {
@@ -531,14 +531,14 @@ var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator;
case "=":
return "&&=";
default:
- throw fail;
+ throw fail();
}
}.call(this);
}, function() {
return "&&";
});
default:
- throw fail;
+ throw fail();
}
}.call(this);
case "(":
@@ -552,7 +552,7 @@ var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator;
case "=":
return "*=";
default:
- throw fail;
+ throw fail();
}
}.call(this);
}, function() {
@@ -565,7 +565,7 @@ var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator;
case "]":
return "]";
default:
- throw fail;
+ throw fail();
}
}.call(this);
return [ s, s ];
@@ -1135,7 +1135,7 @@ var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator;
case "[":
return "[";
default:
- throw fail;
+ throw fail();
}
}.call(this);
});
@@ -1157,7 +1157,7 @@ var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator;
case "*":
return "*";
default:
- throw fail;
+ throw fail();
}
}.call(this);
});
@@ -1173,7 +1173,7 @@ var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator;
case "\r":
return "\r";
default:
- throw fail;
+ throw fail();
}
}.call(this);
});
@@ -1201,7 +1201,7 @@ var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator;
case "]":
return "]";
default:
- throw fail;
+ throw fail();
}
}.call(this);
});
@@ -1242,7 +1242,7 @@ var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator;
case "\n":
return "\n";
default:
- throw fail;
+ throw fail();
}
}.call(this);
}, function() {
@@ -2372,7 +2372,7 @@ var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator;
case "_":
return "_";
default:
- throw fail;
+ throw fail();
}
}.call(this);
}, function() {
@@ -2416,7 +2416,7 @@ var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator;
return unescape("\\" + c);
}.call(this);
default:
- throw fail;
+ throw fail();
}
}.call(this);
}, function() {
@@ -2488,7 +2488,7 @@ var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator;
case "`":
return "`";
default:
- throw fail;
+ throw fail();
}
})).call(this);
return this._apply("tsName");
@@ -2538,7 +2538,7 @@ var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator;
return xs;
}.call(this);
default:
- throw fail;
+ throw fail();
}
}.call(this);
}, function() {
@@ -2796,7 +2796,7 @@ var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator;
case "?":
return [ "Opt", x ];
default:
- throw fail;
+ throw fail();
}
}.call(this);
}, function() {
@@ -2823,7 +2823,7 @@ var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator;
}.call(this);
}.call(this);
default:
- throw fail;
+ throw fail();
}
}.call(this);
}, function() {
@@ -3025,7 +3025,7 @@ var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator;
return [ this["sName"], "._superApplyWithArgs(this,", args.join(","), ")" ].join("");
}.call(this);
default:
- throw fail;
+ throw fail();
}
}.call(this);
}, function() {
@@ -3266,7 +3266,7 @@ var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator;
for (var i = 0; i < cases["length"]; i += 1) {
buf.put("case " + cases[i][0] + ":return " + cases[i][1] + ";");
}
- buf.put("default: throw fail}}).call(this)");
+ buf.put("default: throw fail()}}).call(this)");
return buf.contents();
};
var BSOMetaJSParser = exports.BSOMetaJSParser = objectThatDelegatesTo(BSJSParser, {
@@ -3574,7 +3574,7 @@ var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator;
return [ "And" ].concat(cs).concat([ [ "Act", s ] ]);
}.call(this);
default:
- throw fail;
+ throw fail();
}
}.call(this);
}, function() {
@@ -3723,7 +3723,7 @@ var BSOMetaJSTranslator = ometajs_.BSOMetaJSTranslator;
return this._applyWithArgs("exactly", "'");
}.call(this);
default:
- throw fail;
+ throw fail();
}
}.call(this);
});
View
2  src/bs-ometa-compiler.ometajs
@@ -117,7 +117,7 @@ BSOMetaTranslator.jumpTableCode = function(cases) {
buf.put("(function(){switch(this._apply('anything')){")
for (var i = 0; i < cases.length; i += 1)
buf.put("case " + cases[i][0] + ":return " + cases[i][1] + ";")
- buf.put("default: throw fail}}).call(this)")
+ buf.put("default: throw fail()}}).call(this)")
return buf.contents()
}
Please sign in to comment.
Something went wrong with that request. Please try again.