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 017b662 WIP.
017b662 is described below
commit 017b6626d7c64354e4384bebdd03381907fa2d2a
Author: Aaron Radzinzski <[email protected]>
AuthorDate: Thu Feb 25 19:28:59 2021 -0800
WIP.
---
.../nlpcraft/common/makro/NCMacroCompiler.scala | 20 ++++++++--------
.../nlpcraft/common/makro/antlr4/NCMacroDsl.g4 | 6 -----
.../model/intent/impl/antlr4/NCIntentDsl.g4 | 28 +++++++++++++++-------
.../intent/impl/ver2/NCIntentDslCompiler.scala | 21 +++++++++-------
...lerSpec.scala => NCIntentDslCompilerSpec.scala} | 2 +-
.../dsl/{NCDslSpec.scala => NCIntentDslSpec.scala} | 4 ++--
...lTestModel.scala => NCIntentDslTestModel.scala} | 2 +-
7 files changed, 46 insertions(+), 37 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 340b3e6..dcceda7 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
@@ -66,7 +66,7 @@ object NCMacroCompiler extends LazyLogging {
* @return
*/
private def compilerError(errMsg: String)(implicit ctx:
ParserRuleContext): NCE = {
- val tok = ctx.start
+ val tok = ctx.stop
new NCE(mkCompilerError(errMsg, tok.getLine,
tok.getCharPositionInLine, in))
}
@@ -208,10 +208,10 @@ object NCMacroCompiler extends LazyLogging {
override def syntaxError(
recognizer: Recognizer[_, _],
offendingSymbol: scala.Any,
- line: Int,
- charPos: Int,
+ line: Int, // 1, 2, ...
+ charPos: Int, // 1, 2, ...
msg: String,
- e: RecognitionException): Unit = throw new
NCE(mkCompilerError(msg, line, charPos, in))
+ e: RecognitionException): Unit = throw new
NCE(mkCompilerError(msg, line, charPos - 1, in))
}
/**
@@ -223,16 +223,16 @@ object NCMacroCompiler extends LazyLogging {
*/
private def mkCompilerError(
msg: String,
- line: Int,
- charPos: Int, // 1, 2, ...
+ line: Int, // 1, 2, ...
+ charPos: Int, // 0, 1, 2, ...
in: String
): String = {
val dash = "-" * in.length
- val pos = Math.max(0, charPos - 1)
- val posPtr = dash.substring(0, pos) + r("^") + dash.substring(pos + 1)
- val inPtr = in.substring(0, pos) + r(in.charAt(pos)) +
in.substring(pos + 1)
+ 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 - $msg\n" +
+ s"Macro compiler error at line $line:${charPos + 1} - $msg\n" +
s" |-- ${c("Macro:")} $inPtr\n" +
s" +-- ${c("Location:")} $posPtr"
}
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/makro/antlr4/NCMacroDsl.g4
b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/makro/antlr4/NCMacroDsl.g4
index 9773dc4..679ba01 100644
---
a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/makro/antlr4/NCMacroDsl.g4
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/makro/antlr4/NCMacroDsl.g4
@@ -44,7 +44,6 @@ fragment ESC: '\\' ESC_CHAR;
fragment TXT_CHAR
: [~!@#$%^&*()+._]
| [-=<>/\\;:`'",]
- | '\u00B7'
| 'A'..'Z'
| 'a'..'z'
| '0'..'9'
@@ -56,13 +55,8 @@ fragment TXT_CHAR
| '\u0259'..'\u0292' /* IPA Extensions. */
| '\u02B0'..'\u02FF' /* Spacing modifier letters. */
| '\u203F'..'\u2040'
- | '\u00C0'..'\u00D6'
- | '\u00D8'..'\u00F6'
- | '\u00F8'..'\u02FF'
- | '\u0370'..'\u03FF' /* Greek and Coptic. */
| '\u1F01'..'\u1FFF' /* Greek Extended. */
| '\u0400'..'\u04FF' /* Cyrillic. */
- | '\u037F'..'\u1FFF'
| '\u200C'..'\u200D'
| '\u2070'..'\u218F'
| '\u2C00'..'\u2FEF'
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/antlr4/NCIntentDsl.g4
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/antlr4/NCIntentDsl.g4
index f273be2..2793bda 100644
---
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/antlr4/NCIntentDsl.g4
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/intent/impl/antlr4/NCIntentDsl.g4
@@ -17,7 +17,17 @@
grammar NCIntentDsl;
-intent: intentId orderedDecl? flowDecl? metaDecl? terms EOF;
+// Parser.
+dsl: dslItems EOF;
+dslItems
+ : dslItem
+ | dslItems dslItem
+ ;
+dslItem: intent | pred;
+pred: predId terms;
+predId: 'predicate' ASSIGN ID;
+predRef: 'predicate' LPAR ID RPAR;
+intent: intentId orderedDecl? flowDecl? metaDecl? terms;
intentId: 'intent' ASSIGN ID;
orderedDecl: 'ordered' ASSIGN BOOL;
flowDecl: 'flow' ASSIGN qstring;
@@ -39,7 +49,10 @@ jsonArr
: LBR jsonVal (COMMA jsonVal)* RBR
| LBR RBR
;
-terms: term | terms term;
+terms
+ : termItem
+ | terms termItem;
+termItem: term | predRef;
termEq
: ASSIGN // Do not use conversation.
| TILDA // Use conversation.
@@ -87,6 +100,9 @@ minMaxShortcut
| MULT
;
minMaxRange: LBR INT COMMA INT RBR;
+
+// Lexer.
+COMMENT : ('//' ~[\r\n]* '\r'? '\n' | '/*' .*? '*/' ) -> skip;
SQSTRING: SQUOTE ((~'\'') | ('\\''\''))* SQUOTE; // Allow for \' (escaped
single quote) in the string.
DQSTRING: DQUOTE ((~'"') | ('\\''"'))* DQUOTE; // Allow for \" (escape double
quote) in the string.
BOOL: 'true' | 'false';
@@ -127,8 +143,7 @@ INT: '0' | [1-9] [_0-9]*;
REAL: DOT [0-9]+;
EXP: [Ee] [+\-]? INT;
fragment UNI_CHAR // International chars.
- : '\u00B7'
- | '\u0300'..'\u036F'
+ : '\u0300'..'\u036F'
| '\u00A0'..'\u00FF' /* Latin-1 Supplement. */
| '\u0100'..'\u017F' /* Latin Extended-A. */
| '\u0180'..'\u024F' /* Latin Extended-B. */
@@ -136,13 +151,8 @@ fragment UNI_CHAR // International chars.
| '\u0259'..'\u0292' /* IPA Extensions. */
| '\u02B0'..'\u02FF' /* Spacing modifier letters. */
| '\u203F'..'\u2040'
- | '\u00C0'..'\u00D6'
- | '\u00D8'..'\u00F6'
- | '\u00F8'..'\u02FF'
- | '\u0370'..'\u03FF' /* Greek and Coptic. */
| '\u1F01'..'\u1FFF' /* Greek Extended. */
| '\u0400'..'\u04FF' /* Cyrillic. */
- | '\u037F'..'\u1FFF'
| '\u200C'..'\u200D'
| '\u2070'..'\u218F'
| '\u2C00'..'\u2FEF'
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 4bbbb8c..dc6acf8 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
@@ -220,13 +220,18 @@ object NCIntentDslCompiler extends LazyLogging {
* @param mdlId
* @return
*/
- private def mkSyntaxError(msg: String, line: Int, charPos: Int, dsl:
String, mdlId: String): String = {
+ private def mkSyntaxError(
+ msg: String,
+ line: Int, // 1, 2, ...
+ charPos: Int, // 0, 1, 2, ...
+ dsl: String,
+ mdlId: String): String = {
val dash = "-" * dsl.length
- val pos = Math.max(0, charPos - 1)
- val posPtr = dash.substring(0, pos) + r("^") + dash.substring(pos + 1)
- val dslPtr = dsl.substring(0, pos) + r(dsl.charAt(pos)) +
dsl.substring(pos + 1)
+ val pos = Math.max(0, charPos)
+ val posPtr = dash.substring(0, pos) + r("^") + y(dash.substring(pos +
1))
+ val dslPtr = dsl.substring(0, pos) + r(dsl.charAt(pos)) +
y(dsl.substring(pos + 1))
- s"Intent DSL syntax error at line $line:$charPos - $msg\n" +
+ s"Intent DSL syntax error at line $line:${charPos + 1} - $msg\n" +
s" |-- ${c("Model:")} $mdlId\n" +
s" |-- ${c("Intent:")} $dslPtr\n" +
s" +-- ${c("Location:")} $posPtr"
@@ -263,10 +268,10 @@ object NCIntentDslCompiler extends LazyLogging {
override def syntaxError(
recognizer: Recognizer[_, _],
offendingSymbol: scala.Any,
- line: Int,
- charPos: Int,
+ line: Int, // 1, 2, ...
+ charPos: Int, // 1, 2, ...
msg: String,
- e: RecognitionException): Unit = throw new NCE(mkSyntaxError(msg,
line, charPos, dsl, mdlId))
+ e: RecognitionException): Unit = throw new NCE(mkSyntaxError(msg,
line, charPos - 1, dsl, mdlId))
}
/**
diff --git
a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/dsl/NCDslCompilerSpec.scala
b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/dsl/NCIntentDslCompilerSpec.scala
similarity index 98%
rename from
nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/dsl/NCDslCompilerSpec.scala
rename to
nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/dsl/NCIntentDslCompilerSpec.scala
index b256106..9f8b66e 100644
---
a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/dsl/NCDslCompilerSpec.scala
+++
b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/dsl/NCIntentDslCompilerSpec.scala
@@ -24,7 +24,7 @@ import org.junit.jupiter.api.Test
/**
* Tests for DSL compiler.
*/
-class NCDslCompilerSpec {
+class NCIntentDslCompilerSpec {
/**
*
* @param dsl
diff --git
a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/dsl/NCDslSpec.scala
b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/dsl/NCIntentDslSpec.scala
similarity index 96%
rename from
nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/dsl/NCDslSpec.scala
rename to
nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/dsl/NCIntentDslSpec.scala
index 8c67241..f8ed762 100644
---
a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/dsl/NCDslSpec.scala
+++
b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/dsl/NCIntentDslSpec.scala
@@ -29,7 +29,7 @@ import java.util.Collections
/**
* DSL test model test. Make sure to start up the NLPCraft server before
running this test.
*/
-class NCDslSpec {
+class NCIntentDslSpec {
private var cli: NCTestClient = _
@BeforeEach
@@ -37,7 +37,7 @@ class NCDslSpec {
@throws[IOException]
private[dsl] def setUp(): Unit = {
// Start embedded probe with the test model.
- if (NCEmbeddedProbe.start(null,
Collections.singletonList(classOf[NCDslTestModel].getName))) {
+ if (NCEmbeddedProbe.start(null,
Collections.singletonList(classOf[NCIntentDslTestModel].getName))) {
cli = new NCTestClientBuilder().newBuilder.build
cli.open("nlpcraft.dsl.test")
diff --git
a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/dsl/NCDslTestModel.scala
b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/dsl/NCIntentDslTestModel.scala
similarity index 88%
rename from
nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/dsl/NCDslTestModel.scala
rename to
nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/dsl/NCIntentDslTestModel.scala
index bd64fd8..3c8adb6 100644
---
a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/dsl/NCDslTestModel.scala
+++
b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/dsl/NCIntentDslTestModel.scala
@@ -22,7 +22,7 @@ import org.apache.nlpcraft.model._
/**
* Test model for intent DSL.
*/
-class NCDslTestModel extends
NCModelFileAdapter("src/test/scala/org/apache/nlpcraft/model/intent/dsl/dsl_test_model.yaml")
{
+class NCIntentDslTestModel extends
NCModelFileAdapter("src/test/scala/org/apache/nlpcraft/model/intent/dsl/dsl_test_model.yaml")
{
@NCIntentRef("i1")
private def callback$i4(ctx: NCIntentMatch): NCResult =
NCResult.text("callback$i1")
}