mbeckerle commented on a change in pull request #422: URL: https://github.com/apache/incubator-daffodil/pull/422#discussion_r497838711
########## File path: daffodil-core/src/main/scala/org/apache/daffodil/runtime2/generators/ParserGenerator.scala ########## @@ -0,0 +1,314 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.daffodil.runtime2.generators + +import org.apache.daffodil.api.DFDL +import org.apache.daffodil.dpath.NodeInfo +import org.apache.daffodil.dpath.NodeInfo.PrimType +import org.apache.daffodil.dsom.ElementBase +import org.apache.daffodil.exceptions.ThrowsSDE + +import scala.collection.mutable + +/** + * Gives an object the ability to generate code. + */ +trait ParserGenerator { + def generateCode(state: CodeGeneratorState): Unit +} + +/** + * Builds up the state of generated code. + */ +class CodeGeneratorState extends DFDL.CodeGeneratorState { + private val structs = mutable.Stack[ComplexCGState]() + private val prototypes = mutable.ArrayBuffer[String]() + private val erds = mutable.ArrayBuffer[String]() + private val finalStructs = mutable.ArrayBuffer[String]() + private val finalImplementation = mutable.ArrayBuffer[String]() + + def addImplementation(context: ElementBase): Unit = { + val C = context.namedQName.local + val initStatements = structs.top.initStatements.mkString("\n") + val parserStatements = structs.top.parserStatements.mkString("\n") + val unparserStatements = structs.top.unparserStatements.mkString("\n") + val prototypeFunctions = + s"""static void ${C}_initSelf($C *instance); + |static const char *${C}_parseSelf($C *instance, const PState *pstate); + |static const char *${C}_unparseSelf(const $C *instance, const UState *ustate);""".stripMargin + prototypes += prototypeFunctions + val functions = + s"""static void + |${C}_initSelf($C *instance) + |{ + |$initStatements + |} + | + |static const char * + |${C}_parseSelf($C *instance, const PState *pstate) + |{ + | const char *error_msg = NULL; + |$parserStatements + | return error_msg; + |} + | + |static const char * + |${C}_unparseSelf(const $C *instance, const UState *ustate) + |{ + | const char *error_msg = NULL; + |$unparserStatements + | return error_msg; + |} + |""".stripMargin + finalImplementation += functions + } + + private def defineQNameInit(context: ElementBase): String = { + val qname = context.namedQName.toQNameString + val xmlns = if (context.namedQName.prefix.isDefined) s"xmlns:${context.namedQName.prefix.get}" else "xmlns" + val ns = context.namedQName.namespace.toStringOrNullIfNoNS + // Optimize away xmlns=ns declaration if possible, although this approach may not be entirely correct + val parentOpt = context.enclosingElements.headOption + val parentNs = if (parentOpt.isDefined) parentOpt.get.namedQName.namespace.toStringOrNullIfNoNS + val qnameInit = if (ns == null || ns == parentNs) + s""" {"$qname"}, // namedQName.name""" + else + s""" { + | "$qname", // namedQName.name + | "$xmlns", // namedQName.xmlns Review comment: This should be the prefix string, or null. It should not be an artifact of conversion to XML wanting to make an xmlns:pre binding out of it via the mxml library. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
