tre.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. tre-internal.h - TRE internal definitions
  3. Copyright (c) 2001-2009 Ville Laurikari <[email protected]>
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions
  7. are met:
  8. 1. Redistributions of source code must retain the above copyright
  9. notice, this list of conditions and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
  14. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  15. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  16. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  17. HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  18. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  19. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include <regex.h>
  26. #include <wchar.h>
  27. #include <wctype.h>
  28. #undef TRE_MBSTATE
  29. #define NDEBUG
  30. #define TRE_REGEX_T_FIELD __opaque
  31. typedef int reg_errcode_t;
  32. typedef wchar_t tre_char_t;
  33. #define DPRINT(msg) do { } while(0)
  34. #define elementsof(x) ( sizeof(x) / sizeof(x[0]) )
  35. #define tre_mbrtowc(pwc, s, n, ps) (mbtowc((pwc), (s), (n)))
  36. /* Wide characters. */
  37. typedef wint_t tre_cint_t;
  38. #define TRE_CHAR_MAX 0x10ffff
  39. #define tre_isalnum iswalnum
  40. #define tre_isalpha iswalpha
  41. #define tre_isblank iswblank
  42. #define tre_iscntrl iswcntrl
  43. #define tre_isdigit iswdigit
  44. #define tre_isgraph iswgraph
  45. #define tre_islower iswlower
  46. #define tre_isprint iswprint
  47. #define tre_ispunct iswpunct
  48. #define tre_isspace iswspace
  49. #define tre_isupper iswupper
  50. #define tre_isxdigit iswxdigit
  51. #define tre_tolower towlower
  52. #define tre_toupper towupper
  53. #define tre_strlen wcslen
  54. /* Use system provided iswctype() and wctype(). */
  55. typedef wctype_t tre_ctype_t;
  56. #define tre_isctype iswctype
  57. #define tre_ctype wctype
  58. /* Returns number of bytes to add to (char *)ptr to make it
  59. properly aligned for the type. */
  60. #define ALIGN(ptr, type) \
  61. ((((long)ptr) % sizeof(type)) \
  62. ? (sizeof(type) - (((long)ptr) % sizeof(type))) \
  63. : 0)
  64. #undef MAX
  65. #undef MIN
  66. #define MAX(a, b) (((a) >= (b)) ? (a) : (b))
  67. #define MIN(a, b) (((a) <= (b)) ? (a) : (b))
  68. /* TNFA transition type. A TNFA state is an array of transitions,
  69. the terminator is a transition with NULL `state'. */
  70. typedef struct tnfa_transition tre_tnfa_transition_t;
  71. struct tnfa_transition {
  72. /* Range of accepted characters. */
  73. tre_cint_t code_min;
  74. tre_cint_t code_max;
  75. /* Pointer to the destination state. */
  76. tre_tnfa_transition_t *state;
  77. /* ID number of the destination state. */
  78. int state_id;
  79. /* -1 terminated array of tags (or NULL). */
  80. int *tags;
  81. /* Assertion bitmap. */
  82. int assertions;
  83. /* Assertion parameters. */
  84. union {
  85. /* Character class assertion. */
  86. tre_ctype_t class;
  87. /* Back reference assertion. */
  88. int backref;
  89. } u;
  90. /* Negative character class assertions. */
  91. tre_ctype_t *neg_classes;
  92. };
  93. /* Assertions. */
  94. #define ASSERT_AT_BOL 1 /* Beginning of line. */
  95. #define ASSERT_AT_EOL 2 /* End of line. */
  96. #define ASSERT_CHAR_CLASS 4 /* Character class in `class'. */
  97. #define ASSERT_CHAR_CLASS_NEG 8 /* Character classes in `neg_classes'. */
  98. #define ASSERT_AT_BOW 16 /* Beginning of word. */
  99. #define ASSERT_AT_EOW 32 /* End of word. */
  100. #define ASSERT_AT_WB 64 /* Word boundary. */
  101. #define ASSERT_AT_WB_NEG 128 /* Not a word boundary. */
  102. #define ASSERT_BACKREF 256 /* A back reference in `backref'. */
  103. #define ASSERT_LAST 256
  104. /* Tag directions. */
  105. typedef enum {
  106. TRE_TAG_MINIMIZE = 0,
  107. TRE_TAG_MAXIMIZE = 1
  108. } tre_tag_direction_t;
  109. /* Instructions to compute submatch register values from tag values
  110. after a successful match. */
  111. struct tre_submatch_data {
  112. /* Tag that gives the value for rm_so (submatch start offset). */
  113. int so_tag;
  114. /* Tag that gives the value for rm_eo (submatch end offset). */
  115. int eo_tag;
  116. /* List of submatches this submatch is contained in. */
  117. int *parents;
  118. };
  119. typedef struct tre_submatch_data tre_submatch_data_t;
  120. /* TNFA definition. */
  121. typedef struct tnfa tre_tnfa_t;
  122. struct tnfa {
  123. tre_tnfa_transition_t *transitions;
  124. unsigned int num_transitions;
  125. tre_tnfa_transition_t *initial;
  126. tre_tnfa_transition_t *final;
  127. tre_submatch_data_t *submatch_data;
  128. char *firstpos_chars;
  129. int first_char;
  130. unsigned int num_submatches;
  131. tre_tag_direction_t *tag_directions;
  132. int *minimal_tags;
  133. int num_tags;
  134. int num_minimals;
  135. int end_tag;
  136. int num_states;
  137. int cflags;
  138. int have_backrefs;
  139. int have_approx;
  140. };
  141. /* from tre-mem.h: */
  142. #define TRE_MEM_BLOCK_SIZE 1024
  143. typedef struct tre_list {
  144. void *data;
  145. struct tre_list *next;
  146. } tre_list_t;
  147. typedef struct tre_mem_struct {
  148. tre_list_t *blocks;
  149. tre_list_t *current;
  150. char *ptr;
  151. size_t n;
  152. int failed;
  153. void **provided;
  154. } *tre_mem_t;
  155. #define tre_mem_new_impl __tre_mem_new_impl
  156. #define tre_mem_alloc_impl __tre_mem_alloc_impl
  157. #define tre_mem_destroy __tre_mem_destroy
  158. hidden tre_mem_t tre_mem_new_impl(int provided, void *provided_block);
  159. hidden void *tre_mem_alloc_impl(tre_mem_t mem, int provided, void *provided_block,
  160. int zero, size_t size);
  161. /* Returns a new memory allocator or NULL if out of memory. */
  162. #define tre_mem_new() tre_mem_new_impl(0, NULL)
  163. /* Allocates a block of `size' bytes from `mem'. Returns a pointer to the
  164. allocated block or NULL if an underlying malloc() failed. */
  165. #define tre_mem_alloc(mem, size) tre_mem_alloc_impl(mem, 0, NULL, 0, size)
  166. /* Allocates a block of `size' bytes from `mem'. Returns a pointer to the
  167. allocated block or NULL if an underlying malloc() failed. The memory
  168. is set to zero. */
  169. #define tre_mem_calloc(mem, size) tre_mem_alloc_impl(mem, 0, NULL, 1, size)
  170. #ifdef TRE_USE_ALLOCA
  171. /* alloca() versions. Like above, but memory is allocated with alloca()
  172. instead of malloc(). */
  173. #define tre_mem_newa() \
  174. tre_mem_new_impl(1, alloca(sizeof(struct tre_mem_struct)))
  175. #define tre_mem_alloca(mem, size) \
  176. ((mem)->n >= (size) \
  177. ? tre_mem_alloc_impl((mem), 1, NULL, 0, (size)) \
  178. : tre_mem_alloc_impl((mem), 1, alloca(TRE_MEM_BLOCK_SIZE), 0, (size)))
  179. #endif /* TRE_USE_ALLOCA */
  180. /* Frees the memory allocator and all memory allocated with it. */
  181. hidden void tre_mem_destroy(tre_mem_t mem);
  182. #define xmalloc malloc
  183. #define xcalloc calloc
  184. #define xfree free
  185. #define xrealloc realloc