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

[new-parser] Anonymous classes not supported #5932

Closed
yurloc opened this issue May 13, 2024 · 4 comments
Closed

[new-parser] Anonymous classes not supported #5932

yurloc opened this issue May 13, 2024 · 4 comments
Assignees

Comments

@yurloc
Copy link
Contributor

yurloc commented May 13, 2024

Parent issue

Failing tests

  • org.drools.mvel.integrationtests.Misc2Test#testJittingFunctionReturningAnInnerClass

Notes

At the end of line 3, a semi is expected instead of class body. This is because DRL6Expressions.g4 overrides the classCreatorRest rule and does not allow classBody?.

Rule code snippet

import java.util.function.Function
function Function<String, Integer> f() {
    return new Function<String, Integer>() {
        public Integer apply(String s) {
            return s.length();
        }
    };
}

Error output

### parse : ANTLR4_PARSER_ENABLED = true
line 3:43 missing ';' at '{'
line 4:23 missing '.' at 'apply'
line 4:28 mismatched input '(' expecting '.'
line 4:36 extraneous input 's' expecting ')'
line 4:39 extraneous input '{' expecting {'abstract', 'class', 'final', 'interface', 'private', 'protected', 'public', 'static', 'strictfp', 'module', 'open', 'requires', 'exports', 'opens', 'to', 'uses', 'provides', 'with', 'transitive', 'var', 'yield', 'record', 'sealed', 'permits', 'non-sealed', '@', IDENTIFIER}
11:06:45.941 [main] ERROR o.d.c.k.b.impl.AbstractKieProject.buildKnowledgePackages:280 - Unable to build KieBaseModel:KieBaseModelName
[3,43]: missing ';' at '{'
[4,23]: missing '.' at 'apply'
[4,28]: mismatched input '(' expecting '.'
[4,36]: extraneous input 's' expecting ')'
[4,39]: extraneous input '{' expecting {'abstract', 'class', 'final', 'interface', 'private', 'protected', 'public', 'static', 'strictfp', 'module', 'open', 'requires', 'exports', 'opens', 'to', 'uses', 'provides', 'with', 'transitive', 'var', 'yield', 'record', 'sealed', 'permits', 'non-sealed', '@', IDENTIFIER}
[0,0]: Parser returned a null Package

11:06:45.949 [main] ERROR o.d.testcoverage.common.util.KieUtil.getKieBuilderFromKieFileSystem:165 - KieBuilder errors: [Message [id=1, kieBase=KieBaseModelName, level=ERROR, path=rules1.drl, line=3, column=0
   text=missing ';' at '{'], Message [id=2, kieBase=KieBaseModelName, level=ERROR, path=rules1.drl, line=4, column=0
   text=missing '.' at 'apply'], Message [id=3, kieBase=KieBaseModelName, level=ERROR, path=rules1.drl, line=4, column=0
   text=mismatched input '(' expecting '.'], Message [id=4, kieBase=KieBaseModelName, level=ERROR, path=rules1.drl, line=4, column=0
   text=extraneous input 's' expecting ')'], Message [id=5, kieBase=KieBaseModelName, level=ERROR, path=rules1.drl, line=4, column=0
   text=extraneous input '{' expecting {'abstract', 'class', 'final', 'interface', 'private', 'protected', 'public', 'static', 'strictfp', 'module', 'open', 'requires', 'exports', 'opens', 'to', 'uses', 'provides', 'with', 'transitive', 'var', 'yield', 'record', 'sealed', 'permits', 'non-sealed', '@', IDENTIFIER}], Message [id=6, kieBase=KieBaseModelName, level=ERROR, path=rules1.drl, line=0, column=0
   text=Parser returned a null Package]]

java.lang.AssertionError: [[Message [id=1, kieBase=KieBaseModelName, level=ERROR, path=rules1.drl, line=3, column=0
   text=missing ';' at '{'], Message [id=2, kieBase=KieBaseModelName, level=ERROR, path=rules1.drl, line=4, column=0
   text=missing '.' at 'apply'], Message [id=3, kieBase=KieBaseModelName, level=ERROR, path=rules1.drl, line=4, column=0
   text=mismatched input '(' expecting '.'], Message [id=4, kieBase=KieBaseModelName, level=ERROR, path=rules1.drl, line=4, column=0
   text=extraneous input 's' expecting ')'], Message [id=5, kieBase=KieBaseModelName, level=ERROR, path=rules1.drl, line=4, column=0
   text=extraneous input '{' expecting {'abstract', 'class', 'final', 'interface', 'private', 'protected', 'public', 'static', 'strictfp', 'module', 'open', 'requires', 'exports', 'opens', 'to', 'uses', 'provides', 'with', 'transitive', 'var', 'yield', 'record', 'sealed', 'permits', 'non-sealed', '@', IDENTIFIER}], Message [id=6, kieBase=KieBaseModelName, level=ERROR, path=rules1.drl, line=0, column=0
   text=Parser returned a null Package]]] 
Expecting empty but was: [Message [id=1, kieBase=KieBaseModelName, level=ERROR, path=rules1.drl, line=3, column=0
   text=missing ';' at '{'],
    Message [id=2, kieBase=KieBaseModelName, level=ERROR, path=rules1.drl, line=4, column=0
   text=missing '.' at 'apply'],
    Message [id=3, kieBase=KieBaseModelName, level=ERROR, path=rules1.drl, line=4, column=0
   text=mismatched input '(' expecting '.'],
    Message [id=4, kieBase=KieBaseModelName, level=ERROR, path=rules1.drl, line=4, column=0
   text=extraneous input 's' expecting ')'],
    Message [id=5, kieBase=KieBaseModelName, level=ERROR, path=rules1.drl, line=4, column=0
   text=extraneous input '{' expecting {'abstract', 'class', 'final', 'interface', 'private', 'protected', 'public', 'static', 'strictfp', 'module', 'open', 'requires', 'exports', 'opens', 'to', 'uses', 'provides', 'with', 'transitive', 'var', 'yield', 'record', 'sealed', 'permits', 'non-sealed', '@', IDENTIFIER}],
    Message [id=6, kieBase=KieBaseModelName, level=ERROR, path=rules1.drl, line=0, column=0
   text=Parser returned a null Package]]

	at org.drools.testcoverage.common.util.KieUtil.getKieBuilderFromKieFileSystem(KieUtil.java:169)
	at org.drools.testcoverage.common.util.KieUtil.getKieBuilderFromKieFileSystem(KieUtil.java:134)
	at org.drools.testcoverage.common.util.KieUtil.buildAndInstallKieModuleIntoRepo(KieUtil.java:78)
	at org.drools.testcoverage.common.util.KieUtil.buildAndInstallKieModuleIntoRepo(KieUtil.java:72)
	at org.drools.testcoverage.common.util.KieUtil.getKieModuleFromResources(KieUtil.java:249)
	at org.drools.testcoverage.common.util.KieUtil.getKieModuleFromResources(KieUtil.java:238)
	at org.drools.testcoverage.common.util.KieBaseUtil.getKieBaseFromKieModuleFromResources(KieBaseUtil.java:102)
	at org.drools.testcoverage.common.util.KieBaseUtil.getKieBaseFromKieModuleFromDrl(KieBaseUtil.java:115)
	at org.drools.mvel.integrationtests.Misc2Test.testJittingFunctionReturningAnInnerClass(Misc2Test.java:8019)
@yurloc
Copy link
Contributor Author

yurloc commented May 13, 2024

good-first-issue

@tkobayas
Copy link
Contributor

functiondef : DRL_FUNCTION typeTypeOrVoid? drlIdentifier formalParameters drlBlock ;

Probably drlBlock can be replaced with LBRACE chunk? RBRACE just like old parser.

@yurloc
Copy link
Contributor Author

yurloc commented May 21, 2024

functiondef : DRL_FUNCTION typeTypeOrVoid? drlIdentifier formalParameters drlBlock ;

Probably drlBlock can be replaced with LBRACE chunk? RBRACE just like old parser.

That's the easy solution. I'm not against it as our goal for the ANTL4 rewrite is not to improve the parser's capability.

But another solution exists.

parseTree

If you traverse the parser rules according to the parse tree above, you'll find out that classCreatorRest is declared in DRL6Expressions.g4 and it effectively overrides a rule with the same name in JavaParser.g4. The difference between the two rules is that the one in DRL6Expressions.g4 does not allow class body:

classCreatorRest
: arguments //classBody? //sotty: restored classBody to allow for inline, anonymous classes
;

The obvious fix would be to uncomment classBody? but that would be an unnecessary change in the DRL6Expressions.g4 grammar. I would prefer simply renaming the classCreatorRest rule in DRL6Expressions.g4 leaving its behavior unchanged and letting the drlCreator rule in DRLParser.g4 use the classCreatorRest from JavaParser.g4.

@tkobayas
Copy link
Contributor

tkobayas commented Jun 4, 2024

Fixed by #5979

@tkobayas tkobayas closed this as completed Jun 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: 🎯 Done
Development

No branches or pull requests

2 participants