diff --git a/drools-drl/drools-drl10-parser/src/main/antlr4/org/drools/parser/DRLLexer.g4 b/drools-drl/drools-drl10-parser/src/main/antlr4/org/drools/parser/DRLLexer.g4 index 60e282c49fb..0e91fb3f2e5 100644 --- a/drools-drl/drools-drl10-parser/src/main/antlr4/org/drools/parser/DRLLexer.g4 +++ b/drools-drl/drools-drl10-parser/src/main/antlr4/org/drools/parser/DRLLexer.g4 @@ -30,11 +30,11 @@ import JavaLexer; DRL_UNIT : 'unit'; DRL_FUNCTION : 'function'; DRL_GLOBAL : 'global'; +DRL_DECLARE : 'declare'; DRL_RULE : 'rule'; DRL_QUERY : 'query'; DRL_WHEN : 'when'; -DRL_THEN : 'then'; -DRL_END : 'end'; +DRL_THEN : 'then' -> pushMode(RHS); DRL_AND : 'and'; DRL_OR : 'or'; @@ -114,3 +114,8 @@ fragment DrlUnicodeEscape : '\\' 'u' HexDigit HexDigit HexDigit HexDigit ; + +mode RHS; +RHS_WS : [ \t\r\n\u000C]+ -> channel(HIDDEN); +DRL_END : 'end' [ \t]* ('\n' | '\r\n' | EOF) {setText("end");} -> popMode; +RHS_CHUNK : ~[ \t\r\n\u000C]+ ; diff --git a/drools-drl/drools-drl10-parser/src/main/antlr4/org/drools/parser/DRLParser.g4 b/drools-drl/drools-drl10-parser/src/main/antlr4/org/drools/parser/DRLParser.g4 index be4ab9c6712..af10b445e97 100644 --- a/drools-drl/drools-drl10-parser/src/main/antlr4/org/drools/parser/DRLParser.g4 +++ b/drools-drl/drools-drl10-parser/src/main/antlr4/org/drools/parser/DRLParser.g4 @@ -273,7 +273,7 @@ lhsNot : DRL_NOT lhsPatternBind ; rhs : DRL_THEN consequence ; -consequence : drlRhsBlockStatement* ; +consequence : RHS_CHUNK* ; stringId : ( IDENTIFIER | DRL_STRING_LITERAL ) ; @@ -309,107 +309,12 @@ assignmentOperator : ASSIGN label : IDENTIFIER COLON ; unif : IDENTIFIER UNIFY ; -/* extending JavaParser blockStatement */ -drlRhsBlockStatement - : drlLocalVariableDeclaration SEMI - | drlRhsStatement - | localTypeDeclaration - ; - -/* extending JavaParser localVariableDeclaration */ -drlLocalVariableDeclaration - : variableModifier* (typeType drlVariableDeclarators | VAR drlIdentifier ASSIGN drlExpression) - ; - -/* extending JavaParser variableDeclarators */ -drlVariableDeclarators - : drlVariableDeclarator (COMMA drlVariableDeclarator)* - ; - -/* extending JavaParser variableDeclarator */ -drlVariableDeclarator - : drlVariableDeclaratorId (ASSIGN drlVariableInitializer)? - ; - -/* extending JavaParser variableDeclaratorId */ -drlVariableDeclaratorId - : drlIdentifier (LBRACK RBRACK)* - ; - /* extending JavaParser variableInitializer */ drlVariableInitializer : arrayInitializer | drlExpression ; - -/* extending JavaParser statement */ -drlRhsStatement - : blockLabel=block - | ASSERT drlRhsExpression (COLON drlRhsExpression)? SEMI - | IF parExpression drlRhsStatement (ELSE drlRhsStatement)? - | FOR LPAREN forControl RPAREN drlRhsStatement - | WHILE parExpression drlRhsStatement - | DO drlRhsStatement WHILE parExpression SEMI - | TRY block (catchClause+ finallyBlock? | finallyBlock) - | TRY resourceSpecification block catchClause* finallyBlock? - | SWITCH parExpression LBRACE switchBlockStatementGroup* switchLabel* RBRACE - | SYNCHRONIZED parExpression block - | RETURN drlRhsExpression? SEMI - | THROW drlRhsExpression SEMI - | BREAK drlIdentifier? SEMI - | CONTINUE drlIdentifier? SEMI - | YIELD drlRhsExpression SEMI // Java17 - | SEMI - | statementExpression=drlRhsExpression SEMI - | switchExpression SEMI? // Java17 - | identifierLabel=drlIdentifier COLON drlRhsStatement - ; - -/* extending JavaParser expression */ -drlRhsExpression - : drlPrimary - | drlRhsExpression bop=DOT - ( - drlIdentifier - | methodCall - | THIS - | NEW nonWildcardTypeArguments? innerCreator - | SUPER superSuffix - | explicitGenericInvocation - ) - | drlRhsExpression LBRACK drlRhsExpression RBRACK - | methodCall - | NEW creator - | LPAREN annotation* typeType (BITAND typeType)* RPAREN drlRhsExpression - | drlRhsExpression postfix=(INC | DEC) - | prefix=(ADD|SUB|INC|DEC) drlRhsExpression - | prefix=(TILDE|BANG) drlRhsExpression - | drlRhsExpression bop=(MUL|DIV|MOD) drlRhsExpression - | drlRhsExpression bop=(ADD|SUB) drlRhsExpression - | drlRhsExpression (LT LT | GT GT GT | GT GT) drlRhsExpression - | drlRhsExpression bop=(LE | GE | GT | LT) drlRhsExpression - | drlRhsExpression bop=INSTANCEOF (typeType | pattern) - | drlRhsExpression bop=DRL_MATCHES drlRhsExpression - | drlRhsExpression bop=(EQUAL | NOTEQUAL) drlRhsExpression - | drlRhsExpression bop=BITAND drlRhsExpression - | drlRhsExpression bop=CARET drlRhsExpression - | drlRhsExpression bop=BITOR drlRhsExpression - | drlRhsExpression bop=AND drlRhsExpression - | drlRhsExpression bop=OR drlRhsExpression - | drlRhsExpression bop=QUESTION drlRhsExpression COLON drlRhsExpression - | drlRhsExpression - bop=(ASSIGN | ADD_ASSIGN | SUB_ASSIGN | MUL_ASSIGN | DIV_ASSIGN | AND_ASSIGN | OR_ASSIGN | XOR_ASSIGN | RSHIFT_ASSIGN | URSHIFT_ASSIGN | LSHIFT_ASSIGN | MOD_ASSIGN) - drlRhsExpression - | lambdaExpression // Java8 - | switchExpression // Java17 - - // Java 8 methodReference - | drlRhsExpression COLONCOLON typeArguments? drlIdentifier - | typeType COLONCOLON (typeArguments? drlIdentifier | NEW) - | classType COLONCOLON typeArguments? NEW - ; - drlCreator : nonWildcardTypeArguments createdName classCreatorRest | createdName (drlArrayCreatorRest | classCreatorRest) diff --git a/drools-drl/drools-drl10-parser/src/test/java/org/drools/parser/DRLParserTest.java b/drools-drl/drools-drl10-parser/src/test/java/org/drools/parser/DRLParserTest.java index 4b9497b422a..83a8b1f5d23 100644 --- a/drools-drl/drools-drl10-parser/src/test/java/org/drools/parser/DRLParserTest.java +++ b/drools-drl/drools-drl10-parser/src/test/java/org/drools/parser/DRLParserTest.java @@ -90,10 +90,6 @@ void computeTokenIndex_basicRule() { assertThat((int) computeTokenIndex(parser, 2, 1)).isEqualTo(7); assertThat((int) computeTokenIndex(parser, 2, 6)).isEqualTo(7); assertThat((int) computeTokenIndex(parser, 2, 7)).isEqualTo(8); - assertThat((int) computeTokenIndex(parser, 9, 0)).isEqualTo(80); - assertThat((int) computeTokenIndex(parser, 9, 1)).isEqualTo(81); - assertThat((int) computeTokenIndex(parser, 9, 4)).isEqualTo(82); - assertThat((int) computeTokenIndex(parser, 9, 5)).isEqualTo(82); - assertThat((int) computeTokenIndex(parser, 10, 0)).isEqualTo(82); // EOF + // Skip RHS token assertion as it is fluid part at the moment. } } diff --git a/drools-drl/drools-drl10-parser/src/test/java/org/drools/parser/MiscDRLParserTest.java b/drools-drl/drools-drl10-parser/src/test/java/org/drools/parser/MiscDRLParserTest.java index ed5532ff2f9..dd88f613513 100644 --- a/drools-drl/drools-drl10-parser/src/test/java/org/drools/parser/MiscDRLParserTest.java +++ b/drools-drl/drools-drl10-parser/src/test/java/org/drools/parser/MiscDRLParserTest.java @@ -3279,4 +3279,48 @@ public void parse_WindowUsage() throws Exception { assertThat(pd.getSource().getText()).isEqualTo("Y"); } + @Test + public void endInRhs() throws Exception { + final String text = "package org.drools\n" + + "rule X\n" + + "when\n" + + " $s : String()\n" + + "then\n" + + " System.out.println($s.endsWith(\"xyz\"));\n" + + "end\n"; + PackageDescr packageDescr = parser.parse(text ); + + RuleDescr ruleDescr = packageDescr.getRules().get(0); + assertThat(ruleDescr.getConsequence().toString()).isEqualToIgnoringWhitespace("System.out.println($s.endsWith(\"xyz\"));"); + } + + @Test + public void endTokenInRhs() throws Exception { + final String text = "package org.drools\n" + + "rule X\n" + + "when\n" + + " $s : String()\n" + + "then\n" + + " int end = 10;\n" + + "end\n"; + PackageDescr packageDescr = parser.parse(text ); + + RuleDescr ruleDescr = packageDescr.getRules().get(0); + assertThat(ruleDescr.getConsequence().toString()).isEqualToIgnoringWhitespace("int end = 10;"); + } + + @Test + public void ruleTokenInRhs() throws Exception { + final String text = "package org.drools\n" + + "rule X\n" + + "when\n" + + " $s : String()\n" + + "then\n" + + " int rule = 10;\n" + + "end\n"; + PackageDescr packageDescr = parser.parse(text ); + + RuleDescr ruleDescr = packageDescr.getRules().get(0); + assertThat(ruleDescr.getConsequence().toString()).isEqualToIgnoringWhitespace("int rule = 10;"); + } }