UNPKG

@roku-road/bright

Version:

Blazing fast parser for BrightScript that gives you ESTree like AST

225 lines (224 loc) 6.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const lodash_1 = require("lodash"); const VisitorKeys_1 = require("./VisitorKeys"); const shouldBeVisitor = (method) => { return VisitorKeys_1.visitorKeys[method] && method.toUpperCase() !== method; }; class BaseASTWalker { Program() { throw new Error("Method not implemented."); } LibraryStatement() { throw new Error("Method not implemented."); } UnTypedIdentifier() { throw new Error("Method not implemented."); } ParameterList() { throw new Error("Method not implemented."); } AdditionExpression() { throw new Error("Method not implemented."); } AssignmentExpression() { throw new Error("Method not implemented."); } MultiplicationExpression() { throw new Error("Method not implemented."); } LogicExpression() { throw new Error("Method not implemented."); } ParenthesisExpression() { throw new Error("Method not implemented."); } ArrayElement() { throw new Error("Method not implemented."); } ArrayExpression() { throw new Error("Method not implemented."); } CallExpression() { throw new Error("Method not implemented."); } ConditionalConst() { throw new Error("Method not implemented."); } ConditionalElseIfStatement() { throw new Error("Method not implemented."); } ConditionalElseStatement() { throw new Error("Method not implemented."); } ConditionalError() { throw new Error("Method not implemented."); } ConditionalIfStatement() { throw new Error("Method not implemented."); } DimStatement() { throw new Error("Method not implemented."); } DotMemberExpression() { throw new Error("Method not implemented."); } ElseIfStatement() { throw new Error("Method not implemented."); } ElseStatement() { throw new Error("Method not implemented."); } EmptyStatement() { throw new Error("Method not implemented."); } Comment() { throw new Error("Method not implemented."); } ForEachStatement() { throw new Error("Method not implemented."); } ForStatement() { throw new Error("Method not implemented."); } FunctionDeclaration() { throw new Error("Method not implemented."); } FunctionExpression() { throw new Error("Method not implemented."); } GoToStatement() { throw new Error("Method not implemented."); } IfStatement() { throw new Error("Method not implemented."); } Literal() { throw new Error("Method not implemented."); } MemberExpression() { throw new Error("Method not implemented."); } NextStatement() { throw new Error("Method not implemented."); } PostfixExpression() { throw new Error("Method not implemented."); } PrintStatement() { throw new Error("Method not implemented."); } RelationExpression() { throw new Error("Method not implemented."); } ReturnStatement() { throw new Error("Method not implemented."); } StopStatement() { throw new Error("Method not implemented."); } SubDeclaration() { throw new Error("Method not implemented."); } SubExpression() { throw new Error("Method not implemented."); } UnaryExpression() { throw new Error("Method not implemented."); } WhileStatement() { throw new Error("Method not implemented."); } RokuTryStatement() { throw new Error("Method not implemented."); } BlockStatement() { throw new Error("Method not implemented."); } TypeAnnotation() { throw new Error("Method not implemented."); } ObjectExpression() { throw new Error("Method not implemented."); } Parameter() { throw new Error("Method not implemented."); } Identifier() { throw new Error("Method not implemented."); } Property() { throw new Error("Method not implemented."); } Arguments() { throw new Error("Method not implemented."); } STRING_LITERAL(ctx) { return ctx.loc.source; } visit(ctx) { if (!ctx.type) { return null; } const keysToVisit = VisitorKeys_1.visitorKeys[ctx.type]; const mapped = lodash_1.map(keysToVisit, (key) => { if (!ctx[key]) { return ctx[key]; } if (ctx[key].type) { const method = ctx[key].type; if (!this[method]) { if (shouldBeVisitor(method)) { console.warn(method); } else { return this.STRING_LITERAL(ctx[key]); } } else { return this[method](ctx[key]); } } else { if (lodash_1.isArray(ctx[key])) { return lodash_1.map(ctx[key], (element) => { const method = element.type; if (!this[method]) { if (shouldBeVisitor(method)) { console.warn(method); } else { return this.STRING_LITERAL(element); } } else { return this[method](element); } }); } } }); return lodash_1.zipObject(keysToVisit, mapped); } asArray(value) { if (!value) { return []; } return lodash_1.isArray(value) ? value : [value]; } binary(ctx) { let { left, right, operator } = this.visit(ctx); left = this.asArray(left); right = this.asArray(right); operator = this.asArray(operator); return this.mergeOperands(right, left, operator); } mergeOperands(from = [], to = [], dividers = []) { while (from.length) { to.push(dividers.shift()); to.push(from.shift()); } return to; } } exports.BaseASTWalker = BaseASTWalker;