Skip to content

Commit

Permalink
RD-10639/RD-10608: SQL parameter definition and parsing (also fixes: …
Browse files Browse the repository at this point in the history
…RD-10712, RD-10712, RD-10681, RD-10612) (#368)

- The *snapi-parser* project is now containing also the sql parser, so
it should be renamed to "parsers".
- Sql client now depends on kiama so we can use the positions functions
and also pretty-print, and walk the tree if needed
- The parser is designed in such way to gather information that we need,
and ignore everything else. It should always parse without errors. Some
big queries are added to the tests to make sure they parse.
- The params are kept track of with their defined type and their
occurances.
- Tree, positions, params (with their type, default value, occurrences,
description), return description are available after parsing

---------

Co-authored-by: Benjamin Gaidioz <ben@raw-labs.com>
Co-authored-by: Cesar Matos <cesar@raw-labs.com>
  • Loading branch information
3 people authored Mar 14, 2024
1 parent 2f86003 commit b661522
Show file tree
Hide file tree
Showing 29 changed files with 3,738 additions and 874 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ snapi-truffle/version
snapi-parser/version
python-client/version
snapi-parser/src/main/java/raw/compiler/rql2/generated

snapi-parser/src/main/java/raw/client/sql/generated
70 changes: 40 additions & 30 deletions snapi-parser/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ val generateParser = taskKey[Unit]("Generated antlr4 base parser and lexer")

generateParser := {

// List of output paths
val basePath: String = s"${baseDirectory.value}/src/main/java"
val parsers = List(
(s"${basePath}/raw/compiler/rql2/generated", "raw.compiler.rql2.generated", s"$basePath/raw/snapi/grammar", "Snapi"),
(s"${basePath}/raw/client/sql/generated", "raw.client.sql.generated", s"$basePath/raw/psql/grammar", "Psql")
)

def deleteRecursively(file: File): Unit = {
if (file.isDirectory) {
file.listFiles.foreach(deleteRecursively)
Expand All @@ -148,45 +155,48 @@ generateParser := {
}
}

val basePath: String = s"${baseDirectory.value}/src/main/java"
val s: TaskStreams = streams.value

val outputPath: String = s"${baseDirectory.value}/src/main/java/raw/compiler/rql2/generated"
parsers.foreach(parser => {

val file = new File(outputPath)
if (file.exists()) {
deleteRecursively(file)
}
val outputPath = parser._1

val file = new File(outputPath)
if (file.exists()) {
deleteRecursively(file)
}

val packageName: String = "raw.compiler.rql2.generated"
val packageName: String = parser._2

val jarName = "antlr-4.12.0-complete.jar"
val jarName = "antlr-4.12.0-complete.jar"

val command: String = s"java -jar $basePath/antlr4/$jarName -visitor -package $packageName -o $outputPath"
val command: String = s"java -jar $basePath/antlr4/$jarName -visitor -package $packageName -o $outputPath"

val s: TaskStreams = streams.value
val output = new StringBuilder
val logger = ProcessLogger(
(o: String) => output.append(o + "\n"), // for standard output
(e: String) => output.append(e + "\n") // for standard error
)
val output = new StringBuilder
val logger = ProcessLogger(
(o: String) => output.append(o + "\n"), // for standard output
(e: String) => output.append(e + "\n") // for standard error
)

val grammarPath = s"$basePath/raw/parser/grammar"
val grammarPath = parser._3
val grammarName = parser._4

val lexerResult = s"$command $grammarPath/SnapiLexer.g4".!(logger)
if (lexerResult == 0) {
s.log.info("Lexer code generated successfully")
} else {
s.log.error("Lexer code generation failed with exit code " + lexerResult)
s.log.error("Output:\n" + output.toString)
}
val lexerResult = s"$command $grammarPath/${grammarName}Lexer.g4".!(logger)
if (lexerResult == 0) {
s.log.info("Lexer code generated successfully")
} else {
s.log.error("Lexer code generation failed with exit code " + lexerResult)
s.log.error("Output:\n" + output.toString)
}

val parserResult = s"$command $grammarPath/SnapiParser.g4".!(logger)
if (parserResult == 0) {
s.log.info("Parser code generated successfully")
} else {
s.log.error("Parser code generation failed with exit code " + lexerResult)
s.log.error("Output:\n" + output.toString)
}
val parserResult = s"$command $grammarPath/${grammarName}Parser.g4".!(logger)
if (parserResult == 0) {
s.log.info("Parser code generated successfully")
} else {
s.log.error("Parser code generation failed with exit code " + lexerResult)
s.log.error("Output:\n" + output.toString)
}
})
}

Compile / compile := (Compile / compile).dependsOn(generateParser).value
Expand Down
1 change: 1 addition & 0 deletions snapi-parser/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

module raw.snapi.parser {
exports raw.compiler.rql2.generated;
exports raw.client.sql.generated;

requires org.antlr.antlr4.runtime;
}
Loading

0 comments on commit b661522

Please sign in to comment.