Skip to content

Commit

Permalink
Aesthetics (#492)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelbranco80 authored Aug 23, 2024
1 parent fd046fd commit d66f94a
Show file tree
Hide file tree
Showing 1,208 changed files with 8,541 additions and 9,385 deletions.
26 changes: 11 additions & 15 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ writeVersionToFile := {

lazy val root = (project in file("."))
.aggregate(
protocolCompiler,
protocolRaw,
protocolCompiler,
utilsCore,
utilsSources,
compiler,
Expand Down Expand Up @@ -115,12 +115,16 @@ lazy val protocolRaw = (project in file("protocol-raw"))
)

lazy val protocolCompiler = (project in file("protocol-compiler"))
.dependsOn(
protocolRaw % "compile->compile;test->test;protobuf->protobuf"
)
.enablePlugins(ProtobufPlugin)
.settings(
commonSettings,
commonCompileSettings,
testSettings,
ProtobufConfig / version := "3.25.4",
ProtobufConfig / protobufIncludePaths += (protocolRaw / ProtobufConfig / sourceDirectory).value,
// Include the protobuf files in the JAR
Compile / unmanagedResourceDirectories += (ProtobufConfig / sourceDirectory).value
)
Expand All @@ -142,20 +146,16 @@ lazy val snapiParser = (project in file("snapi-parser"))
.settings(
commonSettings,
commonCompileSettings,
javaSrcBasePath := s"${baseDirectory.value}/src/main/java",
parserDefinitions := List(
(
s"${javaSrcBasePath.value}/com/rawlabs/snapi/parser/generated",
s"${(Compile / sourceManaged).value}/java/com/rawlabs/snapi/parser/generated",
"com.rawlabs.snapi.parser.generated",
s"${javaSrcBasePath.value}/com/rawlabs/snapi/parser/grammar",
s"${(Compile / sourceDirectory).value}/java/com/rawlabs/snapi/parser/grammar",
"Snapi"
)
),
Compile / doc := { file("/dev/null") },
compileOrder := CompileOrder.JavaThenScala,
libraryDependencies ++= Seq(
antlr4Runtime
)
libraryDependencies += antlr4Runtime
)

lazy val snapiFrontend = (project in file("snapi-frontend"))
Expand Down Expand Up @@ -274,20 +274,16 @@ lazy val sqlParser = (project in file("sql-parser"))
.settings(
commonSettings,
commonCompileSettings,
javaSrcBasePath := s"${baseDirectory.value}/src/main/java",
parserDefinitions := List(
(
s"${javaSrcBasePath.value}/com/rawlabs/sql/parser/generated",
s"${(Compile / sourceManaged).value}/java/com/rawlabs/sql/parser/generated",
"com.rawlabs.sql.parser.generated",
s"${javaSrcBasePath.value}/com/rawlabs/sql/parser/grammar",
s"${(Compile / sourceDirectory).value}/java/com/rawlabs/sql/parser/grammar",
"Psql"
)
),
Compile / doc := { file("/dev/null") },
compileOrder := CompileOrder.JavaThenScala,
libraryDependencies ++= Seq(
antlr4Runtime
)
libraryDependencies += antlr4Runtime
)

lazy val sqlCompiler = (project in file("sql-compiler"))
Expand Down
1 change: 1 addition & 0 deletions compiler/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
requires com.fasterxml.jackson.datatype.jsr310;
requires com.fasterxml.jackson.datatype.jdk8;
requires raw.utils.core;
requires raw.protocol.raw;
requires raw.protocol.compiler;

exports com.rawlabs.compiler;
Expand Down
43 changes: 7 additions & 36 deletions compiler/src/main/scala/com/rawlabs/compiler/CompilerService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@

package com.rawlabs.compiler

import com.fasterxml.jackson.annotation.{JsonSubTypes, JsonTypeInfo}
import org.graalvm.polyglot.Engine

import java.io.OutputStream
import scala.collection.mutable
import com.fasterxml.jackson.annotation.JsonSubTypes.{Type => JsonType}
import com.rawlabs.utils.core.{RawException, RawService, RawSettings}

// Exception that wraps the underlying error so that it includes the extra debug info.
Expand Down Expand Up @@ -173,40 +171,13 @@ final case class ExecutionSuccess(complete: Boolean) extends ExecutionResponse
final case class ExecutionValidationFailure(errors: List[ErrorMessage]) extends ExecutionResponse
final case class ExecutionRuntimeFailure(error: String) extends ExecutionResponse

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes(
Array(
new JsonType(value = classOf[FormatCodeResponse], name = "formatCode"),
new JsonType(value = classOf[AutoCompleteResponse], name = "autoComplete"),
new JsonType(value = classOf[HoverResponse], name = "hover"),
new JsonType(value = classOf[GoToDefinitionResponse], name = "definition"),
new JsonType(value = classOf[RenameResponse], name = "rename"),
new JsonType(value = classOf[ErrorResponse], name = "error"),
new JsonType(value = classOf[ValidateResponse], name = "validate")
)
)
sealed trait ClientLspResponse
final case class FormatCodeResponse(code: Option[String]) extends ClientLspResponse
final case class HoverResponse(completion: Option[Completion]) extends ClientLspResponse
final case class RenameResponse(positions: Array[Pos]) extends ClientLspResponse
final case class GoToDefinitionResponse(position: Option[Pos]) extends ClientLspResponse
final case class ValidateResponse(messages: List[Message]) extends ClientLspResponse
final case class ErrorResponse(errors: List[ErrorMessage]) extends ClientLspResponse
final case class AutoCompleteResponse(completions: Array[Completion]) extends ClientLspResponse

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes(
Array(
new JsonType(value = classOf[TypeCompletion], name = "tipe"),
new JsonType(value = classOf[FieldCompletion], name = "field"),
new JsonType(value = classOf[LetBindCompletion], name = "bind"),
new JsonType(value = classOf[LetFunCompletion], name = "function"),
new JsonType(value = classOf[LetFunRecCompletion], name = "recursiveFunction"),
new JsonType(value = classOf[FunParamCompletion], name = "functionParameter"),
new JsonType(value = classOf[PackageCompletion], name = "package"),
new JsonType(value = classOf[PackageEntryCompletion], name = "packageEntry")
)
)
final case class FormatCodeResponse(code: Option[String])
final case class HoverResponse(completion: Option[Completion])
final case class RenameResponse(positions: Array[Pos])
final case class GoToDefinitionResponse(position: Option[Pos])
final case class ValidateResponse(messages: List[Message])
final case class AutoCompleteResponse(completions: Array[Completion])

sealed trait Completion
final case class TypeCompletion(name: String, tipe: String) extends Completion
final case class FieldCompletion(name: String, tipe: String) extends Completion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,21 @@
* licenses/APL.txt.
*/

package com.rawlabs.snapi.compiler
package com.rawlabs.compiler

import com.rawlabs.compiler.CompilerServiceProvider
import com.rawlabs.utils.core.RawTestSuite
import org.scalatest.BeforeAndAfterAll

trait Rql2CompilerServiceTestContext extends BeforeAndAfterAll {
trait CompilerServiceTestContext extends BeforeAndAfterAll {
this: RawTestSuite =>

private var instance: Rql2CompilerService = _
private var instance: CompilerService = _

private var language: Set[String] = _

def compilerService: Rql2CompilerService = instance
def compilerService: CompilerService = instance

def setCompilerService(compilerService: Rql2CompilerService): Unit = {
def setCompilerService(compilerService: CompilerService): Unit = {
// Remember the language we have set.
if (compilerService != null) {
language = compilerService.language
Expand Down
71 changes: 34 additions & 37 deletions project/GenParserPlugin.scala
Original file line number Diff line number Diff line change
@@ -1,43 +1,39 @@
package raw.build

import sbt.Keys._
import sbt.{Def, _}
import sbt.Keys.*
import sbt.*
import sbt.plugins.JvmPlugin

import com.jsuereth.sbtpgp.PgpKeys.{publishSigned}
import java.io.{File, FileInputStream, IOException}
import java.net.URL
import java.security.MessageDigest
import scala.util.control.Breaks._

import scala.sys.process._
import scala.util.control.Breaks.*
import scala.sys.process.*

object GenParserPlugin extends AutoPlugin {
override def requires = JvmPlugin
override def trigger: PluginTrigger = noTrigger

// Task keys
object autoImport {
val generateParser = taskKey[Unit]("Generated antlr4 base parser and lexer")
val parserDefinitions = settingKey[List[(String, String, String, String)]]("List of parser definitions with output path first followed by the name of the generated package")
val javaSrcBasePath = settingKey[String]("Java src relative path")
val generateParser = taskKey[Seq[File]]("Generated antlr4 base parser and lexer")
val parserDefinitions = settingKey[List[(String, String, String, String)]](
"List of parser definitions with output path first followed by the name of the generated package"
)
}

import autoImport._

// Provide default settings
override def projectSettings: Seq[Def.Setting[_]] = Seq(

generateParser := {
val s: TaskStreams = streams.value

val basePath: String = javaSrcBasePath.value
val parsers = parserDefinitions.value

// Ensure antlr jar is available
val antlrJarPath = (ThisBuild / baseDirectory).value / "antlr-4.12.0-complete.jar"
val antlrJarChecksum = "88f18a2bfac0dde1009eda5c7dce358a52877faef7868f56223a5bcc15329e43"
ensureAntlrJarAvailable(antlrJarPath.toString(), antlrJarChecksum, s)
ensureAntlrJarAvailable(antlrJarPath, antlrJarChecksum, s)

def deleteRecursively(file: File): Unit = {
if (file.isDirectory) {
Expand All @@ -59,22 +55,22 @@ object GenParserPlugin extends AutoPlugin {
sys.error(s"Java binary not found at $javaBin")
}

parsers.foreach(parser => {
val outputPath = parser._1
val generatedFiles = parsers.flatMap { parser =>
val outputPath: String = parser._1
val file = new File(outputPath)
if (file.exists()) {
deleteRecursively(file)
}
val packageName: String = parser._2
val command: String = s"$javaBin -jar ${antlrJarPath} -visitor -package $packageName -o $outputPath"
val command: String = s"$javaBin -jar $antlrJarPath -visitor -package $packageName -o $outputPath"
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 = parser._3
val grammarName = parser._4
val lexerResult = s"$command $grammarPath/${grammarName}Lexer.g4".!(logger)
val lexerResult = s"$command $grammarPath/${grammarName}Lexer.g4".!(logger)
if (lexerResult == 0) {
s.log.info("Lexer code generated successfully")
} else {
Expand All @@ -90,19 +86,22 @@ object GenParserPlugin extends AutoPlugin {
s.log.error("Output:\n" + output.toString)
throw new RuntimeException("Parser code generation failed.")
}
})
new File(outputPath).listFiles.filter(_.isFile).filter(_.getName.endsWith(".java"))
}

generatedFiles
},
parserDefinitions := List(),
javaSrcBasePath := "",
Compile / compile := (Compile / compile).dependsOn(generateParser).value,
Test / test := (Test / test).dependsOn(generateParser).value,
publish := publish.dependsOn(generateParser).value,
publishSigned := publishSigned.dependsOn(generateParser).value
Compile / sourceGenerators += generateParser.taskValue
)

//Ensure ANTLR JAR is downloaded if not present
def ensureAntlrJarAvailable(jarPath: String, expectedChecksum: String, s: TaskStreams, maxRetries: Int = 3): Unit = {
val jarFile = new File(jarPath)
// Ensure ANTLR JAR is downloaded if not present
private def ensureAntlrJarAvailable(
jarFile: File,
expectedChecksum: String,
s: TaskStreams,
maxRetries: Int = 3
): Unit = {
if (!jarFile.exists() || !verifyChecksum(jarFile, expectedChecksum)) {
val jarName = jarFile.getName
val url = s"https://github.com/antlr/website-antlr4/raw/gh-pages/download/$jarName"
Expand Down Expand Up @@ -133,17 +132,15 @@ object GenParserPlugin extends AutoPlugin {
}
}

def verifyChecksum(file: File, expectedChecksum: String): Boolean = {
val buffer = new Array[Byte](64)
val sha256 = MessageDigest.getInstance("SHA-256")
val fis = new FileInputStream(file)
private def verifyChecksum(file: File, expectedChecksum: String): Boolean = {
val buffer = new Array[Byte](64)
val sha256 = MessageDigest.getInstance("SHA-256")
val fis = new FileInputStream(file)

Stream.continually(fis.read(buffer)).takeWhile(_ != -1).foreach { read =>
sha256.update(buffer, 0, read)
}
fis.close()
Stream.continually(fis.read(buffer)).takeWhile(_ != -1).foreach(read => sha256.update(buffer, 0, read))
fis.close()

val fileChecksum = sha256.digest().map("%02x".format(_)).mkString
fileChecksum.equalsIgnoreCase(expectedChecksum)
}
val fileChecksum = sha256.digest().map("%02x".format(_)).mkString
fileChecksum.equalsIgnoreCase(expectedChecksum)
}
}
1 change: 1 addition & 0 deletions protocol-compiler/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

module raw.protocol.compiler {
requires com.google.protobuf;
requires raw.protocol.raw;

exports com.rawlabs.protocol.compiler;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2024 RAW Labs S.A.
*
* Use of this software is governed by the Business Source License
* included in the file licenses/BSL.txt.
*
* As of the Change Date specified in that file, in accordance with
* the Business Source License, use of this software will be governed
* by the Apache License, Version 2.0, included in the file
* licenses/APL.txt.
*/

syntax = "proto3";

option java_package = "com.rawlabs.protocol.compiler";
option java_multiple_files = true;

package com.rawlabs.protocol.compiler;

message PackageDoc {
string description = 1;
optional string info = 2;
optional string warning = 3;
optional string danger = 4;
}

message EntryCompletion {
string name = 1;
EntryDoc doc = 2;
}

message EntryDoc {
string summary = 1;
optional string description = 2;
repeated ExampleDoc examples = 3;
repeated ParamDoc params = 4;
optional ReturnDoc ret = 5;
optional string info = 6;
optional string warning = 7;
optional string danger = 8;
}

message ExampleDoc {
string example = 1;
optional string result = 2;
}

message ParamDoc {
string name = 1;
TypeDoc typeDoc = 2;
string description = 3;
bool isOptional = 4;
bool isVarArg = 5;
optional string info = 6;
optional string warning = 7;
optional string danger = 8;
optional string customSyntax = 9;
}

message ReturnDoc {
string description = 1;
optional TypeDoc retType = 2;
}

message TypeDoc {
repeated string possibleTypes = 1;
}
Loading

0 comments on commit d66f94a

Please sign in to comment.