UNPKG

@eagleoutice/flowr

Version:

Static Dataflow Analyzer and Program Slicer for the R Programming Language

86 lines 3.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NodeId = void 0; exports.recoverName = recoverName; exports.recoverContent = recoverContent; const vertex_1 = require("../../../../../dataflow/graph/vertex"); const retriever_1 = require("../../../../retriever"); const identifier_1 = require("../../../../../dataflow/environments/identifier"); /** * The type of the id assigned to each node. Branded to avoid problematic usages with other string or numeric types. * The default ids are numeric, but we use a branded type to avoid confusion with other numeric types. * Custom ids or scoped ids can be strings, but they will be normalized to numbers if they are numeric strings. */ exports.NodeId = { name: 'NodeId', /** * Normalizes a node id by converting numeric strings to numbers. * This allows us to use numeric ids without storing them as strings, while still allowing custom string ids if needed. */ normalize(id) { // check if string is number if (typeof id === 'string') { /* typescript is a beautiful converter */ const num = +id; if (!Number.isNaN(num)) { return num; } } return id; }, /** * The prefix used for built-in function or operator ids. */ builtInPrefix: 'built-in:', /** * Checks if a given node id is a built-in function or operator id by checking if it is a string that starts with the built-in prefix. * @see {@link toBuiltIn} - to convert a built-in function or operator name to a built-in id * @see {@link fromBuiltIn} - to recover the built-in function or operator name from a built-in id */ isBuiltIn(id) { return typeof id === 'string' && id.startsWith(exports.NodeId.builtInPrefix); }, /** * Converts a built-in function or operator name to a built-in id by prefixing it with the built-in prefix. * @see {@link isBuiltIn} - to check if a given node id is a built-in function or operator id * @see {@link fromBuiltIn} - to recover the built-in function or operator name from a built-in id */ toBuiltIn(name) { return `built-in:${name}`; }, /** * Recovers the built-in function or operator name from a built-in id by removing the built-in prefix. * @see {@link isBuiltIn} - to check if a given node id is a built-in function or operator id * @see {@link toBuiltIn} - to convert a built-in function or operator name to a built-in id */ fromBuiltIn(id) { return id.slice(builtInPrefixLength); } }; const builtInPrefixLength = exports.NodeId.builtInPrefix.length; /** * Recovers the lexeme of a {@link RNode|node} from its id in the {@link AstIdMap|id map}. * @see {@link recoverContent} - to recover the content of a node */ function recoverName(id, idMap) { return idMap?.get(id)?.lexeme; } /** * Recovers the content of a {@link RNode|node} from its id in the {@link DataflowGraph|dataflow graph}. */ function recoverContent(id, graph) { const vertex = graph.getVertex(id); if (vertex && vertex.tag === vertex_1.VertexType.FunctionCall && vertex.name) { return identifier_1.Identifier.toString(vertex.name); } const node = graph.idMap?.get(id); if (node === undefined) { return undefined; } const lexeme = node.lexeme ?? node.info.fullLexeme ?? ''; if (vertex?.tag === vertex_1.VertexType.Use) { return (0, retriever_1.removeRQuotes)(lexeme); } return lexeme; } //# sourceMappingURL=node-id.js.map