-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
176 lines (165 loc) · 3.45 KB
/
main.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
173
174
175
176
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"strings"
"unicode/utf8"
"github.com/elves/upgrade-scripts-for-0.11/parse"
)
func main() {
filenames := os.Args[1:]
if len(filenames) == 0 {
fixed, ok := fix("[stdin]", func() ([]byte, error) {
return ioutil.ReadAll(os.Stdin)
})
if ok {
os.Stdout.Write(fixed)
}
} else {
// Fix files.
for _, filename := range filenames {
fixed, ok := fix(filename, func() ([]byte, error) {
return ioutil.ReadFile(filename)
})
if ok {
ioutil.WriteFile(filename, fixed, 0644)
}
}
}
}
func fix(filename string, readall func() ([]byte, error)) ([]byte, bool) {
src, err := readall()
if err != nil {
log.Printf("cannot read %s: %s; skipping", filename, err)
return nil, false
}
if !utf8.Valid(src) {
log.Printf("%s not utf8; skipping", filename)
return nil, false
}
// os.Stdout.WriteString(fix("[stdin]", src))
chunk, err := parse.Parse(filename, string(src))
if err != nil {
log.Printf("cannot parse %s: %s; skipping", filename, err)
return nil, false
}
buf := new(bytes.Buffer)
fixNode(chunk, buf)
return buf.Bytes(), true
}
var (
suppress = map[string]bool{
"\n": true, ";": true, "in": true,
"do": true, "done": true,
"then": true, "fi": true,
"tried": true,
}
addSpace = map[string]bool{
"elif": true, "else": true,
"except": true, "finally": true,
}
)
func fixNode(n parse.Node, w io.Writer) {
switch n := n.(type) {
case *parse.Primary:
if n.Type == parse.Variable {
// $&x
// to
// $x~
w.Write([]byte("$" + convert(n.Value)))
} else {
fixNodeDefault(n, w)
}
case *parse.Assignment:
// '&x'={ } echo
// to
// x~={ } echo
for _, child := range n.Children() {
if child == n.Left {
fixLHS(n.Left, w)
} else {
fixNode(child, w)
}
}
case *parse.Form:
for _, child := range n.Children() {
if parse.IsCompound(child) {
var varChild *parse.Compound
for _, v := range n.Vars {
if v == child {
varChild = v
break
}
}
if varChild != nil {
for _, grandchild := range varChild.Children() {
if in := parse.GetIndexing(grandchild); in != nil {
fixLHS(in, w)
continue
}
fixNode(grandchild, w)
}
continue
}
}
fixNode(child, w)
}
case *parse.Compound:
// $x~foo
// to
// $x''~foo
firstIndexing := true
for _, child := range n.Children() {
if parse.IsIndexing(child) {
if !firstIndexing && strings.HasPrefix(child.SourceText(), "~") {
w.Write([]byte("''"))
}
firstIndexing = false
}
fixNode(child, w)
}
default:
fixNodeDefault(n, w)
}
}
func fixNodeDefault(n parse.Node, w io.Writer) {
if len(n.Children()) == 0 {
text := n.SourceText()
w.Write([]byte(text))
} else {
for _, child := range n.Children() {
fixNode(child, w)
}
}
}
func fixLHS(n *parse.Indexing, w io.Writer) {
for _, child := range n.Children() {
if child == n.Head {
w.Write([]byte(convert(n.Head.Value)))
} else {
fixNode(child, w)
}
}
}
func convert(old string) string {
ns, name := parseVariableQName(old)
if strings.HasPrefix(name, "&") {
name = name[1:] + "~"
}
qname := ns + name
if strings.ContainsRune(qname, '&') {
fmt.Fprintf(os.Stderr, "Warning: rewritten variable $%s still contains &\n", qname)
}
return qname
}
func parseVariableQName(qname string) (ns, name string) {
i := strings.LastIndexByte(qname, ':')
if i == -1 {
return "", qname
}
return qname[:i+1], qname[i+1:]
}