繁体   English   中英

为什么 Idea 找不到 ANTLR4 生成的源?

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

我有一个项目,我想在一段 Kotlin 代码中使用由 ANTLR4 生成的类。

pom.xml中,ANTLR4 配置如下。

<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>

生成的类被放入target/generated-sources/antlr4

在此处输入图像描述

mvn clean packagemvn clean assembly ,以及在Idea中重建项目导致以下错误:

在此处输入图像描述

请注意,错误仅出现在 Kotlin class Transpiler.kt 中,而不是在测试中。

我该如何解决这个问题(确保 ANTLR4 生成的类可以在 Kotlin 代码中使用)?

更新 1:按照@Bart Kiers 的建议移动语法文件并执行mvn clean antlr4:antlr4后,Idea 中的错误消失了。 但是mvn clean antlr4:antlr4 install仍然会导致构建错误

[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

如果您执行以下操作,它应该可以工作:

  1. Deplorable.g4移动到src/main/antlr4/com/dpisarenko/deplorable/ (注意你把它放在了src/main/antlr4/com.dpisarenko.deplorable/ !)
  2. 运行mvn clean antlr4:antlr4
  3. 如果尚未完成,请将target/generated-sources/antlr4为“Generated Sources Root”(在您的 IDE 和 select Mark Directory as右键单击它)

如果没有,请尝试使用最新的 ANTLR4 版本: 4.9.1 (不仅是工具和运行时,还适用于antlr4-maven-plugin )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM