Skip to content

Commit

Permalink
[DROOLS-7515] Parser : Accept plain text in RHS (#26)
Browse files Browse the repository at this point in the history
* [DROOLS-7515] Parser : Accept plain text in RHS

* - Handle keyword token in RHS

* - Removed sysout

* - Revert statementKeywords. Instead, define DRL_END with newline or EOF

* - fix method name
  • Loading branch information
tkobayas authored and mariofusco committed Feb 14, 2024
1 parent 0b10f1f commit b91f45d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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]+ ;
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ lhsNot : DRL_NOT lhsPatternBind ;

rhs : DRL_THEN consequence ;

consequence : drlRhsBlockStatement* ;
consequence : RHS_CHUNK* ;

stringId : ( IDENTIFIER | DRL_STRING_LITERAL ) ;

Expand Down Expand Up @@ -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
| <assoc=right> drlRhsExpression bop=QUESTION drlRhsExpression COLON drlRhsExpression
| <assoc=right> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;");
}
}

0 comments on commit b91f45d

Please sign in to comment.