mbeckerle 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_r213064334
 
 

 ##########
 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:
   So, if an assert has a message expression which fails with a Schema 
definition error, this will be converted into an assertion failed which is a 
parse error. Is that right? Maybe this shouldn't catch schema definition errors 
at all, since they're fatal. 

----------------------------------------------------------------
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