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 10, 2023
1 parent d010f93 commit 397cbef
Show file tree
Hide file tree
Showing 11 changed files with 914 additions and 258 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 @@ -168,6 +169,11 @@ private static final class BuilderContext {
}
}

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

@Override
public VisitResult visitMethodInvocation(MethodInvocation inv, Void arg) {
String exprId = this.exprId;
Expand Down Expand Up @@ -195,8 +201,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
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,8 @@
import java.util.Deque;
import java.util.Objects;

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.Model;
import io.helidon.build.archetype.engine.v2.ast.*;
import io.helidon.build.archetype.engine.v2.ast.Node.VisitResult;
import io.helidon.build.archetype.engine.v2.ast.Output;
import io.helidon.build.archetype.engine.v2.ast.Preset;
import io.helidon.build.archetype.engine.v2.ast.Step;
import io.helidon.build.archetype.engine.v2.ast.Variable;
import io.helidon.build.archetype.engine.v2.context.Context;
import io.helidon.build.archetype.engine.v2.context.ContextScope;
import io.helidon.build.archetype.engine.v2.context.ContextValue.ValueKind;
Expand All @@ -48,6 +42,13 @@ private Controller(InputResolver inputResolver,
this.steps = inputResolver.steps();
}

private VisitResult evaluate(Expression expression, Context ctx) {
if (expression.eval(ctx::getValue)) {
return VisitResult.CONTINUE;
}
return VisitResult.SKIP_SUBTREE;
}

@Override
public VisitResult visitStep(Step step, Context ctx) {
steps.push(step);
Expand Down Expand Up @@ -92,10 +93,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
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,28 @@ void processValidation() {
}
}

void processCondition() {
String expression;
ConditionBlock.Builder builder;
Block.Kind kind = blockKind();
switch (kind) {
case IF:
case ELSEIF:
expression = 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,106 @@
/*
* 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 String rawExpression;
private final Expression expression;

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

public static Builder builder(BuilderInfo info, Kind kind) {
return new Builder(info, kind);
}

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

/**
* Get the raw expression.
*
* @return raw expression
*/
public String rawExpression() {
return rawExpression;
}

@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 String rawExpression;
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.rawExpression = expression;
this.expression = Expression.create(expression);
return this;
}

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

@Override
protected ConditionBlock doBuild() {
return new ConditionBlock(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,9 @@

import io.helidon.build.archetype.engine.v2.ScriptLoader;
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.Expression;
import io.helidon.build.archetype.engine.v2.ast.Input;
import io.helidon.build.archetype.engine.v2.ast.*;
import io.helidon.build.archetype.engine.v2.ast.Input.DeclaredInput;
import io.helidon.build.archetype.engine.v2.ast.Node;
import io.helidon.build.archetype.engine.v2.ast.Node.VisitResult;
import io.helidon.build.archetype.engine.v2.ast.Preset;
import io.helidon.build.archetype.engine.v2.ast.Step;
import io.helidon.build.archetype.engine.v2.ast.Value;
import io.helidon.build.archetype.engine.v2.ast.Variable;
import io.helidon.build.archetype.engine.v2.context.Context;
import io.helidon.build.archetype.engine.v2.context.ContextPath;
import io.helidon.build.archetype.engine.v2.context.ContextScope;
Expand Down Expand Up @@ -149,10 +141,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 +165,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 397cbef

Please sign in to comment.