UNPKG

@igorivaniuk/tlb-parser

Version:

Parse TLB syntax into TypeScript objects

51 lines (50 loc) 1.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NodeVisitor = exports.walk = exports.iterChildNodes = void 0; const nodes_1 = require("./nodes"); function* iterChildNodes(node) { const type = Object.getPrototypeOf(node).constructor; for (let attributeName of type._attributes) { // @ts-ignore const attribute = node[attributeName]; if (attribute instanceof Array) { for (let arrayItem of attribute) { if (arrayItem instanceof nodes_1.ASTRootBase) { yield arrayItem; } } } else if (attribute instanceof nodes_1.ASTRootBase) { yield attribute; } } } exports.iterChildNodes = iterChildNodes; function* walk(node) { const todo = [node]; while (todo.length > 0) { const current = todo.shift(); todo.push(...iterChildNodes(current)); yield current; } } exports.walk = walk; class NodeVisitor { visit(node) { const name = node.constructor.name; // @ts-ignore const handler = this[`visit${name}`]; if (handler === undefined) { return this.genericVisit(node); } else { return handler(node); } } genericVisit(node) { for (let attribute of iterChildNodes(node)) { this.visit(attribute); } } } exports.NodeVisitor = NodeVisitor;