All files / src/walk nfaWalker.js

0% Statements 0/18
0% Branches 0/8
0% Functions 0/3
0% Lines 0/18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43                                                                                     
var simulate = require( '../core/nfa/simulate' );
var getMatch = require( '../core/nfa/getMatch' )
var compile = require( '../core/nfa/compile' );
var Problem = require( '../models/Problem' );
 
function nfaWalker( clause, walkFn ) {
  var nfa;
 
  return {
    trailblaze: nfaTrailblaze,
    reconstruct: nfaReconstruct,
  }
 
  function nfaTrailblaze( x, walkOpts ) {
 
    if ( !nfa ) {
      //lazy
      nfa = compile( clause );
    }
    var { chain, matched, lastProblem } = simulate( nfa, x, walkFn, walkOpts );
    if ( matched === true ) {
      return { chain };
    } else {
      let subproblems;
 
      if ( lastProblem ) {
        let { name, position, problem } = lastProblem;
        subproblems = { [ name ? `"${name}"` : `<At position ${position}>` ]: problem };
      } else {
        subproblems = [];
      }
      return new Problem( x, clause, subproblems, 'Clause ' + clause.type + ' did not match value' );
    }
  }
 
  function nfaReconstruct( { chain }, walkOpts ) {
    var result = getMatch( chain, walkFn, walkOpts );
    return result;
  }
}
 
module.exports = nfaWalker;