Source: SLR1/Config.js

"use strict";

/** @fileOverview Implements `class page.SLR1.Config`.
  * @author Axel T. Schreiner <ats@cs.rit.edu>
  * @version 1.5.0
  */

/** Creates a new representation for a marked rule.
  * @class Represents a mark in a rule.
  * @private
  *
  * @param {page.BNF.Rule} _rule rule to mark.
  * @param {number} _position position in rule, before a symbol or after all.
  *
  * @property {page.BNF.Rule} rule rule to mark.
  * @property {number} position position in rule, before a symbol or after all.
  * @property {boolean} complete true if position is after all symbols.
  */
page.SLR1.Config = function (_rule, _position) {
  this.rule = _rule;
  this.position = _position;

  this.complete = _position == this.rule.symbols.length;
};

page.baseclass(page.SLR1.Config, 'page.SLR1.Config');

/** Displays the marked rule.
  * @returns {string}
  */
page.SLR1.Config.prototype.toString = function () {
  return this.rule.toString(this.position);
};

/** Compares two configurations.
  *
  * @param {page.SLR1.Config} _c to compare to <tt>this</tt>.
  *
  * @returns true same rule and same position.
  */
page.SLR1.Config.prototype.equals = function (_c) {
  this.assert('__WHERE__', _c instanceof page.SLR1.Config);

  return this.rule === _c.rule && this.position == _c.position;
};

/** Advances the mark.
  *
  * @param {page.SLR1} _factory the keeper of the state table, factory for new configurations.
  *
  * @returns {page.SLR1.Config} a new configuration with the mark moved right;
  *   the actual class depends on the factory.
  */
page.SLR1.Config.prototype.advance = function (_factory) {
  this.assert('__WHERE__', _factory instanceof page.SLR1);
  this.assert('__WHERE__', this.position < this.rule.symbols.length);

  return _factory.config(this.rule, this.position + 1);
};