Glory to Russia
Glory to Russia

Reputation: 18712

Why can't Idea not find sources generated by ANTLR4?

I have a project in which I want to use classes generated by ANTLR4 in a piece of Kotlin code.

In pom.xml, ANTLR4 is configured as follows.

<dependencies>
    <dependency>
        <groupId>org.antlr</groupId>
        <artifactId>antlr4-runtime</artifactId>
        <version>4.7.1</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.antlr</groupId>
            <artifactId>antlr4-maven-plugin</artifactId>
            <version>4.7.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>antlr4</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

The generated classes are put into target/generated-sources/antlr4:

enter image description here

mvn clean package, mvn clean assembly, as well as rebuilding the project in Idea lead to the following error:

enter image description here

Note that the errors occur only in the Kotlin class Transpiler.kt, but not in the test.

How can I fix this (make sure that classes generated by ANTLR4 can be used in Kotlin code)?

Update 1: After moving the grammar file as suggested by @Bart Kiers and executing mvn clean antlr4:antlr4, the errors in Idea disappeared. However mvn clean antlr4:antlr4 install still results in build errors:

[ERROR] Failed to execute goal org.jetbrains.kotlin:kotlin-maven-plugin:1.4.21:compile (compile) on project elispt: Compilation failure: Compilation failure: 
[ERROR] /Users/dp118m/dev/misc/elispt/src/main/kotlin/com/dpisarenko/deplorable/Transpiler.kt:[9,21] Unresolved reference: DeplorableLexer
[ERROR] /Users/dp118m/dev/misc/elispt/src/main/kotlin/com/dpisarenko/deplorable/Transpiler.kt:[11,22] Unresolved reference: DeplorableParser
[ERROR] /Users/dp118m/dev/misc/elispt/src/main/kotlin/com/dpisarenko/deplorable/Transpiler.kt:[12,21] Unresolved reference: DeplorableParser

Upvotes: 1

Views: 1629

Answers (1)

Bart Kiers
Bart Kiers

Reputation: 170158

It should work if you do the following:

  1. move Deplorable.g4 to src/main/antlr4/com/dpisarenko/deplorable/ (note that you placed it inside src/main/antlr4/com.dpisarenko.deplorable/!)
  2. run mvn clean antlr4:antlr4
  3. if not already done, mark target/generated-sources/antlr4 as the "Generated Sources Root" (right click it in your IDE and select Mark Directory as)

If not, try using the latest ANTLR4 version: 4.9.1 (not just the tool and runtime, but also for antlr4-maven-plugin).

Upvotes: 4

Related Questions