From 7e7bd04b20cb61fa050178cd08b7d25542dec1d6 Mon Sep 17 00:00:00 2001 From: olabusayoT <50379531+olabusayoT@users.noreply.github.com> Date: Fri, 3 Apr 2026 14:40:43 -0400 Subject: [PATCH 1/3] Add support for `dfdl:lengthKind="endOfParent"` - Implemented logic to handle elements with `dfdl:lengthKind="endOfParent"`. - Added validations to enforce schema/spec constraints and error conditions specific to `endOfParent` lengthKind. - Introduced determination of effective length units for parent elements and related error checks. - removed NYI Errors - add tests for endOfParent elements with different LengthKinds incl nested EndOfParent DAFFODIL-238 --- .../core/dsom/LocalElementMixin.scala | 6 +- .../org/apache/daffodil/core/dsom/Term.scala | 20 + .../daffodil/core/grammar/AlignedMixin.scala | 6 +- .../grammar/ElementBaseGrammarMixin.scala | 157 +- .../primitives/PrimitivesLengthKind.scala | 10 + .../grammar/primitives/SpecifiedLength.scala | 24 + .../parsers/HexBinaryLengthParsers.scala | 7 +- ...yNumberTraits.scala => ParserTraits.scala} | 0 .../parsers/SpecifiedLengthParsers.scala | 11 + .../runtime1/SpecifiedLengthUnparsers.scala | 14 + .../section12/aligned_data/Aligned_Data.tdml | 79 + .../lengthKind/EndOfParentTests.tdml | 1965 ++++++++++++++++- .../aligned_data/TestAlignedData.scala | 4 + .../TestLengthKindEndOfParent.scala | 74 + .../TestLengthKindEndOfParent2.scala | 34 - 15 files changed, 2319 insertions(+), 92 deletions(-) rename daffodil-core/src/main/scala/org/apache/daffodil/runtime1/processors/parsers/{BinaryNumberTraits.scala => ParserTraits.scala} (100%) create mode 100644 daffodil-test/src/test/scala/org/apache/daffodil/section12/lengthKind/TestLengthKindEndOfParent.scala delete mode 100644 daffodil-test/src/test/scala/org/apache/daffodil/section12/lengthKind/TestLengthKindEndOfParent2.scala diff --git a/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/LocalElementMixin.scala b/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/LocalElementMixin.scala index 0e9aaf57e9..4756afcc0f 100644 --- a/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/LocalElementMixin.scala +++ b/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/LocalElementMixin.scala @@ -61,10 +61,8 @@ trait LocalElementMixin extends ParticleMixin with LocalElementGrammarMixin { else if (representation =:= Representation.Binary) true else false } - case LengthKind.EndOfParent if isComplexType => - notYetImplemented("lengthKind='endOfParent' for complex type") - case LengthKind.EndOfParent => - notYetImplemented("lengthKind='endOfParent' for simple type") + // per DFDL Spec 9.3.2, endOfParent is already positioned at parent's end so length is zero + case LengthKind.EndOfParent => false } res }.value diff --git a/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/Term.scala b/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/Term.scala index 7558483d5b..4e6495b3d1 100644 --- a/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/Term.scala +++ b/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/Term.scala @@ -269,6 +269,26 @@ trait Term .getOrElse(false) } + final lazy val immediatelyEnclosingElementParent: Option[ElementBase] = { + val p = optLexicalParent.flatMap { + case e: ElementBase => Some(e) + case ge: GlobalElementDecl => Some(ge.asRoot) + case s: SequenceTermBase => s.immediatelyEnclosingElementParent + case c: ChoiceTermBase => c.immediatelyEnclosingElementParent + case ct: ComplexTypeBase => { + ct.optLexicalParent.flatMap { + case e: ElementBase => Some(e) + case ge: GlobalElementDecl => Some(ge.asRoot) + case _ => { + None + } + } + } + case _ => None + } + p + } + final lazy val immediatelyEnclosingGroupDef: Option[GroupDefLike] = { optLexicalParent.flatMap { lexicalParent => val res: Option[GroupDefLike] = lexicalParent match { diff --git a/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/AlignedMixin.scala b/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/AlignedMixin.scala index 00ebdee130..63c7936eb3 100644 --- a/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/AlignedMixin.scala +++ b/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/AlignedMixin.scala @@ -456,7 +456,11 @@ trait AlignedMixin extends GrammarMixin { self: Term => } case LengthKind.Delimited => encodingLengthApprox case LengthKind.Pattern => encodingLengthApprox - case LengthKind.EndOfParent => LengthMultipleOf(1) // NYI + case LengthKind.EndOfParent => + eb.immediatelyEnclosingElementParent match { + case Some(parent) => parent.elementSpecifiedLengthApprox + case _ => LengthMultipleOf(1) + } // If an element is lengthKind="prefixed", the element's length is the length // of the value of the prefix element, which can't be known till runtime case LengthKind.Prefixed => LengthMultipleOf(1) // NYI (see DAFFODIL-3066) diff --git a/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/ElementBaseGrammarMixin.scala b/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/ElementBaseGrammarMixin.scala index 3a33fdfb07..011f323b23 100644 --- a/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/ElementBaseGrammarMixin.scala +++ b/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/ElementBaseGrammarMixin.scala @@ -19,10 +19,14 @@ package org.apache.daffodil.core.grammar import java.lang.Long as JLong +import org.apache.daffodil.core.dsom.ChoiceTermBase import org.apache.daffodil.core.dsom.ElementBase import org.apache.daffodil.core.dsom.ExpressionCompilers import org.apache.daffodil.core.dsom.InitiatedTerminatedMixin +import org.apache.daffodil.core.dsom.ModelGroup import org.apache.daffodil.core.dsom.PrefixLengthQuasiElementDecl +import org.apache.daffodil.core.dsom.Root +import org.apache.daffodil.core.dsom.SequenceTermBase import org.apache.daffodil.core.grammar.primitives.* import org.apache.daffodil.core.runtime1.ElementBaseRuntime1Mixin import org.apache.daffodil.lib.exceptions.Assert @@ -52,6 +56,7 @@ trait ElementBaseGrammarMixin requiredEvaluationsIfActivated(checkPrefixedLengthElementDecl) requiredEvaluationsIfActivated(checkDelimitedLengthEVDP) + requiredEvaluationsIfActivated(checkEndOfParentElem) private val context = this @@ -252,6 +257,134 @@ trait ElementBaseGrammarMixin } final lazy val prefixedLengthBody = prefixedLengthElementDecl.parsedValue + final lazy val parentEffectiveLengthUnits: LengthUnits = + immediatelyEnclosingElementParent match { + case Some(parent: ElementBase) => { + parent.lengthKind match { + case LengthKind.Explicit | LengthKind.Prefixed => parent.lengthUnits + case LengthKind.Pattern => LengthUnits.Characters + case _ + if parent.isInstanceOf[ChoiceTermBase] && (parent + .asInstanceOf[ChoiceTermBase] + .choiceLengthKind == ChoiceLengthKind.Explicit) => + LengthUnits.Bytes + case LengthKind.EndOfParent => parent.parentEffectiveLengthUnits + case _ => + Assert.invariantFailed( + s"Could not figure effective length unit of parents of ${context}" + ) + } + } + case None if this.isInstanceOf[Root] => LengthUnits.Characters + case _ => + Assert.invariantFailed( + s"Could not figure effective length unit of parents of ${context}" + ) + } + final lazy val checkEndOfParentElem: Unit = { + if (lengthKind != LengthKind.EndOfParent) () + else { + schemaDefinitionWhen( + hasTerminator, + "%s is specified as dfdl:lengthKind=\"endOfParent\", but specifies a dfdl:terminator.", + context + ) + schemaDefinitionWhen( + trailingSkip != 0, + "%s is specified as dfdl:lengthKind=\"endOfParent\", but specifies a non-zero dfdl:trailingSkip.", + context + ) + schemaDefinitionWhen( + maxOccurs > 1, + "%s is specified as dfdl:lengthKind=\"endOfParent\", but specifies a maxOccurs greater than 1.", + context + ) + schemaDefinitionWhen( + nextSibling.isDefined && nextSibling.get.isInstanceOf[ModelGroup], + "%s is specified as dfdl:lengthKind=\"endOfParent\", but a model group is defined between this element and the end of the enclosing component", + context + ) + schemaDefinitionWhen( + nextSibling.isDefined && nextSibling.get.isRepresented, + "%s is specified as dfdl:lengthKind=\"endOfParent\", but a represented element is defined between this element and the end of the enclosing component", + context + ) + immediatelyEnclosingElementParent match { + case Some(parent: ElementBase) => + parent.lengthKind match { + case LengthKind.Implicit | LengthKind.Delimited => + schemaDefinitionError( + "%s is specified as dfdl:lengthKind=\"endOfParent\", but its parent is an element with dfdl:lengthKind 'implicit' or 'delimited'.", + context + ) + case _ => // do nothing + } + case _ => // do nothing + } + schemaDefinitionWhen( + representation == Representation.Text && knownEncodingWidthInBits != 8 && parentEffectiveLengthUnits != LengthUnits.Characters, + "%s is specified as dfdl:lengthKind=\"endOfParent\", but the element has text representation, does not have a single-byte character set encoding, and the effective length units of the parent is not 'characters'.", + context + ) + + immediatelyEnclosingModelGroup match { + case Some(s: SequenceTermBase) => { + schemaDefinitionWhen( + s.separatorPosition == SeparatorPosition.Postfix, + "%s is specified as dfdl:lengthKind=\"endOfParent\", but is in a sequence with dfdl:separatorPosition defined as 'postfix'.", + context + ) + schemaDefinitionWhen( + s.sequenceKind != SequenceKind.Ordered, + "%s is specified as dfdl:lengthKind=\"endOfParent\", but is in a sequence with dfdl:sequenceKind defined as 'unordered'.", + context + ) + schemaDefinitionWhen( + s.hasTerminator, + "%s is specified as dfdl:lengthKind=\"endOfParent\", but is in a sequence with a dfdl:terminator.", + context + ) + schemaDefinitionWhen( + s.elementChildren.exists(e => e.floating == YesNo.Yes), + "%s is specified as dfdl:lengthKind=\"endOfParent\", but is in a sequence with elements defining dfdl:floating='yes'.", + context + ) + schemaDefinitionWhen( + s.trailingSkip != 0, + "%s is specified as dfdl:lengthKind=\"endOfParent\", but is in a sequence with a non-zero dfdl:trailingSkip." + ) + } + case Some(c: ChoiceTermBase) if c.choiceLengthKind == ChoiceLengthKind.Implicit => { + schemaDefinitionWhen( + c.hasTerminator, + "%s is specified as dfdl:lengthKind=\"endOfParent\", but is in a choice with a dfdl:terminator.", + context + ) + schemaDefinitionWhen( + c.trailingSkip != 0, + "%s is specified as dfdl:lengthKind=\"endOfParent\", but is in a choice with a non-zero dfdl:trailingSkip.", + context + ) + } + case _ => // do nothing + } + + if (isSimpleType) { + schemaDefinitionUnless( + (primType eq PrimType.String) + || (representation == Representation.Text) + || (primType eq PrimType.HexBinary) + || (representation == Representation.Binary + && Seq(BinaryNumberRep.Packed, BinaryNumberRep.Bcd, BinaryNumberRep.Ibm4690Packed) + .contains(binaryNumberRep)), + "%s is a simple type specified as dfdl:lengthKind=\"endOfParent\", but isn't a string type, doesn't have text representation, isn't a hexbinary type, or doesn't have binary representation with packed decimal representation.", + context + ) + } + + } + } + /** * Quite tricky when we add padding or fill * @@ -648,10 +781,7 @@ trait ElementBaseGrammarMixin Assert.invariant(pt == PrimType.String) StringOfSpecifiedLength(this) } - case LengthKind.EndOfParent if isComplexType => - notYetImplemented("lengthKind='endOfParent' for complex type") - case LengthKind.EndOfParent => - notYetImplemented("lengthKind='endOfParent' for simple type") + case LengthKind.EndOfParent => StringOfSpecifiedLength(this) } } @@ -667,6 +797,10 @@ trait ElementBaseGrammarMixin new HexBinaryLengthPrefixed(this) } + private lazy val hexBinaryLengthEndOfParent = prod("hexBinaryLengthEndOfParent") { + new HexBinaryLengthEndOfParent(this) + } + private lazy val hexBinaryValue = prod("hexBinaryValue") { schemaDefinitionWhen( lengthUnits == LengthUnits.Characters, @@ -678,10 +812,7 @@ trait ElementBaseGrammarMixin case LengthKind.Delimited => hexBinaryDelimitedEndOfData case LengthKind.Pattern => hexBinaryLengthPattern case LengthKind.Prefixed => hexBinaryLengthPrefixed - case LengthKind.EndOfParent if isComplexType => - notYetImplemented("lengthKind='endOfParent' for complex type") - case LengthKind.EndOfParent => - notYetImplemented("lengthKind='endOfParent' for simple type") + case LengthKind.EndOfParent => hexBinaryLengthEndOfParent } } @@ -1280,10 +1411,7 @@ trait ElementBaseGrammarMixin case LengthKind.Implicit => LiteralValueNilOfSpecifiedLength(this) case LengthKind.Prefixed => LiteralValueNilOfSpecifiedLength(this) - case LengthKind.EndOfParent if isComplexType => - notYetImplemented("lengthKind='endOfParent' for complex type") - case LengthKind.EndOfParent => - notYetImplemented("lengthKind='endOfParent' for simple type") + case LengthKind.EndOfParent => LiteralValueNilOfSpecifiedLength(this) } } case NilKind.LiteralCharacter => { @@ -1396,10 +1524,7 @@ trait ElementBaseGrammarMixin case LengthKind.Implicit if isSimpleType && impliedRepresentation == Representation.Binary => new SpecifiedLengthImplicit(this, body, implicitBinaryLengthInBits) - case LengthKind.EndOfParent if isComplexType => - notYetImplemented("lengthKind='endOfParent' for complex type") - case LengthKind.EndOfParent => - notYetImplemented("lengthKind='endOfParent' for simple type") + case LengthKind.EndOfParent => new SpecifiedLengthEndOfParent(this, body) case LengthKind.Delimited | LengthKind.Implicit => Assert.impossibleCase( "Delimited and ComplexType Implicit cases should not be reached" diff --git a/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/primitives/PrimitivesLengthKind.scala b/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/primitives/PrimitivesLengthKind.scala index e069353bd5..fcda6161f8 100644 --- a/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/primitives/PrimitivesLengthKind.scala +++ b/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/primitives/PrimitivesLengthKind.scala @@ -191,6 +191,16 @@ case class HexBinaryLengthPrefixed(e: ElementBase) extends Terminal(e, true) { new HexBinaryMinLengthInBytesUnparser(e.minLength.longValue, e.elementRuntimeData) } +case class HexBinaryLengthEndOfParent(e: ElementBase) extends Terminal(e, true) { + + override lazy val parser: DaffodilParser = new HexBinaryEndOfBitLimitParser( + e.elementRuntimeData + ) + + override lazy val unparser: DaffodilUnparser = + new HexBinaryMinLengthInBytesUnparser(e.minLength.longValue, e.elementRuntimeData) +} + abstract class PackedIntegerDelimited( e: ElementBase, packedSignCodes: PackedSignCodes diff --git a/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/primitives/SpecifiedLength.scala b/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/primitives/SpecifiedLength.scala index 0ae3e6a4d9..49baa728c2 100644 --- a/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/primitives/SpecifiedLength.scala +++ b/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/primitives/SpecifiedLength.scala @@ -132,6 +132,30 @@ class SpecifiedLengthExplicit(e: ElementBase, eGram: => Gram, bitsMultiplier: In } +class SpecifiedLengthEndOfParent(e: ElementBase, eGram: => Gram) + extends SpecifiedLengthCombinatorBase(e, eGram) { + + lazy val kind = "EndOfParent_" + e.lengthUnits.toString + + lazy val parser: Parser = { + if (eParser.isEmpty) eParser + else + new SpecifiedLengthEndOfParentParser( + eParser, + e.elementRuntimeData + ) + } + + lazy val unparser: Unparser = { + if (eUnparser.isEmpty) eUnparser + else + new SpecifiedLengthEndOfParentUnparser( + eUnparser, + e.elementRuntimeData + ) + } +} + class SpecifiedLengthImplicit(e: ElementBase, eGram: => Gram, nBits: Long) extends SpecifiedLengthCombinatorBase(e, eGram) with SpecifiedLengthExplicitImplicitUnparserMixin { diff --git a/daffodil-core/src/main/scala/org/apache/daffodil/runtime1/processors/parsers/HexBinaryLengthParsers.scala b/daffodil-core/src/main/scala/org/apache/daffodil/runtime1/processors/parsers/HexBinaryLengthParsers.scala index 7c9f50efca..0dc7b21996 100644 --- a/daffodil-core/src/main/scala/org/apache/daffodil/runtime1/processors/parsers/HexBinaryLengthParsers.scala +++ b/daffodil-core/src/main/scala/org/apache/daffodil/runtime1/processors/parsers/HexBinaryLengthParsers.scala @@ -84,11 +84,8 @@ final class HexBinarySpecifiedLengthParser(erd: ElementRuntimeData, lengthEv: Le } final class HexBinaryEndOfBitLimitParser(erd: ElementRuntimeData) - extends HexBinaryLengthParser(erd) { + extends HexBinaryLengthParser(erd), + BitLengthFromBitLimitMixin { override def runtimeDependencies = Vector() - - override def getLengthInBits(pstate: PState): Long = { - pstate.bitLimit0b.get - pstate.bitPos0b - } } diff --git a/daffodil-core/src/main/scala/org/apache/daffodil/runtime1/processors/parsers/BinaryNumberTraits.scala b/daffodil-core/src/main/scala/org/apache/daffodil/runtime1/processors/parsers/ParserTraits.scala similarity index 100% rename from daffodil-core/src/main/scala/org/apache/daffodil/runtime1/processors/parsers/BinaryNumberTraits.scala rename to daffodil-core/src/main/scala/org/apache/daffodil/runtime1/processors/parsers/ParserTraits.scala diff --git a/daffodil-core/src/main/scala/org/apache/daffodil/runtime1/processors/parsers/SpecifiedLengthParsers.scala b/daffodil-core/src/main/scala/org/apache/daffodil/runtime1/processors/parsers/SpecifiedLengthParsers.scala index 2f3ee1f5dd..1d2bb11521 100644 --- a/daffodil-core/src/main/scala/org/apache/daffodil/runtime1/processors/parsers/SpecifiedLengthParsers.scala +++ b/daffodil-core/src/main/scala/org/apache/daffodil/runtime1/processors/parsers/SpecifiedLengthParsers.scala @@ -132,6 +132,17 @@ class SpecifiedLengthPatternParser( } } +class SpecifiedLengthEndOfParentParser( + eParser: Parser, + erd: ElementRuntimeData +) extends SpecifiedLengthParserBase(eParser, erd), + BitLengthFromBitLimitMixin { + + override protected def getBitLength(s: PState): MaybeULong = { + MaybeULong(super[BitLengthFromBitLimitMixin].getBitLength(s)) + } +} + class SpecifiedLengthExplicitParser( eParser: Parser, erd: ElementRuntimeData, diff --git a/daffodil-core/src/main/scala/org/apache/daffodil/unparsers/runtime1/SpecifiedLengthUnparsers.scala b/daffodil-core/src/main/scala/org/apache/daffodil/unparsers/runtime1/SpecifiedLengthUnparsers.scala index 44af8e887a..071c810a04 100644 --- a/daffodil-core/src/main/scala/org/apache/daffodil/unparsers/runtime1/SpecifiedLengthUnparsers.scala +++ b/daffodil-core/src/main/scala/org/apache/daffodil/unparsers/runtime1/SpecifiedLengthUnparsers.scala @@ -72,6 +72,20 @@ final class SpecifiedLengthExplicitImplicitUnparser( } } +final class SpecifiedLengthEndOfParentUnparser( + eUnparser: Unparser, + erd: ElementRuntimeData +) extends CombinatorUnparser(erd) { + + override def runtimeDependencies = Vector() + + override def childProcessors = Vector(eUnparser) + + override final def unparse(state: UState): Unit = { + eUnparser.unparse1(state) + } +} + /** * This trait is to be used with prefixed length unparsers where the length * must be calculated based on the content length of the data. This means the diff --git a/daffodil-test/src/test/resources/org/apache/daffodil/section12/aligned_data/Aligned_Data.tdml b/daffodil-test/src/test/resources/org/apache/daffodil/section12/aligned_data/Aligned_Data.tdml index c2be97ef3f..e97a545c6b 100644 --- a/daffodil-test/src/test/resources/org/apache/daffodil/section12/aligned_data/Aligned_Data.tdml +++ b/daffodil-test/src/test/resources/org/apache/daffodil/section12/aligned_data/Aligned_Data.tdml @@ -639,6 +639,28 @@ + + + + + + + + + + + + + + + @@ -664,6 +686,30 @@ + + + + + + + + + + + + + + + + @@ -4357,6 +4403,21 @@ + + IaaBbb + + + + + + aa + bb + + + + + + IAaabbee @@ -4373,6 +4434,22 @@ + + IAaabbee + + + + + + aa + bb + + ee + + + + + aaTBbb @@ -4613,4 +4690,6 @@ + + diff --git a/daffodil-test/src/test/resources/org/apache/daffodil/section12/lengthKind/EndOfParentTests.tdml b/daffodil-test/src/test/resources/org/apache/daffodil/section12/lengthKind/EndOfParentTests.tdml index fdbcc2e32d..29aa3214f0 100644 --- a/daffodil-test/src/test/resources/org/apache/daffodil/section12/lengthKind/EndOfParentTests.tdml +++ b/daffodil-test/src/test/resources/org/apache/daffodil/section12/lengthKind/EndOfParentTests.tdml @@ -20,9 +20,9 @@ description="Section 12 - lengthKind=endOfParent" xmlns:tdml="http://www.ibm.com/xmlns/dfdl/testData" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dfdl="http://www.ogf.org/dfdl/dfdl-1.0/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" - xmlns:ex="http://example.com" defaultRoundTrip="true"> + xmlns:ex="http://example.com" defaultRoundTrip="onePass"> - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Schema Definition Error + endOfParent + but its parent + lengthKind + delimited + + + + + + + Schema Definition Error + endOfParent + but its parent + lengthKind + delimited + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Schema Definition Error + endOfParent + but its parent + lengthKind + implicit + + + + + + + Schema Definition Error + endOfParent + but its parent + lengthKind + implicit + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1 + 1 + + + + 2 + 2 + + + 3 + 3 + + + + A + + + + + + + + + + + + X + YZ + + A + + + + + + + + + - + + + + + + + + + + + - - + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1 + 1 + + + + 2 + 2 + + + 3 + 3 + + + + A + + + + + + + + + + + + X + YZ + + A + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1 + 1 + + + + 2 + 2 + + + 3 + 3 + + + + A + + + + + + + + + + + + X + YZ + + A + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1 + 1 + + + + 2 + 2 + + + 3 + 3 + + + + + + + + + + + + + + + X + YZ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + endOfParent + represented element + between this element and + end of the enclosing component + + + + + + + endOfParent + represented element + between this element and + end of the enclosing component + + + + - + + + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + - Schema Definition Error - not - implemented endOfParent - complex - type + model group + between this element and + end of the enclosing component - - + + - Schema Definition Error - not - implemented endOfParent - simple - type + model group + between this element and + end of the enclosing component + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1 + 1 + + + + 2 + 2 + + + 3 + 3 + + + A + + + + + + + + + + + + + X + YZ + A + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + endOfParent + specifies + terminator + + + + + + + endOfParent + specifies + terminator + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + endOfParent + specifies + non-zero + trailingSkip + + + + + + + endOfParent + specifies + non-zero + trailingSkip + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + endOfParent + specifies + maxOccurs + greater than 1 + + + + + + + endOfParent + specifies + maxOccurs + greater than 1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + endOfParent + does not have + single-byte character set encoding + + + + + + + endOfParent + does not have + single-byte character set encoding + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + endOfParent + in a sequence with + separatorPosition + postfix + + + + + + + endOfParent + in a sequence with + separatorPosition + postfix + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + endOfParent + in a sequence with + sequenceKind + unordered + + + + + + + endOfParent + in a sequence with + sequenceKind + unordered + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + endOfParent + in a sequence with + elements + floating='yes + + + + + + + endOfParent + in a sequence with + elements + floating='yes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + endOfParent + in a sequence with + non-zero + trailingSkip + + + + + + + endOfParent + in a sequence with + non-zero + trailingSkip + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + endOfParent + in a choice with + non-zero + trailingSkip + + + + + + + endOfParent + in a choice with + non-zero + trailingSkip + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + endOfParent + in a choice with + terminator + + + + + + + endOfParent + in a choice with + terminator + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + simple type + endOfParent + isn't a string type + + + + + + + simple type + endOfParent + doesn't have binary representation with packed decimal representation + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + X + 3132 + + + + + + + + + + + + + X + 12 + + + + + + + diff --git a/daffodil-test/src/test/scala/org/apache/daffodil/section12/aligned_data/TestAlignedData.scala b/daffodil-test/src/test/scala/org/apache/daffodil/section12/aligned_data/TestAlignedData.scala index 3c4de0add4..ad587d2a09 100644 --- a/daffodil-test/src/test/scala/org/apache/daffodil/section12/aligned_data/TestAlignedData.scala +++ b/daffodil-test/src/test/scala/org/apache/daffodil/section12/aligned_data/TestAlignedData.scala @@ -209,6 +209,10 @@ class TestAlignedData extends TdmlTests { @Test def prior_siblings_3 = test @Test def prior_siblings_4 = test @Test def prior_siblings_5 = test + + // DAFFODIL-238 + @Test def test_init_alignment_1_eop = test + @Test def test_init_alignment_2_eop = test } class TestBinaryInput extends TdmlTests { diff --git a/daffodil-test/src/test/scala/org/apache/daffodil/section12/lengthKind/TestLengthKindEndOfParent.scala b/daffodil-test/src/test/scala/org/apache/daffodil/section12/lengthKind/TestLengthKindEndOfParent.scala new file mode 100644 index 0000000000..53f25c3720 --- /dev/null +++ b/daffodil-test/src/test/scala/org/apache/daffodil/section12/lengthKind/TestLengthKindEndOfParent.scala @@ -0,0 +1,74 @@ +/* + * 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.section12.lengthKind + +import org.apache.daffodil.junit.tdml.TdmlSuite +import org.apache.daffodil.junit.tdml.TdmlTests + +import org.junit.Test + +object TestLengthKindEndOfParent extends TdmlSuite { + val tdmlResource = "/org/apache/daffodil/section12/lengthKind/EndOfParentTests.tdml" +} + +class TestLengthKindEndOfParent extends TdmlTests { + val tdmlSuite = TestLengthKindEndOfParent + + @Test def TestEndOfParentComplexTypesDelimited = test + @Test def TestEndOfParentSimpleTypesDelimited = test + @Test def TestEndOfParentComplexTypesImplicit = test + @Test def TestEndOfParentSimpleTypesImplicit = test + @Test def TestEndOfParentComplexTypesExplicit = test + @Test def TestEndOfParentSimpleTypesExplicit = test + @Test def TestEndOfParentComplexTypesPrefixed = test + @Test def TestEndOfParentSimpleTypesPrefixed = test + @Test def TestEndOfParentComplexTypesPattern = test + @Test def TestEndOfParentSimpleTypesPattern = test + @Test def TestEndOfParentComplexTypesEOP = test + @Test def TestEndOfParentSimpleTypesEOP = test + @Test def TestEndOfParentComplexTypes1 = test + @Test def TestEndOfParentSimpleTypes1 = test + @Test def TestEndOfParentComplexTypes2 = test + @Test def TestEndOfParentSimpleTypes2 = test + @Test def TestEndOfParentComplexTypes3 = test + @Test def TestEndOfParentSimpleTypes3 = test + @Test def TestEndOfParentComplexTypes4 = test + @Test def TestEndOfParentSimpleTypes4 = test + @Test def TestEndOfParentComplexTypes5 = test + @Test def TestEndOfParentSimpleTypes5 = test + @Test def TestEndOfParentComplexTypes6 = test + @Test def TestEndOfParentSimpleTypes6 = test + @Test def TestEndOfParentComplexTypes7 = test + @Test def TestEndOfParentSimpleTypes7 = test + @Test def TestEndOfParentComplexTypes8 = test + @Test def TestEndOfParentSimpleTypes8 = test + @Test def TestEndOfParentComplexTypes9 = test + @Test def TestEndOfParentSimpleTypes9 = test + @Test def TestEndOfParentComplexTypes10 = test + @Test def TestEndOfParentSimpleTypes10 = test + @Test def TestEndOfParentComplexTypes11 = test + @Test def TestEndOfParentSimpleTypes11 = test + @Test def TestEndOfParentComplexTypes12 = test + @Test def TestEndOfParentSimpleTypes12 = test + @Test def TestEndOfParentComplexTypes13 = test + @Test def TestEndOfParentSimpleTypes13 = test + @Test def TestEndOfParentSimpleTypes14 = test + @Test def TestEndOfParentSimpleTypes15 = test + @Test def TestEndOfParentSimpleTypes16 = test + @Test def TestEndOfParentSimpleTypes17 = test +} diff --git a/daffodil-test/src/test/scala/org/apache/daffodil/section12/lengthKind/TestLengthKindEndOfParent2.scala b/daffodil-test/src/test/scala/org/apache/daffodil/section12/lengthKind/TestLengthKindEndOfParent2.scala deleted file mode 100644 index 5928ed5f71..0000000000 --- a/daffodil-test/src/test/scala/org/apache/daffodil/section12/lengthKind/TestLengthKindEndOfParent2.scala +++ /dev/null @@ -1,34 +0,0 @@ -/* - * 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.section12.lengthKind - -import org.apache.daffodil.junit.tdml.TdmlSuite -import org.apache.daffodil.junit.tdml.TdmlTests - -import org.junit.Test - -object TestLengthKindEndOfParent2 extends TdmlSuite { - val tdmlResource = "/org/apache/daffodil/section12/lengthKind/EndOfParentTests.tdml" -} - -class TestLengthKindEndOfParent2 extends TdmlTests { - val tdmlSuite = TestLengthKindEndOfParent2 - - @Test def TestEndOfParentNYIComplexTypes = test - @Test def TestEndOfParentNYISimpleTypes = test -} From df8100e79bf54c360a6674dddaffb597bd37956c Mon Sep 17 00:00:00 2001 From: olabusayoT <50379531+olabusayoT@users.noreply.github.com> Date: Thu, 7 May 2026 09:58:59 -0400 Subject: [PATCH 2/3] fixup! - Add checkEndOfParentRestriction to choice/sequence/elements - employ top-down approach looking from parents to their children instead of lexical child to parent approach - address checks for root elements - add support for EOP for packed decimals - add support for bytesTillEndOfDataStream for BBIS/BIS - update tests --- .../daffodil/core/dsom/ChoiceGroup.scala | 32 +- .../core/dsom/LocalElementMixin.scala | 2 +- .../apache/daffodil/core/dsom/SchemaSet.scala | 2 + .../daffodil/core/dsom/SequenceGroup.scala | 34 + .../org/apache/daffodil/core/dsom/Term.scala | 38 +- .../daffodil/core/grammar/AlignedMixin.scala | 8 +- .../grammar/ElementBaseGrammarMixin.scala | 288 +++-- .../grammar/primitives/PrimitivesBCD.scala | 30 +- .../primitives/PrimitivesBinaryBoolean.scala | 18 + .../primitives/PrimitivesIBM4690Packed.scala | 27 +- .../primitives/PrimitivesLengthKind.scala | 9 +- .../grammar/primitives/PrimitivesPacked.scala | 42 +- .../grammar/primitives/SpecifiedLength.scala | 10 +- .../org/apache/daffodil/io/InputSource.scala | 29 + .../io/InputSourceDataInputStream.scala | 2 + .../runtime1/processors/BCDParsers.scala | 9 +- .../IBM4690PackedDecimalParsers.scala | 9 +- .../processors/PackedDecimalParsers.scala | 10 +- .../parsers/BinaryBooleanParsers.scala | 5 +- .../parsers/HexBinaryLengthParsers.scala | 4 +- .../processors/parsers/ParserTraits.scala | 13 +- .../parsers/SpecifiedLengthParsers.scala | 2 +- .../runtime1/SpecifiedLengthUnparsers.scala | 14 - .../lengthKind/EndOfParentTests.tdml | 1035 ++++++++++++++++- .../daffodil/section13/packed/packed.tdml | 245 ++++ .../choice_groups/ChoiceLengthExplicit.tdml | 76 ++ .../TestLengthKindEndOfParent.scala | 47 +- .../section13/packed/TestPacked.scala | 14 + .../section15/choice_groups/TestChoice.scala | 2 + 29 files changed, 1840 insertions(+), 216 deletions(-) diff --git a/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/ChoiceGroup.scala b/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/ChoiceGroup.scala index 927bb38987..8af289b2b9 100644 --- a/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/ChoiceGroup.scala +++ b/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/ChoiceGroup.scala @@ -24,7 +24,9 @@ import org.apache.daffodil.core.dsom.walker.ChoiceView import org.apache.daffodil.core.grammar.ChoiceGrammarMixin import org.apache.daffodil.lib.schema.annotation.props.Found import org.apache.daffodil.lib.schema.annotation.props.gen.ChoiceAGMixin +import org.apache.daffodil.lib.schema.annotation.props.gen.ChoiceLengthKind import org.apache.daffodil.lib.schema.annotation.props.gen.Choice_AnnotationMixin +import org.apache.daffodil.lib.schema.annotation.props.gen.LengthUnits import org.apache.daffodil.lib.schema.annotation.props.gen.YesNo /** @@ -103,6 +105,7 @@ abstract class ChoiceTermBase( requiredEvaluationsIfActivated(noBranchesFound) requiredEvaluationsIfActivated(branchesAreNonOptional) requiredEvaluationsIfActivated(branchesAreNotIVCElements) + requiredEvaluationsIfActivated(checkEndOfParentRestrictions()) final protected lazy val optionChoiceDispatchKeyRaw = findPropertyOption("choiceDispatchKey", expressionAllowed = true) @@ -159,7 +162,7 @@ abstract class ChoiceTermBase( * Open issues: * 1) Is alignment or leading/trailing skip to be considered syntax. Alignment might not be there. * 2) What about an empty sequence that only carries statement annotations such as dfdl:assert or - * dfdl:setVariable + * dfdl:setVariable * * This latter need to be allowed, because while they do not have known required syntax they do * have to be executed for side-effect. @@ -189,6 +192,33 @@ abstract class ChoiceTermBase( } assuming(branchesOk.forall { x => x }) }.value + + lazy val optMyEffectiveLengthUnits: Option[LengthUnits] = + this match { + case self: ChoiceTermBase if self.choiceLengthKind == ChoiceLengthKind.Explicit => + Some(LengthUnits.Bytes) + case _ => None + } + + def checkEndOfParentRestrictions() = { + val parent = this + val eopChildren = this.childrenEndOfParent + if (eopChildren.isEmpty) {} else { + parent match { + case c: ChoiceTermBase if c.choiceLengthKind == ChoiceLengthKind.Implicit => { + schemaDefinitionWhen( + c.hasTerminator, + "element is specified as dfdl:lengthKind=\"endOfParent\", but is in a choice with a dfdl:terminator." + ) + schemaDefinitionWhen( + c.trailingSkip != 0, + "element is specified as dfdl:lengthKind=\"endOfParent\", but is in a choice with a non-zero dfdl:trailingSkip." + ) + } + case _ => // do nothing + } + } + } } object Choice { diff --git a/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/LocalElementMixin.scala b/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/LocalElementMixin.scala index 4756afcc0f..d00cbc2b3f 100644 --- a/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/LocalElementMixin.scala +++ b/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/LocalElementMixin.scala @@ -61,7 +61,7 @@ trait LocalElementMixin extends ParticleMixin with LocalElementGrammarMixin { else if (representation =:= Representation.Binary) true else false } - // per DFDL Spec 9.3.2, endOfParent is already positioned at parent's end so length is zero + // we can rarely statically know if an endOfParent element must have non-zero length case LengthKind.EndOfParent => false } res diff --git a/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/SchemaSet.scala b/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/SchemaSet.scala index 18a2483fac..ca76780db9 100644 --- a/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/SchemaSet.scala +++ b/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/SchemaSet.scala @@ -30,6 +30,7 @@ import org.apache.daffodil.lib.iapi.Diagnostic import org.apache.daffodil.lib.iapi.UnitTestSchemaSource import org.apache.daffodil.lib.oolag.OOLAG import org.apache.daffodil.lib.schema.annotation.props.LookupLocation +import org.apache.daffodil.lib.schema.annotation.props.gen.LengthUnits import org.apache.daffodil.lib.util.TransitiveClosure import org.apache.daffodil.lib.xml.* @@ -116,6 +117,7 @@ final class SchemaSet private ( requiredEvaluationsAlways(root) requiredEvaluationsAlways(checkForDuplicateTopLevels()) + requiredEvaluationsAlways(root.checkEndOfParentRestrictions(LengthUnits.Characters)) lazy val resolver = DFDLCatalogResolver.get diff --git a/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/SequenceGroup.scala b/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/SequenceGroup.scala index a6a9bc0037..ef510f2a2f 100644 --- a/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/SequenceGroup.scala +++ b/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/SequenceGroup.scala @@ -34,6 +34,7 @@ import org.apache.daffodil.lib.schema.annotation.props.gen.OccursCountKind import org.apache.daffodil.lib.schema.annotation.props.gen.SeparatorPosition import org.apache.daffodil.lib.schema.annotation.props.gen.SequenceKind import org.apache.daffodil.lib.schema.annotation.props.gen.TestKind +import org.apache.daffodil.lib.schema.annotation.props.gen.YesNo import org.apache.daffodil.lib.xml.RefQName import org.apache.daffodil.lib.xml.XMLUtils import org.apache.daffodil.runtime1.layers.LayerRuntimeData @@ -105,6 +106,7 @@ abstract class SequenceGroupTermBase(xml: Node, lexicalParent: SchemaComponent, requiredEvaluationsIfActivated(checkValidityOccursCountKind) requiredEvaluationsIfActivated(checkIfNonEmptyAndDiscrimsOrAsserts) requiredEvaluationsIfActivated(checkIfMultipleChildrenWithSameName) + requiredEvaluationsIfActivated(checkEndOfParentRestrictions()) protected def apparentXMLChildren: Seq[Node] @@ -270,6 +272,38 @@ abstract class SequenceGroupTermBase(xml: Node, lexicalParent: SchemaComponent, case SequenceKind.Unordered => false } + def checkEndOfParentRestrictions() = { + val parent = this + val eopChildren = this.childrenEndOfParent + if (eopChildren.isEmpty) {} else { + parent match { + case s: SequenceTermBase => { + schemaDefinitionWhen( + s.separatorPosition == SeparatorPosition.Postfix, + "element is specified as dfdl:lengthKind=\"endOfParent\", but is in a sequence with dfdl:separatorPosition defined as 'postfix'." + ) + schemaDefinitionWhen( + s.sequenceKind != SequenceKind.Ordered, + "element is specified as dfdl:lengthKind=\"endOfParent\", but is in a sequence with dfdl:sequenceKind defined as 'unordered'." + ) + schemaDefinitionWhen( + s.hasTerminator, + "element is specified as dfdl:lengthKind=\"endOfParent\", but is in a sequence with a dfdl:terminator." + ) + schemaDefinitionWhen( + s.realElementChildren.exists(e => e.floating == YesNo.Yes), + "element is specified as dfdl:lengthKind=\"endOfParent\", but is in a sequence with elements defining dfdl:floating='yes'." + ) + schemaDefinitionWhen( + s.trailingSkip != 0, + "element is specified as dfdl:lengthKind=\"endOfParent\", but is in a sequence with a non-zero dfdl:trailingSkip." + ) + } + case null => // do nothing + } + } + } + } /** diff --git a/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/Term.scala b/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/Term.scala index 4e6495b3d1..d8db9d33a9 100644 --- a/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/Term.scala +++ b/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/Term.scala @@ -27,9 +27,7 @@ import org.apache.daffodil.lib.iapi.WarnID import org.apache.daffodil.lib.schema.annotation.props.Found import org.apache.daffodil.lib.schema.annotation.props.NotFound import org.apache.daffodil.lib.schema.annotation.props.SeparatorSuppressionPolicy -import org.apache.daffodil.lib.schema.annotation.props.gen.LengthKind -import org.apache.daffodil.lib.schema.annotation.props.gen.OccursCountKind -import org.apache.daffodil.lib.schema.annotation.props.gen.YesNo +import org.apache.daffodil.lib.schema.annotation.props.gen.* /** * Mixin for objects that are shared, but have consistency checks to be run @@ -96,6 +94,17 @@ trait Term requiredEvaluationsIfActivated(defaultPropertySources) requiredEvaluationsIfActivated(termChecks) + final lazy val childrenEndOfParent: Seq[ElementBase] = LV(Symbol("childrenEndOfParent")) { + val gms = termChildren + val chls = gms.flatMap { + case eb: ElementBase if eb.lengthKind == LengthKind.EndOfParent => Seq(eb) + case eb: ElementBase => Nil + case c: Choice => Nil + case mg: ModelGroup => mg.childrenEndOfParent + } + chls + }.value + private lazy val termChecks = { statements.foreach { _.checkTerm(this) } } @@ -269,12 +278,12 @@ trait Term .getOrElse(false) } - final lazy val immediatelyEnclosingElementParent: Option[ElementBase] = { + final lazy val immediatelyEnclosingParentForEOPElem: Option[Term] = { val p = optLexicalParent.flatMap { case e: ElementBase => Some(e) case ge: GlobalElementDecl => Some(ge.asRoot) - case s: SequenceTermBase => s.immediatelyEnclosingElementParent - case c: ChoiceTermBase => c.immediatelyEnclosingElementParent + case s: SequenceTermBase => s.immediatelyEnclosingParentForEOPElem + case c: ChoiceTermBase => Some(c) case ct: ComplexTypeBase => { ct.optLexicalParent.flatMap { case e: ElementBase => Some(e) @@ -582,4 +591,21 @@ trait Term } } + final protected lazy val realElementChildren: Seq[ElementBase] = { + termChildren.flatMap { + case eb: ElementBase => Seq(eb) + case c: Choice => Nil + case mg: ModelGroup => mg.realElementChildren + } + } + + lazy val flattenedChildren: IndexedSeq[Term] = termChildren.flatMap { c => + c match { + case eb: ElementBase => IndexedSeq(eb) + case mg: ModelGroup if mg.groupMembers.isEmpty => IndexedSeq(mg) + case s: SequenceTermBase => s.flattenedChildren + case c: ChoiceTermBase => c.flattenedChildren + case _ => Nil + } + }.toIndexedSeq } diff --git a/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/AlignedMixin.scala b/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/AlignedMixin.scala index 63c7936eb3..9b0942e9b1 100644 --- a/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/AlignedMixin.scala +++ b/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/AlignedMixin.scala @@ -457,10 +457,10 @@ trait AlignedMixin extends GrammarMixin { self: Term => case LengthKind.Delimited => encodingLengthApprox case LengthKind.Pattern => encodingLengthApprox case LengthKind.EndOfParent => - eb.immediatelyEnclosingElementParent match { - case Some(parent) => parent.elementSpecifiedLengthApprox - case _ => LengthMultipleOf(1) - } + // technically the alignment of an EndOfParent element would be the + // alignment of its parent minus our current alignment (i.e alignment of + // prior sibs) but since nothing can follow + LengthMultipleOf(1) // If an element is lengthKind="prefixed", the element's length is the length // of the value of the prefix element, which can't be known till runtime case LengthKind.Prefixed => LengthMultipleOf(1) // NYI (see DAFFODIL-3066) diff --git a/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/ElementBaseGrammarMixin.scala b/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/ElementBaseGrammarMixin.scala index 011f323b23..16b09e2873 100644 --- a/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/ElementBaseGrammarMixin.scala +++ b/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/ElementBaseGrammarMixin.scala @@ -19,14 +19,12 @@ package org.apache.daffodil.core.grammar import java.lang.Long as JLong -import org.apache.daffodil.core.dsom.ChoiceTermBase import org.apache.daffodil.core.dsom.ElementBase import org.apache.daffodil.core.dsom.ExpressionCompilers import org.apache.daffodil.core.dsom.InitiatedTerminatedMixin import org.apache.daffodil.core.dsom.ModelGroup import org.apache.daffodil.core.dsom.PrefixLengthQuasiElementDecl import org.apache.daffodil.core.dsom.Root -import org.apache.daffodil.core.dsom.SequenceTermBase import org.apache.daffodil.core.grammar.primitives.* import org.apache.daffodil.core.runtime1.ElementBaseRuntime1Mixin import org.apache.daffodil.lib.exceptions.Assert @@ -56,7 +54,6 @@ trait ElementBaseGrammarMixin requiredEvaluationsIfActivated(checkPrefixedLengthElementDecl) requiredEvaluationsIfActivated(checkDelimitedLengthEVDP) - requiredEvaluationsIfActivated(checkEndOfParentElem) private val context = this @@ -257,131 +254,124 @@ trait ElementBaseGrammarMixin } final lazy val prefixedLengthBody = prefixedLengthElementDecl.parsedValue - final lazy val parentEffectiveLengthUnits: LengthUnits = - immediatelyEnclosingElementParent match { - case Some(parent: ElementBase) => { - parent.lengthKind match { - case LengthKind.Explicit | LengthKind.Prefixed => parent.lengthUnits - case LengthKind.Pattern => LengthUnits.Characters - case _ - if parent.isInstanceOf[ChoiceTermBase] && (parent - .asInstanceOf[ChoiceTermBase] - .choiceLengthKind == ChoiceLengthKind.Explicit) => - LengthUnits.Bytes - case LengthKind.EndOfParent => parent.parentEffectiveLengthUnits - case _ => - Assert.invariantFailed( - s"Could not figure effective length unit of parents of ${context}" - ) - } - } - case None if this.isInstanceOf[Root] => LengthUnits.Characters + def myEffectiveLengthUnits(lastNonEOPLU: LengthUnits): LengthUnits = { + lengthKind match { + case LengthKind.EndOfParent => lastNonEOPLU + case LengthKind.Explicit | LengthKind.Prefixed => lengthUnits + case LengthKind.Pattern => LengthUnits.Characters case _ => Assert.invariantFailed( - s"Could not figure effective length unit of parents of ${context}" + "Delimited and Implicit are illegal for the parent of EndOfParent element" ) } - final lazy val checkEndOfParentElem: Unit = { - if (lengthKind != LengthKind.EndOfParent) () - else { - schemaDefinitionWhen( - hasTerminator, - "%s is specified as dfdl:lengthKind=\"endOfParent\", but specifies a dfdl:terminator.", - context - ) - schemaDefinitionWhen( - trailingSkip != 0, - "%s is specified as dfdl:lengthKind=\"endOfParent\", but specifies a non-zero dfdl:trailingSkip.", - context + } + + def checkChildrenForSiblingsAfterEOPElement( + parent: ElementBase, + specificChild: ElementBase + ) = { + lazy val foundPosition = flattenedChildren.indexOf(specificChild) + lazy val lastIndexOfChildren = flattenedChildren.length - 1 + if (flattenedChildren.isEmpty || foundPosition < 0) { + // not found amongst children + Assert.impossible("EndOfParent element not found amongst term children of parent") + } else if (foundPosition != lastIndexOfChildren) { + // get the following children after the EOP element+ 1 + val followingChildrenAfter = + flattenedChildren.slice(foundPosition + 1, lastIndexOfChildren + 1) + followingChildrenAfter.foreach { + case m: ModelGroup => { + specificChild.SDE( + "element is specified as dfdl:lengthKind=\"endOfParent\", but a model group is defined between this element and the end of the enclosing component" + ) + } + case r if r.isRepresented => { + specificChild.SDE( + "element is specified as dfdl:lengthKind=\"endOfParent\", but a represented element is defined between this element and the end of the enclosing component" + ) + } + case _ => // do nothing + } + } + } + + def checkEndOfParentRestrictions(lastNonEOPLU: LengthUnits): Unit = { + val parent = this + val eopChildren = this.childrenEndOfParent + // checks + this match { + case rootElem: Root if lengthKind == LengthKind.EndOfParent => { + rootElem.checkEndOfParentRestrictionsOnCurrentElement(lastNonEOPLU) + } + case _ => // do nothing + } + + if (eopChildren.isEmpty) + ( + this.elementChildren.foreach(_.checkEndOfParentRestrictions(lastNonEOPLU)) ) + else { + eopChildren.foreach { eopChild => + lazy val parentELU = myEffectiveLengthUnits(lastNonEOPLU) + checkChildrenForSiblingsAfterEOPElement(parent, eopChild) + parent.lengthKind match { + case LengthKind.Implicit | LengthKind.Delimited => + schemaDefinitionError( + "element is specified as dfdl:lengthKind=\"endOfParent\", but its parent is an element with dfdl:lengthKind 'implicit' or 'delimited'." + ) + case _ => // do nothing + } + eopChild.checkEndOfParentRestrictionsOnCurrentElement(parentELU) + if (parent.lengthKind != LengthKind.EndOfParent) { + eopChild.checkEndOfParentRestrictions(parentELU) + } else { + eopChild.checkEndOfParentRestrictions(lastNonEOPLU) + } + } + } + // end checks + } + + def checkEndOfParentRestrictionsOnCurrentElement(parentELU: LengthUnits): Unit = { + val currentElement: ElementBase = this + if (currentElement.lengthKind != LengthKind.EndOfParent) {} else { schemaDefinitionWhen( - maxOccurs > 1, - "%s is specified as dfdl:lengthKind=\"endOfParent\", but specifies a maxOccurs greater than 1.", - context + currentElement.hasTerminator, + "element is specified as dfdl:lengthKind=\"endOfParent\", but specifies a dfdl:terminator." ) schemaDefinitionWhen( - nextSibling.isDefined && nextSibling.get.isInstanceOf[ModelGroup], - "%s is specified as dfdl:lengthKind=\"endOfParent\", but a model group is defined between this element and the end of the enclosing component", - context + currentElement.trailingSkip != 0, + "element is specified as dfdl:lengthKind=\"endOfParent\", but specifies a non-zero dfdl:trailingSkip." ) schemaDefinitionWhen( - nextSibling.isDefined && nextSibling.get.isRepresented, - "%s is specified as dfdl:lengthKind=\"endOfParent\", but a represented element is defined between this element and the end of the enclosing component", - context + currentElement.maxOccurs > 1, + "element is specified as dfdl:lengthKind=\"endOfParent\", but specifies a maxOccurs greater than 1." ) - immediatelyEnclosingElementParent match { - case Some(parent: ElementBase) => - parent.lengthKind match { - case LengthKind.Implicit | LengthKind.Delimited => - schemaDefinitionError( - "%s is specified as dfdl:lengthKind=\"endOfParent\", but its parent is an element with dfdl:lengthKind 'implicit' or 'delimited'.", - context - ) - case _ => // do nothing - } - case _ => // do nothing - } schemaDefinitionWhen( - representation == Representation.Text && knownEncodingWidthInBits != 8 && parentEffectiveLengthUnits != LengthUnits.Characters, - "%s is specified as dfdl:lengthKind=\"endOfParent\", but the element has text representation, does not have a single-byte character set encoding, and the effective length units of the parent is not 'characters'.", - context + currentElement.impliedRepresentation == Representation.Text + && (!currentElement.isKnownEncoding || !currentElement.knownEncodingIsFixedWidth || currentElement.knownEncodingWidthInBits != 8) + && (parentELU != LengthUnits.Characters), + "element is specified as dfdl:lengthKind=\"endOfParent\", but the element has text representation, and does not have a single-byte character set encoding, and the effective length units of the parent is not 'characters'." ) - - immediatelyEnclosingModelGroup match { - case Some(s: SequenceTermBase) => { - schemaDefinitionWhen( - s.separatorPosition == SeparatorPosition.Postfix, - "%s is specified as dfdl:lengthKind=\"endOfParent\", but is in a sequence with dfdl:separatorPosition defined as 'postfix'.", - context - ) - schemaDefinitionWhen( - s.sequenceKind != SequenceKind.Ordered, - "%s is specified as dfdl:lengthKind=\"endOfParent\", but is in a sequence with dfdl:sequenceKind defined as 'unordered'.", - context - ) - schemaDefinitionWhen( - s.hasTerminator, - "%s is specified as dfdl:lengthKind=\"endOfParent\", but is in a sequence with a dfdl:terminator.", - context - ) - schemaDefinitionWhen( - s.elementChildren.exists(e => e.floating == YesNo.Yes), - "%s is specified as dfdl:lengthKind=\"endOfParent\", but is in a sequence with elements defining dfdl:floating='yes'.", - context - ) - schemaDefinitionWhen( - s.trailingSkip != 0, - "%s is specified as dfdl:lengthKind=\"endOfParent\", but is in a sequence with a non-zero dfdl:trailingSkip." - ) - } - case Some(c: ChoiceTermBase) if c.choiceLengthKind == ChoiceLengthKind.Implicit => { - schemaDefinitionWhen( - c.hasTerminator, - "%s is specified as dfdl:lengthKind=\"endOfParent\", but is in a choice with a dfdl:terminator.", - context - ) - schemaDefinitionWhen( - c.trailingSkip != 0, - "%s is specified as dfdl:lengthKind=\"endOfParent\", but is in a choice with a non-zero dfdl:trailingSkip.", - context - ) - } - case _ => // do nothing - } - - if (isSimpleType) { + if (currentElement.isSimpleType) { schemaDefinitionUnless( - (primType eq PrimType.String) - || (representation == Representation.Text) - || (primType eq PrimType.HexBinary) - || (representation == Representation.Binary + (currentElement.primType eq PrimType.String) + || (currentElement.representation == Representation.Text) + || (currentElement.primType eq PrimType.HexBinary) + || (currentElement.representation == Representation.Binary && Seq(BinaryNumberRep.Packed, BinaryNumberRep.Bcd, BinaryNumberRep.Ibm4690Packed) - .contains(binaryNumberRep)), - "%s is a simple type specified as dfdl:lengthKind=\"endOfParent\", but isn't a string type, doesn't have text representation, isn't a hexbinary type, or doesn't have binary representation with packed decimal representation.", - context + .contains(currentElement.binaryNumberRep)) + || (currentElement.representation == Representation.Binary + && optionBinaryCalendarRep.isDefined + && Seq( + BinaryCalendarRep.Packed, + BinaryCalendarRep.Bcd, + BinaryCalendarRep.Ibm4690Packed + ) + .contains(currentElement.binaryCalendarRep)), + "element is a simple type specified as dfdl:lengthKind=\"endOfParent\", but isn't a string type, doesn't have text representation, isn't a hexbinary type, or doesn't have binary representation with packed decimal representation." ) } - } } @@ -738,7 +728,19 @@ trait ElementBaseGrammarMixin case LengthKind.Pattern => schemaDefinitionError("Binary data elements cannot have lengthKind='pattern'.") case LengthKind.EndOfParent => - schemaDefinitionError("Binary data elements cannot have lengthKind='endOfParent'.") + // only for packed binary data, length must be computed at runtime. + if ( + representation == Representation.Binary + && optionBinaryCalendarRep.isDefined + && Seq(BinaryCalendarRep.Packed, BinaryCalendarRep.Bcd, BinaryCalendarRep.Ibm4690Packed) + .contains(binaryCalendarRep) + || representation == Representation.Binary + && optionBinaryNumberRep.isDefined + && Seq(BinaryNumberRep.Packed, BinaryNumberRep.Bcd, BinaryNumberRep.Ibm4690Packed) + .contains(binaryNumberRep) + ) -1 + else + SDE("lengthKind='endOfParent' only supported for packed binary formats.") } private def explicitBinaryLengthInBits() = { @@ -924,6 +926,10 @@ trait ElementBaseGrammarMixin prod("bcdPrefixedLengthCalendar", binaryCalendarRep == BinaryCalendarRep.Bcd) { ConvertZonedCombinator(this, new BCDIntegerPrefixedLength(this), textConverter) } + private lazy val bcdEndOfParentLengthCalendar = + prod("bcdEndOfParentLengthCalendar", binaryCalendarRep == BinaryCalendarRep.Bcd) { + ConvertZonedCombinator(this, new BCDIntegerEndOfParentLength(this), textConverter) + } private lazy val ibm4690PackedKnownLengthCalendar = prod( "ibm4690PackedKnownLengthCalendar", @@ -965,6 +971,16 @@ trait ElementBaseGrammarMixin textConverter ) } + private lazy val ibm4690PackedEndOfParentLengthCalendar = prod( + "ibm4690PackedEndOfParentLengthCalendar", + binaryCalendarRep == BinaryCalendarRep.Ibm4690Packed + ) { + ConvertZonedCombinator( + this, + new IBM4690PackedIntegerEndOfParentLength(this), + textConverter + ) + } private lazy val packedKnownLengthCalendar = prod("packedKnownLengthCalendar", binaryCalendarRep == BinaryCalendarRep.Packed) { @@ -1002,6 +1018,14 @@ trait ElementBaseGrammarMixin textConverter ) } + private lazy val packedEndOfParentLengthCalendar = + prod("packedPrefixedLengthCalendar", binaryCalendarRep == BinaryCalendarRep.Packed) { + ConvertZonedCombinator( + this, + new PackedIntegerEndOfParentLength(this, packedSignCodes), + textConverter + ) + } def primType: PrimType @@ -1053,6 +1077,8 @@ trait ElementBaseGrammarMixin byteOrderRaw // must be defined or SDE } (binaryNumberRep, lengthKind, binaryNumberKnownLengthInBits) match { + case (BinaryNumberRep.Binary, LengthKind.EndOfParent, _) => + SDE("lengthKind='endOfParent' is not allowed with binary number representation") case (BinaryNumberRep.Binary, LengthKind.Prefixed, _) => new BinaryIntegerPrefixedLength(this) case (BinaryNumberRep.Binary, _, -1) => new BinaryIntegerRuntimeLength(this) @@ -1070,6 +1096,8 @@ trait ElementBaseGrammarMixin new PackedIntegerDelimitedEndOfData(this, packedSignCodes) case (BinaryNumberRep.Packed, LengthKind.Prefixed, -1) => new PackedIntegerPrefixedLength(this, packedSignCodes) + case (BinaryNumberRep.Packed, LengthKind.EndOfParent, -1) => + new PackedIntegerEndOfParentLength(this, packedSignCodes) case (BinaryNumberRep.Packed, _, -1) => new PackedIntegerRuntimeLength(this, packedSignCodes) case (BinaryNumberRep.Packed, _, _) => @@ -1082,6 +1110,8 @@ trait ElementBaseGrammarMixin new IBM4690PackedIntegerDelimitedEndOfData(this) case (BinaryNumberRep.Ibm4690Packed, LengthKind.Prefixed, -1) => new IBM4690PackedIntegerPrefixedLength(this) + case (BinaryNumberRep.Ibm4690Packed, LengthKind.EndOfParent, -1) => + new IBM4690PackedIntegerEndOfParentLength(this) case (BinaryNumberRep.Ibm4690Packed, _, -1) => new IBM4690PackedIntegerRuntimeLength(this) case (BinaryNumberRep.Ibm4690Packed, _, _) => @@ -1094,6 +1124,7 @@ trait ElementBaseGrammarMixin (lengthKind, binaryNumberKnownLengthInBits) match { case (LengthKind.Delimited, -1) => new BCDIntegerDelimitedEndOfData(this) case (LengthKind.Prefixed, -1) => new BCDIntegerPrefixedLength(this) + case (LengthKind.EndOfParent, -1) => new BCDIntegerEndOfParentLength(this) case (_, -1) => new BCDIntegerRuntimeLength(this) case (_, _) => new BCDIntegerKnownLength(this, binaryNumberKnownLengthInBits) } @@ -1161,6 +1192,8 @@ trait ElementBaseGrammarMixin ) byteOrderRaw // must have or SDE (binaryNumberRep, lengthKind, binaryNumberKnownLengthInBits) match { + case (BinaryNumberRep.Binary, LengthKind.EndOfParent, _) => + SDE("lengthKind='endOfParent' is not allowed with binary number representation") case (BinaryNumberRep.Binary, LengthKind.Prefixed, _) => new BinaryDecimalPrefixedLength(this) case (BinaryNumberRep.Binary, _, -1) => new BinaryDecimalRuntimeLength(this) @@ -1178,6 +1211,8 @@ trait ElementBaseGrammarMixin new PackedDecimalDelimitedEndOfData(this, packedSignCodes) case (BinaryNumberRep.Packed, LengthKind.Prefixed, _) => new PackedDecimalPrefixedLength(this, packedSignCodes) + case (BinaryNumberRep.Packed, LengthKind.EndOfParent, _) => + new PackedDecimalEndOfParentLength(this, packedSignCodes) case (BinaryNumberRep.Packed, _, -1) => new PackedDecimalRuntimeLength(this, packedSignCodes) case (BinaryNumberRep.Packed, _, _) => @@ -1186,6 +1221,8 @@ trait ElementBaseGrammarMixin new BCDDecimalDelimitedEndOfData(this) case (BinaryNumberRep.Bcd, LengthKind.Prefixed, _) => new BCDDecimalPrefixedLength(this) + case (BinaryNumberRep.Bcd, LengthKind.EndOfParent, _) => + new BCDDecimalEndOfParentLength(this) case (BinaryNumberRep.Bcd, _, -1) => new BCDDecimalRuntimeLength(this) case (BinaryNumberRep.Bcd, _, _) => new BCDDecimalKnownLength(this, binaryNumberKnownLengthInBits) @@ -1193,6 +1230,8 @@ trait ElementBaseGrammarMixin new IBM4690PackedDecimalDelimitedEndOfData(this) case (BinaryNumberRep.Ibm4690Packed, LengthKind.Prefixed, _) => new IBM4690PackedDecimalPrefixedLength(this) + case (BinaryNumberRep.Ibm4690Packed, LengthKind.EndOfParent, _) => + new IBM4690PackedDecimalEndOfParentLength(this) case (BinaryNumberRep.Ibm4690Packed, _, -1) => new IBM4690PackedDecimalRuntimeLength(this) case (BinaryNumberRep.Ibm4690Packed, _, _) => @@ -1203,6 +1242,7 @@ trait ElementBaseGrammarMixin case PrimType.Boolean => { lengthKind match { case LengthKind.Prefixed => new BinaryBooleanPrefixedLength(this) + case LengthKind.EndOfParent => new BinaryBooleanEndOfParentLength(this) case _ => new BinaryBoolean(this) } } @@ -1256,6 +1296,7 @@ trait ElementBaseGrammarMixin (lengthKind, binaryNumberKnownLengthInBits) match { case (LengthKind.Delimited, -1) => bcdDelimitedLengthCalendar case (LengthKind.Prefixed, -1) => bcdPrefixedLengthCalendar + case (LengthKind.EndOfParent, -1) => bcdEndOfParentLengthCalendar case (_, -1) => bcdRuntimeLengthCalendar case (_, _) => bcdKnownLengthCalendar } @@ -1264,6 +1305,7 @@ trait ElementBaseGrammarMixin (lengthKind, binaryNumberKnownLengthInBits) match { case (LengthKind.Delimited, -1) => ibm4690PackedDelimitedLengthCalendar case (LengthKind.Prefixed, -1) => ibm4690PackedPrefixedLengthCalendar + case (LengthKind.EndOfParent, -1) => ibm4690PackedEndOfParentLengthCalendar case (_, -1) => ibm4690PackedRuntimeLengthCalendar case (_, _) => ibm4690PackedKnownLengthCalendar } @@ -1272,6 +1314,7 @@ trait ElementBaseGrammarMixin (lengthKind, binaryNumberKnownLengthInBits) match { case (LengthKind.Delimited, -1) => packedDelimitedLengthCalendar case (LengthKind.Prefixed, -1) => packedPrefixedLengthCalendar + case (LengthKind.EndOfParent, -1) => packedEndOfParentLengthCalendar case (_, -1) => packedRuntimeLengthCalendar case (_, _) => packedKnownLengthCalendar } @@ -1474,13 +1517,15 @@ trait ElementBaseGrammarMixin // processor to calculate the length and set the bit limit which this processor will use as // the length. The following determines if this element requires another processor to // calculate and set the bit limit, and if so adds the appropriate grammar to do that - val bodyRequiresSpecifiedLengthBitLimit = lengthKind != LengthKind.Delimited && ( - isSimpleType && impliedRepresentation == Representation.Text || - isSimpleType && isNillable || - isComplexType && lengthKind != LengthKind.Implicit || - lengthKind == LengthKind.Prefixed || - isSimpleType && primType == PrimType.HexBinary && lengthKind == LengthKind.Pattern - ) + val bodyRequiresSpecifiedLengthBitLimit = lengthKind != LengthKind.Delimited + && !(isSimpleType && lengthKind == LengthKind.EndOfParent && !this.isInstanceOf[Root]) + && ( + isSimpleType && impliedRepresentation == Representation.Text || + isSimpleType && isNillable || + isComplexType && lengthKind != LengthKind.Implicit || + lengthKind == LengthKind.Prefixed || + isSimpleType && primType == PrimType.HexBinary && lengthKind == LengthKind.Pattern + ) if (!bodyRequiresSpecifiedLengthBitLimit) { body } else { @@ -1524,8 +1569,9 @@ trait ElementBaseGrammarMixin case LengthKind.Implicit if isSimpleType && impliedRepresentation == Representation.Binary => new SpecifiedLengthImplicit(this, body, implicitBinaryLengthInBits) - case LengthKind.EndOfParent => new SpecifiedLengthEndOfParent(this, body) - case LengthKind.Delimited | LengthKind.Implicit => + case LengthKind.EndOfParent if (isComplexType || this.isInstanceOf[Root]) => + new SpecifiedLengthEndOfParent(this, body) + case LengthKind.Delimited | LengthKind.Implicit | LengthKind.EndOfParent => Assert.impossibleCase( "Delimited and ComplexType Implicit cases should not be reached" ) diff --git a/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/primitives/PrimitivesBCD.scala b/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/primitives/PrimitivesBCD.scala index e9e0996b00..5dda4397b1 100644 --- a/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/primitives/PrimitivesBCD.scala +++ b/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/primitives/PrimitivesBCD.scala @@ -52,7 +52,18 @@ class BCDIntegerKnownLength(val e: ElementBase, lengthInBits: Long) extends Term class BCDIntegerPrefixedLength(val e: ElementBase) extends Terminal(e, true) { - override lazy val parser = new BCDIntegerBitLimitLengthParser(e.elementRuntimeData) + override lazy val parser = + new BCDIntegerBitLimitLengthParser(e.elementRuntimeData, isEndOfParent = false) + + override lazy val unparser: Unparser = new BCDIntegerMinimumLengthUnparser( + e.elementRuntimeData + ) +} + +class BCDIntegerEndOfParentLength(val e: ElementBase) extends Terminal(e, true) { + + override lazy val parser = + new BCDIntegerBitLimitLengthParser(e.elementRuntimeData, isEndOfParent = true) override lazy val unparser: Unparser = new BCDIntegerMinimumLengthUnparser( e.elementRuntimeData @@ -94,9 +105,26 @@ class BCDDecimalPrefixedLength(val e: ElementBase) extends Terminal(e, true) { override lazy val parser = new BCDDecimalBitLimitLengthParser( + e.elementRuntimeData, + e.binaryDecimalVirtualPoint, + isEndOfParent = false + ) + + override lazy val unparser: Unparser = + new BCDDecimalMinimumLengthUnparser( e.elementRuntimeData, e.binaryDecimalVirtualPoint ) +} + +class BCDDecimalEndOfParentLength(val e: ElementBase) extends Terminal(e, true) { + + override lazy val parser = + new BCDDecimalBitLimitLengthParser( + e.elementRuntimeData, + e.binaryDecimalVirtualPoint, + isEndOfParent = true + ) override lazy val unparser: Unparser = new BCDDecimalMinimumLengthUnparser( diff --git a/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/primitives/PrimitivesBinaryBoolean.scala b/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/primitives/PrimitivesBinaryBoolean.scala index b934a24fb5..a5be07b9c1 100644 --- a/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/primitives/PrimitivesBinaryBoolean.scala +++ b/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/primitives/PrimitivesBinaryBoolean.scala @@ -47,11 +47,29 @@ class BinaryBoolean(val e: ElementBase) extends Terminal(e, true) { class BinaryBooleanPrefixedLength(val e: ElementBase) extends Terminal(e, true) { override lazy val parser = new BinaryBooleanBitLimitLengthParser( + e.elementRuntimeData, + e.binaryBooleanTrueRep, + e.binaryBooleanFalseRep, + e.lengthUnits, + isEndOfParent = false + ) + + override lazy val unparser: Unparser = new BinaryBooleanMinimumLengthUnparser( e.elementRuntimeData, e.binaryBooleanTrueRep, e.binaryBooleanFalseRep, e.lengthUnits ) +} + +class BinaryBooleanEndOfParentLength(val e: ElementBase) extends Terminal(e, true) { + override lazy val parser = new BinaryBooleanBitLimitLengthParser( + e.elementRuntimeData, + e.binaryBooleanTrueRep, + e.binaryBooleanFalseRep, + e.lengthUnits, + isEndOfParent = true + ) override lazy val unparser: Unparser = new BinaryBooleanMinimumLengthUnparser( e.elementRuntimeData, diff --git a/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/primitives/PrimitivesIBM4690Packed.scala b/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/primitives/PrimitivesIBM4690Packed.scala index 91d0d25efe..6c04df2801 100644 --- a/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/primitives/PrimitivesIBM4690Packed.scala +++ b/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/primitives/PrimitivesIBM4690Packed.scala @@ -59,7 +59,16 @@ class IBM4690PackedIntegerKnownLength(val e: ElementBase, lengthInBits: Long) class IBM4690PackedIntegerPrefixedLength(val e: ElementBase) extends Terminal(e, true) { override lazy val parser = - new IBM4690PackedIntegerBitLimitLengthParser(e.elementRuntimeData) + new IBM4690PackedIntegerBitLimitLengthParser(e.elementRuntimeData, isEndOfParent = false) + + override lazy val unparser: Unparser = new IBM4690PackedIntegerMinimumLengthUnparser( + e.elementRuntimeData + ) +} + +class IBM4690PackedIntegerEndOfParentLength(val e: ElementBase) extends Terminal(e, true) { + override lazy val parser = + new IBM4690PackedIntegerBitLimitLengthParser(e.elementRuntimeData, isEndOfParent = true) override lazy val unparser: Unparser = new IBM4690PackedIntegerMinimumLengthUnparser( e.elementRuntimeData @@ -104,10 +113,26 @@ class IBM4690PackedDecimalKnownLength(val e: ElementBase, lengthInBits: Long) class IBM4690PackedDecimalPrefixedLength(val e: ElementBase) extends Terminal(e, true) { override lazy val parser = new IBM4690PackedDecimalBitLimitLengthParser( + e.elementRuntimeData, + e.binaryDecimalVirtualPoint, + e.decimalSigned, + isEndOfParent = false + ) + + override lazy val unparser: Unparser = new IBM4690PackedDecimalMinimumLengthUnparser( e.elementRuntimeData, e.binaryDecimalVirtualPoint, e.decimalSigned ) +} + +class IBM4690PackedDecimalEndOfParentLength(val e: ElementBase) extends Terminal(e, true) { + override lazy val parser = new IBM4690PackedDecimalBitLimitLengthParser( + e.elementRuntimeData, + e.binaryDecimalVirtualPoint, + e.decimalSigned, + isEndOfParent = true + ) override lazy val unparser: Unparser = new IBM4690PackedDecimalMinimumLengthUnparser( e.elementRuntimeData, diff --git a/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/primitives/PrimitivesLengthKind.scala b/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/primitives/PrimitivesLengthKind.scala index fcda6161f8..521e1370b7 100644 --- a/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/primitives/PrimitivesLengthKind.scala +++ b/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/primitives/PrimitivesLengthKind.scala @@ -174,7 +174,8 @@ case class HexBinaryDelimitedEndOfData(e: ElementBase) extends HexBinaryDelimite case class HexBinaryEndOfBitLimit(e: ElementBase) extends Terminal(e, true) { override lazy val parser: DaffodilParser = new HexBinaryEndOfBitLimitParser( - e.elementRuntimeData + e.elementRuntimeData, + isEndOfParent = false ) override lazy val unparser: DaffodilUnparser = @@ -184,7 +185,8 @@ case class HexBinaryEndOfBitLimit(e: ElementBase) extends Terminal(e, true) { case class HexBinaryLengthPrefixed(e: ElementBase) extends Terminal(e, true) { override lazy val parser: DaffodilParser = new HexBinaryEndOfBitLimitParser( - e.elementRuntimeData + e.elementRuntimeData, + isEndOfParent = false ) override lazy val unparser: DaffodilUnparser = @@ -194,7 +196,8 @@ case class HexBinaryLengthPrefixed(e: ElementBase) extends Terminal(e, true) { case class HexBinaryLengthEndOfParent(e: ElementBase) extends Terminal(e, true) { override lazy val parser: DaffodilParser = new HexBinaryEndOfBitLimitParser( - e.elementRuntimeData + e.elementRuntimeData, + isEndOfParent = true ) override lazy val unparser: DaffodilUnparser = diff --git a/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/primitives/PrimitivesPacked.scala b/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/primitives/PrimitivesPacked.scala index f5c81be1f4..bb4b010cd2 100644 --- a/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/primitives/PrimitivesPacked.scala +++ b/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/primitives/PrimitivesPacked.scala @@ -78,7 +78,27 @@ class PackedIntegerPrefixedLength( ) extends Terminal(e, true) { override lazy val parser = - new PackedIntegerBitLimitLengthParser(e.elementRuntimeData, packedSignCodes) + new PackedIntegerBitLimitLengthParser( + e.elementRuntimeData, + packedSignCodes, + isEndOfParent = false + ) + + override lazy val unparser: Unparser = + new PackedIntegerMinimumLengthUnparser(e.elementRuntimeData, packedSignCodes) +} + +class PackedIntegerEndOfParentLength( + val e: ElementBase, + packedSignCodes: PackedSignCodes +) extends Terminal(e, true) { + + override lazy val parser = + new PackedIntegerBitLimitLengthParser( + e.elementRuntimeData, + packedSignCodes, + isEndOfParent = true + ) override lazy val unparser: Unparser = new PackedIntegerMinimumLengthUnparser(e.elementRuntimeData, packedSignCodes) @@ -132,11 +152,31 @@ class PackedDecimalPrefixedLength(val e: ElementBase, packedSignCodes: PackedSig extends Terminal(e, true) { override lazy val parser = new PackedDecimalBitLimitLengthParser( + e.elementRuntimeData, + e.binaryDecimalVirtualPoint, + packedSignCodes, + e.decimalSigned, + isEndOfParent = false + ) + + override lazy val unparser: Unparser = new PackedDecimalMinimumLengthUnparser( e.elementRuntimeData, e.binaryDecimalVirtualPoint, packedSignCodes, e.decimalSigned ) +} + +class PackedDecimalEndOfParentLength(val e: ElementBase, packedSignCodes: PackedSignCodes) + extends Terminal(e, true) { + + override lazy val parser = new PackedDecimalBitLimitLengthParser( + e.elementRuntimeData, + e.binaryDecimalVirtualPoint, + packedSignCodes, + e.decimalSigned, + isEndOfParent = true + ) override lazy val unparser: Unparser = new PackedDecimalMinimumLengthUnparser( e.elementRuntimeData, diff --git a/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/primitives/SpecifiedLength.scala b/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/primitives/SpecifiedLength.scala index 49baa728c2..5c2bde7f84 100644 --- a/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/primitives/SpecifiedLength.scala +++ b/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/primitives/SpecifiedLength.scala @@ -146,14 +146,8 @@ class SpecifiedLengthEndOfParent(e: ElementBase, eGram: => Gram) ) } - lazy val unparser: Unparser = { - if (eUnparser.isEmpty) eUnparser - else - new SpecifiedLengthEndOfParentUnparser( - eUnparser, - e.elementRuntimeData - ) - } + lazy val unparser: Unparser = eUnparser + } class SpecifiedLengthImplicit(e: ElementBase, eGram: => Gram, nBits: Long) diff --git a/daffodil-core/src/main/scala/org/apache/daffodil/io/InputSource.scala b/daffodil-core/src/main/scala/org/apache/daffodil/io/InputSource.scala index c951fb2c7f..627c48ee2a 100644 --- a/daffodil-core/src/main/scala/org/apache/daffodil/io/InputSource.scala +++ b/daffodil-core/src/main/scala/org/apache/daffodil/io/InputSource.scala @@ -181,6 +181,12 @@ abstract class InputSource { */ def releasePosition(bytePos0b: Long): Unit + /** + * Get the number of bytes that are available to be read until the end of + * the data stream. + */ + lazy val bytesTillEndOfDataStream: Long = -1L + /** * Alerts the implementation to attempt to free data that is no longer used, * if possible. If possible, this should free any unlocked bytes. @@ -564,6 +570,22 @@ class BucketingInputSource( headBucketBytePosition0b += bytesRemoved oldestBucketIndex = 0 } + + override lazy val bytesTillEndOfDataStream: Long = { + val initialBytePosition = position() + val endOfDataNotReached = fillBucketsToIndex(maxNumberOfNonNullBuckets, 8) + if (!endOfDataNotReached) { + val numBytesFilled = totalBytesBucketed - initialBytePosition + // reset the byte position to the initial byte position + position(initialBytePosition) + numBytesFilled + } else { + position(initialBytePosition) + throw new Exception( + s"Attempted to fill to end of data stream, but did not reach end of data stream before maxCacheSizeInBytes: ${maxCacheSizeInBytes}." + ) + } + } } /** @@ -645,4 +667,11 @@ class ByteBufferInputSource(byteBuffer: ByteBuffer) extends InputSource { override def close(): Unit = { // do nothing. No resources to release. } + + override lazy val bytesTillEndOfDataStream: Long = { + val initialPosition = position() + val br = byteBuffer.remaining + position(initialPosition) + br + } } diff --git a/daffodil-core/src/main/scala/org/apache/daffodil/io/InputSourceDataInputStream.scala b/daffodil-core/src/main/scala/org/apache/daffodil/io/InputSourceDataInputStream.scala index 8e39d7db43..5e1bc1ff5d 100644 --- a/daffodil-core/src/main/scala/org/apache/daffodil/io/InputSourceDataInputStream.scala +++ b/daffodil-core/src/main/scala/org/apache/daffodil/io/InputSourceDataInputStream.scala @@ -136,6 +136,8 @@ final class InputSourceDataInputStream private (val inputSource: InputSource) */ def hasReachedEndOfData: Boolean = inputSource.hasReachedEndOfData + lazy val bytesTillEndOfDataStream: Long = inputSource.bytesTillEndOfDataStream + /** * Return the number of currently available bytes. * diff --git a/daffodil-core/src/main/scala/org/apache/daffodil/runtime1/processors/BCDParsers.scala b/daffodil-core/src/main/scala/org/apache/daffodil/runtime1/processors/BCDParsers.scala index 36fc5ddeda..f6c285d04a 100644 --- a/daffodil-core/src/main/scala/org/apache/daffodil/runtime1/processors/BCDParsers.scala +++ b/daffodil-core/src/main/scala/org/apache/daffodil/runtime1/processors/BCDParsers.scala @@ -52,9 +52,10 @@ class BCDDecimalRuntimeLengthParser( class BCDDecimalBitLimitLengthParser( e: ElementRuntimeData, - binaryDecimalVirtualPoint: Int + binaryDecimalVirtualPoint: Int, + isEndOfParent: Boolean ) extends PackedBinaryDecimalBaseParser(e, binaryDecimalVirtualPoint, None) - with BitLengthFromBitLimitMixin { + with BitLengthFromBitLimitMixin(isEndOfParent) { override def toNumber(num: Array[Byte]): JBigDecimal = DecimalUtils.bcdToBigDecimal(num, binaryDecimalVirtualPoint) @@ -79,9 +80,9 @@ class BCDIntegerKnownLengthParser(e: ElementRuntimeData, val lengthInBits: Int) } -class BCDIntegerBitLimitLengthParser(e: ElementRuntimeData) +class BCDIntegerBitLimitLengthParser(e: ElementRuntimeData, isEndOfParent: Boolean) extends PackedBinaryIntegerBaseParser(e) - with BitLengthFromBitLimitMixin { + with BitLengthFromBitLimitMixin(isEndOfParent) { override def toNumber(num: Array[Byte]): JBigInteger = DecimalUtils.bcdToBigInteger(num) } diff --git a/daffodil-core/src/main/scala/org/apache/daffodil/runtime1/processors/IBM4690PackedDecimalParsers.scala b/daffodil-core/src/main/scala/org/apache/daffodil/runtime1/processors/IBM4690PackedDecimalParsers.scala index 6ea612a57d..e9761a4349 100644 --- a/daffodil-core/src/main/scala/org/apache/daffodil/runtime1/processors/IBM4690PackedDecimalParsers.scala +++ b/daffodil-core/src/main/scala/org/apache/daffodil/runtime1/processors/IBM4690PackedDecimalParsers.scala @@ -56,9 +56,10 @@ class IBM4690PackedDecimalRuntimeLengthParser( class IBM4690PackedDecimalBitLimitLengthParser( e: ElementRuntimeData, binaryDecimalVirtualPoint: Int, - decimalSigned: YesNo + decimalSigned: YesNo, + isEndOfParent: Boolean ) extends PackedBinaryDecimalBaseParser(e, binaryDecimalVirtualPoint, Some(decimalSigned)) - with BitLengthFromBitLimitMixin { + with BitLengthFromBitLimitMixin(isEndOfParent) { override def toNumber(num: Array[Byte]): JBigDecimal = DecimalUtils.ibm4690ToBigDecimal(num, binaryDecimalVirtualPoint) @@ -88,9 +89,9 @@ class IBM4690PackedIntegerKnownLengthParser( } -class IBM4690PackedIntegerBitLimitLengthParser(e: ElementRuntimeData) +class IBM4690PackedIntegerBitLimitLengthParser(e: ElementRuntimeData, isEndOfParent: Boolean) extends PackedBinaryIntegerBaseParser(e) - with BitLengthFromBitLimitMixin { + with BitLengthFromBitLimitMixin(isEndOfParent) { override def toNumber(num: Array[Byte]): JBigInteger = DecimalUtils.ibm4690ToBigInteger(num) diff --git a/daffodil-core/src/main/scala/org/apache/daffodil/runtime1/processors/PackedDecimalParsers.scala b/daffodil-core/src/main/scala/org/apache/daffodil/runtime1/processors/PackedDecimalParsers.scala index 46fc6c091d..59779e1a84 100644 --- a/daffodil-core/src/main/scala/org/apache/daffodil/runtime1/processors/PackedDecimalParsers.scala +++ b/daffodil-core/src/main/scala/org/apache/daffodil/runtime1/processors/PackedDecimalParsers.scala @@ -58,9 +58,10 @@ class PackedDecimalBitLimitLengthParser( e: ElementRuntimeData, binaryDecimalVirtualPoint: Int, packedSignCodes: PackedSignCodes, - decimalSigned: YesNo + decimalSigned: YesNo, + isEndOfParent: Boolean ) extends PackedBinaryDecimalBaseParser(e, binaryDecimalVirtualPoint, Some(decimalSigned)) - with BitLengthFromBitLimitMixin { + with BitLengthFromBitLimitMixin(isEndOfParent) { override def toNumber(num: Array[Byte]): JBigDecimal = DecimalUtils.packedToBigDecimal(num, binaryDecimalVirtualPoint, packedSignCodes) @@ -93,9 +94,10 @@ class PackedIntegerKnownLengthParser( class PackedIntegerBitLimitLengthParser( e: ElementRuntimeData, - packedSignCodes: PackedSignCodes + packedSignCodes: PackedSignCodes, + isEndOfParent: Boolean ) extends PackedBinaryIntegerBaseParser(e) - with BitLengthFromBitLimitMixin { + with BitLengthFromBitLimitMixin(isEndOfParent) { override def toNumber(num: Array[Byte]): JBigInteger = DecimalUtils.packedToBigInteger(num, packedSignCodes) diff --git a/daffodil-core/src/main/scala/org/apache/daffodil/runtime1/processors/parsers/BinaryBooleanParsers.scala b/daffodil-core/src/main/scala/org/apache/daffodil/runtime1/processors/parsers/BinaryBooleanParsers.scala index a4a30de0bd..72584d234a 100644 --- a/daffodil-core/src/main/scala/org/apache/daffodil/runtime1/processors/parsers/BinaryBooleanParsers.scala +++ b/daffodil-core/src/main/scala/org/apache/daffodil/runtime1/processors/parsers/BinaryBooleanParsers.scala @@ -109,9 +109,10 @@ class BinaryBooleanBitLimitLengthParser( override val context: ElementRuntimeData, binaryBooleanTrueRep: MaybeULong, binaryBooleanFalseRep: ULong, - lengthUnits: LengthUnits + lengthUnits: LengthUnits, + isEndOfParent: Boolean ) extends BinaryBooleanParserBase(binaryBooleanTrueRep, binaryBooleanFalseRep, lengthUnits) - with BitLengthFromBitLimitMixin { + with BitLengthFromBitLimitMixin(isEndOfParent) { override def runtimeDependencies = Vector() diff --git a/daffodil-core/src/main/scala/org/apache/daffodil/runtime1/processors/parsers/HexBinaryLengthParsers.scala b/daffodil-core/src/main/scala/org/apache/daffodil/runtime1/processors/parsers/HexBinaryLengthParsers.scala index 0dc7b21996..2e496d59df 100644 --- a/daffodil-core/src/main/scala/org/apache/daffodil/runtime1/processors/parsers/HexBinaryLengthParsers.scala +++ b/daffodil-core/src/main/scala/org/apache/daffodil/runtime1/processors/parsers/HexBinaryLengthParsers.scala @@ -83,9 +83,9 @@ final class HexBinarySpecifiedLengthParser(erd: ElementRuntimeData, lengthEv: Le } -final class HexBinaryEndOfBitLimitParser(erd: ElementRuntimeData) +final class HexBinaryEndOfBitLimitParser(erd: ElementRuntimeData, isEndOfParent: Boolean) extends HexBinaryLengthParser(erd), - BitLengthFromBitLimitMixin { + BitLengthFromBitLimitMixin(isEndOfParent) { override def runtimeDependencies = Vector() } diff --git a/daffodil-core/src/main/scala/org/apache/daffodil/runtime1/processors/parsers/ParserTraits.scala b/daffodil-core/src/main/scala/org/apache/daffodil/runtime1/processors/parsers/ParserTraits.scala index 814a9ee31b..93feb7d6de 100644 --- a/daffodil-core/src/main/scala/org/apache/daffodil/runtime1/processors/parsers/ParserTraits.scala +++ b/daffodil-core/src/main/scala/org/apache/daffodil/runtime1/processors/parsers/ParserTraits.scala @@ -172,7 +172,7 @@ trait PrefixedLengthParserMixin { * An example of this is prefix length parsers. This trait can be used by those * parsers to do determine the length based on the bitLimit and position. */ -trait BitLengthFromBitLimitMixin { +trait BitLengthFromBitLimitMixin(val isEndOfParent: Boolean = false) { def getBitLength(s: ParseOrUnparseState): Int = { val pState = s.asInstanceOf[PState] @@ -181,7 +181,14 @@ trait BitLengthFromBitLimitMixin { } def getLengthInBits(pstate: PState): Long = { - val len = pstate.bitLimit0b.get - pstate.bitPos0b - len + if (pstate.bitLimit0b.isDefined) { + val len = pstate.bitLimit0b.get - pstate.bitPos0b + len + } else if (isEndOfParent) { + val byteLimit = pstate.dataInputStream.bytesTillEndOfDataStream + byteLimit * 8 + } else { + Assert.invariantFailed("BitLimit not set for parser.") + } } } diff --git a/daffodil-core/src/main/scala/org/apache/daffodil/runtime1/processors/parsers/SpecifiedLengthParsers.scala b/daffodil-core/src/main/scala/org/apache/daffodil/runtime1/processors/parsers/SpecifiedLengthParsers.scala index 1d2bb11521..a2fec40539 100644 --- a/daffodil-core/src/main/scala/org/apache/daffodil/runtime1/processors/parsers/SpecifiedLengthParsers.scala +++ b/daffodil-core/src/main/scala/org/apache/daffodil/runtime1/processors/parsers/SpecifiedLengthParsers.scala @@ -136,7 +136,7 @@ class SpecifiedLengthEndOfParentParser( eParser: Parser, erd: ElementRuntimeData ) extends SpecifiedLengthParserBase(eParser, erd), - BitLengthFromBitLimitMixin { + BitLengthFromBitLimitMixin(true) { override protected def getBitLength(s: PState): MaybeULong = { MaybeULong(super[BitLengthFromBitLimitMixin].getBitLength(s)) diff --git a/daffodil-core/src/main/scala/org/apache/daffodil/unparsers/runtime1/SpecifiedLengthUnparsers.scala b/daffodil-core/src/main/scala/org/apache/daffodil/unparsers/runtime1/SpecifiedLengthUnparsers.scala index 071c810a04..44af8e887a 100644 --- a/daffodil-core/src/main/scala/org/apache/daffodil/unparsers/runtime1/SpecifiedLengthUnparsers.scala +++ b/daffodil-core/src/main/scala/org/apache/daffodil/unparsers/runtime1/SpecifiedLengthUnparsers.scala @@ -72,20 +72,6 @@ final class SpecifiedLengthExplicitImplicitUnparser( } } -final class SpecifiedLengthEndOfParentUnparser( - eUnparser: Unparser, - erd: ElementRuntimeData -) extends CombinatorUnparser(erd) { - - override def runtimeDependencies = Vector() - - override def childProcessors = Vector(eUnparser) - - override final def unparse(state: UState): Unit = { - eUnparser.unparse1(state) - } -} - /** * This trait is to be used with prefixed length unparsers where the length * must be calculated based on the content length of the data. This means the diff --git a/daffodil-test/src/test/resources/org/apache/daffodil/section12/lengthKind/EndOfParentTests.tdml b/daffodil-test/src/test/resources/org/apache/daffodil/section12/lengthKind/EndOfParentTests.tdml index 29aa3214f0..6b2348fee5 100644 --- a/daffodil-test/src/test/resources/org/apache/daffodil/section12/lengthKind/EndOfParentTests.tdml +++ b/daffodil-test/src/test/resources/org/apache/daffodil/section12/lengthKind/EndOfParentTests.tdml @@ -20,7 +20,8 @@ description="Section 12 - lengthKind=endOfParent" xmlns:tdml="http://www.ibm.com/xmlns/dfdl/testData" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dfdl="http://www.ogf.org/dfdl/dfdl-1.0/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" - xmlns:ex="http://example.com" defaultRoundTrip="onePass"> + xmlns:ex="http://example.com" defaultRoundTrip="onePass" + xmlns:dfdlx="http://www.ogf.org/dfdl/dfdl-1.0/extensions"> @@ -253,6 +254,14 @@ + + + + + + + + + + + + endOfParent + does not have + single-byte character set encoding + + + @@ -1247,7 +1267,7 @@ + dfdl:lengthKind="endOfParent" dfdl:representation="text" dfdl:encoding="UTF-8"> @@ -1278,7 +1298,7 @@ - @@ -1675,6 +1695,77 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -1862,8 +1953,7 @@ separator="" leadingSkip='0' encoding="US-ASCII" ignoreCase='no' initiator="" terminator="" initiatedContent="no" textNumberRep="standard" separatorSuppressionPolicy="anyEmpty" separatorPosition="infix" - documentFinalTerminatorCanBeMissing='yes' byteOrder="bigEndian" - binaryNumberRep='binary' /> + documentFinalTerminatorCanBeMissing='yes' byteOrder="bigEndian" /> @@ -1881,42 +1971,15 @@ - - - - - - - - - - - - - - - - simple type - endOfParent - isn't a string type - - - - - - - simple type endOfParent - doesn't have binary representation with packed decimal representation + supported for + packed binary @@ -1995,5 +2058,909 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1 + 1 + + + + 2 + 2 + + + 3 + 3 + + + + + + + + + + + + + X + YZ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + abcde + + + + abcde + + + + + + + ábcde + + + endOfParent + represented element + between this element and + end of the enclosing component + + + + + + abcde + + + + + abcde + + + + + + + + abcde + + + + + + abcde + + + + + + + + + abcde + + + + abcde + + + + + + + abcde + + + + abcde + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 12 + + + + 12 + + + + + + + 123 + + + endOfParent + represented element + between this element and + end of the enclosing component + + + + + + 12 + + + + 12 + + + + + + + 12 + + + + 12 + + + + + + + 123.45 + + + + 123.45 + + + + + + 123.45 + + + + 123.45 + + + + + + + 123.45 + + + + 123.45 + + + + + + + 2017-08-28T18:29:00 + + + + 2017-08-28T18:29:00 + + + + + + + 2017-08-28T18:29:00 + + + + 2017-08-28T18:29:00 + + + + + + + 2017-08-28T18:29:00 + + + + 2017-08-28T18:29:00 + + + + + + + TRUE + + + + true + + + + + + + TRUE + + + + true + + + + + + + + TRUE + + + + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 01234C + + + + 1234 + + + + + + + + 1234 + + + + 1234 + + + + + + + 1234 + + + + 1234 + + + + + + + 30 39 + + + endOfParent + only supported + packed binary formats + + + + + + 01234C + + + + 12.34 + + + + + + + 1234 + + + + 12.34 + + + + + + + 1234 + + + + 12.34 + + + + + + + + DE AD BE EF + + + + DEADBEEF + + + + + + + + 01 + + + endOfParent + doesn't have + packed decimal representation + + + + + + + 01 22 51 64 51 93 65 0C + + + + 1645-12-25T19:36:50 + + + + + + + 06 14 20 04 18 56 03 + + + + 2004-06-14T18:56:03 + + + + + + + 11 30 20 07 04 15 08 + + + + 2007-11-30T04:15:08 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + hello + + + + hello + + + + + + + hello + + + + hello + + + + + + + hello} + + + endOfParent + sequence with + terminator + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + helff + + + + hel + + + + + + + helff + + + + hel + + + diff --git a/daffodil-test/src/test/resources/org/apache/daffodil/section13/packed/packed.tdml b/daffodil-test/src/test/resources/org/apache/daffodil/section13/packed/packed.tdml index 6714a288c5..ce71d4d347 100644 --- a/daffodil-test/src/test/resources/org/apache/daffodil/section13/packed/packed.tdml +++ b/daffodil-test/src/test/resources/org/apache/daffodil/section13/packed/packed.tdml @@ -259,6 +259,68 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -1050,4 +1112,187 @@ + + + + 003C 123D + + + + + 3 + -123 + + + + + + + + + 003C 123D + + + + + 0.03 + -1.23 + + + + + + + + + + 3 + -123 + + + + + + 003C 123D + + + + + + + + + 0.03 + -1.23 + + + + + + 003C 123D + + + + + + + 0003 0123 + + + + + 3 + 123 + + + + + + + + + 0003 0123 + + + + + 0.03 + 1.23 + + + + + + + + + + 3 + 123 + + + + + + 0003 0123 + + + + + + + + 0.03 + 1.23 + + + + + + 0003 0123 + + + + + + + + FFF3 D123 + + + + + 3 + -123 + + + + + + + + + FFF3 D123 + + + + + 0.03 + -1.23 + + + + + + + + + + + 3 + -123 + + + + + + FFF3 D123 + + + + + + + + 0.03 + -1.23 + + + + + + FFF3 D123 + + + diff --git a/daffodil-test/src/test/resources/org/apache/daffodil/section15/choice_groups/ChoiceLengthExplicit.tdml b/daffodil-test/src/test/resources/org/apache/daffodil/section15/choice_groups/ChoiceLengthExplicit.tdml index 099c6c5a1b..ff00047748 100644 --- a/daffodil-test/src/test/resources/org/apache/daffodil/section15/choice_groups/ChoiceLengthExplicit.tdml +++ b/daffodil-test/src/test/resources/org/apache/daffodil/section15/choice_groups/ChoiceLengthExplicit.tdml @@ -84,6 +84,45 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + A + BC + + + + + + + + + + + + + + + + + A + BC + + + + + + + diff --git a/daffodil-test/src/test/scala/org/apache/daffodil/section12/lengthKind/TestLengthKindEndOfParent.scala b/daffodil-test/src/test/scala/org/apache/daffodil/section12/lengthKind/TestLengthKindEndOfParent.scala index 53f25c3720..606daefff3 100644 --- a/daffodil-test/src/test/scala/org/apache/daffodil/section12/lengthKind/TestLengthKindEndOfParent.scala +++ b/daffodil-test/src/test/scala/org/apache/daffodil/section12/lengthKind/TestLengthKindEndOfParent.scala @@ -35,6 +35,7 @@ class TestLengthKindEndOfParent extends TdmlTests { @Test def TestEndOfParentSimpleTypesImplicit = test @Test def TestEndOfParentComplexTypesExplicit = test @Test def TestEndOfParentSimpleTypesExplicit = test + @Test def TestEndOfParentCSVExplicit = test @Test def TestEndOfParentComplexTypesPrefixed = test @Test def TestEndOfParentSimpleTypesPrefixed = test @Test def TestEndOfParentComplexTypesPattern = test @@ -63,12 +64,56 @@ class TestLengthKindEndOfParent extends TdmlTests { @Test def TestEndOfParentSimpleTypes10 = test @Test def TestEndOfParentComplexTypes11 = test @Test def TestEndOfParentSimpleTypes11 = test +// @Test def TestEndOfParentComplexTypesRootChoice = test +// @Test def TestEndOfParentSimpleTypesRootChoice = test @Test def TestEndOfParentComplexTypes12 = test @Test def TestEndOfParentSimpleTypes12 = test @Test def TestEndOfParentComplexTypes13 = test @Test def TestEndOfParentSimpleTypes13 = test @Test def TestEndOfParentSimpleTypes14 = test - @Test def TestEndOfParentSimpleTypes15 = test @Test def TestEndOfParentSimpleTypes16 = test @Test def TestEndOfParentSimpleTypes17 = test + @Test def TestEndOfParentComplexTypesRootEOP = test + @Test def TestEndOfParentSimpleTypesRootEOP = test + + @Test def text_string_txt_bytes = test + @Test def text_string_txt_bits = test + @Test def text_string_txt_chars = test + @Test def text_string_txt_ref1 = test + @Test def text_string_txt_ref2 = test + @Test def text_string_txt_ref3 = test + @Test def text_string_txt_bytes_nil = test + @Test def text_string_txt_bits_nil = test + @Test def text_string_txt_chars_nil = test + @Test def text_int_txt_bytes = test + @Test def text_int_txt_bytes_group_ref = test + @Test def text_int_txt_bits = test + @Test def text_int_txt_chars = test + @Test def text_dec_txt_bytes = test + @Test def text_dec_txt_bits = test + @Test def text_dec_txt_chars = test + @Test def text_date_txt_bytes = test + @Test def text_date_txt_bits = test + @Test def text_date_txt_chars = test + @Test def text_bool_txt_bytes = test + @Test def text_bool_txt_bits = test + @Test def text_bool_txt_chars = test + @Test def bin_int_bin_bytes_packed = test + @Test def bin_int_bin_bytes_bcd = test + @Test def bin_int_bin_bytes_ibm4690 = test + @Test def bin_dec_bin_bytes = test + @Test def bin_dec_bin_bytes_packed = test + @Test def bin_dec_bin_bytes_bcd = test + @Test def bin_dec_bin_bytes_ibm4690 = test + @Test def bin_hex_bytes = test + @Test def bin_bool_bin_bytes = test + @Test def bin_date_bin_bytes_packed = test + @Test def bin_date_bin_bytes_bcd = test + @Test def bin_date_bin_bytes_ibm4690 = test + + @Test def nested_01 = test + @Test def nested_02 = test + @Test def nested_03 = test + @Test def checks_01 = test + @Test def checks_02 = test } diff --git a/daffodil-test/src/test/scala/org/apache/daffodil/section13/packed/TestPacked.scala b/daffodil-test/src/test/scala/org/apache/daffodil/section13/packed/TestPacked.scala index 48fbcf95cf..4c7b516881 100644 --- a/daffodil-test/src/test/scala/org/apache/daffodil/section13/packed/TestPacked.scala +++ b/daffodil-test/src/test/scala/org/apache/daffodil/section13/packed/TestPacked.scala @@ -95,4 +95,18 @@ class TestPacked extends TdmlTests { // Daffodil-2961 @Test def bcdBigIntToLongExpr = test + + // Daffodil-238 + @Test def EOPPackedIntSeq = test + @Test def EOPPackedDecSeq = test + @Test def EOPPackedIntSeqUnparser = test + @Test def EOPPackedDecSeqUnparser = test + @Test def EOPBCDIntSeq = test + @Test def EOPBCDDecSeq = test + @Test def EOPBCDIntSeqUnparser = test + @Test def EOPBCDDecSeqUnparser = test + @Test def EOPIBM4690IntSeq = test + @Test def EOPIBM4690DecSeq = test + @Test def EOPIBM4690IntSeqUnparser = test + @Test def EOPIBM4690DecSeqUnparser = test } diff --git a/daffodil-test/src/test/scala/org/apache/daffodil/section15/choice_groups/TestChoice.scala b/daffodil-test/src/test/scala/org/apache/daffodil/section15/choice_groups/TestChoice.scala index cfbb16a117..075f3a5183 100644 --- a/daffodil-test/src/test/scala/org/apache/daffodil/section15/choice_groups/TestChoice.scala +++ b/daffodil-test/src/test/scala/org/apache/daffodil/section15/choice_groups/TestChoice.scala @@ -133,6 +133,8 @@ class TestChoiceLengthExplicit extends TdmlTests { @Test def explicit_07 = test @Test def explicit_08 = test @Test def explicit_09 = test + @Test def explicit_10 = test + @Test def explicit_11 = test @Test def explicit_multiple_choices = test From 4a974ba0ba93716d12b4b11259021f64566a992e Mon Sep 17 00:00:00 2001 From: olabusayoT <50379531+olabusayoT@users.noreply.github.com> Date: Tue, 26 May 2026 10:41:51 -0400 Subject: [PATCH 3/3] fixup! fixup! fixup! fixup! fixup! fixup! fixup! - consolidate checkEndOfParentRestrictions in TermGrammarMixin - add more tests - clean up commented code --- .../daffodil/core/dsom/ChoiceGroup.scala | 30 --- .../apache/daffodil/core/dsom/SchemaSet.scala | 2 +- .../daffodil/core/dsom/SequenceGroup.scala | 35 ---- .../org/apache/daffodil/core/dsom/Term.scala | 13 +- .../grammar/ElementBaseGrammarMixin.scala | 85 +------- .../core/grammar/TermGrammarMixin.scala | 189 +++++++++++++++++ .../lengthKind/EndOfParentTests.tdml | 194 +++++++++++------- .../TestLengthKindEndOfParent.scala | 5 +- 8 files changed, 315 insertions(+), 238 deletions(-) diff --git a/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/ChoiceGroup.scala b/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/ChoiceGroup.scala index 8af289b2b9..e662e1ce6b 100644 --- a/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/ChoiceGroup.scala +++ b/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/ChoiceGroup.scala @@ -24,9 +24,7 @@ import org.apache.daffodil.core.dsom.walker.ChoiceView import org.apache.daffodil.core.grammar.ChoiceGrammarMixin import org.apache.daffodil.lib.schema.annotation.props.Found import org.apache.daffodil.lib.schema.annotation.props.gen.ChoiceAGMixin -import org.apache.daffodil.lib.schema.annotation.props.gen.ChoiceLengthKind import org.apache.daffodil.lib.schema.annotation.props.gen.Choice_AnnotationMixin -import org.apache.daffodil.lib.schema.annotation.props.gen.LengthUnits import org.apache.daffodil.lib.schema.annotation.props.gen.YesNo /** @@ -105,7 +103,6 @@ abstract class ChoiceTermBase( requiredEvaluationsIfActivated(noBranchesFound) requiredEvaluationsIfActivated(branchesAreNonOptional) requiredEvaluationsIfActivated(branchesAreNotIVCElements) - requiredEvaluationsIfActivated(checkEndOfParentRestrictions()) final protected lazy val optionChoiceDispatchKeyRaw = findPropertyOption("choiceDispatchKey", expressionAllowed = true) @@ -192,33 +189,6 @@ abstract class ChoiceTermBase( } assuming(branchesOk.forall { x => x }) }.value - - lazy val optMyEffectiveLengthUnits: Option[LengthUnits] = - this match { - case self: ChoiceTermBase if self.choiceLengthKind == ChoiceLengthKind.Explicit => - Some(LengthUnits.Bytes) - case _ => None - } - - def checkEndOfParentRestrictions() = { - val parent = this - val eopChildren = this.childrenEndOfParent - if (eopChildren.isEmpty) {} else { - parent match { - case c: ChoiceTermBase if c.choiceLengthKind == ChoiceLengthKind.Implicit => { - schemaDefinitionWhen( - c.hasTerminator, - "element is specified as dfdl:lengthKind=\"endOfParent\", but is in a choice with a dfdl:terminator." - ) - schemaDefinitionWhen( - c.trailingSkip != 0, - "element is specified as dfdl:lengthKind=\"endOfParent\", but is in a choice with a non-zero dfdl:trailingSkip." - ) - } - case _ => // do nothing - } - } - } } object Choice { diff --git a/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/SchemaSet.scala b/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/SchemaSet.scala index ca76780db9..dcd6a87ee9 100644 --- a/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/SchemaSet.scala +++ b/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/SchemaSet.scala @@ -117,7 +117,7 @@ final class SchemaSet private ( requiredEvaluationsAlways(root) requiredEvaluationsAlways(checkForDuplicateTopLevels()) - requiredEvaluationsAlways(root.checkEndOfParentRestrictions(LengthUnits.Characters)) + requiredEvaluationsAlways(root.checkEndOfParentRestrictions(Some(LengthUnits.Characters))) lazy val resolver = DFDLCatalogResolver.get diff --git a/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/SequenceGroup.scala b/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/SequenceGroup.scala index ef510f2a2f..210797ed01 100644 --- a/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/SequenceGroup.scala +++ b/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/SequenceGroup.scala @@ -34,7 +34,6 @@ import org.apache.daffodil.lib.schema.annotation.props.gen.OccursCountKind import org.apache.daffodil.lib.schema.annotation.props.gen.SeparatorPosition import org.apache.daffodil.lib.schema.annotation.props.gen.SequenceKind import org.apache.daffodil.lib.schema.annotation.props.gen.TestKind -import org.apache.daffodil.lib.schema.annotation.props.gen.YesNo import org.apache.daffodil.lib.xml.RefQName import org.apache.daffodil.lib.xml.XMLUtils import org.apache.daffodil.runtime1.layers.LayerRuntimeData @@ -106,7 +105,6 @@ abstract class SequenceGroupTermBase(xml: Node, lexicalParent: SchemaComponent, requiredEvaluationsIfActivated(checkValidityOccursCountKind) requiredEvaluationsIfActivated(checkIfNonEmptyAndDiscrimsOrAsserts) requiredEvaluationsIfActivated(checkIfMultipleChildrenWithSameName) - requiredEvaluationsIfActivated(checkEndOfParentRestrictions()) protected def apparentXMLChildren: Seq[Node] @@ -271,39 +269,6 @@ abstract class SequenceGroupTermBase(xml: Node, lexicalParent: SchemaComponent, case SequenceKind.Ordered => true case SequenceKind.Unordered => false } - - def checkEndOfParentRestrictions() = { - val parent = this - val eopChildren = this.childrenEndOfParent - if (eopChildren.isEmpty) {} else { - parent match { - case s: SequenceTermBase => { - schemaDefinitionWhen( - s.separatorPosition == SeparatorPosition.Postfix, - "element is specified as dfdl:lengthKind=\"endOfParent\", but is in a sequence with dfdl:separatorPosition defined as 'postfix'." - ) - schemaDefinitionWhen( - s.sequenceKind != SequenceKind.Ordered, - "element is specified as dfdl:lengthKind=\"endOfParent\", but is in a sequence with dfdl:sequenceKind defined as 'unordered'." - ) - schemaDefinitionWhen( - s.hasTerminator, - "element is specified as dfdl:lengthKind=\"endOfParent\", but is in a sequence with a dfdl:terminator." - ) - schemaDefinitionWhen( - s.realElementChildren.exists(e => e.floating == YesNo.Yes), - "element is specified as dfdl:lengthKind=\"endOfParent\", but is in a sequence with elements defining dfdl:floating='yes'." - ) - schemaDefinitionWhen( - s.trailingSkip != 0, - "element is specified as dfdl:lengthKind=\"endOfParent\", but is in a sequence with a non-zero dfdl:trailingSkip." - ) - } - case null => // do nothing - } - } - } - } /** diff --git a/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/Term.scala b/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/Term.scala index d8db9d33a9..1172f35d56 100644 --- a/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/Term.scala +++ b/daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/Term.scala @@ -94,17 +94,6 @@ trait Term requiredEvaluationsIfActivated(defaultPropertySources) requiredEvaluationsIfActivated(termChecks) - final lazy val childrenEndOfParent: Seq[ElementBase] = LV(Symbol("childrenEndOfParent")) { - val gms = termChildren - val chls = gms.flatMap { - case eb: ElementBase if eb.lengthKind == LengthKind.EndOfParent => Seq(eb) - case eb: ElementBase => Nil - case c: Choice => Nil - case mg: ModelGroup => mg.childrenEndOfParent - } - chls - }.value - private lazy val termChecks = { statements.foreach { _.checkTerm(this) } } @@ -591,7 +580,7 @@ trait Term } } - final protected lazy val realElementChildren: Seq[ElementBase] = { + final lazy val realElementChildren: Seq[ElementBase] = { termChildren.flatMap { case eb: ElementBase => Seq(eb) case c: Choice => Nil diff --git a/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/ElementBaseGrammarMixin.scala b/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/ElementBaseGrammarMixin.scala index 16b09e2873..ba759e23f7 100644 --- a/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/ElementBaseGrammarMixin.scala +++ b/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/ElementBaseGrammarMixin.scala @@ -22,7 +22,6 @@ import java.lang.Long as JLong import org.apache.daffodil.core.dsom.ElementBase import org.apache.daffodil.core.dsom.ExpressionCompilers import org.apache.daffodil.core.dsom.InitiatedTerminatedMixin -import org.apache.daffodil.core.dsom.ModelGroup import org.apache.daffodil.core.dsom.PrefixLengthQuasiElementDecl import org.apache.daffodil.core.dsom.Root import org.apache.daffodil.core.grammar.primitives.* @@ -254,85 +253,7 @@ trait ElementBaseGrammarMixin } final lazy val prefixedLengthBody = prefixedLengthElementDecl.parsedValue - def myEffectiveLengthUnits(lastNonEOPLU: LengthUnits): LengthUnits = { - lengthKind match { - case LengthKind.EndOfParent => lastNonEOPLU - case LengthKind.Explicit | LengthKind.Prefixed => lengthUnits - case LengthKind.Pattern => LengthUnits.Characters - case _ => - Assert.invariantFailed( - "Delimited and Implicit are illegal for the parent of EndOfParent element" - ) - } - } - - def checkChildrenForSiblingsAfterEOPElement( - parent: ElementBase, - specificChild: ElementBase - ) = { - lazy val foundPosition = flattenedChildren.indexOf(specificChild) - lazy val lastIndexOfChildren = flattenedChildren.length - 1 - if (flattenedChildren.isEmpty || foundPosition < 0) { - // not found amongst children - Assert.impossible("EndOfParent element not found amongst term children of parent") - } else if (foundPosition != lastIndexOfChildren) { - // get the following children after the EOP element+ 1 - val followingChildrenAfter = - flattenedChildren.slice(foundPosition + 1, lastIndexOfChildren + 1) - followingChildrenAfter.foreach { - case m: ModelGroup => { - specificChild.SDE( - "element is specified as dfdl:lengthKind=\"endOfParent\", but a model group is defined between this element and the end of the enclosing component" - ) - } - case r if r.isRepresented => { - specificChild.SDE( - "element is specified as dfdl:lengthKind=\"endOfParent\", but a represented element is defined between this element and the end of the enclosing component" - ) - } - case _ => // do nothing - } - } - } - - def checkEndOfParentRestrictions(lastNonEOPLU: LengthUnits): Unit = { - val parent = this - val eopChildren = this.childrenEndOfParent - // checks - this match { - case rootElem: Root if lengthKind == LengthKind.EndOfParent => { - rootElem.checkEndOfParentRestrictionsOnCurrentElement(lastNonEOPLU) - } - case _ => // do nothing - } - - if (eopChildren.isEmpty) - ( - this.elementChildren.foreach(_.checkEndOfParentRestrictions(lastNonEOPLU)) - ) - else { - eopChildren.foreach { eopChild => - lazy val parentELU = myEffectiveLengthUnits(lastNonEOPLU) - checkChildrenForSiblingsAfterEOPElement(parent, eopChild) - parent.lengthKind match { - case LengthKind.Implicit | LengthKind.Delimited => - schemaDefinitionError( - "element is specified as dfdl:lengthKind=\"endOfParent\", but its parent is an element with dfdl:lengthKind 'implicit' or 'delimited'." - ) - case _ => // do nothing - } - eopChild.checkEndOfParentRestrictionsOnCurrentElement(parentELU) - if (parent.lengthKind != LengthKind.EndOfParent) { - eopChild.checkEndOfParentRestrictions(parentELU) - } else { - eopChild.checkEndOfParentRestrictions(lastNonEOPLU) - } - } - } - // end checks - } - - def checkEndOfParentRestrictionsOnCurrentElement(parentELU: LengthUnits): Unit = { + def checkEndOfParentRestrictionsOnCurrentElement(optParentELU: Option[LengthUnits]): Unit = { val currentElement: ElementBase = this if (currentElement.lengthKind != LengthKind.EndOfParent) {} else { schemaDefinitionWhen( @@ -350,7 +271,7 @@ trait ElementBaseGrammarMixin schemaDefinitionWhen( currentElement.impliedRepresentation == Representation.Text && (!currentElement.isKnownEncoding || !currentElement.knownEncodingIsFixedWidth || currentElement.knownEncodingWidthInBits != 8) - && (parentELU != LengthUnits.Characters), + && (!optParentELU.contains(LengthUnits.Characters)), "element is specified as dfdl:lengthKind=\"endOfParent\", but the element has text representation, and does not have a single-byte character set encoding, and the effective length units of the parent is not 'characters'." ) if (currentElement.isSimpleType) { @@ -362,7 +283,7 @@ trait ElementBaseGrammarMixin && Seq(BinaryNumberRep.Packed, BinaryNumberRep.Bcd, BinaryNumberRep.Ibm4690Packed) .contains(currentElement.binaryNumberRep)) || (currentElement.representation == Representation.Binary - && optionBinaryCalendarRep.isDefined + && currentElement.optionBinaryCalendarRep.isDefined && Seq( BinaryCalendarRep.Packed, BinaryCalendarRep.Bcd, diff --git a/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/TermGrammarMixin.scala b/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/TermGrammarMixin.scala index 995a97cd87..c12a2b264d 100644 --- a/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/TermGrammarMixin.scala +++ b/daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/TermGrammarMixin.scala @@ -17,9 +17,21 @@ package org.apache.daffodil.core.grammar +import org.apache.daffodil.core.dsom.ChoiceTermBase +import org.apache.daffodil.core.dsom.ElementBase +import org.apache.daffodil.core.dsom.ModelGroup +import org.apache.daffodil.core.dsom.Root +import org.apache.daffodil.core.dsom.SequenceTermBase import org.apache.daffodil.core.dsom.Term import org.apache.daffodil.core.grammar.primitives.MandatoryTextAlignment import org.apache.daffodil.core.runtime1.TermRuntime1Mixin +import org.apache.daffodil.lib.exceptions.Assert +import org.apache.daffodil.lib.schema.annotation.props.gen.ChoiceLengthKind +import org.apache.daffodil.lib.schema.annotation.props.gen.LengthKind +import org.apache.daffodil.lib.schema.annotation.props.gen.LengthUnits +import org.apache.daffodil.lib.schema.annotation.props.gen.SeparatorPosition +import org.apache.daffodil.lib.schema.annotation.props.gen.SequenceKind +import org.apache.daffodil.lib.schema.annotation.props.gen.YesNo ///////////////////////////////////////////////////////////////// // Common to all Terms (Elements and ModelGroups) @@ -84,4 +96,181 @@ trait TermGrammarMixin extends AlignedMixin with BitOrderMixin with TermRuntime1 MandatoryTextAlignment(this, knownEncodingAlignmentInBits, true) } + def myEffectiveLengthUnits(optLastNonEOPLU: Option[LengthUnits]): Option[LengthUnits] = { + val elu = this match { + case e: ElementBase => + e.lengthKind match { + case LengthKind.EndOfParent => optLastNonEOPLU + case LengthKind.Explicit | LengthKind.Prefixed => Some(e.lengthUnits) + case LengthKind.Pattern => Some(LengthUnits.Characters) + case _ => None + } + case c: ChoiceTermBase if c.choiceLengthKind == ChoiceLengthKind.Explicit => + Some(LengthUnits.Bytes) + // the spec doesn't actually account for this case, but logically it makes + // sense that ELU of a sequence is the ELU of its parent that technically is the + // last non-EndOfParent ELU. + case s: SequenceTermBase => optLastNonEOPLU + case _ => None + } + elu + } + + final lazy val childrenEndOfParent: Seq[Term] = LV(Symbol("childrenEndOfParent")) { + val gms = termChildren + val chls = gms.flatMap { + case eb: ElementBase if eb.lengthKind == LengthKind.EndOfParent => Seq(eb) + case eb: ElementBase => Nil + case c: ChoiceTermBase => Nil + case mg: ModelGroup => mg.childrenEndOfParent + } + chls + }.value + + def checkEndOfParentRestrictions(optLastNonEOPLU: Option[LengthUnits]): Unit = { + val term = this + lazy val eopChildren = this.childrenEndOfParent + // checks + term match { + case rootElem: Root if rootElem.lengthKind == LengthKind.EndOfParent => { + rootElem.checkEndOfParentRestrictionsOnCurrentElement(Some(LengthUnits.Characters)) + eopChildren.foreach { + case e: ElementBase => { + rootElem.checkChildrenForSiblingsAfterEOPElement(e) + Assert.invariant( + optLastNonEOPLU.isDefined, + "Effective Length Units of parent should not be None" + ) + e.checkEndOfParentRestrictionsOnCurrentElement(optLastNonEOPLU) + } + case _ => // do nothing + } + } + case parent: ElementBase if eopChildren.nonEmpty => { + eopChildren.foreach { + case e: ElementBase => { + val optParentELU = parent.myEffectiveLengthUnits(optLastNonEOPLU) + parent.lengthKind match { + case LengthKind.Implicit | LengthKind.Delimited => + schemaDefinitionError( + "element is specified as dfdl:lengthKind=\"endOfParent\", but its parent is an element with dfdl:lengthKind 'implicit' or 'delimited'." + ) + case _ => // do nothing + } + parent.checkChildrenForSiblingsAfterEOPElement(e) + Assert.invariant( + optParentELU.isDefined, + "Effective Length Units of parent should not be None" + ) + e.checkEndOfParentRestrictionsOnCurrentElement(optParentELU) + } + case _ => // do nothing + } + } + case s: SequenceTermBase if eopChildren.nonEmpty => { + schemaDefinitionWhen( + s.separatorPosition == SeparatorPosition.Postfix, + "element is specified as dfdl:lengthKind=\"endOfParent\", but is in a sequence with dfdl:separatorPosition defined as 'postfix'." + ) + schemaDefinitionWhen( + s.sequenceKind != SequenceKind.Ordered, + "element is specified as dfdl:lengthKind=\"endOfParent\", but is in a sequence with dfdl:sequenceKind defined as 'unordered'." + ) + schemaDefinitionWhen( + s.hasTerminator, + "element is specified as dfdl:lengthKind=\"endOfParent\", but is in a sequence with a dfdl:terminator." + ) + schemaDefinitionWhen( + s.realElementChildren.exists(e => e.floating == YesNo.Yes), + "element is specified as dfdl:lengthKind=\"endOfParent\", but is in a sequence with elements defining dfdl:floating='yes'." + ) + schemaDefinitionWhen( + s.trailingSkip != 0, + "element is specified as dfdl:lengthKind=\"endOfParent\", but is in a sequence with a non-zero dfdl:trailingSkip." + ) + eopChildren.foreach { + case e: ElementBase => { + s.checkChildrenForSiblingsAfterEOPElement(e) + e.checkEndOfParentRestrictionsOnCurrentElement(optLastNonEOPLU) + } + case _ => // do nothing + } + } + case c: ChoiceTermBase if eopChildren.nonEmpty => { + // TODO: The DFDL spec (12.3.6) explicitly mentions that an EndOfParent element + // can be terminated by a choice with choiceLengthKind='explicit', with no reference + // to implicit length choices. Later on in 12.3.6, it specified what the parent Effective + // Length Units would be for an explicit length choice, again with no reference to + // implicit length choices. The only way to get an EndOfParent element within an + // implicit length choice would be to have it actually be terminated by the choice's + // parent, similar to how we treat elements within an element that is also EndOfParent. + // The only mention of implicit length choices in the DFDL Spec is to mention SDEs when + // an EndOfParent element is enclosed by a choice with choiceLengthKind='implicit'. We + // think that may be a typo, so for now we disallow ChoiceLengthKind.Implicit + // enclosing an EndOfParent element. + // See Daffodil-3080 + schemaDefinitionWhen( + c.choiceLengthKind == ChoiceLengthKind.Implicit, + "element is specified as dfdl:lengthKind=\"endOfParent\", but its parent is a choice with dfdl:choiceLengthKind 'implicit'." + ) + schemaDefinitionWhen( + c.hasTerminator, + "element is specified as dfdl:lengthKind=\"endOfParent\", but is in a choice with a dfdl:terminator." + ) + schemaDefinitionWhen( + c.trailingSkip != 0, + "element is specified as dfdl:lengthKind=\"endOfParent\", but is in a choice with a non-zero dfdl:trailingSkip." + ) + val optParentELU = c.myEffectiveLengthUnits(optLastNonEOPLU) + eopChildren.foreach { + case e: ElementBase => { + c.checkChildrenForSiblingsAfterEOPElement(e) + Assert.invariant( + optParentELU.isDefined, + "Effective Length Units of parent should not be None" + ) + e.checkEndOfParentRestrictionsOnCurrentElement(optParentELU) + } + case _ => // do nothing + } + } + case _ => // do nothing + } + // end checks + term.termChildren.foreach { e => + lazy val optParentELU = term.myEffectiveLengthUnits(optLastNonEOPLU) + term match { + case parent @ (_: ElementBase | _: ChoiceTermBase | _: SequenceTermBase) => { + e.checkEndOfParentRestrictions(optParentELU) + } + case _ => // do nothing + } + } + } + + def checkChildrenForSiblingsAfterEOPElement(specificChild: ElementBase) = { + lazy val foundPosition = flattenedChildren.indexOf(specificChild) + lazy val lastIndexOfChildren = flattenedChildren.length - 1 + if (flattenedChildren.isEmpty || foundPosition < 0) { + // not found amongst children + Assert.impossible("EndOfParent element not found amongst term children of parent") + } else if (foundPosition != lastIndexOfChildren) { + // get the following children after the EOP element+ 1 + val followingChildrenAfter = + flattenedChildren.slice(foundPosition + 1, lastIndexOfChildren + 1) + followingChildrenAfter.foreach { + case m: ModelGroup => { + specificChild.SDE( + "element is specified as dfdl:lengthKind=\"endOfParent\", but a model group is defined between this element and the end of the enclosing component" + ) + } + case r if r.isRepresented => { + specificChild.SDE( + "element is specified as dfdl:lengthKind=\"endOfParent\", but a represented element is defined between this element and the end of the enclosing component" + ) + } + case _ => // do nothing + } + } + } } diff --git a/daffodil-test/src/test/resources/org/apache/daffodil/section12/lengthKind/EndOfParentTests.tdml b/daffodil-test/src/test/resources/org/apache/daffodil/section12/lengthKind/EndOfParentTests.tdml index 6b2348fee5..47a77e97c6 100644 --- a/daffodil-test/src/test/resources/org/apache/daffodil/section12/lengthKind/EndOfParentTests.tdml +++ b/daffodil-test/src/test/resources/org/apache/daffodil/section12/lengthKind/EndOfParentTests.tdml @@ -1695,77 +1695,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -1776,7 +1705,7 @@ documentFinalTerminatorCanBeMissing='yes' byteOrder="bigEndian" binaryNumberRep='binary' /> - + @@ -1817,7 +1746,7 @@ - + @@ -1833,6 +1762,64 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + endOfParent + a choice with + choiceLengthKind + implicit + + + + + + + endOfParent + a choice with + choiceLengthKind + implicit + + + @@ -1872,7 +1881,7 @@ - + @@ -1913,7 +1922,7 @@ - + + + + + + + + + + + + + + + + + + + + + @@ -2894,6 +2923,19 @@ + + + hello + + + + hello + + + + @@ -2918,7 +2960,7 @@ - + diff --git a/daffodil-test/src/test/scala/org/apache/daffodil/section12/lengthKind/TestLengthKindEndOfParent.scala b/daffodil-test/src/test/scala/org/apache/daffodil/section12/lengthKind/TestLengthKindEndOfParent.scala index 606daefff3..0f5d8071ec 100644 --- a/daffodil-test/src/test/scala/org/apache/daffodil/section12/lengthKind/TestLengthKindEndOfParent.scala +++ b/daffodil-test/src/test/scala/org/apache/daffodil/section12/lengthKind/TestLengthKindEndOfParent.scala @@ -64,10 +64,10 @@ class TestLengthKindEndOfParent extends TdmlTests { @Test def TestEndOfParentSimpleTypes10 = test @Test def TestEndOfParentComplexTypes11 = test @Test def TestEndOfParentSimpleTypes11 = test -// @Test def TestEndOfParentComplexTypesRootChoice = test -// @Test def TestEndOfParentSimpleTypesRootChoice = test @Test def TestEndOfParentComplexTypes12 = test @Test def TestEndOfParentSimpleTypes12 = test + @Test def TestEndOfParentComplexTypes12Implicit = test + @Test def TestEndOfParentSimpleTypes12Implicit = test @Test def TestEndOfParentComplexTypes13 = test @Test def TestEndOfParentSimpleTypes13 = test @Test def TestEndOfParentSimpleTypes14 = test @@ -114,6 +114,7 @@ class TestLengthKindEndOfParent extends TdmlTests { @Test def nested_01 = test @Test def nested_02 = test @Test def nested_03 = test + @Test def nested_04 = test @Test def checks_01 = test @Test def checks_02 = test }