Fixed up node util.
This commit is contained in:
parent
22352ea7f0
commit
752d58c40a
@ -1,72 +0,0 @@
|
||||
package groowt.view.component.web.ast;
|
||||
|
||||
import groowt.view.component.web.antlr.TokenList;
|
||||
import groowt.view.component.web.ast.extension.NodeExtension;
|
||||
import groowt.view.component.web.ast.node.LeafNode;
|
||||
import groowt.view.component.web.ast.node.Node;
|
||||
import groowt.view.component.web.ast.node.TreeNode;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public final class NodeUtil {
|
||||
|
||||
public static boolean isAnyOfType(Node subject, Class<?>... nodeTypes) {
|
||||
Objects.requireNonNull(subject);
|
||||
for (final var type : nodeTypes) {
|
||||
if (type.isAssignableFrom(subject.getClass())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static boolean hasExtensionOfType(Node subject, Class<?>... extensionTypes) {
|
||||
Objects.requireNonNull(subject);
|
||||
for (final var extensionType : extensionTypes) {
|
||||
if (subject.hasExtension((Class<? extends NodeExtension>) extensionType)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean isAnyOfType(Node subject, List<Class<? extends Node>> nodeTypes) {
|
||||
Objects.requireNonNull(subject);
|
||||
for (final var type : nodeTypes) {
|
||||
if (type.isAssignableFrom(subject.getClass())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static @Nullable LeafNode getDeepestLeftmostLeafNode(Node node) {
|
||||
return switch (node) {
|
||||
case LeafNode leafNode -> leafNode;
|
||||
case TreeNode treeNode when treeNode.getChildrenSize() > 0 ->
|
||||
getDeepestLeftmostLeafNode(treeNode.getAt(0));
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
|
||||
private static void doFormatAst(Node node, StringBuilder sb, int indentTimes, String indent, TokenList tokenList) {
|
||||
NodeUtilKt.formatSingleNode(node, sb, indentTimes, indent, tokenList);
|
||||
if (node instanceof TreeNode treeNode) {
|
||||
treeNode.getChildren().forEach(child -> doFormatAst(
|
||||
child, sb, indentTimes + 1, indent, tokenList
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
public static String formatAst(Node node, TokenList tokenList) {
|
||||
final var sb = new StringBuilder();
|
||||
doFormatAst(node, sb, 0, " ", tokenList);
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private NodeUtil() {}
|
||||
|
||||
}
|
@ -1,11 +1,45 @@
|
||||
@file:JvmName("NodeUtil")
|
||||
package groowt.view.component.web.ast
|
||||
|
||||
import groowt.view.component.web.antlr.TokenList
|
||||
import groowt.view.component.web.antlr.formatToken
|
||||
import groowt.view.component.web.ast.node.LeafNode
|
||||
import groowt.view.component.web.ast.node.Node
|
||||
import groowt.view.component.web.ast.node.TreeNode
|
||||
|
||||
fun formatSingleNode(node: Node, sb: StringBuilder, indentTimes: Int, indent: String, tokenList: TokenList) {
|
||||
fun getDeepestLeftmost(node: Node): LeafNode? {
|
||||
return when (node) {
|
||||
is LeafNode -> node
|
||||
is TreeNode -> {
|
||||
if (node.children.isNotEmpty()) {
|
||||
getDeepestLeftmost(node.children.first())
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun formatAst(node: Node, tokenList: TokenList): String {
|
||||
val sb = StringBuilder()
|
||||
formatAst(node, tokenList, sb, indentTimes = 0, indent = " ")
|
||||
return sb.toString()
|
||||
}
|
||||
|
||||
fun formatAst(
|
||||
node: Node,
|
||||
tokenList: TokenList,
|
||||
sb: StringBuilder,
|
||||
indentTimes: Int = 0,
|
||||
indent: String = " ",
|
||||
) {
|
||||
formatSingleNode(node, sb, indentTimes, indent, tokenList)
|
||||
if (node is TreeNode) {
|
||||
node.children.forEach { formatAst(it, tokenList, sb, indentTimes + 1, indent) }
|
||||
}
|
||||
}
|
||||
|
||||
private fun formatSingleNode(node: Node, sb: StringBuilder, indentTimes: Int, indent: String, tokenList: TokenList) {
|
||||
sb.append(indent.repeat(indentTimes))
|
||||
sb.append(
|
||||
"${node.javaClass.simpleName}(${node.tokenRange.startPosition.toStringShort()}.."
|
||||
|
Loading…
Reference in New Issue
Block a user