StandardChars.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "ParserPch.h"
  6. namespace UnifiedRegex
  7. {
  8. // ----------------------------------------------------------------------
  9. // ASCIIChars
  10. // ----------------------------------------------------------------------
  11. /*
  12. To get these two tables run:
  13. ch.exe ascii.js
  14. where ascii.js is:
  15. ----------------------------------------------------------------------
  16. function echo(s) { WScript.Echo(s); }
  17. var NumChars = 1 << 8;
  18. var Word = 1 << 0;
  19. var Newline = 1 << 1;
  20. var Whitespace = 1 << 2;
  21. var Letter = 1 << 3;
  22. var Digit = 1 << 4;
  23. var Octal = 1 << 5;
  24. var Hex = 1 << 6;
  25. var classes = [];
  26. var values = [];
  27. function cc(s) {
  28. return s.charCodeAt(0);
  29. }
  30. var c;
  31. for (c = 0; c < NumChars; c++)
  32. {
  33. classes[c] = 0;
  34. values[c] = 0;
  35. }
  36. for (c = cc('0'); c <= cc('7'); c++)
  37. {
  38. classes[c] |= Word | Octal | Digit | Hex;
  39. values[c] = c - cc('0');
  40. }
  41. for (c = cc('8'); c <= cc('9'); c++)
  42. {
  43. classes[c] |= Word | Digit | Hex;
  44. values[c] = c - cc('0');
  45. }
  46. for (c = cc('a'); c <= cc('f'); c++)
  47. {
  48. classes[c] |= Word | Hex | Letter;
  49. values[c] = 10 + c - cc('a');
  50. }
  51. for (c = cc('g'); c <= cc('z'); c++)
  52. classes[c] |= Word | Letter;
  53. for (c = cc('A'); c <= cc('F'); c++)
  54. {
  55. classes[c] |= Word | Hex | Letter;
  56. values[c] = 10 + c - cc('A');
  57. }
  58. for (c = cc('G'); c <= cc('Z'); c++)
  59. classes[c] |= Word | Letter;
  60. classes[cc('_')] |= Word;
  61. classes[cc('\n')] |= Newline;
  62. classes[cc('\r')] |= Newline;
  63. for (c = cc('\t'); c <= cc('\r'); c++)
  64. classes[c] |= Whitespace;
  65. classes[cc(' ')] |= Whitespace;
  66. classes[cc('\x85')] |= Whitespace;
  67. classes[cc('\xa0')] |= Whitespace;
  68. hex = "0123456789abcdef";
  69. function toHex(n) {
  70. return "0x" + hex[n >> 4] + hex[n & 0xf];
  71. }
  72. function dump(a) {
  73. for (c = 0; c < NumChars; c++) {
  74. if (c % 16 == 0)
  75. str = " ";
  76. else
  77. str += ", ";
  78. str += toHex(a[c]);
  79. if (c % 16 == 15)
  80. {
  81. if (c < NumChars - 1)
  82. str += ",";
  83. echo(str);
  84. }
  85. }
  86. }
  87. echo(" const uint8 ASCIIChars::classes[] = {");
  88. dump(classes);
  89. echo(" };");
  90. echo(" const uint8 ASCIIChars::values[] = {");
  91. dump(values);
  92. echo(" };");
  93. ----------------------------------------------------------------------
  94. */
  95. // Character classes represented as a bit vector for each character.
  96. const uint8 ASCIIChars::classes[] = {
  97. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x06, 0x04, 0x04, 0x06, 0x00, 0x00,
  98. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  99. 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  100. 0x71, 0x71, 0x71, 0x71, 0x71, 0x71, 0x71, 0x71, 0x51, 0x51, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  101. 0x00, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,
  102. 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x00, 0x00, 0x00, 0x00, 0x01,
  103. 0x00, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,
  104. 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00,
  105. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  106. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  107. 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  108. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  109. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  110. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  111. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  112. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  113. };
  114. // Numeric values of ASCII characters interpreted as hex digits (applies to [0-9a-fA-F], all others are 0x00).
  115. const uint8 ASCIIChars::values[] = {
  116. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  117. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  118. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  119. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  120. 0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  121. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  122. 0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  123. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  124. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  125. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  126. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  127. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  128. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  129. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  130. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  131. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  132. };
  133. // ----------------------------------------------------------------------
  134. // TrivialCaseMapper
  135. // ----------------------------------------------------------------------
  136. const TrivialCaseMapper TrivialCaseMapper::Instance;
  137. // ----------------------------------------------------------------------
  138. // StandardChars<char16>
  139. // ----------------------------------------------------------------------
  140. /*
  141. To get the whitespaces string, run:
  142. gawk -f spaces.gawk http://www.unicode.org/Public/9.0.0/ucd/UnicodeData.txt
  143. where spaces.gawk is
  144. ----------------------------------------------------------------------
  145. BEGIN {
  146. FS = ";";
  147. start = -1;
  148. last = -1;
  149. str = "";
  150. }
  151. {
  152. code = strtonum("0x" $1);
  153. if ($3 == "Zs" || code == 0x0009 || code == 0x000B || code == 0x000C || code == 0x0020 || code == 0x00A0 || code == 0xFEFF || code == 0x000A || code == 0x000D || code == 0x2028 || code == 0x2029)
  154. {
  155. if (start < 0)
  156. start = code;
  157. else if (code > last + 1) {
  158. str = sprintf("%s\\x%04x\\x%04x", str, start, last);
  159. start = code;
  160. }
  161. last = code;
  162. }
  163. }
  164. END {
  165. str = sprintf("%s\\x%04x\\x%04x", str, start, last);
  166. print str;
  167. }
  168. ----------------------------------------------------------------------
  169. */
  170. const int StandardChars<char16>::numDigitPairs = 1;
  171. const char16* const StandardChars<char16>::digitStr = _u("09");
  172. const int StandardChars<char16>::numWhitespacePairs = 10;
  173. const char16* const StandardChars<char16>::whitespaceStr = _u("\x0009\x000d\x0020\x0020\x00a0\x00a0\x1680\x1680\x2000\x200a\x2028\x2029\x202f\x202f\x205f\x205f\x3000\x3000\xfeff\xfeff");
  174. const int StandardChars<char16>::numWordPairs = 4;
  175. const char16* const StandardChars<char16>::wordStr = _u("09AZ__az");
  176. const int StandardChars<char16>::numWordIUPairs = 6; // Under /iu flags, Sharp S and Kelvin sign map to S and K, respectively.
  177. const char16* const StandardChars<char16>::wordIUStr = _u("09AZ__az\x017F\x017F\x212A\x212A");
  178. const int StandardChars<char16>::numNewlinePairs = 3;
  179. const char16* const StandardChars<char16>::newlineStr = _u("\x000a\x000a\x000d\x000d\x2028\x2029");
  180. StandardChars<char16>::StandardChars(ArenaAllocator* allocator)
  181. : allocator(allocator)
  182. , unicodeDataCaseMapper(allocator, CaseInsensitive::MappingSource::UnicodeData, &TrivialCaseMapper::Instance)
  183. , caseFoldingCaseMapper(allocator, CaseInsensitive::MappingSource::CaseFolding, &unicodeDataCaseMapper)
  184. , fullSet(0)
  185. , emptySet(0)
  186. , wordSet(0)
  187. , nonWordSet(0)
  188. , newlineSet(0)
  189. , whitespaceSet(0)
  190. {
  191. }
  192. void StandardChars<char16>::SetDigits(ArenaAllocator* setAllocator, CharSet<Char> &set)
  193. {
  194. set.SetRanges(setAllocator, numDigitPairs, digitStr);
  195. }
  196. void StandardChars<char16>::SetNonDigits(ArenaAllocator* setAllocator, CharSet<Char> &set)
  197. {
  198. set.SetNotRanges(setAllocator, numDigitPairs, digitStr);
  199. }
  200. void StandardChars<char16>::SetWhitespace(ArenaAllocator* setAllocator, CharSet<Char> &set)
  201. {
  202. set.SetRanges(setAllocator, numWhitespacePairs, whitespaceStr);
  203. }
  204. void StandardChars<char16>::SetNonWhitespace(ArenaAllocator* setAllocator, CharSet<Char> &set)
  205. {
  206. set.SetNotRanges(setAllocator, numWhitespacePairs, whitespaceStr);
  207. }
  208. void StandardChars<char16>::SetWordChars(ArenaAllocator* setAllocator, CharSet<Char> &set)
  209. {
  210. set.SetRanges(setAllocator, numWordPairs, wordStr);
  211. }
  212. void StandardChars<char16>::SetNonWordChars(ArenaAllocator* setAllocator, CharSet<Char> &set)
  213. {
  214. set.SetNotRanges(setAllocator, numWordPairs, wordStr);
  215. }
  216. void StandardChars<char16>::SetWordIUChars(ArenaAllocator* setAllocator, CharSet<Char> &set)
  217. {
  218. set.SetRanges(setAllocator, numWordIUPairs, wordIUStr);
  219. }
  220. void StandardChars<char16>::SetNonWordIUChars(ArenaAllocator* setAllocator, CharSet<Char> &set)
  221. {
  222. set.SetNotRanges(setAllocator, numWordIUPairs, wordIUStr);
  223. }
  224. void StandardChars<char16>::SetNewline(ArenaAllocator* setAllocator, CharSet<Char> &set)
  225. {
  226. set.SetRanges(setAllocator, numNewlinePairs, newlineStr);
  227. }
  228. void StandardChars<char16>::SetNonNewline(ArenaAllocator* setAllocator, CharSet<Char> &set)
  229. {
  230. set.SetNotRanges(setAllocator, numNewlinePairs, newlineStr);
  231. }
  232. CharSet<char16>* StandardChars<char16>::GetFullSet()
  233. {
  234. if (fullSet == 0)
  235. {
  236. fullSet = Anew(allocator, UnicodeCharSet);
  237. fullSet->SetRange(allocator, MinChar, MaxChar);
  238. }
  239. return fullSet;
  240. }
  241. CharSet<char16>* StandardChars<char16>::GetEmptySet()
  242. {
  243. if (emptySet == 0)
  244. {
  245. emptySet = Anew(allocator, UnicodeCharSet);
  246. // leave empty
  247. }
  248. return emptySet;
  249. }
  250. CharSet<char16>* StandardChars<char16>::GetWordSet()
  251. {
  252. if (wordSet == 0)
  253. {
  254. wordSet = Anew(allocator, UnicodeCharSet);
  255. wordSet->SetRanges(allocator, numWordPairs, wordStr);
  256. }
  257. return wordSet;
  258. }
  259. CharSet<char16>* StandardChars<char16>::GetNonWordSet()
  260. {
  261. if (nonWordSet == 0)
  262. {
  263. nonWordSet = Anew(allocator, UnicodeCharSet);
  264. nonWordSet->SetNotRanges(allocator, numWordPairs, wordStr);
  265. }
  266. return nonWordSet;
  267. }
  268. CharSet<char16>* StandardChars<char16>::GetNewlineSet()
  269. {
  270. if (newlineSet == 0)
  271. {
  272. newlineSet = Anew(allocator, UnicodeCharSet);
  273. newlineSet->SetRanges(allocator, numNewlinePairs, newlineStr);
  274. }
  275. return newlineSet;
  276. }
  277. CharSet<char16>* StandardChars<char16>::GetWhitespaceSet()
  278. {
  279. if (whitespaceSet == 0)
  280. {
  281. whitespaceSet = Anew(allocator, UnicodeCharSet);
  282. whitespaceSet->SetRanges(allocator, numWhitespacePairs, whitespaceStr);
  283. }
  284. return whitespaceSet;
  285. }
  286. CharSet<char16>* StandardChars<char16>::GetSurrogateUpperRange()
  287. {
  288. if (surrogateUpperRange == 0)
  289. {
  290. surrogateUpperRange = Anew(allocator, UnicodeCharSet);
  291. surrogateUpperRange->SetRange(allocator, (char16)0xDC00u, (char16)0xDFFFu);
  292. }
  293. return surrogateUpperRange;
  294. }
  295. }