Skip to content

Commit 17209ac

Browse files
committed
replace string node with unary node
1 parent 16829d2 commit 17209ac

8 files changed

Lines changed: 48 additions & 71 deletions

File tree

src/main/java/com/github/sidhant92/boolparser/application/ArithmeticExpressionEvaluator.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import com.github.sidhant92.boolparser.constant.ContainerDataType;
1111
import com.github.sidhant92.boolparser.constant.DataType;
1212
import com.github.sidhant92.boolparser.constant.Operator;
13-
import com.github.sidhant92.boolparser.domain.StringNode;
13+
import com.github.sidhant92.boolparser.domain.UnaryNode;
1414
import com.github.sidhant92.boolparser.domain.arithmetic.ArithmeticLeafNode;
1515
import com.github.sidhant92.boolparser.domain.arithmetic.ArithmeticNode;
1616
import com.github.sidhant92.boolparser.domain.arithmetic.ArithmeticUnaryNode;
@@ -61,16 +61,17 @@ private Object evaluateToken(final Node node, final Map<String, Object> data) {
6161
return evaluateUnaryArithmeticToken((ArithmeticUnaryNode) node, data);
6262
case ARITHMETIC_FUNCTION:
6363
return evaluateArithmeticFunctionToken((ArithmeticFunctionNode) node, data);
64-
case STRING:
65-
return evaluateStringToken((StringNode) node, data);
64+
case UNARY:
65+
return evaluateStringToken((UnaryNode) node, data);
6666
default:
6767
log.error("unsupported token {}", node.getTokenType());
6868
throw new UnsupportedToken();
6969
}
7070
}
7171

72-
private Object evaluateStringToken(final StringNode stringNode, final Map<String, Object> data) {
73-
return ValueUtils.getValueFromMap(stringNode.getField(), data).orElse(stringNode.getField());
72+
private Object evaluateStringToken(final UnaryNode unaryNode, final Map<String, Object> data) {
73+
return unaryNode.getDataType() == DataType.STRING ? ValueUtils.getValueFromMap(unaryNode.getValue().toString(), data)
74+
.orElse(unaryNode.getValue()) : unaryNode.getValue();
7475
}
7576

7677
private Pair<Object, DataType> evaluateArithmeticLeafToken(final ArithmeticLeafNode arithmeticLeafNode, final Map<String, Object> data) {

src/main/java/com/github/sidhant92/boolparser/domain/StringNode.java

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/main/java/com/github/sidhant92/boolparser/parser/antlr/BooleanExpressionBaseListener.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,13 @@ public class BooleanExpressionBaseListener implements BooleanExpressionListener
184184
*
185185
* <p>The default implementation does nothing.</p>
186186
*/
187-
@Override public void enterArrayArithmeticFunction(BooleanExpressionParser.ArrayArithmeticFunctionContext ctx) { }
187+
@Override public void enterArithmeticFunction(BooleanExpressionParser.ArithmeticFunctionContext ctx) { }
188188
/**
189189
* {@inheritDoc}
190190
*
191191
* <p>The default implementation does nothing.</p>
192192
*/
193-
@Override public void exitArrayArithmeticFunction(BooleanExpressionParser.ArrayArithmeticFunctionContext ctx) { }
193+
@Override public void exitArithmeticFunction(BooleanExpressionParser.ArithmeticFunctionContext ctx) { }
194194
/**
195195
* {@inheritDoc}
196196
*

src/main/java/com/github/sidhant92/boolparser/parser/antlr/BooleanExpressionListener.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,15 @@ public interface BooleanExpressionListener extends ParseTreeListener {
169169
*/
170170
void exitArithmeticOperator(BooleanExpressionParser.ArithmeticOperatorContext ctx);
171171
/**
172-
* Enter a parse tree produced by {@link BooleanExpressionParser#arrayArithmeticFunction}.
172+
* Enter a parse tree produced by {@link BooleanExpressionParser#arithmeticFunction}.
173173
* @param ctx the parse tree
174174
*/
175-
void enterArrayArithmeticFunction(BooleanExpressionParser.ArrayArithmeticFunctionContext ctx);
175+
void enterArithmeticFunction(BooleanExpressionParser.ArithmeticFunctionContext ctx);
176176
/**
177-
* Exit a parse tree produced by {@link BooleanExpressionParser#arrayArithmeticFunction}.
177+
* Exit a parse tree produced by {@link BooleanExpressionParser#arithmeticFunction}.
178178
* @param ctx the parse tree
179179
*/
180-
void exitArrayArithmeticFunction(BooleanExpressionParser.ArrayArithmeticFunctionContext ctx);
180+
void exitArithmeticFunction(BooleanExpressionParser.ArithmeticFunctionContext ctx);
181181
/**
182182
* Enter a parse tree produced by {@link BooleanExpressionParser#wordlist}.
183183
* @param ctx the parse tree

src/main/java/com/github/sidhant92/boolparser/parser/antlr/BooleanExpressionParser.java

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ public class BooleanExpressionParser extends Parser {
2424
SQ=40, DQ=41;
2525
public static final int
2626
RULE_parse = 0, RULE_expression = 1, RULE_comparator = 2, RULE_arithmeticOperator = 3,
27-
RULE_arrayArithmeticFunction = 4, RULE_wordlist = 5, RULE_arrayOperators = 6,
27+
RULE_arithmeticFunction = 4, RULE_wordlist = 5, RULE_arrayOperators = 6,
2828
RULE_numericTypes = 7, RULE_types = 8, RULE_binary = 9, RULE_bool = 10;
2929
private static String[] makeRuleNames() {
3030
return new String[] {
31-
"parse", "expression", "comparator", "arithmeticOperator", "arrayArithmeticFunction",
31+
"parse", "expression", "comparator", "arithmeticOperator", "arithmeticFunction",
3232
"wordlist", "arrayOperators", "numericTypes", "types", "binary", "bool"
3333
};
3434
}
@@ -283,10 +283,10 @@ public void exitRule(ParseTreeListener listener) {
283283
}
284284
@SuppressWarnings("CheckReturnValue")
285285
public static class ArithmeticFunctionExpressionContext extends ExpressionContext {
286-
public ArrayArithmeticFunctionContext left;
286+
public ArithmeticFunctionContext left;
287287
public WordlistContext data;
288-
public ArrayArithmeticFunctionContext arrayArithmeticFunction() {
289-
return getRuleContext(ArrayArithmeticFunctionContext.class,0);
288+
public ArithmeticFunctionContext arithmeticFunction() {
289+
return getRuleContext(ArithmeticFunctionContext.class,0);
290290
}
291291
public WordlistContext wordlist() {
292292
return getRuleContext(WordlistContext.class,0);
@@ -447,7 +447,7 @@ private ExpressionContext expression(int _p) throws RecognitionException {
447447
_ctx = _localctx;
448448
_prevctx = _localctx;
449449
setState(34);
450-
((ArithmeticFunctionExpressionContext)_localctx).left = arrayArithmeticFunction();
450+
((ArithmeticFunctionExpressionContext)_localctx).left = arithmeticFunction();
451451
setState(35);
452452
((ArithmeticFunctionExpressionContext)_localctx).data = wordlist();
453453
}
@@ -778,7 +778,7 @@ public final ArithmeticOperatorContext arithmeticOperator() throws RecognitionEx
778778
}
779779

780780
@SuppressWarnings("CheckReturnValue")
781-
public static class ArrayArithmeticFunctionContext extends ParserRuleContext {
781+
public static class ArithmeticFunctionContext extends ParserRuleContext {
782782
public TerminalNode MIN() { return getToken(BooleanExpressionParser.MIN, 0); }
783783
public TerminalNode MAX() { return getToken(BooleanExpressionParser.MAX, 0); }
784784
public TerminalNode SUM() { return getToken(BooleanExpressionParser.SUM, 0); }
@@ -788,23 +788,23 @@ public static class ArrayArithmeticFunctionContext extends ParserRuleContext {
788788
public TerminalNode LEN() { return getToken(BooleanExpressionParser.LEN, 0); }
789789
public TerminalNode MEDIAN() { return getToken(BooleanExpressionParser.MEDIAN, 0); }
790790
public TerminalNode INT() { return getToken(BooleanExpressionParser.INT, 0); }
791-
public ArrayArithmeticFunctionContext(ParserRuleContext parent, int invokingState) {
791+
public ArithmeticFunctionContext(ParserRuleContext parent, int invokingState) {
792792
super(parent, invokingState);
793793
}
794-
@Override public int getRuleIndex() { return RULE_arrayArithmeticFunction; }
794+
@Override public int getRuleIndex() { return RULE_arithmeticFunction; }
795795
@Override
796796
public void enterRule(ParseTreeListener listener) {
797-
if ( listener instanceof BooleanExpressionListener ) ((BooleanExpressionListener)listener).enterArrayArithmeticFunction(this);
797+
if ( listener instanceof BooleanExpressionListener ) ((BooleanExpressionListener)listener).enterArithmeticFunction(this);
798798
}
799799
@Override
800800
public void exitRule(ParseTreeListener listener) {
801-
if ( listener instanceof BooleanExpressionListener ) ((BooleanExpressionListener)listener).exitArrayArithmeticFunction(this);
801+
if ( listener instanceof BooleanExpressionListener ) ((BooleanExpressionListener)listener).exitArithmeticFunction(this);
802802
}
803803
}
804804

805-
public final ArrayArithmeticFunctionContext arrayArithmeticFunction() throws RecognitionException {
806-
ArrayArithmeticFunctionContext _localctx = new ArrayArithmeticFunctionContext(_ctx, getState());
807-
enterRule(_localctx, 8, RULE_arrayArithmeticFunction);
805+
public final ArithmeticFunctionContext arithmeticFunction() throws RecognitionException {
806+
ArithmeticFunctionContext _localctx = new ArithmeticFunctionContext(_ctx, getState());
807+
enterRule(_localctx, 8, RULE_arithmeticFunction);
808808
int _la;
809809
try {
810810
enterOuterAlt(_localctx, 1);
@@ -834,15 +834,15 @@ public final ArrayArithmeticFunctionContext arrayArithmeticFunction() throws Rec
834834

835835
@SuppressWarnings("CheckReturnValue")
836836
public static class WordlistContext extends ParserRuleContext {
837-
public TypesContext first;
838-
public TypesContext rest;
837+
public ExpressionContext first;
838+
public ExpressionContext rest;
839839
public TerminalNode LPAREN() { return getToken(BooleanExpressionParser.LPAREN, 0); }
840840
public TerminalNode RPAREN() { return getToken(BooleanExpressionParser.RPAREN, 0); }
841-
public List<TypesContext> types() {
842-
return getRuleContexts(TypesContext.class);
841+
public List<ExpressionContext> expression() {
842+
return getRuleContexts(ExpressionContext.class);
843843
}
844-
public TypesContext types(int i) {
845-
return getRuleContext(TypesContext.class,i);
844+
public ExpressionContext expression(int i) {
845+
return getRuleContext(ExpressionContext.class,i);
846846
}
847847
public List<TerminalNode> WS() { return getTokens(BooleanExpressionParser.WS); }
848848
public TerminalNode WS(int i) {
@@ -889,7 +889,7 @@ public final WordlistContext wordlist() throws RecognitionException {
889889
_alt = getInterpreter().adaptivePredict(_input,7,_ctx);
890890
}
891891
setState(105);
892-
((WordlistContext)_localctx).first = types();
892+
((WordlistContext)_localctx).first = expression(0);
893893
setState(109);
894894
_errHandler.sync(this);
895895
_la = _input.LA(1);
@@ -929,7 +929,7 @@ public final WordlistContext wordlist() throws RecognitionException {
929929
_alt = getInterpreter().adaptivePredict(_input,9,_ctx);
930930
}
931931
setState(119);
932-
((WordlistContext)_localctx).rest = types();
932+
((WordlistContext)_localctx).rest = expression(0);
933933
setState(123);
934934
_errHandler.sync(this);
935935
_la = _input.LA(1);
@@ -1339,13 +1339,13 @@ private boolean expression_sempred(ExpressionContext _localctx, int predIndex) {
13391339
"\u0000\u0000a\t\u0001\u0000\u0000\u0000bf\u0005 \u0000\u0000ce\u0005%"+
13401340
"\u0000\u0000dc\u0001\u0000\u0000\u0000eh\u0001\u0000\u0000\u0000fd\u0001"+
13411341
"\u0000\u0000\u0000fg\u0001\u0000\u0000\u0000gi\u0001\u0000\u0000\u0000"+
1342-
"hf\u0001\u0000\u0000\u0000im\u0003\u0010\b\u0000jl\u0005%\u0000\u0000"+
1342+
"hf\u0001\u0000\u0000\u0000im\u0003\u0002\u0001\u0000jl\u0005%\u0000\u0000"+
13431343
"kj\u0001\u0000\u0000\u0000lo\u0001\u0000\u0000\u0000mk\u0001\u0000\u0000"+
13441344
"\u0000mn\u0001\u0000\u0000\u0000n\u0080\u0001\u0000\u0000\u0000om\u0001"+
13451345
"\u0000\u0000\u0000pt\u0005\u0001\u0000\u0000qs\u0005%\u0000\u0000rq\u0001"+
13461346
"\u0000\u0000\u0000sv\u0001\u0000\u0000\u0000tr\u0001\u0000\u0000\u0000"+
13471347
"tu\u0001\u0000\u0000\u0000uw\u0001\u0000\u0000\u0000vt\u0001\u0000\u0000"+
1348-
"\u0000w{\u0003\u0010\b\u0000xz\u0005%\u0000\u0000yx\u0001\u0000\u0000"+
1348+
"\u0000w{\u0003\u0002\u0001\u0000xz\u0005%\u0000\u0000yx\u0001\u0000\u0000"+
13491349
"\u0000z}\u0001\u0000\u0000\u0000{y\u0001\u0000\u0000\u0000{|\u0001\u0000"+
13501350
"\u0000\u0000|\u007f\u0001\u0000\u0000\u0000}{\u0001\u0000\u0000\u0000"+
13511351
"~p\u0001\u0000\u0000\u0000\u007f\u0082\u0001\u0000\u0000\u0000\u0080~"+

src/main/java/com/github/sidhant92/boolparser/parser/antlr/BooleanFilterListener.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import com.github.sidhant92.boolparser.constant.DataType;
1414
import com.github.sidhant92.boolparser.constant.LogicalOperationType;
1515
import com.github.sidhant92.boolparser.constant.Operator;
16-
import com.github.sidhant92.boolparser.domain.StringNode;
1716
import com.github.sidhant92.boolparser.domain.arithmetic.ArithmeticFunctionNode;
1817
import com.github.sidhant92.boolparser.domain.arithmetic.ArithmeticLeafNode;
1918
import com.github.sidhant92.boolparser.domain.arithmetic.ArithmeticNode;
@@ -174,9 +173,9 @@ public void exitInExpression(BooleanExpressionParser.InExpressionContext ctx) {
174173
private List<Pair<DataType, Object>> getArrayElements(final List<ParseTree> trees) {
175174
return trees
176175
.stream()
177-
.filter(child -> child instanceof BooleanExpressionParser.TypesContext)
176+
.filter(child -> child instanceof BooleanExpressionParser.TypesExpressionContext)
178177
.map(child -> {
179-
final DataType dataType = getDataType(((BooleanExpressionParser.TypesContext) child).start);
178+
final DataType dataType = getDataType(((BooleanExpressionParser.TypesExpressionContext) child).start);
180179
final Object value = ValueUtils.convertValue(child.getText(), dataType);
181180
return Pair.of(dataType, value);
182181
})
@@ -186,9 +185,9 @@ private List<Pair<DataType, Object>> getArrayElements(final List<ParseTree> tree
186185
private List<ArithmeticLeafNode> getArithmeticArrayElements(final List<ParseTree> trees) {
187186
return trees
188187
.stream()
189-
.filter(child -> child instanceof BooleanExpressionParser.TypesContext)
188+
.filter(child -> child instanceof BooleanExpressionParser.TypesExpressionContext)
190189
.map(child -> {
191-
final DataType dataType = getDataType(((BooleanExpressionParser.TypesContext) child).start);
190+
final DataType dataType = getDataType(((BooleanExpressionParser.TypesExpressionContext) child).start);
192191
final Object value = ValueUtils.convertValue(child.getText(), dataType);
193192
return new ArithmeticLeafNode(value, dataType);
194193
})
@@ -248,7 +247,8 @@ public void exitParse(BooleanExpressionParser.ParseContext ctx) {
248247
}
249248
}
250249
if (this.node == null && tokenCount == 1 && lastToken instanceof CommonToken) {
251-
this.node = StringNode.builder().field(ValueUtils.convertValue(lastToken.getText(), DataType.STRING).toString()).build();
250+
this.node = UnaryNode.builder().dataType(DataType.STRING).value(ValueUtils.convertValue(lastToken.getText(), DataType.STRING).toString())
251+
.build();
252252
}
253253
if (this.node == null) {
254254
log.error("Error parsing expression for the string {}", ctx.getText());

src/main/java/resources/BooleanExpression.g4

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ expression
1515
| left=expression op= MODULUS right=expression #arithmeticExpression
1616
| left=expression op= ADD right=expression #arithmeticExpression
1717
| left=expression op= SUBTRACT right=expression #arithmeticExpression
18-
| left=arrayArithmeticFunction data=wordlist #arithmeticFunctionExpression
18+
| left=arithmeticFunction data=wordlist #arithmeticFunctionExpression
1919
| left=expression op=binary right=expression #binaryExpression
2020
| types #typesExpression
2121
| (field=WORD)? lower=numericTypes TO upper=numericTypes #toExpression
@@ -36,7 +36,7 @@ arithmeticOperator
3636
| EXPONENT
3737
;
3838

39-
arrayArithmeticFunction
39+
arithmeticFunction
4040
: MIN
4141
| MAX
4242
| SUM
@@ -48,9 +48,8 @@ arithmeticOperator
4848
| INT
4949
;
5050

51-
5251
wordlist
53-
: LPAREN WS* first=types WS* (',' WS* rest=types WS*)* RPAREN
52+
: LPAREN WS* first=expression WS* (',' WS* rest=expression WS*)* RPAREN
5453
;
5554

5655
arrayOperators

src/test/java/com/github/sidhant92/boolparser/parser/antlr/BooleanFilterBoolParserTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import com.github.sidhant92.boolparser.constant.LogicalOperationType;
1111
import com.github.sidhant92.boolparser.constant.NodeType;
1212
import com.github.sidhant92.boolparser.constant.Operator;
13-
import com.github.sidhant92.boolparser.domain.StringNode;
1413
import com.github.sidhant92.boolparser.domain.arithmetic.ArithmeticFunctionNode;
1514
import com.github.sidhant92.boolparser.domain.arithmetic.ArithmeticLeafNode;
1615
import com.github.sidhant92.boolparser.domain.arithmetic.ArithmeticNode;
@@ -330,7 +329,9 @@ public void testStringList2() {
330329
public void testSingleToken() {
331330
final Try<Node> nodeOptional = boolExpressionBoolParser.parseExpression("a");
332331
assertTrue(nodeOptional.isSuccess());
333-
assertTrue(nodeOptional.get() instanceof StringNode);
332+
assertTrue(nodeOptional.get() instanceof UnaryNode);
333+
assertEquals(((UnaryNode) nodeOptional.get()).getDataType(), DataType.STRING);
334+
assertEquals(((UnaryNode) nodeOptional.get()).getValue(), "a");
334335
}
335336

336337
@Test

0 commit comments

Comments
 (0)