makeucnid.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /* Make ucnid.h from various sources.
  2. Copyright (C) 2005-2022 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify it
  4. under the terms of the GNU General Public License as published by the
  5. Free Software Foundation; either version 3, or (at your option) any
  6. later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; see the file COPYING3. If not see
  13. <http://www.gnu.org/licenses/>. */
  14. /* Run this program as
  15. ./makeucnid ucnid.tab UnicodeData.txt DerivedNormalizationProps.txt \
  16. DerivedCoreProperties.txt > ucnid.h
  17. */
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <ctype.h>
  21. #include <stdbool.h>
  22. #include <stdlib.h>
  23. enum {
  24. C99 = 1,
  25. CXX = 2,
  26. N99 = 4,
  27. C11 = 8,
  28. N11 = 16,
  29. CXX23 = 32,
  30. NXX23 = 64,
  31. all_languages = C99 | CXX | C11 | CXX23 | NXX23,
  32. not_NFC = 128,
  33. not_NFKC = 256,
  34. maybe_not_NFC = 512
  35. };
  36. #define NUM_CODE_POINTS 0x110000
  37. #define MAX_CODE_POINT 0x10ffff
  38. static unsigned flags[NUM_CODE_POINTS];
  39. static unsigned int all_decomp[NUM_CODE_POINTS][2];
  40. static unsigned int decomp[NUM_CODE_POINTS][2];
  41. static unsigned char combining_value[NUM_CODE_POINTS];
  42. /* Die! */
  43. static void
  44. fail (const char *s)
  45. {
  46. fprintf (stderr, "%s\n", s);
  47. exit (1);
  48. }
  49. /* Read ucnid.tab and set the flags for language versions in header[]. */
  50. static void
  51. read_ucnid (const char *fname)
  52. {
  53. FILE *f = fopen (fname, "r");
  54. unsigned fl = 0;
  55. if (!f)
  56. fail ("opening ucnid.tab");
  57. for (;;)
  58. {
  59. char line[256];
  60. if (!fgets (line, sizeof (line), f))
  61. break;
  62. if (strcmp (line, "[C99]\n") == 0)
  63. fl = C99;
  64. else if (strcmp (line, "[C99DIG]\n") == 0)
  65. fl = C99|N99;
  66. else if (strcmp (line, "[CXX]\n") == 0)
  67. fl = CXX;
  68. else if (strcmp (line, "[C11]\n") == 0)
  69. fl = C11;
  70. else if (strcmp (line, "[C11NOSTART]\n") == 0)
  71. fl = C11|N11;
  72. else if (isxdigit (line[0]))
  73. {
  74. char *l = line;
  75. while (*l)
  76. {
  77. unsigned long start, end;
  78. char *endptr;
  79. start = strtoul (l, &endptr, 16);
  80. if (endptr == l || (*endptr != '-' && ! isspace (*endptr)))
  81. fail ("parsing ucnid.tab [1]");
  82. l = endptr;
  83. if (*l != '-')
  84. end = start;
  85. else
  86. {
  87. end = strtoul (l + 1, &endptr, 16);
  88. if (end < start)
  89. fail ("parsing ucnid.tab, end before start");
  90. l = endptr;
  91. if (! isspace (*l))
  92. fail ("parsing ucnid.tab, junk after range");
  93. }
  94. while (isspace (*l))
  95. l++;
  96. if (end > MAX_CODE_POINT)
  97. fail ("parsing ucnid.tab, end too large");
  98. while (start <= end)
  99. flags[start++] |= fl;
  100. }
  101. }
  102. }
  103. if (ferror (f))
  104. fail ("reading ucnid.tab");
  105. fclose (f);
  106. }
  107. /* Read UnicodeData.txt and fill in the 'decomp' table to be the
  108. decompositions of characters for which both the character
  109. decomposed and all the code points in the decomposition are valid
  110. for some supported language version, and the 'all_decomp' table to
  111. be the decompositions of all characters without those
  112. constraints. */
  113. static void
  114. read_table (char *fname)
  115. {
  116. FILE * f = fopen (fname, "r");
  117. if (!f)
  118. fail ("opening UnicodeData.txt");
  119. for (;;)
  120. {
  121. char line[256];
  122. unsigned long codepoint, this_decomp[4];
  123. char *l;
  124. int i, j;
  125. int decomp_useful;
  126. if (!fgets (line, sizeof (line), f))
  127. break;
  128. codepoint = strtoul (line, &l, 16);
  129. if (l == line || *l != ';')
  130. fail ("parsing UnicodeData.txt, reading code point");
  131. if (codepoint > MAX_CODE_POINT)
  132. fail ("parsing UnicodeData.txt, code point too large");
  133. do {
  134. l++;
  135. } while (*l != ';');
  136. /* Category value. */
  137. do {
  138. l++;
  139. } while (*l != ';');
  140. /* Canonical combining class; in NFC/NFKC, they must be increasing
  141. (or zero). */
  142. if (! isdigit (*++l))
  143. fail ("parsing UnicodeData.txt, combining class not number");
  144. combining_value[codepoint] = strtoul (l, &l, 10);
  145. if (*l++ != ';')
  146. fail ("parsing UnicodeData.txt, junk after combining class");
  147. /* Skip over bidi value. */
  148. do {
  149. l++;
  150. } while (*l != ';');
  151. /* Decomposition mapping. */
  152. decomp_useful = flags[codepoint];
  153. if (*++l == '<') /* Compatibility mapping. */
  154. continue;
  155. for (i = 0; i < 4; i++)
  156. {
  157. if (*l == ';')
  158. break;
  159. if (!isxdigit (*l))
  160. fail ("parsing UnicodeData.txt, decomposition format");
  161. this_decomp[i] = strtoul (l, &l, 16);
  162. decomp_useful &= flags[this_decomp[i]];
  163. while (isspace (*l))
  164. l++;
  165. }
  166. if (i > 2) /* Decomposition too long. */
  167. fail ("parsing UnicodeData.txt, decomposition too long");
  168. for (j = 0; j < i; j++)
  169. all_decomp[codepoint][j] = this_decomp[j];
  170. if ((flags[codepoint] & all_languages) && decomp_useful)
  171. while (--i >= 0)
  172. decomp[codepoint][i] = this_decomp[i];
  173. }
  174. if (ferror (f))
  175. fail ("reading UnicodeData.txt");
  176. fclose (f);
  177. }
  178. /* Read DerivedNormalizationProps.txt and set the flags that say whether
  179. a character is in NFC, NFKC, or is context-dependent. */
  180. static void
  181. read_derived (const char *fname)
  182. {
  183. FILE * f = fopen (fname, "r");
  184. if (!f)
  185. fail ("opening DerivedNormalizationProps.txt");
  186. for (;;)
  187. {
  188. char line[256];
  189. unsigned long start, end;
  190. char *l;
  191. bool not_NFC_p, not_NFKC_p, maybe_not_NFC_p;
  192. if (!fgets (line, sizeof (line), f))
  193. break;
  194. not_NFC_p = (strstr (line, "; NFC_QC; N") != NULL);
  195. not_NFKC_p = (strstr (line, "; NFKC_QC; N") != NULL);
  196. maybe_not_NFC_p = (strstr (line, "; NFC_QC; M") != NULL);
  197. if (! not_NFC_p && ! not_NFKC_p && ! maybe_not_NFC_p)
  198. continue;
  199. start = strtoul (line, &l, 16);
  200. if (l == line)
  201. fail ("parsing DerivedNormalizationProps.txt, reading start");
  202. if (start > MAX_CODE_POINT)
  203. fail ("parsing DerivedNormalizationProps.txt, code point too large");
  204. if (*l == '.' && l[1] == '.')
  205. end = strtoul (l + 2, &l, 16);
  206. else
  207. end = start;
  208. while (start <= end)
  209. flags[start++] |= ((not_NFC_p ? not_NFC : 0)
  210. | (not_NFKC_p ? not_NFKC : 0)
  211. | (maybe_not_NFC_p ? maybe_not_NFC : 0)
  212. );
  213. }
  214. if (ferror (f))
  215. fail ("reading DerivedNormalizationProps.txt");
  216. fclose (f);
  217. }
  218. /* Read DerivedCoreProperties.txt and fill in languages version in
  219. flags from the XID_Start and XID_Continue properties. */
  220. static void
  221. read_derivedcore (char *fname)
  222. {
  223. FILE * f = fopen (fname, "r");
  224. if (!f)
  225. fail ("opening DerivedCoreProperties.txt");
  226. for (;;)
  227. {
  228. char line[256];
  229. unsigned long codepoint_start, codepoint_end;
  230. char *l;
  231. int i, j;
  232. if (!fgets (line, sizeof (line), f))
  233. break;
  234. if (line[0] == '#' || line[0] == '\n' || line[0] == '\r')
  235. continue;
  236. codepoint_start = strtoul (line, &l, 16);
  237. if (l == line)
  238. fail ("parsing DerivedCoreProperties.txt, reading code point");
  239. if (codepoint_start > MAX_CODE_POINT)
  240. fail ("parsing DerivedCoreProperties.txt, code point too large");
  241. if (*l == '.' && l[1] == '.')
  242. {
  243. char *l2 = l + 2;
  244. codepoint_end = strtoul (l + 2, &l, 16);
  245. if (l == l2 || codepoint_end < codepoint_start)
  246. fail ("parsing DerivedCoreProperties.txt, reading code point");
  247. if (codepoint_end > MAX_CODE_POINT)
  248. fail ("parsing DerivedCoreProperties.txt, code point too large");
  249. }
  250. else
  251. codepoint_end = codepoint_start;
  252. while (*l == ' ')
  253. l++;
  254. if (*l++ != ';')
  255. fail ("parsing DerivedCoreProperties.txt, reading code point");
  256. while (*l == ' ')
  257. l++;
  258. if (codepoint_end < 0x80)
  259. continue;
  260. if (strncmp (l, "XID_Start ", 10) == 0)
  261. {
  262. for (; codepoint_start <= codepoint_end; codepoint_start++)
  263. flags[codepoint_start]
  264. = (flags[codepoint_start] | CXX23) & ~NXX23;
  265. }
  266. else if (strncmp (l, "XID_Continue ", 13) == 0)
  267. {
  268. for (; codepoint_start <= codepoint_end; codepoint_start++)
  269. if ((flags[codepoint_start] & CXX23) == 0)
  270. flags[codepoint_start] |= CXX23 | NXX23;
  271. }
  272. }
  273. if (ferror (f))
  274. fail ("reading DerivedCoreProperties.txt");
  275. fclose (f);
  276. }
  277. /* Write out the table.
  278. The table consists of two words per entry. The first word is the flags
  279. for the unicode code points up to and including the second word. */
  280. static void
  281. write_table (void)
  282. {
  283. unsigned i;
  284. unsigned last_flag = flags[0];
  285. bool really_safe = decomp[0][0] == 0;
  286. unsigned char last_combine = combining_value[0];
  287. printf ("static const struct ucnrange ucnranges[] = {\n");
  288. for (i = 1; i <= NUM_CODE_POINTS; i++)
  289. if (i == NUM_CODE_POINTS
  290. || (flags[i] != last_flag && ((flags[i] | last_flag) & all_languages))
  291. || really_safe != (decomp[i][0] == 0)
  292. || combining_value[i] != last_combine)
  293. {
  294. printf ("{ %s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s, %3d, %#06x },\n",
  295. last_flag & C99 ? "C99" : " 0",
  296. last_flag & N99 ? "N99" : " 0",
  297. last_flag & CXX ? "CXX" : " 0",
  298. last_flag & C11 ? "C11" : " 0",
  299. last_flag & N11 ? "N11" : " 0",
  300. last_flag & CXX23 ? "CXX23" : " 0",
  301. last_flag & NXX23 ? "NXX23" : " 0",
  302. really_safe ? "CID" : " 0",
  303. last_flag & not_NFC ? " 0" : "NFC",
  304. last_flag & not_NFKC ? " 0" : "NKC",
  305. last_flag & maybe_not_NFC ? "CTX" : " 0",
  306. combining_value[i - 1],
  307. i - 1);
  308. last_flag = flags[i];
  309. last_combine = combining_value[i];
  310. really_safe = decomp[i][0] == 0;
  311. }
  312. printf ("};\n");
  313. }
  314. /* Return whether a given character is valid in an identifier for some
  315. supported language, either as itself or as a UCN. */
  316. static bool
  317. char_id_valid (unsigned int c)
  318. {
  319. return ((flags[c] & all_languages)
  320. || (c == 0x24)
  321. || (c >= 0x30 && c <= 0x39)
  322. || (c >= 0x41 && c <= 0x5a)
  323. || (c >= 0x61 && c <= 0x7a));
  324. }
  325. /* Write out the switch statement over characters for which it is
  326. context-dependent whether they are in NFC. */
  327. static void
  328. write_context_switch (void)
  329. {
  330. unsigned i;
  331. printf ("static bool\n"
  332. "check_nfc (cpp_reader *pfile, cppchar_t c, cppchar_t p)\n"
  333. "{\n"
  334. " switch (c)\n"
  335. " {\n");
  336. for (i = 0; i < NUM_CODE_POINTS; i++)
  337. {
  338. bool found_case = false;
  339. unsigned j;
  340. if (!(flags[i] & all_languages) || !(flags[i] & maybe_not_NFC))
  341. continue;
  342. if ((i >= 0x1161 && i <= 0x1175) || (i >= 0x11A8 && i <= 0x11C2))
  343. continue; /* Hangul handled algorithmically. */
  344. printf (" case %#06x:\n"
  345. " switch (p)\n"
  346. "\t{\n", i);
  347. /* If an NFC starter character decomposes with this character I
  348. as the second character and an NFC starter character S as the
  349. first character, that latter character as a previous
  350. character means this character is not NFC. Furthermore, any
  351. NFC starter character K made by a series of compositions of S
  352. with combining characters whose combining class is greater
  353. than that of I also means this character is not NFC. */
  354. for (j = 0; j < NUM_CODE_POINTS; j++)
  355. {
  356. unsigned s, k;
  357. if (all_decomp[j][1] != i)
  358. continue;
  359. s = all_decomp[j][0];
  360. if (combining_value[s] != 0 || (flags[s] & not_NFC) != 0)
  361. continue;
  362. if (char_id_valid (s))
  363. {
  364. found_case = true;
  365. printf ("\tcase %#06x:\n", s);
  366. }
  367. for (k = 0; k < NUM_CODE_POINTS; k++)
  368. {
  369. unsigned t = k;
  370. if (k == s || !char_id_valid (k))
  371. continue;
  372. while (all_decomp[t][1] != 0
  373. && combining_value[all_decomp[t][1]] > combining_value[i])
  374. {
  375. if (combining_value[t] != 0 || (flags[t] & not_NFC) != 0)
  376. break;
  377. t = all_decomp[t][0];
  378. }
  379. if (t == s)
  380. {
  381. found_case = true;
  382. printf ("\tcase %#06x:\n", k);
  383. }
  384. }
  385. }
  386. if (found_case)
  387. printf ("\t return false;\n");
  388. else
  389. printf ("\t/* Non-NFC cases not applicable to C/C++. */\n");
  390. printf ("\tdefault:\n"
  391. "\t return true;\n"
  392. "\t}\n\n");
  393. }
  394. printf (" default:\n"
  395. " cpp_error (pfile, CPP_DL_ICE, \"Character %%x might not be NFKC\", c);\n"
  396. " return true;\n"
  397. " }\n"
  398. "}\n");
  399. }
  400. /* Print out the huge copyright notice. */
  401. static void
  402. write_copyright (void)
  403. {
  404. static const char copyright[] = "\
  405. /* Unicode characters and various properties.\n\
  406. Copyright (C) 2003-2022 Free Software Foundation, Inc.\n\
  407. \n\
  408. This program is free software; you can redistribute it and/or modify it\n\
  409. under the terms of the GNU General Public License as published by the\n\
  410. Free Software Foundation; either version 3, or (at your option) any\n\
  411. later version.\n\
  412. \n\
  413. This program is distributed in the hope that it will be useful,\n\
  414. but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
  415. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\
  416. GNU General Public License for more details.\n\
  417. \n\
  418. You should have received a copy of the GNU General Public License\n\
  419. along with this program; see the file COPYING3. If not see\n\
  420. <http://www.gnu.org/licenses/>.\n\
  421. \n\
  422. \n\
  423. Copyright (C) 1991-2005 Unicode, Inc. All rights reserved.\n\
  424. Distributed under the Terms of Use in\n\
  425. http://www.unicode.org/copyright.html.\n\
  426. \n\
  427. Permission is hereby granted, free of charge, to any person\n\
  428. obtaining a copy of the Unicode data files and any associated\n\
  429. documentation (the \"Data Files\") or Unicode software and any\n\
  430. associated documentation (the \"Software\") to deal in the Data Files\n\
  431. or Software without restriction, including without limitation the\n\
  432. rights to use, copy, modify, merge, publish, distribute, and/or\n\
  433. sell copies of the Data Files or Software, and to permit persons to\n\
  434. whom the Data Files or Software are furnished to do so, provided\n\
  435. that (a) the above copyright notice(s) and this permission notice\n\
  436. appear with all copies of the Data Files or Software, (b) both the\n\
  437. above copyright notice(s) and this permission notice appear in\n\
  438. associated documentation, and (c) there is clear notice in each\n\
  439. modified Data File or in the Software as well as in the\n\
  440. documentation associated with the Data File(s) or Software that the\n\
  441. data or software has been modified.\n\
  442. \n\
  443. THE DATA FILES AND SOFTWARE ARE PROVIDED \"AS IS\", WITHOUT WARRANTY\n\
  444. OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE\n\
  445. WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\n\
  446. NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE\n\
  447. COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR\n\
  448. ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY\n\
  449. DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,\n\
  450. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS\n\
  451. ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE\n\
  452. OF THE DATA FILES OR SOFTWARE.\n\
  453. \n\
  454. Except as contained in this notice, the name of a copyright holder\n\
  455. shall not be used in advertising or otherwise to promote the sale,\n\
  456. use or other dealings in these Data Files or Software without prior\n\
  457. written authorization of the copyright holder. */\n";
  458. puts (copyright);
  459. }
  460. /* Main program. */
  461. int
  462. main(int argc, char ** argv)
  463. {
  464. if (argc != 5)
  465. fail ("too few arguments to makeucn");
  466. read_ucnid (argv[1]);
  467. read_table (argv[2]);
  468. read_derived (argv[3]);
  469. read_derivedcore (argv[4]);
  470. write_copyright ();
  471. write_table ();
  472. write_context_switch ();
  473. return 0;
  474. }