UNPKG

univac

Version:

Generate AST of lots of common programming languages using antlr4. JavaScript API and CLI tool.

81 lines 2.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var Visitor = /** @class */ (function () { function Visitor() { } Object.defineProperty(Visitor.prototype, "options", { get: function () { return this._options; }, set: function (value) { this._options = value; }, enumerable: true, configurable: true }); Visitor.prototype.getAst = function () { if (!this._options) { throw new Error('Options not set'); } if (!this.currentParent) { throw new Error('tree.accept(visitor) not called. '); } else { return this.currentParent; } }; Visitor.prototype.visitChildren = function (ctx) { var _this = this; if (!ctx) { return; } var node = this.getNode(ctx); if (!this.currentParent) { this.currentParent = node; } else { this.currentParent.children.push(node); if (this.options.parents) { node.parent = this.currentParent; } } if (ctx.children) { var previous = this.currentParent; this.currentParent = node; var result = ctx.children.map(function (child) { if (child.children && child.children.length) { return child.accept(_this); } else { return child.getText(); } }); this.currentParent = previous; return result; } return node; }; Visitor.prototype.getNode = function (ctx) { return { type: ctx.parser.ruleNames[ctx.ruleIndex] || ctx.constructor.name, start: !this.options.omitPosition && ctx.start ? this.getPosition(ctx.start) : undefined, stop: !this.options.omitPosition && ctx.stop ? this.getPosition(ctx.stop) : undefined, text: this.options.text ? ctx.getText() : undefined, children: [] }; }; Visitor.prototype.getPosition = function (start) { var source = start.source.find(function (e) { return typeof e.strdata === 'string'; }); return { start: start.start, stop: start.stop, line: start.line, column: start.column + 1, text: start.text, source: this.options.positionSource && source && source.strdata }; }; return Visitor; }()); exports.Visitor = Visitor; //# sourceMappingURL=visitor.js.map