Source: BNFP/Rule.js

"use strict";

/** @fileOverview Implements `class page.BNFP.Rule extends page.BNF.Rule`.
  * @author Axel T. Schreiner <ats@cs.rit.edu>
  * @version 1.5.1
  */

/** Creates a new BNFP rule.
  * @class Represents a BNFP rule.
  * @extends page.BNF.Rule
  * @private
  *
  * @param {page.BNF.NT} _nt  left-hand side, non-terminal.
  * @param {Node[]} _symbols right-hand side.
  * @param {{level:number, assoc:string, t:page.BNF.T}|boolean} [_prec] precedence or `false`.
  * 
  * @property {Object|boolean} prec precedence or `false`.
  * @property {number} prec.level precedence level, from 0.
  * @property {string} prec.assoc associativity, `'left'`, `'right'` or `'nonassoc'`.
  * @property {page.BNF.T} prec.t terminal providing the precedence.
  * @property {boolean} prec.explicit `true` if the precedence was explicitly assigned.
  */
page.BNFP.Rule = function (_nt, _symbols, _prec) {
  page.BNF.Rule.call(this, _nt, _symbols);
  this.prec = _prec;
};

page.subclass(page.BNFP.Rule, 'page.BNFP.Rule', page.BNF.Rule);

/** Displays a rule in BNF notation.
  * @override
  * @variation BNFP
  *
  * @param {number} [_mark] precedes a symbol on the right-hand side if it is in range.
  * 
  * @returns {string}
  */
page.BNFP.Rule.prototype.toString = function (_mark) {
  var result = page.BNF.Rule.prototype.toString.call(this, _mark);
  return this.prec && this.prec.explicit && typeof _mark != 'number' ? result.replace(/.$/, ' %prec ' + this.prec.t + ';') : result;
};