gen.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. // Copyright 2017 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build ignore
  5. package main
  6. import (
  7. "flag"
  8. "log"
  9. "strconv"
  10. "strings"
  11. "golang.org/x/text/internal/cldrtree"
  12. "golang.org/x/text/internal/gen"
  13. "golang.org/x/text/language"
  14. "golang.org/x/text/unicode/cldr"
  15. )
  16. var (
  17. draft = flag.String("draft",
  18. "contributed",
  19. `Minimal draft requirements (approved, contributed, provisional, unconfirmed).`)
  20. )
  21. // TODO:
  22. // - Compile format patterns.
  23. // - Compress the large amount of redundancy in metazones.
  24. // - Split trees (with shared buckets) with data that is enough for default
  25. // formatting of Go Time values and tables that are needed for larger
  26. // variants.
  27. // - zone to metaZone mappings (in supplemental)
  28. // - Add more enum values and also some key maps for some of the elements.
  29. func main() {
  30. gen.Init()
  31. r := gen.OpenCLDRCoreZip()
  32. defer r.Close()
  33. d := &cldr.Decoder{}
  34. d.SetDirFilter("supplemental", "main")
  35. d.SetSectionFilter("dates")
  36. data, err := d.DecodeZip(r)
  37. if err != nil {
  38. log.Fatalf("DecodeZip: %v", err)
  39. }
  40. dates := cldrtree.New("dates")
  41. buildCLDRTree(data, dates)
  42. w := gen.NewCodeWriter()
  43. if err := dates.Gen(w); err != nil {
  44. log.Fatal(err)
  45. }
  46. gen.WriteCLDRVersion(w)
  47. w.WriteGoFile("tables.go", "date")
  48. w = gen.NewCodeWriter()
  49. if err := dates.GenTestData(w); err != nil {
  50. log.Fatal(err)
  51. }
  52. w.WriteGoFile("data_test.go", "date")
  53. }
  54. func buildCLDRTree(data *cldr.CLDR, dates *cldrtree.Builder) {
  55. context := cldrtree.Enum("context")
  56. widthMap := func(s string) string {
  57. // Align era with width values.
  58. if r, ok := map[string]string{
  59. "eraAbbr": "abbreviated",
  60. "eraNarrow": "narrow",
  61. "eraNames": "wide",
  62. }[s]; ok {
  63. s = r
  64. }
  65. // Prefix width to disambiguate with some overlapping length values.
  66. return "width" + strings.Title(s)
  67. }
  68. width := cldrtree.EnumFunc("width", widthMap, "abbreviated", "narrow", "wide")
  69. length := cldrtree.Enum("length", "short", "long")
  70. month := cldrtree.Enum("month", "leap7")
  71. relTime := cldrtree.EnumFunc("relTime", func(s string) string {
  72. x, err := strconv.ParseInt(s, 10, 8)
  73. if err != nil {
  74. log.Fatal("Invalid number:", err)
  75. }
  76. return []string{
  77. "before2",
  78. "before1",
  79. "current",
  80. "after1",
  81. "after2",
  82. "after3",
  83. }[x+2]
  84. })
  85. // Disambiguate keys like 'months' and 'sun'.
  86. cycleType := cldrtree.EnumFunc("cycleType", func(s string) string {
  87. return s + "CycleType"
  88. })
  89. field := cldrtree.EnumFunc("field", func(s string) string {
  90. return s + "Field"
  91. })
  92. timeType := cldrtree.EnumFunc("timeType", func(s string) string {
  93. if s == "" {
  94. return "genericTime"
  95. }
  96. return s + "Time"
  97. }, "generic")
  98. zoneType := []cldrtree.Option{cldrtree.SharedType(), timeType}
  99. metaZoneType := []cldrtree.Option{cldrtree.SharedType(), timeType}
  100. for _, lang := range data.Locales() {
  101. tag := language.Make(lang)
  102. ldml := data.RawLDML(lang)
  103. if ldml.Dates == nil {
  104. continue
  105. }
  106. x := dates.Locale(tag)
  107. if x := x.Index(ldml.Dates.Calendars); x != nil {
  108. for _, cal := range ldml.Dates.Calendars.Calendar {
  109. x := x.IndexFromType(cal)
  110. if x := x.Index(cal.Months); x != nil {
  111. for _, mc := range cal.Months.MonthContext {
  112. x := x.IndexFromType(mc, context)
  113. for _, mw := range mc.MonthWidth {
  114. x := x.IndexFromType(mw, width)
  115. for _, m := range mw.Month {
  116. x.SetValue(m.Yeartype+m.Type, m, month)
  117. }
  118. }
  119. }
  120. }
  121. if x := x.Index(cal.MonthPatterns); x != nil {
  122. for _, mc := range cal.MonthPatterns.MonthPatternContext {
  123. x := x.IndexFromType(mc, context)
  124. for _, mw := range mc.MonthPatternWidth {
  125. // Value is always leap, so no need to create a
  126. // subindex.
  127. for _, m := range mw.MonthPattern {
  128. x.SetValue(mw.Type, m, width)
  129. }
  130. }
  131. }
  132. }
  133. if x := x.Index(cal.CyclicNameSets); x != nil {
  134. for _, cns := range cal.CyclicNameSets.CyclicNameSet {
  135. x := x.IndexFromType(cns, cycleType)
  136. for _, cc := range cns.CyclicNameContext {
  137. x := x.IndexFromType(cc, context)
  138. for _, cw := range cc.CyclicNameWidth {
  139. x := x.IndexFromType(cw, width)
  140. for _, c := range cw.CyclicName {
  141. x.SetValue(c.Type, c)
  142. }
  143. }
  144. }
  145. }
  146. }
  147. if x := x.Index(cal.Days); x != nil {
  148. for _, dc := range cal.Days.DayContext {
  149. x := x.IndexFromType(dc, context)
  150. for _, dw := range dc.DayWidth {
  151. x := x.IndexFromType(dw, width)
  152. for _, d := range dw.Day {
  153. x.SetValue(d.Type, d)
  154. }
  155. }
  156. }
  157. }
  158. if x := x.Index(cal.Quarters); x != nil {
  159. for _, qc := range cal.Quarters.QuarterContext {
  160. x := x.IndexFromType(qc, context)
  161. for _, qw := range qc.QuarterWidth {
  162. x := x.IndexFromType(qw, width)
  163. for _, q := range qw.Quarter {
  164. x.SetValue(q.Type, q)
  165. }
  166. }
  167. }
  168. }
  169. if x := x.Index(cal.DayPeriods); x != nil {
  170. for _, dc := range cal.DayPeriods.DayPeriodContext {
  171. x := x.IndexFromType(dc, context)
  172. for _, dw := range dc.DayPeriodWidth {
  173. x := x.IndexFromType(dw, width)
  174. for _, d := range dw.DayPeriod {
  175. x.IndexFromType(d).SetValue(d.Alt, d)
  176. }
  177. }
  178. }
  179. }
  180. if x := x.Index(cal.Eras); x != nil {
  181. opts := []cldrtree.Option{width, cldrtree.SharedType()}
  182. if x := x.Index(cal.Eras.EraNames, opts...); x != nil {
  183. for _, e := range cal.Eras.EraNames.Era {
  184. x.IndexFromAlt(e).SetValue(e.Type, e)
  185. }
  186. }
  187. if x := x.Index(cal.Eras.EraAbbr, opts...); x != nil {
  188. for _, e := range cal.Eras.EraAbbr.Era {
  189. x.IndexFromAlt(e).SetValue(e.Type, e)
  190. }
  191. }
  192. if x := x.Index(cal.Eras.EraNarrow, opts...); x != nil {
  193. for _, e := range cal.Eras.EraNarrow.Era {
  194. x.IndexFromAlt(e).SetValue(e.Type, e)
  195. }
  196. }
  197. }
  198. if x := x.Index(cal.DateFormats); x != nil {
  199. for _, dfl := range cal.DateFormats.DateFormatLength {
  200. x := x.IndexFromType(dfl, length)
  201. for _, df := range dfl.DateFormat {
  202. for _, p := range df.Pattern {
  203. x.SetValue(p.Alt, p)
  204. }
  205. }
  206. }
  207. }
  208. if x := x.Index(cal.TimeFormats); x != nil {
  209. for _, tfl := range cal.TimeFormats.TimeFormatLength {
  210. x := x.IndexFromType(tfl, length)
  211. for _, tf := range tfl.TimeFormat {
  212. for _, p := range tf.Pattern {
  213. x.SetValue(p.Alt, p)
  214. }
  215. }
  216. }
  217. }
  218. if x := x.Index(cal.DateTimeFormats); x != nil {
  219. for _, dtfl := range cal.DateTimeFormats.DateTimeFormatLength {
  220. x := x.IndexFromType(dtfl, length)
  221. for _, dtf := range dtfl.DateTimeFormat {
  222. for _, p := range dtf.Pattern {
  223. x.SetValue(p.Alt, p)
  224. }
  225. }
  226. }
  227. // TODO:
  228. // - appendItems
  229. // - intervalFormats
  230. }
  231. }
  232. }
  233. // TODO: this is a lot of data and is probably relatively little used.
  234. // Store this somewhere else.
  235. if x := x.Index(ldml.Dates.Fields); x != nil {
  236. for _, f := range ldml.Dates.Fields.Field {
  237. x := x.IndexFromType(f, field)
  238. for _, d := range f.DisplayName {
  239. x.Index(d).SetValue(d.Alt, d)
  240. }
  241. for _, r := range f.Relative {
  242. x.Index(r).SetValue(r.Type, r, relTime)
  243. }
  244. for _, rt := range f.RelativeTime {
  245. x := x.Index(rt).IndexFromType(rt)
  246. for _, p := range rt.RelativeTimePattern {
  247. x.SetValue(p.Count, p)
  248. }
  249. }
  250. for _, rp := range f.RelativePeriod {
  251. x.Index(rp).SetValue(rp.Alt, rp)
  252. }
  253. }
  254. }
  255. if x := x.Index(ldml.Dates.TimeZoneNames); x != nil {
  256. format := x.IndexWithName("zoneFormat")
  257. for _, h := range ldml.Dates.TimeZoneNames.HourFormat {
  258. format.SetValue(h.Element(), h)
  259. }
  260. for _, g := range ldml.Dates.TimeZoneNames.GmtFormat {
  261. format.SetValue(g.Element(), g)
  262. }
  263. for _, g := range ldml.Dates.TimeZoneNames.GmtZeroFormat {
  264. format.SetValue(g.Element(), g)
  265. }
  266. for _, r := range ldml.Dates.TimeZoneNames.RegionFormat {
  267. x.Index(r).SetValue(r.Type, r, timeType)
  268. }
  269. set := func(x *cldrtree.Index, e []*cldr.Common, zone string) {
  270. for _, n := range e {
  271. x.Index(n, zoneType...).SetValue(zone, n)
  272. }
  273. }
  274. zoneWidth := []cldrtree.Option{length, cldrtree.SharedType()}
  275. zs := x.IndexWithName("zone")
  276. for _, z := range ldml.Dates.TimeZoneNames.Zone {
  277. for _, l := range z.Long {
  278. x := zs.Index(l, zoneWidth...)
  279. set(x, l.Generic, z.Type)
  280. set(x, l.Standard, z.Type)
  281. set(x, l.Daylight, z.Type)
  282. }
  283. for _, s := range z.Short {
  284. x := zs.Index(s, zoneWidth...)
  285. set(x, s.Generic, z.Type)
  286. set(x, s.Standard, z.Type)
  287. set(x, s.Daylight, z.Type)
  288. }
  289. }
  290. set = func(x *cldrtree.Index, e []*cldr.Common, zone string) {
  291. for _, n := range e {
  292. x.Index(n, metaZoneType...).SetValue(zone, n)
  293. }
  294. }
  295. zoneWidth = []cldrtree.Option{length, cldrtree.SharedType()}
  296. zs = x.IndexWithName("metaZone")
  297. for _, z := range ldml.Dates.TimeZoneNames.Metazone {
  298. for _, l := range z.Long {
  299. x := zs.Index(l, zoneWidth...)
  300. set(x, l.Generic, z.Type)
  301. set(x, l.Standard, z.Type)
  302. set(x, l.Daylight, z.Type)
  303. }
  304. for _, s := range z.Short {
  305. x := zs.Index(s, zoneWidth...)
  306. set(x, s.Generic, z.Type)
  307. set(x, s.Standard, z.Type)
  308. set(x, s.Daylight, z.Type)
  309. }
  310. }
  311. }
  312. }
  313. }