@abaplint/core
Version:
abaplint - Core API
274 lines • 8.54 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExpressionNode = void 0;
const token_node_1 = require("./token_node");
const tokens_1 = require("../1_lexer/tokens");
const _abstract_node_1 = require("./_abstract_node");
class ExpressionNode extends _abstract_node_1.AbstractNode {
constructor(expression) {
super();
this.expression = expression;
}
get() {
return this.expression;
}
countTokens() {
let ret = 0;
for (const c of this.getChildren()) {
ret = ret + c.countTokens();
}
return ret;
}
getFirstToken() {
for (const child of this.getChildren()) {
return child.getFirstToken();
}
throw new Error("ExpressionNode, getFirstToken, no children");
}
concatTokensWithLinebreaks() {
let str = "";
let prev;
for (const token of this.getTokens()) {
if (token instanceof tokens_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 {
if (prev && prev.getRow() !== token.getRow()) {
str = str + "\n" + token.getStr();
}
else {
str = str + " " + token.getStr();
}
}
prev = token;
}
return str;
}
concatTokens() {
let str = "";
let prev;
for (const token of this.getTokens()) {
if (token instanceof tokens_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;
}
concatTokensWithoutStringsAndComments() {
let str = "";
let prev;
for (const token of this.getTokens()) {
if (token instanceof tokens_1.Comment
|| token instanceof tokens_1.StringToken
|| token instanceof tokens_1.StringTemplate
|| token instanceof tokens_1.StringTemplateBegin
|| token instanceof tokens_1.StringTemplateMiddle
|| token instanceof tokens_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;
}
// todo: delete this method?, its slow
getTokens() {
const tokens = [];
for (const c of this.getChildren()) {
tokens.push(...this.toTokens(c));
}
return tokens;
}
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;
}
getLastToken() {
const child = this.getLastChild();
if (child) {
return child.getLastToken();
}
throw new Error("ExpressionNode, getLastToken, no children");
}
getAllTokens() {
const ret = [];
for (const child of this.getChildren()) {
if (child instanceof token_node_1.TokenNode) {
ret.push(child.get());
}
else {
ret.push(...child.getAllTokens());
}
}
return ret;
}
getDirectTokens() {
const ret = [];
for (const child of this.getChildren()) {
if (child instanceof token_node_1.TokenNode) {
ret.push(child.get());
}
}
return ret;
}
findDirectExpression(type) {
for (const child of this.getChildren()) {
if (child instanceof ExpressionNode && child.get() instanceof type) {
return child;
}
}
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 ExpressionNode) {
return next;
}
}
return undefined;
}
findDirectExpressions(type) {
const ret = [];
for (const child of this.getChildren()) {
if (child instanceof ExpressionNode && child.get() instanceof type) {
ret.push(child);
}
}
return ret;
}
findDirectExpressionsMulti(type) {
const ret = [];
for (const child of this.getChildren()) {
if (child instanceof ExpressionNode) {
for (const t of type) {
if (child.get() instanceof t) {
ret.push(child);
break;
}
}
}
}
return ret;
}
findDirectTokenByText(text) {
const search = text.toUpperCase();
for (const child of this.getChildren()) {
if (child instanceof token_node_1.TokenNode && child.get().getStr().toUpperCase() === search) {
return child.get();
}
}
return undefined;
}
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;
}
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;
}
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;
}
findFirstExpression(type) {
if (this.get() instanceof type) {
return this;
}
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;
}
}
exports.ExpressionNode = ExpressionNode;
//# sourceMappingURL=expression_node.js.map