lib/parser.js
function earley(tokens, grammar) {
  let states  = Array.apply(null, Array(tokens.length + 1)).map(() => []);
  var i, j;

  let rulePairs = grammar.map((rule) => ({
    name    : rule.lhs.name,
    rule    : rule,
    position: 0,
    origin  : 0
  }));

  [].push.apply(states[0], rulePairs);

  for (i = 0; i <= tokens.length; i += 1) {
    for (j = 0; j < states[i].length; j += 1) {
      predict(tokens, states, i, j, grammar);
      scan(tokens, states, i, j);
      complete(tokens, states, i, j);
    }
  }

  return swap(removeUnfinishedItems(states));
}
function earley(tokens, grammar) {
  let states  = Array.apply(null, Array(tokens.length + 1)).map(() => []);
  var i, j;

  let rulePairs = grammar.map((rule) => ({
    name    : rule.lhs.name,
    rule    : rule,
    position: 0,
    origin  : 0
  }));

  [].push.apply(states[0], rulePairs);

  for (i = 0; i <= tokens.length; i += 1) {
    for (j = 0; j < states[i].length; j += 1) {
      predict(tokens, states, i, j, grammar);
      scan(tokens, states, i, j);
      complete(tokens, states, i, j);
    }
  }

  return swap(removeUnfinishedItems(states));
}
function earley ( tokens , grammar ) { let states = Array . apply ( null , Array ( tokens . length + 1 ) ) . map ( ( ) => [ ] ) ; var i , j ; let rulePairs = grammar . map ( ( rule ) => ( { name : rule . lhs . name , rule : rule , position : 0 , origin : 0 } ) ) ; [ ] . push . apply ( states [ 0 ] , rulePairs ) ; for ( i = 0 ; i <= tokens . length ; i += 1 ) { for ( j = 0 ; j < states [ i ] . length ; j += 1 ) { predict ( tokens , states , i , j , grammar ) ; scan ( tokens , states , i , j ) ; complete ( tokens , states , i , j ) ; } } return swap ( removeUnfinishedItems ( states ) ) ; }
Parses the input tokens using the earley top-down chart parsing algorithm
to product a set of states, each containing a list of earley items

@function earley
@memberof module:lib/parser
@param {string[]} tokens - Sequence of symbols to be parsed
@param {Rule[]|CFG} grammar - Set of rules that define a language
@return {state[]} Set of 'states', each of which contains a list of earley
items. Each earley item looks something like this:
<pre><code>
{
name: [string],
rule: [Rule],
position: [number],
origin: [number]
}
</code></pre>
An earley item represents a completed parse of some individual rule. The
position should be equivalent to rule.length, and the origin, despite its
name, describes the state at which parse finished.

This means that an earley item <i>should</i> exist in state 0 with an
origin equivalent to the number of tokens passed in to indicate that the
entire input was parsed successfully for some rule
Parses the input tokens using the earley top - down chart parsing algorithm to product a set of states each containing a list of earley items