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

 ##########
 File path: 
daffodil-core/src/main/scala/org/apache/daffodil/grammar/primitives/SequenceChild.scala
 ##########
 @@ -0,0 +1,223 @@
+/*
+ * 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._
+import org.apache.daffodil.dsom._
+import org.apache.daffodil.processors.unparsers._
+import org.apache.daffodil.processors.parsers._
+import org.apache.daffodil.dpath.NodeInfo
+import org.apache.daffodil.exceptions.Assert
+import org.apache.daffodil.schema.annotation.props.gen.OccursCountKind
+import org.apache.daffodil.schema.annotation.props.SeparatorSuppressionPolicy
+import org.apache.daffodil.schema.annotation.props.gen.LengthKind
+import org.apache.daffodil.schema.annotation.props.gen.Representation
+
+abstract class SequenceChild(
+  protected val sq: SequenceTermBase, child: Term, groupIndex: Int)
+  extends Terminal(child, true) {
+
+  protected def childParser = child.termContentBody.parser
+  protected def childUnparser = child.termContentBody.unparser
+
+  final override def parser = sequenceChildParser
+  final override def unparser = sequenceChildUnparser
+
+  protected def sequenceChildParser: SequenceChildParser
+  protected def sequenceChildUnparser: SequenceChildUnparser
+
+  def optSequenceChildParser: Option[SequenceChildParser] =
+    if (childParser.isEmpty) None else Some(parser)
+
+  def optSequenceChildUnparser: Option[SequenceChildUnparser] =
+    if (childUnparser.isEmpty) None else Some(unparser)
+
+  protected lazy val sepGram = sq.sequenceSeparator
+  protected lazy val sepParser = sepGram.parser
+  protected lazy val sepUnparser = sepGram.unparser
+
+  lazy val srd = sq.sequenceRuntimeData
+  lazy val trd = child.termRuntimeData
+
+  /**
+   * Used by unparsing algorithms that involve separator suppression for
+   * zero-length data.
+   */
+  final lazy val zeroLengthDetector: ZeroLengthDetector = {
+    val result: ZeroLengthDetector = child match {
+      case e: ElementBase => {
+        Assert.invariant(e.isRepresented)
+        import LengthKind._
+
+        lazy val couldBeZLSimpleType =
+          !e.hasDelimiters &&
+            e.isSimpleType &&
+            (e.lengthKind match {
+              case Delimited | EndOfParent => true
+              //
+              // When parsing, the pattern might not match zero-length data, 
but
+              // when unparsing we just output the data as it appears in the 
infoset.
+              // We don't check that it satisfies the pattern. So a zero-length
+              // string in the infoset will be zero-length when output.
+              //
+              case Pattern => true
+              case Explicit | Implicit => e.hasFixedLengthOf(0)
+              case Prefixed => false
+            }) &&
+            //
+            // Alignment stuff - can't be any aligning possible or we'd have
+            // to do a runtime check for whether things are aligned or not.
+            // In the unparser, that is a suspendable operation, since we may
+            // not know the bit position.
+            //
+            // Separated sequences where there's any alignment possibly needed
+            // are an obscure corner case we don't need to handle.
+            //
+            e.hasNoSkipRegions &&
+            (((e.impliedRepresentation eq Representation.Text) && 
e.hasTextAlignment) ||
+              // binary data is allowed to be delimited... so long as the
+              // separators are recognizably not confused with binary bytes.
+              ((e.impliedRepresentation eq Representation.Binary) && 
e.isKnownToBeAligned))
+
+        val hasNilZL = (e.isNillable && !e.hasNilValueRequiredSyntax)
+        val hasEmptyZL = (e.emptyIsAnObservableConcept && 
e.hasEmptyValueZLSyntax)
+        val hasStringZL = e.isSimpleType && (e.simpleType.primType eq 
NodeInfo.String) && (couldBeZLSimpleType || hasEmptyZL)
+        val hasHexBinaryZL = e.isSimpleType && (e.simpleType.primType eq 
NodeInfo.HexBinary) && (couldBeZLSimpleType || hasEmptyZL)
+        (hasNilZL, hasStringZL, hasHexBinaryZL) match {
+          case (true, false, false) => new NillableZeroLengthDetector
+          case (false, true, false) => new StringZeroLengthDetector
+          case (false, false, true) => new HexBinaryZeroLengthDetector
+          case (true, true, false) => new NillableStringZeroLengthDetector
+          case (true, false, true) => new NillableHexBinaryZeroLengthDetector
+          case (_, true, true) => Assert.invariantFailed("Can't be both String 
and HexBinary type")
+          case _ => new NeverZeroLengthDetector
+        }
+      }
+      case m: ModelGroup => {
+        new NeverZeroLengthDetector
+      }
+    }
+    result
+  }
+
+  /**
+   * Used when unparsing to determine which of several separator suppression
+   * algorithms is required.
+   *
+   * Relevant only if the enclosing sequence is separated.
+   */
+  final lazy val separatorSuppressionAlgorithm: SeparatorSuppressionAlgorithm 
= {
 
 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