Commit 63dcdedb authored by Malte Heithoff's avatar Malte Heithoff
Browse files

Implementing Visitors

parent 31bb7f4d
Pipeline #392215 passed with stage
in 1 minute and 31 seconds
/**
* (c) https://github.com/MontiCore/monticore
*
* The license generally applicable for this project
* can be found under https://github.com/MontiCore/monticore.
*/
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.lang.monticar.emadl.math.visitor;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarcmath._symboltable.math.symbols.*;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarcmath._symboltable.math.visitor.CopyEMAMMathExpressionSymbol;
import de.monticore.lang.math._symboltable.MathStatementsSymbol;
import de.monticore.lang.math._symboltable.expression.MathExpressionSymbol;
import de.monticore.lang.math._symboltable.visitor.MathStatementsSymbolCopy;
import de.monticore.lang.monticar.cnnarch._symboltable.TupleExpressionSymbol;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class CopyEMADLMathExpressionSymbol extends CopyEMAMMathExpressionSymbol
implements EMADLMathExpressionSymbolVisitor {
public static MathStatementsSymbol copy(MathStatementsSymbol symbol) {
MathStatementsSymbolCopy res = new MathStatementsSymbolCopy(symbol.getName(), symbol.astMathStatements);
if (symbol.getAstNode().isPresent()) res.setAstNode(symbol.getAstNode().get());
res.setAccessModifier(symbol.getAccessModifier());
res.setPackageName(symbol.getPackageName());
res.setFullName(symbol.getFullName());
List<MathExpressionSymbol> mathExpressionSymbolsCopy = new LinkedList<>();
for (MathExpressionSymbol mathExpressionSymbol : symbol.getMathExpressionSymbols()) {
mathExpressionSymbolsCopy.add(copy(mathExpressionSymbol));
}
res.setMathExpressionSymbols(mathExpressionSymbolsCopy);
return res;
}
public static <T extends MathExpressionSymbol> T copy(T symbol) {
CopyEMADLMathExpressionSymbol copy = new CopyEMADLMathExpressionSymbol();
copy.handle(symbol);
T res = copy.get(symbol);
return res;
}
@Override
protected <T extends MathExpressionSymbol> T get(T symbol) {
MathExpressionSymbol copy = copyMap.get(symbol);
if (copy != null) return (T) copy;
if (symbol instanceof TupleExpressionSymbol)
copy = new TupleExpressionSymbol();
else
return super.get(symbol);
copyMap.put(symbol, copy);
return (T) copy;
}
@Override
public void endVisit(TupleExpressionSymbol node) {
TupleExpressionSymbol res = get(node);
copyMathExpressionSymbol(res, node);
for (MathExpressionSymbol expression : node.getExpressions())
res.add(get(expression));
}
}
/**
* (c) https://github.com/MontiCore/monticore
*
* The license generally applicable for this project
* can be found under https://github.com/MontiCore/monticore.
*/
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.lang.monticar.emadl.math.visitor;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarcmath._symboltable.math.visitor.EMAMMathExpressionSymbolParentAwareVisitor;
import de.monticore.lang.monticar.cnnarch._symboltable.TupleExpressionSymbol;
public interface EMADLMathExpressionSymbolParentAwareVisitor extends EMAMMathExpressionSymbolParentAwareVisitor,
EMADLMathExpressionSymbolVisitor {
@Override
default void handle(TupleExpressionSymbol node) {
if (shouldContinue(node)) {
visit(node);
pushParent(node);
traverse(node);
popParent();
endVisit(node);
}
}
}
/**
* (c) https://github.com/MontiCore/monticore
*
* The license generally applicable for this project
* can be found under https://github.com/MontiCore/monticore.
*/
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.lang.monticar.emadl.math.visitor;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarcmath._symboltable.math.visitor.EMAMMathExpressionSymbolReplacementVisitor;
import de.monticore.lang.math._symboltable.MathStatementsSymbol;
import de.monticore.lang.math._symboltable.expression.MathExpressionSymbol;
import de.monticore.lang.monticar.cnnarch._symboltable.TupleExpressionSymbol;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
public class EMADLMathExpressionSymbolReplacementVisitor extends EMAMMathExpressionSymbolReplacementVisitor
implements EMADLMathExpressionSymbolVisitor {
public EMADLMathExpressionSymbolReplacementVisitor(Map<MathExpressionSymbol, MathExpressionSymbol> replacementMap) {
super(replacementMap);
}
public EMADLMathExpressionSymbolReplacementVisitor(Function<MathExpressionSymbol, MathExpressionSymbol> replacementFunction) {
super(replacementFunction);
}
public static void replace(MathExpressionSymbol mathExpressionSymbol,
Map<MathExpressionSymbol, MathExpressionSymbol> replacementMap) {
EMADLMathExpressionSymbolReplacementVisitor replacementVisitor =
new EMADLMathExpressionSymbolReplacementVisitor(replacementMap);
replacementVisitor.handle(mathExpressionSymbol);
}
public static void replace(MathExpressionSymbol mathExpressionSymbol,
Function<MathExpressionSymbol, MathExpressionSymbol> replacementFunction) {
EMADLMathExpressionSymbolReplacementVisitor replacementVisitor =
new EMADLMathExpressionSymbolReplacementVisitor(replacementFunction);
replacementVisitor.handle(mathExpressionSymbol);
}
public static void replace(MathStatementsSymbol mathStatementsSymbol,
Map<MathExpressionSymbol, MathExpressionSymbol> replacementMap) {
EMADLMathExpressionSymbolReplacementVisitor replacementVisitor =
new EMADLMathExpressionSymbolReplacementVisitor(replacementMap);
replacementVisitor.handle(mathStatementsSymbol);
}
public static void replace(MathStatementsSymbol mathStatementsSymbol,
Function<MathExpressionSymbol, MathExpressionSymbol> replacementFunction) {
EMADLMathExpressionSymbolReplacementVisitor replacementVisitor =
new EMADLMathExpressionSymbolReplacementVisitor(replacementFunction);
replacementVisitor.handle(mathStatementsSymbol);
}
@Override
public void visit(TupleExpressionSymbol node) {
List<MathExpressionSymbol> expressions = new LinkedList();
for (MathExpressionSymbol expression : node.getExpressions())
expressions.add(get(expression));
node.setExpressions(expressions);
}
}
/**
* (c) https://github.com/MontiCore/monticore
*
* The license generally applicable for this project
* can be found under https://github.com/MontiCore/monticore.
*/
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.lang.monticar.emadl.math.visitor;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarcmath._symboltable.math.visitor.EMAMMathExpressionSymbolVisitor;
import de.monticore.lang.math._symboltable.expression.MathExpressionSymbol;
import de.monticore.lang.monticar.cnnarch._symboltable.TupleExpressionSymbol;
public interface EMADLMathExpressionSymbolVisitor extends EMAMMathExpressionSymbolVisitor {
@Override
default void handle(MathExpressionSymbol node) {
if (node == null) return;
else if (node instanceof TupleExpressionSymbol)
handle((TupleExpressionSymbol) node);
else
EMAMMathExpressionSymbolVisitor.super.handle(node);
}
public default void handle(TupleExpressionSymbol node) {
if (shouldContinue(node)) {
visit(node);
traverse(node);
endVisit(node);
}
}
public default void traverse(TupleExpressionSymbol node) {
for (MathExpressionSymbol expression : node.getExpressions()) {
handle(expression);
}
}
public default void visit(TupleExpressionSymbol node) {
}
public default void endVisit(TupleExpressionSymbol node) {
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment