pegisland
Version:
General PEG-based parser supporting island grammars with lake symbols
71 lines • 1.92 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.peToString = void 0;
class Printer {
buildString(pe) {
return pe.accept(this);
}
visitNonterminal(pe) {
return pe.rule.symbol;
}
visitTerminal(pe) {
return pe.source;
}
visitZeroOrMore(pe) {
return this.suffixOperator(pe, '*');
}
visitOneOrMore(pe) {
return this.suffixOperator(pe, '+');
}
visitOptional(pe) {
return this.suffixOperator(pe, '?');
}
visitAnd(pe) {
return this.prefixOperator(pe, '&');
}
visitNot(pe) {
return this.prefixOperator(pe, '!');
}
visitSequence(pe) {
return this.separatingOperator(pe, ' ');
}
visitOrderedChoice(pe) {
return this.separatingOperator(pe, ' / ');
}
visitGrouping(pe) {
return this.surroundingOperator(pe, '( ', ' )');
}
visitRewriting(pe) {
return pe.operand.accept(this);
}
visitColon(pe) {
return this.infixOperator(pe, ':');
}
visitColonNot(pe) {
return this.infixOperator(pe, ':!');
}
visitLake(pe) {
return this.surroundingOperator(pe, '<< ', ' >>');
}
separatingOperator(pe, operator) {
return pe.operands.map((operand) => operand.accept(this)).join(operator);
}
suffixOperator(pe, operator) {
return pe.operand.accept(this) + operator;
}
prefixOperator(pe, operator) {
return operator + pe.operand.accept(this);
}
infixOperator(pe, operator) {
return pe.lhs.accept(this) + operator + pe.rhs.accept(this);
}
surroundingOperator(pe, left, right) {
return left + pe.operand.accept(this) + right;
}
}
function peToString(pe) {
const printer = new Printer();
return printer.buildString(pe);
}
exports.peToString = peToString;
//# sourceMappingURL=Printer.js.map