-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat.sh
More file actions
executable file
·136 lines (119 loc) · 3.35 KB
/
format.sh
File metadata and controls
executable file
·136 lines (119 loc) · 3.35 KB
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
#!/bin/bash
types() {
for file in ast_class.go ast_conditional.go ast_expression.go ast_function.go ast.go ast_module.go ast_statement.go; do
while read type; do
echo "$type" "$file";
done < <(grep "type .* struct {" "$file" | cut -d' ' -f2);
done | sort;
}
(
cat <<HEREDOC
package javascript
// File automatically generated with format.sh.
HEREDOC
while read type file; do
echo -e "\nfunc (f *$type) printType(w writer, v bool) {";
echo " pp := w.Indent()";
echo;
echo " pp.WriteString(\"$type {\")";
while read fieldName fieldType; do
if [ -z "$fieldType" ]; then
fieldType="$fieldName";
fieldName="${fieldName/\*/}";
fi;
if [ "$fieldType" = "bool" ]; then
echo;
echo " if f.$fieldName || v {";
echo " pp.Printf(\"\\n$fieldName: %v\", f.$fieldName)";
echo " }";
elif [ "$fieldType" = "uint" -o "$fieldType" = "int" ]; then
echo;
echo " if f.$fieldName != 0 || v {";
echo " pp.Printf(\"\\n$fieldName: %v\", f.$fieldName)";
echo " }";
elif [ "${fieldType:0:2}" = "[]" ]; then
echo;
echo " if f.$fieldName == nil {";
echo " pp.WriteString(\"\\n$fieldName: nil\")";
echo " } else if len(f.$fieldName) > 0 {";
echo " pp.WriteString(\"\\n$fieldName: [\")";
echo;
echo " ipp := pp.Indent()";
echo;
echo " for n, e := range f.$fieldName {";
echo " ipp.Printf(\"\n%d: \", n)";
echo " e.printType(ipp, v)";
echo " }";
echo;
echo " pp.WriteString(\"\\n]\")";
echo " } else if v {";
echo " pp.WriteString(\"\\n$fieldName: []\")";
echo " }";
elif [ "${fieldType:0:1}" = "[" ]; then
echo;
echo " pp.WriteString(\"\\n$fieldName: [\")";
echo;
echo " ipp := pp.Indent()";
echo;
echo " for n, e := range f.$fieldName {";
echo " ipp.Printf(\"\n%d: \", n)";
echo " e.printType(ipp, v)";
echo " }";
echo;
echo " pp.WriteString(\"\n]\")";
elif [ "${fieldType:0:1}" = "*" ]; then
echo;
echo " if f.$fieldName != nil {";
echo " pp.WriteString(\"\\n$fieldName: \")";
echo " f.$fieldName.printType(pp, v)";
echo " } else if v {";
echo " pp.WriteString(\"\\n$fieldName: nil\")";
echo " }";
else
echo;
echo " pp.WriteString(\"\\n$fieldName: \")";
echo " f.$fieldName.printType(pp, v)";
fi;
done < <(sed '/^type '$type' struct {$/,/^}$/!d;//d' "$file" | grep -v "^$");
echo;
echo " w.WriteString(\"\\n}\")";
echo "}";
done < <(types);
) > "format_types.go";
(
cat <<HEREDOC
package javascript
// File automatically generated with format.sh.
import "fmt"
HEREDOC
while read type _; do
echo -e "\n// Format implements the fmt.Formatter interface";
echo "func (f $type) Format(s fmt.State, v rune) {";
echo " if v == 'v' && s.Flag('#') {";
echo " type X = $type";
echo " type $type X";
echo;
echo " fmt.Fprintf(s, \"%#v\", $type(f))";
echo " } else {";
echo " format(&f, s, v)";
echo " }";
echo "}";
done < <(types);
) > "format_format.go";
(
cat <<HEREDOC
package javascript
// File automatically generated with format.sh.
import "fmt"
// Type is an interface satisfied by all javascript structural types.
type Type interface {
fmt.Formatter
javascriptType()
}
func (Tokens) javascriptType() {}
func (Token) javascriptType() {}
HEREDOC
while read type _; do
echo -e "\nfunc ($type) javascriptType() {}";
done < <(types);
) > "types.go";