package io.proleap.vb6.util;

import java.util.Arrays;
import java.util.List;

import org.antlr.v4.runtime.Parser;
import org.antlr.v4.runtime.misc.NotNull;
import org.antlr.v4.runtime.misc.Utils;
import org.antlr.v4.runtime.tree.Tree;
import org.antlr.v4.runtime.tree.Trees;
import org.sonatype.inject.Nullable;

public class TreeUtils {

	public static final String NEWLINE = "\n";

	public static final String TAB = "\t";

	public static String indent(final int indent) {
		return org.apache.commons.lang3.StringUtils.repeat(TAB, indent);
	}

	/**
	 * @see org.antlr.v4.runtime.tree.Trees.toStringTree(Tree, List<String>)
	 */
	public static String toStringTree(@NotNull final Tree t, @Nullable final List<String> ruleNames, final int depth) {
		String s = Utils.escapeWhitespace(Trees.getNodeText(t, ruleNames), false);

		if (t.getChildCount() == 0) {
			return s;
		}

		final StringBuilder buf = new StringBuilder();

		if (depth > 0) {
			buf.append(NEWLINE);
		}

		buf.append(indent(depth));
		buf.append("(");
		s = Utils.escapeWhitespace(Trees.getNodeText(t, ruleNames), false);
		buf.append(s);
		buf.append(' ');

		for (int i = 0; i < t.getChildCount(); i++) {
			if (i > 0) {
				buf.append(' ');
			}

			buf.append(toStringTree(t.getChild(i), ruleNames, depth + 1));
		}

		buf.append(")");

		return buf.toString();
	}

	/**
	 * @see org.antlr.v4.runtime.tree.Trees.toStringTree(Tree, Parser)
	 */
	public static String toStringTree(@NotNull final Tree t, @Nullable final Parser recog) {
		final String[] ruleNames = recog != null ? recog.getRuleNames() : null;
		final List<String> ruleNamesList = ruleNames != null ? Arrays.asList(ruleNames) : null;

		return toStringTree(t, ruleNamesList, 0);
	}
}

@@ -112,50 +112,8 @@
 er;%0A
-import org.antlr.v4.runtime.misc.NotNull;%0A
 impo
@@ -612,33 +612,24 @@
 oStringTree(
-@NotNull 
 final Tree t
@@ -1398,17 +1398,8 @@
 ree(
-@NotNull 
 fina

removed not null