Skip to content

Commit 49fdcf9

Browse files
committed
[DROOLS-889] disambiguate end keyword and end() method in drl
1 parent 208cab2 commit 49fdcf9

File tree

4 files changed

+133
-254
lines changed

4 files changed

+133
-254
lines changed

drools-compiler/src/main/java/org/drools/compiler/lang/DRL6Parser.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4340,6 +4340,22 @@ public void namedConsequence(RuleDescrBuilder rule) {
43404340
}
43414341

43424342
protected String getConsequenceCode( int first ) {
4343+
while (input.LA(1) != DRL6Lexer.EOF) {
4344+
if (helper.validateIdentifierKey(DroolsSoftKeywords.END)) {
4345+
int next = input.LA(2) == DRL6Lexer.SEMICOLON ? 3 : 2;
4346+
if (input.LA(next) == DRL6Lexer.EOF || helper.validateStatement(next)) {
4347+
break;
4348+
}
4349+
} else if (helper.validateIdentifierKey(DroolsSoftKeywords.THEN)) {
4350+
if (isNextTokenThenCompatible( input.LA( 2 ) ) ) {
4351+
break;
4352+
}
4353+
}
4354+
4355+
helper.emit( input.LT( 1 ), DroolsEditorType.CODE_CHUNK );
4356+
input.consume();
4357+
}
4358+
43434359
while (input.LA(1) != DRL6Lexer.EOF &&
43444360
!helper.validateIdentifierKey(DroolsSoftKeywords.END) &&
43454361
!helper.validateIdentifierKey(DroolsSoftKeywords.THEN)) {
@@ -4364,6 +4380,14 @@ protected String getConsequenceCode( int first ) {
43644380
return chunk;
43654381
}
43664382

4383+
private boolean isNextTokenThenCompatible(int next) {
4384+
return next != DRL6Lexer.LEFT_PAREN &&
4385+
next != DRL6Lexer.RIGHT_PAREN &&
4386+
next != DRL6Lexer.RIGHT_SQUARE &&
4387+
next != DRL6Lexer.COMMA &&
4388+
next != DRL6Lexer.SEMICOLON;
4389+
}
4390+
43674391
/* ------------------------------------------------------------------------------------------------
43684392
* ANNOTATION
43694393
* ------------------------------------------------------------------------------------------------ */

drools-compiler/src/main/java/org/drools/compiler/lang/DRL6StrictParser.java

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3258,9 +3258,10 @@ void failUnexpectedAnnotationException(String annotationName) throws DroolsUnexp
32583258
}
32593259

32603260
/**
3261-
* lhsPattern := QUESTION? qualifiedIdentifier
3262-
* LEFT_PAREN positionalConstraints? constraints? RIGHT_PAREN
3263-
* (OVER patternFilter)? (FROM patternSource)?
3261+
* lhsPattern := xpathPrimary |
3262+
* ( QUESTION? qualifiedIdentifier
3263+
* LEFT_PAREN positionalConstraints? constraints? RIGHT_PAREN
3264+
* (OVER patternFilter)? (FROM patternSource)? )
32643265
*
32653266
* @param pattern
32663267
* @param label
@@ -3269,7 +3270,18 @@ void failUnexpectedAnnotationException(String annotationName) throws DroolsUnexp
32693270
*/
32703271
void lhsPattern(PatternDescrBuilder<?> pattern,
32713272
String label,
3272-
boolean isUnification) throws RecognitionException {
3273+
boolean isUnification) throws RecognitionException {
3274+
3275+
if (label != null && input.LA(1) == DRL6Lexer.DIV) {
3276+
int first = input.index();
3277+
exprParser.xpathPrimary();
3278+
if (state.failed) return;
3279+
int last = input.LT(-1).getTokenIndex();
3280+
String expr = toExpression("", first, last);
3281+
pattern.id( label, isUnification ).constraint( expr );
3282+
return;
3283+
}
3284+
32733285
boolean query = false;
32743286
if (input.LA(1) == DRL6Lexer.QUESTION) {
32753287
match(input,
@@ -4347,13 +4359,21 @@ public void namedConsequence(RuleDescrBuilder rule) {
43474359
}
43484360

43494361
protected String getConsequenceCode( int first ) {
4350-
while (input.LA(1) != DRL6Lexer.EOF &&
4351-
!helper.validateIdentifierKey(DroolsSoftKeywords.END) &&
4352-
!helper.validateIdentifierKey(DroolsSoftKeywords.THEN)) {
4353-
helper.emit(input.LT(1), DroolsEditorType.CODE_CHUNK);
4362+
while (input.LA(1) != DRL6Lexer.EOF) {
4363+
if (helper.validateIdentifierKey(DroolsSoftKeywords.END)) {
4364+
int next = input.LA(2) == DRL6Lexer.SEMICOLON ? 3 : 2;
4365+
if (input.LA(next) == DRL6Lexer.EOF || input.LA(next) == DRL6Lexer.AT || helper.validateStatement(next)) {
4366+
break;
4367+
}
4368+
} else if (helper.validateIdentifierKey(DroolsSoftKeywords.THEN)) {
4369+
if (isNextTokenThenCompatible( input.LA( 2 ) ) ) {
4370+
break;
4371+
}
4372+
}
4373+
4374+
helper.emit( input.LT( 1 ), DroolsEditorType.CODE_CHUNK );
43544375
input.consume();
43554376
}
4356-
43574377
int last = input.LT(1).getTokenIndex();
43584378
if (last <= first) {
43594379
return "";
@@ -4371,6 +4391,14 @@ protected String getConsequenceCode( int first ) {
43714391
return chunk;
43724392
}
43734393

4394+
private boolean isNextTokenThenCompatible(int next) {
4395+
return next != DRL6Lexer.LEFT_PAREN &&
4396+
next != DRL6Lexer.RIGHT_PAREN &&
4397+
next != DRL6Lexer.RIGHT_SQUARE &&
4398+
next != DRL6Lexer.COMMA &&
4399+
next != DRL6Lexer.SEMICOLON;
4400+
}
4401+
43744402
/* ------------------------------------------------------------------------------------------------
43754403
* ANNOTATION
43764404
* ------------------------------------------------------------------------------------------------ */

0 commit comments

Comments
 (0)