lib/parser.js
function swap(states) {
  let newStates = Array.apply(null, Array(states.length)).map(() => []);

  states.forEach((state, i) => {
    state.forEach((earleyItem) => {
      newStates[earleyItem.origin].push(earleyItem);
      earleyItem.origin = i;
    });
  });
  return newStates;
}
function swap(states) {
  let newStates = Array.apply(null, Array(states.length)).map(() => []);

  states.forEach((state, i) => {
    state.forEach((earleyItem) => {
      newStates[earleyItem.origin].push(earleyItem);
      earleyItem.origin = i;
    });
  });
  return newStates;
}
function swap ( states ) { let newStates = Array . apply ( null , Array ( states . length ) ) . map ( ( ) => [ ] ) ; states . forEach ( ( state , i ) => { state . forEach ( ( earleyItem ) => { newStates [ earleyItem . origin ] . push ( earleyItem ) ; earleyItem . origin = i ; } ) ; } ) ; return newStates ; }
Places earley items in the states in which they originated, as opposed to the
states in which they finished parsing, and set their `origin` properties to
the state in which they finished.

This allows a depth-first search of the chart to move forwards through the
graph, which is more intuitive than having to move backwards

@function swap
@param {state[]} states - Set of lists of earley items
@return {state[]} Set of lists of earley items, but each item now exists in
the state at which it originated, and the <code>origin</code> property of
each item points to the state at which the parse completed
Places earley items in the states in which they originated as opposed to the states in which they finished parsing and set their origin properties to the state in which they finished .