Skip to content

Commit

Permalink
Make token matching semantics user-definable
Browse files Browse the repository at this point in the history
  • Loading branch information
tjvr committed Mar 10, 2017
1 parent f1b5345 commit a5df448
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions lib/nearley.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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];
Expand All @@ -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);
Expand Down

0 comments on commit a5df448

Please sign in to comment.