/js/src/yarr/pcre/pcre_internal.h

http://github.com/zpao/v8monkey · C Header · 434 lines · 264 code · 67 blank · 103 comment · 23 complexity · 987e0b3c7cd60a45c658677bc7a893a1 MD5 · raw file

  1. /* This is JavaScriptCore's variant of the PCRE library. While this library
  2. started out as a copy of PCRE, many of the features of PCRE have been
  3. removed. This library now supports only the regular expression features
  4. required by the JavaScript language specification, and has only the functions
  5. needed by JavaScriptCore and the rest of WebKit.
  6. Originally written by Philip Hazel
  7. Copyright (c) 1997-2006 University of Cambridge
  8. Copyright (C) 2002, 2004, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
  9. -----------------------------------------------------------------------------
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions are met:
  12. * Redistributions of source code must retain the above copyright notice,
  13. this list of conditions and the following disclaimer.
  14. * Redistributions in binary form must reproduce the above copyright
  15. notice, this list of conditions and the following disclaimer in the
  16. documentation and/or other materials provided with the distribution.
  17. * Neither the name of the University of Cambridge nor the names of its
  18. contributors may be used to endorse or promote products derived from
  19. this software without specific prior written permission.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  24. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. POSSIBILITY OF SUCH DAMAGE.
  31. -----------------------------------------------------------------------------
  32. */
  33. /* This header contains definitions that are shared between the different
  34. modules, but which are not relevant to the exported API. This includes some
  35. functions whose names all begin with "_pcre_". */
  36. #ifndef PCRE_INTERNAL_H
  37. #define PCRE_INTERNAL_H
  38. /* Bit definitions for entries in the pcre_ctypes table. */
  39. #define ctype_space 0x01
  40. #define ctype_xdigit 0x08
  41. #define ctype_word 0x10 /* alphameric or '_' */
  42. /* Offsets for the bitmap tables in pcre_cbits. Each table contains a set
  43. of bits for a class map. Some classes are built by combining these tables. */
  44. #define cbit_space 0 /* \s */
  45. #define cbit_digit 32 /* \d */
  46. #define cbit_word 64 /* \w */
  47. #define cbit_length 96 /* Length of the cbits table */
  48. /* Offsets of the various tables from the base tables pointer, and
  49. total length. */
  50. #define lcc_offset 0
  51. #define fcc_offset 128
  52. #define cbits_offset 256
  53. #define ctypes_offset (cbits_offset + cbit_length)
  54. #define tables_length (ctypes_offset + 128)
  55. #ifndef DFTABLES
  56. #include "pcre.h"
  57. /* The value of LINK_SIZE determines the number of bytes used to store links as
  58. offsets within the compiled regex. The default is 2, which allows for compiled
  59. patterns up to 64K long. */
  60. #define LINK_SIZE 3
  61. /* Use a macro for debugging printing, 'cause that eliminates the use of #ifdef
  62. inline, and there are *still* stupid compilers about that don't like indented
  63. pre-processor statements, or at least there were when I first wrote this. After
  64. all, it had only been about 10 years then... */
  65. #ifdef DEBUG
  66. #define DPRINTF(p) /*printf p; fflush(stdout);*/
  67. #else
  68. #define DPRINTF(p) /*nothing*/
  69. #endif
  70. /* PCRE keeps offsets in its compiled code as 2-byte quantities (always stored
  71. in big-endian order) by default. These are used, for example, to link from the
  72. start of a subpattern to its alternatives and its end. The use of 2 bytes per
  73. offset limits the size of the compiled regex to around 64K, which is big enough
  74. for almost everybody. However, I received a request for an even bigger limit.
  75. For this reason, and also to make the code easier to maintain, the storing and
  76. loading of offsets from the byte string is now handled by the functions that are
  77. defined here. */
  78. /* PCRE uses some other 2-byte quantities that do not change when the size of
  79. offsets changes. There are used for repeat counts and for other things such as
  80. capturing parenthesis numbers in back references. */
  81. static inline void put2ByteValue(unsigned char* opcodePtr, int value)
  82. {
  83. JS_ASSERT(value >= 0 && value <= 0xFFFF);
  84. opcodePtr[0] = value >> 8;
  85. opcodePtr[1] = value;
  86. }
  87. static inline void put3ByteValue(unsigned char* opcodePtr, int value)
  88. {
  89. JS_ASSERT(value >= 0 && value <= 0xFFFFFF);
  90. opcodePtr[0] = value >> 16;
  91. opcodePtr[1] = value >> 8;
  92. opcodePtr[2] = value;
  93. }
  94. static inline int get2ByteValue(const unsigned char* opcodePtr)
  95. {
  96. return (opcodePtr[0] << 8) | opcodePtr[1];
  97. }
  98. static inline int get3ByteValue(const unsigned char* opcodePtr)
  99. {
  100. return (opcodePtr[0] << 16) | (opcodePtr[1] << 8) | opcodePtr[2];
  101. }
  102. static inline void put2ByteValueAndAdvance(unsigned char*& opcodePtr, int value)
  103. {
  104. put2ByteValue(opcodePtr, value);
  105. opcodePtr += 2;
  106. }
  107. static inline void put3ByteValueAndAdvance(unsigned char*& opcodePtr, int value)
  108. {
  109. put3ByteValue(opcodePtr, value);
  110. opcodePtr += 3;
  111. }
  112. static inline void putLinkValueAllowZero(unsigned char* opcodePtr, int value)
  113. {
  114. #if LINK_SIZE == 3
  115. put3ByteValue(opcodePtr, value);
  116. #elif LINK_SIZE == 2
  117. put2ByteValue(opcodePtr, value);
  118. #else
  119. # error LINK_SIZE not supported.
  120. #endif
  121. }
  122. static inline int getLinkValueAllowZero(const unsigned char* opcodePtr)
  123. {
  124. #if LINK_SIZE == 3
  125. return get3ByteValue(opcodePtr);
  126. #elif LINK_SIZE == 2
  127. return get2ByteValue(opcodePtr);
  128. #else
  129. # error LINK_SIZE not supported.
  130. #endif
  131. }
  132. #define MAX_PATTERN_SIZE 4096 * 1024 // Derived by empirical testing of compile time in PCRE and WREC.
  133. JS_STATIC_ASSERT(MAX_PATTERN_SIZE < (1 << (8 * LINK_SIZE)));
  134. static inline void putLinkValue(unsigned char* opcodePtr, int value)
  135. {
  136. JS_ASSERT(value);
  137. putLinkValueAllowZero(opcodePtr, value);
  138. }
  139. static inline int getLinkValue(const unsigned char* opcodePtr)
  140. {
  141. int value = getLinkValueAllowZero(opcodePtr);
  142. JS_ASSERT(value);
  143. return value;
  144. }
  145. static inline void putLinkValueAndAdvance(unsigned char*& opcodePtr, int value)
  146. {
  147. putLinkValue(opcodePtr, value);
  148. opcodePtr += LINK_SIZE;
  149. }
  150. static inline void putLinkValueAllowZeroAndAdvance(unsigned char*& opcodePtr, int value)
  151. {
  152. putLinkValueAllowZero(opcodePtr, value);
  153. opcodePtr += LINK_SIZE;
  154. }
  155. // FIXME: These are really more of a "compiled regexp state" than "regexp options"
  156. enum RegExpOptions {
  157. UseFirstByteOptimizationOption = 0x40000000, /* firstByte is set */
  158. UseRequiredByteOptimizationOption = 0x20000000, /* reqByte is set */
  159. UseMultiLineFirstByteOptimizationOption = 0x10000000, /* start after \n for multiline */
  160. IsAnchoredOption = 0x02000000, /* can't use partial with this regex */
  161. IgnoreCaseOption = 0x00000001,
  162. MatchAcrossMultipleLinesOption = 0x00000002
  163. };
  164. /* Flags added to firstByte or reqByte; a "non-literal" item is either a
  165. variable-length repeat, or a anything other than literal characters. */
  166. #define REQ_IGNORE_CASE 0x0100 /* indicates should ignore case */
  167. #define REQ_VARY 0x0200 /* reqByte followed non-literal item */
  168. /* Miscellaneous definitions */
  169. /* Flag bits and data types for the extended class (OP_XCLASS) for classes that
  170. contain UTF-8 characters with values greater than 255. */
  171. #define XCL_NOT 0x01 /* Flag: this is a negative class */
  172. #define XCL_MAP 0x02 /* Flag: a 32-byte map is present */
  173. #define XCL_END 0 /* Marks end of individual items */
  174. #define XCL_SINGLE 1 /* Single item (one multibyte char) follows */
  175. #define XCL_RANGE 2 /* A range (two multibyte chars) follows */
  176. /* These are escaped items that aren't just an encoding of a particular data
  177. value such as \n. They must have non-zero values, as check_escape() returns
  178. their negation. Also, they must appear in the same order as in the opcode
  179. definitions below, up to ESC_w. The final one must be
  180. ESC_REF as subsequent values are used for \1, \2, \3, etc. There is are two
  181. tests in the code for an escape > ESC_b and <= ESC_w to
  182. detect the types that may be repeated. These are the types that consume
  183. characters. If any new escapes are put in between that don't consume a
  184. character, that code will have to change. */
  185. enum { ESC_B = 1, ESC_b, ESC_D, ESC_d, ESC_S, ESC_s, ESC_W, ESC_w, ESC_REF };
  186. /* Opcode table: OP_BRA must be last, as all values >= it are used for brackets
  187. that extract substrings. Starting from 1 (i.e. after OP_END), the values up to
  188. OP_EOD must correspond in order to the list of escapes immediately above.
  189. Note that whenever this list is updated, the two macro definitions that follow
  190. must also be updated to match. */
  191. #define FOR_EACH_OPCODE(macro) \
  192. macro(END) \
  193. \
  194. , macro(NOT_WORD_BOUNDARY) \
  195. , macro(WORD_BOUNDARY) \
  196. , macro(NOT_DIGIT) \
  197. , macro(DIGIT) \
  198. , macro(NOT_WHITESPACE) \
  199. , macro(WHITESPACE) \
  200. , macro(NOT_WORDCHAR) \
  201. , macro(WORDCHAR) \
  202. \
  203. , macro(NOT_NEWLINE) \
  204. \
  205. , macro(CIRC) \
  206. , macro(DOLL) \
  207. , macro(BOL) \
  208. , macro(EOL) \
  209. , macro(CHAR) \
  210. , macro(CHAR_IGNORING_CASE) \
  211. , macro(ASCII_CHAR) \
  212. , macro(ASCII_LETTER_IGNORING_CASE) \
  213. , macro(NOT) \
  214. \
  215. , macro(STAR) \
  216. , macro(MINSTAR) \
  217. , macro(PLUS) \
  218. , macro(MINPLUS) \
  219. , macro(QUERY) \
  220. , macro(MINQUERY) \
  221. , macro(UPTO) \
  222. , macro(MINUPTO) \
  223. , macro(EXACT) \
  224. \
  225. , macro(NOTSTAR) \
  226. , macro(NOTMINSTAR) \
  227. , macro(NOTPLUS) \
  228. , macro(NOTMINPLUS) \
  229. , macro(NOTQUERY) \
  230. , macro(NOTMINQUERY) \
  231. , macro(NOTUPTO) \
  232. , macro(NOTMINUPTO) \
  233. , macro(NOTEXACT) \
  234. \
  235. , macro(TYPESTAR) \
  236. , macro(TYPEMINSTAR) \
  237. , macro(TYPEPLUS) \
  238. , macro(TYPEMINPLUS) \
  239. , macro(TYPEQUERY) \
  240. , macro(TYPEMINQUERY) \
  241. , macro(TYPEUPTO) \
  242. , macro(TYPEMINUPTO) \
  243. , macro(TYPEEXACT) \
  244. \
  245. , macro(CRSTAR) \
  246. , macro(CRMINSTAR) \
  247. , macro(CRPLUS) \
  248. , macro(CRMINPLUS) \
  249. , macro(CRQUERY) \
  250. , macro(CRMINQUERY) \
  251. , macro(CRRANGE) \
  252. , macro(CRMINRANGE) \
  253. \
  254. , macro(CLASS) \
  255. , macro(NCLASS) \
  256. , macro(XCLASS) \
  257. \
  258. , macro(REF) \
  259. \
  260. , macro(ALT) \
  261. , macro(KET) \
  262. , macro(KETRMAX) \
  263. , macro(KETRMIN) \
  264. \
  265. , macro(ASSERT) \
  266. , macro(ASSERT_NOT) \
  267. \
  268. , macro(BRAZERO) \
  269. , macro(BRAMINZERO) \
  270. , macro(BRANUMBER) \
  271. , macro(BRA)
  272. #define OPCODE_ENUM_VALUE(opcode) OP_##opcode
  273. enum { FOR_EACH_OPCODE(OPCODE_ENUM_VALUE) };
  274. /* WARNING WARNING WARNING: There is an implicit assumption in pcre.c and
  275. study.c that all opcodes are less than 128 in value. This makes handling UTF-8
  276. character sequences easier. */
  277. /* The highest extraction number before we have to start using additional
  278. bytes. (Originally PCRE didn't have support for extraction counts higher than
  279. this number.) The value is limited by the number of opcodes left after OP_BRA,
  280. i.e. 255 - OP_BRA. We actually set it a bit lower to leave room for additional
  281. opcodes. */
  282. /* FIXME: Note that OP_BRA + 100 is > 128, so the two comments above
  283. are in conflict! */
  284. #define EXTRACT_BASIC_MAX 100
  285. /* The code vector runs on as long as necessary after the end. */
  286. struct JSRegExp {
  287. unsigned options;
  288. unsigned short topBracket;
  289. unsigned short topBackref;
  290. unsigned short firstByte;
  291. unsigned short reqByte;
  292. };
  293. /* Internal shared data tables. These are tables that are used by more than one
  294. of the exported public functions. They have to be "external" in the C sense,
  295. but are not part of the PCRE public API. The data for these tables is in the
  296. pcre_tables.c module. */
  297. #define jsc_pcre_utf8_table1_size 6
  298. extern const int jsc_pcre_utf8_table1[6];
  299. extern const int jsc_pcre_utf8_table2[6];
  300. extern const int jsc_pcre_utf8_table3[6];
  301. extern const unsigned char jsc_pcre_utf8_table4[0x40];
  302. extern const unsigned char jsc_pcre_default_tables[tables_length];
  303. static inline unsigned char toLowerCase(unsigned char c)
  304. {
  305. static const unsigned char* lowerCaseChars = jsc_pcre_default_tables + lcc_offset;
  306. return lowerCaseChars[c];
  307. }
  308. static inline unsigned char flipCase(unsigned char c)
  309. {
  310. static const unsigned char* flippedCaseChars = jsc_pcre_default_tables + fcc_offset;
  311. return flippedCaseChars[c];
  312. }
  313. static inline unsigned char classBitmapForChar(unsigned char c)
  314. {
  315. static const unsigned char* charClassBitmaps = jsc_pcre_default_tables + cbits_offset;
  316. return charClassBitmaps[c];
  317. }
  318. static inline unsigned char charTypeForChar(unsigned char c)
  319. {
  320. const unsigned char* charTypeMap = jsc_pcre_default_tables + ctypes_offset;
  321. return charTypeMap[c];
  322. }
  323. static inline bool isWordChar(UChar c)
  324. {
  325. return c < 128 && (charTypeForChar(c) & ctype_word);
  326. }
  327. static inline bool isSpaceChar(UChar c)
  328. {
  329. return (c < 128 && (charTypeForChar(c) & ctype_space)) || c == 0x00A0;
  330. }
  331. static inline bool isNewline(UChar nl)
  332. {
  333. return (nl == 0xA || nl == 0xD || nl == 0x2028 || nl == 0x2029);
  334. }
  335. static inline bool isBracketStartOpcode(unsigned char opcode)
  336. {
  337. if (opcode >= OP_BRA)
  338. return true;
  339. switch (opcode) {
  340. case OP_ASSERT:
  341. case OP_ASSERT_NOT:
  342. return true;
  343. default:
  344. return false;
  345. }
  346. }
  347. static inline void advanceToEndOfBracket(const unsigned char*& opcodePtr)
  348. {
  349. JS_ASSERT(isBracketStartOpcode(*opcodePtr) || *opcodePtr == OP_ALT);
  350. do
  351. opcodePtr += getLinkValue(opcodePtr + 1);
  352. while (*opcodePtr == OP_ALT);
  353. }
  354. /* Internal shared functions. These are functions that are used in more
  355. that one of the source files. They have to have external linkage, but
  356. but are not part of the public API and so not exported from the library. */
  357. extern int jsc_pcre_ucp_othercase(unsigned);
  358. extern bool jsc_pcre_xclass(int, const unsigned char*);
  359. #endif
  360. #endif
  361. /* End of pcre_internal.h */