Strumenta / antlr-kotlin

Support for Kotlin as a target for ANTLR 4
Apache License 2.0
221 stars 47 forks source link

antlr-kotlin with Kotlin / Python3 / JS Lexers and Parsers #186

Closed sdfgsdfgd closed 2 months ago

sdfgsdfgd commented 2 months ago

hi how are you all

I'm trying to use 3 different lexer/parsers for Kotlin, Python and Javascript, via antlr-kotlin runtime.

My purpose is to use Kotlin to be able to parse AST Trees of kotlin/python/JS codes

Kotlin lexer and parser works really nice with

val tokens = CommonTokenStream(KotlinLexer(CharStreams.fromFileName(path)))
val parser = KotlinParser(tokens)

after running the ./gradlew generateKotlinGrammarSource

but with Python3Lexer and Python3Parser, I can't get a CommonTokenStream with

CommonTokenStream(Python3Lexer(CharStreams.fromFileName(path)))

it says Python3Lexer doesn't implement a TokenSource interface. Am I catching a bug or is the python3/python targets not working on antlr-kotlin ?

I only included the 2 .g4 files from the antlr-grammars repository for each of these languages ? am I maybe missing a g4 file

This is my build gradle:

sourceSets {
        val desktopMain by getting {
            kotlin.srcDir("build/generated/src/antlr/main")
            kotlin.srcDir("generatedAntlr")
        }
}

tasks.register<AntlrKotlinTask>("generateKotlinGrammarSource") {
    // Grammar files
    source = fileTree("/Users/x/Desktop/kotlin/PROJECT/composeApp/src/desktopMain/kotlin/antlr/") {
        include("**/*.g4")
    }

    // Output directory
    val outputDir = layout.buildDirectory.dir("generatedAntlr")
    outputDirectory = outputDir.get().asFile
    doFirst {
        outputDir.get().asFile.mkdirs() // Ensure directory is created
    }

    arguments = listOf("-visitor", "-listener", "-package", "net.sdfgsdfg")
    packageName = "net.sdfgsdfg"
}

tasks.named("cleanGenerateKotlinGrammarSource") {
    dependsOn("clean")
}

These are the python files I got from antlr-grammar /Users/x/Desktop/kotlin/PROJECT/composeApp/src/desktopMain/kotlin/antlr/Python3Lexer.g4 /Users/x/Desktop/kotlin/PROJECT/composeApp/src/desktopMain/kotlin/antlr/Python3Parser.g4

I could find about 2 of these files within the Python directory which should be what I need right ? or do I need some other g4 ?

What could I be doing wrong, this thing is working perfectly with kotlin lexer/parser but not with python or js :/

sdfgsdfgd commented 2 months ago

after generating the grammar sources for python it looks like the Python3LexerBase is unresolved and there are a few errors in Python3Lexer and Python3Parser

Screenshot 2024-06-24 at 11 41 48 AM

What could be going wrong here....... I'm 1 step away from an awesome solution for AST parsing 3 languages, helpppp

sdfgsdfgd commented 2 months ago

ok I had to manually also import the java base classes additionally, for these targets.

everything is working nao

ftomassetti commented 2 months ago

Hi @sdfgsdfgd . I am happy to hear you get this one solved. If I understand correctly there is nothing to be changed in antlr-kotlin, if not explaining how to use base parsers. Correct?