pegisland
Version:
General PEG-based parser supporting island grammars with lake symbols
47 lines • 1.91 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GraphBuilder = void 0;
// Copyright (C) 2022- Katsumi Okuda. All rights reserved.
const BeginningCalculator_1 = require("./set/BeginningCalculator");
const ParsingExpression_1 = require("./ParsingExpression");
const IParsingExpressionVisitor_1 = require("./IParsingExpressionVisitor");
const PostorderExpressionTraverser_1 = require("./PostorderExpressionTraverser");
const Rule_1 = require("./Rule");
class GraphBuilder extends IParsingExpressionVisitor_1.DefaultParsingExpressionVisitor {
constructor() {
super(...arguments);
this.parents = new Map();
this.children = new Map();
this.rule = new Rule_1.Rule('dummy', new ParsingExpression_1.NullParsingExpression());
this.beginningSet = new Set();
}
build(peg) {
const beginningSets = new BeginningCalculator_1.BeginningCalculator(peg.rules, true).calculate();
const traverser = new PostorderExpressionTraverser_1.PostorderExpressionTraverser(this);
[...peg.rules.values()].forEach((rule) => {
this.rule = rule;
this.beginningSet = beginningSets.get(rule.rhs);
traverser.traverse(rule.rhs);
});
return [this.parents, this.children];
}
addParent(rule, parent) {
if (!this.parents.has(rule)) {
this.parents.set(rule, new Set());
}
const parents = this.parents.get(rule);
parents.add(parent);
if (!this.children.has(parent)) {
this.children.set(parent, new Set());
}
const children = this.children.get(parent);
children.add(rule);
}
visitNonterminal(pe) {
if (this.beginningSet.has(pe)) {
this.addParent(pe.rule, this.rule);
}
}
}
exports.GraphBuilder = GraphBuilder;
//# sourceMappingURL=GraphBuilder.js.map