-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from uaraven/graph-diagram
Add Graphiz dot graph generator
- Loading branch information
Showing
12 changed files
with
264 additions
and
60 deletions.
There are no files selected for viewing
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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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,74 @@ | ||
package net.ninjacat.headlights | ||
|
||
import net.ninjacat.headlights.antlr.ParseTreeError | ||
import net.ninjacat.headlights.antlr.ParseTreeNode | ||
import net.ninjacat.headlights.antlr.ParseTreeTerminal | ||
|
||
object GraphvizGen { | ||
|
||
private val displayStyles = mapOf( | ||
"bg" to "dimgray", | ||
"bg_node" to "azure", | ||
"edge_color" to "lightsteelblue", | ||
"error_text" to "red", | ||
"error_stroke" to "orangered", | ||
"error_shape" to "note", | ||
"error_style" to "filled", | ||
"terminal_text" to "navyblue", | ||
"terminal_stroke" to "blue", | ||
"terminal_shape" to "box", | ||
"terminal_style" to "filled", | ||
"rule_text" to "black", | ||
"rule_stroke" to "white", | ||
"rule_shape" to "box", | ||
"rule_style" to "\"filled,rounded\"", | ||
) | ||
|
||
private val exportStyles = mapOf( | ||
"bg" to "white", | ||
"bg_node" to "white", | ||
"edge_color" to "black", | ||
"error_text" to "red", | ||
"error_stroke" to "orangered", | ||
"error_shape" to "note", | ||
"error_style" to "\"\"", | ||
"terminal_text" to "navyblue", | ||
"terminal_stroke" to "blue", | ||
"terminal_shape" to "box", | ||
"terminal_style" to "\"\"", | ||
"rule_text" to "black", | ||
"rule_stroke" to "black", | ||
"rule_shape" to "box", | ||
"rule_style" to "rounded", | ||
) | ||
|
||
private var activeStyle = exportStyles | ||
|
||
fun generateDotFile(root: ParseTreeNode, export: Boolean = false): String { | ||
val nodes = mutableListOf<String>() | ||
val edges = mutableListOf<String>() | ||
activeStyle = if (export) exportStyles else displayStyles | ||
|
||
processNodes(root, nodes, edges) | ||
return "graph ParseTree {\nbgcolor=${activeStyle["bg"]}\n" + nodes.joinToString("\n") + "\n" + edges.joinToString("\n") + "\n}\n" | ||
} | ||
|
||
private fun nodeStyle(nodeType: String): String { | ||
return "color=${activeStyle[nodeType + "_stroke"]} style=${activeStyle[nodeType+"_style"]} " + | ||
"fillcolor=${activeStyle["bg_node"]} fontcolor=${activeStyle[nodeType + "_text"]} " + | ||
"shape=${activeStyle[nodeType + "_shape"]}" | ||
} | ||
|
||
private fun processNodes(node: ParseTreeNode, nodes: MutableList<String>, edges: MutableList<String>) { | ||
when (node) { | ||
is ParseTreeError -> nodes.add("\"${node.text}_${node.id}\" [${nodeStyle("error")} label=\"${node.text}\"]") | ||
is ParseTreeTerminal -> nodes.add("\"${node.text}_${node.id}\" [${nodeStyle("terminal")} label=\"${node.text}\"]") | ||
else -> nodes.add("\"${node.text}_${node.id}\" [${nodeStyle("rule")} label=\"${node.text}\"]") | ||
} | ||
val edgeIds = node.children.map { "\"${it.text}_${it.id}\"" }.joinToString(" ") | ||
if (node.children.isNotEmpty()) { | ||
edges.add("\"${node.text}_${node.id}\" -- {$edgeIds} [color=${activeStyle["edge_color"]}]") | ||
} | ||
node.children.forEach { processNodes(it, nodes, edges) } | ||
} | ||
} |
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
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
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
Oops, something went wrong.