You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@daffodil.apache.org by GitBox <gi...@apache.org> on 2020/08/13 15:47:02 UTC
[GitHub] [incubator-daffodil] stevedlawrence commented on a change in pull request #406: Fix multiple discriminator behavior and free up marks when resolved
stevedlawrence commented on a change in pull request #406:
URL: https://github.com/apache/incubator-daffodil/pull/406#discussion_r470041575
##########
File path: daffodil-macro-lib/src/main/scala/org/apache/daffodil/processors/parsers/PointOfUncertaintyMacros.scala
##########
@@ -0,0 +1,97 @@
+/*
+ * 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.processors.parsers
+
+import scala.reflect.macros.blackbox.Context
+
+
+object PointOfUncertaintyMacros {
+
+ /**
+ * For creating a point of uncertainty and ensuring that it is cleaned up
+ * appropriately. This allows the use of standard scala-style syntax to
+ * create and use points of uncertainties, while avoiding allocating
+ * functions. Thus, this allows something like this:
+ *
+ * pstate.withPointOfUncertainty { pou =>
Review comment:
Yep, it took some macro magic to figure out how to get it work, but this technique is definitely easily reusable to avoid allocations.
##########
File path: daffodil-runtime1/src/main/scala/org/apache/daffodil/debugger/InteractiveDebugger.scala
##########
@@ -1592,6 +1578,30 @@ class InteractiveDebugger(runner: InteractiveDebuggerRunner, eCompilers: Express
override val name = "parser" // scala -xcheckinit reported this was uninitialized
} with InfoProcessorBase
+ object InfoPointsOfUncertainty extends DebugCommand with DebugCommandValidateZeroArgs {
+ val name = "pointsOfUncertainty"
+ override lazy val short = "pou"
+ val desc = "display list of unresolved point of uncertainties"
Review comment:
Will fix.
##########
File path: daffodil-core/src/main/scala/org/apache/daffodil/grammar/primitives/PrimitivesExpressions.scala
##########
@@ -121,29 +122,34 @@ case class InitiatedContent(
override val forWhat = ForParser
override def parser = {
+ // Each of the parsers that this could create always appear immediately
+ // after an initiator parser. So they can all assume that the previous
+ // initiator was successfully parsed and thus resolve points of uncertainty
+ // appropriately
+
t match {
- case eb: ElementBase => {
+ case eb: ElementBase if eb.isArray => {
(mg, eb.optPoUMinOccurs) match {
case (sq: SequenceTermBase, Some(min)) => {
- // no PoU if occursIndex is <= min. After that
- // there is a PoU, so we set discriminator true
- // if we evaluate this parser, since this parser is invoked
- // only if the initiator's parser was successful.
+ // This is an array of elements inside a sequence with initiated
+ // content. There is no PoU if occursIndex <= min. All elements
+ // after that have a PoU, which are resolved by this parser.
new InitiatedContentDiscrimOnIndexGreaterThanMinParser(min, eb.erd)
}
case (ch: ChoiceTermBase, None) => {
- // if any array elements initiators are parsed successfully we
- // discriminate the choice. But there is only the choice PoU
- // to set discriminator on.
- new InitiatedContentDiscrimChoiceParser(eb.erd)
+ // This branch of a choice is a fixed number of array elements.
+ // Because they are fixed, there is no PoU associated with the
+ // elements, only the choice. This must resolve the choice PoU only
+ // the first time so as not to resolve any other PoUs.
+ new InitiatedContentDiscrimChoiceOnlyOnFirstIndexParser(eb.erd)
}
case (ch: ChoiceTermBase, Some(min)) => {
- // When occursIndex is 1, then we set the choice discriminator
- // which is 1 below the top of the discriminator stack.
- // Note that the min is supposed to be 1 or greater.
- //
- // If the occursIndex is > min then we set the array discriminator
- // which is top of discriminator stack.
+ // This branch of a choice is for an array of elements with a
+ // minimum number of occurrences. When occursIndex <= min, there
Review comment:
Yeah, the ``min`` variable here actually comes from ``optPoUMinOccurs above``, which is ``Some(0)`` in the case of occursCountKind="parsed", regardless of the actual value of minOccurs. I'll update this comment to make it clear that this min is not necessarily the same as minOccurs.
##########
File path: daffodil-macro-lib/src/main/scala/org/apache/daffodil/processors/parsers/PointOfUncertaintyMacros.scala
##########
@@ -0,0 +1,97 @@
+/*
+ * 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.processors.parsers
+
+import scala.reflect.macros.blackbox.Context
+
+
+object PointOfUncertaintyMacros {
+
+ /**
+ * For creating a point of uncertainty and ensuring that it is cleaned up
+ * appropriately. This allows the use of standard scala-style syntax to
+ * create and use points of uncertainties, while avoiding allocating
+ * functions. Thus, this allows something like this:
+ *
+ * pstate.withPointOfUncertainty { pou =>
+ * // code that implements parsing, and may potentially reset to,
+ * // discard, or resolve this pou variable
+ * }
+ *
+ * Upon completion of the block, if the new point of uncertainty has not been
+ * discarded, reset to, or resovled, this pou will then be discarded,
+ * ensuring that Marks are always cleaned up appropriately.
+ */
+ def withPointOfUncertainty[A, B](c: Context)(pouID: c.Expr[String], context: c.Tree)(func: c.Expr[A => B]) = {
+
+ import c.universe._
+
+ val state = TermName(c.freshName("state"))
+ val id = TermName(c.freshName("id"))
+ val pou = TermName(c.freshName("pou"))
+ val ctx = TermName(c.freshName("context"))
+
+ func.tree match {
+
+ // The func tree is something like (param => body), where param is the
+ // name the caller wants to name the point of uncertainty mark, and body
+ // is the code that uses this point of uncertainty. We transform this
+ // code to create a new point of uncertainty, evaluate the body, and then
+ // use a finally block to ensure we discard the PoU if it has not been
+ // resolved.
+ case q"""($param: $_) => $body""" => {
+
+ // The "body" uses the "param" variable name, and scala doesn't like it
+ // if we just create a new variable with this name, with a very unclear
+ // error message. Instead, we need to create a "fresh" variable name to
+ // hold he new point of uncertainty, and then transform the body so
+ // that all instances of "param" are replaced with our new fresh name.
Review comment:
Agreed. Will do.
##########
File path: daffodil-runtime1/src/main/scala/org/apache/daffodil/processors/parsers/InitiatedContentParsers.scala
##########
@@ -47,13 +57,18 @@ final class InitiatedContentDiscrimChoiceAndIndexGreaterThanMinParser(
extends PrimParser {
override lazy val runtimeDependencies = Vector()
- def parse(start: PState): Unit = {
- val top = start.discriminator
- start.popPointOfUncertainty // pop array discriminator off of stack and any changed variables
- start.setDiscriminator(true) // set the choice discriminator
- start.pushPointOfUncertainty
- start.setDiscriminator(top) // restore the array discriminator to top of stack
- if (start.arrayPos > min)
- start.setDiscriminator(true) // set array discriminator only if
+ final def parse(start: PState): Unit = {
+ if (start.arrayPos > min) {
+ // resolve the point of uncertainty associated with array elements once
+ // we have parsed min occurrences. There should not be a point of
+ // uncertainty associated with this array until we have parsed more than
+ // min occurrences
+ start.resolvePointOfUncertainty()
+ }
+
+ if (start.arrayPos == 1) {
Review comment:
If min is zero, i think there should be a point of uncertainty for the first first array element, since the entire array is optional. And there should also be a separate point of uncertainty for the enclosing choice. So this should resolve two points of uncertainty. At least that's how I understand this stuff. I'll add a test to make sure this does the right thing.
##########
File path: daffodil-runtime1/src/main/scala/org/apache/daffodil/processors/parsers/PState.scala
##########
@@ -304,23 +302,38 @@ final class PState private (
def removeVariableInstance(vrd: VariableRuntimeData): Unit = {
variableMap.removeVariableInstance(vrd)
}
+
+ def withPointOfUncertainty[B](pouID: String, context: RuntimeData)(func: PState.Mark => B): B =
+ macro PointOfUncertaintyMacros.withPointOfUncertainty[PState.Mark, B]
- def pushPointOfUncertainty: Unit = {
- // threadCheck()
- discriminatorStack.push(false)
- changedVariablesStack.push(mutable.MutableList[GlobalQName]())
+ // must be public, but should only be used by the withPointOfUncertainty macro
+ def createPointOfUncertainty(pouID: String, context: RuntimeData): PState.Mark = {
+ val pou = mark(pouID, context)
+ pointsOfUncertainty.push(pou)
+ pou
}
- def popPointOfUncertainty: Unit = {
- // threadCheck()
- discriminatorStack.pop
- changedVariablesStack.pop
+ def resetToPointOfUncertainty(pou: PState.Mark): Unit = {
+ val pouPop = pointsOfUncertainty.pop
+ Assert.invariant(pou == pouPop)
+ reset(pouPop)
}
- def setDiscriminator(disc: Boolean): Unit = {
- // threadCheck()
- discriminatorStack.pop()
- discriminatorStack.push(disc)
+ def discardPointOfUncertainty(pou: PState.Mark): Unit = {
+ val pouPop = pointsOfUncertainty.pop
+ Assert.invariant(pou == pouPop)
+ discard(pou)
+ }
+
+ def resolvePointOfUncertainty(): Unit = {
+ if (!pointsOfUncertainty.isEmpty) {
+ val pouPop = pointsOfUncertainty.pop
+ discard(pouPop)
+ }
+ }
+
+ def isPointOfUncertaintyResolved(pou: PState.Mark): Boolean = {
Review comment:
The quick description is: parsers push PoU's onto the top of the stack when one exists. So the the current in-scope PoU is always at the top of the stack. Discriminators/initated content/etc. resolve whatever is on the top of the stack. This means resolve a PoU changes the in-scope PoU, allowing multiple discriminators to resolve multiple PoUs down the stack. When parsers that created PoU's wind down, two cases are possible:
1. The parers PoU is at the top of the stack, which means nothing resolved it
2. Some previous PoU is at the top of the stack, which can only happen when a discriminator/initated content/etc. popped it off, meaning it was resolved. So this isPoUResolved function really just needs to see if the PoU the parser created is at the top of the stack.
I'll add some doc to all these things to make this clear.
##########
File path: daffodil-runtime1/src/main/scala/org/apache/daffodil/processors/parsers/NilEmptyCombinatorParsers.scala
##########
@@ -33,55 +27,23 @@ abstract class NilOrValueParser(ctxt: TermRuntimeData, nilParser: Parser, valueP
override lazy val runtimeDependencies = Vector()
def parse(pstate: PState): Unit = {
- var mark: PState.Mark = pstate.mark("NilOrValueParser")
- var markLeakCausedByException = false
- try {
+ // This isn't technically a point of uncertainty. Nothing in the nilParser
+ // can discriminate this PoU. We are really just using this as a technique
+ // to be able to reset back to this point if the nil parser fails.
+ pstate.withPointOfUncertainty("NilOrValueParser", ctxt) { pou =>
+
nilParser.parse1(pstate)
if (pstate.processorStatus ne Success) {
- pstate.reset(mark)
- mark = null
+ // did not find a nil. reset back to the pou and try to parse the value
+ pstate.resetToPointOfUncertainty(pou)
valueParser.parse1(pstate)
} else {
- pstate.discard(mark)
Review comment:
Yeah, I've always hated the way we handled these mark leaks, so much code to maintain and mutable variables. The macro makes this much cleaner and consistent among all uses.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org