Skip to content

Commit

Permalink
Conditional Block
Browse files Browse the repository at this point in the history
Signed-off-by: tvallin <[email protected]>
  • Loading branch information
tvallin committed Oct 18, 2023
1 parent d010f93 commit f07d6e4
Show file tree
Hide file tree
Showing 12 changed files with 1,149 additions and 228 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import io.helidon.build.archetype.engine.v2.Walker;
import io.helidon.build.archetype.engine.v2.ast.Block;
import io.helidon.build.archetype.engine.v2.ast.Condition;
import io.helidon.build.archetype.engine.v2.ast.ConditionBlock;
import io.helidon.build.archetype.engine.v2.ast.DeclaredBlock;
import io.helidon.build.archetype.engine.v2.ast.Expression;
import io.helidon.build.archetype.engine.v2.ast.Expression.Token;
Expand Down Expand Up @@ -195,8 +196,12 @@ public VisitResult visitInvocation(Invocation invocation, Script script) {

@Override
public VisitResult visitCondition(Condition cond, Script script) {
exprId = exprIds.computeIfAbsent(cond.expression(), this::exprId);
return VisitResult.CONTINUE;
return evaluate(cond.expression());
}

@Override
public VisitResult visitConditionBlock(ConditionBlock condition, JsonObjectBuilder arg) {
return evaluate(condition.expression());
}

@Override
Expand Down Expand Up @@ -369,6 +374,11 @@ private String exprId(Expression expr) {
return exprId;
}

private VisitResult evaluate(Expression expression) {
exprId = exprIds.computeIfAbsent(expression, this::exprId);
return VisitResult.CONTINUE;
}

private static final class TokenVisitor implements Token.Visitor<JsonObjectBuilder> {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import io.helidon.build.archetype.engine.v2.ast.Block;
import io.helidon.build.archetype.engine.v2.ast.Condition;
import io.helidon.build.archetype.engine.v2.ast.ConditionBlock;
import io.helidon.build.archetype.engine.v2.ast.Expression;
import io.helidon.build.archetype.engine.v2.ast.Model;
import io.helidon.build.archetype.engine.v2.ast.Node.VisitResult;
import io.helidon.build.archetype.engine.v2.ast.Output;
Expand Down Expand Up @@ -92,10 +94,12 @@ public VisitResult postVisitBlock(Block block, Context ctx) {

@Override
public VisitResult visitCondition(Condition condition, Context ctx) {
if (condition.expression().eval(ctx::getValue)) {
return VisitResult.CONTINUE;
}
return VisitResult.SKIP_SUBTREE;
return evaluate(condition.expression(), ctx);
}

@Override
public VisitResult visitConditionBlock(ConditionBlock condition, Context ctx) {
return evaluate(condition.expression(), ctx);
}

/**
Expand Down Expand Up @@ -212,4 +216,11 @@ public static void walk(InputResolver resolver,
throw new IllegalStateException("Invalid scope after walking block");
}
}

private VisitResult evaluate(Expression expression, Context ctx) {
if (expression.eval(ctx::getValue)) {
return VisitResult.CONTINUE;
}
return VisitResult.SKIP_SUBTREE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import io.helidon.build.archetype.engine.v2.ast.Block;
import io.helidon.build.archetype.engine.v2.ast.Condition;
import io.helidon.build.archetype.engine.v2.ast.ConditionBlock;
import io.helidon.build.archetype.engine.v2.ast.DynamicValue;
import io.helidon.build.archetype.engine.v2.ast.Input;
import io.helidon.build.archetype.engine.v2.ast.Invocation;
Expand Down Expand Up @@ -178,6 +179,7 @@ private static final class ReaderImpl implements SimpleXMLParser.Reader {
private String qName;
private Map<String, Value> attrs;
private LinkedList<Context> stack;
private LinkedList<String> expressions;
private Context ctx;
private Script.Builder scriptBuilder;

Expand All @@ -188,6 +190,7 @@ private static final class ReaderImpl implements SimpleXMLParser.Reader {
Script read(InputStream is, Path path) throws IOException {
this.path = Objects.requireNonNull(path, "path is null");
stack = new LinkedList<>();
expressions = new LinkedList<>();
parser = SimpleXMLParser.create(is, this);
parser.parse();
if (scriptBuilder == null) {
Expand All @@ -198,7 +201,7 @@ Script read(InputStream is, Path path) throws IOException {

@Override
public void startElement(String qName, Map<String, String> attrs) {
this.qName = qName;
this.qName = qName.replace("-", "");
this.attrs = Maps.mapValue(attrs, DynamicValue::create);
Location location = Location.of(path, parser.lineNumber(), parser.charNumber());
info = BuilderInfo.of(loader, path, location);
Expand Down Expand Up @@ -241,6 +244,11 @@ public void endElement(String name) {

void processElement() {
switch (qName) {
case "if":
case "elseif":
case "else":
processCondition();
return;
case "directory":
case "help":
stack.push(new Context(ctx.state, new ValueBuilder(ctx.builder, qName)));
Expand Down Expand Up @@ -338,6 +346,32 @@ void processValidation() {
}
}

void processCondition() {
String expression;
ConditionBlock.Builder builder;
Block.Kind kind = blockKind();
switch (kind) {
case IF:
expression = processXmlEscapes(attrs.get("expr").asString());
builder = ConditionBlock.builder(info, kind).expression(expression);
expressions.push(expression);
break;
case ELSEIF:
expression = String.format("!(%s) && %s", expressions.pop(), processXmlEscapes(attrs.get("expr").asString()));
builder = ConditionBlock.builder(info, kind).expression(expression);
expressions.push(expression);
break;
case ELSE:
expression = expressions.pop();
builder = ConditionBlock.builder(info, kind).expression(String.format("!(%s)", expression));
break;
default:
throw new XMLReaderException(String.format(
"Invalid input block: %s. { element=%s }", kind, qName));
}
addChild(ctx.state, builder);
}

void processPreset() {
Block.Builder builder;
Block.Kind kind = blockKind();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2022 Oracle and/or its affiliates.
* Copyright (c) 2021, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -277,6 +277,28 @@ default VisitResult postVisitModel(Model model, A arg) {
return postVisitAny(model, arg);
}

/**
* Visit a condition block.
*
* @param condition condition block
* @param arg visitor argument
* @return result
*/
default VisitResult visitConditionBlock(ConditionBlock condition, A arg) {
return visitAny(condition, arg);
}

/**
* Visit a condition block after traversing the nested nodes.
*
* @param condition condition block
* @param arg visitor argument
* @return result
*/
default VisitResult postVisitConditionBlock(ConditionBlock condition, A arg) {
return postVisitAny(condition, arg);
}

/**
* Visit any block.
*
Expand Down Expand Up @@ -504,6 +526,21 @@ public enum Kind {
* Invoke.
*/
INVOKE,

/**
* If.
*/
IF,

/**
* Else.
*/
ELSE,

/**
* Else-if.
*/
ELSEIF
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.build.archetype.engine.v2.ast;

/**
* ConditionBlock.
*/
public final class ConditionBlock extends Block {
private final Expression expression;

private ConditionBlock(Builder builder) {
super(builder);
this.expression = builder.expression;
}

/**
* Create a new Builder instance.
*
* @param info builder info
* @param kind kind
* @return builder instance
*/
public static Builder builder(BuilderInfo info, Kind kind) {
return new Builder(info, kind);
}

/**
* Get the expression.
*
* @return expression
*/
public Expression expression() {
return expression;
}

@Override
public <A> VisitResult accept(Block.Visitor<A> visitor, A arg) {
return visitor.visitConditionBlock(this, arg);
}

@Override
public <A> VisitResult acceptAfter(Visitor<A> visitor, A arg) {
return visitor.postVisitConditionBlock(this, arg);
}

/**
* ConditionBlock builder.
*/
public static final class Builder extends Block.Builder {
private Expression expression;

private Builder(BuilderInfo info, Kind kind) {
super(info, kind);
}

/**
* Set the expression.
*
* @param expression expression
* @return this builder
*/
public Builder expression(String expression) {
this.expression = Expression.create(expression);
return this;
}

@Override
protected ConditionBlock doBuild() {
return new ConditionBlock(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import io.helidon.build.archetype.engine.v2.Walker;
import io.helidon.build.archetype.engine.v2.ast.Block;
import io.helidon.build.archetype.engine.v2.ast.Condition;
import io.helidon.build.archetype.engine.v2.ast.ConditionBlock;
import io.helidon.build.archetype.engine.v2.ast.Expression;
import io.helidon.build.archetype.engine.v2.ast.Input;
import io.helidon.build.archetype.engine.v2.ast.Input.DeclaredInput;
Expand Down Expand Up @@ -149,10 +150,9 @@ private List<Block> refs(String path, Context ctx) {
return null;
}

@Override
public VisitResult visitCondition(Condition condition, Context ctx) {
private VisitResult evaluate(Node node, Expression expression, Context ctx) {
try {
condition.expression().eval(variable -> {
expression.eval(variable -> {
List<Block> refs = refs(variable, ctx);
if (refs == null || refs.isEmpty()) {
return null;
Expand All @@ -174,20 +174,30 @@ public VisitResult visitCondition(Condition condition, Context ctx) {
});
} catch (Expression.UnresolvedVariableException ex) {
errors.add(String.format("%s %s: '%s'",
condition.location(),
node.location(),
EXPR_UNRESOLVED_VARIABLE,
ex.variable()));
} catch (IllegalStateException ex) {
errors.add(String.format(
"%s %s: '%s'",
condition.location(),
node.location(),
EXPR_EVAL_ERROR,
ex.getMessage()));
}
// visit all branches
return VisitResult.CONTINUE;
}

@Override
public VisitResult visitCondition(Condition condition, Context ctx) {
return evaluate(condition, condition.expression(), ctx);
}

@Override
public VisitResult visitConditionBlock(ConditionBlock condition, Context ctx) {
return evaluate(condition, condition.expression(), ctx);
}

@Override
public VisitResult visitPreset(Preset preset, Context ctx) {
ctx.scope().putValue(preset.path(), preset.value(), preset.isModel(), ValueKind.LOCAL_VAR);
Expand Down
Loading

0 comments on commit f07d6e4

Please sign in to comment.