@eagleoutice/flowr
Version:
Static Dataflow Analyzer and Program Slicer for the R Programming Language
43 lines • 2.13 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.normalizedAstToMermaid = normalizedAstToMermaid;
exports.normalizedAstToMermaidUrl = normalizedAstToMermaidUrl;
const mermaid_1 = require("./mermaid");
const visitor_1 = require("../../r-bridge/lang-4.x/ast/model/processing/visitor");
const type_1 = require("../../r-bridge/lang-4.x/ast/model/type");
const r_function_call_1 = require("../../r-bridge/lang-4.x/ast/model/nodes/r-function-call");
function normalizedAstToMermaid(ast, prefix = 'flowchart TD\n') {
let output = prefix;
function showNode(n) {
const name = `${n.type} (${n.info.id})\\n${n.lexeme ?? ' '}`;
output += ` n${n.info.id}(["${(0, mermaid_1.escapeMarkdown)(name)}"])\n`;
if (n.info.parent !== undefined) {
const context = n.info;
const roleSuffix = context.role === "expr-list-child" /* RoleInParent.ExpressionListChild */ || context.role === "call-argument" /* RoleInParent.FunctionCallArgument */ || context.role === "function-def-param" /* RoleInParent.FunctionDefinitionParameter */ ? `-${context.index}` : '';
output += ` n${n.info.parent} -->|"${context.role}${roleSuffix}"| n${n.info.id}\n`;
}
if (n.type === type_1.RType.ExpressionList && n.grouping !== undefined) {
output += ` n${n.info.id} -.-|"group-open"| n${n.grouping[0].info.id}\n`;
output += ` n${n.info.id} -.-|"group-close"| n${n.grouping[1].info.id}\n`;
}
}
(0, visitor_1.visitAst)(ast, n => {
showNode(n);
if (n.type === 'RAccess' && (n.operator !== '[' && n.operator !== '[[')) {
for (const k of n.access) {
if (k !== r_function_call_1.EmptyArgument) {
showNode(k);
}
}
}
return false;
});
return output;
}
/**
* Use mermaid to visualize the normalized AST.
*/
function normalizedAstToMermaidUrl(ast, prefix = 'flowchart TD\n') {
return (0, mermaid_1.mermaidCodeToUrl)(normalizedAstToMermaid(ast, prefix));
}
//# sourceMappingURL=ast.js.map