
java_cup.runtime.SyntaxTreeDFS Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of javacup Show documentation
Show all versions of javacup Show documentation
A fork of the Java Cup parser generator with some bug fixes and enhancements. (Forked at version 11b)
The newest version!
package java_cup.runtime;
import java.util.HashMap;
import java.util.List;
public class SyntaxTreeDFS {
public static interface ElementHandler {
public void handle(XMLElement parent, List children);
}
public static abstract class AbstractVisitor implements Visitor {
private HashMap preMap = new HashMap<>();
private HashMap postMap = new HashMap<>();
public abstract void defaultPre(XMLElement element, List children);
public abstract void defaultPost(XMLElement element, List children);
@Override
public void preVisit(XMLElement element) {
ElementHandler handler = preMap.get(element.tagname);
if (handler == null) {
defaultPre(element, element.getChildren());
} else
handler.handle(element, element.getChildren());
}
@Override
public void postVisit(XMLElement element) {
ElementHandler handler = postMap.get(element.tagname);
if (handler == null) {
defaultPost(element, element.getChildren());
} else
handler.handle(element, element.getChildren());
}
public void registerPreVisit(String s, ElementHandler h) {
preMap.put(s, h);
}
public void registerPostVisit(String s, ElementHandler h) {
postMap.put(s, h);
}
}
public static interface Visitor {
public void preVisit(XMLElement element);
public void postVisit(XMLElement element);
}
public static void dfs(XMLElement element, Visitor visitor) {
visitor.preVisit(element);
for (XMLElement el : element.getChildren()) {
dfs(el, visitor);
}
visitor.postVisit(element);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy