Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bind custom operators between mul and unary #895

Merged
merged 8 commits into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 77 additions & 39 deletions src/main/java/com/hubspot/jinjava/el/ext/ExtendedParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,83 @@ protected AstRangeBracket createAstRangeBracket(
);
}

@Override
protected AstNode mul(boolean required) throws ScanException, ParseException {
AstNode v = filter(required);
if (v == null) {
return null;
}
while (true) {
switch (getToken().getSymbol()) {
case MUL:
consumeToken();
v = createAstBinary(v, filter(true), AstBinary.MUL);
break;
case DIV:
consumeToken();
v = createAstBinary(v, filter(true), AstBinary.DIV);
break;
case MOD:
consumeToken();
v = createAstBinary(v, filter(true), AstBinary.MOD);
break;
case EXTENSION:
if (getExtensionHandler(getToken()).getExtensionPoint() == ExtensionPoint.MUL) {
v = getExtensionHandler(consumeToken()).createAstNode(v, filter(true));
break;
}
default:
return v;
}
}
}

protected AstNode filter(boolean required) throws ScanException, ParseException {
AstNode v = unary(required);
if (v == null) {
return null;
}
while (true) {
if ("|".equals(getToken().getImage()) && lookahead(0).getSymbol() == IDENTIFIER) {
do {
consumeToken(); // '|'
String filterName = consumeToken().getImage();
List<AstNode> filterParams = Lists.newArrayList(v, interpreter());

// optional filter args
if (getToken().getSymbol() == Symbol.LPAREN) {
AstParameters astParameters = params();
for (int i = 0; i < astParameters.getCardinality(); i++) {
filterParams.add(astParameters.getChild(i));
}
}

AstProperty filterProperty = createAstDot(
identifier(FILTER_PREFIX + filterName),
"filter",
true
);
v = createAstMethod(filterProperty, createAstParameters(filterParams)); // function("filter:" + filterName, new AstParameters(filterParams));
} while ("|".equals(getToken().getImage()));
} else if (
"is".equals(getToken().getImage()) &&
"not".equals(lookahead(0).getImage()) &&
isPossibleExpTest(lookahead(1).getSymbol())
) {
consumeToken(); // 'is'
consumeToken(); // 'not'
v = buildAstMethodForIdentifier(v, "evaluateNegated");
} else if (
"is".equals(getToken().getImage()) && isPossibleExpTest(lookahead(0).getSymbol())
) {
consumeToken(); // 'is'
v = buildAstMethodForIdentifier(v, "evaluate");
} else {
return v;
}
}
}

@Override
protected AstNode value() throws ScanException, ParseException {
tkindy marked this conversation as resolved.
Show resolved Hide resolved
boolean lvalue = true;
Expand Down Expand Up @@ -475,45 +552,6 @@ protected AstNode value() throws ScanException, ParseException {

break;
default:
if (
"|".equals(getToken().getImage()) && lookahead(0).getSymbol() == IDENTIFIER
) {
do {
consumeToken(); // '|'
String filterName = consumeToken().getImage();
List<AstNode> filterParams = Lists.newArrayList(v, interpreter());

// optional filter args
if (getToken().getSymbol() == Symbol.LPAREN) {
AstParameters astParameters = params();
for (int i = 0; i < astParameters.getCardinality(); i++) {
filterParams.add(astParameters.getChild(i));
}
}

AstProperty filterProperty = createAstDot(
identifier(FILTER_PREFIX + filterName),
"filter",
true
);
v = createAstMethod(filterProperty, createAstParameters(filterParams)); // function("filter:" + filterName, new AstParameters(filterParams));
} while ("|".equals(getToken().getImage()));
} else if (
"is".equals(getToken().getImage()) &&
"not".equals(lookahead(0).getImage()) &&
isPossibleExpTest(lookahead(1).getSymbol())
) {
consumeToken(); // 'is'
consumeToken(); // 'not'
v = buildAstMethodForIdentifier(v, "evaluateNegated");
} else if (
"is".equals(getToken().getImage()) &&
isPossibleExpTest(lookahead(0).getSymbol())
) {
consumeToken(); // 'is'
v = buildAstMethodForIdentifier(v, "evaluate");
}

return v;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,4 +332,36 @@ public void itHidesWarningErrors() {
);
assertThat(interpreter.getErrors()).isEmpty();
}

@Test
public void itBindsUnaryMinusTighterThanCmp() {
assertThat(interpreter.render("{{ (-5 > 4) }}")).isEqualTo("false");
}

@Test
public void itBindsUnaryMinusTighterThanIs() {
assertThat(interpreter.render("{{ (-5 is integer) == true }}")).isEqualTo("true");
}

@Test
public void itBindsUnaryMinusTighterThanIsNot() {
assertThat(interpreter.render("{{ (-5 is not integer) == false }}"))
.isEqualTo("true");
}

@Test
public void itBindsUnaryMinusTighterThanFilters() {
assertThat(interpreter.render("{{ (-5 | abs) }}")).isEqualTo("5");
}

@Test
public void itBindsFiltersTighterThanMul() {
assertThat(interpreter.render("{{ (-5 * -4 | abs) }}")).isEqualTo("-20");
}

@Test
public void itInterpretsFilterChainsInOrder() {
assertThat(interpreter.render("{{ 'foo' | upper | replace('O', 'A') }}"))
.isEqualTo("FAA");
}
}