Skip to content

Commit

Permalink
Adjusted the examples for zaach#205/342 to showcase the latest kernel…
Browse files Browse the repository at this point in the history
… optimizations too: when there's no match, i.e. a parse error, then the parser still invokes `parseError()` like in the old days and has it throw an exception if you don't override it, but you can also make it return `false` to turn the parser into a true *matcher*, returning `true` or `false` whether the input parses correctly or not! See examples/issue-205-2.jison.
  • Loading branch information
GerHobbelt committed Feb 11, 2017
1 parent c843887 commit dd49f0a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
28 changes: 28 additions & 0 deletions examples/issue-205-2.jison
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ X return 'COMMON1';
/lex


%options no-default-action no-try-catch


%token PREFIX1 PREFIX2 SUFFIX1 SUFFIX2
Expand Down Expand Up @@ -70,6 +71,33 @@ parser.main = function () {
}
}
console.log("\nAnd now the failing inputs: even these deliver a result:\n");
// set up an aborting error handler which does not throw an exception
// but returns a special parse 'result' instead:
var errmsg = null;
var errReturnValue = false;
parser.yy.parseError = function (msg, hash) {
errmsg = msg;
return errReturnValue;
};
var testset_not_ok = ['a', 'b', 'aB', 'bA', '?', 'AA', 'BB', 'aa', 'bb', '?A', '?a'];
var base = i;
var rv;
for (var i = 0, len = testset_not_ok.length; i < len; i++) {
try {
rv = parser.parse(testset_not_ok[i]);
console.log("test #" + (base + i) + ": '" + testset_not_ok[i] + "' ==> ", rv);
assert.strictEqual(rv, errReturnValue);
} catch (ex) {
console.log("test #" + (base + i) + ": '" + testset_not_ok[i] + "' ==> EXCEPTION: ", ex);
throw ex;
}
}
// if you get past the assert(), you're good.
console.log("tested OK");
};
Expand Down
28 changes: 28 additions & 0 deletions examples/issue-205.jison
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ B return 'SUFFIX2';
/lex


%options no-default-action no-try-catch


%token PREFIX1 PREFIX2 SUFFIX1 SUFFIX2
Expand Down Expand Up @@ -66,6 +67,33 @@ parser.main = function () {
}
}
console.log("\nAnd now the failing inputs: even these deliver a result:\n");
// set up an aborting error handler which does not throw an exception
// but returns a special parse 'result' instead:
var errmsg = null;
var errReturnValue = '@@@';
parser.yy.parseError = function (msg, hash) {
errmsg = msg;
return errReturnValue;
};
var testset_not_ok = ['a', 'b', 'aB', 'bA', '?', 'AA', 'BB', 'aa', 'bb', '?A', '?a'];
var base = i;
var rv;
for (var i = 0, len = testset_not_ok.length; i < len; i++) {
try {
rv = parser.parse(testset_not_ok[i]);
console.log("test #" + (base + i) + ": '" + testset_not_ok[i] + "' ==> ", rv);
assert.strictEqual(rv, errReturnValue);
} catch (ex) {
console.log("test #" + (base + i) + ": '" + testset_not_ok[i] + "' ==> EXCEPTION: ", ex);
throw ex;
}
}
// if you get past the assert(), you're good.
console.log("tested OK");
};
Expand Down

0 comments on commit dd49f0a

Please sign in to comment.