UNPKG

antlr-ng

Version:

Next generation ANTLR Tool

40 lines (39 loc) 1.48 kB
var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); import { dupTree } from "../support/helpers.js"; import { RewriteRuleElementStream } from "./RewriteRuleElementStream.js"; class RewriteRuleSubtreeStream extends RewriteRuleElementStream { static { __name(this, "RewriteRuleSubtreeStream"); } /** * Treats next element as a single node even if it's a subtree. This is used instead of next() when the result has * to be a tree root node. Also prevents us from duplicating recently-added children; e.g., ^(type ID)+ adds ID * to type and then 2nd iteration must dup the type node, but ID has been added. * * Referencing a rule result twice is ok. Dup entire tree as we can't be adding trees as root, e.g., expr expr. * * Hideous code duplication here with super.next(). Can't think of a proper way to refactor. This needs to always * call dup node and super.next() doesn't know which to call: dup node or dup tree. * * @returns The next node in the stream. */ nextNode() { const n = this.size(); if (this.dirty || this.cursor >= n && n === 1) { const el = this.getNext(); return el.dupNode(); } let tree = this.getNext(); while (tree.isNil() && tree.children.length === 1) { tree = tree.children[0]; } return tree.dupNode(); } dup(el) { return dupTree(el); } } export { RewriteRuleSubtreeStream };