3 Star 0 Fork 1

Gitee 极速下载/ntt

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/nokia/ntt
克隆/下载
dot.go 1.60 KB
一键复制 编辑 原始数据 按行查看 历史
Matthias Simon 提交于 2023-04-17 11:14 . Move ast package to syntax package
package main
import (
"bufio"
"encoding/base64"
"fmt"
"html"
"os"
"strings"
"github.com/nokia/ntt/ttcn3/syntax"
)
func dot(n syntax.Node) {
w := bufio.NewWriter(os.Stdout)
defer w.Flush()
w.WriteString(`digraph {
rankdir=LR
`)
q := []syntax.Node{n}
toks := []syntax.Token{}
for len(q) > 0 {
n := q[0]
q = q[1:]
if syntax.IsNil(n) {
continue
}
if tok, ok := n.(syntax.Token); ok {
toks = append(toks, tok)
continue
}
fmt.Fprintf(w, "\t%s %s;\n", nodeID(n), nodeProps(n))
for _, child := range n.Children() {
if !syntax.IsNil(child) {
fmt.Fprintf(w, "\t%s -> %s;\n", nodeID(n), nodeID(child))
q = append(q, child)
}
}
}
w.WriteString(" { \n")
for _, tok := range toks {
fmt.Fprintf(w, "\t%s %s;\n", nodeID(tok), nodeProps(tok))
}
w.WriteString(" }")
w.WriteString("}")
}
func nodeID(n syntax.Node) string {
if tok, ok := n.(syntax.Token); ok {
return fmt.Sprintf("t%d", tok.Pos())
}
return fmt.Sprintf("n%p%s", n, base64.RawStdEncoding.EncodeToString([]byte(fmt.Sprintf("%T", n))))
}
func nodeProps(n syntax.Node) string {
if tok, ok := n.(syntax.Token); ok {
label := fmt.Sprintf("%v", tok.Kind())
if tok.Kind().IsLiteral() {
label = tok.String()
}
return fmt.Sprintf("[label=<<B>%s</B>>; shape=box; style=filled; fillcolor=lightgrey]", escape(label))
}
label := strings.TrimPrefix(fmt.Sprintf("%T", n), "*syntax.")
return fmt.Sprintf("[label=\"%s\"]", label)
}
func escape(s string) string {
s = html.EscapeString(s)
s = strings.Replace(s, "[", " [", -1)
s = strings.Replace(s, "|", " |", -1)
s = strings.Replace(s, "]", " ]", -1)
return s
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors/ntt.git
git@gitee.com:mirrors/ntt.git
mirrors
ntt
ntt
master

搜索帮助