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

aradzinski pushed a commit to branch NLPCRAFT-206
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git


The following commit(s) were added to refs/heads/NLPCRAFT-206 by this push:
     new 8cd0199  WIP.
8cd0199 is described below

commit 8cd0199b79e515836bc8407ddf1d260a8c9db7a0
Author: Aaron Radzinzski <[email protected]>
AuthorDate: Mon Mar 1 21:43:19 2021 -0800

    WIP.
---
 .../intent/impl/ver2/NCIntentDslCompiler.scala     | 21 ++++++-----
 .../impl/ver2/NCIntentDslFragmentCache.scala       | 11 ++++++
 .../model/intent/dsl/NCIntentDslCompilerSpec.scala | 44 ++++++++++++++++++++--
 3 files changed, 63 insertions(+), 13 deletions(-)

diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/ver2/NCIntentDslCompiler.scala
 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/ver2/NCIntentDslCompiler.scala
index 97a95fd..3351d07 100644
--- 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/ver2/NCIntentDslCompiler.scala
+++ 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/ver2/NCIntentDslCompiler.scala
@@ -111,7 +111,14 @@ object NCIntentDslCompiler extends LazyLogging {
             refMtdName = Some(ctx.id().getText)
         }
 
-        override def exitTermId(ctx: IDP.TermIdContext): Unit = termId = 
ctx.id().getText
+        override def exitTermId(ctx: IDP.TermIdContext): Unit = {
+            termId = ctx.id().getText
+    
+            // Check term ID uniqueness here for better error location.
+            if (terms.exists(t ⇒ t.id != null && t.id == termId))
+                throw newSyntaxError(s"Duplicate term ID: $termId")(ctx.id())
+        }
+        
         override def exitTermEq(ctx: IDP.TermEqContext): Unit =  termConv = 
ctx.TILDA() != null
         override def exitIntentId(ctx: IDP.IntentIdContext): Unit = intentId = 
ctx.id().getText
         override def exitFragId(ctx: IDP.FragIdContext): Unit = fragId = 
ctx.id().getText
@@ -120,8 +127,6 @@ object NCIntentDslCompiler extends LazyLogging {
         override def exitOrderedDecl(ctx: IDP.OrderedDeclContext): Unit = 
ordered = ctx.BOOL().getText == "true"
 
         override def exitFragRef(ctx: IDP.FragRefContext): Unit = {
-            implicit val evidence: ParserRuleContext = ctx
-
             val id = ctx.id().getText
 
             FragCache.get(mdlId, id) match {
@@ -130,11 +135,11 @@ object NCIntentDslCompiler extends LazyLogging {
 
                     for (fragTerm ← frag.terms)
                          if (terms.exists(t ⇒ t.id != null && t.id == 
fragTerm.id))
-                            throw newSyntaxError(s"Duplicate fragment term ID: 
${fragTerm.id}")
+                            throw newSyntaxError(s"Duplicate term ID 
'${fragTerm.id}' in fragment '$id'.")(ctx.id())
                         else
                             terms += fragTerm.cloneWithMeta(meta)
 
-                case None ⇒ throw newSyntaxError(s"Unknown intent fragment ID: 
$id")
+                case None ⇒ throw newSyntaxError(s"Unknown intent fragment ID: 
$id")(ctx.id())
             }
 
             fragMeta = null
@@ -154,7 +159,7 @@ object NCIntentDslCompiler extends LazyLogging {
                         Pattern.compile(flowRegex.get)
                     catch {
                         case e: PatternSyntaxException ⇒
-                            newSyntaxError(s"${e.getDescription} in intent 
flow regex '${e.getPattern}' near index ${e.getIndex}.")
+                            newSyntaxError(s"${e.getDescription} in intent 
flow regex '${e.getPattern}' near index ${e.getIndex}.")(ctx.qstring())
                     }
             }
         }
@@ -221,10 +226,6 @@ object NCIntentDslCompiler extends LazyLogging {
 
                 }
                 
-            // Check term ID uniqueness.
-            if (terms.exists(t ⇒ t.id != null && t.id == termId))
-                throw newSyntaxError(s"Duplicate term ID: $termId")
-                
             // Add term.
             terms += NCDslTerm(
                 termId,
diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/ver2/NCIntentDslFragmentCache.scala
 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/ver2/NCIntentDslFragmentCache.scala
index c240e84..b079321 100644
--- 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/ver2/NCIntentDslFragmentCache.scala
+++ 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/ver2/NCIntentDslFragmentCache.scala
@@ -30,6 +30,17 @@ object NCIntentDslFragmentCache {
     
     /**
       *
+      */
+    def clear(): Unit = cache.clear()
+    
+    /**
+      *
+      * @param mdlId
+      */
+    def clear(mdlId: String): Unit = cache += mdlId → 
mutable.HashMap.empty[String, NCDslFragment]
+    
+    /**
+      *
       * @param mdlId
       * @param frag
       */
diff --git 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/dsl/NCIntentDslCompilerSpec.scala
 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/dsl/NCIntentDslCompilerSpec.scala
index 608536e..41316b0 100644
--- 
a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/dsl/NCIntentDslCompilerSpec.scala
+++ 
b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/dsl/NCIntentDslCompilerSpec.scala
@@ -18,20 +18,22 @@
 package org.apache.nlpcraft.model.intent.dsl
 
 import org.apache.nlpcraft.common._
-import org.apache.nlpcraft.model.intent.impl.ver2.NCIntentDslCompiler
+import org.apache.nlpcraft.model.intent.impl.ver2.{NCIntentDslCompiler, 
NCIntentDslFragmentCache}
 import org.junit.jupiter.api.Test
 
 /**
  * Tests for DSL compiler.
  */
 class NCIntentDslCompilerSpec {
+    private final val MODEL_ID = "test.mdl.id"
+    
     /**
      *
      * @param dsl
      */
     private def checkOk(dsl: String): Unit =
         try {
-            NCIntentDslCompiler.compile(dsl, "mdl.id")
+            NCIntentDslCompiler.compile(dsl, MODEL_ID)
 
             assert(true)
         }
@@ -45,7 +47,7 @@ class NCIntentDslCompilerSpec {
      */
     private def checkError(txt: String): Unit =
         try {
-            NCIntentDslCompiler.compile(txt, "mdl.id")
+            NCIntentDslCompiler.compile(txt, MODEL_ID)
 
             assert(false)
         } catch {
@@ -57,6 +59,8 @@ class NCIntentDslCompilerSpec {
     @Test
     @throws[NCException]
     def testOk(): Unit = {
+        NCIntentDslFragmentCache.clear(MODEL_ID)
+        
         checkOk(
             """
               |intent=i1
@@ -106,6 +110,8 @@ class NCIntentDslCompilerSpec {
     @Test
     @throws[NCException]
     def testFail(): Unit = {
+        NCIntentDslFragmentCache.clear(MODEL_ID)
+        
         checkError(
             """
               |intent=i1
@@ -124,9 +130,41 @@ class NCIntentDslCompilerSpec {
         checkError(
             """
               |intent=i1
+              |     flow="a[^0-9b]"
+              |     term(t1)={true}
+              |     term(t1)={true}
+              |""".stripMargin
+        )
+        checkError(
+            """
+              |intent=i1
               |     flow="a[^0-9]b"
               |     term(t1)={has(json("{'a': true, 'b\'2': {'arr': [1, 2, 
3]}}"), map("k1\"", 'v1\'v1', "k2", "v2"))}[1:2]
               |""".stripMargin
         )
+        checkError(
+            """
+              |fragment=f1
+              |     term(t1)={2==2}
+              |     term~/class#method/
+              |
+              |intent=i1
+              |     flow="a[^0-9]b"
+              |     term(t1)={has(json("{'a': true, 'b\'2': {'arr': [1, 2, 
3]}}"), map("موسكو\"", 'v1\'v1', "k2", "v2"))}
+              |     fragment(f1, {'a': true, 'b': ["s1", "s2"]})
+              |""".stripMargin
+        )
+        checkError(
+            """
+              |fragment=f111
+              |     term(t1)={2==2}
+              |     term~/class#method/
+              |
+              |intent=i1
+              |     flow="a[^0-9]b"
+              |     term(t1)={has(json("{'a': true, 'b\'2': {'arr': [1, 2, 
3]}}"), map("موسكو\"", 'v1\'v1', "k2", "v2"))}
+              |     fragment(f1_, {'a': true, 'b': ["s1", "s2"]})
+              |""".stripMargin
+        )
     }
 }

Reply via email to