states.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. *
  3. * Copyright (c) 1998-2002
  4. * John Maddock
  5. *
  6. * Use, modification and distribution are subject to the
  7. * Boost Software License, Version 1.0. (See accompanying file
  8. * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. *
  10. */
  11. /*
  12. * LOCATION: see http://www.boost.org for most recent version.
  13. * FILE states.cpp
  14. * VERSION see <boost/version.hpp>
  15. * DESCRIPTION: Declares internal state machine structures.
  16. */
  17. #ifndef BOOST_REGEX_V5_STATES_HPP
  18. #define BOOST_REGEX_V5_STATES_HPP
  19. namespace boost{
  20. namespace BOOST_REGEX_DETAIL_NS{
  21. /*** mask_type *******************************************************
  22. Whenever we have a choice of two alternatives, we use an array of bytes
  23. to indicate which of the two alternatives it is possible to take for any
  24. given input character. If mask_take is set, then we can take the next
  25. state, and if mask_skip is set then we can take the alternative.
  26. ***********************************************************************/
  27. enum mask_type
  28. {
  29. mask_take = 1,
  30. mask_skip = 2,
  31. mask_init = 4,
  32. mask_any = mask_skip | mask_take,
  33. mask_all = mask_any
  34. };
  35. /*** helpers **********************************************************
  36. These helpers let us use function overload resolution to detect whether
  37. we have narrow or wide character strings:
  38. ***********************************************************************/
  39. struct _narrow_type{};
  40. struct _wide_type{};
  41. template <class charT> struct is_byte;
  42. template<> struct is_byte<char> { typedef _narrow_type width_type; };
  43. template<> struct is_byte<unsigned char>{ typedef _narrow_type width_type; };
  44. template<> struct is_byte<signed char> { typedef _narrow_type width_type; };
  45. template <class charT> struct is_byte { typedef _wide_type width_type; };
  46. /*** enum syntax_element_type ******************************************
  47. Every record in the state machine falls into one of the following types:
  48. ***********************************************************************/
  49. enum syntax_element_type
  50. {
  51. // start of a marked sub-expression, or perl-style (?...) extension
  52. syntax_element_startmark = 0,
  53. // end of a marked sub-expression, or perl-style (?...) extension
  54. syntax_element_endmark = syntax_element_startmark + 1,
  55. // any sequence of literal characters
  56. syntax_element_literal = syntax_element_endmark + 1,
  57. // start of line assertion: ^
  58. syntax_element_start_line = syntax_element_literal + 1,
  59. // end of line assertion $
  60. syntax_element_end_line = syntax_element_start_line + 1,
  61. // match any character: .
  62. syntax_element_wild = syntax_element_end_line + 1,
  63. // end of expression: we have a match when we get here
  64. syntax_element_match = syntax_element_wild + 1,
  65. // perl style word boundary: \b
  66. syntax_element_word_boundary = syntax_element_match + 1,
  67. // perl style within word boundary: \B
  68. syntax_element_within_word = syntax_element_word_boundary + 1,
  69. // start of word assertion: \<
  70. syntax_element_word_start = syntax_element_within_word + 1,
  71. // end of word assertion: \>
  72. syntax_element_word_end = syntax_element_word_start + 1,
  73. // start of buffer assertion: \`
  74. syntax_element_buffer_start = syntax_element_word_end + 1,
  75. // end of buffer assertion: \'
  76. syntax_element_buffer_end = syntax_element_buffer_start + 1,
  77. // backreference to previously matched sub-expression
  78. syntax_element_backref = syntax_element_buffer_end + 1,
  79. // either a wide character set [..] or one with multicharacter collating elements:
  80. syntax_element_long_set = syntax_element_backref + 1,
  81. // narrow character set: [...]
  82. syntax_element_set = syntax_element_long_set + 1,
  83. // jump to a new state in the machine:
  84. syntax_element_jump = syntax_element_set + 1,
  85. // choose between two production states:
  86. syntax_element_alt = syntax_element_jump + 1,
  87. // a repeat
  88. syntax_element_rep = syntax_element_alt + 1,
  89. // match a combining character sequence
  90. syntax_element_combining = syntax_element_rep + 1,
  91. // perl style soft buffer end: \z
  92. syntax_element_soft_buffer_end = syntax_element_combining + 1,
  93. // perl style continuation: \G
  94. syntax_element_restart_continue = syntax_element_soft_buffer_end + 1,
  95. // single character repeats:
  96. syntax_element_dot_rep = syntax_element_restart_continue + 1,
  97. syntax_element_char_rep = syntax_element_dot_rep + 1,
  98. syntax_element_short_set_rep = syntax_element_char_rep + 1,
  99. syntax_element_long_set_rep = syntax_element_short_set_rep + 1,
  100. // a backstep for lookbehind repeats:
  101. syntax_element_backstep = syntax_element_long_set_rep + 1,
  102. // an assertion that a mark was matched:
  103. syntax_element_assert_backref = syntax_element_backstep + 1,
  104. syntax_element_toggle_case = syntax_element_assert_backref + 1,
  105. // a recursive expression:
  106. syntax_element_recurse = syntax_element_toggle_case + 1,
  107. // Verbs:
  108. syntax_element_fail = syntax_element_recurse + 1,
  109. syntax_element_accept = syntax_element_fail + 1,
  110. syntax_element_commit = syntax_element_accept + 1,
  111. syntax_element_then = syntax_element_commit + 1
  112. };
  113. #ifdef BOOST_REGEX_DEBUG
  114. // dwa 09/26/00 - This is needed to suppress warnings about an ambiguous conversion
  115. std::ostream& operator<<(std::ostream&, syntax_element_type);
  116. #endif
  117. struct re_syntax_base;
  118. /*** union offset_type ************************************************
  119. Points to another state in the machine. During machine construction
  120. we use integral offsets, but these are converted to pointers before
  121. execution of the machine.
  122. ***********************************************************************/
  123. union offset_type
  124. {
  125. re_syntax_base* p;
  126. std::ptrdiff_t i;
  127. };
  128. /*** struct re_syntax_base ********************************************
  129. Base class for all states in the machine.
  130. ***********************************************************************/
  131. struct re_syntax_base
  132. {
  133. syntax_element_type type; // what kind of state this is
  134. offset_type next; // next state in the machine
  135. };
  136. /*** struct re_brace **************************************************
  137. A marked parenthesis.
  138. ***********************************************************************/
  139. struct re_brace : public re_syntax_base
  140. {
  141. // The index to match, can be zero (don't mark the sub-expression)
  142. // or negative (for perl style (?...) extensions):
  143. int index;
  144. bool icase;
  145. };
  146. /*** struct re_dot **************************************************
  147. Match anything.
  148. ***********************************************************************/
  149. enum
  150. {
  151. dont_care = 1,
  152. force_not_newline = 0,
  153. force_newline = 2,
  154. test_not_newline = 2,
  155. test_newline = 3
  156. };
  157. struct re_dot : public re_syntax_base
  158. {
  159. unsigned char mask;
  160. };
  161. /*** struct re_literal ************************************************
  162. A string of literals, following this structure will be an
  163. array of characters: charT[length]
  164. ***********************************************************************/
  165. struct re_literal : public re_syntax_base
  166. {
  167. unsigned int length;
  168. };
  169. /*** struct re_case ************************************************
  170. Indicates whether we are moving to a case insensive block or not
  171. ***********************************************************************/
  172. struct re_case : public re_syntax_base
  173. {
  174. bool icase;
  175. };
  176. /*** struct re_set_long ***********************************************
  177. A wide character set of characters, following this structure will be
  178. an array of type charT:
  179. First csingles null-terminated strings
  180. Then 2 * cranges NULL terminated strings
  181. Then cequivalents NULL terminated strings
  182. ***********************************************************************/
  183. template <class mask_type>
  184. struct re_set_long : public re_syntax_base
  185. {
  186. unsigned int csingles, cranges, cequivalents;
  187. mask_type cclasses;
  188. mask_type cnclasses;
  189. bool isnot;
  190. bool singleton;
  191. };
  192. /*** struct re_set ****************************************************
  193. A set of narrow-characters, matches any of _map which is none-zero
  194. ***********************************************************************/
  195. struct re_set : public re_syntax_base
  196. {
  197. unsigned char _map[1 << CHAR_BIT];
  198. };
  199. /*** struct re_jump ***************************************************
  200. Jump to a new location in the machine (not next).
  201. ***********************************************************************/
  202. struct re_jump : public re_syntax_base
  203. {
  204. offset_type alt; // location to jump to
  205. };
  206. /*** struct re_alt ***************************************************
  207. Jump to a new location in the machine (possibly next).
  208. ***********************************************************************/
  209. struct re_alt : public re_jump
  210. {
  211. unsigned char _map[1 << CHAR_BIT]; // which characters can take the jump
  212. unsigned int can_be_null; // true if we match a NULL string
  213. };
  214. /*** struct re_repeat *************************************************
  215. Repeat a section of the machine
  216. ***********************************************************************/
  217. struct re_repeat : public re_alt
  218. {
  219. std::size_t min, max; // min and max allowable repeats
  220. int state_id; // Unique identifier for this repeat
  221. bool leading; // True if this repeat is at the start of the machine (lets us optimize some searches)
  222. bool greedy; // True if this is a greedy repeat
  223. };
  224. /*** struct re_recurse ************************************************
  225. Recurse to a particular subexpression.
  226. **********************************************************************/
  227. struct re_recurse : public re_jump
  228. {
  229. int state_id; // identifier of first nested repeat within the recursion.
  230. };
  231. /*** struct re_commit *************************************************
  232. Used for the PRUNE, SKIP and COMMIT verbs which basically differ only in what happens
  233. if no match is found and we start searching forward.
  234. **********************************************************************/
  235. enum commit_type
  236. {
  237. commit_prune,
  238. commit_skip,
  239. commit_commit
  240. };
  241. struct re_commit : public re_syntax_base
  242. {
  243. commit_type action;
  244. };
  245. /*** enum re_jump_size_type *******************************************
  246. Provides compiled size of re_jump structure (allowing for trailing alignment).
  247. We provide this so we know how manybytes to insert when constructing the machine
  248. (The value of padding_mask is defined in regex_raw_buffer.hpp).
  249. ***********************************************************************/
  250. enum re_jump_size_type
  251. {
  252. re_jump_size = (sizeof(re_jump) + padding_mask) & ~(padding_mask),
  253. re_repeater_size = (sizeof(re_repeat) + padding_mask) & ~(padding_mask),
  254. re_alt_size = (sizeof(re_alt) + padding_mask) & ~(padding_mask)
  255. };
  256. /*** proc re_is_set_member *********************************************
  257. Forward declaration: we'll need this one later...
  258. ***********************************************************************/
  259. template<class charT, class traits>
  260. struct regex_data;
  261. template <class iterator, class charT, class traits_type, class char_classT>
  262. iterator re_is_set_member(iterator next,
  263. iterator last,
  264. const re_set_long<char_classT>* set_,
  265. const regex_data<charT, traits_type>& e, bool icase);
  266. } // namespace BOOST_REGEX_DETAIL_NS
  267. } // namespace boost
  268. #endif