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

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


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

commit dcf211e162e231e4bf530e964ef478bc2750f85c
Author: Aaron Radzinzski <[email protected]>
AuthorDate: Mon Feb 22 19:33:05 2021 -0800

    WIP.
---
 .../nlpcraft/common/makro/NCMacroCompiler.scala    | 64 ++++++++++++----------
 1 file changed, 35 insertions(+), 29 deletions(-)

diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/makro/NCMacroCompiler.scala
 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/makro/NCMacroCompiler.scala
index ac07f74..ca6ded7 100644
--- 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/makro/NCMacroCompiler.scala
+++ 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/makro/NCMacroCompiler.scala
@@ -43,8 +43,9 @@ object NCMacroCompiler extends LazyLogging {
     /**
       *
       * @param parser
+      * @param in
       */
-    class FiniteStateMachine(parser: P) extends NCMacroDslBaseListener {
+    class FiniteStateMachine(parser: P, in: String) extends 
NCMacroDslBaseListener {
         private val stack = new mutable.ArrayStack[StackItem]
 
         // Current min/max quantifier.
@@ -67,9 +68,12 @@ object NCMacroCompiler extends LazyLogging {
           * @param ctx
           * @return
           */
-        def error(errMsg: String)(implicit ctx: ParserRuleContext): 
RecognitionException =
-            new RecognitionException(errMsg, parser, parser.getInputStream, 
ctx)
-
+        def compilerError(errMsg: String)(implicit ctx: ParserRuleContext): 
NCE = {
+            val tok = ctx.start
+            
+            new NCE(mkCompilerError(errMsg, tok.getLine, 
tok.getCharPositionInLine, in))
+        }
+    
         override def enterExpr(ctx: NCMacroDslParser.ExprContext): Unit = {
             val buf = mutable.Buffer.empty[String]
 
@@ -139,16 +143,16 @@ object NCMacroCompiler extends LazyLogging {
             try
                 min = java.lang.Integer.parseInt(minStr)
             catch {
-                case _: NumberFormatException ⇒ throw error(s"Invalid min 
quantifier: $minStr")
+                case _: NumberFormatException ⇒ throw compilerError(s"Invalid 
min quantifier: $minStr")
             }
             try
                 max = java.lang.Integer.parseInt(maxStr)
             catch {
-                case _: NumberFormatException ⇒ throw error(s"Invalid max 
quantifier: $maxStr")
+                case _: NumberFormatException ⇒ throw compilerError(s"Invalid 
max quantifier: $maxStr")
             }
             
             if (min < 0 || max < 0 || min > max || max == 0)
-                throw error(s"[min,max] quantifiers should satisfy 'max >= 
min, min >= 0, max > 0': [$min, $max]")
+                throw compilerError(s"[$min,$max] quantifiers should satisfy 
'max >= min, min >= 0, max > 0'.")
         }
     
         /**
@@ -168,18 +172,6 @@ object NCMacroCompiler extends LazyLogging {
     class CompilerErrorListener(in: String) extends BaseErrorListener {
         /**
          *
-         * @param len
-         * @param pos
-         * @return
-         */
-        private def makeCharPosPointer(len: Int, pos: Int): String = {
-            val s = (for (_ ← 1 to len) yield '-').mkString("")
-
-            s.substring(0, pos - 1) + '^' + s.substring(pos)
-        }
-
-        /**
-         *
          * @param recognizer
          * @param offendingSymbol
          * @param line
@@ -193,16 +185,30 @@ object NCMacroCompiler extends LazyLogging {
             line: Int,
             charPos: Int,
             msg: String,
-            e: RecognitionException): Unit = {
-
-            val errMsg = s"Macro syntax error at line $line:$charPos - $msg\n" 
+
-                s"  |-- ${c("Macro:")} $in\n" +
-                s"  +-- ${c("Error:")} ${makeCharPosPointer(in.length, 
charPos)}"
-
-            throw new NCE(errMsg)
-        }
+            e: RecognitionException): Unit =  throw new 
NCE(mkCompilerError(msg, line, charPos, in))
     }
-
+    
+    /**
+      *
+      * @param line
+      * @param charPos
+      * @param in
+      * @param msg
+      */
+    private def mkCompilerError(
+        msg: String,
+        line: Int,
+        charPos: Int,
+        in: String
+    ): String = {
+        val s = "_" * in.length
+        val charPosPtr = s.substring(0, charPos - 1) + '^' + 
s.substring(charPos)
+    
+        s"Macro compiler error at line $line:$charPos - $msg\n" +
+        s"  |-- ${c("Macro:")} $in\n" +
+        s"  +-- ${c("Error:")} $charPosPtr"
+    }
+    
     /**
      *
      * @param in Macro to expand.
@@ -221,7 +227,7 @@ object NCMacroCompiler extends LazyLogging {
         parser.addErrorListener(new CompilerErrorListener(in))
 
         // State automata.
-        val fsm = new FiniteStateMachine(parser)
+        val fsm = new FiniteStateMachine(parser, in)
 
         // Parse the input DSL and walk built AST.
         (new ParseTreeWalker).walk(fsm, parser.makro())

Reply via email to