代码拉取完成,页面将自动刷新
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
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。