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/02/21 19:50:41 UTC
[incubator-nlpcraft] branch NLPCRAFT-247 updated: Create
NCMacroCompiler.scala
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 81a40d4 Create NCMacroCompiler.scala
81a40d4 is described below
commit 81a40d40c6e76e80c9e3bb437803093d9db54153
Author: Aaron Radzinski <ar...@apache.org>
AuthorDate: Sun Feb 21 11:50:31 2021 -0800
Create NCMacroCompiler.scala
---
.../nlpcraft/common/makro/NCMacroCompiler.scala | 108 +++++++++++++++++++++
1 file changed, 108 insertions(+)
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
new file mode 100644
index 0000000..71a9e13
--- /dev/null
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/makro/NCMacroCompiler.scala
@@ -0,0 +1,108 @@
+/*
+ * 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.common.makro
+
+import com.typesafe.scalalogging.LazyLogging
+import org.antlr.v4.runtime.tree.ParseTreeWalker
+import org.antlr.v4.runtime.{BaseErrorListener, CharStreams, CommonTokenStream, RecognitionException, Recognizer}
+import org.apache.nlpcraft.common.makro.antlr4._
+import org.apache.nlpcraft.common._
+
+/**
+ *
+ */
+object NCMacroCompiler extends LazyLogging {
+ /**
+ *
+ */
+ class FiniteStateMachine extends NCMacroDslBaseListener {
+ /**
+ *
+ * @return
+ */
+ def getExpandedMacro(): Set[String] = ???
+ }
+
+ /**
+ * Custom error handler.
+ */
+ 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
+ * @param charPos
+ * @param msg
+ * @param e
+ */
+ override def syntaxError(
+ recognizer: Recognizer[_, _],
+ offendingSymbol: scala.Any,
+ 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)
+ }
+ }
+
+ /**
+ *
+ * @param in Macro to expand.
+ * @return Expanded macro as a set of finite strings.
+ */
+ def compile(in: String): Set[String] = {
+ // ANTLR4 armature.
+ val lexer = new NCMacroDslLexer(CharStreams.fromString(in))
+ val tokens = new CommonTokenStream(lexer)
+ val parser = new NCMacroDslParser(tokens)
+
+ // Set custom error handlers.
+ lexer.removeErrorListeners()
+ parser.removeErrorListeners()
+ lexer.addErrorListener(new CompilerErrorListener(in))
+ parser.addErrorListener(new CompilerErrorListener(in))
+
+ // State automata.
+ val fsm = new FiniteStateMachine
+
+ // Parse the input DSL and walk built AST.
+ (new ParseTreeWalker).walk(fsm, parser.line())
+
+ // Return the expanded macro.
+ fsm.getExpandedMacro()
+ }
+}