states.hpp 11 KB

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