You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nlpcraft.apache.org by ar...@apache.org on 2021/06/26 00:18:05 UTC

[incubator-nlpcraft] branch master updated: Improved compiler error messaging.

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 35c37ec  Improved compiler error messaging.
35c37ec is described below

commit 35c37ec52f8de3ffb95912aa7fe4d97ee981662c
Author: Aaron Radzinski <ar...@datalingvo.com>
AuthorDate: Fri Jun 25 17:17:53 2021 -0700

    Improved compiler error messaging.
---
 .../nlpcraft/common/antlr4/NCCompilerUtils.scala   | 52 ++++++++++++++++++++++
 .../nlpcraft/common/ascii/NCAsciiTable.scala       |  6 +--
 .../nlpcraft/common/makro/NCMacroCompiler.scala    | 16 +++----
 .../model/intent/compiler/NCIdlCompiler.scala      | 18 ++++----
 .../model/intent/compiler/NCIdlCompilerBase.scala  |  4 +-
 5 files changed, 73 insertions(+), 23 deletions(-)

diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/antlr4/NCCompilerUtils.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/antlr4/NCCompilerUtils.scala
new file mode 100644
index 0000000..406e959
--- /dev/null
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/antlr4/NCCompilerUtils.scala
@@ -0,0 +1,52 @@
+/*
+ * 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
+ *
+ *      https://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.nlpcraft.common.antlr4
+
+import org.apache.nlpcraft.common._
+
+case class CompilerErrorHolder(
+    ptrStr: String,
+    origStr: String
+)
+
+object NCCompilerUtils {
+    /**
+     *
+     * @param in
+     * @param charPos
+     * @return
+     */
+    def mkErrorHolder(in: String, charPos: Int): CompilerErrorHolder = {
+        val charPos0 = charPos - (in.length - in.stripLeading().length)
+        val in0 = in.strip()
+
+        val pos = Math.max(0, charPos0)
+        val dash = "-" * in0.length
+
+        var ptrStr = dash.substring(0, pos) + r("^")
+
+        if (pos < dash.length - 1)
+            ptrStr = ptrStr + y("~") + y(dash.substring(pos + 2))
+        else
+            ptrStr = ptrStr + y(dash.substring(pos + 1))
+
+        val origStr = in0.substring(0, pos) + r(in0.charAt(pos)) + y(in0.substring(pos + 1))
+
+        CompilerErrorHolder(ptrStr, origStr)
+    }
+}
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ascii/NCAsciiTable.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ascii/NCAsciiTable.scala
index 812e7d0..b66dc0d 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ascii/NCAsciiTable.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ascii/NCAsciiTable.scala
@@ -158,10 +158,10 @@ class NCAsciiTable {
     /**
      * Sets table's margin.
      *
-     * @param top    Top margin.
-     * @param right  Right margin.
+     * @param top Top margin.
+     * @param right Right margin.
      * @param bottom Bottom margin.
-     * @param left   Left margin.
+     * @param left Left margin.
      */
     def margin(top: Int = 0, right: Int = 0, bottom: Int = 0, left: Int = 0): NCAsciiTable = {
         margin = Margin(top, right, bottom, left)
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 ca4a6e2..cab25fc 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
@@ -21,6 +21,7 @@ import com.typesafe.scalalogging.LazyLogging
 import org.antlr.v4.runtime.tree.ParseTreeWalker
 import org.antlr.v4.runtime._
 import org.apache.nlpcraft.common._
+import org.apache.nlpcraft.common.antlr4._
 import org.apache.nlpcraft.common.makro.antlr4._
 import org.apache.nlpcraft.common.makro.antlr4.{NCMacroDslParser => P}
 import scala.collection.mutable
@@ -47,7 +48,7 @@ object NCMacroCompiler extends LazyLogging {
       * @param in
       */
     class FiniteStateMachine(parser: P, in: String) extends NCMacroDslBaseListener {
-        private val stack = new mutable.ArrayStack[StackItem]
+        private val stack = new mutable.Stack[StackItem]
 
         private var expandedSyns: Set[String] = _
 
@@ -227,14 +228,11 @@ object NCMacroCompiler extends LazyLogging {
         charPos: Int, // 0, 1, 2, ...
         in: String
     ): String = {
-        val dash = "-" * in.length
-        val pos = Math.max(0, charPos)
-        val posPtr = dash.substring(0, pos) + r("^") + y(dash.substring(pos + 1))
-        val inPtr = in.substring(0, pos) + r(in.charAt(pos)) + y(in.substring(pos + 1))
-    
-        s"Macro compiler error at line $line:${charPos + 1} - $msg\n" +
-        s"  |-- ${c("Macro:")}    $inPtr\n" +
-        s"  +-- ${c("Location:")} $posPtr"
+        val hold = NCCompilerUtils.mkErrorHolder(in, charPos)
+
+        s"Macro compiler error at line $line - $msg\n" +
+        s"  |-- ${c("Macro:")} ${hold.origStr}\n" +
+        s"  +-- ${c("Error:")} ${hold.ptrStr}"
     }
     
     /**
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/compiler/NCIdlCompiler.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/compiler/NCIdlCompiler.scala
index 56e999d..7748a2e 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/compiler/NCIdlCompiler.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/compiler/NCIdlCompiler.scala
@@ -22,6 +22,7 @@ import org.antlr.v4.runtime.tree.ParseTreeWalker
 import org.antlr.v4.runtime._
 import org.antlr.v4.runtime.{ParserRuleContext => PRC}
 import org.apache.nlpcraft.common._
+import org.apache.nlpcraft.common.antlr4.NCCompilerUtils
 import org.apache.nlpcraft.model.intent.compiler.antlr4.{NCIdlBaseListener, NCIdlLexer, NCIdlParser => IDP}
 import org.apache.nlpcraft.model.intent.compiler.{NCIdlCompilerGlobal => Global}
 import org.apache.nlpcraft.model._
@@ -172,7 +173,7 @@ object NCIdlCompiler extends LazyLogging {
                 if (min < 0)
                     throw newSyntaxError(s"Min value cannot be negative: $min")(ctx)
                 if (min > max)
-                    throw newSyntaxError(s"Min value '$min' cannot be greater than max value '$min'.")(ctx)
+                    throw newSyntaxError(s"Min value '$min' cannot be greater than max value '$max'.")(ctx)
                 if (max > MINMAX_MAX)
                     throw newSyntaxError(s"Max value '$max' cannot be greater than '$MINMAX_MAX'.")(ctx)
 
@@ -540,22 +541,21 @@ object NCIdlCompiler extends LazyLogging {
         origin: String,
         mdl: NCModel): String = {
         val idlLine = idl.split("\n")(line - 1)
-        val dash = "-" * idlLine.length
-        val pos = Math.max(0, charPos)
-        val posPtr = dash.substring(0, pos) + r("^") + y(dash.substring(pos + 1))
-        val idlPtr = idlLine.substring(0, pos) + r(idlLine.charAt(pos)) + y(idlLine.substring(pos + 1))
+
+        val hold = NCCompilerUtils.mkErrorHolder(idlLine, charPos)
+
         val aMsg = U.decapitalize(msg) match {
             case s: String if s.last == '.' => s
             case s: String => s + '.'
         }
 
-        s"IDL $kind error in '$srcName' at line $line:${charPos + 1} - $aMsg\n" +
+        s"IDL $kind error in '$srcName' at line $line - $aMsg\n" +
             s"  |-- ${c("Model ID:")} ${mdl.getId}\n" +
             s"  |-- ${c("Model origin:")} ${mdl.getOrigin}\n" +
             s"  |-- ${c("Intent origin:")} $origin\n" +
-            s"  |-- $RST$W--------------$RST\n" +
-            s"  |-- ${c("Line:")}     $idlPtr\n" +
-            s"  +-- ${c("Position:")} $posPtr"
+            s"  |--<\n" +
+            s"  |-- ${c("Line:")}  ${hold.origStr}\n" +
+            s"  +-- ${c("Error:")} ${hold.ptrStr}"
     }
 
     /**
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/compiler/NCIdlCompilerBase.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/compiler/NCIdlCompilerBase.scala
index ec30c64..367e338 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/compiler/NCIdlCompilerBase.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/compiler/NCIdlCompilerBase.scala
@@ -31,7 +31,7 @@ import java.time.{LocalDate, LocalTime}
 import java.util
 import java.util.{Calendar, Collections, Collection => JColl, List => JList, Map => JMap}
 
-import scala.jdk.CollectionConverters.{CollectionHasAsScala, IterableHasAsJava}
+import scala.jdk.CollectionConverters.CollectionHasAsScala
 
 trait NCIdlCompilerBase {
     type S = NCIdlStack
@@ -152,7 +152,7 @@ trait NCIdlCompilerBase {
     def rtMissingParamError(argNum: Int, fun: String)(implicit ctx: PRC): NCE =
         newRuntimeError(s"Missing parameters for IDL function ($argNum is required): $fun()")
     def rtTooManyParamsError(argNum: Int, fun: String)(implicit ctx: PRC): NCE =
-        newRuntimeError(s"Too many parameters for IDL function (only $argNum is required): $fun()")
+        newRuntimeError(s"Too many parameters for IDL function ($argNum is required): $fun()")
     def rtParamTypeError(fun: String, invalid: Object, expectType: String)(implicit ctx: PRC): NCE =
         newRuntimeError(s"Expected '$expectType' type of parameter for IDL function '$fun()', found: $invalid")
     def rtParamNullError(fun: String)(implicit ctx: PRC): NCE =