Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to use example class, because I am unable to import classes #12

Closed
rahullohra2903 opened this issue May 13, 2020 · 4 comments
Closed

Comments

@rahullohra2903
Copy link

Hi, Please help in what I am doing wrong
I am unable to use this AstSource

Dependencies used -

    implementation 'com.github.drieks.antlr-kotlin:antlr-kotlin-runtime-jvm:b09d76328'
    implementation 'com.github.drieks.antlr-kotlin:antlr-kotlin-runtime:b09d76328'
    api("com.github.kotlinx.ast:grammar-kotlin-parser-antlr-kotlin:b09d76328")

Sample code -

    val source = AstSource.File(
        "grammar-kotlin-parser-antlr-kotlin/src/jvmTest/kotlin/kotlinx/ast/example/ExampleMain.kt"
    )
    val kotlinFile = KotlinGrammarAntlrKotlinParser.parseKotlinFile(source)
    kotlinFile.summary(attachRawAst = false)
        .onSuccess { astList ->
            astList.forEach(Ast::print)
        }.onFailure { errors ->
            errors.forEach(::println)
        }
}

Error -
Unable to import class - AstSource

@drieks
Copy link
Collaborator

drieks commented May 13, 2020

Hi @rahullohra2903,
can you show me the full gradle file(s)?

@rahullohra2903
Copy link
Author

rahullohra2903 commented May 15, 2020

Here are the files @drieks
settings.gradle

rootProject.name = 'KotlinAst'
enableFeaturePreview("GRADLE_METADATA")

build.gradle

buildscript {
    repositories {
        mavenCentral()
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

plugins {
    id 'groovy'
    id 'java'
    id 'org.jetbrains.kotlin.jvm' version '1.3.72'
    id 'antlr'
}

group 'org.example'
version '1.0-SNAPSHOT'


repositories {
    mavenCentral()
    jcenter()
    maven { url "https://jitpack.io" }
}

dependencies {

    compile 'org.codehaus.groovy:groovy-all:2.3.11'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
    testCompile group: 'junit', name: 'junit', version: '4.12'
    implementation 'com.github.drieks.antlr-kotlin:antlr-kotlin-gradle-plugin:b09d76328'
    implementation 'com.github.drieks.antlr-kotlin:antlr-kotlin-runtime-jvm:b09d76328'
    implementation 'com.github.drieks.antlr-kotlin:antlr-kotlin-runtime:b09d76328'
}

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

@drieks
Copy link
Collaborator

drieks commented May 15, 2020

Hi @rahullohra2903,

I'm not sure what you are trying to do. You need com.github.drieks.antlr-kotlin:antlr-kotlin-gradle-plugin if you want to convert a ANTLR-Grammar into a parser written in Kotlin. com.github.kotlinx.ast:grammar-kotlin-parser-antlr-kotlin-jvm already contains such a parser, created from the official kotlin language grammar (https://kotlinlang.org/docs/reference/grammar.html).

Because you opened the Issue here, I think you don't want to create a grammar, therefore you don't need antlr-kotlin directly. com.github.kotlinx.ast:grammar-kotlin-parser-antlr-kotlin-jvm contains a dependency to antlr-kotlin.

You also don't need the groovy, java and antlr plugins to just run the example. The buildscript part is also not required.

Please try this build.gradle file and yours settings.gradle:

plugins {
    id 'org.jetbrains.kotlin.jvm' version '1.3.72'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
    jcenter()
    maven { url "https://jitpack.io" }
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
    api("com.github.kotlinx.ast:grammar-kotlin-parser-antlr-kotlin-jvm:c35b50fa44")
}

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

kotlinx.ast support different parsers, you can also choose the parser generated by the official java antlr: KotlinGrammarAntlrJavaParser (https://github.com/kotlinx/ast/blob/master/grammar-kotlin-parser-antlr-java/src/test/kotlin/kotlinx/ast/example/ExampleMain.kt)

The API of kotlinx.ast is always the same, only the name of the implementation differs

  • KotlinGrammarAntlrKotlinParser is provided by api("com.github.kotlinx.ast:grammar-kotlin-parser-antlr-kotlin-jvm:c35b50fa44")
  • KotlinGrammarAntlrJavaParser is provided by api("com.github.kotlinx.ast:grammar-kotlin-parser-antlr-java-jvm:c35b50fa44")
  • parseKotlinFile is supported by both versions, just choose one of them.

antlr-java creates parsers written in java, antlr-kotlin creates parsers written in kotlin, but if you are using kotlinx.ast you won't see the parser because it is only internally used by kotlinx.ast. I hope this helps you :-).

Your example for KotlinGrammarAntlrKotlinParser:

package issue12

import kotlinx.ast.common.AstSource
import kotlinx.ast.common.ast.Ast
import kotlinx.ast.common.print
import kotlinx.ast.grammar.kotlin.common.summary
import kotlinx.ast.grammar.kotlin.target.antlr.kotlin.KotlinGrammarAntlrKotlinParser

fun main() {
    val source = AstSource.File(
            "grammar-kotlin-parser-antlr-kotlin/src/jvmTest/kotlin/kotlinx/ast/example/ExampleMain.kt"
    )
    val kotlinFile = KotlinGrammarAntlrKotlinParser.parseKotlinFile(source)
    kotlinFile.summary(attachRawAst = false)
            .onSuccess { astList ->
                astList.forEach(Ast::print)
            }.onFailure { errors ->
                errors.forEach(::println)
            }
}

@rahullohra2903
Copy link
Author

Thank you @drieks for your support . Everything is fine now. The build.gradle that you shared is running fine.

Only 1 dependency
implementation "com.github.kotlinx.ast:grammar-kotlin-parser-antlr-kotlin-jvm:c35b50fa44"

was required in the build.gradle file

Thank you for providing guidance in antlr-java and antlr-kotlin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants