summaryrefslogtreecommitdiffstats
path: root/vespajlib
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@verizonmedia.com>2019-06-30 13:20:49 -0500
committerJon Bratseth <bratseth@verizonmedia.com>2019-06-30 13:20:49 -0500
commit40144341bdbbfcec9f21ee3784e3e3cf5e320c91 (patch)
treef21c5039c36cc550efcfeaf9fbd77e7c2e9434d3 /vespajlib
parent32a5521059e08308b5abae10d6b5e8ce1589e705 (diff)
Output the intermediate graph
Diffstat (limited to 'vespajlib')
-rw-r--r--vespajlib/src/main/java/com/yahoo/text/ParenthesisExpressionPrettyPrinter.java47
-rw-r--r--vespajlib/src/test/java/com/yahoo/text/ParenthesisExpressionPrettyPrinterTest.java82
2 files changed, 129 insertions, 0 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/text/ParenthesisExpressionPrettyPrinter.java b/vespajlib/src/main/java/com/yahoo/text/ParenthesisExpressionPrettyPrinter.java
new file mode 100644
index 00000000000..ad235d78679
--- /dev/null
+++ b/vespajlib/src/main/java/com/yahoo/text/ParenthesisExpressionPrettyPrinter.java
@@ -0,0 +1,47 @@
+// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.text;
+
+/**
+ * Pretty prints any parenthesis expression
+ *
+ * @author bratseth
+ */
+public class ParenthesisExpressionPrettyPrinter {
+
+ private static final int indentUnit = 2;
+
+ public static String prettyPrint(String parenthesisExpression) {
+ StringBuilder b = new StringBuilder();
+ prettyPrint(parenthesisExpression, 0, b);
+ return b.toString();
+ }
+
+ private static void prettyPrint(String expression, int indent, StringBuilder b) {
+ int nextStartParenthesis = expression.indexOf("(");
+ int nextEndParenthesis = expression.indexOf(")");
+ if (nextStartParenthesis < 0)
+ nextStartParenthesis = Integer.MAX_VALUE;
+ if (nextEndParenthesis < 0)
+ nextEndParenthesis = Integer.MAX_VALUE;
+
+ boolean start = nextStartParenthesis < nextEndParenthesis;
+ int nextParenthesis = Math.min(nextStartParenthesis, nextEndParenthesis);
+
+ int effectiveIndent = start || nextParenthesis > 0 ? indent : indent - 2;
+ b.append(" ".repeat(Math.max(0, effectiveIndent)));
+ if (nextParenthesis == Integer.MAX_VALUE) {
+ b.append(expression);
+ }
+ else {
+ if (! start && nextParenthesis > 0) {
+ b.append(expression, 0, nextParenthesis).append("\n");
+ b.append(" ".repeat(Math.max(0, indent - 2))).append(")\n");
+ }
+ else {
+ b.append(expression, 0, nextParenthesis + 1).append("\n");
+ }
+ prettyPrint(expression.substring(nextParenthesis + 1), indent + (start ? indentUnit : -indentUnit), b);
+ }
+ }
+
+}
diff --git a/vespajlib/src/test/java/com/yahoo/text/ParenthesisExpressionPrettyPrinterTest.java b/vespajlib/src/test/java/com/yahoo/text/ParenthesisExpressionPrettyPrinterTest.java
new file mode 100644
index 00000000000..79bdc6a5318
--- /dev/null
+++ b/vespajlib/src/test/java/com/yahoo/text/ParenthesisExpressionPrettyPrinterTest.java
@@ -0,0 +1,82 @@
+// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.text;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * @author bratseth
+ */
+public class ParenthesisExpressionPrettyPrinterTest {
+
+ @Test
+ public void testBasic() {
+ String expected =
+ "foo(\n" +
+ " bar(\n" +
+ " baz(\n" +
+ " )\n" +
+ " )\n" +
+ ")\n";
+ assertPrettyPrint(expected, "foo(bar(baz()))");
+ }
+
+ @Test
+ public void testInnerContent() {
+ String expected =
+ "foo(\n" +
+ " bar(\n" +
+ " baz(\n" +
+ " hello world\n" +
+ " )\n" +
+ " )\n" +
+ ")\n";
+ assertPrettyPrint(expected, "foo(bar(baz(hello world)))");
+ }
+ @Test
+ public void testUnmatchedStart() {
+ String expected =
+ "foo(\n" +
+ " (\n" +
+ " bar(\n" +
+ " baz(\n" +
+ " )\n" +
+ " )\n" +
+ " )\n" +
+ " ";
+ assertPrettyPrint(expected, "foo((bar(baz()))");
+ }
+
+ @Test
+ public void testUnmatchedEnd() {
+ String expected =
+ "foo(\n" +
+ " bar(\n" +
+ " baz(\n" +
+ " )\n" +
+ " )\n" +
+ ")\n" +
+ ")\n";
+ assertPrettyPrint(expected, "foo(bar(baz())))");
+ }
+
+ @Test
+ public void testNoParenthesis() {
+ String expected =
+ "foo bar baz";
+ assertPrettyPrint(expected, "foo bar baz");
+ }
+
+ @Test
+ public void testEmpty() {
+ String expected =
+ "";
+ assertPrettyPrint(expected, "");
+ }
+
+ private void assertPrettyPrint(String expected, String expression) {
+ assertEquals(expected, ParenthesisExpressionPrettyPrinter.prettyPrint(expression));
+ }
+
+}