-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
chapi-ast-java/src/main/kotlin/chapi/ast/javaast/TreeViewer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package chapi.ast.javaast | ||
|
||
import org.antlr.v4.runtime.misc.Utils; | ||
import org.antlr.v4.runtime.tree.Tree; | ||
import org.antlr.v4.runtime.tree.Trees; | ||
|
||
class TreeViewer { | ||
|
||
/** Platform dependent end-of-line marker */ | ||
val Eol = System.lineSeparator() | ||
|
||
/** The literal indent char(s) used for pretty-printing */ | ||
val Indents = " " | ||
private var level = 0 | ||
|
||
/** | ||
* Pretty print out a whole tree. [.getNodeText] is used on the node payloads to get the text | ||
* for the nodes. (Derived from Trees.toStringTree(....)) | ||
*/ | ||
fun toPrettyTree(t: Tree, ruleNames: List<String>): String { | ||
level = 0 | ||
return process(t, ruleNames).replace("(?m)^\\s+$".toRegex(), "").replace("\\r?\\n\\r?\\n".toRegex(), Eol) | ||
} | ||
|
||
private fun process(t: Tree, ruleNames: List<String>): String { | ||
if (t.childCount == 0) return Utils.escapeWhitespace(Trees.getNodeText(t, ruleNames), false) | ||
val sb = StringBuilder() | ||
sb.append(lead(level)) | ||
level++ | ||
val s: String = Utils.escapeWhitespace(Trees.getNodeText(t, ruleNames), false) | ||
sb.append("$s ") | ||
for (i in 0 until t.getChildCount()) { | ||
sb.append(process(t.getChild(i), ruleNames)) | ||
} | ||
level-- | ||
sb.append(lead(level)) | ||
return sb.toString() | ||
} | ||
|
||
private fun lead(level: Int): String { | ||
val sb = StringBuilder() | ||
if (level > 0) { | ||
sb.append(Eol) | ||
for (cnt in 0 until level) { | ||
sb.append(Indents) | ||
} | ||
} | ||
|
||
return sb.toString() | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
chapi-ast-java/src/test/kotlin/chapi/ast/javaast/TreeViewerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package chapi.ast.javaast | ||
|
||
import org.junit.jupiter.api.Test | ||
|
||
internal class TreeViewerTest { | ||
@Test | ||
fun show() { | ||
JavaAnalyser().parse("public class Test { }").let { | ||
val ruleNamesList: List<String> = it.ruleNames.toList() | ||
println(TreeViewer().toPrettyTree(it.compilationUnit(), ruleNamesList)) | ||
} | ||
} | ||
} |