regparse.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. #ifndef REGPARSE_H
  2. #define REGPARSE_H
  3. /**********************************************************************
  4. regparse.h - Oniguruma (regular expression library)
  5. **********************************************************************/
  6. /*-
  7. * Copyright (c) 2002-2007 K.Kosako <sndgk393 AT ybb DOT ne DOT jp>
  8. * 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
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. */
  31. #include "regint.h"
  32. /* node type */
  33. #define NT_STR 0
  34. #define NT_CCLASS 1
  35. #define NT_CTYPE 2
  36. #define NT_CANY 3
  37. #define NT_BREF 4
  38. #define NT_QTFR 5
  39. #define NT_ENCLOSE 6
  40. #define NT_ANCHOR 7
  41. #define NT_LIST 8
  42. #define NT_ALT 9
  43. #define NT_CALL 10
  44. /* node type bit */
  45. #define NTYPE2BIT(type) (1<<(type))
  46. #define BIT_NT_STR NTYPE2BIT(NT_STR)
  47. #define BIT_NT_CCLASS NTYPE2BIT(NT_CCLASS)
  48. #define BIT_NT_CTYPE NTYPE2BIT(NT_CTYPE)
  49. #define BIT_NT_CANY NTYPE2BIT(NT_CANY)
  50. #define BIT_NT_BREF NTYPE2BIT(NT_BREF)
  51. #define BIT_NT_QTFR NTYPE2BIT(NT_QTFR)
  52. #define BIT_NT_ENCLOSE NTYPE2BIT(NT_ENCLOSE)
  53. #define BIT_NT_ANCHOR NTYPE2BIT(NT_ANCHOR)
  54. #define BIT_NT_LIST NTYPE2BIT(NT_LIST)
  55. #define BIT_NT_ALT NTYPE2BIT(NT_ALT)
  56. #define BIT_NT_CALL NTYPE2BIT(NT_CALL)
  57. #define IS_NODE_TYPE_SIMPLE(type) \
  58. ((NTYPE2BIT(type) & (BIT_NT_STR | BIT_NT_CCLASS | BIT_NT_CTYPE |\
  59. BIT_NT_CANY | BIT_NT_BREF)) != 0)
  60. #define NTYPE(node) ((node)->u.base.type)
  61. #define SET_NTYPE(node, ntype) (node)->u.base.type = (ntype)
  62. #define NSTR(node) (&((node)->u.str))
  63. #define NCCLASS(node) (&((node)->u.cclass))
  64. #define NCTYPE(node) (&((node)->u.ctype))
  65. #define NBREF(node) (&((node)->u.bref))
  66. #define NQTFR(node) (&((node)->u.qtfr))
  67. #define NENCLOSE(node) (&((node)->u.enclose))
  68. #define NANCHOR(node) (&((node)->u.anchor))
  69. #define NCONS(node) (&((node)->u.cons))
  70. #define NCALL(node) (&((node)->u.call))
  71. #define NCAR(node) (NCONS(node)->car)
  72. #define NCDR(node) (NCONS(node)->cdr)
  73. #define ANCHOR_ANYCHAR_STAR_MASK (ANCHOR_ANYCHAR_STAR | ANCHOR_ANYCHAR_STAR_ML)
  74. #define ANCHOR_END_BUF_MASK (ANCHOR_END_BUF | ANCHOR_SEMI_END_BUF)
  75. #define ENCLOSE_MEMORY (1<<0)
  76. #define ENCLOSE_OPTION (1<<1)
  77. #define ENCLOSE_STOP_BACKTRACK (1<<2)
  78. #define NODE_STR_MARGIN 16
  79. #define NODE_STR_BUF_SIZE 24 /* sizeof(CClassNode) - sizeof(int)*4 */
  80. #define NODE_BACKREFS_SIZE 6
  81. #define NSTR_RAW (1<<0) /* by backslashed number */
  82. #define NSTR_AMBIG (1<<1)
  83. #define NSTR_DONT_GET_OPT_INFO (1<<2)
  84. #define NSTRING_LEN(node) ((node)->u.str.end - (node)->u.str.s)
  85. #define NSTRING_SET_RAW(node) (node)->u.str.flag |= NSTR_RAW
  86. #define NSTRING_CLEAR_RAW(node) (node)->u.str.flag &= ~NSTR_RAW
  87. #define NSTRING_SET_AMBIG(node) (node)->u.str.flag |= NSTR_AMBIG
  88. #define NSTRING_SET_DONT_GET_OPT_INFO(node) \
  89. (node)->u.str.flag |= NSTR_DONT_GET_OPT_INFO
  90. #define NSTRING_IS_RAW(node) (((node)->u.str.flag & NSTR_RAW) != 0)
  91. #define NSTRING_IS_AMBIG(node) (((node)->u.str.flag & NSTR_AMBIG) != 0)
  92. #define NSTRING_IS_DONT_GET_OPT_INFO(node) \
  93. (((node)->u.str.flag & NSTR_DONT_GET_OPT_INFO) != 0)
  94. #define BACKREFS_P(br) \
  95. (IS_NOT_NULL((br)->back_dynamic) ? (br)->back_dynamic : (br)->back_static);
  96. #define NQ_TARGET_ISNOT_EMPTY 0
  97. #define NQ_TARGET_IS_EMPTY 1
  98. #define NQ_TARGET_IS_EMPTY_MEM 2
  99. #define NQ_TARGET_IS_EMPTY_REC 3
  100. /* status bits */
  101. #define NST_MIN_FIXED (1<<0)
  102. #define NST_MAX_FIXED (1<<1)
  103. #define NST_CLEN_FIXED (1<<2)
  104. #define NST_MARK1 (1<<3)
  105. #define NST_MARK2 (1<<4)
  106. #define NST_MEM_BACKREFED (1<<5)
  107. #define NST_STOP_BT_SIMPLE_REPEAT (1<<6)
  108. #define NST_RECURSION (1<<7)
  109. #define NST_CALLED (1<<8)
  110. #define NST_ADDR_FIXED (1<<9)
  111. #define NST_NAMED_GROUP (1<<10)
  112. #define NST_NAME_REF (1<<11)
  113. #define NST_IN_REPEAT (1<<12) /* STK_REPEAT is nested in stack. */
  114. #define NST_NEST_LEVEL (1<<13)
  115. #define NST_BY_NUMBER (1<<14) /* {n,m} */
  116. #define SET_ENCLOSE_STATUS(node,f) (node)->u.enclose.state |= (f)
  117. #define CLEAR_ENCLOSE_STATUS(node,f) (node)->u.enclose.state &= ~(f)
  118. #define IS_ENCLOSE_CALLED(en) (((en)->state & NST_CALLED) != 0)
  119. #define IS_ENCLOSE_ADDR_FIXED(en) (((en)->state & NST_ADDR_FIXED) != 0)
  120. #define IS_ENCLOSE_RECURSION(en) (((en)->state & NST_RECURSION) != 0)
  121. #define IS_ENCLOSE_MARK1(en) (((en)->state & NST_MARK1) != 0)
  122. #define IS_ENCLOSE_MARK2(en) (((en)->state & NST_MARK2) != 0)
  123. #define IS_ENCLOSE_MIN_FIXED(en) (((en)->state & NST_MIN_FIXED) != 0)
  124. #define IS_ENCLOSE_MAX_FIXED(en) (((en)->state & NST_MAX_FIXED) != 0)
  125. #define IS_ENCLOSE_CLEN_FIXED(en) (((en)->state & NST_CLEN_FIXED) != 0)
  126. #define IS_ENCLOSE_STOP_BT_SIMPLE_REPEAT(en) \
  127. (((en)->state & NST_STOP_BT_SIMPLE_REPEAT) != 0)
  128. #define IS_ENCLOSE_NAMED_GROUP(en) (((en)->state & NST_NAMED_GROUP) != 0)
  129. #define SET_CALL_RECURSION(node) (node)->u.call.state |= NST_RECURSION
  130. #define IS_CALL_RECURSION(cn) (((cn)->state & NST_RECURSION) != 0)
  131. #define IS_CALL_NAME_REF(cn) (((cn)->state & NST_NAME_REF) != 0)
  132. #define IS_BACKREF_NAME_REF(bn) (((bn)->state & NST_NAME_REF) != 0)
  133. #define IS_BACKREF_NEST_LEVEL(bn) (((bn)->state & NST_NEST_LEVEL) != 0)
  134. #define IS_QUANTIFIER_IN_REPEAT(qn) (((qn)->state & NST_IN_REPEAT) != 0)
  135. #define IS_QUANTIFIER_BY_NUMBER(qn) (((qn)->state & NST_BY_NUMBER) != 0)
  136. #define CALLNODE_REFNUM_UNDEF -1
  137. typedef struct {
  138. NodeBase base;
  139. UChar* s;
  140. UChar* end;
  141. unsigned int flag;
  142. int capa; /* (allocated size - 1) or 0: use buf[] */
  143. UChar buf[NODE_STR_BUF_SIZE];
  144. } StrNode;
  145. typedef struct {
  146. NodeBase base;
  147. int state;
  148. struct _Node* target;
  149. int lower;
  150. int upper;
  151. int greedy;
  152. int target_empty_info;
  153. struct _Node* head_exact;
  154. struct _Node* next_head_exact;
  155. int is_refered; /* include called node. don't eliminate even if {0} */
  156. #ifdef USE_COMBINATION_EXPLOSION_CHECK
  157. int comb_exp_check_num; /* 1,2,3...: check, 0: no check */
  158. #endif
  159. } QtfrNode;
  160. typedef struct {
  161. NodeBase base;
  162. int state;
  163. int type;
  164. int regnum;
  165. OnigOptionType option;
  166. struct _Node* target;
  167. AbsAddrType call_addr;
  168. /* for multiple call reference */
  169. OnigDistance min_len; /* min length (byte) */
  170. OnigDistance max_len; /* max length (byte) */
  171. int char_len; /* character length */
  172. int opt_count; /* referenced count in optimize_node_left() */
  173. } EncloseNode;
  174. #ifdef USE_SUBEXP_CALL
  175. typedef struct {
  176. int offset;
  177. struct _Node* target;
  178. } UnsetAddr;
  179. typedef struct {
  180. int num;
  181. int alloc;
  182. UnsetAddr* us;
  183. } UnsetAddrList;
  184. typedef struct {
  185. NodeBase base;
  186. int state;
  187. int group_num;
  188. UChar* name;
  189. UChar* name_end;
  190. struct _Node* target; /* EncloseNode : ENCLOSE_MEMORY */
  191. UnsetAddrList* unset_addr_list;
  192. } CallNode;
  193. #endif
  194. typedef struct {
  195. NodeBase base;
  196. int state;
  197. int back_num;
  198. int back_static[NODE_BACKREFS_SIZE];
  199. int* back_dynamic;
  200. int nest_level;
  201. } BRefNode;
  202. typedef struct {
  203. NodeBase base;
  204. int type;
  205. struct _Node* target;
  206. int char_len;
  207. } AnchorNode;
  208. typedef struct {
  209. NodeBase base;
  210. struct _Node* car;
  211. struct _Node* cdr;
  212. } ConsAltNode;
  213. typedef struct {
  214. NodeBase base;
  215. int ctype;
  216. int not;
  217. } CtypeNode;
  218. typedef struct _Node {
  219. union {
  220. NodeBase base;
  221. StrNode str;
  222. CClassNode cclass;
  223. QtfrNode qtfr;
  224. EncloseNode enclose;
  225. BRefNode bref;
  226. AnchorNode anchor;
  227. ConsAltNode cons;
  228. CtypeNode ctype;
  229. #ifdef USE_SUBEXP_CALL
  230. CallNode call;
  231. #endif
  232. } u;
  233. } Node;
  234. #define NULL_NODE ((Node* )0)
  235. #define SCANENV_MEMNODES_SIZE 8
  236. #define SCANENV_MEM_NODES(senv) \
  237. (IS_NOT_NULL((senv)->mem_nodes_dynamic) ? \
  238. (senv)->mem_nodes_dynamic : (senv)->mem_nodes_static)
  239. typedef struct {
  240. OnigOptionType option;
  241. OnigCaseFoldType case_fold_flag;
  242. OnigEncoding enc;
  243. OnigSyntaxType* syntax;
  244. BitStatusType capture_history;
  245. BitStatusType bt_mem_start;
  246. BitStatusType bt_mem_end;
  247. BitStatusType backrefed_mem;
  248. UChar* pattern;
  249. UChar* pattern_end;
  250. UChar* error;
  251. UChar* error_end;
  252. regex_t* reg; /* for reg->names only */
  253. int num_call;
  254. #ifdef USE_SUBEXP_CALL
  255. UnsetAddrList* unset_addr_list;
  256. #endif
  257. int num_mem;
  258. #ifdef USE_NAMED_GROUP
  259. int num_named;
  260. #endif
  261. int mem_alloc;
  262. Node* mem_nodes_static[SCANENV_MEMNODES_SIZE];
  263. Node** mem_nodes_dynamic;
  264. #ifdef USE_COMBINATION_EXPLOSION_CHECK
  265. int num_comb_exp_check;
  266. int comb_exp_max_regnum;
  267. int curr_max_regnum;
  268. int has_recursion;
  269. #endif
  270. } ScanEnv;
  271. #define IS_SYNTAX_OP(syn, opm) (((syn)->op & (opm)) != 0)
  272. #define IS_SYNTAX_OP2(syn, opm) (((syn)->op2 & (opm)) != 0)
  273. #define IS_SYNTAX_BV(syn, bvm) (((syn)->behavior & (bvm)) != 0)
  274. #ifdef USE_NAMED_GROUP
  275. typedef struct {
  276. int new_val;
  277. } GroupNumRemap;
  278. extern int onig_renumber_name_table P_((regex_t* reg, GroupNumRemap* map));
  279. #endif
  280. extern int onig_strncmp P_((const UChar* s1, const UChar* s2, int n));
  281. extern void onig_strcpy P_((UChar* dest, const UChar* src, const UChar* end));
  282. extern void onig_scan_env_set_error_string P_((ScanEnv* env, int ecode, UChar* arg, UChar* arg_end));
  283. extern int onig_scan_unsigned_number P_((UChar** src, const UChar* end, OnigEncoding enc));
  284. extern void onig_reduce_nested_quantifier P_((Node* pnode, Node* cnode));
  285. extern void onig_node_conv_to_str_node P_((Node* node, int raw));
  286. extern int onig_node_str_cat P_((Node* node, const UChar* s, const UChar* end));
  287. extern int onig_node_str_set P_((Node* node, const UChar* s, const UChar* end));
  288. extern void onig_node_free P_((Node* node));
  289. extern Node* onig_node_new_enclose P_((int type));
  290. extern Node* onig_node_new_anchor P_((int type));
  291. extern Node* onig_node_new_str P_((const UChar* s, const UChar* end));
  292. extern Node* onig_node_new_list P_((Node* left, Node* right));
  293. extern Node* onig_node_list_add P_((Node* list, Node* x));
  294. extern Node* onig_node_new_alt P_((Node* left, Node* right));
  295. extern void onig_node_str_clear P_((Node* node));
  296. extern int onig_free_node_list P_((void));
  297. extern int onig_names_free P_((regex_t* reg));
  298. extern int onig_parse_make_tree P_((Node** root, const UChar* pattern, const UChar* end, regex_t* reg, ScanEnv* env));
  299. extern int onig_free_shared_cclass_table P_((void));
  300. #ifdef ONIG_DEBUG
  301. #ifdef USE_NAMED_GROUP
  302. extern int onig_print_names(FILE*, regex_t*);
  303. #endif
  304. #endif
  305. #if (defined (__GNUC__) && __GNUC__ > 2 ) && !defined(DARWIN) && !defined(__hpux) && !defined(_AIX)
  306. # define UNEXPECTED(condition) __builtin_expect(condition, 0)
  307. #else
  308. # define UNEXPECTED(condition) (condition)
  309. #endif
  310. #define SAFE_ENC_LEN(enc, p, end, res) do { \
  311. int __res = enclen(enc, p); \
  312. if (UNEXPECTED(p + __res > end)) __res = end - p; \
  313. res = __res; \
  314. } while(0);
  315. #endif /* REGPARSE_H */