1
2
3
4
5
6
7 package main
8
9 import (
10 "flag"
11 "log"
12
13 "golang.org/x/text/internal/gen"
14 "golang.org/x/text/internal/triegen"
15 "golang.org/x/text/internal/ucd"
16 )
17
18 var outputFile = flag.String("out", "tables.go", "output file")
19
20 func main() {
21 gen.Init()
22 gen.Repackage("gen_trieval.go", "trieval.go", "bidi")
23 gen.Repackage("gen_ranges.go", "ranges_test.go", "bidi")
24
25 genTables()
26 }
27
28
29
30 var bidiClass = map[string]Class{
31 "AL": AL,
32 "AN": AN,
33 "B": B,
34 "BN": BN,
35 "CS": CS,
36 "EN": EN,
37 "ES": ES,
38 "ET": ET,
39 "L": L,
40 "NSM": NSM,
41 "ON": ON,
42 "R": R,
43 "S": S,
44 "WS": WS,
45
46 "FSI": Control,
47 "PDF": Control,
48 "PDI": Control,
49 "LRE": Control,
50 "LRI": Control,
51 "LRO": Control,
52 "RLE": Control,
53 "RLI": Control,
54 "RLO": Control,
55 }
56
57 func genTables() {
58 if numClass > 0x0F {
59 log.Fatalf("Too many Class constants (%#x > 0x0F).", numClass)
60 }
61 w := gen.NewCodeWriter()
62 defer w.WriteVersionedGoFile(*outputFile, "bidi")
63
64 gen.WriteUnicodeVersion(w)
65
66 t := triegen.NewTrie("bidi")
67
68
69
70 orMask := map[rune]uint64{}
71
72 xorMap := map[rune]int{}
73 xorMasks := []rune{0}
74
75 ucd.Parse(gen.OpenUCDFile("BidiBrackets.txt"), func(p *ucd.Parser) {
76 r1 := p.Rune(0)
77 r2 := p.Rune(1)
78 xor := r1 ^ r2
79 if _, ok := xorMap[xor]; !ok {
80 xorMap[xor] = len(xorMasks)
81 xorMasks = append(xorMasks, xor)
82 }
83 entry := uint64(xorMap[xor]) << xorMaskShift
84 switch p.String(2) {
85 case "o":
86 entry |= openMask
87 case "c", "n":
88 default:
89 log.Fatalf("Unknown bracket class %q.", p.String(2))
90 }
91 orMask[r1] = entry
92 })
93
94 w.WriteComment(`
95 xorMasks contains masks to be xor-ed with brackets to get the reverse
96 version.`)
97 w.WriteVar("xorMasks", xorMasks)
98
99 done := map[rune]bool{}
100
101 insert := func(r rune, c Class) {
102 if !done[r] {
103 t.Insert(r, orMask[r]|uint64(c))
104 done[r] = true
105 }
106 }
107
108
109 ucd.Parse(gen.OpenUCDFile("extracted/DerivedBidiClass.txt"), func(p *ucd.Parser) {
110 r := p.Rune(0)
111 class, ok := bidiClass[p.String(1)]
112 if !ok {
113 log.Fatalf("%U: Unknown BiDi class %q", r, p.String(1))
114 }
115 insert(r, class)
116 })
117 visitDefaults(insert)
118
119
120
121
122 sz, err := t.Gen(w)
123 if err != nil {
124 log.Fatal(err)
125 }
126 w.Size += sz
127 }
128
129
130
131 var (
132 xorMasks []rune
133 )
134
View as plain text