diff options
author | Ryo Nihei <nihei.dev@gmail.com> | 2021-04-08 00:36:22 +0900 |
---|---|---|
committer | Ryo Nihei <nihei.dev@gmail.com> | 2021-04-08 00:36:22 +0900 |
commit | 91b93662e8ec8a92d763fad74da56b360bba2660 (patch) | |
tree | 231e3728a0b682d9376bbe84c7a7a6ce55ff4177 /compiler/ast.go | |
parent | Print the result of the lex command in JSON format (diff) | |
download | tre-91b93662e8ec8a92d763fad74da56b360bba2660.tar.gz tre-91b93662e8ec8a92d763fad74da56b360bba2660.tar.xz |
Add logging to compile command
compile command writes logs out to the maleeni-compile.log file.
When you use compiler.Compile(), you can choose whether the lexer writes logs or not.
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) + } +} |