|
| 1 | +class Generator { |
| 2 | + static func Generate(_ datamodel: DataModel) -> String { |
| 3 | + var output = "" |
| 4 | + let pluginList = datamodel.plugins |
| 5 | + |
| 6 | + // optional imports |
| 7 | + if datamodel.imports != nil { |
| 8 | + output = output + Generator.ImportsToString(datamodel.imports!) + "\n" |
| 9 | + } |
| 10 | + |
| 11 | + // optional accesscontrol |
| 12 | + if datamodel.accessControlLevel != nil { |
| 13 | + output = output + Generator.AccessControlLevelToString(datamodel.accessControlLevel!) + " " |
| 14 | + } |
| 15 | + |
| 16 | + // struct or class |
| 17 | + let shouldUseClass: Bool = Generator.shouldUseClass(pluginList) |
| 18 | + if shouldUseClass { |
| 19 | + output = output + Keyword.CLASS + " " |
| 20 | + } else { |
| 21 | + output = output + Keyword.STRUCT + " " |
| 22 | + } |
| 23 | + // name |
| 24 | + output = output + datamodel.name + " " |
| 25 | + |
| 26 | + // optional super classes |
| 27 | + if pluginList != nil { |
| 28 | + output = output + Generator.PrintSuperClasses(pluginList!) |
| 29 | + } |
| 30 | + |
| 31 | + // variables |
| 32 | + var datamodelBody = Generator.SchemaToString(datamodel.schema) |
| 33 | + |
| 34 | + // post variable body from plugins |
| 35 | + if pluginList != nil { |
| 36 | + let additionalBody = Generator.GeneratePostVariableDefinitionObjectBodyWithPluginList(pluginList!, datamodel) |
| 37 | + if !additionalBody.isEmpty { |
| 38 | + datamodelBody = datamodelBody + "\n" + additionalBody |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + // constructor |
| 43 | + if shouldUseClass { |
| 44 | + let constructor = Generator.PrintConstructorForClass(datamodel.schema) |
| 45 | + datamodelBody = datamodelBody + "\n" + constructor + "\n" |
| 46 | + } |
| 47 | + |
| 48 | + // post constructor body from plugins |
| 49 | + if pluginList != nil { |
| 50 | + let additionalBody = Generator.GeneratePostConstructorObjectBodyWithPluginList(pluginList!, datamodel) |
| 51 | + if !additionalBody.isEmpty { |
| 52 | + datamodelBody = datamodelBody + "\n" + additionalBody + "\n" |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + datamodelBody = StringUtils.formatStringWithIndentLevel(str: datamodelBody, indentLevel: 1) |
| 57 | + output = output + Generator.WrappedModelBody(datamodelBody) |
| 58 | + return output |
| 59 | + } |
| 60 | + |
| 61 | + static func ImportsToString(_ imports: [Import]) -> String { |
| 62 | + var output = "" |
| 63 | + for importStatment in imports { |
| 64 | + if importStatment is SingleModuleImport { |
| 65 | + let singleModuleImport = importStatment as! SingleModuleImport |
| 66 | + output = output + [Keyword.IMPORT, singleModuleImport.module].joined(separator: " ") |
| 67 | + } else if importStatment is ModuleWithSubmoduleImport { |
| 68 | + let moduleWithSubmoduleImport = importStatment as! ModuleWithSubmoduleImport |
| 69 | + output = output + [Keyword.IMPORT, moduleWithSubmoduleImport.module + "." + moduleWithSubmoduleImport.submodule].joined(separator: " ") |
| 70 | + } else if importStatment is ModuleWithSymbolImport { |
| 71 | + let moduleWithSymbolImport = importStatment as! ModuleWithSymbolImport |
| 72 | + output = output + [Keyword.IMPORT, moduleWithSymbolImport.kind, moduleWithSymbolImport.module + "." + moduleWithSymbolImport.symbol].joined(separator: " ") |
| 73 | + } |
| 74 | + output = output + "\n" |
| 75 | + } |
| 76 | + return output |
| 77 | + } |
| 78 | + |
| 79 | + static func AccessControlLevelToString(_ accessControl: AccessControl) -> String { |
| 80 | + switch accessControl { |
| 81 | + case AccessControl.levelPublic: |
| 82 | + return Keyword.AccessControlModifier.PUBLIC_LEVEL |
| 83 | + case AccessControl.levelInternal: |
| 84 | + return Keyword.AccessControlModifier.INTERNAL_LEVEL |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + static func shouldUseClass(_ pluginList: PluginList?) -> Bool { |
| 89 | + if pluginList == nil { |
| 90 | + return false |
| 91 | + } |
| 92 | + for pluginName in pluginList!.plugins { |
| 93 | + if Plugins.PLUGIN_MAP[pluginName] != nil, Plugins.PLUGIN_MAP[pluginName]!.shouldUseClass() { |
| 94 | + return true |
| 95 | + } |
| 96 | + } |
| 97 | + return false |
| 98 | + } |
| 99 | + |
| 100 | + static func PrintSuperClasses(_ pluginList: PluginList) -> String { |
| 101 | + var superClasses: [String] = [] |
| 102 | + for pluginName in pluginList.plugins { |
| 103 | + if Plugins.PLUGIN_MAP[pluginName] != nil { |
| 104 | + superClasses = superClasses + Plugins.PLUGIN_MAP[pluginName]!.superClasses() |
| 105 | + } |
| 106 | + } |
| 107 | + if !superClasses.isEmpty { |
| 108 | + return ": " + superClasses.joined(separator: ", ") |
| 109 | + } else { |
| 110 | + return "" |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + static func PrintConstructorForClass(_ schema: Schema) -> String { |
| 115 | + let constructorFormat = "init(%@) {\n%@\n}" |
| 116 | + var parameters: [String] = [] |
| 117 | + var assignments: [String] = [] |
| 118 | + for statement in schema.statements { |
| 119 | + if statement is StateDef { |
| 120 | + parameters.append((statement as! StateDef).name + ":" + (statement as! StateDef).type) |
| 121 | + assignments.append("self." + (statement as! StateDef).name + " = " + (statement as! StateDef).name) |
| 122 | + } |
| 123 | + } |
| 124 | + let parametersString = parameters.joined(separator: ", ") |
| 125 | + let assignmentString = StringUtils.formatStringWithIndentLevel(str: assignments.joined(separator: "\n"), indentLevel: 1) |
| 126 | + return String(format: constructorFormat, parametersString, assignmentString) |
| 127 | + } |
| 128 | + |
| 129 | + static func SchemaToString(_ schema: Schema) -> String { |
| 130 | + var result: [String] = [] |
| 131 | + for i in 0 ..< schema.statements.count { |
| 132 | + let statement = schema.statements[i] |
| 133 | + if statement is Comment { |
| 134 | + result.append("//" + (statement as! Comment).source) |
| 135 | + } else if statement is StateDef { |
| 136 | + let stateDef = statement as! StateDef |
| 137 | + result.append("let " + stateDef.name + " : " + stateDef.type) |
| 138 | + } |
| 139 | + } |
| 140 | + return result.joined(separator: "\n") + "\n" |
| 141 | + } |
| 142 | + |
| 143 | + static func GeneratePostVariableDefinitionObjectBodyWithPluginList(_ pluginList: PluginList, _ datamodel: DataModel) -> String { |
| 144 | + var additionalBody: [String] = [] |
| 145 | + for pluginName in pluginList.plugins { |
| 146 | + if Plugins.PLUGIN_MAP[pluginName] != nil { |
| 147 | + let bodyFromPlugin = Plugins.PLUGIN_MAP[pluginName]!.postVariableDefinition(datamodel) |
| 148 | + if !bodyFromPlugin.isEmpty { |
| 149 | + additionalBody.append(bodyFromPlugin) |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + return additionalBody.joined(separator: "\n") |
| 154 | + } |
| 155 | + |
| 156 | + static func GeneratePostConstructorObjectBodyWithPluginList(_ pluginList: PluginList, _ datamodel: DataModel) -> String { |
| 157 | + var additionalMethods: [String] = [] |
| 158 | + for pluginName in pluginList.plugins { |
| 159 | + if Plugins.PLUGIN_MAP[pluginName] != nil { |
| 160 | + let methodFromPlugin = Plugins.PLUGIN_MAP[pluginName]!.postConstructor(datamodel) |
| 161 | + if !methodFromPlugin.isEmpty { |
| 162 | + additionalMethods.append(methodFromPlugin) |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + return additionalMethods.joined(separator: "\n") |
| 167 | + } |
| 168 | + |
| 169 | + static func WrappedModelBody(_ body: String) -> String { |
| 170 | + return "{\n" + body + "}" |
| 171 | + } |
| 172 | +} |
0 commit comments