pegisland
Version:
General PEG-based parser supporting island grammars with lake symbols
32 lines • 1.09 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Peg = void 0;
// Copyright (C) 2021- Katsumi Okuda. All rights reserved.
const lake_1 = require("./lake");
const ParsingExpression_1 = require("./ParsingExpression");
const Printer_1 = require("./Printer");
function ruleToString(rule) {
if (rule.rhs instanceof ParsingExpression_1.OrderedChoice) {
return `\n${Array.from(rule.rhs.operands.map(Printer_1.peToString))
.map((s) => ` ${s}`)
.join(' /\n')}`;
}
return (0, Printer_1.peToString)(rule.rhs);
}
function makeAnnotation(rule) {
return rule.isWater ? '@water\n' : '';
}
class Peg {
constructor(rules, toplevelRules) {
this.rules = rules;
this.toplevelRules = toplevelRules;
}
toString() {
return Array.from(this.rules)
.filter(([symbol]) => !(0, lake_1.isLake)(symbol))
.map(([symbol, rule]) => `${makeAnnotation(rule)}${symbol} <- ${ruleToString(rule)}`)
.join('\n');
}
}
exports.Peg = Peg;
//# sourceMappingURL=Peg.js.map