This is an automated email from the ASF dual-hosted git repository.

mbeckerle pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/daffodil.git


The following commit(s) were added to refs/heads/main by this push:
     new 25391d8be Add dfdlx:currentPosition function
25391d8be is described below

commit 25391d8bedc13bd13301e86a671e5d7299ccfc82
Author: Michael Beckerle <[email protected]>
AuthorDate: Wed Feb 26 12:02:53 2025 -0500

    Add dfdlx:currentPosition function
    
    This is for some DFDL experiments with data formats
    that DFDL can't currently represent.
    
    The function is only meaningful at parse time.
    
    DAFFODIL-2974
---
 .../apache/daffodil/core/dpath/Expression.scala    | 15 ++++
 .../daffodil/runtime1/dpath/DFDLXFunctions.scala   | 26 +++++++
 .../section23/dfdl_functions/CurrentPosition.tdml  | 80 ++++++++++++++++++++++
 .../TestCurrentPositionFunction.scala              | 32 +++++++++
 4 files changed, 153 insertions(+)

diff --git 
a/daffodil-core/src/main/scala/org/apache/daffodil/core/dpath/Expression.scala 
b/daffodil-core/src/main/scala/org/apache/daffodil/core/dpath/Expression.scala
index 94d6e7cef..0a50bde5b 100644
--- 
a/daffodil-core/src/main/scala/org/apache/daffodil/core/dpath/Expression.scala
+++ 
b/daffodil-core/src/main/scala/org/apache/daffodil/core/dpath/Expression.scala
@@ -1747,6 +1747,21 @@ case class FunctionCallExpression(functionQNameString: 
String, expressions: List
         DFDLXBitBinaryExpr(functionQNameString, functionQName, args, 
DFDLXBitOr(_, _))
       case (RefQName(_, "bitNot", DFDLX), args) =>
         DFDLXBitUnaryExpr(functionQNameString, functionQName, args, 
DFDLXBitNot(_, _))
+      //
+      // Returns the current position in bits or bytes. This is 1-based.
+      // Experimental Feature: Why? because well... is this even well defined
+      // when unparsing given that expressions can get
+      // retried, and the position may not be well known yet at that time.
+      //
+      case (RefQName(_, "currentPosition", DFDLX), args) =>
+        FNOneArgExpr(
+          functionQNameString,
+          functionQName,
+          args,
+          NodeInfo.Long,
+          NodeInfo.String,
+          DFDLXCurrentPosition(_, _)
+        )
 
       case (RefQName(_, "year-from-dateTime", FUNC), args) =>
         FNOneArgExpr(
diff --git 
a/daffodil-core/src/main/scala/org/apache/daffodil/runtime1/dpath/DFDLXFunctions.scala
 
b/daffodil-core/src/main/scala/org/apache/daffodil/runtime1/dpath/DFDLXFunctions.scala
index 66f6a6aa1..3e045eb12 100644
--- 
a/daffodil-core/src/main/scala/org/apache/daffodil/runtime1/dpath/DFDLXFunctions.scala
+++ 
b/daffodil-core/src/main/scala/org/apache/daffodil/runtime1/dpath/DFDLXFunctions.scala
@@ -347,3 +347,29 @@ case class DFDLXDoubleToRawLong(recipes: CompiledDPath, 
argType: NodeInfo.Kind)
     JDouble.doubleToRawLongBits(value.getDouble)
   }
 }
+
+/**
+ * Computes the current position in the data stream in unis of bits or bytes.
+ * 
+ * Not well defined for unparsing since an absolute position may not be 
available
+ * at the time this is called. 
+ */
+case class DFDLXCurrentPosition(recipes: CompiledDPath, argType: NodeInfo.Kind)
+  extends FNOneArg(recipes, NodeInfo.String) {
+  Assert.invariant(argType == NodeInfo.String)
+
+  override def computeValue(arg1: DataValuePrimitive, dstate: DState): 
DataValueLong = {
+    val units = arg1.getString
+    val maybeState = dstate.parseOrUnparseState
+    val bitpos1b =
+      if (maybeState.isDefined) maybeState.get.bitPos1b
+      else throw new IllegalStateException() // when constant folding, this 
can't be folded.
+    val res: Long = units match {
+      case "bits" => bitpos1b
+      case "bytes" =>
+        maybeState.get.bytePos1b // note: if bitpos is not on a byte boundary, 
this will be first prior byte pos.
+      case _ => dstate.SDE("Units argument was not 'bits' or 'bytes'. Value 
was: '%s'.", units)
+    }
+    res
+  }
+}
diff --git 
a/daffodil-test/src/test/resources/org/apache/daffodil/section23/dfdl_functions/CurrentPosition.tdml
 
b/daffodil-test/src/test/resources/org/apache/daffodil/section23/dfdl_functions/CurrentPosition.tdml
new file mode 100644
index 000000000..d5abb2c18
--- /dev/null
+++ 
b/daffodil-test/src/test/resources/org/apache/daffodil/section23/dfdl_functions/CurrentPosition.tdml
@@ -0,0 +1,80 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  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.
+-->
+
+<tdml:testSuite suiteName="CurrentPosition" description="Current Position DFDL 
Extension Function"
+  xmlns:dfdlx="http://www.ogf.org/dfdl/dfdl-1.0/extensions";
+  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:math="http://www.w3.org/2005/xpath-functions/math";
+  xmlns:ex="http://example.com";
+  xmlns:home="http://home.com";
+  xmlns:first="http://first.com";
+  defaultRoundTrip="onePass">
+
+  <tdml:defineSchema elementFormDefault="unqualified" 
useDefaultNamespace="false"
+                     name="cp">
+    <xs:include 
schemaLocation="/org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/>
+    <dfdl:format
+      ref="ex:GeneralFormat"
+      representation="binary"
+      binaryNumberRep="binary"
+      lengthUnits="bits"
+      alignmentUnits="bits"
+      alignment="1"/>
+
+  <xs:simpleType name="uLongN" dfdl:lengthKind="explicit">
+    <xs:restriction base="xs:unsignedLong"/>
+  </xs:simpleType>
+
+  <xs:element name="bits1">
+    <xs:complexType>
+    <xs:sequence>
+      <xs:element name="bitPos1bAtStart" type="xs:long" dfdl:inputValueCalc='{ 
dfdlx:currentPosition("bits") }'/>
+      <xs:element name="oneBit" type="ex:uLongN" dfdl:length="1"/>
+      <xs:element name="bitPos1bAfterOneBit" type="xs:long" 
dfdl:inputValueCalc='{ dfdlx:currentPosition("bits") }'/>
+      <xs:element name="bytePos1bAfterOneBit" type="xs:long" 
dfdl:inputValueCalc='{ dfdlx:currentPosition("bytes") }'/>
+      <xs:element name="eightBits" type="ex:uLongN" dfdl:length="8"/>
+      <xs:element name="bitPos1bAfter9Bits" type="xs:long" 
dfdl:inputValueCalc='{ dfdlx:currentPosition("bits") }'/>
+      <xs:element name="bytePos1bAfter9Bits" type="xs:long" 
dfdl:inputValueCalc='{ dfdlx:currentPosition("bytes") }'/>
+      <xs:element name="sevenBits" type="ex:uLongN" dfdl:length="7"/>
+    </xs:sequence>
+    </xs:complexType>
+  </xs:element>
+
+  </tdml:defineSchema>
+
+  <tdml:parserTestCase name="testCurrentPosition1" root="bits1" model="cp">
+    <tdml:document>
+      <tdml:documentPart type="bits">1000 0010 1001 1111</tdml:documentPart>
+    </tdml:document>
+      <tdml:infoset>
+        <tdml:dfdlInfoset>
+          <ex:bits1>
+            <bitPos1bAtStart>1</bitPos1bAtStart>
+            <oneBit>1</oneBit>
+            <bitPos1bAfterOneBit>2</bitPos1bAfterOneBit>
+            <bytePos1bAfterOneBit>1</bytePos1bAfterOneBit>
+            <eightBits>5</eightBits>
+            <bitPos1bAfter9Bits>10</bitPos1bAfter9Bits>
+            <bytePos1bAfter9Bits>2</bytePos1bAfter9Bits>
+            <sevenBits>31</sevenBits>
+          </ex:bits1>
+        </tdml:dfdlInfoset>
+      </tdml:infoset>
+  </tdml:parserTestCase>
+
+</tdml:testSuite>
diff --git 
a/daffodil-test/src/test/scala/org/apache/daffodil/section23/dfdl_expressions/TestCurrentPositionFunction.scala
 
b/daffodil-test/src/test/scala/org/apache/daffodil/section23/dfdl_expressions/TestCurrentPositionFunction.scala
new file mode 100644
index 000000000..b482c2a3c
--- /dev/null
+++ 
b/daffodil-test/src/test/scala/org/apache/daffodil/section23/dfdl_expressions/TestCurrentPositionFunction.scala
@@ -0,0 +1,32 @@
+/*
+ * 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.section23.dfdl_expressions
+
+import org.apache.daffodil.junit.tdml.{ TdmlSuite, TdmlTests }
+
+import org.junit.Test
+
+object TestCurrentPositionFunction extends TdmlSuite {
+  val tdmlResource = 
"/org/apache/daffodil/section23/dfdl_functions/CurrentPosition.tdml"
+}
+
+class TestCurrentPositionFunction extends TdmlTests {
+  val tdmlSuite = TestCurrentPositionFunction
+
+  @Test def testCurrentPosition1 = test
+}

Reply via email to