UNPKG

ohm-js

Version:

An object-oriented language for parsing and pattern matching

71 lines (60 loc) 2.5 kB
import {abstract} from './common.js'; import * as errors from './errors.js'; import * as pexprs from './pexprs-main.js'; // -------------------------------------------------------------------- // Operations // -------------------------------------------------------------------- pexprs.PExpr.prototype.assertChoicesHaveUniformArity = abstract( 'assertChoicesHaveUniformArity', ); pexprs.any.assertChoicesHaveUniformArity = pexprs.end.assertChoicesHaveUniformArity = pexprs.Terminal.prototype.assertChoicesHaveUniformArity = pexprs.Range.prototype.assertChoicesHaveUniformArity = pexprs.Param.prototype.assertChoicesHaveUniformArity = pexprs.Lex.prototype.assertChoicesHaveUniformArity = pexprs.UnicodeChar.prototype.assertChoicesHaveUniformArity = function(ruleName) { // no-op }; pexprs.Alt.prototype.assertChoicesHaveUniformArity = function(ruleName) { if (this.terms.length === 0) { return; } const arity = this.terms[0].getArity(); for (let idx = 0; idx < this.terms.length; idx++) { const term = this.terms[idx]; term.assertChoicesHaveUniformArity(); const otherArity = term.getArity(); if (arity !== otherArity) { throw errors.inconsistentArity(ruleName, arity, otherArity, term); } } }; pexprs.Extend.prototype.assertChoicesHaveUniformArity = function(ruleName) { // Extend is a special case of Alt that's guaranteed to have exactly two // cases: [extensions, origBody]. const actualArity = this.terms[0].getArity(); const expectedArity = this.terms[1].getArity(); if (actualArity !== expectedArity) { throw errors.inconsistentArity(ruleName, expectedArity, actualArity, this.terms[0]); } }; pexprs.Seq.prototype.assertChoicesHaveUniformArity = function(ruleName) { for (let idx = 0; idx < this.factors.length; idx++) { this.factors[idx].assertChoicesHaveUniformArity(ruleName); } }; pexprs.Iter.prototype.assertChoicesHaveUniformArity = function(ruleName) { this.expr.assertChoicesHaveUniformArity(ruleName); }; pexprs.Not.prototype.assertChoicesHaveUniformArity = function(ruleName) { // no-op (not required b/c the nested expr doesn't show up in the CST) }; pexprs.Lookahead.prototype.assertChoicesHaveUniformArity = function(ruleName) { this.expr.assertChoicesHaveUniformArity(ruleName); }; pexprs.Apply.prototype.assertChoicesHaveUniformArity = function(ruleName) { // The arities of the parameter expressions is required to be 1 by // `assertAllApplicationsAreValid()`. };