Skip to content

Commit

Permalink
feat(protobuf): add antlr grammar and parser for protobuf files #31
Browse files Browse the repository at this point in the history
This commit introduces support for parsing Protocol Buffers (protobuf) files by adding an ANTLR grammar and associated parser. The antlr grammar (Protobuf3.g4) is based on the protobuf3 specification and includes a comprehensive set of rules for all protobuf syntax elements. The antlr-generated parser (Protobuf3Lexer and Protobuf3Parser) is used by the ProtobufAnalyser class to analyze protobuf files and extract package and message information.

Additionally, the commit includes a test protobuf file (common.proto) and a test case (ProtobufAnalyserTest.kt) to validate the parser. The antlr-generated lexer and parser are configured in build.gradle.kts, and the protobuf grammar file is located in src/main/antlr. The antlr grammar and parser provide a solid foundation for further protobuf analysis and processing within the Chapi project.
  • Loading branch information
phodal committed Oct 25, 2024
1 parent 20537cb commit f56a0f6
Show file tree
Hide file tree
Showing 10 changed files with 1,026 additions and 0 deletions.
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ dependencies {
jacocoAggregation(project(":chapi-ast-kotlin"))
jacocoAggregation(project(":chapi-ast-scala"))
jacocoAggregation(project(":chapi-ast-cpp"))
jacocoAggregation(project(":chapi-ast-protobuf"))

jacocoAggregation(project(":chapi-parser-toml"))
jacocoAggregation(project(":chapi-parser-cmake"))
}
Expand Down
60 changes: 60 additions & 0 deletions chapi-ast-protobuf/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
plugins {
id("antlr")
java
kotlin("jvm")
kotlin("plugin.serialization") version "1.6.10"

`jacoco-conventions`
}

repositories {
mavenCentral()
mavenLocal()
}

dependencies {
antlr("org.antlr:antlr4:4.13.1")

// project deps
implementation(project(":chapi-domain"))

implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.2")

implementation(kotlin("stdlib-jdk8"))
implementation(kotlin("reflect"))
// Kotlin reflection.
testImplementation(kotlin("test"))

// JUnit 5
testImplementation("org.junit.jupiter:junit-jupiter-api:5.6.0")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.6.0")
testRuntimeOnly("org.junit.platform:junit-platform-console:1.6.0")

implementation("org.antlr:antlr4:4.13.1")
implementation("org.antlr:antlr4-runtime:4.13.1")
}

sourceSets.main {
java.srcDirs("${project.buildDir}/generated-src")
}

tasks.generateGrammarSource {
maxHeapSize = "64m"
arguments = arguments + listOf("-package", "chapi.ast.antlr") + listOf("-visitor", "-long-messages")
outputDirectory = file("${project.buildDir}/generated-src/chapi/ast/antlr")
}

tasks.withType<AntlrTask> {

}

tasks.named("compileKotlin") {
dependsOn(tasks.withType<AntlrTask>())
}

tasks.withType<Test> {
useJUnitPlatform()
testLogging {
events("passed", "skipped", "failed")
}
}
Loading

0 comments on commit f56a0f6

Please # to comment.