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

Custom placeholders #107

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
Prev Previous commit
Next Next commit
Abstract Sdf3 tranformation tasks
Virtlink committed Jan 28, 2022

Verified

This commit was signed with the committer’s verified signature.
Virtlink Daniel A.A. Pelsmaeker
commit 7ad3439be32865417e9ed151339321139fd55fcd
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package mb.sdf3.task;

import mb.common.result.Result;
import mb.pie.api.ExecContext;
import mb.pie.api.Interactivity;
import mb.pie.api.Supplier;
import mb.pie.api.TaskDef;
import mb.sdf3.Sdf3Scope;
import mb.sdf3.stratego.Sdf3Context;
import mb.sdf3.task.spec.Sdf3Config;
import mb.stratego.common.StrategoException;
import mb.stratego.common.StrategoRuntime;
import mb.stratego.pie.AstStrategoTransformTaskDef;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spoofax.interpreter.terms.IStrategoTerm;

import javax.inject.Inject;
import javax.inject.Provider;
import java.io.Serializable;
import java.util.Objects;
import java.util.Set;

/**
* Abstract task definition similar to {@link AstStrategoTransformTaskDef}
* but injects a {@link Sdf3Context} into the {@link StrategoRuntime}.
*/
public abstract class Sdf3AstStrategoTransformTaskDef<I extends Sdf3AstStrategoTransformTaskDef.Input> implements TaskDef<I, Result<IStrategoTerm, ?>> {
public /* open */ static class Input implements Serializable {
public final Supplier<? extends Result<IStrategoTerm, ?>> astSupplier;
public final String strategyAffix;
public final Sdf3Config sdf3Config;

public Input(
Supplier<? extends Result<IStrategoTerm, ?>> astSupplier,
String strategyAffix,
Sdf3Config sdf3Config
) {
this.astSupplier = astSupplier;
this.strategyAffix = strategyAffix;
this.sdf3Config = sdf3Config;
}

@Override public boolean equals(@Nullable Object o) {
if(this == o) return true;
if(o == null || getClass() != o.getClass()) return false;
final Input input = (Input)o;
return astSupplier.equals(input.astSupplier)
&& strategyAffix.equals(input.strategyAffix)
&& sdf3Config.equals(input.sdf3Config);
}

@Override public int hashCode() {
return Objects.hash(astSupplier, strategyAffix, sdf3Config);
}

@Override public String toString() {
return "Sdf3AstStrategoTransformTaskDef$Input{" +
"astSupplier=" + astSupplier +
", strategyAffix='" + strategyAffix + '\'' +
", sdf3Config=" + sdf3Config +
'}';
}
}

private final Provider<StrategoRuntime> strategoRuntimeProvider;

protected Sdf3AstStrategoTransformTaskDef(Provider<StrategoRuntime> strategoRuntimeProvider) {
this.strategoRuntimeProvider = strategoRuntimeProvider;
}

@Override public Result<IStrategoTerm, ?> exec(ExecContext context, I input) throws Exception {
final Sdf3Context sdf3Context = new Sdf3Context(
input.strategyAffix,
input.sdf3Config.placeholderPrefix,
input.sdf3Config.placeholderSuffix
);
final StrategoRuntime strategoRuntime = strategoRuntimeProvider.get().addContextObject(sdf3Context);
return context.require(input.astSupplier).flatMapOrElse((ast) -> {
try {
ast = doExec(context, input, strategoRuntime, ast);
return Result.ofOk(ast);
} catch(Exception e) {
return Result.ofErr(e);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Catching all Exceptions is bad practice, as it also catches all RuntimeExceptions indicating bugs such as NullPointerException which should just crash the build, as well as catching InterruptedExceptions which should cancel the build, etc.

}
}, Result::ofErr);
}

protected abstract IStrategoTerm doExec(ExecContext context, I input, StrategoRuntime strategoRuntime, IStrategoTerm ast) throws StrategoException, Exception;
}
Original file line number Diff line number Diff line change
@@ -1,90 +1,30 @@
package mb.sdf3.task;

import mb.common.result.Result;
import mb.pie.api.ExecContext;
import mb.pie.api.Interactivity;
import mb.pie.api.Supplier;
import mb.pie.api.TaskDef;
import mb.sdf3.stratego.Sdf3Context;
import mb.sdf3.Sdf3Scope;
import mb.sdf3.task.spec.Sdf3Config;
import mb.sdf3.task.spec.Sdf3SpecConfig;
import mb.stratego.common.StrategoException;
import mb.stratego.common.StrategoRuntime;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spoofax.interpreter.terms.IStrategoTerm;

import javax.inject.Inject;
import javax.inject.Provider;
import java.io.Serializable;
import java.util.Objects;
import java.util.Set;

@Sdf3Scope
public class Sdf3ToPrettyPrinter implements TaskDef<Sdf3ToPrettyPrinter.Input, Result<IStrategoTerm, ?>> {
public static class Input implements Serializable {
public final Supplier<? extends Result<IStrategoTerm, ?>> astSupplier;
public final String strategyAffix;
public final Sdf3Config sdf3Config;

public Input(
Supplier<? extends Result<IStrategoTerm, ?>> astSupplier,
String strategyAffix,
Sdf3Config sdf3Config
) {
this.astSupplier = astSupplier;
this.strategyAffix = strategyAffix;
this.sdf3Config = sdf3Config;
}

@Override public boolean equals(@Nullable Object o) {
if(this == o) return true;
if(o == null || getClass() != o.getClass()) return false;
final Input input = (Input)o;
return astSupplier.equals(input.astSupplier)
&& strategyAffix.equals(input.strategyAffix)
&& sdf3Config.equals(input.sdf3Config);
}

@Override public int hashCode() {
return Objects.hash(astSupplier, strategyAffix, sdf3Config);
}

@Override public String toString() {
return "Sdf3ToPrettyPrinter$Input{" +
"astSupplier=" + astSupplier +
", strategyAffix='" + strategyAffix + '\'' +
", sdf3Config=" + sdf3Config +
'}';
}
}

private final Provider<StrategoRuntime> strategoRuntimeProvider;
public class Sdf3ToPrettyPrinter extends Sdf3AstStrategoTransformTaskDef<Sdf3AstStrategoTransformTaskDef.Input> {

@Inject public Sdf3ToPrettyPrinter(Provider<StrategoRuntime> strategoRuntimeProvider) {
this.strategoRuntimeProvider = strategoRuntimeProvider;
super(strategoRuntimeProvider);
}


@Override public String getId() {
return getClass().getName();
}

@Override public Result<IStrategoTerm, ?> exec(ExecContext context, Input input) throws Exception {
final Sdf3Context sdf3Context = new Sdf3Context(
input.strategyAffix,
input.sdf3Config.placeholderPrefix,
input.sdf3Config.placeholderSuffix
);
final StrategoRuntime strategoRuntime = strategoRuntimeProvider.get().addContextObject(sdf3Context);
return context.require(input.astSupplier).flatMapOrElse((ast) -> {
try {
ast = strategoRuntime.invoke("module-to-pp", ast, strategoRuntime.getTermFactory().makeString("2"));
return Result.ofOk(ast);
} catch(StrategoException e) {
return Result.ofErr(e);
}
}, Result::ofErr);
@Override
protected IStrategoTerm doExec(ExecContext context, Input input, StrategoRuntime strategoRuntime, IStrategoTerm ast) throws StrategoException, Exception {
return strategoRuntime.invoke("module-to-pp", ast, strategoRuntime.getTermFactory().makeString("2"));
}

@Override public boolean shouldExecWhenAffected(Input input, Set<?> tags) {