]> granicus.if.org Git - onig/blob - src/regint.h
implement absent group
[onig] / src / regint.h
1 #ifndef REGINT_H
2 #define REGINT_H
3 /**********************************************************************
4   regint.h -  Oniguruma (regular expression library)
5 **********************************************************************/
6 /*-
7  * Copyright (c) 2002-2017  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
32 /* for debug */
33 #define ONIG_DEBUG_PARSE_TREE
34 #define ONIG_DEBUG_COMPILE
35 /* #define ONIG_DEBUG_SEARCH */
36 /* #define ONIG_DEBUG_MATCH */
37 /* #define ONIG_DONT_OPTIMIZE */
38
39 /* for byte-code statistical data. */
40 /* #define ONIG_DEBUG_STATISTICS */
41
42 #if defined(ONIG_DEBUG_PARSE_TREE) || defined(ONIG_DEBUG_MATCH) || \
43     defined(ONIG_DEBUG_SEARCH) || defined(ONIG_DEBUG_COMPILE) || \
44     defined(ONIG_DEBUG_STATISTICS)
45 #ifndef ONIG_DEBUG
46 #define ONIG_DEBUG
47 #endif
48 #endif
49
50 #if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
51     (defined(__ppc__) && defined(__APPLE__)) || \
52     defined(__x86_64) || defined(__x86_64__) || \
53     defined(__mc68020__)
54 #define PLATFORM_UNALIGNED_WORD_ACCESS
55 #endif
56
57 /* config */
58 /* spec. config */
59 #define USE_NAMED_GROUP
60 #define USE_CALL
61 #define USE_BACKREF_WITH_LEVEL        /* \k<name+n>, \k<name-n> */
62 #define USE_INSISTENT_CHECK_CAPTURES_STATUS_IN_ENDLESS_REPEAT  /* /(?:()|())*\2/ */
63 #define USE_NEWLINE_AT_END_OF_STRING_HAS_EMPTY_LINE     /* /\n$/ =~ "\n" */
64 #define USE_WARNING_REDUNDANT_NESTED_REPEAT_OPERATOR
65 /* !!! moved to regenc.h. */ /* #define USE_CRNL_AS_LINE_TERMINATOR */
66
67 /* internal config */
68 #define USE_OP_PUSH_OR_JUMP_EXACT
69 #define USE_QUANT_PEEK_NEXT
70 #define USE_ST_LIBRARY
71
72 #define INIT_MATCH_STACK_SIZE                     160
73 #define DEFAULT_MATCH_STACK_LIMIT_SIZE              0 /* unlimited */
74 #define DEFAULT_PARSE_DEPTH_LIMIT                4096
75
76 #if defined(__GNUC__)
77 #  define ARG_UNUSED  __attribute__ ((unused))
78 #else
79 #  define ARG_UNUSED
80 #endif
81
82 /* */
83 /* escape other system UChar definition */
84 #include "config.h"
85 #ifdef ONIG_ESCAPE_UCHAR_COLLISION
86 #undef ONIG_ESCAPE_UCHAR_COLLISION
87 #endif
88
89 #define USE_WORD_BEGIN_END        /* "\<", "\>" */
90 #define USE_CAPTURE_HISTORY
91 #define USE_VARIABLE_META_CHARS
92 #define USE_POSIX_API_REGION_OPTION
93 #define USE_FIND_LONGEST_SEARCH_ALL_OF_RANGE
94 /* #define USE_COMBINATION_EXPLOSION_CHECK */     /* (X*)* */
95
96 #define xmalloc     malloc
97 #define xrealloc    realloc
98 #define xcalloc     calloc
99 #define xfree       free
100
101 #define CHECK_INTERRUPT_IN_MATCH_AT
102
103 #define st_init_table                  onig_st_init_table
104 #define st_init_table_with_size        onig_st_init_table_with_size
105 #define st_init_numtable               onig_st_init_numtable
106 #define st_init_numtable_with_size     onig_st_init_numtable_with_size
107 #define st_init_strtable               onig_st_init_strtable
108 #define st_init_strtable_with_size     onig_st_init_strtable_with_size
109 #define st_delete                      onig_st_delete
110 #define st_delete_safe                 onig_st_delete_safe
111 #define st_insert                      onig_st_insert
112 #define st_lookup                      onig_st_lookup
113 #define st_foreach                     onig_st_foreach
114 #define st_add_direct                  onig_st_add_direct
115 #define st_free_table                  onig_st_free_table
116 #define st_cleanup_safe                onig_st_cleanup_safe
117 #define st_copy                        onig_st_copy
118 #define st_nothing_key_clone           onig_st_nothing_key_clone
119 #define st_nothing_key_free            onig_st_nothing_key_free
120 /* */
121 #define onig_st_is_member              st_is_member
122
123 #define STATE_CHECK_STRING_THRESHOLD_LEN             7
124 #define STATE_CHECK_BUFF_MAX_SIZE               0x4000
125
126 #define xmemset     memset
127 #define xmemcpy     memcpy
128 #define xmemmove    memmove
129
130 #if defined(_WIN32) && !defined(__GNUC__)
131 #define xalloca     _alloca
132 #define xvsnprintf(buf,size,fmt,args)  _vsnprintf_s(buf,size,_TRUNCATE,fmt,args)
133 #define xsnprintf   sprintf_s
134 #define xstrcat(dest,src,size)   strcat_s(dest,size,src)
135 #else
136 #define xalloca     alloca
137 #define xvsnprintf  vsnprintf
138 #define xsnprintf   snprintf
139 #define xstrcat(dest,src,size)   strcat(dest,src)
140 #endif
141
142
143 #ifdef HAVE_STDLIB_H
144 #include <stdlib.h>
145 #endif
146
147 #if defined(HAVE_ALLOCA_H) && !defined(__GNUC__)
148 #include <alloca.h>
149 #endif
150
151 #ifdef HAVE_STRING_H
152 # include <string.h>
153 #else
154 # include <strings.h>
155 #endif
156
157 #include <ctype.h>
158 #ifdef HAVE_SYS_TYPES_H
159 #ifndef __BORLANDC__
160 #include <sys/types.h>
161 #endif
162 #endif
163
164 #ifdef HAVE_INTTYPES_H
165 #include <inttypes.h>
166 #endif
167
168 #ifdef __BORLANDC__
169 #include <malloc.h>
170 #endif
171
172 #ifdef ONIG_DEBUG
173 # include <stdio.h>
174 #endif
175
176 #ifdef _WIN32
177 #if defined(_MSC_VER) && (_MSC_VER < 1300)
178 typedef int intptr_t;
179 #endif
180 #endif
181
182 #include "regenc.h"
183
184 #ifdef MIN
185 #undef MIN
186 #endif
187 #ifdef MAX
188 #undef MAX
189 #endif
190 #define MIN(a,b) (((a)>(b))?(b):(a))
191 #define MAX(a,b) (((a)<(b))?(b):(a))
192
193 #define IS_NULL(p)                    (((void*)(p)) == (void*)0)
194 #define IS_NOT_NULL(p)                (((void*)(p)) != (void*)0)
195 #define CHECK_NULL_RETURN(p)          if (IS_NULL(p)) return NULL
196 #define CHECK_NULL_RETURN_MEMERR(p)   if (IS_NULL(p)) return ONIGERR_MEMORY
197 #define NULL_UCHARP                   ((UChar* )0)
198
199 #ifdef PLATFORM_UNALIGNED_WORD_ACCESS
200
201 #define PLATFORM_GET_INC(val,p,type) do{\
202   val  = *(type* )p;\
203   (p) += sizeof(type);\
204 } while(0)
205
206 #else
207
208 #define PLATFORM_GET_INC(val,p,type) do{\
209   xmemcpy(&val, (p), sizeof(type));\
210   (p) += sizeof(type);\
211 } while(0)
212
213 /* sizeof(OnigCodePoint) */
214 #define WORD_ALIGNMENT_SIZE     SIZEOF_LONG
215
216 #define GET_ALIGNMENT_PAD_SIZE(addr,pad_size) do {\
217   (pad_size) = WORD_ALIGNMENT_SIZE \
218                - ((unsigned int )(addr) % WORD_ALIGNMENT_SIZE);\
219   if ((pad_size) == WORD_ALIGNMENT_SIZE) (pad_size) = 0;\
220 } while (0)
221
222 #define ALIGNMENT_RIGHT(addr) do {\
223   (addr) += (WORD_ALIGNMENT_SIZE - 1);\
224   (addr) -= ((unsigned int )(addr) % WORD_ALIGNMENT_SIZE);\
225 } while (0)
226
227 #endif /* PLATFORM_UNALIGNED_WORD_ACCESS */
228
229 typedef struct {
230   int num_keeper;
231   int* keepers;
232 } RegExt;
233
234 #define REG_EXTP(reg)      (RegExt* )((reg)->chain)
235 #define REG_EXTPL(reg)     ((reg)->chain)
236
237 /* stack pop level */
238 #define STACK_POP_LEVEL_FREE        0
239 #define STACK_POP_LEVEL_MEM_START   1
240 #define STACK_POP_LEVEL_ALL         2
241
242 /* optimize flags */
243 #define ONIG_OPTIMIZE_NONE              0
244 #define ONIG_OPTIMIZE_EXACT             1   /* Slow Search */
245 #define ONIG_OPTIMIZE_EXACT_BM          2   /* Boyer Moore Search */
246 #define ONIG_OPTIMIZE_EXACT_BM_NOT_REV  3   /* BM   (but not simple match) */
247 #define ONIG_OPTIMIZE_EXACT_IC          4   /* Slow Search (ignore case) */
248 #define ONIG_OPTIMIZE_MAP               5   /* char map */
249
250 /* bit status */
251 typedef unsigned int  MemStatusType;
252
253 #define MEM_STATUS_BITS_NUM          (sizeof(MemStatusType) * 8)
254 #define MEM_STATUS_CLEAR(stats)      (stats) = 0
255 #define MEM_STATUS_ON_ALL(stats)     (stats) = ~((MemStatusType )0)
256 #define MEM_STATUS_AT(stats,n) \
257   ((n) < (int )MEM_STATUS_BITS_NUM  ?  ((stats) & ((MemStatusType )1 << n)) : ((stats) & 1))
258 #define MEM_STATUS_AT0(stats,n) \
259   ((n) > 0 && (n) < (int )MEM_STATUS_BITS_NUM  ?  ((stats) & ((MemStatusType )1 << n)) : ((stats) & 1))
260
261 #define MEM_STATUS_ON(stats,n) do {\
262   if ((n) < (int )MEM_STATUS_BITS_NUM) {\
263     if ((n) != 0)\
264       (stats) |= ((MemStatusType )1 << (n));\
265   }\
266   else\
267     (stats) |= 1;\
268 } while (0)
269
270 #define MEM_STATUS_ON_SIMPLE(stats,n) do {\
271     if ((n) < (int )MEM_STATUS_BITS_NUM)\
272     (stats) |= ((MemStatusType )1 << (n));\
273 } while (0)
274
275
276 #define INT_MAX_LIMIT           ((1UL << (SIZEOF_INT * 8 - 1)) - 1)
277
278 #define DIGITVAL(code)    ((code) - '0')
279 #define ODIGITVAL(code)   DIGITVAL(code)
280 #define XDIGITVAL(enc,code) \
281   (ONIGENC_IS_CODE_DIGIT(enc,code) ? DIGITVAL(code) \
282    : (ONIGENC_IS_CODE_UPPER(enc,code) ? (code) - 'A' + 10 : (code) - 'a' + 10))
283
284 #define IS_SINGLELINE(option)     ((option) & ONIG_OPTION_SINGLELINE)
285 #define IS_MULTILINE(option)      ((option) & ONIG_OPTION_MULTILINE)
286 #define IS_IGNORECASE(option)     ((option) & ONIG_OPTION_IGNORECASE)
287 #define IS_EXTEND(option)         ((option) & ONIG_OPTION_EXTEND)
288 #define IS_FIND_LONGEST(option)   ((option) & ONIG_OPTION_FIND_LONGEST)
289 #define IS_FIND_NOT_EMPTY(option) ((option) & ONIG_OPTION_FIND_NOT_EMPTY)
290 #define IS_FIND_CONDITION(option) ((option) & \
291           (ONIG_OPTION_FIND_LONGEST | ONIG_OPTION_FIND_NOT_EMPTY))
292 #define IS_NOTBOL(option)         ((option) & ONIG_OPTION_NOTBOL)
293 #define IS_NOTEOL(option)         ((option) & ONIG_OPTION_NOTEOL)
294 #define IS_POSIX_REGION(option)   ((option) & ONIG_OPTION_POSIX_REGION)
295
296 /* OP_SET_OPTION is required for these options.
297 #define IS_DYNAMIC_OPTION(option) \
298   (((option) & (ONIG_OPTION_MULTILINE | ONIG_OPTION_IGNORECASE)) != 0)
299 */
300 /* ignore-case and multibyte status are included in compiled code. */
301 #define IS_DYNAMIC_OPTION(option)  0
302
303 #define DISABLE_CASE_FOLD_MULTI_CHAR(case_fold_flag) \
304   ((case_fold_flag) & ~INTERNAL_ONIGENC_CASE_FOLD_MULTI_CHAR)
305
306 #define REPEAT_INFINITE         -1
307 #define IS_REPEAT_INFINITE(n)   ((n) == REPEAT_INFINITE)
308
309 /* bitset */
310 #define BITS_PER_BYTE      8
311 #define SINGLE_BYTE_SIZE   (1 << BITS_PER_BYTE)
312 #define BITS_IN_ROOM       (sizeof(Bits) * BITS_PER_BYTE)
313 #define BITSET_SIZE        (SINGLE_BYTE_SIZE / BITS_IN_ROOM)
314
315 #ifdef PLATFORM_UNALIGNED_WORD_ACCESS
316 typedef unsigned int   Bits;
317 #else
318 typedef unsigned char  Bits;
319 #endif
320 typedef Bits           BitSet[BITSET_SIZE];
321 typedef Bits*          BitSetRef;
322
323 #define SIZE_BITSET        sizeof(BitSet)
324
325 #define BITSET_CLEAR(bs) do {\
326   int i;\
327   for (i = 0; i < (int )BITSET_SIZE; i++) { (bs)[i] = 0; }      \
328 } while (0)
329
330 #define BS_ROOM(bs,pos)            (bs)[pos / BITS_IN_ROOM]
331 #define BS_BIT(pos)                (1 << (pos % BITS_IN_ROOM))
332
333 #define BITSET_AT(bs, pos)         (BS_ROOM(bs,pos) & BS_BIT(pos))
334 #define BITSET_SET_BIT(bs, pos)     BS_ROOM(bs,pos) |= BS_BIT(pos)
335 #define BITSET_CLEAR_BIT(bs, pos)   BS_ROOM(bs,pos) &= ~(BS_BIT(pos))
336 #define BITSET_INVERT_BIT(bs, pos)  BS_ROOM(bs,pos) ^= BS_BIT(pos)
337
338 /* bytes buffer */
339 typedef struct _BBuf {
340   UChar* p;
341   unsigned int used;
342   unsigned int alloc;
343 } BBuf;
344
345 #define BBUF_INIT(buf,size)    onig_bbuf_init((BBuf* )(buf), (size))
346
347 #define BBUF_SIZE_INC(buf,inc) do{\
348   (buf)->alloc += (inc);\
349   (buf)->p = (UChar* )xrealloc((buf)->p, (buf)->alloc);\
350   if (IS_NULL((buf)->p)) return(ONIGERR_MEMORY);\
351 } while (0)
352
353 #define BBUF_EXPAND(buf,low) do{\
354   do { (buf)->alloc *= 2; } while ((buf)->alloc < (unsigned int )low);\
355   (buf)->p = (UChar* )xrealloc((buf)->p, (buf)->alloc);\
356   if (IS_NULL((buf)->p)) return(ONIGERR_MEMORY);\
357 } while (0)
358
359 #define BBUF_ENSURE_SIZE(buf,size) do{\
360   unsigned int new_alloc = (buf)->alloc;\
361   while (new_alloc < (unsigned int )(size)) { new_alloc *= 2; }\
362   if ((buf)->alloc != new_alloc) {\
363     (buf)->p = (UChar* )xrealloc((buf)->p, new_alloc);\
364     if (IS_NULL((buf)->p)) return(ONIGERR_MEMORY);\
365     (buf)->alloc = new_alloc;\
366   }\
367 } while (0)
368
369 #define BBUF_WRITE(buf,pos,bytes,n) do{\
370   int used = (pos) + (n);\
371   if ((buf)->alloc < (unsigned int )used) BBUF_EXPAND((buf),used);\
372   xmemcpy((buf)->p + (pos), (bytes), (n));\
373   if ((buf)->used < (unsigned int )used) (buf)->used = used;\
374 } while (0)
375
376 #define BBUF_WRITE1(buf,pos,byte) do{\
377   int used = (pos) + 1;\
378   if ((buf)->alloc < (unsigned int )used) BBUF_EXPAND((buf),used);\
379   (buf)->p[(pos)] = (byte);\
380   if ((buf)->used < (unsigned int )used) (buf)->used = used;\
381 } while (0)
382
383 #define BBUF_ADD(buf,bytes,n)       BBUF_WRITE((buf),(buf)->used,(bytes),(n))
384 #define BBUF_ADD1(buf,byte)         BBUF_WRITE1((buf),(buf)->used,(byte))
385 #define BBUF_GET_ADD_ADDRESS(buf)   ((buf)->p + (buf)->used)
386 #define BBUF_GET_OFFSET_POS(buf)    ((buf)->used)
387
388 /* from < to */
389 #define BBUF_MOVE_RIGHT(buf,from,to,n) do {\
390   if ((unsigned int )((to)+(n)) > (buf)->alloc) BBUF_EXPAND((buf),(to) + (n));\
391   xmemmove((buf)->p + (to), (buf)->p + (from), (n));\
392   if ((unsigned int )((to)+(n)) > (buf)->used) (buf)->used = (to) + (n);\
393 } while (0)
394
395 /* from > to */
396 #define BBUF_MOVE_LEFT(buf,from,to,n) do {\
397   xmemmove((buf)->p + (to), (buf)->p + (from), (n));\
398 } while (0)
399
400 /* from > to */
401 #define BBUF_MOVE_LEFT_REDUCE(buf,from,to) do {\
402   xmemmove((buf)->p + (to), (buf)->p + (from), (buf)->used - (from));\
403   (buf)->used -= (from - to);\
404 } while (0)
405
406 #define BBUF_INSERT(buf,pos,bytes,n) do {\
407   if (pos >= (buf)->used) {\
408     BBUF_WRITE(buf,pos,bytes,n);\
409   }\
410   else {\
411     BBUF_MOVE_RIGHT((buf),(pos),(pos) + (n),((buf)->used - (pos)));\
412     xmemcpy((buf)->p + (pos), (bytes), (n));\
413   }\
414 } while (0)
415
416 #define BBUF_GET_BYTE(buf, pos) (buf)->p[(pos)]
417
418
419 /* has body */
420 #define ANCHOR_PREC_READ        (1<<0)
421 #define ANCHOR_PREC_READ_NOT    (1<<1)
422 #define ANCHOR_LOOK_BEHIND      (1<<2)
423 #define ANCHOR_LOOK_BEHIND_NOT  (1<<3)
424 /* no body */
425 #define ANCHOR_BEGIN_BUF        (1<<4)
426 #define ANCHOR_BEGIN_LINE       (1<<5)
427 #define ANCHOR_BEGIN_POSITION   (1<<6)
428 #define ANCHOR_END_BUF          (1<<7)
429 #define ANCHOR_SEMI_END_BUF     (1<<8)
430 #define ANCHOR_END_LINE         (1<<9)
431 #define ANCHOR_WORD_BOUND       (1<<10)
432 #define ANCHOR_NOT_WORD_BOUND   (1<<11)
433 #define ANCHOR_WORD_BEGIN       (1<<12)
434 #define ANCHOR_WORD_END         (1<<13)
435 #define ANCHOR_ANYCHAR_STAR     (1<<14)   /* ".*" optimize info */
436 #define ANCHOR_ANYCHAR_STAR_ML  (1<<15)   /* ".*" optimize info (multi-line) */
437
438 #define ANCHOR_HAS_BODY(a)      ((a)->type < ANCHOR_BEGIN_BUF)
439
440
441 /* operation code */
442 enum OpCode {
443   OP_FINISH = 0,        /* matching process terminator (no more alternative) */
444   OP_END    = 1,        /* pattern code terminator (success end) */
445
446   OP_EXACT1 = 2,        /* single byte, N = 1 */
447   OP_EXACT2,            /* single byte, N = 2 */
448   OP_EXACT3,            /* single byte, N = 3 */
449   OP_EXACT4,            /* single byte, N = 4 */
450   OP_EXACT5,            /* single byte, N = 5 */
451   OP_EXACTN,            /* single byte */
452   OP_EXACTMB2N1,        /* mb-length = 2 N = 1 */
453   OP_EXACTMB2N2,        /* mb-length = 2 N = 2 */
454   OP_EXACTMB2N3,        /* mb-length = 2 N = 3 */
455   OP_EXACTMB2N,         /* mb-length = 2 */
456   OP_EXACTMB3N,         /* mb-length = 3 */
457   OP_EXACTMBN,          /* other length */
458
459   OP_EXACT1_IC,         /* single byte, N = 1, ignore case */
460   OP_EXACTN_IC,         /* single byte,        ignore case */
461
462   OP_CCLASS,
463   OP_CCLASS_MB,
464   OP_CCLASS_MIX,
465   OP_CCLASS_NOT,
466   OP_CCLASS_MB_NOT,
467   OP_CCLASS_MIX_NOT,
468   OP_CCLASS_NODE,       /* pointer to CClassNode node */
469
470   OP_ANYCHAR,                 /* "."  */
471   OP_ANYCHAR_ML,              /* "."  multi-line */
472   OP_ANYCHAR_STAR,            /* ".*" */
473   OP_ANYCHAR_ML_STAR,         /* ".*" multi-line */
474   OP_ANYCHAR_STAR_PEEK_NEXT,
475   OP_ANYCHAR_ML_STAR_PEEK_NEXT,
476
477   OP_WORD,
478   OP_NOT_WORD,
479   OP_WORD_BOUND,
480   OP_NOT_WORD_BOUND,
481   OP_WORD_BEGIN,
482   OP_WORD_END,
483
484   OP_BEGIN_BUF,
485   OP_END_BUF,
486   OP_BEGIN_LINE,
487   OP_END_LINE,
488   OP_SEMI_END_BUF,
489   OP_BEGIN_POSITION,
490
491   OP_BACKREF1,
492   OP_BACKREF2,
493   OP_BACKREF_N,
494   OP_BACKREF_N_IC,
495   OP_BACKREF_MULTI,
496   OP_BACKREF_MULTI_IC,
497   OP_BACKREF_WITH_LEVEL,        /* \k<xxx+n>, \k<xxx-n> */
498   OP_BACKREF_CHECK,             /* (?(n)), (?('name')) */
499   OP_BACKREF_CHECK_WITH_LEVEL,  /* (?(n)), (?('name')) */
500
501   OP_MEMORY_START,
502   OP_MEMORY_START_PUSH,   /* push back-tracker to stack */
503   OP_MEMORY_END_PUSH,     /* push back-tracker to stack */
504   OP_MEMORY_END_PUSH_REC, /* push back-tracker to stack */
505   OP_MEMORY_END,
506   OP_MEMORY_END_REC,      /* push marker to stack */
507
508   OP_FAIL,               /* pop stack and move */
509   OP_JUMP,
510   OP_PUSH,
511   OP_POP,
512   OP_PUSH_OR_JUMP_EXACT1,  /* if match exact then push, else jump. */
513   OP_PUSH_IF_PEEK_NEXT,    /* if match exact then push, else none. */
514   OP_REPEAT,               /* {n,m} */
515   OP_REPEAT_NG,            /* {n,m}? (non greedy) */
516   OP_REPEAT_INC,
517   OP_REPEAT_INC_NG,        /* non greedy */
518   OP_REPEAT_INC_SG,        /* search and get in stack */
519   OP_REPEAT_INC_NG_SG,     /* search and get in stack (non greedy) */
520   OP_EMPTY_CHECK_START,     /* null loop checker start */
521   OP_EMPTY_CHECK_END,       /* null loop checker end   */
522   OP_EMPTY_CHECK_END_MEMST, /* null loop checker end (with capture status) */
523   OP_EMPTY_CHECK_END_MEMST_PUSH, /* with capture status and push check-end */
524
525   OP_PUSH_POS,             /* (?=...)  start */
526   OP_POP_POS,              /* (?=...)  end   */
527   OP_PUSH_PREC_READ_NOT,   /* (?!...)  start */
528   OP_FAIL_PREC_READ_NOT,   /* (?!...)  end   */
529   OP_PUSH_STOP_BT,         /* (?>...)  start */
530   OP_POP_STOP_BT,          /* (?>...)  end   */
531   OP_LOOK_BEHIND,          /* (?<=...) start (no needs end opcode) */
532   OP_PUSH_LOOK_BEHIND_NOT, /* (?<!...) start */
533   OP_FAIL_LOOK_BEHIND_NOT, /* (?<!...) end   */
534
535   OP_CALL,                 /* \g<name> */
536   OP_RETURN,
537   OP_PUSH_SAVE_VAL,
538   OP_UPDATE_VAR,
539
540   OP_STATE_CHECK_PUSH,         /* combination explosion check and push */
541   OP_STATE_CHECK_PUSH_OR_JUMP, /* check ok -> push, else jump  */
542   OP_STATE_CHECK,              /* check only */
543   OP_STATE_CHECK_ANYCHAR_STAR,
544   OP_STATE_CHECK_ANYCHAR_ML_STAR,
545
546   /* no need: IS_DYNAMIC_OPTION() == 0 */
547   OP_SET_OPTION_PUSH,    /* set option and push recover option */
548   OP_SET_OPTION          /* set option */
549 };
550
551 enum SaveType {
552   SAVE_KEEP = 0,
553   SAVE_RIGHT_RANGE = 1,
554 };
555
556 enum UpdateVarType {
557   UPDATE_VAR_KEEP_FROM_STACK_LAST = 0,
558   UPDATE_VAR_RIGHT_RANGE_FROM_STACK_LAST = 1,
559   UPDATE_VAR_RIGHT_RANGE_SPREV = 2,
560 };
561
562 typedef int RelAddrType;
563 typedef int AbsAddrType;
564 typedef int LengthType;
565 typedef int RepeatNumType;
566 typedef int MemNumType;
567 typedef short int StateCheckNumType;
568 typedef void* PointerType;
569 typedef int SaveType;
570 typedef int UpdateVarType;
571
572 #define SIZE_OPCODE           1
573 #define SIZE_RELADDR          sizeof(RelAddrType)
574 #define SIZE_ABSADDR          sizeof(AbsAddrType)
575 #define SIZE_LENGTH           sizeof(LengthType)
576 #define SIZE_MEMNUM           sizeof(MemNumType)
577 #define SIZE_STATE_CHECK_NUM  sizeof(StateCheckNumType)
578 #define SIZE_REPEATNUM        sizeof(RepeatNumType)
579 #define SIZE_OPTION           sizeof(OnigOptionType)
580 #define SIZE_CODE_POINT       sizeof(OnigCodePoint)
581 #define SIZE_POINTER          sizeof(PointerType)
582 #define SIZE_SAVE_TYPE        sizeof(SaveType)
583 #define SIZE_UPDATE_VAR_TYPE  sizeof(UpdateVarType)
584
585 #define GET_RELADDR_INC(addr,p)    PLATFORM_GET_INC(addr,   p, RelAddrType)
586 #define GET_ABSADDR_INC(addr,p)    PLATFORM_GET_INC(addr,   p, AbsAddrType)
587 #define GET_LENGTH_INC(len,p)      PLATFORM_GET_INC(len,    p, LengthType)
588 #define GET_MEMNUM_INC(num,p)      PLATFORM_GET_INC(num,    p, MemNumType)
589 #define GET_REPEATNUM_INC(num,p)   PLATFORM_GET_INC(num,    p, RepeatNumType)
590 #define GET_OPTION_INC(option,p)   PLATFORM_GET_INC(option, p, OnigOptionType)
591 #define GET_POINTER_INC(ptr,p)     PLATFORM_GET_INC(ptr,    p, PointerType)
592 #define GET_STATE_CHECK_NUM_INC(num,p)  PLATFORM_GET_INC(num, p, StateCheckNumType)
593 #define GET_SAVE_TYPE_INC(type,p)       PLATFORM_GET_INC(type, p, SaveType)
594 #define GET_UPDATE_VAR_TYPE_INC(type,p) PLATFORM_GET_INC(type, p, UpdateVarType)
595
596 /* code point's address must be aligned address. */
597 #define GET_CODE_POINT(code,p)   code = *((OnigCodePoint* )(p))
598 #define GET_BYTE_INC(byte,p) do{\
599   byte = *(p);\
600   (p)++;\
601 } while(0)
602
603
604 /* op-code + arg size */
605 #define SIZE_OP_ANYCHAR_STAR            SIZE_OPCODE
606 #define SIZE_OP_ANYCHAR_STAR_PEEK_NEXT (SIZE_OPCODE + 1)
607 #define SIZE_OP_JUMP                   (SIZE_OPCODE + SIZE_RELADDR)
608 #define SIZE_OP_PUSH                   (SIZE_OPCODE + SIZE_RELADDR)
609 #define SIZE_OP_POP                     SIZE_OPCODE
610 #define SIZE_OP_PUSH_OR_JUMP_EXACT1    (SIZE_OPCODE + SIZE_RELADDR + 1)
611 #define SIZE_OP_PUSH_IF_PEEK_NEXT      (SIZE_OPCODE + SIZE_RELADDR + 1)
612 #define SIZE_OP_REPEAT_INC             (SIZE_OPCODE + SIZE_MEMNUM)
613 #define SIZE_OP_REPEAT_INC_NG          (SIZE_OPCODE + SIZE_MEMNUM)
614 #define SIZE_OP_PUSH_POS                SIZE_OPCODE
615 #define SIZE_OP_PUSH_PREC_READ_NOT     (SIZE_OPCODE + SIZE_RELADDR)
616 #define SIZE_OP_POP_POS                 SIZE_OPCODE
617 #define SIZE_OP_FAIL_PREC_READ_NOT      SIZE_OPCODE
618 #define SIZE_OP_SET_OPTION             (SIZE_OPCODE + SIZE_OPTION)
619 #define SIZE_OP_SET_OPTION_PUSH        (SIZE_OPCODE + SIZE_OPTION)
620 #define SIZE_OP_FAIL                    SIZE_OPCODE
621 #define SIZE_OP_MEMORY_START           (SIZE_OPCODE + SIZE_MEMNUM)
622 #define SIZE_OP_MEMORY_START_PUSH      (SIZE_OPCODE + SIZE_MEMNUM)
623 #define SIZE_OP_MEMORY_END_PUSH        (SIZE_OPCODE + SIZE_MEMNUM)
624 #define SIZE_OP_MEMORY_END_PUSH_REC    (SIZE_OPCODE + SIZE_MEMNUM)
625 #define SIZE_OP_MEMORY_END             (SIZE_OPCODE + SIZE_MEMNUM)
626 #define SIZE_OP_MEMORY_END_REC         (SIZE_OPCODE + SIZE_MEMNUM)
627 #define SIZE_OP_PUSH_STOP_BT            SIZE_OPCODE
628 #define SIZE_OP_POP_STOP_BT             SIZE_OPCODE
629 #define SIZE_OP_EMPTY_CHECK_START       (SIZE_OPCODE + SIZE_MEMNUM)
630 #define SIZE_OP_EMPTY_CHECK_END         (SIZE_OPCODE + SIZE_MEMNUM)
631 #define SIZE_OP_LOOK_BEHIND            (SIZE_OPCODE + SIZE_LENGTH)
632 #define SIZE_OP_PUSH_LOOK_BEHIND_NOT   (SIZE_OPCODE + SIZE_RELADDR + SIZE_LENGTH)
633 #define SIZE_OP_FAIL_LOOK_BEHIND_NOT    SIZE_OPCODE
634 #define SIZE_OP_CALL                   (SIZE_OPCODE + SIZE_ABSADDR)
635 #define SIZE_OP_RETURN                  SIZE_OPCODE
636 #define SIZE_OP_PUSH_SAVE_VAL          (SIZE_OPCODE + SIZE_SAVE_TYPE + SIZE_MEMNUM)
637 #define SIZE_OP_UPDATE_VAR             (SIZE_OPCODE + SIZE_UPDATE_VAR_TYPE + SIZE_MEMNUM)
638
639 #ifdef USE_COMBINATION_EXPLOSION_CHECK
640 #define SIZE_OP_STATE_CHECK            (SIZE_OPCODE + SIZE_STATE_CHECK_NUM)
641 #define SIZE_OP_STATE_CHECK_PUSH       (SIZE_OPCODE + SIZE_STATE_CHECK_NUM + SIZE_RELADDR)
642 #define SIZE_OP_STATE_CHECK_PUSH_OR_JUMP (SIZE_OPCODE + SIZE_STATE_CHECK_NUM + SIZE_RELADDR)
643 #define SIZE_OP_STATE_CHECK_ANYCHAR_STAR (SIZE_OPCODE + SIZE_STATE_CHECK_NUM)
644 #endif
645
646 #define MC_ESC(syn)               (syn)->meta_char_table.esc
647 #define MC_ANYCHAR(syn)           (syn)->meta_char_table.anychar
648 #define MC_ANYTIME(syn)           (syn)->meta_char_table.anytime
649 #define MC_ZERO_OR_ONE_TIME(syn)  (syn)->meta_char_table.zero_or_one_time
650 #define MC_ONE_OR_MORE_TIME(syn)  (syn)->meta_char_table.one_or_more_time
651 #define MC_ANYCHAR_ANYTIME(syn)   (syn)->meta_char_table.anychar_anytime
652
653 #define IS_MC_ESC_CODE(code, syn) \
654   ((code) == MC_ESC(syn) && \
655    !IS_SYNTAX_OP2((syn), ONIG_SYN_OP2_INEFFECTIVE_ESCAPE))
656
657
658 #define SYN_POSIX_COMMON_OP \
659  ( ONIG_SYN_OP_DOT_ANYCHAR | ONIG_SYN_OP_POSIX_BRACKET | \
660    ONIG_SYN_OP_DECIMAL_BACKREF | \
661    ONIG_SYN_OP_BRACKET_CC | ONIG_SYN_OP_ASTERISK_ZERO_INF | \
662    ONIG_SYN_OP_LINE_ANCHOR | \
663    ONIG_SYN_OP_ESC_CONTROL_CHARS )
664
665 #define SYN_GNU_REGEX_OP \
666   ( ONIG_SYN_OP_DOT_ANYCHAR | ONIG_SYN_OP_BRACKET_CC | \
667     ONIG_SYN_OP_POSIX_BRACKET | ONIG_SYN_OP_DECIMAL_BACKREF | \
668     ONIG_SYN_OP_BRACE_INTERVAL | ONIG_SYN_OP_LPAREN_SUBEXP | \
669     ONIG_SYN_OP_VBAR_ALT | \
670     ONIG_SYN_OP_ASTERISK_ZERO_INF | ONIG_SYN_OP_PLUS_ONE_INF | \
671     ONIG_SYN_OP_QMARK_ZERO_ONE | \
672     ONIG_SYN_OP_ESC_AZ_BUF_ANCHOR | ONIG_SYN_OP_ESC_CAPITAL_G_BEGIN_ANCHOR | \
673     ONIG_SYN_OP_ESC_W_WORD | \
674     ONIG_SYN_OP_ESC_B_WORD_BOUND | ONIG_SYN_OP_ESC_LTGT_WORD_BEGIN_END | \
675     ONIG_SYN_OP_ESC_S_WHITE_SPACE | ONIG_SYN_OP_ESC_D_DIGIT | \
676     ONIG_SYN_OP_LINE_ANCHOR )
677
678 #define SYN_GNU_REGEX_BV \
679   ( ONIG_SYN_CONTEXT_INDEP_ANCHORS | ONIG_SYN_CONTEXT_INDEP_REPEAT_OPS | \
680     ONIG_SYN_CONTEXT_INVALID_REPEAT_OPS | ONIG_SYN_ALLOW_INVALID_INTERVAL | \
681     ONIG_SYN_BACKSLASH_ESCAPE_IN_CC | ONIG_SYN_ALLOW_DOUBLE_RANGE_OP_IN_CC )
682
683
684 #define NCCLASS_FLAGS(cc)           ((cc)->flags)
685 #define NCCLASS_FLAG_SET(cc,flag)    (NCCLASS_FLAGS(cc) |= (flag))
686 #define NCCLASS_FLAG_CLEAR(cc,flag)  (NCCLASS_FLAGS(cc) &= ~(flag))
687 #define IS_NCCLASS_FLAG_ON(cc,flag) ((NCCLASS_FLAGS(cc) & (flag)) != 0)
688
689 /* cclass node */
690 #define FLAG_NCCLASS_NOT           (1<<0)
691 #define FLAG_NCCLASS_SHARE         (1<<1)
692
693 #define NCCLASS_SET_NOT(nd)     NCCLASS_FLAG_SET(nd, FLAG_NCCLASS_NOT)
694 #define NCCLASS_CLEAR_NOT(nd)   NCCLASS_FLAG_CLEAR(nd, FLAG_NCCLASS_NOT)
695 #define IS_NCCLASS_NOT(nd)      IS_NCCLASS_FLAG_ON(nd, FLAG_NCCLASS_NOT)
696
697 typedef struct {
698   void* stack_p;
699   int   stack_n;
700   OnigOptionType options;
701   OnigRegion*    region;
702   int   ptr_num;
703   const UChar* start;   /* search start position (for \G: BEGIN_POSITION) */
704 #ifdef USE_FIND_LONGEST_SEARCH_ALL_OF_RANGE
705   int    best_len;      /* for ONIG_OPTION_FIND_LONGEST */
706   UChar* best_s;
707 #endif
708 #ifdef USE_COMBINATION_EXPLOSION_CHECK
709   void* state_check_buff;
710   int   state_check_buff_size;
711 #endif
712 } OnigMatchArg;
713
714
715 #define IS_CODE_SB_WORD(enc,code) \
716   (ONIGENC_IS_CODE_ASCII(code) && ONIGENC_IS_CODE_WORD(enc,code))
717
718 typedef struct OnigEndCallListItem {
719   struct OnigEndCallListItem* next;
720   void (*func)(void);
721 } OnigEndCallListItemType;
722
723 extern void onig_add_end_call(void (*func)(void));
724
725
726 #ifdef ONIG_DEBUG
727
728 typedef struct {
729   short int opcode;
730   char*     name;
731   short int arg_type;
732 } OnigOpInfoType;
733
734 extern OnigOpInfoType OnigOpInfo[];
735
736
737 extern void onig_print_compiled_byte_code P_((FILE* f, UChar* bp, UChar** nextp, UChar* start, OnigEncoding enc));
738
739 #ifdef ONIG_DEBUG_STATISTICS
740 extern void onig_statistics_init P_((void));
741 extern int  onig_print_statistics P_((FILE* f));
742 #endif
743 #endif
744
745 extern void onig_warning(const char* s);
746 extern UChar* onig_error_code_to_format P_((int code));
747 extern void  onig_snprintf_with_pattern PV_((UChar buf[], int bufsize, OnigEncoding enc, UChar* pat, UChar* pat_end, const UChar *fmt, ...));
748 extern int  onig_bbuf_init P_((BBuf* buf, int size));
749 extern int  onig_compile P_((regex_t* reg, const UChar* pattern, const UChar* pattern_end, OnigErrorInfo* einfo));
750 extern void onig_transfer P_((regex_t* to, regex_t* from));
751 extern int  onig_is_code_in_cc_len P_((int enclen, OnigCodePoint code, void* /* CClassNode* */ cc));
752
753 /* strend hash */
754 typedef void hash_table_type;
755 #ifdef _WIN32
756 # include <windows.h>
757 typedef ULONG_PTR hash_data_type;
758 #else
759 typedef unsigned long hash_data_type;
760 #endif
761
762 extern hash_table_type* onig_st_init_strend_table_with_size P_((int size));
763 extern int onig_st_lookup_strend P_((hash_table_type* table, const UChar* str_key, const UChar* end_key, hash_data_type *value));
764 extern int onig_st_insert_strend P_((hash_table_type* table, const UChar* str_key, const UChar* end_key, hash_data_type value));
765
766 typedef int (*ONIGENC_INIT_PROPERTY_LIST_FUNC_TYPE)(void);
767
768 #endif /* REGINT_H */