UNPKG

antlr-ng

Version:

Next generation ANTLR Tool

39 lines (38 loc) 1.19 kB
var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); class TreeVisitor { static { __name(this, "TreeVisitor"); } /** * Visit every node in tree t and trigger an action for each node before/after having visited all of its children. * Execute both actions even if t has no children. If a child visit yields a new child, it can update its * parent's child list or just return the new child. The child update code works even if the child visit * alters its parent and returns the new tree. * * @param t The tree to visit. * @param action The action to trigger for each node. * * @returns The result of applying post action to this node. */ visit(t, action) { const isNil = t.isNil(); if (action && !isNil) { t = action.pre(t); } for (let i = 0; i < t.children.length; i++) { const child = t.children[i]; const visitResult = this.visit(child, action); if (visitResult !== child) { t.setChild(i, visitResult); } } if (action && !isNil) { t = action.post(t); } return t; } } export { TreeVisitor };