UNPKG

ohm-js

Version:

An object-oriented language for parsing and pattern matching

77 lines (61 loc) 2.04 kB
import {abstract} from './common.js'; import * as pexprs from './pexprs-main.js'; // -------------------------------------------------------------------- // Operations // -------------------------------------------------------------------- /* e1.toString() === e2.toString() ==> e1 and e2 are semantically equivalent. Note that this is not an iff (<==>): e.g., (~"b" "a").toString() !== ("a").toString(), even though ~"b" "a" and "a" are interchangeable in any grammar, both in terms of the languages they accept and their arities. */ pexprs.PExpr.prototype.toString = abstract('toString'); pexprs.any.toString = function() { return 'any'; }; pexprs.end.toString = function() { return 'end'; }; pexprs.Terminal.prototype.toString = function() { return JSON.stringify(this.obj); }; pexprs.Range.prototype.toString = function() { return JSON.stringify(this.from) + '..' + JSON.stringify(this.to); }; pexprs.Param.prototype.toString = function() { return '$' + this.index; }; pexprs.Lex.prototype.toString = function() { return '#(' + this.expr.toString() + ')'; }; pexprs.Alt.prototype.toString = function() { return this.terms.length === 1 ? this.terms[0].toString() : '(' + this.terms.map(term => term.toString()).join(' | ') + ')'; }; pexprs.Seq.prototype.toString = function() { return this.factors.length === 1 ? this.factors[0].toString() : '(' + this.factors.map(factor => factor.toString()).join(' ') + ')'; }; pexprs.Iter.prototype.toString = function() { return this.expr + this.operator; }; pexprs.Not.prototype.toString = function() { return '~' + this.expr; }; pexprs.Lookahead.prototype.toString = function() { return '&' + this.expr; }; pexprs.Apply.prototype.toString = function() { if (this.args.length > 0) { const ps = this.args.map(arg => arg.toString()); return this.ruleName + '<' + ps.join(',') + '>'; } else { return this.ruleName; } }; pexprs.UnicodeChar.prototype.toString = function() { return '\\p{' + this.category + '}'; };