diff --git a/lib/nearley.js b/lib/nearley.js index 05d97fdf..7878dc91 100644 --- a/lib/nearley.js +++ b/lib/nearley.js @@ -177,6 +177,12 @@ Grammar.fromCompiled = function(rules, start) { } +function defaultMatcher(expect, token) { + // either regex or literal + return expect.test ? expect.test(token) : expect.literal === token +} + + function Parser(rules, start, options) { if (rules instanceof Grammar) { var grammar = rules; @@ -187,14 +193,10 @@ function Parser(rules, start, options) { this.grammar = grammar; // Read options - this.options = { + this.options = Object.assign({ keepHistory: false, - // rewindable: false, - }; - for (var key in (options || {})) { - this.options[key] = options[key]; - } - // if (this.options.rewindable) { this.options.keepHistory = true; } + matcher: defaultMatcher, + }, options); // Setup a table var column = new Column(grammar, 0); @@ -212,6 +214,7 @@ function Parser(rules, start, options) { Parser.fail = {}; Parser.prototype.feed = function(chunk) { + var matcher = this.options.matcher; for (var chunkPos = 0; chunkPos < chunk.length; chunkPos++) { // We add new states to table[current+1] var column = this.table[this.current + chunkPos]; @@ -234,8 +237,7 @@ Parser.prototype.feed = function(chunk) { var state = scannable[w]; var expect = state.rule.symbols[state.dot]; // Try to consume the token - // either regex or literal - if (expect.test ? expect.test(token) : expect.literal === token) { + if (matcher(expect, token)) { // Add it var next = state.nextState(token); nextColumn.states.push(next);