pegisland
Version:
General PEG-based parser supporting island grammars with lake symbols
145 lines • 4.21 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Lake = exports.Rewriting = exports.Grouping = exports.OrderedChoice = exports.Sequence = exports.ColonNot = exports.Colon = exports.Not = exports.And = exports.Optional = exports.OneOrMore = exports.ZeroOrMore = exports.Terminal = exports.Nonterminal = exports.NullParsingExpression = void 0;
class UnaryOperator {
constructor(operand) {
this.operand = operand;
}
}
class BinaryOperator {
constructor(lhs, rhs) {
this.lhs = lhs;
this.rhs = rhs;
}
}
class GeneralOperator {
constructor(operands) {
this.operands = operands;
}
}
class NullParsingExpression {
accept(_visitor, ..._arg) {
throw Error('Should not be called');
}
}
exports.NullParsingExpression = NullParsingExpression;
class Nonterminal {
constructor(rule, name = '') {
this.rule = rule;
this.name = name;
}
accept(visitor, ...arg) {
return visitor.visitNonterminal(this, ...arg);
}
}
exports.Nonterminal = Nonterminal;
class Terminal {
constructor(pattern, source) {
this.pattern = pattern;
this.source = source;
this.regex = new RegExp(pattern.source, 'smy');
}
accept(visitor, ...arg) {
return visitor.visitTerminal(this, ...arg);
}
}
exports.Terminal = Terminal;
class ZeroOrMore extends UnaryOperator {
accept(visitor, ...arg) {
return visitor.visitZeroOrMore(this, ...arg);
}
}
exports.ZeroOrMore = ZeroOrMore;
class OneOrMore extends UnaryOperator {
accept(visitor, ...arg) {
return visitor.visitOneOrMore(this, ...arg);
}
}
exports.OneOrMore = OneOrMore;
class Optional extends UnaryOperator {
accept(visitor, ...arg) {
return visitor.visitOptional(this, ...arg);
}
}
exports.Optional = Optional;
class And extends UnaryOperator {
accept(visitor, ...arg) {
return visitor.visitAnd(this, ...arg);
}
}
exports.And = And;
class Not extends UnaryOperator {
accept(visitor, ...arg) {
return visitor.visitNot(this, ...arg);
}
}
exports.Not = Not;
class Colon extends BinaryOperator {
accept(visitor, ...arg) {
return visitor.visitColon(this, ...arg);
}
}
exports.Colon = Colon;
class ColonNot extends BinaryOperator {
accept(visitor, ...arg) {
return visitor.visitColonNot(this, ...arg);
}
}
exports.ColonNot = ColonNot;
class Sequence extends GeneralOperator {
accept(visitor, ...arg) {
return visitor.visitSequence(this, ...arg);
}
}
exports.Sequence = Sequence;
class OrderedChoice extends GeneralOperator {
accept(visitor, ...arg) {
return visitor.visitOrderedChoice(this, ...arg);
}
}
exports.OrderedChoice = OrderedChoice;
class Grouping extends UnaryOperator {
accept(visitor, ...arg) {
return visitor.visitGrouping(this, ...arg);
}
}
exports.Grouping = Grouping;
class Rewriting {
constructor(operand, spec) {
this.operand = operand;
this.spec = spec;
}
accept(visitor, ...arg) {
return visitor.visitRewriting(this, ...arg);
}
}
exports.Rewriting = Rewriting;
class Lake extends UnaryOperator {
constructor(operand) {
super(operand);
this.originalOperand = operand;
}
makeSemantics(symbols, waterRules) {
const operandIsEpsilon = this.operand instanceof Sequence && this.operand.operands.length === 0;
const wildcard = new OrderedChoice([
...waterRules.map((rule) => new Nonterminal(rule)),
new Sequence([
...[...symbols].map((symbol) => new Not(symbol)),
new Terminal(/./, '.'),
]),
]);
const alwaysFailExpression = new Sequence([
new Not(new Terminal(/./, '.')),
new And(new Terminal(/./, '.')),
]);
this.operand = new ZeroOrMore(new Grouping(new OrderedChoice([
operandIsEpsilon ? alwaysFailExpression : this.operand,
wildcard,
])));
}
accept(visitor, ...arg) {
return visitor.visitLake(this, ...arg);
}
}
exports.Lake = Lake;
//# sourceMappingURL=ParsingExpression.js.map