Skip to main content
Module

x/ohm_js/src/pexprs-main.js

A library and language for building parsers, interpreters, compilers, etc.
Go to Latest
File
'use strict';
// --------------------------------------------------------------------// Imports// --------------------------------------------------------------------
const UnicodeCategories = require('../third_party/UnicodeCategories');const common = require('./common');
// --------------------------------------------------------------------// Private stuff// --------------------------------------------------------------------
// General stuff
class PExpr { constructor() { if (this.constructor === PExpr) { throw new Error("PExpr cannot be instantiated -- it's abstract"); } }
// Set the `source` property to the interval containing the source for this expression. withSource(interval) { if (interval) { this.source = interval.trimmed(); } return this; }}
// Any
const any = Object.create(PExpr.prototype);
// End
const end = Object.create(PExpr.prototype);
// Terminals
class Terminal extends PExpr { constructor(obj) { super(); this.obj = obj; }}
// Ranges
class Range extends PExpr { constructor(from, to) { super(); this.from = from; this.to = to; // If either `from` or `to` is made up of multiple code units, then // the range should consume a full code point, not a single code unit. this.matchCodePoint = from.length > 1 || to.length > 1; }}
// Parameters
class Param extends PExpr { constructor(index) { super(); this.index = index; }}
// Alternation
class Alt extends PExpr { constructor(terms) { super(); this.terms = terms; }}
// Extend is an implementation detail of rule extension
class Extend extends Alt { constructor(superGrammar, name, body) { const origBody = superGrammar.rules[name].body; super([body, origBody]);
this.superGrammar = superGrammar; this.name = name; this.body = body; }}
// Splice is an implementation detail of rule overriding with the `...` operator.class Splice extends Alt { constructor(superGrammar, ruleName, beforeTerms, afterTerms) { const origBody = superGrammar.rules[ruleName].body; super([...beforeTerms, origBody, ...afterTerms]);
this.superGrammar = superGrammar; this.ruleName = ruleName; this.expansionPos = beforeTerms.length; }}
// Sequences
class Seq extends PExpr { constructor(factors) { super(); this.factors = factors; }}
// Iterators and optionals
class Iter extends PExpr { constructor(expr) { super(); this.expr = expr; }}
class Star extends Iter {}class Plus extends Iter {}class Opt extends Iter {}
Star.prototype.operator = '*';Plus.prototype.operator = '+';Opt.prototype.operator = '?';
Star.prototype.minNumMatches = 0;Plus.prototype.minNumMatches = 1;Opt.prototype.minNumMatches = 0;
Star.prototype.maxNumMatches = Number.POSITIVE_INFINITY;Plus.prototype.maxNumMatches = Number.POSITIVE_INFINITY;Opt.prototype.maxNumMatches = 1;
// Predicates
class Not extends PExpr { constructor(expr) { super(); this.expr = expr; }}
class Lookahead extends PExpr { constructor(expr) { super(); this.expr = expr; }}
// "Lexification"
class Lex extends PExpr { constructor(expr) { super(); this.expr = expr; }}
// Rule application
class Apply extends PExpr { constructor(ruleName, args = []) { super(); this.ruleName = ruleName; this.args = args; }
isSyntactic() { return common.isSyntactic(this.ruleName); }
// This method just caches the result of `this.toString()` in a non-enumerable property. toMemoKey() { if (!this._memoKey) { Object.defineProperty(this, '_memoKey', {value: this.toString()}); } return this._memoKey; }}
// Unicode character
class UnicodeChar extends PExpr { constructor(category) { super(); this.category = category; this.pattern = UnicodeCategories[category]; }}
// --------------------------------------------------------------------// Exports// --------------------------------------------------------------------
exports.PExpr = PExpr;exports.any = any;exports.end = end;exports.Terminal = Terminal;exports.Range = Range;exports.Param = Param;exports.Alt = Alt;exports.Extend = Extend;exports.Splice = Splice;exports.Seq = Seq;exports.Iter = Iter;exports.Star = Star;exports.Plus = Plus;exports.Opt = Opt;exports.Not = Not;exports.Lookahead = Lookahead;exports.Lex = Lex;exports.Apply = Apply;exports.UnicodeChar = UnicodeChar;