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] Support LHS pattern annotations #5797

Merged
merged 2 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3739,4 +3739,20 @@ public void functionWithMultipleBlockStatements() {
assertThat(function.getParameterNames().get(0)).isEqualTo("s");
assertThat(function.getBody()).isEqualToIgnoringWhitespace( "String result = s + \"*\"; return result;");
}

@Test
void lhsPatternAnnotation() {
final String text = "package org.drools\n" +
"rule R1\n" +
"when\n" +
" $p : Person( name == \"Mario\" ) @watch(!*, age)\n" +
"then\n" +
"end\n";
PackageDescr packageDescr = parser.parse(text);
RuleDescr ruleDescr = packageDescr.getRules().get(0);
PatternDescr patternDescr = (PatternDescr) ruleDescr.getLhs().getDescrs().get(0);
AnnotationDescr annotationDescr = patternDescr.getAnnotations().iterator().next();
assertThat(annotationDescr.getName()).isEqualTo("watch");
assertThat(annotationDescr.getSingleValueAsString()).isEqualTo("!*, age");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ lhsPattern : xpathPrimary (OVER patternFilter)? |
( QUESTION? qualifiedIdentifier LPAREN positionalConstraints? constraints? RPAREN (OVER patternFilter)? (FROM patternSource)? ) ;
*/

lhsPattern : QUESTION? objectType=drlQualifiedName LPAREN positionalConstraints? constraints? RPAREN (DRL_OVER patternFilter)? (DRL_FROM patternSource)? ;
lhsPattern : QUESTION? objectType=drlQualifiedName LPAREN positionalConstraints? constraints? RPAREN drlAnnotation* (DRL_OVER patternFilter)? (DRL_FROM patternSource)? ;
Copy link
Contributor Author

@yurloc yurloc Mar 20, 2024

Choose a reason for hiding this comment

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

This allows an annotation to appear after a LHS pattern, for example, Person() @watch(!age).

positionalConstraints : constraint (COMMA constraint)* SEMI ;
constraints : constraint (COMMA constraint)* ;
constraint : ( nestedConstraint | conditionalOrExpression ) ;
Expand Down Expand Up @@ -459,7 +459,7 @@ type : (classOrInterfaceType | primitiveType) typeArguments? ( DOT IDENTIFIER ty
drlArguments : LPAREN drlArgument (COMMA drlArgument)* RPAREN ;
drlArgument : ( stringId | floatLiteral | BOOL_LITERAL | NULL_LITERAL ) ;

drlAnnotation : AT name=drlQualifiedName (LPAREN ( drlElementValuePairs | drlElementValue )? RPAREN)? ;
drlAnnotation : AT name=drlQualifiedName ((LPAREN ( drlElementValuePairs | drlElementValue )? RPAREN) | chunk)? ;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This allows an annotation value to be something that doesn't fit into the standard Java annotation key-value format, for example, @watch(!*, age).


drlElementValuePairs : drlElementValuePair (COMMA drlElementValuePair)* ;
drlElementValuePair : key=drlIdentifier ASSIGN value=drlElementValue ;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,6 @@ private Antlr4ParserStringUtils() {
// Private constructor to prevent instantiation.
}

/**
* Strip string delimiters (e.g. "foo" -> foo)
*/
public static String safeStripStringDelimiters(String value) {
if (value != null) {
value = value.trim();
if (value.length() >= 2 && value.startsWith("\"") && value.endsWith("\"")) {
value = value.substring(1, value.length() - 1);
}
}
return value;
}

/**
* Get text from ParserRuleContext's CharStream without trimming whitespace
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@

import static org.drools.drl.parser.antlr4.Antlr4ParserStringUtils.getTextPreservingWhitespace;
import static org.drools.drl.parser.antlr4.Antlr4ParserStringUtils.getTokenTextPreservingWhitespace;
import static org.drools.drl.parser.antlr4.Antlr4ParserStringUtils.safeStripStringDelimiters;
import static org.drools.drl.parser.antlr4.Antlr4ParserStringUtils.trimThen;
import static org.drools.drl.parser.antlr4.DRLParserHelper.getTextWithoutErrorNode;
import static org.drools.drl.parser.util.ParserStringUtils.appendPrefix;
import static org.drools.drl.parser.util.ParserStringUtils.safeStripDelimiters;
import static org.drools.drl.parser.util.ParserStringUtils.safeStripStringDelimiters;
import static org.drools.util.StringUtils.unescapeJava;

/**
Expand Down Expand Up @@ -322,6 +323,8 @@ public AnnotationDescr visitDrlAnnotation(DRLParser.DrlAnnotationContext ctx) {
AnnotationDescr annotationDescr = new AnnotationDescr(ctx.name.getText());
if (ctx.drlElementValue() != null) {
annotationDescr.setValue(getTextPreservingWhitespace(ctx.drlElementValue())); // single value
} else if (ctx.chunk() != null) {
annotationDescr.setValue(safeStripDelimiters(getTextPreservingWhitespace(ctx.chunk()), "(", ")"));
} else if (ctx.drlElementValuePairs() != null) {
visitDrlElementValuePairs(ctx.drlElementValuePairs(), annotationDescr); // multiple values
}
Expand Down Expand Up @@ -496,6 +499,7 @@ public PatternDescr visitLhsPattern(DRLParser.LhsPatternContext ctx) {
patternSourceDescr.setResource(patternDescr.getResource());
patternDescr.setSource(patternSourceDescr);
}
ctx.drlAnnotation().stream().map(this::visitDrlAnnotation).forEach(patternDescr::addAnnotation);
List<ExprConstraintDescr> constraintDescrList = visitConstraints(ctx.positionalConstraints(), ctx.constraints());
constraintDescrList.forEach(descr -> addToPatternDescr(patternDescr, descr));
return patternDescr;
Expand Down Expand Up @@ -874,4 +878,4 @@ public ParseTree getNode() {
private String visitConstraintChildren(ParserRuleContext ctx) {
return getTokenTextPreservingWhitespace(ctx, tokenStream);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@
import org.slf4j.LoggerFactory;

import static org.drools.drl.parser.util.ParserStringUtils.appendPrefix;
import static org.drools.drl.parser.util.ParserStringUtils.safeStripDelimiters;
import static org.drools.drl.parser.util.ParserStringUtils.safeStripStringDelimiters;

public class DRL6Parser extends AbstractDRLParser implements DRLParser {

Expand Down Expand Up @@ -5192,29 +5194,4 @@ public boolean mismatchIsMissingToken(TokenStream input,
return false;
}

private String safeStripDelimiters(String value,
String left,
String right) {
if (value != null) {
value = value.trim();
if (value.length() >= left.length() + right.length() &&
value.startsWith(left) && value.endsWith(right)) {
value = value.substring(left.length(),
value.length() - right.length());
}
}
return value;
}

private String safeStripStringDelimiters(String value) {
if (value != null) {
value = value.trim();
if (value.length() >= 2 && value.startsWith("\"") && value.endsWith("\"")) {
value = value.substring(1,
value.length() - 1);
}
}
return value;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,31 @@ private ParserStringUtils() {
// Private constructor to prevent instantiation.
}

/**
* Strip string delimiters (e.g. "foo" -> foo)
*/
public static String safeStripStringDelimiters(String value) {
if (value != null) {
value = value.trim();
if (value.length() >= 2 && value.startsWith("\"") && value.endsWith("\"")) {
value = value.substring(1, value.length() - 1);
}
}
return value;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This one is used by both the old parser and the new parser's visitor.


public static String safeStripDelimiters(String value, String left, String right) {
if (value != null) {
value = value.trim();
if (value.length() >= left.length() + right.length() &&
value.startsWith(left) && value.endsWith(right)) {
value = value.substring(left.length(),
value.length() - right.length());
}
}
return value;
}

/**
* Append a prefix to a grouped constraint.
* Even if the constraint contains || and/or &&, append the prefix to each element.
Expand Down
Loading