mbeckerle commented on a change in pull request #88: Daffodil 1919 separators
URL: https://github.com/apache/incubator-daffodil/pull/88#discussion_r209076820
 
 

 ##########
 File path: 
daffodil-core/src/main/scala/org/apache/daffodil/grammar/primitives/ChoiceCombinator.scala
 ##########
 @@ -0,0 +1,124 @@
+/*
+ * 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.grammar.primitives
+
+import org.apache.daffodil.grammar.Terminal
+import org.apache.daffodil.dsom._
+import org.apache.daffodil.processors.parsers._
+import org.apache.daffodil.processors.unparsers._
+import org.apache.daffodil.grammar.Gram
+import org.apache.daffodil.exceptions.Assert
+import org.apache.daffodil.cookers.ChoiceBranchKeyCooker
+import org.apache.daffodil.api.WarnID
+import org.apache.daffodil.equality._
+
+/*
+ * The purpose of the ChoiceCombinator (and the parsers it creates) is to
+ * determine which branch to go down. In the parser case, for non-direct
+ * dispatch, we just rely on backtracking here.
+ *
+ * For direct dispatch, we create a disapatch-branch key map
+ * which is used to determine which branch to parse at runtime.
+ *
+ * In the unparser case, we know which element we got from the infoset, but we
+ * need to determine which branch of the choice to take at runtime. This
+ * unparser uses a Map to make the determination based on the element seen.
+ */
+case class ChoiceCombinator(ch: ChoiceTermBase, rawAlternatives: Seq[Gram])
+  extends Terminal(ch, !rawAlternatives.isEmpty) {
+
+  private lazy val alternatives = rawAlternatives.filterNot(_.isEmpty)
+
+  private lazy val parsers = alternatives.map { _.parser }.filterNot { 
_.isEmpty }
+
+  override def isEmpty = super.isEmpty || alternatives.isEmpty
+
+  lazy val parser: Parser = {
+    if (!ch.isDirectDispatch) {
+      new ChoiceParser(ch.termRuntimeData, parsers)
+    } else {
+      val dispatchBranchKeyValueTuples = alternatives.flatMap { alt =>
+        val keyTerm = alt.context.asInstanceOf[Term]
+        val cbk = keyTerm.choiceBranchKey
+        // Note that this behaves differently than the specification, since
+        // this accepts a space separated list of keys
+        val cbks = ChoiceBranchKeyCooker.convertConstant(cbk, 
keyTerm.runtimeData, forUnparse = false)
+        cbks.map { (_, alt) }
+      }
+
+      // check for duplicate dfdl:choiceBranchKeys
+      val groupedByKey = dispatchBranchKeyValueTuples.groupBy(_._1)
+      groupedByKey.foreach {
+        case (k, kvs) =>
+          if (kvs.length > 1) {
+            SDE("dfdl:choiceBranchKey value (%s) is not unique across all 
branches of a direct dispatch choice. Offending branches are:\n%s",
+              k, kvs.map(_._2.context.runtimeData).map(rd => 
rd.diagnosticDebugName + " " + 
rd.schemaFileLocation.locationDescription).mkString("- ", "\n- ", ""))
+          }
+      }
+
+      val dispatchBranchKeyMap = 
dispatchBranchKeyValueTuples.toMap.mapValues(_.parser)
+      val serializableMap = dispatchBranchKeyMap.map(identity)
+
+      new ChoiceDispatchCombinatorParser(ch.termRuntimeData, 
ch.choiceDispatchKeyEv, serializableMap)
+    }
+  }
+
+  override lazy val unparser: Unparser = {
+    if (!ch.isHidden) {
+      val eventRDMap = ch.choiceBranchMap
+      val eventUnparserMap = eventRDMap.flatMap {
+        case (cbe, rd) =>
+          // if we don't find a matching RD for a term that's probably
+          // because the term is an empty sequence or empty choice (which do 
happen
+          // and we even have tests for them). Since those can never be chosen 
by
+          // means of an element event, they don't appear in the map.
+          val altGram = alternatives.find { alt =>
+            val crd = alt.context.runtimeData
+            val found = crd =:= rd
+            found
+          }
+          altGram.map { ag => (cbe, ag.unparser) }
+      }
+      val mapValues = eventUnparserMap.map { case (k, v) => v 
}.toSeq.filterNot(_.isEmpty)
+      if (mapValues.isEmpty)
 
 Review comment:
   Done

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to