Skip to content
Snippets Groups Projects
Commit 2f66b9da authored by Johannes Mey's avatar Johannes Mey
Browse files

update AST printing

parent cbd8f4ad
No related branches found
No related tags found
No related merge requests found
Pipeline #14001 passed
relast_version = 0.3.0-137
relast2uml_version = 1.0.2-68
relast2uml_version = 1.1.0
jupyter_version = 5.8.2
assertj_version = 3.22.0
grammar2uml_version = 0.2.1
......
aspect Tokens {
inh boolean Token.isSubToken();
eq Token.getChild().isSubToken() = true;
eq MotionGrammarElement.getChild().isSubToken() = false;
syn boolean Token.isNegated() = getClass().getSimpleName().matches("(?s).*Not?[A-Z].*");
syn String Token.negatedLabel() = "¬" + getClass().getSimpleName().replaceAll("Not?([A-Z])", "$1");
syn String Token.label() {
String result = "";
boolean useOr = isNegated();
boolean first = true;
if (numChildren() > 0) {
for (ASTNode c : this.astChildren()) {
if (first) {
first = false;
} else {
if (useOr) {
result += " || ";
} else {
result += " && ";
}
}
if (c instanceof Opt) {
Opt optChild = (Opt) c;
if (optChild.getChild(0) != null) {
result += ((Token)optChild.getChild(0)).label();
} else {
result += "false";
}
} else {
result += ((Token) c).label();
}
}
} else {
result = isNegated() ? negatedLabel() : getClass().getSimpleName();
}
return result;
}
}
......@@ -16,7 +16,6 @@ aspect Tracing {
try {
de.tudresden.inf.st.jastadd.dumpAst.ast.Dumper.read(world)
.setNameMethod(o -> o == null ? "null" : o.getClass().getSimpleName())
.includeRelationsFor(".*", ".*")
.setBackgroundColorMethod(n -> (n == highlightNode ? highlightColor : DEFAULT_COLOR))
.excludeTokens("Q.") // FIXME remove domain-specific code
.dumpAsPNG(de.tudresden.inf.st.mg.common.MotionGrammarConfig.astDiagramDir.resolve("Context-" + worldName + "-" + new java.text.SimpleDateFormat("yyyy.MM.dd.HH.mm.ss.SSS").format(new java.util.Date()) + "-" + step + ".png"));
......
......@@ -42,10 +42,9 @@ public abstract class MotionGrammarParser<T extends MotionGrammarElement> {
try {
// TODO remove this once the issue in relast2uml has been resolved
if (rootContainer_.getChild(0) != null) {
Dumper.read(rootContainer_.getChild(0))
.setBackgroundColorMethod(n -> (n == highlightNode ? (n instanceof Token ? H_T : H_N) : (n instanceof Token ? N_T : N_N)))
.includeChildWhen((parentNode, childNode, contextName) -> !(parentNode instanceof Token))
.setNameMethod(o -> {
String result = "";
try {
......@@ -53,29 +52,25 @@ public abstract class MotionGrammarParser<T extends MotionGrammarElement> {
result += "<:boom:> ";
} catch (NoSuchMethodException ignored) {
}
return result + o.getClass().getSimpleName();
if (o instanceof Token) {
Token t = (Token) o;
// every token with optional children has at least one (in the OR-case)
result += t.label();
} else {
result += o.getClass().getSimpleName();
}
return result;
})
.customPreamble("title " + step)
.skinParam(SkinParamBooleanSetting.Shadowing, false)
.skinParam(SkinParamStringSetting.backgroundColor, "white")
.dumpAsPNG(MotionGrammarConfig.astDiagramDir.resolve("AST-" + String.format("%03d", timeStep_) + "-" + new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss.SSS").format(new java.util.Date()) + "-" + step + ".png"));
}
timeStep_++;
} catch (IOException e) {
e.printStackTrace();
}
}
public static class ParseException extends Exception {
public ParseException(String ruleName, TokenType... expectedTokens) {
super("Unable to parse nonterminal " + ruleName + ". Expected tokens " + Arrays.stream(expectedTokens).map(Object::toString).collect(Collectors.joining(", ")) + ".");
}
public ParseException(TokenType expectedToken) {
super("Unable to parse token " + expectedToken.name() + ".");
}
}
/**
* Parse a motion grammar with root T
*
......@@ -97,4 +92,14 @@ public abstract class MotionGrammarParser<T extends MotionGrammarElement> {
}
protected abstract void parse(ASTNode<?> parent, int index) throws ParseException;
public static class ParseException extends Exception {
public ParseException(String ruleName, TokenType... expectedTokens) {
super("Unable to parse nonterminal " + ruleName + ". Expected tokens " + Arrays.stream(expectedTokens).map(Object::toString).collect(Collectors.joining(", ")) + ".");
}
public ParseException(TokenType expectedToken) {
super("Unable to parse token " + expectedToken.name() + ".");
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment