Source: BNF/NT.js

"use strict";

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

/** Creates a new non-terminal representation for BNF.
  * @class Represents a (unique!) non-terminal for BNF.
  * @private
  *
  * @param {string} _name  non-terminal's name.
  * @param {number} _index non-terminal's index in {@link page.BNF}`.nts`.
  *
  * @property {string} name non-terminal's name.
  * @property {number} index non-terminal's index in {@link page.BNF}`.nts`;
  *   transient, maintained by {@link page.BNF#nt}.
  * @property {number} ord non-terminal's global index;
  *   transient, set in {@link page.BNF#init}.
  * @property {page.BNF.Rule[]} rules defining `this`;
  *   transient, maintained when a rule is constructed.
  * @property {boolean} empty true if no input can be accepted.
  * @property {boolean} reached true if this can be reached
  *   from rule zero, transient.
  * @property {boolean} finite true if there is a non-recursive expansion,
  *   transient.
  * @property {Map} first terminals at front, maps ord to {@link page.BNF.T}.
  * @property {Map} follow terminals following, maps ord to {@link page.BNF.T}.
  */
page.BNF.NT = function (_name, _index) {
  this.name = _name;
  this.index = _index;
  this.ord = undefined;
  this.rules = [];
  this.empty = false;
  this.reached = false;
  this.finite = false;
  this.first = {};
  this.follow = {};
};

page.baseclass(page.BNF.NT, 'page.BNF.NT');

/** Displays name.
  * @returns {string}
  */
page.BNF.NT.prototype.toString = function () {
  return this.name;
};
  
/** Displays index or ord and name and contents of all sets.
  * @returns {string}
  */
page.BNF.NT.prototype.dump = function () {
  var result = (' ' + (this.ord ? this.ord : this.index)).substr(-2) + ': ' + this.toString();

  if (this.empty)
    result += '\n\tempty: true';
    
  for (var set in { first:0, follow:0 }) {
    var r = '';
    for (var ord in this[set]) r += ' ' + this[set][ord];
    if (r.length) result += '\n\t' + set + ':' + r;
  }

  return result;
};