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

Update DRL6Expressions.g4 to ANTLR 4 #47

Merged

Conversation

yurloc
Copy link
Member

@yurloc yurloc commented Jan 16, 2024

Overview

This PR includes 3 main parts:

  • The expression parser grammar and its updates to ANTLR 4.
  • Bunch of Java classes representing the expression parser's API (DrlExprParser) and some supporting classes.
  • The associated unit test DRLExprParserTest that uses DrlExprParser to parse an expression string and then makes assertions on the resulting descriptors from drools-drl-ast.

Grammar changes

I simply went and fixed all issues one by one until the ANTLR compilation finished without errors. That includes:

ANTLR v4 API changes

Most of the supporting classes from drools-drl-parser can be reused. That's why the dependency is being added. But some classes use the ANTLR v3 API. Those classes were copied here and updated to the v4 API.

formatParserLocation() );
codeAndMessage.add( message );
codeAndMessage.add( "ERR 107" );
} else if ( e instanceof MismatchedNotSetException ) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of these deleted mismatch branches should perhaps be replaced with the InputMismatchException. There is also the LexerNoViableAltException in ANTLR 4. It would be nice to add those together with some negative tests so that we have a better understanding under which conditions these exceptions occur.

See https://github.com/antlr/antlr4/blob/master/doc/parser-rules.md#catching-exceptions.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yurloc Thanks. Please file a JIRA to add tests to cover the use cases. There are drools-test-coverage/test-compiler-integration/src/test/java/org/drools/mvel/compiler/lang/ErrorsParserTest.java, but probably it's not enough.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public void setUp() throws Exception {
new EvaluatorRegistry();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what this is for. It was also removed from the MiscDRLParserTest so I removed it here as well. In the original RuleParserTest there is a comment that says "initializes pluggable operators".

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think EvaluatorRegistry is not required in parser phase, it's fine to remove.

@tkobayas
Copy link
Collaborator

@yurloc Great work!

I think you can also add drools-test-coverage/test-compiler-integration/src/test/java/org/drools/mvel/compiler/lang/DescrDumperTest.java in a next PR.

@yurloc
Copy link
Member Author

yurloc commented Jan 17, 2024

@yurloc Great work!

I think you can also add drools-test-coverage/test-compiler-integration/src/test/java/org/drools/mvel/compiler/lang/DescrDumperTest.java in a next PR.

Yes, I'm already working on the dumper test locally and it proves some of the syntactic predicates cannot be removed without a replacement. I'll see if there is an easy way to work around that and whether to include it in this PR or the next one.

@tkobayas
Copy link
Collaborator

Btw, @mariofusco , do you think refactoring the DRL6Expressions.g4 to meet antlr4 Visitor style has high priority? We did it for DRLParser.g4, but it was because DRL6Parser.java is hard-coded so it's hard to maintain. However, DRL6Expressions.g4 can generate Java code with antlr4.

A) Refactor DRL6Expressions.g4 to meet antlr4 Visitor style and then migrate the parsers to drools repo
or
B) Migrate the parsers to drools repo right away and go ahead for detecting any other issues with the drools test cases (= the refactoring DRL6Expressions.g4 is a parallel task)

@mariofusco
Copy link
Member

Btw, @mariofusco , do you think refactoring the DRL6Expressions.g4 to meet antlr4 Visitor style has high priority? We did it for DRLParser.g4, but it was because DRL6Parser.java is hard-coded so it's hard to maintain. However, DRL6Expressions.g4 can generate Java code with antlr4.

A) Refactor DRL6Expressions.g4 to meet antlr4 Visitor style and then migrate the parsers to drools repo or B) Migrate the parsers to drools repo right away and go ahead for detecting any other issues with the drools test cases (= the refactoring DRL6Expressions.g4 is a parallel task)

I think that option B) is much more pragmatic and will allow us to test and use this work also directly into drools as soon as possible. Refactoring DRL6Expressions.g4 to meet antlr4 Visitor style is a nice to have on the longer term, but it can definitively wait for now. Do you agree?

@mariofusco mariofusco merged commit c457b62 into kiegroup:dev-drl-expression Jan 17, 2024
3 checks passed
@tkobayas
Copy link
Collaborator

tkobayas commented Jan 18, 2024

I think that option B) is much more pragmatic and will allow us to test and use this work also directly into drools as soon as possible. Refactoring DRL6Expressions.g4 to meet antlr4 Visitor style is a nice to have on the longer term, but it can definitively wait for now. Do you agree?

Agreed.

@yurloc Please go ahead to fix the DescrDumperTest issues in a next PR to this repo:branch drools-lsp:dev-drl-expression.

Then, we can go for the option B). Probably creating a feature branch in drools repo.

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

Successfully merging this pull request may close these issues.

None yet

3 participants