-
Notifications
You must be signed in to change notification settings - Fork 33
/
dump.go
172 lines (148 loc) · 3.97 KB
/
dump.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"bufio"
"encoding/json"
"fmt"
"os"
"reflect"
"strings"
"github.com/fatih/color"
"github.com/nokia/ntt/internal/fs"
"github.com/nokia/ntt/ttcn3"
printer "github.com/nokia/ntt/ttcn3/format"
"github.com/nokia/ntt/ttcn3/syntax"
syntax2 "github.com/nokia/ntt/ttcn3/v2/syntax"
"github.com/spf13/cobra"
)
var (
indent = 0
DumpCommand = &cobra.Command{
Use: "dump",
Short: "Dump TTCN-3 syntax trees",
RunE: func(cmd *cobra.Command, args []string) error {
srcs, _ := fs.TTCN3Files(Project.Sources...)
imports, _ := fs.TTCN3Files(Project.Imports...)
files := append(srcs, imports...)
for _, file := range files {
tree := ttcn3.ParseFile(file)
switch format := Format(); format {
case "ttcn3":
b, err := fs.Content(file)
if err != nil {
fatal(err)
}
if err := printer.Fprint(os.Stdout, b); err != nil {
fatal(err)
}
case "dot":
dot(tree.Root)
case "text":
dumpAST(0, "Root", reflect.ValueOf(tree.Root.NodeList.Nodes))
w.Flush()
case "plain":
if !onlyTokens {
fatal(fmt.Errorf("parsing syntax not implemented. Please use option --only-tokens"))
}
b, err := fs.Content(file)
if err != nil {
fatal(err)
}
w := bufio.NewWriter(os.Stdout)
syntax2.Tokenize(b).Inspect(func(n syntax2.Node) bool {
if !n.IsValid() || !n.IsToken() {
return true
}
if !withTrivia && n.Kind().IsTrivia() {
return true
}
if withPosition {
fmt.Fprintf(w, "%d %d ", n.Pos(), n.End())
}
if n.Kind().IsKeyword() {
fmt.Fprintf(w, "KEYWORD")
} else {
fmt.Fprintf(w, strings.ToUpper(n.Kind().String()))
}
if withValue {
fmt.Fprintf(w, " %s", strings.ReplaceAll(n.Text(), "\n", "\\n"))
}
fmt.Fprintln(w)
return true
})
w.Flush()
default:
fatal(fmt.Errorf("format not supported: %s", Format()))
}
}
return nil
},
}
outputTTCN3 = false
outputDot = false
onlyTokens = false
withTrivia = false
withPosition = false
withValue = false
bold = color.New(color.Bold)
faint = color.New(color.Faint)
token = color.New(color.FgMagenta)
)
func init() {
DumpCommand.PersistentFlags().BoolVarP(&outputTTCN3, "ttcn3", "", false, "formatted TTCN-3 output")
DumpCommand.PersistentFlags().BoolVarP(&outputDot, "dot", "", false, "graphviz output")
DumpCommand.PersistentFlags().BoolVarP(&onlyTokens, "only-tokens", "", false, "only dump tokens")
DumpCommand.PersistentFlags().BoolVarP(&withTrivia, "with-trivia", "", false, "dump tokens with trivia")
DumpCommand.PersistentFlags().BoolVarP(&withPosition, "with-position", "", false, "dump tokens with position")
DumpCommand.PersistentFlags().BoolVarP(&withValue, "with-value", "", false, "dump token with value")
}
func dumpJSON(tree *ttcn3.Tree) {
b, err := json.MarshalIndent(tree.Root, "", " ")
if err != nil {
fatal(err)
}
fmt.Println(string(b))
}
func dumpAST(indent int, name string, v reflect.Value) {
if !v.IsValid() || v.IsZero() {
return
}
span := ""
if v.CanInterface() {
if n, ok := v.Interface().(syntax.Node); ok {
span = fmt.Sprintf("[%d:%d)", n.Pos(), n.End())
}
}
fmt.Fprintf(w, "%-15s", span)
for i := 0; i < indent; i++ {
faint.Fprint(w, "· ")
}
bold.Fprintf(w, "%s:", name)
if v.CanInterface() {
switch n := v.Interface().(type) {
case syntax.Token:
token.Fprintf(w, " %s\n", n.String())
return // do not recurse any further
case syntax.Node:
fmt.Fprintf(w, " %s", strings.TrimPrefix(reflect.TypeOf(n).String(), "*syntax."))
}
}
fmt.Fprintln(w)
if v.Kind() == reflect.Interface {
v = v.Elem()
}
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
switch v.Kind() {
case reflect.Struct:
for i := 0; i < v.NumField(); i++ {
f := v.Field(i)
tf := v.Type().Field(i)
dumpAST(indent+1, tf.Name, f)
}
case reflect.Slice:
for i := 0; i < v.Len(); i++ {
dumpAST(indent+1, fmt.Sprintf("[%d]", i), v.Index(i))
}
}
}