UNPKG

@abaplint/transpiler

Version:
70 lines 2.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TryTranspiler = void 0; const abaplint = require("@abaplint/core"); const chunk_1 = require("../chunk"); class TryTranspiler { transpile(node, traversal) { const ret = new chunk_1.Chunk(); const catches = node.findDirectStructures(abaplint.Structures.Catch); const cleanup = node.findDirectStructures(abaplint.Structures.Cleanup); let catchCode = this.buildCatchCode(catches, traversal); for (const c of node.getChildren()) { if (c.get() instanceof abaplint.Structures.Catch) { if (catchCode) { ret.appendChunk(catchCode); } catchCode = undefined; } else if (c.get() instanceof abaplint.Structures.Cleanup) { ret.appendString(`} finally {\n// Transpiler todo: CLEANUP ignored\n`); } else if (c.get() instanceof abaplint.Statements.Try || c.get() instanceof abaplint.Statements.EndTry) { if (catches.length === 0 && cleanup.length === 0) { continue; } ret.appendChunk(traversal.traverse(c)); } else { ret.appendChunk(traversal.traverse(c)); } } return ret; } ///////////////////// buildCatchCode(nodes, traversal) { let ret = ""; let first = true; if (nodes.length === 0) { return new chunk_1.Chunk(ret); } ret += `} catch (e) {\n`; for (const n of nodes) { const catchStatement = n.findDirectStatement(abaplint.Statements.Catch); if (catchStatement === undefined) { throw "TryTranspiler, unexpected structure"; } const catchNames = catchStatement.findDirectExpressions(abaplint.Expressions.ClassName).map(e => traversal.lookupClassOrInterface(e.concatTokens(), e.getFirstToken())); ret += first ? "" : " else "; first = false; ret += "if (" + catchNames?.map(n => "(" + n + " && e instanceof " + n + ")").join(" || ") + ") {\n"; const intoNode = catchStatement.findExpressionAfterToken("INTO"); if (intoNode) { ret += traversal.traverse(intoNode).getCode() + ".set(e);\n"; } const body = n.findDirectStructure(abaplint.Structures.Body); if (body) { ret += traversal.traverse(body).getCode(); } ret += "}"; } // "else" unhandled in this TRY-CATCH, or a javascript runtime error ret += ` else {\n` + `throw e;\n` + `}\n`; return new chunk_1.Chunk(ret); } } exports.TryTranspiler = TryTranspiler; //# sourceMappingURL=try.js.map