UNPKG

antlr-ng

Version:

Next generation ANTLR Tool

82 lines (81 loc) 2.77 kB
var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); import { HashSet, OrderedHashSet } from "antlr4ng"; import { ST } from "stringtemplate4ts"; import { isModelElement } from "../misc/ModelElement.js"; import { IssueCode } from "../tool/Issues.js"; import { OutputModelObject } from "./model/OutputModelObject.js"; class OutputModelWalker { static { __name(this, "OutputModelWalker"); } tool; templates; constructor(tool, templates) { this.tool = tool; this.templates = templates; } walk(omo, header) { let templateName = omo.constructor.name; if (header) { templateName += "Header"; } const st = this.templates.getInstanceOf(templateName); if (st === null) { this.tool.errorManager.toolError(IssueCode.CodeGenTemplatesIncomplete, templateName); return new ST("[" + templateName + " invalid]"); } if (!st.impl?.formalArguments) { this.tool.errorManager.toolError(IssueCode.CodeTemaplateArgIssue, templateName, "<none>"); return st; } const formalArgs = st.impl.formalArguments; const [modelArgName] = [...formalArgs.keys()]; st.add(modelArgName, omo); const usedFieldNames = /* @__PURE__ */ new Set(); for (const fieldName in omo) { if (!isModelElement(omo, fieldName)) { continue; } if (usedFieldNames.has(fieldName)) { this.tool.errorManager.toolError(IssueCode.InternalError, "Model object " + omo.constructor.name + " has multiple fields named '" + fieldName + "'"); continue; } usedFieldNames.add(fieldName); if (!formalArgs.get(fieldName)) { continue; } const o = omo[fieldName]; if (o instanceof OutputModelObject) { const nestedOmo = o; const nestedST = this.walk(nestedOmo, header); st.add(fieldName, nestedST); } else if (o instanceof Set || o instanceof HashSet || o instanceof OrderedHashSet || Array.isArray(o)) { for (const nestedOmo of o) { if (!nestedOmo) { continue; } const nestedST = this.walk(nestedOmo, header); st.add(fieldName, nestedST); } } else if (o instanceof Map) { const nestedOmoMap = o; const m = /* @__PURE__ */ new Map(); for (const [key, value] of nestedOmoMap) { const nestedST = this.walk(value, header); m.set(key, nestedST); } st.add(fieldName, m); } else if (o !== void 0) { this.tool.errorManager.toolError( IssueCode.InternalError, "not recognized nested model element: " + fieldName ); } } return st; } } export { OutputModelWalker };