Skip to content

Commit

Permalink
- enhance getTextPreservingWhitespace to avoid string manipulation
Browse files Browse the repository at this point in the history
  • Loading branch information
tkobayas committed Mar 21, 2024
1 parent 81e97dd commit cceba44
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3718,4 +3718,22 @@ public void functionWithStringLiteralAddition() {
assertThat(function.getParameterNames().get(0)).isEqualTo("s");
assertThat(function.getBody()).isEqualToIgnoringWhitespace( "return s + \"*\";");
}

@Test
public void functionWithMultipleBlockStatements() {
final String text = "function String star(String s) {\n" +
" String result = s + \"*\";\n" +
" return result;\n" +
"}";
PackageDescr packageDescr = parser.parse(text);
assertThat(parser.hasErrors()).as(parser.getErrors().toString()).isFalse();

FunctionDescr function = packageDescr.getFunctions().get( 0 );

assertThat(function.getName()).isEqualTo("star");
assertThat(function.getReturnType()).isEqualTo("String");
assertThat(function.getParameterTypes().get(0)).isEqualTo("String");
assertThat(function.getParameterNames().get(0)).isEqualTo("s");
assertThat(function.getBody()).isEqualToIgnoringWhitespace( "String result = s + \"*\"; return result;");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ fromAccumulate : (DRL_ACCUMULATE|DRL_ACC) LPAREN lhsAndDef (COMMA|SEMI)
RPAREN (SEMI)?
;

blockStatements : blockStatement* ;
blockStatements : drlBlockStatement* ;

/*
accumulateFunction := label? ID parameters
Expand Down Expand Up @@ -519,23 +519,23 @@ drlBlock
;
/* extending JavaParser blockStatement */
drlBlockStatement
: localVariableDeclaration SEMI
: drlLocalVariableDeclaration SEMI
| drlStatement
| localTypeDeclaration
;

/* extending JavaParser statements */
/* extending JavaParser statement */
drlStatement
: blockLabel=block
: blockLabel=drlBlock
| ASSERT drlExpression (COLON drlExpression)? SEMI
| IF parExpression drlStatement (ELSE drlStatement)?
| FOR LPAREN forControl RPAREN drlStatement
| WHILE parExpression drlStatement
| DO drlStatement WHILE parExpression SEMI
| TRY block (catchClause+ finallyBlock? | finallyBlock)
| TRY resourceSpecification block catchClause* finallyBlock?
| TRY drlBlock (catchClause+ finallyBlock? | finallyBlock)
| TRY resourceSpecification drlBlock catchClause* finallyBlock?
| SWITCH parExpression LBRACE switchBlockStatementGroup* switchLabel* RBRACE
| SYNCHRONIZED parExpression block
| SYNCHRONIZED parExpression drlBlock
| RETURN drlExpression? SEMI
| THROW drlExpression SEMI
| BREAK drlIdentifier? SEMI
Expand All @@ -545,4 +545,24 @@ drlStatement
| statementExpression=drlExpression SEMI
| switchExpression SEMI? // Java17
| identifierLabel=drlIdentifier COLON drlStatement
;
;

/* extending JavaParser localVariableDeclaration */
drlLocalVariableDeclaration
: variableModifier* (typeType drlVariableDeclarators | VAR drlIdentifier ASSIGN expression)
;

/* extending JavaParser variableDeclarators */
drlVariableDeclarators
: drlVariableDeclarator (COMMA drlVariableDeclarator)*
;

/* extending JavaParser variableDeclarator */
drlVariableDeclarator
: drlVariableDeclaratorId (ASSIGN drlVariableInitializer)?
;

/* extending JavaParser variableDeclaratorId */
drlVariableDeclaratorId
: drlIdentifier (LBRACK RBRACK)*
;
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
*/
package org.drools.drl.parser.antlr4;

import java.util.List;
import java.util.stream.Collectors;

import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.TokenStream;
import org.antlr.v4.runtime.misc.Interval;
Expand Down Expand Up @@ -59,6 +62,13 @@ public static String getTextPreservingWhitespace(ParserRuleContext ctx) {
return ctx.start.getTokenSource().getInputStream().getText(interval);
}

/**
* Get text from List of ParserRuleContext's CharStream without trimming whitespace
*/
public static String getTextPreservingWhitespace(List<? extends ParserRuleContext> ctx) {
return ctx.stream().map(Antlr4ParserStringUtils::getTextPreservingWhitespace).collect(Collectors.joining());
}

/**
* Get text from ParserRuleContext's CharStream without trimming whitespace
* tokenStream is required to get hidden channel token (e.g. whitespace).
Expand All @@ -79,12 +89,4 @@ public static String trimThen(String rhs) {
}
}

public static String stripBracesFromBlock(String text) {
text = text.trim();
if (text.length() >= 2 && text.startsWith("{") && text.endsWith("}")) {
return text.substring(1, text.length() - 1);
} else {
throw new DRLParserException("block has to start with '{' and end with '}' : block = " + text);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
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.stripBracesFromBlock;
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;
Expand Down Expand Up @@ -194,7 +193,7 @@ public FunctionDescr visitFunctiondef(DRLParser.FunctiondefContext ctx) {
functionDescr.addParameter(typeTypeContext.getText(), variableDeclaratorIdContext.getText());
});
}
functionDescr.setBody(stripBracesFromBlock(getTextPreservingWhitespace(ctx.drlBlock())));
functionDescr.setBody(getTextPreservingWhitespace(ctx.drlBlock().drlBlockStatement()));
return functionDescr;
}

Expand Down

0 comments on commit cceba44

Please sign in to comment.