stevedlawrence commented on a change in pull request #117: Allow DFDL 
expressions in the message attribute of asserts/discrimina…
URL: https://github.com/apache/incubator-daffodil/pull/117#discussion_r213066856
 
 

 ##########
 File path: 
daffodil-runtime1/src/main/scala/org/apache/daffodil/processors/parsers/AssertPatternParsers.scala
 ##########
 @@ -17,83 +17,54 @@
 
 package org.apache.daffodil.processors.parsers
 
+import java.util.regex.Matcher
+
+import org.apache.daffodil.dsom.CompiledExpression
+import org.apache.daffodil.dsom.SchemaDefinitionDiagnosticBase
 import org.apache.daffodil.processors._
 import org.apache.daffodil.util.LogLevel
-import java.util.regex.Matcher
 import org.apache.daffodil.util.OnStack
 
-abstract class AssertPatternParserBase(
-  eName: String,
-  kindString: String,
+class AssertPatternParser(
   override val context: TermRuntimeData,
+  discrim: Boolean,
   testPattern: String,
-  message: String)
+  messageExpr: CompiledExpression[AnyRef])
   extends PrimParser {
   override lazy val runtimeDependencies = Vector()
 
+  private def kindString = if (discrim) "Discriminator" else "Assertion"
+
   override def toBriefXML(depthLimit: Int = -1) = {
     "<" + kindString + ">" + testPattern + "</" + kindString + ">"
   }
 
-  // private lazy val compiledPattern = 
ScalaPatternParser.compilePattern(testPattern, context)
-
   lazy val pattern = ("(?s)" + testPattern).r.pattern // imagine a really big 
expensive pattern to compile.
   object withMatcher extends OnStack[Matcher](pattern.matcher(""))
 
   final def parse(start: PState): Unit = {
     val bytePos = (start.bitPos >> 3).toInt
-    log(LogLevel.Debug, "%s - Starting at bit pos: %s", eName, start.bitPos)
-    log(LogLevel.Debug, "%s - Starting at byte pos: %s", eName, bytePos)
-
-    log(LogLevel.Debug, "%s - Looking for testPattern = %s", eName, 
testPattern)
 
     val dis = start.dataInputStream
     val mark = dis.markPos
     withMatcher { m =>
       val isMatch = dis.lookingAt(m, start)
-      afterParse(start, isMatch, m)
+      if (!isMatch) {
+        val message =
+          try {
+            messageExpr.evaluate(start).asInstanceOf[String]
+          } catch {
+            case e @ (_: ParseError | _: SchemaDefinitionDiagnosticBase) => {
+              kindString + " message expression evaluation failed: " + 
e.getMessage
+            }
+          }
+        val diag = new AssertionFailed(context.schemaFileLocation, start, 
message)
 
 Review comment:
   The last paragraph of section 7.3.1 says:
   >If a processing error or schema definition error occurs while evaluating 
the message expression, a recoverable error is issued to record this error 
(containing implementation-dependent content), then processing of the assert 
continues as if there was no problem and in a manner consistent with the 
failureType property, but using an implementation-dependent substitute message.
   
   So I think the correct behavior is to mask the SDE and still create an 
assertion/parse error, just with a implementation defined message. Which in 
this case is message "expression failed".

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


With regards,
Apache Git Services

Reply via email to