Skip to content

Commit e3a566a

Browse files
committed
Reimplement Grammar to allow rich expressions
1 parent 3987142 commit e3a566a

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

src/main/antlr4/dev/vepo/jsonata/functions/generated/JSONataGrammar.g4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ expression:
1616
| expression ARR_OPEN expression ARR_CLOSE # arrayQuery
1717
| expression rangePredicate # rangeQuery
1818
| expression op=('<' | '<=' | '>' | '>=' | '!=' | '=' | 'in') expression # booleanCompare
19+
| expression '&' expression # concatValues
1920
| '(' expression ')' # contextValue
2021
| STRING # stringValue
2122
| NUMBER # numberValue
Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
package dev.vepo.jsonata.functions;
22

33
import static dev.vepo.jsonata.functions.json.JsonFactory.stringValue;
4-
import static java.util.stream.Collectors.joining;
5-
6-
import java.util.List;
7-
import java.util.function.Function;
84

95
import dev.vepo.jsonata.functions.data.Data;
106

11-
public record StringConcatJSONataFunction(List<Function<Data, Data>> sources) implements JSONataFunction {
7+
public record StringConcatJSONataFunction(JSONataFunction firstValue, JSONataFunction secondValue) implements JSONataFunction {
128

139
@Override
1410
public Data map(Data original, Data current) {
15-
return stringValue(sources.stream()
16-
.map(fn -> fn.apply(current).toJson().asText())
17-
.collect(joining()));
11+
return stringValue(firstValue.map(original, current).toJson().asText() +
12+
secondValue.map(original, current).toJson().asText());
1813
}
1914
}

src/main/java/dev/vepo/jsonata/parser/JSONataGrammarListener.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,21 @@
1616
import dev.vepo.jsonata.functions.ArrayIndexJSONataFunction;
1717
import dev.vepo.jsonata.functions.ArrayQueryJSONataFunction;
1818
import dev.vepo.jsonata.functions.ArrayRangeJSONataFunction;
19-
import dev.vepo.jsonata.functions.BooleanCompareJSONataFunction;
20-
import dev.vepo.jsonata.functions.BooleanOperator;
2119
import dev.vepo.jsonata.functions.CompareOperator;
2220
import dev.vepo.jsonata.functions.CompareValuesJSONataFunction;
21+
import dev.vepo.jsonata.functions.ContextValueJSONataFunction;
2322
import dev.vepo.jsonata.functions.DeepFindByFieldNameJSONataFunction;
2423
import dev.vepo.jsonata.functions.FieldPathJSONataFunction;
25-
import dev.vepo.jsonata.functions.ContextValueJSONataFunction;
2624
import dev.vepo.jsonata.functions.JSONataFunction;
2725
import dev.vepo.jsonata.functions.JoinJSONataFunction;
26+
import dev.vepo.jsonata.functions.StringConcatJSONataFunction;
2827
import dev.vepo.jsonata.functions.WildcardJSONataFunction;
2928
import dev.vepo.jsonata.functions.generated.JSONataGrammarBaseListener;
3029
import dev.vepo.jsonata.functions.generated.JSONataGrammarParser.AllDescendantSearchContext;
3130
import dev.vepo.jsonata.functions.generated.JSONataGrammarParser.ArrayIndexQueryContext;
3231
import dev.vepo.jsonata.functions.generated.JSONataGrammarParser.ArrayQueryContext;
3332
import dev.vepo.jsonata.functions.generated.JSONataGrammarParser.BooleanCompareContext;
33+
import dev.vepo.jsonata.functions.generated.JSONataGrammarParser.ConcatValuesContext;
3434
import dev.vepo.jsonata.functions.generated.JSONataGrammarParser.ContextRefereceContext;
3535
import dev.vepo.jsonata.functions.generated.JSONataGrammarParser.ContextValueContext;
3636
import dev.vepo.jsonata.functions.generated.JSONataGrammarParser.ExpNumberValueContext;
@@ -206,7 +206,12 @@ public void exitToArray(ToArrayContext ctx) {
206206

207207
@Override
208208
public void exitArrayIndexQuery(ArrayIndexQueryContext ctx) {
209+
if (expressions.isEmpty()) {
209210
expressions.offer(new ArrayIndexJSONataFunction(Integer.valueOf(ctx.NUMBER().getText())));
211+
} else {
212+
var previousFunction = expressions.removeLast();
213+
expressions.offer(new JoinJSONataFunction(previousFunction, new ArrayIndexJSONataFunction(Integer.valueOf(ctx.NUMBER().getText()))));
214+
}
210215
}
211216

212217
@Override
@@ -269,6 +274,13 @@ public void exitContextReferece(ContextRefereceContext ctx) {
269274
this.expressions.offer((original, value) -> original);
270275
}
271276

277+
@Override
278+
public void exitConcatValues(ConcatValuesContext ctx) {
279+
var currentFunction = expressions.removeLast();
280+
var previousFunction = expressions.removeLast();
281+
expressions.offer(new StringConcatJSONataFunction(previousFunction, currentFunction));
282+
}
283+
272284
// @Override
273285
// public void exitTransformerStringConcat(TransformerStringConcatContext ctx) {
274286
// this.expressions.peekFirst()

0 commit comments

Comments
 (0)