UNPKG

@abaplint/core

Version:
335 lines • 11.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.StatementNode = void 0; const _abstract_node_1 = require("./_abstract_node"); const token_node_1 = require("./token_node"); const expression_node_1 = require("./expression_node"); const comment_1 = require("../1_lexer/tokens/comment"); const pragma_1 = require("../1_lexer/tokens/pragma"); const string_1 = require("../1_lexer/tokens/string"); const string_template_middle_1 = require("../1_lexer/tokens/string_template_middle"); const string_template_end_1 = require("../1_lexer/tokens/string_template_end"); const string_template_begin_1 = require("../1_lexer/tokens/string_template_begin"); const string_template_1 = require("../1_lexer/tokens/string_template"); class StatementNode extends _abstract_node_1.AbstractNode { constructor(statement, colon, pragmas) { super(); this.statement = statement; this.colon = colon; if (pragmas) { this.pragmas = pragmas; } else { this.pragmas = []; } } get() { return this.statement; } getColon() { return this.colon; } getPragmas() { return this.pragmas; } setChildren(children) { if (children.length === 0) { throw new Error("statement: zero children"); } this.children = children; return this; } getStart() { return this.getFirstToken().getStart(); } getEnd() { const last = this.getLastToken(); return last.getEnd(); } getTokens() { const tokens = []; for (const c of this.getChildren()) { tokens.push(...this.toTokens(c)); } return tokens; } includesToken(search) { for (const t of this.getTokens()) { if (t.getStart().equals(search.getStart())) { return true; } } return false; } getTokenNodes() { const tokens = []; for (const c of this.getChildren()) { tokens.push(...this.toTokenNodess(c)); } return tokens; } concatTokens() { let str = ""; let prev; for (const token of this.getTokens()) { if (token instanceof pragma_1.Pragma) { continue; } if (str === "") { str = token.getStr(); } else if (prev && prev.getStr().length + prev.getCol() === token.getCol() && prev.getRow() === token.getRow()) { str = str + token.getStr(); } else { str = str + " " + token.getStr(); } prev = token; } return str; } concatTokensVirtual() { let str = ""; let prev; for (const token of this.getTokens()) { if (token instanceof pragma_1.Pragma) { continue; } const vprev = prev === null || prev === void 0 ? void 0 : prev.getStart(); const vtoke = token === null || token === void 0 ? void 0 : token.getStart(); if (str === "") { str = token.getStr(); } else if (prev && vprev && vtoke && prev.getStr().length + vprev.vcol === vtoke.vcol && vprev.vrow === vtoke.vrow) { str = str + token.getStr(); } else { str = str + " " + token.getStr(); } prev = token; } return str; } concatTokensWithoutStringsAndComments() { let str = ""; let prev; for (const token of this.getTokens()) { if (token instanceof comment_1.Comment || token instanceof string_1.StringToken || token instanceof string_template_1.StringTemplate || token instanceof string_template_begin_1.StringTemplateBegin || token instanceof string_template_middle_1.StringTemplateMiddle || token instanceof string_template_end_1.StringTemplateEnd) { continue; } if (str === "") { str = token.getStr(); } else if (prev && prev.getStr().length + prev.getCol() === token.getCol() && prev.getRow() === token.getRow()) { str = str + token.getStr(); } else { str = str + " " + token.getStr(); } prev = token; } return str; } getTerminator() { return this.getLastToken().getStr(); } getFirstToken() { for (const child of this.getChildren()) { return child.getFirstToken(); } throw new Error("StatementNode, getFirstToken, no children, " + this.get().constructor.name); } getLastToken() { const child = this.getLastChild(); if (child !== undefined) { return child.getLastToken(); } throw new Error("StatementNode, getLastToken, no children"); } findDirectExpression(type) { for (const child of this.getChildren()) { if (child instanceof expression_node_1.ExpressionNode && child.get() instanceof type) { return child; } } return undefined; } findDirectExpressions(type) { const ret = []; for (const child of this.getChildren()) { if (child instanceof expression_node_1.ExpressionNode && child.get() instanceof type) { ret.push(child); } } return ret; } findDirectTokenByText(text) { const upper = text.toUpperCase(); for (const child of this.getChildren()) { if (child instanceof token_node_1.TokenNode && child.get().getStr().toUpperCase() === upper) { return child.get(); } } return undefined; } findFirstExpression(type) { for (const child of this.getChildren()) { if (child instanceof token_node_1.TokenNode) { continue; } else if (child.get() instanceof type) { return child; } else { const res = child.findFirstExpression(type); if (res) { return res; } } } return undefined; } findAllExpressions(type) { const ret = []; for (const child of this.getChildren()) { if (child instanceof token_node_1.TokenNode) { continue; } else if (child.get() instanceof type) { ret.push(child); } else { ret.push(...child.findAllExpressions(type)); } } return ret; } findAllExpressionsRecursive(type) { const ret = []; for (const child of this.getChildren()) { if (child instanceof token_node_1.TokenNode) { continue; } else if (child.get() instanceof type) { ret.push(child); } ret.push(...child.findAllExpressionsRecursive(type)); } return ret; } findAllExpressionsMulti(type, recursive = false) { const ret = []; for (const child of this.getChildren()) { if (child instanceof token_node_1.TokenNode) { continue; } const before = ret.length; for (const t of type) { if (child.get() instanceof t) { ret.push(child); } } if (before === ret.length || recursive === true) { ret.push(...child.findAllExpressionsMulti(type, recursive)); } } return ret; } /** * Returns the Position of the first token if the sequence is found, * otherwise undefined. Strings and Comments are ignored in this search. * @param first - Text of the first Token * @param second - Text of the second Token */ findTokenSequencePosition(first, second) { let prev; for (const token of this.getTokens()) { if (token instanceof comment_1.Comment || token instanceof string_1.StringToken || token instanceof string_template_1.StringTemplate || token instanceof string_template_begin_1.StringTemplateBegin || token instanceof string_template_middle_1.StringTemplateMiddle || token instanceof string_template_end_1.StringTemplateEnd) { continue; } if (prev && token.getStr().toUpperCase() === second && (prev === null || prev === void 0 ? void 0 : prev.getStr().toUpperCase()) === first.toUpperCase()) { return prev.getStart(); } else { prev = token; } } return undefined; } findExpressionAfterToken(text) { const children = this.getChildren(); for (let i = 0; i < children.length - 1; i++) { const c = children[i]; const next = children[i + 1]; if (c instanceof token_node_1.TokenNode && c.get().getStr().toUpperCase() === text.toUpperCase() && next instanceof expression_node_1.ExpressionNode) { return next; } } return undefined; } findExpressionsAfterToken(text) { const children = this.getChildren(); const ret = []; for (let i = 0; i < children.length - 1; i++) { const c = children[i]; const next = children[i + 1]; if (c instanceof token_node_1.TokenNode && c.get().getStr().toUpperCase() === text.toUpperCase() && next instanceof expression_node_1.ExpressionNode) { ret.push(next); } } return ret; } //////////////////////////////// toTokens(b) { const tokens = []; if (b instanceof token_node_1.TokenNode) { tokens.push(b.get()); return tokens; } for (const c of b.getChildren()) { if (c instanceof token_node_1.TokenNode) { tokens.push(c.get()); } else { tokens.push(...this.toTokens(c)); } } return tokens; } toTokenNodess(b) { const tokens = []; if (b instanceof token_node_1.TokenNode) { tokens.push(b); return tokens; } for (const c of b.getChildren()) { if (c instanceof token_node_1.TokenNode) { tokens.push(c); } else { tokens.push(...this.toTokenNodess(c)); } } return tokens; } } exports.StatementNode = StatementNode; //# sourceMappingURL=statement_node.js.map