-
Notifications
You must be signed in to change notification settings - Fork 6
/
locale_generate.go
342 lines (293 loc) · 7.7 KB
/
locale_generate.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// +build ignored
package main
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"sort"
"strconv"
"strings"
"unicode"
)
const QtVer = "5.13"
const QtSrc = "https://raw.githubusercontent.com/qt/qtbase/" + QtVer + "/"
func main() {
if err := generate(); err != nil {
fmt.Fprintf(os.Stderr, "Error: generate locale_generated.go: %v\n", err)
os.Exit(1)
return
}
}
func generate() error {
resp, err := http.Get(QtSrc + "src/corelib/tools/qlocale.h")
if err != nil {
return fmt.Errorf("get qlocale.h: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("get qlocale.h: response status %s", resp.Status)
}
language, country, err := parse(resp.Body)
if err != nil {
return fmt.Errorf("parse qlocale.h: %w", err)
}
//
f, err := ioutil.TempFile(".", "locale_generate")
if err != nil {
return fmt.Errorf("create temp file: %w", err)
}
defer f.Close()
defer os.Remove(f.Name())
if _, err := fmt.Fprintf(f, "// Code generated by locale_generate.go from Qt %s. DO NOT EDIT.\npackage qrc\n", QtVer); err != nil {
return err
}
if _, err := fmt.Fprintf(f, "\n// Language is a language supported by Qt (note: multiple names can have the\n// same code).\n"); err != nil {
return err
}
if err := language.GenerateGo(f, "Language", "uint16"); err != nil {
return err
}
if _, err := fmt.Fprintf(f, "\n// Country is a country supported by Qt (note: multiple names can have the same\n// code).\n"); err != nil {
return err
}
if err := country.GenerateGo(f, "Country", "uint16"); err != nil {
return err
}
if err := f.Close(); err != nil {
return fmt.Errorf("close temp file: %w", err)
}
if err := os.Rename(f.Name(), "locale_generated.go"); err != nil {
return fmt.Errorf("rename temp file: %w", err)
}
//
tf, err := ioutil.TempFile(".", "locale_generate")
if err != nil {
return fmt.Errorf("create temp file: %w", err)
}
defer tf.Close()
defer os.Remove(tf.Name())
if _, err := fmt.Fprintf(tf, "// Code generated by locale_generate.go from Qt %s. DO NOT EDIT.\npackage qrc\n", QtVer); err != nil {
return err
}
if _, err := fmt.Fprintf(tf, "\nimport %#v\n", "testing"); err != nil {
return err
}
if err := language.GenerateGoTest(tf, "Language"); err != nil {
return err
}
if err := country.GenerateGoTest(tf, "Country"); err != nil {
return err
}
if err := tf.Close(); err != nil {
return fmt.Errorf("close temp file: %w", err)
}
if err := os.Rename(tf.Name(), "locale_generated_test.go"); err != nil {
return fmt.Errorf("rename temp file: %w", err)
}
return nil
}
func parse(r io.Reader) (language *Enum, country *Enum, err error) {
type State int
const (
StateDefault State = iota
StateLanguage
StateCountry
)
var p struct {
State
Line string
Language, Country *Enum
}
s := bufio.NewScanner(r)
for s.Scan() {
p.Line = strings.Join(strings.Fields(strings.TrimSpace(s.Text())), " ")
switch p.State {
case StateDefault:
switch p.Line {
case "enum Language {":
if p.Language != nil {
return nil, nil, fmt.Errorf("parse qlocale.h: parse languages: already seen")
}
p.Language = NewEnum()
p.State = StateLanguage
case "enum Country {":
if p.Country != nil {
return nil, nil, fmt.Errorf("parse qlocale.h: parse countries: already seen")
}
p.Country = NewEnum()
p.State = StateCountry
default:
p.State = StateDefault
}
case StateLanguage:
switch p.Line {
case "":
p.State = StateLanguage
case "};":
p.State = StateDefault
default:
if err := p.Language.ParseC(p.Line); err != nil {
return nil, nil, fmt.Errorf("parse qlocale.h: parse languages: %w", err)
}
p.State = StateLanguage
}
case StateCountry:
switch p.Line {
case "":
p.State = StateCountry
case "};":
p.State = StateDefault
default:
if err := p.Country.ParseC(p.Line); err != nil {
return nil, nil, fmt.Errorf("parse qlocale.h: parse languages: %w", err)
}
p.State = StateCountry
}
}
}
if err := s.Err(); err != nil {
return nil, nil, err
}
if p.State != StateDefault {
return nil, nil, fmt.Errorf("parse qlocale.h: unexpected EOF at state %d (last line: %q)", p.State, p.Line)
}
if l, c := p.Language != nil, p.Country != nil; !l || !c {
return nil, nil, fmt.Errorf("parse qlocale.h: missing language (found: %t) or country (found: %t)", l, c)
}
return p.Language, p.Country, nil
}
type Enum struct {
Value map[string]int
Name map[int]string
}
func NewEnum() *Enum {
return &Enum{
Value: map[string]int{},
Name: map[int]string{},
}
}
func (e *Enum) ParseC(line string) error {
spl1 := strings.Split(line, " = ")
if len(spl1) != 2 {
return fmt.Errorf("line %q: expected one %q", line, " = ")
}
spl2 := strings.Split(spl1[1], ",")
if len(spl2) > 2 {
return fmt.Errorf("line %q: expected at most one %q", line, ",")
}
a, b := spl1[0], spl2[0]
for _, c := range a {
if !unicode.IsLetter(c) {
return fmt.Errorf("line %q: bad language %q", line, a)
}
}
if _, ok := e.Value[a]; ok {
return fmt.Errorf("line %q: language %q already seen", line, a)
}
if v, err := strconv.Atoi(b); err == nil {
e.Value[a] = v
if _, ok := e.Name[v]; !ok {
e.Name[v] = a
}
} else if v, ok := e.Value[b]; ok {
e.Value[a] = v
} else {
return fmt.Errorf("line %q: invalid value %q", line, b)
}
return nil
}
func (e Enum) GenerateGo(w io.Writer, typeName, typeType string) error {
var vw int
var v []string
for n := range e.Value {
v = append(v, n)
if len(n) > vw {
vw = len(n)
}
}
sort.Strings(v)
var x []int
for c := range e.Name {
x = append(x, c)
}
sort.Ints(x)
if _, err := fmt.Fprintf(w, "type %s %s\n", typeName, typeType); err != nil {
return err
}
if _, err := fmt.Fprintf(w, "\nconst (\n"); err != nil {
return err
}
for _, n := range v {
if e.Name[e.Value[n]] == n {
if _, err := fmt.Fprintf(w, "\t%s%*s %s = %#v\n", typeName, -1*vw, n, typeName, e.Value[n]); err != nil {
return err
}
} else {
if _, err := fmt.Fprintf(w, "\t%s%*s %s = %s%s\n", typeName, -1*vw, n, typeName, typeName, e.Name[e.Value[n]]); err != nil {
return err
}
}
}
if _, err := fmt.Fprintf(w, ")\n"); err != nil {
return err
}
if _, err := fmt.Fprintf(w, "\nfunc (%c %s) String() string {\n\tswitch %c {\n", strings.ToLower(typeName)[0], typeName, strings.ToLower(typeName)[0]); err != nil {
return err
}
for _, c := range x {
if _, err := fmt.Fprintf(w, "\tcase %#v:\n\t\treturn %#v", c, e.Name[c]); err != nil {
return err
}
var tmp []string
for _, n := range v {
if e.Value[n] == c && e.Name[e.Value[n]] != n {
tmp = append(tmp, n)
}
}
if len(tmp) != 0 {
if _, err := fmt.Fprintf(w, " // %s\n", strings.Join(tmp, ", ")); err != nil {
return err
}
} else {
if _, err := fmt.Fprintf(w, "\n"); err != nil {
return err
}
}
}
if _, err := fmt.Fprintf(w, "\t}\n\tpanic(%#v)\n}\n", "no such "+strings.ToLower(typeName)); err != nil {
return err
}
return nil
}
func (e Enum) GenerateGoTest(w io.Writer, typeName string) error {
var x []struct {
C int
N string
X string
}
for n, c := range e.Value { // not e.Name since we want to test that aliases map to the correct names
x = append(x, struct {
C int
N string
X string
}{c, n, e.Name[c]})
}
sort.Slice(x, func(i, j int) bool {
return x[i].C < x[j].C
})
if _, err := fmt.Fprintf(w, "\nfunc Test%s(t *testing.T) {\n", typeName); err != nil {
return err
}
for _, c := range x {
if _, err := fmt.Fprintf(w, "\tif %s%s != %#v || %s%s.String() != %#v {\n\t\tt.Errorf(%#v, %#v)\n\t}\n", typeName, c.N, c.C, typeName, c.N, c.X, "%q incorrect", c.N); err != nil {
return err
}
}
if _, err := fmt.Fprintf(w, "}\n"); err != nil {
return err
}
return nil
}