All files / src/walk nfaWalker.ts

100% Statements 26/26
87.5% Branches 7/8
100% Functions 3/3
100% Lines 21/21
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  15x 15x 15x 15x 15x     2299x         1530x   1530x   1530x 1530x 1444x     86x 86x 73x 73x     13x   86x         1437x 1437x 1437x     15x  
import simulate from "../core/nfa/simulate";
import getMatch from "../core/nfa/getMatch";
import compile from "../core/nfa/compile";
import simuate from "../core/nfa/simulate";
 
import Problem from "../models/Problem";
 
export default function nfaWalker( clause, walkFn ) {
  var nfa;
 
  return {
    trailblaze: nfaTrailblaze,
    reconstruct: nfaReconstruct,
  }E
 
  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;
  }
}