antlr / intellij-plugin-v4

An IntelliJ plugin for ANTLR v4
https://plugins.jetbrains.com/plugin/7358-antlr-v4
BSD 3-Clause "New" or "Revised" License
470 stars 104 forks source link

NullPointerException: Cannot read field "tool" because "g" is null #661

Closed MrRainsdRop closed 1 year ago

MrRainsdRop commented 1 year ago

After I upgraded the plugin version from 1.20 to 1.21, the plugin crashed after opening the project. Here is the error message:

antlr can't process PS.g4

java.lang.NullPointerException: Cannot read field "tool" because "g" is null
    at org.antlr.v4.codegen.CodeGenerator.create(CodeGenerator.java:58)
    at org.antlr.v4.analysis.LeftRecursiveRuleAnalyzer.<init>(LeftRecursiveRuleAnalyzer.java:90)
    at org.antlr.v4.analysis.LeftRecursiveRuleTransformer.translateLeftRecursiveRule(LeftRecursiveRuleTransformer.java:101)
    at org.antlr.v4.analysis.LeftRecursiveRuleTransformer.translateLeftRecursiveRules(LeftRecursiveRuleTransformer.java:71)
    at org.antlr.v4.semantics.SemanticPipeline.process(SemanticPipeline.java:71)
    at org.antlr.v4.Tool.processNonCombinedGrammar(Tool.java:371)
    at org.antlr.v4.Tool.process(Tool.java:359)
    at org.antlr.intellij.plugin.validation.GrammarIssuesCollector.collectGrammarIssues(GrammarIssuesCollector.java:90)
    at org.antlr.intellij.plugin.ANTLRv4ExternalAnnotator.lambda$doAnnotate$0(ANTLRv4ExternalAnnotator.java:39)
    at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:876)
    at org.antlr.intellij.plugin.ANTLRv4ExternalAnnotator.doAnnotate(ANTLRv4ExternalAnnotator.java:38)
    at org.antlr.intellij.plugin.ANTLRv4ExternalAnnotator.doAnnotate(ANTLRv4ExternalAnnotator.java:25)
    at com.intellij.codeInsight.daemon.impl.ExternalToolPass.doAnnotate(ExternalToolPass.java:204)
    at com.intellij.codeInsight.daemon.impl.ExternalToolPass.doAnnotate(ExternalToolPass.java:198)
    at com.intellij.codeInsight.daemon.impl.ExternalToolPass.runChangeAware(ExternalToolPass.java:274)
    at com.intellij.codeInsight.daemon.impl.ExternalToolPass$1.lambda$run$1(ExternalToolPass.java:176)
    at com.intellij.openapi.progress.impl.CoreProgressManager.lambda$runProcess$2(CoreProgressManager.java:186)
    at com.intellij.openapi.progress.impl.CoreProgressManager.lambda$executeProcessUnderProgress$13(CoreProgressManager.java:604)
    at com.intellij.openapi.progress.impl.CoreProgressManager.registerIndicatorAndRun(CoreProgressManager.java:679)
    at com.intellij.openapi.progress.impl.CoreProgressManager.computeUnderProgress(CoreProgressManager.java:635)
    at com.intellij.openapi.progress.impl.CoreProgressManager.executeProcessUnderProgress(CoreProgressManager.java:603)
    at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:60)
    at com.intellij.openapi.progress.impl.CoreProgressManager.runProcess(CoreProgressManager.java:173)
    at com.intellij.openapi.progress.util.BackgroundTaskUtil.runUnderDisposeAwareIndicator(BackgroundTaskUtil.java:361)
    at com.intellij.openapi.progress.util.BackgroundTaskUtil.runUnderDisposeAwareIndicator(BackgroundTaskUtil.java:339)
    at com.intellij.codeInsight.daemon.impl.ExternalToolPass$1.run(ExternalToolPass.java:175)
    at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:350)
    at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:340)
    at com.intellij.util.ui.update.MergingUpdateQueue.doExecute(MergingUpdateQueue.java:297)
    at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:280)
    at com.intellij.util.ui.update.MergingUpdateQueue.run(MergingUpdateQueue.java:245)
    at com.intellij.util.concurrency.QueueProcessor.runSafely(QueueProcessor.java:249)
    at com.intellij.util.Alarm$Request.runSafely(Alarm.java:365)
    at com.intellij.util.Alarm$Request.run(Alarm.java:354)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at com.intellij.util.concurrency.SchedulingWrapper$MyScheduledFutureTask.run(SchedulingWrapper.java:272)
    at com.intellij.util.concurrency.BoundedTaskExecutor.doRun(BoundedTaskExecutor.java:248)
    at com.intellij.util.concurrency.BoundedTaskExecutor.access$200(BoundedTaskExecutor.java:28)
    at com.intellij.util.concurrency.BoundedTaskExecutor$1.executeFirstTaskAndHelpQueue(BoundedTaskExecutor.java:221)
    at com.intellij.util.ConcurrencyUtil.runUnderThreadName(ConcurrencyUtil.java:227)
    at com.intellij.util.concurrency.BoundedTaskExecutor$1.run(BoundedTaskExecutor.java:210)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
    at java.base/java.util.concurrent.Executors$PrivilegedThreadFactory$1$1.run(Executors.java:702)
    at java.base/java.util.concurrent.Executors$PrivilegedThreadFactory$1$1.run(Executors.java:699)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at java.base/java.util.concurrent.Executors$PrivilegedThreadFactory$1.run(Executors.java:699)
    at java.base/java.lang.Thread.run(Thread.java:833)

The problem disappeared when I rolled back the version of the plugin to version 1.20. It looks like there are some issues in the update content of the new version.

Related version information: Error plug-in version: 1.21 IDE Information: Pycharm 2023.1.4(Community Edition)

If you need more information, please let me know.

bjansen commented 1 year ago

Does it happen only once, or every time you open this project?

MrRainsdRop commented 1 year ago

Does it happen only once, or every time you open this project?

Every time, and the error message is the same.

MrRainsdRop commented 1 year ago

Does it happen only once, or every time you open this project?

Every time, and the error message is the same.

I tried to reproduce the problem, and here is my code:

grammar MyGrammar;

options {
    language=Python2;
}

WS: [ \t\r\n]+ -> skip;

COMMA: ',';
NUMBER: [0-9]+;

math: NUMBER (COMMA NUMBER)*;

image

bjansen commented 1 year ago

There are two problems: a bug in ANTLR itself (not the IntelliJ plugin), and a problem with your grammar.

The error in ANTLR:

When an exception is caught in CodeGenerator, the following catch is called:

catch (Exception e) {
    g.tool.errMgr.toolError(ErrorType.CANNOT_CREATE_TARGET_GENERATOR, e, language);
    return null;
}

But g is null in LeftRecursiveRuleAnalyzer:

// use codegen to get correct language templates; that's it though
codegenTemplates = CodeGenerator.create(tool, null, language).getTemplates();

The problem with your grammar:

@MrRainsdRop when I try to reproduce your issue, the following exception is thrown, causing the bug:

java.lang.ClassNotFoundException: org.antlr.v4.codegen.target.Python2Target PluginClassLoader(plugin=PluginDescriptor(name=ANTLR v4, id=org.antlr.intellij.plugin, descriptorPath=plugin.xml, path=~/Dev/projets/perso/intellij-plugin-v4/build/idea-sandbox/plugins/antlr-intellij-plugin-v4, version=next-SNAPSHOT, package=null, isBundled=false), packagePrefix=null, instanceId=16, state=active)

Unfortunately, the Python2 target was removed in ANTLR 4.13.1, I'm afraid you'll have to use Python3 instead.

bjansen commented 1 year ago

@parrt could you transfer this issue to https://github.com/antlr/antlr4 please?

parrt commented 1 year ago

Copied the link over. https://github.com/antlr/antlr4/issues/4432. closing