diff options
Diffstat (limited to 'compiler/ast.go')
-rw-r--r-- | compiler/ast.go | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/compiler/ast.go b/compiler/ast.go index 0b72f8d..e9c1b50 100644 --- a/compiler/ast.go +++ b/compiler/ast.go @@ -2,6 +2,7 @@ package compiler import ( "fmt" + "io" "sort" "strings" ) @@ -421,3 +422,39 @@ func positionSymbols(node astNode, n uint8) uint8 { } return p } + +func printAST(w io.Writer, ast astNode, ruledLine string, childRuledLinePrefix string, withAttrs bool) { + if ast == nil { + return + } + fmt.Fprintf(w, ruledLine) + fmt.Fprintf(w, "node: %v", ast) + if withAttrs { + fmt.Fprintf(w, ", nullable: %v, first: %v, last: %v", ast.nullable(), ast.first(), ast.last()) + } + fmt.Fprintf(w, "\n") + left, right := ast.children() + children := []astNode{} + if left != nil { + children = append(children, left) + } + if right != nil { + children = append(children, right) + } + num := len(children) + for i, child := range children { + line := "└─ " + if num > 1 { + if i == 0 { + line = "├─ " + } else if i < num-1 { + line = "│ " + } + } + prefix := "│ " + if i >= num-1 { + prefix = " " + } + printAST(w, child, childRuledLinePrefix+line, childRuledLinePrefix+prefix, withAttrs) + } +} |