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 5f870b9  WIP.
5f870b9 is described below

commit 5f870b9caf31825ef81a20c78aacd381da304269
Author: Aaron Radzinzski <[email protected]>
AuthorDate: Wed Mar 10 16:21:00 2021 -0800

    WIP.
---
 .../org/apache/nlpcraft/common/ansi/NCAnsi.scala   |   1 +
 .../scala/org/apache/nlpcraft/common/package.scala |  21 +++
 .../intent/compiler/NCDslSyntaxHighlighter.scala   | 196 +++++++++++++++++++++
 .../nlpcraft/probe/mgrs/model/NCModelManager.scala |  13 +-
 4 files changed, 223 insertions(+), 8 deletions(-)

diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ansi/NCAnsi.scala 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ansi/NCAnsi.scala
index 7066cad..eaccf5b 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ansi/NCAnsi.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ansi/NCAnsi.scala
@@ -104,6 +104,7 @@ sealed trait NCAnsi extends LazyLogging {
     def ansiBlack(s: Any): String = s"$ansiBlackFg${s.toString}s$ansiReset"
     def ansiWhite(s: Any): String = s"$ansiWhiteFg${s.toString}$ansiReset"
     def ansiBlue(s: Any): String = s"$ansiBlueFg${s.toString}$ansiReset"
+    def ansiMagenta(s: Any): String = s"$ansiMagentaFg${s.toString}$ansiReset"
     def ansiBold(s: Any): String = s"$ansiBold${s.toString}$ansiReset"
 
     // Erase functions.
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/package.scala 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/package.scala
index ca87d7e..c4d8bad 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/package.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/package.scala
@@ -43,6 +43,7 @@ package object common {
     final val MDL_META_MODEL_CLASS_KEY = "__NLPCRAFT_MDL_CLASS_NAME"
 
     def G: String = ansiGreenFg
+    def M: String = ansiMagentaFg
     def R: String = ansiRedFg
     def C: String = ansiCyanFg
     def Y: String = ansiYellowFg
@@ -51,6 +52,7 @@ package object common {
     def BO: String = ansiBold
     def K: String = ansiBlackFg
     def GB: String = ansiGreenBg
+    def MB: String = ansiMagentaBg
     def RB: String = ansiRedBg
     def CB: String = ansiCyanBg
     def YB: String = ansiYellowBg
@@ -59,12 +61,21 @@ package object common {
     def KB: String = ansiBlackBg
     def RST: String = ansiReset
     def g(s: Any): String = s"$G${s.toString}$RST"
+    def m(s: Any): String = s"$M${s.toString}$RST"
     def r(s: Any): String = s"$R${s.toString}$RST"
     def c(s: Any): String = s"$C${s.toString}$RST"
     def y(s: Any): String = s"$Y${s.toString}$RST"
     def w(s: Any): String = s"$W${s.toString}$RST"
     def b(s: Any): String = s"$B${s.toString}$RST"
     def k(s: Any): String = s"$K${s.toString}$RST"
+    def green(s: Any): String = s"$G${s.toString}$RST"
+    def magenta(s: Any): String = s"$M${s.toString}$RST"
+    def red(s: Any): String = s"$R${s.toString}$RST"
+    def cyan(s: Any): String = s"$C${s.toString}$RST"
+    def yellow(s: Any): String = s"$Y${s.toString}$RST"
+    def white(s: Any): String = s"$W${s.toString}$RST"
+    def blue(s: Any): String = s"$B${s.toString}$RST"
+    def black(s: Any): String = s"$K${s.toString}$RST"
     def gb(s: Any): String = s"$GB${s.toString}$RST"
     def rb(s: Any): String = s"$RB${s.toString}$RST"
     def cb(s: Any): String = s"$CB${s.toString}$RST"
@@ -72,10 +83,20 @@ package object common {
     def wb(s: Any): String = s"$WB${s.toString}$RST"
     def bb(s: Any): String = s"$BB${s.toString}$RST"
     def kb(s: Any): String = s"$KB${s.toString}$RST"
+    def greenBg(s: Any): String = s"$GB${s.toString}$RST"
+    def magentaBg(s: Any): String = s"$MB${s.toString}$RST"
+    def redBg(s: Any): String = s"$RB${s.toString}$RST"
+    def cyanBg(s: Any): String = s"$CB${s.toString}$RST"
+    def yellowBg(s: Any): String = s"$YB${s.toString}$RST"
+    def whiteBg(s: Any): String = s"$WB${s.toString}$RST"
+    def blueBg(s: Any): String = s"$BB${s.toString}$RST"
+    def blackBg(s: Any): String = s"$KB${s.toString}$RST"
 
     // Effect shortcuts...
     def rv(s: Any): String = s"$ansiReversed${s.toString}$RST"
     def bo(s: Any): String = s"$ansiBold${s.toString}$RST"
+    def reverse(s: Any): String = s"$ansiReversed${s.toString}$RST"
+    def bold(s: Any): String = s"$ansiBold${s.toString}$RST"
 
     /**
      * Pimps integers with KB, MB, GB units of measure.
diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/compiler/NCDslSyntaxHighlighter.scala
 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/compiler/NCDslSyntaxHighlighter.scala
new file mode 100644
index 0000000..d1fe9cf
--- /dev/null
+++ 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/compiler/NCDslSyntaxHighlighter.scala
@@ -0,0 +1,196 @@
+/*
+ * 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
+ *
+ *      http://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.model.intent.compiler
+
+import org.apache.nlpcraft.common._
+
+import java.util.StringTokenizer
+import java.util.regex.Pattern
+
+/**
+  * 
+  */
+object NCDslSyntaxHighlighter {
+    private final val NUM_REGEX = Pattern.compile("-?[0-9]+")
+    private final val STR1_REGEX = Pattern.compile("'[^']*'")
+    private final val STR2_REGEX = Pattern.compile(""""[^"]*"""")
+
+    private val KEYWORDS = Seq("flow", "fragment", "intent", "meta", "term", 
"ordered")
+    private val LITERALS = Seq("true", "false", "null")
+    private val FUNS = Seq(
+        "meta_token",
+        "meta_part",
+        "meta_model",
+        "meta_intent",
+        "meta_req",
+        "meta_user",
+        "meta_company",
+        "meta_sys",
+        "meta_conv",
+        "meta_frag",
+        "json",
+        "if",
+        "id",
+        "this",
+        "part",
+        "parts",
+        "ancestors",
+        "parent",
+        "groups",
+        "value",
+        "aliases",
+        "start_idx",
+        "end_idx",
+        "req_id",
+        "req_normtext",
+        "req_tstamp",
+        "req_addr",
+        "req_agent",
+        "user_id",
+        "user_fname",
+        "user_lname",
+        "user_email",
+        "user_admin",
+        "user_signup_tstamp",
+        "comp_id",
+        "comp_name",
+        "comp_website",
+        "comp_country",
+        "comp_region",
+        "comp_city",
+        "comp_addr",
+        "comp_postcode",
+        "trim",
+        "strip",
+        "uppercase",
+        "lowercase",
+        "is_alpha",
+        "is_alphanum",
+        "is_whitespace",
+        "is_num",
+        "is_numspace",
+        "is_alphaspace",
+        "is_alphanumspace",
+        "substring",
+        "charAt",
+        "regex",
+        "soundex",
+        "split",
+        "split_trim",
+        "replace",
+        "abs",
+        "ceil",
+        "floor",
+        "rint",
+        "round",
+        "signum",
+        "sqrt",
+        "cbrt",
+        "pi",
+        "euler",
+        "acos",
+        "asin",
+        "atan",
+        "cos",
+        "sin",
+        "tan",
+        "cosh",
+        "sinh",
+        "tanh",
+        "atn2",
+        "degrees",
+        "radians",
+        "exp",
+        "expm1",
+        "hypot",
+        "log",
+        "log10",
+        "log1p",
+        "pow",
+        "rand",
+        "square",
+        "list",
+        "map",
+        "get",
+        "index",
+        "has",
+        "tail",
+        "add",
+        "remove",
+        "first",
+        "last",
+        "keys",
+        "values",
+        "length",
+        "count",
+        "take",
+        "drop",
+        "size",
+        "reverse",
+        "is_empty",
+        "non_empty",
+        "to_string",
+        "avg",
+        "max",
+        "min",
+        "stdev",
+        "sum",
+        "year",
+        "month",
+        "day_of_month",
+        "day_of_week",
+        "day_of_year",
+        "hour",
+        "minute",
+        "second",
+        "week_of_month",
+        "week_of_year",
+        "quarter",
+        "now"
+    )
+    
+    /**
+      * Returns given DSL string with DSL syntax highlighted.
+      * 
+      * @param dsl DSL string to highlight.
+      * @return
+      */
+    def color(dsl: String): String = {
+        val toks = new StringTokenizer(dsl, " \r\n[]{}()=,~/", true)
+        var res = new StringBuilder
+        
+        while (toks.hasMoreTokens) {
+            val tok = toks.nextToken()
+            
+            if (KEYWORDS.contains(tok))
+                res ++= (if (tok == "intent") bold(cyan(tok)) else blue(tok))
+            else if (LITERALS.contains(tok))
+                res ++= green(tok)
+            else if (FUNS.contains(tok))
+                res ++= yellow(tok)
+            else if (NUM_REGEX.matcher(tok).matches())
+                res ++= green(tok)
+            else if (STR1_REGEX.matcher(tok).matches() || 
STR2_REGEX.matcher(tok).matches())
+                res ++= magenta(tok)
+            else
+                res ++= tok
+        }
+        
+        res.toString
+    }
+}
diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/model/NCModelManager.scala
 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/model/NCModelManager.scala
index 600d037..8991339 100644
--- 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/model/NCModelManager.scala
+++ 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/model/NCModelManager.scala
@@ -21,6 +21,7 @@ import io.opencensus.trace.Span
 import org.apache.nlpcraft.common._
 import org.apache.nlpcraft.common.ascii.NCAsciiTable
 import org.apache.nlpcraft.model._
+import org.apache.nlpcraft.model.intent.compiler.NCDslSyntaxHighlighter
 import org.apache.nlpcraft.probe.mgrs.NCProbeModel
 import org.apache.nlpcraft.probe.mgrs.deploy._
 
@@ -70,14 +71,10 @@ object NCModelManager extends NCService with 
DecorateAsScala {
                         s"Synonyms: $synCnt" + (if (synCnt == 0) s" 
${r("(!)")}" else ""),
                         s"Intents: $intentCnt" + (if (intentCnt == 0) s" 
${r("(!)")}" else "")
                     ),
-                    w.intents
-                        .map(_.dsl)
-                        .flatMap(s ⇒
-                            s
-                            .replaceAll("intent=", s"${g("intent")}=")
-                            .replaceAll("fragment=", s"${b("fragment")}=")
-                            .replaceAll(" term", s"\n  
${c("term")}").split("\n")
-                        )
+                    w.intents.flatMap(i ⇒ i.dsl
+                        .replaceAll(" term", s"\n  term")
+                        .split("\n").map(NCDslSyntaxHighlighter.color)
+                    )
                 )
             })
         }

Reply via email to