You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

311 lines
8.5 KiB

  1. // Copyright 2015 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. // Unicode table generator.
  5. // Data read from the web.
  6. // +build ignore
  7. package main
  8. import (
  9. "flag"
  10. "log"
  11. "unicode"
  12. "unicode/utf8"
  13. "golang.org/x/text/internal/gen"
  14. "golang.org/x/text/internal/triegen"
  15. "golang.org/x/text/internal/ucd"
  16. "golang.org/x/text/unicode/norm"
  17. "golang.org/x/text/unicode/rangetable"
  18. )
  19. var outputFile = flag.String("output", "tables.go", "output file for generated tables; default tables.go")
  20. var assigned, disallowedRunes *unicode.RangeTable
  21. var runeCategory = map[rune]category{}
  22. var overrides = map[category]category{
  23. viramaModifier: viramaJoinT,
  24. greek: greekJoinT,
  25. hebrew: hebrewJoinT,
  26. }
  27. func setCategory(r rune, cat category) {
  28. if c, ok := runeCategory[r]; ok {
  29. if override, ok := overrides[c]; cat == joiningT && ok {
  30. cat = override
  31. } else {
  32. log.Fatalf("%U: multiple categories for rune (%v and %v)", r, c, cat)
  33. }
  34. }
  35. runeCategory[r] = cat
  36. }
  37. func init() {
  38. if numCategories > 1<<propShift {
  39. log.Fatalf("Number of categories is %d; may at most be %d", numCategories, 1<<propShift)
  40. }
  41. }
  42. func main() {
  43. gen.Init()
  44. // Load data
  45. runes := []rune{}
  46. // PrecisIgnorableProperties: https://tools.ietf.org/html/rfc7564#section-9.13
  47. ucd.Parse(gen.OpenUCDFile("DerivedCoreProperties.txt"), func(p *ucd.Parser) {
  48. if p.String(1) == "Default_Ignorable_Code_Point" {
  49. runes = append(runes, p.Rune(0))
  50. }
  51. })
  52. ucd.Parse(gen.OpenUCDFile("PropList.txt"), func(p *ucd.Parser) {
  53. switch p.String(1) {
  54. case "Noncharacter_Code_Point":
  55. runes = append(runes, p.Rune(0))
  56. }
  57. })
  58. // OldHangulJamo: https://tools.ietf.org/html/rfc5892#section-2.9
  59. ucd.Parse(gen.OpenUCDFile("HangulSyllableType.txt"), func(p *ucd.Parser) {
  60. switch p.String(1) {
  61. case "L", "V", "T":
  62. runes = append(runes, p.Rune(0))
  63. }
  64. })
  65. disallowedRunes = rangetable.New(runes...)
  66. assigned = rangetable.Assigned(unicode.Version)
  67. // Load category data.
  68. runeCategory['l'] = latinSmallL
  69. ucd.Parse(gen.OpenUCDFile("UnicodeData.txt"), func(p *ucd.Parser) {
  70. const cccVirama = 9
  71. if p.Int(ucd.CanonicalCombiningClass) == cccVirama {
  72. setCategory(p.Rune(0), viramaModifier)
  73. }
  74. })
  75. ucd.Parse(gen.OpenUCDFile("Scripts.txt"), func(p *ucd.Parser) {
  76. switch p.String(1) {
  77. case "Greek":
  78. setCategory(p.Rune(0), greek)
  79. case "Hebrew":
  80. setCategory(p.Rune(0), hebrew)
  81. case "Hiragana", "Katakana", "Han":
  82. setCategory(p.Rune(0), japanese)
  83. }
  84. })
  85. // Set the rule categories associated with exceptions. This overrides any
  86. // previously set categories. The original categories are manually
  87. // reintroduced in the categoryTransitions table.
  88. for r, e := range exceptions {
  89. if e.cat != 0 {
  90. runeCategory[r] = e.cat
  91. }
  92. }
  93. cat := map[string]category{
  94. "L": joiningL,
  95. "D": joiningD,
  96. "T": joiningT,
  97. "R": joiningR,
  98. }
  99. ucd.Parse(gen.OpenUCDFile("extracted/DerivedJoiningType.txt"), func(p *ucd.Parser) {
  100. switch v := p.String(1); v {
  101. case "L", "D", "T", "R":
  102. setCategory(p.Rune(0), cat[v])
  103. }
  104. })
  105. writeTables()
  106. gen.Repackage("gen_trieval.go", "trieval.go", "precis")
  107. }
  108. type exception struct {
  109. prop property
  110. cat category
  111. }
  112. func init() {
  113. // Programmatically add the Arabic and Indic digits to the exceptions map.
  114. // See comment in the exceptions map below why these are marked disallowed.
  115. for i := rune(0); i <= 9; i++ {
  116. exceptions[0x0660+i] = exception{
  117. prop: disallowed,
  118. cat: arabicIndicDigit,
  119. }
  120. exceptions[0x06F0+i] = exception{
  121. prop: disallowed,
  122. cat: extendedArabicIndicDigit,
  123. }
  124. }
  125. }
  126. // The Exceptions class as defined in RFC 5892
  127. // https://tools.ietf.org/html/rfc5892#section-2.6
  128. var exceptions = map[rune]exception{
  129. 0x00DF: {prop: pValid},
  130. 0x03C2: {prop: pValid},
  131. 0x06FD: {prop: pValid},
  132. 0x06FE: {prop: pValid},
  133. 0x0F0B: {prop: pValid},
  134. 0x3007: {prop: pValid},
  135. // ContextO|J rules are marked as disallowed, taking a "guilty until proven
  136. // innocent" approach. The main reason for this is that the check for
  137. // whether a context rule should be applied can be moved to the logic for
  138. // handing disallowed runes, taken it off the common path. The exception to
  139. // this rule is for katakanaMiddleDot, as the rule logic is handled without
  140. // using a rule function.
  141. // ContextJ (Join control)
  142. 0x200C: {prop: disallowed, cat: zeroWidthNonJoiner},
  143. 0x200D: {prop: disallowed, cat: zeroWidthJoiner},
  144. // ContextO
  145. 0x00B7: {prop: disallowed, cat: middleDot},
  146. 0x0375: {prop: disallowed, cat: greekLowerNumeralSign},
  147. 0x05F3: {prop: disallowed, cat: hebrewPreceding}, // punctuation Geresh
  148. 0x05F4: {prop: disallowed, cat: hebrewPreceding}, // punctuation Gershayim
  149. 0x30FB: {prop: pValid, cat: katakanaMiddleDot},
  150. // These are officially ContextO, but the implementation does not require
  151. // special treatment of these, so we simply mark them as valid.
  152. 0x0660: {prop: pValid},
  153. 0x0661: {prop: pValid},
  154. 0x0662: {prop: pValid},
  155. 0x0663: {prop: pValid},
  156. 0x0664: {prop: pValid},
  157. 0x0665: {prop: pValid},
  158. 0x0666: {prop: pValid},
  159. 0x0667: {prop: pValid},
  160. 0x0668: {prop: pValid},
  161. 0x0669: {prop: pValid},
  162. 0x06F0: {prop: pValid},
  163. 0x06F1: {prop: pValid},
  164. 0x06F2: {prop: pValid},
  165. 0x06F3: {prop: pValid},
  166. 0x06F4: {prop: pValid},
  167. 0x06F5: {prop: pValid},
  168. 0x06F6: {prop: pValid},
  169. 0x06F7: {prop: pValid},
  170. 0x06F8: {prop: pValid},
  171. 0x06F9: {prop: pValid},
  172. 0x0640: {prop: disallowed},
  173. 0x07FA: {prop: disallowed},
  174. 0x302E: {prop: disallowed},
  175. 0x302F: {prop: disallowed},
  176. 0x3031: {prop: disallowed},
  177. 0x3032: {prop: disallowed},
  178. 0x3033: {prop: disallowed},
  179. 0x3034: {prop: disallowed},
  180. 0x3035: {prop: disallowed},
  181. 0x303B: {prop: disallowed},
  182. }
  183. // LetterDigits: https://tools.ietf.org/html/rfc5892#section-2.1
  184. // r in {Ll, Lu, Lo, Nd, Lm, Mn, Mc}.
  185. func isLetterDigits(r rune) bool {
  186. return unicode.In(r,
  187. unicode.Ll, unicode.Lu, unicode.Lm, unicode.Lo, // Letters
  188. unicode.Mn, unicode.Mc, // Modifiers
  189. unicode.Nd, // Digits
  190. )
  191. }
  192. func isIdDisAndFreePVal(r rune) bool {
  193. return unicode.In(r,
  194. // OtherLetterDigits: https://tools.ietf.org/html/rfc7564#section-9.18
  195. // r in in {Lt, Nl, No, Me}
  196. unicode.Lt, unicode.Nl, unicode.No, // Other letters / numbers
  197. unicode.Me, // Modifiers
  198. // Spaces: https://tools.ietf.org/html/rfc7564#section-9.14
  199. // r in in {Zs}
  200. unicode.Zs,
  201. // Symbols: https://tools.ietf.org/html/rfc7564#section-9.15
  202. // r in {Sm, Sc, Sk, So}
  203. unicode.Sm, unicode.Sc, unicode.Sk, unicode.So,
  204. // Punctuation: https://tools.ietf.org/html/rfc7564#section-9.16
  205. // r in {Pc, Pd, Ps, Pe, Pi, Pf, Po}
  206. unicode.Pc, unicode.Pd, unicode.Ps, unicode.Pe,
  207. unicode.Pi, unicode.Pf, unicode.Po,
  208. )
  209. }
  210. // HasCompat: https://tools.ietf.org/html/rfc7564#section-9.17
  211. func hasCompat(r rune) bool {
  212. return !norm.NFKC.IsNormalString(string(r))
  213. }
  214. // From https://tools.ietf.org/html/rfc5892:
  215. //
  216. // If .cp. .in. Exceptions Then Exceptions(cp);
  217. // Else If .cp. .in. BackwardCompatible Then BackwardCompatible(cp);
  218. // Else If .cp. .in. Unassigned Then UNASSIGNED;
  219. // Else If .cp. .in. ASCII7 Then PVALID;
  220. // Else If .cp. .in. JoinControl Then CONTEXTJ;
  221. // Else If .cp. .in. OldHangulJamo Then DISALLOWED;
  222. // Else If .cp. .in. PrecisIgnorableProperties Then DISALLOWED;
  223. // Else If .cp. .in. Controls Then DISALLOWED;
  224. // Else If .cp. .in. HasCompat Then ID_DIS or FREE_PVAL;
  225. // Else If .cp. .in. LetterDigits Then PVALID;
  226. // Else If .cp. .in. OtherLetterDigits Then ID_DIS or FREE_PVAL;
  227. // Else If .cp. .in. Spaces Then ID_DIS or FREE_PVAL;
  228. // Else If .cp. .in. Symbols Then ID_DIS or FREE_PVAL;
  229. // Else If .cp. .in. Punctuation Then ID_DIS or FREE_PVAL;
  230. // Else DISALLOWED;
  231. func writeTables() {
  232. propTrie := triegen.NewTrie("derivedProperties")
  233. w := gen.NewCodeWriter()
  234. defer w.WriteVersionedGoFile(*outputFile, "precis")
  235. gen.WriteUnicodeVersion(w)
  236. // Iterate over all the runes...
  237. for i := rune(0); i < unicode.MaxRune; i++ {
  238. r := rune(i)
  239. if !utf8.ValidRune(r) {
  240. continue
  241. }
  242. e, ok := exceptions[i]
  243. p := e.prop
  244. switch {
  245. case ok:
  246. case !unicode.In(r, assigned):
  247. p = unassigned
  248. case r >= 0x0021 && r <= 0x007e: // Is ASCII 7
  249. p = pValid
  250. case unicode.In(r, disallowedRunes, unicode.Cc):
  251. p = disallowed
  252. case hasCompat(r):
  253. p = idDisOrFreePVal
  254. case isLetterDigits(r):
  255. p = pValid
  256. case isIdDisAndFreePVal(r):
  257. p = idDisOrFreePVal
  258. default:
  259. p = disallowed
  260. }
  261. cat := runeCategory[r]
  262. // Don't set category for runes that are disallowed.
  263. if p == disallowed {
  264. cat = exceptions[r].cat
  265. }
  266. propTrie.Insert(r, uint64(p)|uint64(cat))
  267. }
  268. sz, err := propTrie.Gen(w)
  269. if err != nil {
  270. log.Fatal(err)
  271. }
  272. w.Size += sz
  273. }