]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/RegularExpressionDxe/Oniguruma/regparse.c
4605d40bd12df6c786cdff78d6737f241b7c68a2
[mirror_edk2.git] / MdeModulePkg / Universal / RegularExpressionDxe / Oniguruma / regparse.c
1 /**********************************************************************
2 regparse.c - Oniguruma (regular expression library)
3 **********************************************************************/
4 /*-
5 * Copyright (c) 2002-2019 K.Kosako <sndgk393 AT ybb DOT ne DOT jp>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include "regparse.h"
31 #include "st.h"
32
33 #ifdef DEBUG_NODE_FREE
34 #include <stdio.h>
35 #endif
36
37 #define INIT_TAG_NAMES_ALLOC_NUM 5
38
39 #define WARN_BUFSIZE 256
40
41 #define CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS
42
43 #define IS_ALLOWED_CODE_IN_CALLOUT_NAME(c) \
44 ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '_' /* || c == '!' */)
45 #define IS_ALLOWED_CODE_IN_CALLOUT_TAG_NAME(c) \
46 ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '_')
47
48
49 OnigSyntaxType OnigSyntaxOniguruma = {
50 (( SYN_GNU_REGEX_OP | ONIG_SYN_OP_QMARK_NON_GREEDY |
51 ONIG_SYN_OP_ESC_OCTAL3 | ONIG_SYN_OP_ESC_X_HEX2 |
52 ONIG_SYN_OP_ESC_X_BRACE_HEX8 | ONIG_SYN_OP_ESC_O_BRACE_OCTAL |
53 ONIG_SYN_OP_ESC_CONTROL_CHARS |
54 ONIG_SYN_OP_ESC_C_CONTROL )
55 & ~ONIG_SYN_OP_ESC_LTGT_WORD_BEGIN_END )
56 , ( ONIG_SYN_OP2_QMARK_GROUP_EFFECT |
57 ONIG_SYN_OP2_OPTION_ONIGURUMA |
58 ONIG_SYN_OP2_QMARK_LT_NAMED_GROUP | ONIG_SYN_OP2_ESC_K_NAMED_BACKREF |
59 ONIG_SYN_OP2_QMARK_LPAREN_IF_ELSE |
60 ONIG_SYN_OP2_QMARK_TILDE_ABSENT_GROUP |
61 ONIG_SYN_OP2_QMARK_BRACE_CALLOUT_CONTENTS |
62 ONIG_SYN_OP2_ASTERISK_CALLOUT_NAME |
63 ONIG_SYN_OP2_ESC_X_Y_TEXT_SEGMENT |
64 ONIG_SYN_OP2_ESC_CAPITAL_R_GENERAL_NEWLINE |
65 ONIG_SYN_OP2_ESC_CAPITAL_N_O_SUPER_DOT |
66 ONIG_SYN_OP2_ESC_CAPITAL_K_KEEP |
67 ONIG_SYN_OP2_ESC_G_SUBEXP_CALL |
68 ONIG_SYN_OP2_ESC_P_BRACE_CHAR_PROPERTY |
69 ONIG_SYN_OP2_ESC_P_BRACE_CIRCUMFLEX_NOT |
70 ONIG_SYN_OP2_PLUS_POSSESSIVE_REPEAT |
71 ONIG_SYN_OP2_CCLASS_SET_OP | ONIG_SYN_OP2_ESC_CAPITAL_C_BAR_CONTROL |
72 ONIG_SYN_OP2_ESC_CAPITAL_M_BAR_META | ONIG_SYN_OP2_ESC_V_VTAB |
73 ONIG_SYN_OP2_ESC_H_XDIGIT | ONIG_SYN_OP2_ESC_U_HEX4 )
74 , ( SYN_GNU_REGEX_BV |
75 ONIG_SYN_ALLOW_INTERVAL_LOW_ABBREV |
76 ONIG_SYN_DIFFERENT_LEN_ALT_LOOK_BEHIND |
77 ONIG_SYN_CAPTURE_ONLY_NAMED_GROUP |
78 ONIG_SYN_ALLOW_MULTIPLEX_DEFINITION_NAME |
79 ONIG_SYN_FIXED_INTERVAL_IS_GREEDY_ONLY |
80 ONIG_SYN_ALLOW_INVALID_CODE_END_OF_RANGE_IN_CC |
81 ONIG_SYN_WARN_CC_OP_NOT_ESCAPED |
82 ONIG_SYN_WARN_REDUNDANT_NESTED_REPEAT )
83 , ONIG_OPTION_NONE
84 ,
85 {
86 (OnigCodePoint )'\\' /* esc */
87 , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* anychar '.' */
88 , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* anytime '*' */
89 , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* zero or one time '?' */
90 , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* one or more time '+' */
91 , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* anychar anytime */
92 }
93 };
94
95 OnigSyntaxType OnigSyntaxRuby = {
96 (( SYN_GNU_REGEX_OP | ONIG_SYN_OP_QMARK_NON_GREEDY |
97 ONIG_SYN_OP_ESC_OCTAL3 | ONIG_SYN_OP_ESC_X_HEX2 |
98 ONIG_SYN_OP_ESC_X_BRACE_HEX8 | ONIG_SYN_OP_ESC_O_BRACE_OCTAL |
99 ONIG_SYN_OP_ESC_CONTROL_CHARS |
100 ONIG_SYN_OP_ESC_C_CONTROL )
101 & ~ONIG_SYN_OP_ESC_LTGT_WORD_BEGIN_END )
102 , ( ONIG_SYN_OP2_QMARK_GROUP_EFFECT |
103 ONIG_SYN_OP2_OPTION_RUBY |
104 ONIG_SYN_OP2_QMARK_LT_NAMED_GROUP | ONIG_SYN_OP2_ESC_K_NAMED_BACKREF |
105 ONIG_SYN_OP2_QMARK_LPAREN_IF_ELSE |
106 ONIG_SYN_OP2_QMARK_TILDE_ABSENT_GROUP |
107 ONIG_SYN_OP2_ESC_X_Y_TEXT_SEGMENT |
108 ONIG_SYN_OP2_ESC_CAPITAL_R_GENERAL_NEWLINE |
109 ONIG_SYN_OP2_ESC_CAPITAL_K_KEEP |
110 ONIG_SYN_OP2_ESC_G_SUBEXP_CALL |
111 ONIG_SYN_OP2_ESC_P_BRACE_CHAR_PROPERTY |
112 ONIG_SYN_OP2_ESC_P_BRACE_CIRCUMFLEX_NOT |
113 ONIG_SYN_OP2_PLUS_POSSESSIVE_REPEAT |
114 ONIG_SYN_OP2_CCLASS_SET_OP | ONIG_SYN_OP2_ESC_CAPITAL_C_BAR_CONTROL |
115 ONIG_SYN_OP2_ESC_CAPITAL_M_BAR_META | ONIG_SYN_OP2_ESC_V_VTAB |
116 ONIG_SYN_OP2_ESC_H_XDIGIT | ONIG_SYN_OP2_ESC_U_HEX4 )
117 , ( SYN_GNU_REGEX_BV |
118 ONIG_SYN_ALLOW_INTERVAL_LOW_ABBREV |
119 ONIG_SYN_DIFFERENT_LEN_ALT_LOOK_BEHIND |
120 ONIG_SYN_CAPTURE_ONLY_NAMED_GROUP |
121 ONIG_SYN_ALLOW_MULTIPLEX_DEFINITION_NAME |
122 ONIG_SYN_FIXED_INTERVAL_IS_GREEDY_ONLY |
123 ONIG_SYN_WARN_CC_OP_NOT_ESCAPED |
124 ONIG_SYN_WARN_REDUNDANT_NESTED_REPEAT )
125 , ONIG_OPTION_NONE
126 ,
127 {
128 (OnigCodePoint )'\\' /* esc */
129 , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* anychar '.' */
130 , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* anytime '*' */
131 , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* zero or one time '?' */
132 , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* one or more time '+' */
133 , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* anychar anytime */
134 }
135 };
136
137 OnigSyntaxType* OnigDefaultSyntax = ONIG_SYNTAX_ONIGURUMA;
138
139 extern void onig_null_warn(const char* s ARG_UNUSED) { }
140
141 #ifdef DEFAULT_WARN_FUNCTION
142 static OnigWarnFunc onig_warn = (OnigWarnFunc )DEFAULT_WARN_FUNCTION;
143 #else
144 static OnigWarnFunc onig_warn = onig_null_warn;
145 #endif
146
147 #ifdef DEFAULT_VERB_WARN_FUNCTION
148 static OnigWarnFunc onig_verb_warn = (OnigWarnFunc )DEFAULT_VERB_WARN_FUNCTION;
149 #else
150 static OnigWarnFunc onig_verb_warn = onig_null_warn;
151 #endif
152
153 extern void onig_set_warn_func(OnigWarnFunc f)
154 {
155 onig_warn = f;
156 }
157
158 extern void onig_set_verb_warn_func(OnigWarnFunc f)
159 {
160 onig_verb_warn = f;
161 }
162
163 extern void
164 onig_warning(const char* s)
165 {
166 if (onig_warn == onig_null_warn) return ;
167
168 (*onig_warn)(s);
169 }
170
171 #define DEFAULT_MAX_CAPTURE_NUM 32767
172
173 static int MaxCaptureNum = DEFAULT_MAX_CAPTURE_NUM;
174
175 extern int
176 onig_set_capture_num_limit(int num)
177 {
178 if (num < 0) return -1;
179
180 MaxCaptureNum = num;
181 return 0;
182 }
183
184 static unsigned int ParseDepthLimit = DEFAULT_PARSE_DEPTH_LIMIT;
185
186 extern unsigned int
187 onig_get_parse_depth_limit(void)
188 {
189 return ParseDepthLimit;
190 }
191
192 extern int
193 onig_set_parse_depth_limit(unsigned int depth)
194 {
195 if (depth == 0)
196 ParseDepthLimit = DEFAULT_PARSE_DEPTH_LIMIT;
197 else
198 ParseDepthLimit = depth;
199 return 0;
200 }
201
202 static int
203 bbuf_init(BBuf* buf, int size)
204 {
205 if (size <= 0) {
206 size = 0;
207 buf->p = NULL;
208 }
209 else {
210 buf->p = (UChar* )xmalloc(size);
211 if (IS_NULL(buf->p)) return(ONIGERR_MEMORY);
212 }
213
214 buf->alloc = size;
215 buf->used = 0;
216 return 0;
217 }
218
219 static void
220 bbuf_free(BBuf* bbuf)
221 {
222 if (IS_NOT_NULL(bbuf)) {
223 if (IS_NOT_NULL(bbuf->p)) xfree(bbuf->p);
224 xfree(bbuf);
225 }
226 }
227
228 static int
229 bbuf_clone(BBuf** rto, BBuf* from)
230 {
231 int r;
232 BBuf *to;
233
234 *rto = to = (BBuf* )xmalloc(sizeof(BBuf));
235 CHECK_NULL_RETURN_MEMERR(to);
236 r = BB_INIT(to, from->alloc);
237 if (r != 0) {
238 xfree(to->p);
239 *rto = 0;
240 return r;
241 }
242 to->used = from->used;
243 xmemcpy(to->p, from->p, from->used);
244 return 0;
245 }
246
247 static int backref_rel_to_abs(int rel_no, ScanEnv* env)
248 {
249 if (rel_no > 0) {
250 return env->num_mem + rel_no;
251 }
252 else {
253 return env->num_mem + 1 + rel_no;
254 }
255 }
256
257 #define OPTION_ON(v,f) ((v) |= (f))
258 #define OPTION_OFF(v,f) ((v) &= ~(f))
259
260 #define OPTION_NEGATE(v,f,negative) (negative) ? ((v) &= ~(f)) : ((v) |= (f))
261
262 #define MBCODE_START_POS(enc) \
263 (OnigCodePoint )(ONIGENC_MBC_MINLEN(enc) > 1 ? 0 : 0x80)
264
265 #define SET_ALL_MULTI_BYTE_RANGE(enc, pbuf) \
266 add_code_range_to_buf(pbuf, MBCODE_START_POS(enc), ~((OnigCodePoint )0))
267
268 #define ADD_ALL_MULTI_BYTE_RANGE(enc, mbuf) do {\
269 if (! ONIGENC_IS_SINGLEBYTE(enc)) {\
270 r = SET_ALL_MULTI_BYTE_RANGE(enc, &(mbuf));\
271 if (r != 0) return r;\
272 }\
273 } while (0)
274
275
276 #define BITSET_IS_EMPTY(bs,empty) do {\
277 int i;\
278 empty = 1;\
279 for (i = 0; i < (int )BITSET_SIZE; i++) {\
280 if ((bs)[i] != 0) {\
281 empty = 0; break;\
282 }\
283 }\
284 } while (0)
285
286 static void
287 bitset_set_range(BitSetRef bs, int from, int to)
288 {
289 int i;
290 for (i = from; i <= to && i < SINGLE_BYTE_SIZE; i++) {
291 BITSET_SET_BIT(bs, i);
292 }
293 }
294
295 #if 0
296 static void
297 bitset_set_all(BitSetRef bs)
298 {
299 int i;
300 for (i = 0; i < BITSET_SIZE; i++) { bs[i] = ~((Bits )0); }
301 }
302 #endif
303
304 static void
305 bitset_invert(BitSetRef bs)
306 {
307 int i;
308 for (i = 0; i < (int )BITSET_SIZE; i++) { bs[i] = ~(bs[i]); }
309 }
310
311 static void
312 bitset_invert_to(BitSetRef from, BitSetRef to)
313 {
314 int i;
315 for (i = 0; i < (int )BITSET_SIZE; i++) { to[i] = ~(from[i]); }
316 }
317
318 static void
319 bitset_and(BitSetRef dest, BitSetRef bs)
320 {
321 int i;
322 for (i = 0; i < (int )BITSET_SIZE; i++) { dest[i] &= bs[i]; }
323 }
324
325 static void
326 bitset_or(BitSetRef dest, BitSetRef bs)
327 {
328 int i;
329 for (i = 0; i < (int )BITSET_SIZE; i++) { dest[i] |= bs[i]; }
330 }
331
332 static void
333 bitset_copy(BitSetRef dest, BitSetRef bs)
334 {
335 int i;
336 for (i = 0; i < (int )BITSET_SIZE; i++) { dest[i] = bs[i]; }
337 }
338
339 extern int
340 onig_strncmp(const UChar* s1, const UChar* s2, int n)
341 {
342 int x;
343
344 while (n-- > 0) {
345 x = *s2++ - *s1++;
346 if (x) return x;
347 }
348 return 0;
349 }
350
351 extern void
352 onig_strcpy(UChar* dest, const UChar* src, const UChar* end)
353 {
354 int len = (int )(end - src);
355 if (len > 0) {
356 xmemcpy(dest, src, len);
357 dest[len] = (UChar )0;
358 }
359 }
360
361 static int
362 save_entry(ScanEnv* env, enum SaveType type, int* id)
363 {
364 int nid = env->save_num;
365
366 #if 0
367 if (IS_NULL(env->saves)) {
368 int n = 10;
369 env->saves = (SaveItem* )xmalloc(sizeof(SaveItem) * n);
370 CHECK_NULL_RETURN_MEMERR(env->saves);
371 env->save_alloc_num = n;
372 }
373 else if (env->save_alloc_num <= nid) {
374 int n = env->save_alloc_num * 2;
375 SaveItem* p = (SaveItem* )xrealloc(env->saves, sizeof(SaveItem) * n, sizeof(SaveItem)*env->save_alloc_num);
376 CHECK_NULL_RETURN_MEMERR(p);
377 env->saves = p;
378 env->save_alloc_num = n;
379 }
380
381 env->saves[nid].type = type;
382 #endif
383
384 env->save_num++;
385 *id = nid;
386 return 0;
387 }
388
389 /* scan pattern methods */
390 #define PEND_VALUE 0
391
392 #define PFETCH_READY UChar* pfetch_prev
393 #define PEND (p < end ? 0 : 1)
394 #define PUNFETCH p = pfetch_prev
395 #define PINC do { \
396 pfetch_prev = p; \
397 p += ONIGENC_MBC_ENC_LEN(enc, p); \
398 } while (0)
399 #define PFETCH(c) do { \
400 c = ONIGENC_MBC_TO_CODE(enc, p, end); \
401 pfetch_prev = p; \
402 p += ONIGENC_MBC_ENC_LEN(enc, p); \
403 } while (0)
404
405 #define PINC_S do { \
406 p += ONIGENC_MBC_ENC_LEN(enc, p); \
407 } while (0)
408 #define PFETCH_S(c) do { \
409 c = ONIGENC_MBC_TO_CODE(enc, p, end); \
410 p += ONIGENC_MBC_ENC_LEN(enc, p); \
411 } while (0)
412
413 #define PPEEK (p < end ? ONIGENC_MBC_TO_CODE(enc, p, end) : PEND_VALUE)
414 #define PPEEK_IS(c) (PPEEK == (OnigCodePoint )c)
415
416 static UChar*
417 strcat_capa(UChar* dest, UChar* dest_end, const UChar* src, const UChar* src_end,
418 int capa, int oldCapa)
419 {
420 UChar* r;
421
422 if (dest)
423 r = (UChar* )xrealloc(dest, capa + 1, oldCapa);
424 else
425 r = (UChar* )xmalloc(capa + 1);
426
427 CHECK_NULL_RETURN(r);
428 onig_strcpy(r + (dest_end - dest), src, src_end);
429 return r;
430 }
431
432 /* dest on static area */
433 static UChar*
434 strcat_capa_from_static(UChar* dest, UChar* dest_end,
435 const UChar* src, const UChar* src_end, int capa)
436 {
437 UChar* r;
438
439 r = (UChar* )xmalloc(capa + 1);
440 CHECK_NULL_RETURN(r);
441 onig_strcpy(r, dest, dest_end);
442 onig_strcpy(r + (dest_end - dest), src, src_end);
443 return r;
444 }
445
446
447 #ifdef USE_ST_LIBRARY
448
449 typedef struct {
450 UChar* s;
451 UChar* end;
452 } st_str_end_key;
453
454 static int
455 str_end_cmp(st_str_end_key* x, st_str_end_key* y)
456 {
457 UChar *p, *q;
458 int c;
459
460 if ((x->end - x->s) != (y->end - y->s))
461 return 1;
462
463 p = x->s;
464 q = y->s;
465 while (p < x->end) {
466 c = (int )*p - (int )*q;
467 if (c != 0) return c;
468
469 p++; q++;
470 }
471
472 return 0;
473 }
474
475 static int
476 str_end_hash(st_str_end_key* x)
477 {
478 UChar *p;
479 int val = 0;
480
481 p = x->s;
482 while (p < x->end) {
483 val = val * 997 + (int )*p++;
484 }
485
486 return val + (val >> 5);
487 }
488
489 extern hash_table_type*
490 onig_st_init_strend_table_with_size(int size)
491 {
492 static struct st_hash_type hashType = {
493 str_end_cmp,
494 str_end_hash,
495 };
496
497 return (hash_table_type* )
498 onig_st_init_table_with_size(&hashType, size);
499 }
500
501 extern int
502 onig_st_lookup_strend(hash_table_type* table, const UChar* str_key,
503 const UChar* end_key, hash_data_type *value)
504 {
505 st_str_end_key key;
506
507 key.s = (UChar* )str_key;
508 key.end = (UChar* )end_key;
509
510 return onig_st_lookup(table, (st_data_t )(&key), value);
511 }
512
513 extern int
514 onig_st_insert_strend(hash_table_type* table, const UChar* str_key,
515 const UChar* end_key, hash_data_type value)
516 {
517 st_str_end_key* key;
518 int result;
519
520 key = (st_str_end_key* )xmalloc(sizeof(st_str_end_key));
521 CHECK_NULL_RETURN_MEMERR(key);
522
523 key->s = (UChar* )str_key;
524 key->end = (UChar* )end_key;
525 result = onig_st_insert(table, (st_data_t )key, value);
526 if (result) {
527 xfree(key);
528 }
529 return result;
530 }
531
532
533 #ifdef USE_CALLOUT
534
535 typedef struct {
536 OnigEncoding enc;
537 int type; /* callout type: single or not */
538 UChar* s;
539 UChar* end;
540 } st_callout_name_key;
541
542 static int
543 callout_name_table_cmp(st_callout_name_key* x, st_callout_name_key* y)
544 {
545 UChar *p, *q;
546 int c;
547
548 if (x->enc != y->enc) return 1;
549 if (x->type != y->type) return 1;
550 if ((x->end - x->s) != (y->end - y->s))
551 return 1;
552
553 p = x->s;
554 q = y->s;
555 while (p < x->end) {
556 c = (int )*p - (int )*q;
557 if (c != 0) return c;
558
559 p++; q++;
560 }
561
562 return 0;
563 }
564
565 static int
566 callout_name_table_hash(st_callout_name_key* x)
567 {
568 UChar *p;
569 int val = 0;
570
571 p = x->s;
572 while (p < x->end) {
573 val = val * 997 + (int )*p++;
574 }
575
576 /* use intptr_t for escape warning in Windows */
577 return val + (val >> 5) + ((intptr_t )x->enc & 0xffff) + x->type;
578 }
579
580 extern hash_table_type*
581 onig_st_init_callout_name_table_with_size(int size)
582 {
583 static struct st_hash_type hashType = {
584 callout_name_table_cmp,
585 callout_name_table_hash,
586 };
587
588 return (hash_table_type* )
589 onig_st_init_table_with_size(&hashType, size);
590 }
591
592 extern int
593 onig_st_lookup_callout_name_table(hash_table_type* table,
594 OnigEncoding enc,
595 int type,
596 const UChar* str_key,
597 const UChar* end_key,
598 hash_data_type *value)
599 {
600 st_callout_name_key key;
601
602 key.enc = enc;
603 key.type = type;
604 key.s = (UChar* )str_key;
605 key.end = (UChar* )end_key;
606
607 return onig_st_lookup(table, (st_data_t )(&key), value);
608 }
609
610 static int
611 st_insert_callout_name_table(hash_table_type* table,
612 OnigEncoding enc, int type,
613 UChar* str_key, UChar* end_key,
614 hash_data_type value)
615 {
616 st_callout_name_key* key;
617 int result;
618
619 key = (st_callout_name_key* )xmalloc(sizeof(st_callout_name_key));
620 CHECK_NULL_RETURN_MEMERR(key);
621
622 /* key->s: don't duplicate, because str_key is duped in callout_name_entry() */
623 key->enc = enc;
624 key->type = type;
625 key->s = str_key;
626 key->end = end_key;
627 result = onig_st_insert(table, (st_data_t )key, value);
628 if (result) {
629 xfree(key);
630 }
631 return result;
632 }
633 #endif
634
635 #endif /* USE_ST_LIBRARY */
636
637
638 #define INIT_NAME_BACKREFS_ALLOC_NUM 8
639
640 typedef struct {
641 UChar* name;
642 int name_len; /* byte length */
643 int back_num; /* number of backrefs */
644 int back_alloc;
645 int back_ref1;
646 int* back_refs;
647 } NameEntry;
648
649 #ifdef USE_ST_LIBRARY
650
651 #define INIT_NAMES_ALLOC_NUM 5
652
653 typedef st_table NameTable;
654 typedef st_data_t HashDataType; /* 1.6 st.h doesn't define st_data_t type */
655
656 #define NAMEBUF_SIZE 24
657 #define NAMEBUF_SIZE_1 25
658
659 #ifdef ONIG_DEBUG
660 static int
661 i_print_name_entry(UChar* key, NameEntry* e, void* arg)
662 {
663 int i;
664 FILE* fp = (FILE* )arg;
665
666 fprintf(fp, "%s: ", e->name);
667 if (e->back_num == 0)
668 fputs("-", fp);
669 else if (e->back_num == 1)
670 fprintf(fp, "%d", e->back_ref1);
671 else {
672 for (i = 0; i < e->back_num; i++) {
673 if (i > 0) fprintf(fp, ", ");
674 fprintf(fp, "%d", e->back_refs[i]);
675 }
676 }
677 fputs("\n", fp);
678 return ST_CONTINUE;
679 }
680
681 extern int
682 onig_print_names(FILE* fp, regex_t* reg)
683 {
684 NameTable* t = (NameTable* )reg->name_table;
685
686 if (IS_NOT_NULL(t)) {
687 fprintf(fp, "name table\n");
688 onig_st_foreach(t, i_print_name_entry, (HashDataType )fp);
689 fputs("\n", fp);
690 }
691 return 0;
692 }
693 #endif /* ONIG_DEBUG */
694
695 static int
696 i_free_name_entry(UChar* key, NameEntry* e, void* arg ARG_UNUSED)
697 {
698 xfree(e->name);
699 if (IS_NOT_NULL(e->back_refs)) xfree(e->back_refs);
700 xfree(key);
701 xfree(e);
702 return ST_DELETE;
703 }
704
705 static int
706 names_clear(regex_t* reg)
707 {
708 NameTable* t = (NameTable* )reg->name_table;
709
710 if (IS_NOT_NULL(t)) {
711 onig_st_foreach(t, i_free_name_entry, 0);
712 }
713 return 0;
714 }
715
716 extern int
717 onig_names_free(regex_t* reg)
718 {
719 int r;
720 NameTable* t;
721
722 r = names_clear(reg);
723 if (r != 0) return r;
724
725 t = (NameTable* )reg->name_table;
726 if (IS_NOT_NULL(t)) onig_st_free_table(t);
727 reg->name_table = (void* )NULL;
728 return 0;
729 }
730
731 static NameEntry*
732 name_find(regex_t* reg, const UChar* name, const UChar* name_end)
733 {
734 NameEntry* e;
735 NameTable* t = (NameTable* )reg->name_table;
736
737 e = (NameEntry* )NULL;
738 if (IS_NOT_NULL(t)) {
739 onig_st_lookup_strend(t, name, name_end, (HashDataType* )((void* )(&e)));
740 }
741 return e;
742 }
743
744 typedef struct {
745 int (*func)(const UChar*, const UChar*,int,int*,regex_t*,void*);
746 regex_t* reg;
747 void* arg;
748 int ret;
749 OnigEncoding enc;
750 } INamesArg;
751
752 static int
753 i_names(UChar* key ARG_UNUSED, NameEntry* e, INamesArg* arg)
754 {
755 int r = (*(arg->func))(e->name,
756 e->name + e->name_len,
757 e->back_num,
758 (e->back_num > 1 ? e->back_refs : &(e->back_ref1)),
759 arg->reg, arg->arg);
760 if (r != 0) {
761 arg->ret = r;
762 return ST_STOP;
763 }
764 return ST_CONTINUE;
765 }
766
767 extern int
768 onig_foreach_name(regex_t* reg,
769 int (*func)(const UChar*, const UChar*,int,int*,regex_t*,void*), void* arg)
770 {
771 INamesArg narg;
772 NameTable* t = (NameTable* )reg->name_table;
773
774 narg.ret = 0;
775 if (IS_NOT_NULL(t)) {
776 narg.func = func;
777 narg.reg = reg;
778 narg.arg = arg;
779 narg.enc = reg->enc; /* should be pattern encoding. */
780 onig_st_foreach(t, i_names, (HashDataType )&narg);
781 }
782 return narg.ret;
783 }
784
785 static int
786 i_renumber_name(UChar* key ARG_UNUSED, NameEntry* e, GroupNumRemap* map)
787 {
788 int i;
789
790 if (e->back_num > 1) {
791 for (i = 0; i < e->back_num; i++) {
792 e->back_refs[i] = map[e->back_refs[i]].new_val;
793 }
794 }
795 else if (e->back_num == 1) {
796 e->back_ref1 = map[e->back_ref1].new_val;
797 }
798
799 return ST_CONTINUE;
800 }
801
802 extern int
803 onig_renumber_name_table(regex_t* reg, GroupNumRemap* map)
804 {
805 NameTable* t = (NameTable* )reg->name_table;
806
807 if (IS_NOT_NULL(t)) {
808 onig_st_foreach(t, i_renumber_name, (HashDataType )map);
809 }
810 return 0;
811 }
812
813
814 extern int
815 onig_number_of_names(regex_t* reg)
816 {
817 NameTable* t = (NameTable* )reg->name_table;
818
819 if (IS_NOT_NULL(t))
820 return t->num_entries;
821 else
822 return 0;
823 }
824
825 #else /* USE_ST_LIBRARY */
826
827 #define INIT_NAMES_ALLOC_NUM 8
828
829 typedef struct {
830 NameEntry* e;
831 int num;
832 int alloc;
833 } NameTable;
834
835 #ifdef ONIG_DEBUG
836 extern int
837 onig_print_names(FILE* fp, regex_t* reg)
838 {
839 int i, j;
840 NameEntry* e;
841 NameTable* t = (NameTable* )reg->name_table;
842
843 if (IS_NOT_NULL(t) && t->num > 0) {
844 fprintf(fp, "name table\n");
845 for (i = 0; i < t->num; i++) {
846 e = &(t->e[i]);
847 fprintf(fp, "%s: ", e->name);
848 if (e->back_num == 0) {
849 fputs("-", fp);
850 }
851 else if (e->back_num == 1) {
852 fprintf(fp, "%d", e->back_ref1);
853 }
854 else {
855 for (j = 0; j < e->back_num; j++) {
856 if (j > 0) fprintf(fp, ", ");
857 fprintf(fp, "%d", e->back_refs[j]);
858 }
859 }
860 fputs("\n", fp);
861 }
862 fputs("\n", fp);
863 }
864 return 0;
865 }
866 #endif
867
868 static int
869 names_clear(regex_t* reg)
870 {
871 int i;
872 NameEntry* e;
873 NameTable* t = (NameTable* )reg->name_table;
874
875 if (IS_NOT_NULL(t)) {
876 for (i = 0; i < t->num; i++) {
877 e = &(t->e[i]);
878 if (IS_NOT_NULL(e->name)) {
879 xfree(e->name);
880 e->name = NULL;
881 e->name_len = 0;
882 e->back_num = 0;
883 e->back_alloc = 0;
884 if (IS_NOT_NULL(e->back_refs)) xfree(e->back_refs);
885 e->back_refs = (int* )NULL;
886 }
887 }
888 if (IS_NOT_NULL(t->e)) {
889 xfree(t->e);
890 t->e = NULL;
891 }
892 t->num = 0;
893 }
894 return 0;
895 }
896
897 extern int
898 onig_names_free(regex_t* reg)
899 {
900 int r;
901 NameTable* t;
902
903 r = names_clear(reg);
904 if (r != 0) return r;
905
906 t = (NameTable* )reg->name_table;
907 if (IS_NOT_NULL(t)) xfree(t);
908 reg->name_table = NULL;
909 return 0;
910 }
911
912 static NameEntry*
913 name_find(regex_t* reg, UChar* name, UChar* name_end)
914 {
915 int i, len;
916 NameEntry* e;
917 NameTable* t = (NameTable* )reg->name_table;
918
919 if (IS_NOT_NULL(t)) {
920 len = name_end - name;
921 for (i = 0; i < t->num; i++) {
922 e = &(t->e[i]);
923 if (len == e->name_len && onig_strncmp(name, e->name, len) == 0)
924 return e;
925 }
926 }
927 return (NameEntry* )NULL;
928 }
929
930 extern int
931 onig_foreach_name(regex_t* reg,
932 int (*func)(const UChar*, const UChar*,int,int*,regex_t*,void*), void* arg)
933 {
934 int i, r;
935 NameEntry* e;
936 NameTable* t = (NameTable* )reg->name_table;
937
938 if (IS_NOT_NULL(t)) {
939 for (i = 0; i < t->num; i++) {
940 e = &(t->e[i]);
941 r = (*func)(e->name, e->name + e->name_len, e->back_num,
942 (e->back_num > 1 ? e->back_refs : &(e->back_ref1)),
943 reg, arg);
944 if (r != 0) return r;
945 }
946 }
947 return 0;
948 }
949
950 extern int
951 onig_number_of_names(regex_t* reg)
952 {
953 NameTable* t = (NameTable* )reg->name_table;
954
955 if (IS_NOT_NULL(t))
956 return t->num;
957 else
958 return 0;
959 }
960
961 #endif /* else USE_ST_LIBRARY */
962
963 static int
964 name_add(regex_t* reg, UChar* name, UChar* name_end, int backref, ScanEnv* env)
965 {
966 int r;
967 int alloc;
968 NameEntry* e;
969 NameTable* t = (NameTable* )reg->name_table;
970
971 if (name_end - name <= 0)
972 return ONIGERR_EMPTY_GROUP_NAME;
973
974 e = name_find(reg, name, name_end);
975 if (IS_NULL(e)) {
976 #ifdef USE_ST_LIBRARY
977 if (IS_NULL(t)) {
978 t = onig_st_init_strend_table_with_size(INIT_NAMES_ALLOC_NUM);
979 CHECK_NULL_RETURN_MEMERR(t);
980 reg->name_table = (void* )t;
981 }
982 e = (NameEntry* )xmalloc(sizeof(NameEntry));
983 CHECK_NULL_RETURN_MEMERR(e);
984
985 e->name = onigenc_strdup(reg->enc, name, name_end);
986 if (IS_NULL(e->name)) {
987 xfree(e); return ONIGERR_MEMORY;
988 }
989 r = onig_st_insert_strend(t, e->name, (e->name + (name_end - name)),
990 (HashDataType )e);
991 if (r < 0) return r;
992
993 e->name_len = (int )(name_end - name);
994 e->back_num = 0;
995 e->back_alloc = 0;
996 e->back_refs = (int* )NULL;
997
998 #else
999
1000 if (IS_NULL(t)) {
1001 alloc = INIT_NAMES_ALLOC_NUM;
1002 t = (NameTable* )xmalloc(sizeof(NameTable));
1003 CHECK_NULL_RETURN_MEMERR(t);
1004 t->e = NULL;
1005 t->alloc = 0;
1006 t->num = 0;
1007
1008 t->e = (NameEntry* )xmalloc(sizeof(NameEntry) * alloc);
1009 if (IS_NULL(t->e)) {
1010 xfree(t);
1011 return ONIGERR_MEMORY;
1012 }
1013 t->alloc = alloc;
1014 reg->name_table = t;
1015 goto clear;
1016 }
1017 else if (t->num == t->alloc) {
1018 int i;
1019
1020 alloc = t->alloc * 2;
1021 t->e = (NameEntry* )xrealloc(t->e, sizeof(NameEntry) * alloc, sizeof(NameEntry) * t->alloc);
1022 CHECK_NULL_RETURN_MEMERR(t->e);
1023 t->alloc = alloc;
1024
1025 clear:
1026 for (i = t->num; i < t->alloc; i++) {
1027 t->e[i].name = NULL;
1028 t->e[i].name_len = 0;
1029 t->e[i].back_num = 0;
1030 t->e[i].back_alloc = 0;
1031 t->e[i].back_refs = (int* )NULL;
1032 }
1033 }
1034 e = &(t->e[t->num]);
1035 t->num++;
1036 e->name = onigenc_strdup(reg->enc, name, name_end);
1037 if (IS_NULL(e->name)) return ONIGERR_MEMORY;
1038 e->name_len = name_end - name;
1039 #endif
1040 }
1041
1042 if (e->back_num >= 1 &&
1043 ! IS_SYNTAX_BV(env->syntax, ONIG_SYN_ALLOW_MULTIPLEX_DEFINITION_NAME)) {
1044 onig_scan_env_set_error_string(env, ONIGERR_MULTIPLEX_DEFINED_NAME,
1045 name, name_end);
1046 return ONIGERR_MULTIPLEX_DEFINED_NAME;
1047 }
1048
1049 e->back_num++;
1050 if (e->back_num == 1) {
1051 e->back_ref1 = backref;
1052 }
1053 else {
1054 if (e->back_num == 2) {
1055 alloc = INIT_NAME_BACKREFS_ALLOC_NUM;
1056 e->back_refs = (int* )xmalloc(sizeof(int) * alloc);
1057 CHECK_NULL_RETURN_MEMERR(e->back_refs);
1058 e->back_alloc = alloc;
1059 e->back_refs[0] = e->back_ref1;
1060 e->back_refs[1] = backref;
1061 }
1062 else {
1063 if (e->back_num > e->back_alloc) {
1064 alloc = e->back_alloc * 2;
1065 e->back_refs = (int* )xrealloc(e->back_refs, sizeof(int) * alloc, sizeof(int) * e->back_alloc);
1066 CHECK_NULL_RETURN_MEMERR(e->back_refs);
1067 e->back_alloc = alloc;
1068 }
1069 e->back_refs[e->back_num - 1] = backref;
1070 }
1071 }
1072
1073 return 0;
1074 }
1075
1076 extern int
1077 onig_name_to_group_numbers(regex_t* reg, const UChar* name,
1078 const UChar* name_end, int** nums)
1079 {
1080 NameEntry* e = name_find(reg, name, name_end);
1081
1082 if (IS_NULL(e)) return ONIGERR_UNDEFINED_NAME_REFERENCE;
1083
1084 switch (e->back_num) {
1085 case 0:
1086 break;
1087 case 1:
1088 *nums = &(e->back_ref1);
1089 break;
1090 default:
1091 *nums = e->back_refs;
1092 break;
1093 }
1094 return e->back_num;
1095 }
1096
1097 static int
1098 name_to_group_numbers(ScanEnv* env, const UChar* name, const UChar* name_end,
1099 int** nums)
1100 {
1101 regex_t* reg;
1102 NameEntry* e;
1103
1104 reg = env->reg;
1105 e = name_find(reg, name, name_end);
1106
1107 if (IS_NULL(e)) {
1108 onig_scan_env_set_error_string(env, ONIGERR_UNDEFINED_NAME_REFERENCE,
1109 (UChar* )name, (UChar* )name_end);
1110 return ONIGERR_UNDEFINED_NAME_REFERENCE;
1111 }
1112
1113 switch (e->back_num) {
1114 case 0:
1115 break;
1116 case 1:
1117 *nums = &(e->back_ref1);
1118 break;
1119 default:
1120 *nums = e->back_refs;
1121 break;
1122 }
1123 return e->back_num;
1124 }
1125
1126 extern int
1127 onig_name_to_backref_number(regex_t* reg, const UChar* name,
1128 const UChar* name_end, OnigRegion *region)
1129 {
1130 int i, n, *nums;
1131
1132 n = onig_name_to_group_numbers(reg, name, name_end, &nums);
1133 if (n < 0)
1134 return n;
1135 else if (n == 0)
1136 return ONIGERR_PARSER_BUG;
1137 else if (n == 1)
1138 return nums[0];
1139 else {
1140 if (IS_NOT_NULL(region)) {
1141 for (i = n - 1; i >= 0; i--) {
1142 if (region->beg[nums[i]] != ONIG_REGION_NOTPOS)
1143 return nums[i];
1144 }
1145 }
1146 return nums[n - 1];
1147 }
1148 }
1149
1150 extern int
1151 onig_noname_group_capture_is_active(regex_t* reg)
1152 {
1153 if (ONIG_IS_OPTION_ON(reg->options, ONIG_OPTION_DONT_CAPTURE_GROUP))
1154 return 0;
1155
1156 if (onig_number_of_names(reg) > 0 &&
1157 IS_SYNTAX_BV(reg->syntax, ONIG_SYN_CAPTURE_ONLY_NAMED_GROUP) &&
1158 !ONIG_IS_OPTION_ON(reg->options, ONIG_OPTION_CAPTURE_GROUP)) {
1159 return 0;
1160 }
1161
1162 return 1;
1163 }
1164
1165 #ifdef USE_CALLOUT
1166
1167 typedef struct {
1168 OnigCalloutType type;
1169 int in;
1170 OnigCalloutFunc start_func;
1171 OnigCalloutFunc end_func;
1172 int arg_num;
1173 int opt_arg_num;
1174 unsigned int arg_types[ONIG_CALLOUT_MAX_ARGS_NUM];
1175 OnigValue opt_defaults[ONIG_CALLOUT_MAX_ARGS_NUM];
1176 UChar* name; /* reference to GlobalCalloutNameTable entry: e->name */
1177 } CalloutNameListEntry;
1178
1179 typedef struct {
1180 int n;
1181 int alloc;
1182 CalloutNameListEntry* v;
1183 } CalloutNameListType;
1184
1185 static CalloutNameListType* GlobalCalloutNameList;
1186
1187 static int
1188 make_callout_func_list(CalloutNameListType** rs, int init_size)
1189 {
1190 CalloutNameListType* s;
1191 CalloutNameListEntry* v;
1192
1193 *rs = 0;
1194
1195 s = xmalloc(sizeof(*s));
1196 if (IS_NULL(s)) return ONIGERR_MEMORY;
1197
1198 v = (CalloutNameListEntry* )xmalloc(sizeof(CalloutNameListEntry) * init_size);
1199 if (IS_NULL(v)) {
1200 xfree(s);
1201 return ONIGERR_MEMORY;
1202 }
1203
1204 s->n = 0;
1205 s->alloc = init_size;
1206 s->v = v;
1207
1208 *rs = s;
1209 return ONIG_NORMAL;
1210 }
1211
1212 static void
1213 free_callout_func_list(CalloutNameListType* s)
1214 {
1215 if (IS_NOT_NULL(s)) {
1216 if (IS_NOT_NULL(s->v)) {
1217 int i, j;
1218
1219 for (i = 0; i < s->n; i++) {
1220 CalloutNameListEntry* e = s->v + i;
1221 for (j = e->arg_num - e->opt_arg_num; j < e->arg_num; j++) {
1222 if (e->arg_types[j] == ONIG_TYPE_STRING) {
1223 UChar* p = e->opt_defaults[j].s.start;
1224 if (IS_NOT_NULL(p)) xfree(p);
1225 }
1226 }
1227 }
1228 xfree(s->v);
1229 }
1230 xfree(s);
1231 }
1232 }
1233
1234 static int
1235 callout_func_list_add(CalloutNameListType* s, int* rid)
1236 {
1237 if (s->n >= s->alloc) {
1238 int new_size = s->alloc * 2;
1239 CalloutNameListEntry* nv = (CalloutNameListEntry* )
1240 xrealloc(s->v, sizeof(CalloutNameListEntry) * new_size, sizeof(CalloutNameListEntry)*s->alloc);
1241 if (IS_NULL(nv)) return ONIGERR_MEMORY;
1242
1243 s->alloc = new_size;
1244 s->v = nv;
1245 }
1246
1247 *rid = s->n;
1248
1249 xmemset(&(s->v[s->n]), 0, sizeof(*(s->v)));
1250 s->n++;
1251 return ONIG_NORMAL;
1252 }
1253
1254
1255 typedef struct {
1256 UChar* name;
1257 int name_len; /* byte length */
1258 int id;
1259 } CalloutNameEntry;
1260
1261 #ifdef USE_ST_LIBRARY
1262 typedef st_table CalloutNameTable;
1263 #else
1264 typedef struct {
1265 CalloutNameEntry* e;
1266 int num;
1267 int alloc;
1268 } CalloutNameTable;
1269 #endif
1270
1271 static CalloutNameTable* GlobalCalloutNameTable;
1272 static int CalloutNameIDCounter;
1273
1274 #ifdef USE_ST_LIBRARY
1275
1276 static int
1277 i_free_callout_name_entry(st_callout_name_key* key, CalloutNameEntry* e,
1278 void* arg ARG_UNUSED)
1279 {
1280 xfree(e->name);
1281 /*xfree(key->s); */ /* is same as e->name */
1282 xfree(key);
1283 xfree(e);
1284 return ST_DELETE;
1285 }
1286
1287 static int
1288 callout_name_table_clear(CalloutNameTable* t)
1289 {
1290 if (IS_NOT_NULL(t)) {
1291 onig_st_foreach(t, i_free_callout_name_entry, 0);
1292 }
1293 return 0;
1294 }
1295
1296 static int
1297 global_callout_name_table_free(void)
1298 {
1299 if (IS_NOT_NULL(GlobalCalloutNameTable)) {
1300 int r = callout_name_table_clear(GlobalCalloutNameTable);
1301 if (r != 0) return r;
1302
1303 onig_st_free_table(GlobalCalloutNameTable);
1304 GlobalCalloutNameTable = 0;
1305 CalloutNameIDCounter = 0;
1306 }
1307
1308 return 0;
1309 }
1310
1311 static CalloutNameEntry*
1312 callout_name_find(OnigEncoding enc, int is_not_single,
1313 const UChar* name, const UChar* name_end)
1314 {
1315 int r;
1316 CalloutNameEntry* e;
1317 CalloutNameTable* t = GlobalCalloutNameTable;
1318
1319 e = (CalloutNameEntry* )NULL;
1320 if (IS_NOT_NULL(t)) {
1321 r = onig_st_lookup_callout_name_table(t, enc, is_not_single, name, name_end,
1322 (HashDataType* )((void* )(&e)));
1323 if (r == 0) { /* not found */
1324 if (enc != ONIG_ENCODING_ASCII &&
1325 ONIGENC_IS_ASCII_COMPATIBLE_ENCODING(enc)) {
1326 enc = ONIG_ENCODING_ASCII;
1327 onig_st_lookup_callout_name_table(t, enc, is_not_single, name, name_end,
1328 (HashDataType* )((void* )(&e)));
1329 }
1330 }
1331 }
1332 return e;
1333 }
1334
1335 #else
1336
1337 static int
1338 callout_name_table_clear(CalloutNameTable* t)
1339 {
1340 int i;
1341 CalloutNameEntry* e;
1342
1343 if (IS_NOT_NULL(t)) {
1344 for (i = 0; i < t->num; i++) {
1345 e = &(t->e[i]);
1346 if (IS_NOT_NULL(e->name)) {
1347 xfree(e->name);
1348 e->name = NULL;
1349 e->name_len = 0;
1350 e->id = 0;
1351 e->func = 0;
1352 }
1353 }
1354 if (IS_NOT_NULL(t->e)) {
1355 xfree(t->e);
1356 t->e = NULL;
1357 }
1358 t->num = 0;
1359 }
1360 return 0;
1361 }
1362
1363 static int
1364 global_callout_name_table_free(void)
1365 {
1366 if (IS_NOT_NULL(GlobalCalloutNameTable)) {
1367 int r = callout_name_table_clear(GlobalCalloutNameTable);
1368 if (r != 0) return r;
1369
1370 xfree(GlobalCalloutNameTable);
1371 GlobalCalloutNameTable = 0;
1372 CalloutNameIDCounter = 0;
1373 }
1374 return 0;
1375 }
1376
1377 static CalloutNameEntry*
1378 callout_name_find(UChar* name, UChar* name_end)
1379 {
1380 int i, len;
1381 CalloutNameEntry* e;
1382 CalloutNameTable* t = Calloutnames;
1383
1384 if (IS_NOT_NULL(t)) {
1385 len = name_end - name;
1386 for (i = 0; i < t->num; i++) {
1387 e = &(t->e[i]);
1388 if (len == e->name_len && onig_strncmp(name, e->name, len) == 0)
1389 return e;
1390 }
1391 }
1392 return (CalloutNameEntry* )NULL;
1393 }
1394
1395 #endif
1396
1397 /* name string must be single byte char string. */
1398 static int
1399 callout_name_entry(CalloutNameEntry** rentry, OnigEncoding enc,
1400 int is_not_single, UChar* name, UChar* name_end)
1401 {
1402 int r;
1403 CalloutNameEntry* e;
1404 CalloutNameTable* t = GlobalCalloutNameTable;
1405
1406 *rentry = 0;
1407 if (name_end - name <= 0)
1408 return ONIGERR_INVALID_CALLOUT_NAME;
1409
1410 e = callout_name_find(enc, is_not_single, name, name_end);
1411 if (IS_NULL(e)) {
1412 #ifdef USE_ST_LIBRARY
1413 if (IS_NULL(t)) {
1414 t = onig_st_init_callout_name_table_with_size(INIT_NAMES_ALLOC_NUM);
1415 CHECK_NULL_RETURN_MEMERR(t);
1416 GlobalCalloutNameTable = t;
1417 }
1418 e = (CalloutNameEntry* )xmalloc(sizeof(CalloutNameEntry));
1419 CHECK_NULL_RETURN_MEMERR(e);
1420
1421 e->name = onigenc_strdup(enc, name, name_end);
1422 if (IS_NULL(e->name)) {
1423 xfree(e); return ONIGERR_MEMORY;
1424 }
1425
1426 r = st_insert_callout_name_table(t, enc, is_not_single,
1427 e->name, (e->name + (name_end - name)),
1428 (HashDataType )e);
1429 if (r < 0) return r;
1430
1431 #else
1432
1433 int alloc;
1434
1435 if (IS_NULL(t)) {
1436 alloc = INIT_NAMES_ALLOC_NUM;
1437 t = (CalloutNameTable* )xmalloc(sizeof(CalloutNameTable));
1438 CHECK_NULL_RETURN_MEMERR(t);
1439 t->e = NULL;
1440 t->alloc = 0;
1441 t->num = 0;
1442
1443 t->e = (CalloutNameEntry* )xmalloc(sizeof(CalloutNameEntry) * alloc);
1444 if (IS_NULL(t->e)) {
1445 xfree(t);
1446 return ONIGERR_MEMORY;
1447 }
1448 t->alloc = alloc;
1449 GlobalCalloutNameTable = t;
1450 goto clear;
1451 }
1452 else if (t->num == t->alloc) {
1453 int i;
1454
1455 alloc = t->alloc * 2;
1456 t->e = (CalloutNameEntry* )xrealloc(t->e, sizeof(CalloutNameEntry) * alloc, sizeof(CalloutNameEntry)*t->alloc);
1457 CHECK_NULL_RETURN_MEMERR(t->e);
1458 t->alloc = alloc;
1459
1460 clear:
1461 for (i = t->num; i < t->alloc; i++) {
1462 t->e[i].name = NULL;
1463 t->e[i].name_len = 0;
1464 t->e[i].id = 0;
1465 }
1466 }
1467 e = &(t->e[t->num]);
1468 t->num++;
1469 e->name = onigenc_strdup(enc, name, name_end);
1470 if (IS_NULL(e->name)) return ONIGERR_MEMORY;
1471 #endif
1472
1473 CalloutNameIDCounter++;
1474 e->id = CalloutNameIDCounter;
1475 e->name_len = (int )(name_end - name);
1476 }
1477
1478 *rentry = e;
1479 return e->id;
1480 }
1481
1482 static int
1483 is_allowed_callout_name(OnigEncoding enc, UChar* name, UChar* name_end)
1484 {
1485 UChar* p;
1486 OnigCodePoint c;
1487
1488 if (name >= name_end) return 0;
1489
1490 p = name;
1491 while (p < name_end) {
1492 c = ONIGENC_MBC_TO_CODE(enc, p, name_end);
1493 if (! IS_ALLOWED_CODE_IN_CALLOUT_NAME(c))
1494 return 0;
1495
1496 if (p == name) {
1497 if (c >= '0' && c <= '9') return 0;
1498 }
1499
1500 p += ONIGENC_MBC_ENC_LEN(enc, p);
1501 }
1502
1503 return 1;
1504 }
1505
1506 static int
1507 is_allowed_callout_tag_name(OnigEncoding enc, UChar* name, UChar* name_end)
1508 {
1509 UChar* p;
1510 OnigCodePoint c;
1511
1512 if (name >= name_end) return 0;
1513
1514 p = name;
1515 while (p < name_end) {
1516 c = ONIGENC_MBC_TO_CODE(enc, p, name_end);
1517 if (! IS_ALLOWED_CODE_IN_CALLOUT_TAG_NAME(c))
1518 return 0;
1519
1520 if (p == name) {
1521 if (c >= '0' && c <= '9') return 0;
1522 }
1523
1524 p += ONIGENC_MBC_ENC_LEN(enc, p);
1525 }
1526
1527 return 1;
1528 }
1529
1530 extern int
1531 onig_set_callout_of_name(OnigEncoding enc, OnigCalloutType callout_type,
1532 UChar* name, UChar* name_end, int in,
1533 OnigCalloutFunc start_func,
1534 OnigCalloutFunc end_func,
1535 int arg_num, unsigned int arg_types[],
1536 int opt_arg_num, OnigValue opt_defaults[])
1537 {
1538 int r;
1539 int i;
1540 int j;
1541 int id;
1542 int is_not_single;
1543 CalloutNameEntry* e;
1544 CalloutNameListEntry* fe;
1545
1546 if (callout_type != ONIG_CALLOUT_TYPE_SINGLE)
1547 return ONIGERR_INVALID_ARGUMENT;
1548
1549 if (arg_num < 0 || arg_num > ONIG_CALLOUT_MAX_ARGS_NUM)
1550 return ONIGERR_INVALID_CALLOUT_ARG;
1551
1552 if (opt_arg_num < 0 || opt_arg_num > arg_num)
1553 return ONIGERR_INVALID_CALLOUT_ARG;
1554
1555 if (start_func == 0 && end_func == 0)
1556 return ONIGERR_INVALID_CALLOUT_ARG;
1557
1558 if ((in & ONIG_CALLOUT_IN_PROGRESS) == 0 && (in & ONIG_CALLOUT_IN_RETRACTION) == 0)
1559 return ONIGERR_INVALID_CALLOUT_ARG;
1560
1561 for (i = 0; i < arg_num; i++) {
1562 unsigned int t = arg_types[i];
1563 if (t == ONIG_TYPE_VOID)
1564 return ONIGERR_INVALID_CALLOUT_ARG;
1565 else {
1566 if (i >= arg_num - opt_arg_num) {
1567 if (t != ONIG_TYPE_LONG && t != ONIG_TYPE_CHAR && t != ONIG_TYPE_STRING &&
1568 t != ONIG_TYPE_TAG)
1569 return ONIGERR_INVALID_CALLOUT_ARG;
1570 }
1571 else {
1572 if (t != ONIG_TYPE_LONG) {
1573 t = t & ~ONIG_TYPE_LONG;
1574 if (t != ONIG_TYPE_CHAR && t != ONIG_TYPE_STRING && t != ONIG_TYPE_TAG)
1575 return ONIGERR_INVALID_CALLOUT_ARG;
1576 }
1577 }
1578 }
1579 }
1580
1581 if (! is_allowed_callout_name(enc, name, name_end)) {
1582 return ONIGERR_INVALID_CALLOUT_NAME;
1583 }
1584
1585 is_not_single = (callout_type != ONIG_CALLOUT_TYPE_SINGLE);
1586 id = callout_name_entry(&e, enc, is_not_single, name, name_end);
1587 if (id < 0) return id;
1588
1589 r = ONIG_NORMAL;
1590 if (IS_NULL(GlobalCalloutNameList)) {
1591 r = make_callout_func_list(&GlobalCalloutNameList, 10);
1592 if (r != ONIG_NORMAL) return r;
1593 }
1594
1595 while (id >= GlobalCalloutNameList->n) {
1596 int rid;
1597 r = callout_func_list_add(GlobalCalloutNameList, &rid);
1598 if (r != ONIG_NORMAL) return r;
1599 }
1600
1601 fe = GlobalCalloutNameList->v + id;
1602 fe->type = callout_type;
1603 fe->in = in;
1604 fe->start_func = start_func;
1605 fe->end_func = end_func;
1606 fe->arg_num = arg_num;
1607 fe->opt_arg_num = opt_arg_num;
1608 fe->name = e->name;
1609
1610 for (i = 0; i < arg_num; i++) {
1611 fe->arg_types[i] = arg_types[i];
1612 }
1613 for (i = arg_num - opt_arg_num, j = 0; i < arg_num; i++, j++) {
1614 if(IS_NULL(opt_defaults))return ONIGERR_INVALID_ARGUMENT;
1615 if (fe->arg_types[i] == ONIG_TYPE_STRING) {
1616 OnigValue* val;
1617 UChar* ds;
1618
1619 if (IS_NULL(opt_defaults)) return ONIGERR_INVALID_ARGUMENT;
1620
1621 val = opt_defaults + j;
1622 ds = onigenc_strdup(enc, val->s.start, val->s.end);
1623 CHECK_NULL_RETURN_MEMERR(ds);
1624
1625 fe->opt_defaults[i].s.start = ds;
1626 fe->opt_defaults[i].s.end = ds + (val->s.end - val->s.start);
1627 }
1628 else {
1629 fe->opt_defaults[i] = opt_defaults[j];
1630 }
1631 }
1632
1633 r = id;
1634 return r;
1635 }
1636
1637 static int
1638 get_callout_name_id_by_name(OnigEncoding enc, int is_not_single,
1639 UChar* name, UChar* name_end, int* rid)
1640 {
1641 int r;
1642 CalloutNameEntry* e;
1643
1644 if (! is_allowed_callout_name(enc, name, name_end)) {
1645 return ONIGERR_INVALID_CALLOUT_NAME;
1646 }
1647
1648 e = callout_name_find(enc, is_not_single, name, name_end);
1649 if (IS_NULL(e)) {
1650 return ONIGERR_UNDEFINED_CALLOUT_NAME;
1651 }
1652
1653 r = ONIG_NORMAL;
1654 *rid = e->id;
1655
1656 return r;
1657 }
1658
1659 extern OnigCalloutFunc
1660 onig_get_callout_start_func(regex_t* reg, int callout_num)
1661 {
1662 /* If used for callouts of contents, return 0. */
1663 CalloutListEntry* e;
1664
1665 e = onig_reg_callout_list_at(reg, callout_num);
1666 CHECK_NULL_RETURN(e);
1667 return e->start_func;
1668 }
1669
1670 extern const UChar*
1671 onig_get_callout_tag_start(regex_t* reg, int callout_num)
1672 {
1673 CalloutListEntry* e = onig_reg_callout_list_at(reg, callout_num);
1674 CHECK_NULL_RETURN(e);
1675 return e->tag_start;
1676 }
1677
1678 extern const UChar*
1679 onig_get_callout_tag_end(regex_t* reg, int callout_num)
1680 {
1681 CalloutListEntry* e = onig_reg_callout_list_at(reg, callout_num);
1682 CHECK_NULL_RETURN(e);
1683 return e->tag_end;
1684 }
1685
1686
1687 extern OnigCalloutType
1688 onig_get_callout_type_by_name_id(int name_id)
1689 {
1690 if (name_id < 0 || name_id >= GlobalCalloutNameList->n)
1691 return 0;
1692
1693 return GlobalCalloutNameList->v[name_id].type;
1694 }
1695
1696 extern OnigCalloutFunc
1697 onig_get_callout_start_func_by_name_id(int name_id)
1698 {
1699 if (name_id < 0 || name_id >= GlobalCalloutNameList->n)
1700 return 0;
1701
1702 return GlobalCalloutNameList->v[name_id].start_func;
1703 }
1704
1705 extern OnigCalloutFunc
1706 onig_get_callout_end_func_by_name_id(int name_id)
1707 {
1708 if (name_id < 0 || name_id >= GlobalCalloutNameList->n)
1709 return 0;
1710
1711 return GlobalCalloutNameList->v[name_id].end_func;
1712 }
1713
1714 extern int
1715 onig_get_callout_in_by_name_id(int name_id)
1716 {
1717 if (name_id < 0 || name_id >= GlobalCalloutNameList->n)
1718 return 0;
1719
1720 return GlobalCalloutNameList->v[name_id].in;
1721 }
1722
1723 static int
1724 get_callout_arg_num_by_name_id(int name_id)
1725 {
1726 return GlobalCalloutNameList->v[name_id].arg_num;
1727 }
1728
1729 static int
1730 get_callout_opt_arg_num_by_name_id(int name_id)
1731 {
1732 return GlobalCalloutNameList->v[name_id].opt_arg_num;
1733 }
1734
1735 static unsigned int
1736 get_callout_arg_type_by_name_id(int name_id, int index)
1737 {
1738 return GlobalCalloutNameList->v[name_id].arg_types[index];
1739 }
1740
1741 static OnigValue
1742 get_callout_opt_default_by_name_id(int name_id, int index)
1743 {
1744 return GlobalCalloutNameList->v[name_id].opt_defaults[index];
1745 }
1746
1747 extern UChar*
1748 onig_get_callout_name_by_name_id(int name_id)
1749 {
1750 if (name_id < 0 || name_id >= GlobalCalloutNameList->n)
1751 return 0;
1752
1753 return GlobalCalloutNameList->v[name_id].name;
1754 }
1755
1756 extern int
1757 onig_global_callout_names_free(void)
1758 {
1759 free_callout_func_list(GlobalCalloutNameList);
1760 GlobalCalloutNameList = 0;
1761
1762 global_callout_name_table_free();
1763 return ONIG_NORMAL;
1764 }
1765
1766
1767 typedef st_table CalloutTagTable;
1768 typedef intptr_t CalloutTagVal;
1769
1770 #define CALLOUT_TAG_LIST_FLAG_TAG_EXIST (1<<0)
1771
1772 static int
1773 i_callout_callout_list_set(UChar* key, CalloutTagVal e, void* arg)
1774 {
1775 int num;
1776 RegexExt* ext = (RegexExt* )arg;
1777
1778 num = (int )e - 1;
1779 ext->callout_list[num].flag |= CALLOUT_TAG_LIST_FLAG_TAG_EXIST;
1780 return ST_CONTINUE;
1781 }
1782
1783 static int
1784 setup_ext_callout_list_values(regex_t* reg)
1785 {
1786 int i, j;
1787 RegexExt* ext;
1788
1789 ext = reg->extp;
1790 if (IS_NOT_NULL(ext->tag_table)) {
1791 onig_st_foreach((CalloutTagTable *)ext->tag_table, i_callout_callout_list_set,
1792 (st_data_t )ext);
1793 }
1794
1795 for (i = 0; i < ext->callout_num; i++) {
1796 CalloutListEntry* e = ext->callout_list + i;
1797 if (e->of == ONIG_CALLOUT_OF_NAME) {
1798 for (j = 0; j < e->u.arg.num; j++) {
1799 if (e->u.arg.types[j] == ONIG_TYPE_TAG) {
1800 UChar* start;
1801 UChar* end;
1802 int num;
1803 start = e->u.arg.vals[j].s.start;
1804 end = e->u.arg.vals[j].s.end;
1805 num = onig_get_callout_num_by_tag(reg, start, end);
1806 if (num < 0) return num;
1807 e->u.arg.vals[j].tag = num;
1808 }
1809 }
1810 }
1811 }
1812
1813 return ONIG_NORMAL;
1814 }
1815
1816 extern int
1817 onig_callout_tag_is_exist_at_callout_num(regex_t* reg, int callout_num)
1818 {
1819 RegexExt* ext = reg->extp;
1820
1821 if (IS_NULL(ext) || IS_NULL(ext->callout_list)) return 0;
1822 if (callout_num > ext->callout_num) return 0;
1823
1824 return (ext->callout_list[callout_num].flag &
1825 CALLOUT_TAG_LIST_FLAG_TAG_EXIST) != 0;
1826 }
1827
1828 static int
1829 i_free_callout_tag_entry(UChar* key, CalloutTagVal e, void* arg ARG_UNUSED)
1830 {
1831 xfree(key);
1832 return ST_DELETE;
1833 }
1834
1835 static int
1836 callout_tag_table_clear(CalloutTagTable* t)
1837 {
1838 if (IS_NOT_NULL(t)) {
1839 onig_st_foreach(t, i_free_callout_tag_entry, 0);
1840 }
1841 return 0;
1842 }
1843
1844 extern int
1845 onig_callout_tag_table_free(void* table)
1846 {
1847 CalloutTagTable* t = (CalloutTagTable* )table;
1848
1849 if (IS_NOT_NULL(t)) {
1850 int r = callout_tag_table_clear(t);
1851 if (r != 0) return r;
1852
1853 onig_st_free_table(t);
1854 }
1855
1856 return 0;
1857 }
1858
1859 extern int
1860 onig_get_callout_num_by_tag(regex_t* reg,
1861 const UChar* tag, const UChar* tag_end)
1862 {
1863 int r;
1864 RegexExt* ext;
1865 CalloutTagVal e;
1866
1867 ext = reg->extp;
1868 if (IS_NULL(ext) || IS_NULL(ext->tag_table))
1869 return ONIGERR_INVALID_CALLOUT_TAG_NAME;
1870
1871 r = onig_st_lookup_strend(ext->tag_table, tag, tag_end,
1872 (HashDataType* )((void* )(&e)));
1873 if (r == 0) return ONIGERR_INVALID_CALLOUT_TAG_NAME;
1874 return (int )e;
1875 }
1876
1877 static CalloutTagVal
1878 callout_tag_find(CalloutTagTable* t, const UChar* name, const UChar* name_end)
1879 {
1880 CalloutTagVal e;
1881
1882 e = -1;
1883 if (IS_NOT_NULL(t)) {
1884 onig_st_lookup_strend(t, name, name_end, (HashDataType* )((void* )(&e)));
1885 }
1886 return e;
1887 }
1888
1889 static int
1890 callout_tag_table_new(CalloutTagTable** rt)
1891 {
1892 CalloutTagTable* t;
1893
1894 *rt = 0;
1895 t = onig_st_init_strend_table_with_size(INIT_TAG_NAMES_ALLOC_NUM);
1896 CHECK_NULL_RETURN_MEMERR(t);
1897
1898 *rt = t;
1899 return ONIG_NORMAL;
1900 }
1901
1902 static int
1903 callout_tag_entry_raw(ScanEnv* env, CalloutTagTable* t, UChar* name,
1904 UChar* name_end, CalloutTagVal entry_val)
1905 {
1906 int r;
1907 CalloutTagVal val;
1908
1909 if (name_end - name <= 0)
1910 return ONIGERR_INVALID_CALLOUT_TAG_NAME;
1911
1912 val = callout_tag_find(t, name, name_end);
1913 if (val >= 0) {
1914 onig_scan_env_set_error_string(env, ONIGERR_MULTIPLEX_DEFINED_NAME,
1915 name, name_end);
1916 return ONIGERR_MULTIPLEX_DEFINED_NAME;
1917 }
1918
1919 r = onig_st_insert_strend(t, name, name_end, (HashDataType )entry_val);
1920 if (r < 0) return r;
1921
1922 return ONIG_NORMAL;
1923 }
1924
1925 static int
1926 ext_ensure_tag_table(regex_t* reg)
1927 {
1928 int r;
1929 RegexExt* ext;
1930 CalloutTagTable* t;
1931
1932 ext = onig_get_regex_ext(reg);
1933 CHECK_NULL_RETURN_MEMERR(ext);
1934
1935 if (IS_NULL(ext->tag_table)) {
1936 r = callout_tag_table_new(&t);
1937 if (r != ONIG_NORMAL) return r;
1938
1939 ext->tag_table = t;
1940 }
1941
1942 return ONIG_NORMAL;
1943 }
1944
1945 static int
1946 callout_tag_entry(ScanEnv* env, regex_t* reg, UChar* name, UChar* name_end,
1947 CalloutTagVal entry_val)
1948 {
1949 int r;
1950 RegexExt* ext;
1951 CalloutListEntry* e;
1952
1953 r = ext_ensure_tag_table(reg);
1954 if (r != ONIG_NORMAL) return r;
1955
1956 ext = onig_get_regex_ext(reg);
1957 CHECK_NULL_RETURN_MEMERR(ext);
1958 r = callout_tag_entry_raw(env, ext->tag_table, name, name_end, entry_val);
1959
1960 e = onig_reg_callout_list_at(reg, (int )entry_val);
1961 CHECK_NULL_RETURN_MEMERR(e);
1962 e->tag_start = name;
1963 e->tag_end = name_end;
1964
1965 return r;
1966 }
1967
1968 #endif /* USE_CALLOUT */
1969
1970
1971 #define INIT_SCANENV_MEMENV_ALLOC_SIZE 16
1972
1973 static void
1974 scan_env_clear(ScanEnv* env)
1975 {
1976 MEM_STATUS_CLEAR(env->capture_history);
1977 MEM_STATUS_CLEAR(env->bt_mem_start);
1978 MEM_STATUS_CLEAR(env->bt_mem_end);
1979 MEM_STATUS_CLEAR(env->backrefed_mem);
1980 env->error = (UChar* )NULL;
1981 env->error_end = (UChar* )NULL;
1982 env->num_call = 0;
1983
1984 #ifdef USE_CALL
1985 env->unset_addr_list = NULL;
1986 env->has_call_zero = 0;
1987 #endif
1988
1989 env->num_mem = 0;
1990 env->num_named = 0;
1991 env->mem_alloc = 0;
1992 env->mem_env_dynamic = (MemEnv* )NULL;
1993
1994 xmemset(env->mem_env_static, 0, sizeof(env->mem_env_static));
1995
1996 env->parse_depth = 0;
1997 env->keep_num = 0;
1998 env->save_num = 0;
1999 env->save_alloc_num = 0;
2000 env->saves = 0;
2001 }
2002
2003 static int
2004 scan_env_add_mem_entry(ScanEnv* env)
2005 {
2006 int i, need, alloc;
2007 MemEnv* p;
2008
2009 need = env->num_mem + 1;
2010 if (need > MaxCaptureNum && MaxCaptureNum != 0)
2011 return ONIGERR_TOO_MANY_CAPTURES;
2012
2013 if (need >= SCANENV_MEMENV_SIZE) {
2014 if (env->mem_alloc <= need) {
2015 if (IS_NULL(env->mem_env_dynamic)) {
2016 alloc = INIT_SCANENV_MEMENV_ALLOC_SIZE;
2017 p = (MemEnv* )xmalloc(sizeof(MemEnv) * alloc);
2018 CHECK_NULL_RETURN_MEMERR(p);
2019 xmemcpy(p, env->mem_env_static, sizeof(env->mem_env_static));
2020 }
2021 else {
2022 alloc = env->mem_alloc * 2;
2023 p = (MemEnv* )xrealloc(env->mem_env_dynamic, sizeof(MemEnv) * alloc, sizeof(MemEnv)*env->mem_alloc);
2024 CHECK_NULL_RETURN_MEMERR(p);
2025 }
2026
2027 for (i = env->num_mem + 1; i < alloc; i++) {
2028 p[i].node = NULL_NODE;
2029 #if 0
2030 p[i].in = 0;
2031 p[i].recursion = 0;
2032 #endif
2033 }
2034
2035 env->mem_env_dynamic = p;
2036 env->mem_alloc = alloc;
2037 }
2038 }
2039
2040 env->num_mem++;
2041 return env->num_mem;
2042 }
2043
2044 static int
2045 scan_env_set_mem_node(ScanEnv* env, int num, Node* node)
2046 {
2047 if (env->num_mem >= num)
2048 SCANENV_MEMENV(env)[num].node = node;
2049 else
2050 return ONIGERR_PARSER_BUG;
2051 return 0;
2052 }
2053
2054 extern void
2055 onig_node_free(Node* node)
2056 {
2057 start:
2058 if (IS_NULL(node)) return ;
2059
2060 #ifdef DEBUG_NODE_FREE
2061 fprintf(stderr, "onig_node_free: %p\n", node);
2062 #endif
2063
2064 switch (NODE_TYPE(node)) {
2065 case NODE_STRING:
2066 if (STR_(node)->capacity != 0 &&
2067 IS_NOT_NULL(STR_(node)->s) && STR_(node)->s != STR_(node)->buf) {
2068 xfree(STR_(node)->s);
2069 }
2070 break;
2071
2072 case NODE_LIST:
2073 case NODE_ALT:
2074 onig_node_free(NODE_CAR(node));
2075 {
2076 Node* next_node = NODE_CDR(node);
2077
2078 xfree(node);
2079 node = next_node;
2080 goto start;
2081 }
2082 break;
2083
2084 case NODE_CCLASS:
2085 {
2086 CClassNode* cc = CCLASS_(node);
2087
2088 if (cc->mbuf)
2089 bbuf_free(cc->mbuf);
2090 }
2091 break;
2092
2093 case NODE_BACKREF:
2094 if (IS_NOT_NULL(BACKREF_(node)->back_dynamic))
2095 xfree(BACKREF_(node)->back_dynamic);
2096 break;
2097
2098 case NODE_BAG:
2099 if (NODE_BODY(node))
2100 onig_node_free(NODE_BODY(node));
2101
2102 {
2103 BagNode* en = BAG_(node);
2104 if (en->type == BAG_IF_ELSE) {
2105 onig_node_free(en->te.Then);
2106 onig_node_free(en->te.Else);
2107 }
2108 }
2109 break;
2110
2111 case NODE_QUANT:
2112 case NODE_ANCHOR:
2113 if (NODE_BODY(node))
2114 onig_node_free(NODE_BODY(node));
2115 break;
2116
2117 case NODE_CTYPE:
2118 case NODE_CALL:
2119 case NODE_GIMMICK:
2120 break;
2121 }
2122
2123 xfree(node);
2124 }
2125
2126 static void
2127 cons_node_free_alone(Node* node)
2128 {
2129 NODE_CAR(node) = 0;
2130 NODE_CDR(node) = 0;
2131 onig_node_free(node);
2132 }
2133
2134 static Node*
2135 node_new(void)
2136 {
2137 Node* node;
2138
2139 node = (Node* )xmalloc(sizeof(Node));
2140 CHECK_NULL_RETURN(node);
2141 xmemset(node, 0, sizeof(*node));
2142
2143 #ifdef DEBUG_NODE_FREE
2144 fprintf(stderr, "node_new: %p\n", node);
2145 #endif
2146 return node;
2147 }
2148
2149
2150 static void
2151 initialize_cclass(CClassNode* cc)
2152 {
2153 BITSET_CLEAR(cc->bs);
2154 cc->flags = 0;
2155 cc->mbuf = NULL;
2156 }
2157
2158 static Node*
2159 node_new_cclass(void)
2160 {
2161 Node* node = node_new();
2162 CHECK_NULL_RETURN(node);
2163
2164 NODE_SET_TYPE(node, NODE_CCLASS);
2165 initialize_cclass(CCLASS_(node));
2166 return node;
2167 }
2168
2169 static Node*
2170 node_new_ctype(int type, int not, OnigOptionType options)
2171 {
2172 Node* node = node_new();
2173 CHECK_NULL_RETURN(node);
2174
2175 NODE_SET_TYPE(node, NODE_CTYPE);
2176 CTYPE_(node)->ctype = type;
2177 CTYPE_(node)->not = not;
2178 CTYPE_(node)->options = options;
2179 CTYPE_(node)->ascii_mode = IS_ASCII_MODE_CTYPE_OPTION(type, options);
2180 return node;
2181 }
2182
2183 static Node*
2184 node_new_anychar(void)
2185 {
2186 Node* node = node_new_ctype(CTYPE_ANYCHAR, 0, ONIG_OPTION_NONE);
2187 return node;
2188 }
2189
2190 static Node*
2191 node_new_anychar_with_fixed_option(OnigOptionType option)
2192 {
2193 CtypeNode* ct;
2194 Node* node;
2195
2196 node = node_new_anychar();
2197 CHECK_NULL_RETURN(node);
2198
2199 ct = CTYPE_(node);
2200 ct->options = option;
2201 NODE_STATUS_ADD(node, FIXED_OPTION);
2202 return node;
2203 }
2204
2205 static int
2206 node_new_no_newline(Node** node, ScanEnv* env)
2207 {
2208 Node* n;
2209
2210 n = node_new_anychar_with_fixed_option(ONIG_OPTION_NONE);
2211 CHECK_NULL_RETURN_MEMERR(n);
2212 *node = n;
2213 return 0;
2214 }
2215
2216 static int
2217 node_new_true_anychar(Node** node, ScanEnv* env)
2218 {
2219 Node* n;
2220
2221 n = node_new_anychar_with_fixed_option(ONIG_OPTION_MULTILINE);
2222 CHECK_NULL_RETURN_MEMERR(n);
2223 *node = n;
2224 return 0;
2225 }
2226
2227 static Node*
2228 node_new_list(Node* left, Node* right)
2229 {
2230 Node* node = node_new();
2231 CHECK_NULL_RETURN(node);
2232
2233 NODE_SET_TYPE(node, NODE_LIST);
2234 NODE_CAR(node) = left;
2235 NODE_CDR(node) = right;
2236 return node;
2237 }
2238
2239 extern Node*
2240 onig_node_new_list(Node* left, Node* right)
2241 {
2242 return node_new_list(left, right);
2243 }
2244
2245 extern Node*
2246 onig_node_list_add(Node* list, Node* x)
2247 {
2248 Node *n;
2249
2250 n = onig_node_new_list(x, NULL);
2251 if (IS_NULL(n)) return NULL_NODE;
2252
2253 if (IS_NOT_NULL(list)) {
2254 while (IS_NOT_NULL(NODE_CDR(list)))
2255 list = NODE_CDR(list);
2256
2257 NODE_CDR(list) = n;
2258 }
2259
2260 return n;
2261 }
2262
2263 extern Node*
2264 onig_node_new_alt(Node* left, Node* right)
2265 {
2266 Node* node = node_new();
2267 CHECK_NULL_RETURN(node);
2268
2269 NODE_SET_TYPE(node, NODE_ALT);
2270 NODE_CAR(node) = left;
2271 NODE_CDR(node) = right;
2272 return node;
2273 }
2274
2275 static Node*
2276 make_list_or_alt(NodeType type, int n, Node* ns[])
2277 {
2278 Node* r;
2279
2280 if (n <= 0) return NULL_NODE;
2281
2282 if (n == 1) {
2283 r = node_new();
2284 CHECK_NULL_RETURN(r);
2285 NODE_SET_TYPE(r, type);
2286 NODE_CAR(r) = ns[0];
2287 NODE_CDR(r) = NULL_NODE;
2288 }
2289 else {
2290 Node* right;
2291
2292 r = node_new();
2293 CHECK_NULL_RETURN(r);
2294
2295 right = make_list_or_alt(type, n - 1, ns + 1);
2296 if (IS_NULL(right)) {
2297 onig_node_free(r);
2298 return NULL_NODE;
2299 }
2300
2301 NODE_SET_TYPE(r, type);
2302 NODE_CAR(r) = ns[0];
2303 NODE_CDR(r) = right;
2304 }
2305
2306 return r;
2307 }
2308
2309 static Node*
2310 make_list(int n, Node* ns[])
2311 {
2312 return make_list_or_alt(NODE_LIST, n, ns);
2313 }
2314
2315 static Node*
2316 make_alt(int n, Node* ns[])
2317 {
2318 return make_list_or_alt(NODE_ALT, n, ns);
2319 }
2320
2321 extern Node*
2322 onig_node_new_anchor(int type, int ascii_mode)
2323 {
2324 Node* node = node_new();
2325 CHECK_NULL_RETURN(node);
2326
2327 NODE_SET_TYPE(node, NODE_ANCHOR);
2328 ANCHOR_(node)->type = type;
2329 ANCHOR_(node)->char_len = -1;
2330 ANCHOR_(node)->ascii_mode = ascii_mode;
2331 return node;
2332 }
2333
2334 static Node*
2335 node_new_backref(int back_num, int* backrefs, int by_name,
2336 #ifdef USE_BACKREF_WITH_LEVEL
2337 int exist_level, int nest_level,
2338 #endif
2339 ScanEnv* env)
2340 {
2341 int i;
2342 Node* node = node_new();
2343
2344 CHECK_NULL_RETURN(node);
2345
2346 NODE_SET_TYPE(node, NODE_BACKREF);
2347 BACKREF_(node)->back_num = back_num;
2348 BACKREF_(node)->back_dynamic = (int* )NULL;
2349 if (by_name != 0)
2350 NODE_STATUS_ADD(node, BY_NAME);
2351
2352 #ifdef USE_BACKREF_WITH_LEVEL
2353 if (exist_level != 0) {
2354 NODE_STATUS_ADD(node, NEST_LEVEL);
2355 BACKREF_(node)->nest_level = nest_level;
2356 }
2357 #endif
2358
2359 for (i = 0; i < back_num; i++) {
2360 if (backrefs[i] <= env->num_mem &&
2361 IS_NULL(SCANENV_MEMENV(env)[backrefs[i]].node)) {
2362 NODE_STATUS_ADD(node, RECURSION); /* /...(\1).../ */
2363 break;
2364 }
2365 }
2366
2367 if (back_num <= NODE_BACKREFS_SIZE) {
2368 for (i = 0; i < back_num; i++)
2369 BACKREF_(node)->back_static[i] = backrefs[i];
2370 }
2371 else {
2372 int* p = (int* )xmalloc(sizeof(int) * back_num);
2373 if (IS_NULL(p)) {
2374 onig_node_free(node);
2375 return NULL;
2376 }
2377 BACKREF_(node)->back_dynamic = p;
2378 for (i = 0; i < back_num; i++)
2379 p[i] = backrefs[i];
2380 }
2381 return node;
2382 }
2383
2384 static Node*
2385 node_new_backref_checker(int back_num, int* backrefs, int by_name,
2386 #ifdef USE_BACKREF_WITH_LEVEL
2387 int exist_level, int nest_level,
2388 #endif
2389 ScanEnv* env)
2390 {
2391 Node* node;
2392
2393 node = node_new_backref(back_num, backrefs, by_name,
2394 #ifdef USE_BACKREF_WITH_LEVEL
2395 exist_level, nest_level,
2396 #endif
2397 env);
2398 CHECK_NULL_RETURN(node);
2399
2400 NODE_STATUS_ADD(node, CHECKER);
2401 return node;
2402 }
2403
2404 #ifdef USE_CALL
2405 static Node*
2406 node_new_call(UChar* name, UChar* name_end, int gnum, int by_number)
2407 {
2408 Node* node = node_new();
2409 CHECK_NULL_RETURN(node);
2410
2411 NODE_SET_TYPE(node, NODE_CALL);
2412 CALL_(node)->by_number = by_number;
2413 CALL_(node)->name = name;
2414 CALL_(node)->name_end = name_end;
2415 CALL_(node)->group_num = gnum;
2416 CALL_(node)->entry_count = 1;
2417 return node;
2418 }
2419 #endif
2420
2421 static Node*
2422 node_new_quantifier(int lower, int upper, int by_number)
2423 {
2424 Node* node = node_new();
2425 CHECK_NULL_RETURN(node);
2426
2427 NODE_SET_TYPE(node, NODE_QUANT);
2428 QUANT_(node)->lower = lower;
2429 QUANT_(node)->upper = upper;
2430 QUANT_(node)->greedy = 1;
2431 QUANT_(node)->emptiness = BODY_IS_NOT_EMPTY;
2432 QUANT_(node)->head_exact = NULL_NODE;
2433 QUANT_(node)->next_head_exact = NULL_NODE;
2434 QUANT_(node)->is_refered = 0;
2435 if (by_number != 0)
2436 NODE_STATUS_ADD(node, BY_NUMBER);
2437
2438 return node;
2439 }
2440
2441 static Node*
2442 node_new_bag(enum BagType type)
2443 {
2444 Node* node = node_new();
2445 CHECK_NULL_RETURN(node);
2446
2447 NODE_SET_TYPE(node, NODE_BAG);
2448 BAG_(node)->type = type;
2449
2450 switch (type) {
2451 case BAG_MEMORY:
2452 BAG_(node)->m.regnum = 0;
2453 BAG_(node)->m.called_addr = -1;
2454 BAG_(node)->m.entry_count = 1;
2455 BAG_(node)->m.called_state = 0;
2456 break;
2457
2458 case BAG_OPTION:
2459 BAG_(node)->o.options = 0;
2460 break;
2461
2462 case BAG_STOP_BACKTRACK:
2463 break;
2464
2465 case BAG_IF_ELSE:
2466 BAG_(node)->te.Then = 0;
2467 BAG_(node)->te.Else = 0;
2468 break;
2469 }
2470
2471 BAG_(node)->opt_count = 0;
2472 return node;
2473 }
2474
2475 extern Node*
2476 onig_node_new_bag(enum BagType type)
2477 {
2478 return node_new_bag(type);
2479 }
2480
2481 static Node*
2482 node_new_bag_if_else(Node* cond, Node* Then, Node* Else)
2483 {
2484 Node* n;
2485 n = node_new_bag(BAG_IF_ELSE);
2486 CHECK_NULL_RETURN(n);
2487
2488 NODE_BODY(n) = cond;
2489 BAG_(n)->te.Then = Then;
2490 BAG_(n)->te.Else = Else;
2491 return n;
2492 }
2493
2494 static Node*
2495 node_new_memory(int is_named)
2496 {
2497 Node* node = node_new_bag(BAG_MEMORY);
2498 CHECK_NULL_RETURN(node);
2499 if (is_named != 0)
2500 NODE_STATUS_ADD(node, NAMED_GROUP);
2501
2502 return node;
2503 }
2504
2505 static Node*
2506 node_new_option(OnigOptionType option)
2507 {
2508 Node* node = node_new_bag(BAG_OPTION);
2509 CHECK_NULL_RETURN(node);
2510 BAG_(node)->o.options = option;
2511 return node;
2512 }
2513
2514 static Node*
2515 node_new_group(Node* content)
2516 {
2517 Node* node;
2518
2519 node = node_new();
2520 CHECK_NULL_RETURN(node);
2521 NODE_SET_TYPE(node, NODE_LIST);
2522 NODE_CAR(node) = content;
2523 NODE_CDR(node) = NULL_NODE;
2524
2525 return node;
2526 }
2527
2528 static Node*
2529 node_drop_group(Node* group)
2530 {
2531 Node* content;
2532
2533 content = NODE_CAR(group);
2534 NODE_CAR(group) = NULL_NODE;
2535 onig_node_free(group);
2536 return content;
2537 }
2538
2539 static int
2540 node_new_fail(Node** node, ScanEnv* env)
2541 {
2542 *node = node_new();
2543 CHECK_NULL_RETURN_MEMERR(*node);
2544
2545 NODE_SET_TYPE(*node, NODE_GIMMICK);
2546 GIMMICK_(*node)->type = GIMMICK_FAIL;
2547 return ONIG_NORMAL;
2548 }
2549
2550 static int
2551 node_new_save_gimmick(Node** node, enum SaveType save_type, ScanEnv* env)
2552 {
2553 int id;
2554 int r;
2555
2556 r = save_entry(env, save_type, &id);
2557 if (r != ONIG_NORMAL) return r;
2558
2559 *node = node_new();
2560 CHECK_NULL_RETURN_MEMERR(*node);
2561
2562 NODE_SET_TYPE(*node, NODE_GIMMICK);
2563 GIMMICK_(*node)->id = id;
2564 GIMMICK_(*node)->type = GIMMICK_SAVE;
2565 GIMMICK_(*node)->detail_type = (int )save_type;
2566
2567 return ONIG_NORMAL;
2568 }
2569
2570 static int
2571 node_new_update_var_gimmick(Node** node, enum UpdateVarType update_var_type,
2572 int id, ScanEnv* env)
2573 {
2574 *node = node_new();
2575 CHECK_NULL_RETURN_MEMERR(*node);
2576
2577 NODE_SET_TYPE(*node, NODE_GIMMICK);
2578 GIMMICK_(*node)->id = id;
2579 GIMMICK_(*node)->type = GIMMICK_UPDATE_VAR;
2580 GIMMICK_(*node)->detail_type = (int )update_var_type;
2581
2582 return ONIG_NORMAL;
2583 }
2584
2585 static int
2586 node_new_keep(Node** node, ScanEnv* env)
2587 {
2588 int r;
2589
2590 r = node_new_save_gimmick(node, SAVE_KEEP, env);
2591 if (r != 0) return r;
2592
2593 env->keep_num++;
2594 return ONIG_NORMAL;
2595 }
2596
2597 #ifdef USE_CALLOUT
2598
2599 extern void
2600 onig_free_reg_callout_list(int n, CalloutListEntry* list)
2601 {
2602 int i;
2603 int j;
2604
2605 if (IS_NULL(list)) return ;
2606
2607 for (i = 0; i < n; i++) {
2608 if (list[i].of == ONIG_CALLOUT_OF_NAME) {
2609 for (j = 0; j < list[i].u.arg.passed_num; j++) {
2610 if (list[i].u.arg.types[j] == ONIG_TYPE_STRING) {
2611 if (IS_NOT_NULL(list[i].u.arg.vals[j].s.start))
2612 xfree(list[i].u.arg.vals[j].s.start);
2613 }
2614 }
2615 }
2616 else { /* ONIG_CALLOUT_OF_CONTENTS */
2617 if (IS_NOT_NULL(list[i].u.content.start)) {
2618 xfree((void* )list[i].u.content.start);
2619 }
2620 }
2621 }
2622
2623 xfree(list);
2624 }
2625
2626 extern CalloutListEntry*
2627 onig_reg_callout_list_at(regex_t* reg, int num)
2628 {
2629 RegexExt* ext = reg->extp;
2630 CHECK_NULL_RETURN(ext);
2631
2632 if (num <= 0 || num > ext->callout_num)
2633 return 0;
2634
2635 num--;
2636 return ext->callout_list + num;
2637 }
2638
2639 static int
2640 reg_callout_list_entry(ScanEnv* env, int* rnum)
2641 {
2642 #define INIT_CALLOUT_LIST_NUM 3
2643
2644 int num;
2645 CalloutListEntry* list;
2646 CalloutListEntry* e;
2647 RegexExt* ext;
2648
2649 ext = onig_get_regex_ext(env->reg);
2650 CHECK_NULL_RETURN_MEMERR(ext);
2651
2652 if (IS_NULL(ext->callout_list)) {
2653 list = (CalloutListEntry* )xmalloc(sizeof(*list) * INIT_CALLOUT_LIST_NUM);
2654 CHECK_NULL_RETURN_MEMERR(list);
2655
2656 ext->callout_list = list;
2657 ext->callout_list_alloc = INIT_CALLOUT_LIST_NUM;
2658 ext->callout_num = 0;
2659 }
2660
2661 num = ext->callout_num + 1;
2662 if (num > ext->callout_list_alloc) {
2663 int alloc = ext->callout_list_alloc * 2;
2664 list = (CalloutListEntry* )xrealloc(ext->callout_list,
2665 sizeof(CalloutListEntry) * alloc,
2666 sizeof(CalloutListEntry) * ext->callout_list_alloc);
2667 CHECK_NULL_RETURN_MEMERR(list);
2668
2669 ext->callout_list = list;
2670 ext->callout_list_alloc = alloc;
2671 }
2672
2673 e = ext->callout_list + (num - 1);
2674
2675 e->flag = 0;
2676 e->of = 0;
2677 e->in = ONIG_CALLOUT_OF_CONTENTS;
2678 e->type = 0;
2679 e->tag_start = 0;
2680 e->tag_end = 0;
2681 e->start_func = 0;
2682 e->end_func = 0;
2683 e->u.arg.num = 0;
2684 e->u.arg.passed_num = 0;
2685
2686 ext->callout_num = num;
2687 *rnum = num;
2688 return ONIG_NORMAL;
2689 }
2690
2691 static int
2692 node_new_callout(Node** node, OnigCalloutOf callout_of, int num, int id,
2693 ScanEnv* env)
2694 {
2695 *node = node_new();
2696 CHECK_NULL_RETURN_MEMERR(*node);
2697
2698 NODE_SET_TYPE(*node, NODE_GIMMICK);
2699 GIMMICK_(*node)->id = id;
2700 GIMMICK_(*node)->num = num;
2701 GIMMICK_(*node)->type = GIMMICK_CALLOUT;
2702 GIMMICK_(*node)->detail_type = (int )callout_of;
2703
2704 return ONIG_NORMAL;
2705 }
2706 #endif
2707
2708 static int
2709 make_text_segment(Node** node, ScanEnv* env)
2710 {
2711 int r;
2712 int i;
2713 Node* x;
2714 Node* ns[2];
2715
2716 /* \X == (?>\O(?:\Y\O)*) */
2717
2718 ns[1] = NULL_NODE;
2719
2720 r = ONIGERR_MEMORY;
2721 ns[0] = onig_node_new_anchor(ANCR_NO_TEXT_SEGMENT_BOUNDARY, 0);
2722 if (IS_NULL(ns[0])) goto err;
2723
2724 r = node_new_true_anychar(&ns[1], env);
2725 if (r != 0) goto err1;
2726
2727 x = make_list(2, ns);
2728 if (IS_NULL(x)) goto err;
2729 ns[0] = x;
2730 ns[1] = NULL_NODE;
2731
2732 x = node_new_quantifier(0, INFINITE_REPEAT, 1);
2733 if (IS_NULL(x)) goto err;
2734
2735 NODE_BODY(x) = ns[0];
2736 ns[0] = NULL_NODE;
2737 ns[1] = x;
2738
2739 r = node_new_true_anychar(&ns[0], env);
2740 if (r != 0) goto err1;
2741
2742 x = make_list(2, ns);
2743 if (IS_NULL(x)) goto err;
2744
2745 ns[0] = x;
2746 ns[1] = NULL_NODE;
2747
2748 x = node_new_bag(BAG_STOP_BACKTRACK);
2749 if (IS_NULL(x)) goto err;
2750
2751 NODE_BODY(x) = ns[0];
2752
2753 *node = x;
2754 return ONIG_NORMAL;
2755
2756 err:
2757 r = ONIGERR_MEMORY;
2758 err1:
2759 for (i = 0; i < 2; i++) onig_node_free(ns[i]);
2760 return r;
2761 }
2762
2763 static int
2764 make_absent_engine(Node** node, int pre_save_right_id, Node* absent,
2765 Node* step_one, int lower, int upper, int possessive,
2766 int is_range_cutter, ScanEnv* env)
2767 {
2768 int r;
2769 int i;
2770 int id;
2771 Node* x;
2772 Node* ns[4];
2773
2774 for (i = 0; i < 4; i++) ns[i] = NULL_NODE;
2775
2776 ns[1] = absent;
2777 ns[3] = step_one; /* for err */
2778 r = node_new_save_gimmick(&ns[0], SAVE_S, env);
2779 if (r != 0) goto err;
2780
2781 id = GIMMICK_(ns[0])->id;
2782 r = node_new_update_var_gimmick(&ns[2], UPDATE_VAR_RIGHT_RANGE_FROM_S_STACK,
2783 id, env);
2784 if (r != 0) goto err;
2785
2786 r = node_new_fail(&ns[3], env);
2787 if (r != 0) goto err;
2788
2789 x = make_list(4, ns);
2790 if (IS_NULL(x)) goto err0;
2791
2792 ns[0] = x;
2793 ns[1] = step_one;
2794 ns[2] = ns[3] = NULL_NODE;
2795
2796 x = make_alt(2, ns);
2797 if (IS_NULL(x)) goto err0;
2798
2799 ns[0] = x;
2800
2801 x = node_new_quantifier(lower, upper, 0);
2802 if (IS_NULL(x)) goto err0;
2803
2804 NODE_BODY(x) = ns[0];
2805 ns[0] = x;
2806
2807 if (possessive != 0) {
2808 x = node_new_bag(BAG_STOP_BACKTRACK);
2809 if (IS_NULL(x)) goto err0;
2810
2811 NODE_BODY(x) = ns[0];
2812 ns[0] = x;
2813 }
2814
2815 r = node_new_update_var_gimmick(&ns[1], UPDATE_VAR_RIGHT_RANGE_FROM_STACK,
2816 pre_save_right_id, env);
2817 if (r != 0) goto err;
2818
2819 r = node_new_fail(&ns[2], env);
2820 if (r != 0) goto err;
2821
2822 x = make_list(2, ns + 1);
2823 if (IS_NULL(x)) goto err0;
2824
2825 ns[1] = x; ns[2] = NULL_NODE;
2826
2827 x = make_alt(2, ns);
2828 if (IS_NULL(x)) goto err0;
2829
2830 if (is_range_cutter != 0)
2831 NODE_STATUS_ADD(x, SUPER);
2832
2833 *node = x;
2834 return ONIG_NORMAL;
2835
2836 err0:
2837 r = ONIGERR_MEMORY;
2838 err:
2839 for (i = 0; i < 4; i++) onig_node_free(ns[i]);
2840 return r;
2841 }
2842
2843 static int
2844 make_absent_tail(Node** node1, Node** node2, int pre_save_right_id,
2845 ScanEnv* env)
2846 {
2847 int r;
2848 int id;
2849 Node* save;
2850 Node* x;
2851 Node* ns[2];
2852
2853 *node1 = *node2 = NULL_NODE;
2854 save = ns[0] = ns[1] = NULL_NODE;
2855
2856 r = node_new_save_gimmick(&save, SAVE_RIGHT_RANGE, env);
2857 if (r != 0) goto err;
2858
2859 id = GIMMICK_(save)->id;
2860 r = node_new_update_var_gimmick(&ns[0], UPDATE_VAR_RIGHT_RANGE_FROM_STACK,
2861 id, env);
2862 if (r != 0) goto err;
2863
2864 r = node_new_fail(&ns[1], env);
2865 if (r != 0) goto err;
2866
2867 x = make_list(2, ns);
2868 if (IS_NULL(x)) goto err0;
2869
2870 ns[0] = NULL_NODE; ns[1] = x;
2871
2872 r = node_new_update_var_gimmick(&ns[0], UPDATE_VAR_RIGHT_RANGE_FROM_STACK,
2873 pre_save_right_id, env);
2874 if (r != 0) goto err;
2875
2876 x = make_alt(2, ns);
2877 if (IS_NULL(x)) goto err0;
2878
2879 *node1 = save;
2880 *node2 = x;
2881 return ONIG_NORMAL;
2882
2883 err0:
2884 r = ONIGERR_MEMORY;
2885 err:
2886 onig_node_free(save);
2887 onig_node_free(ns[0]);
2888 onig_node_free(ns[1]);
2889 return r;
2890 }
2891
2892 static int
2893 make_range_clear(Node** node, ScanEnv* env)
2894 {
2895 int r;
2896 int id;
2897 Node* save;
2898 Node* x;
2899 Node* ns[2];
2900
2901 *node = NULL_NODE;
2902 save = ns[0] = ns[1] = NULL_NODE;
2903
2904 r = node_new_save_gimmick(&save, SAVE_RIGHT_RANGE, env);
2905 if (r != 0) goto err;
2906
2907 id = GIMMICK_(save)->id;
2908 r = node_new_update_var_gimmick(&ns[0], UPDATE_VAR_RIGHT_RANGE_FROM_STACK,
2909 id, env);
2910 if (r != 0) goto err;
2911
2912 r = node_new_fail(&ns[1], env);
2913 if (r != 0) goto err;
2914
2915 x = make_list(2, ns);
2916 if (IS_NULL(x)) goto err0;
2917
2918 ns[0] = NULL_NODE; ns[1] = x;
2919
2920 r = node_new_update_var_gimmick(&ns[0], UPDATE_VAR_RIGHT_RANGE_INIT, 0, env);
2921 if (r != 0) goto err;
2922
2923 x = make_alt(2, ns);
2924 if (IS_NULL(x)) goto err0;
2925
2926 NODE_STATUS_ADD(x, SUPER);
2927
2928 ns[0] = save;
2929 ns[1] = x;
2930 save = NULL_NODE;
2931 x = make_list(2, ns);
2932 if (IS_NULL(x)) goto err0;
2933
2934 *node = x;
2935 return ONIG_NORMAL;
2936
2937 err0:
2938 r = ONIGERR_MEMORY;
2939 err:
2940 onig_node_free(save);
2941 onig_node_free(ns[0]);
2942 onig_node_free(ns[1]);
2943 return r;
2944 }
2945
2946 static int
2947 is_simple_one_char_repeat(Node* node, Node** rquant, Node** rbody,
2948 int* is_possessive, ScanEnv* env)
2949 {
2950 Node* quant;
2951 Node* body;
2952
2953 *rquant = *rbody = 0;
2954 *is_possessive = 0;
2955
2956 if (NODE_TYPE(node) == NODE_QUANT) {
2957 quant = node;
2958 }
2959 else {
2960 if (NODE_TYPE(node) == NODE_BAG) {
2961 BagNode* en = BAG_(node);
2962 if (en->type == BAG_STOP_BACKTRACK) {
2963 *is_possessive = 1;
2964 quant = NODE_BAG_BODY(en);
2965 if (NODE_TYPE(quant) != NODE_QUANT)
2966 return 0;
2967 }
2968 else
2969 return 0;
2970 }
2971 else
2972 return 0;
2973 }
2974
2975 if (QUANT_(quant)->greedy == 0)
2976 return 0;
2977
2978 body = NODE_BODY(quant);
2979 switch (NODE_TYPE(body)) {
2980 case NODE_STRING:
2981 {
2982 int len;
2983 StrNode* sn = STR_(body);
2984 UChar *s = sn->s;
2985
2986 len = 0;
2987 while (s < sn->end) {
2988 s += enclen(env->enc, s);
2989 len++;
2990 }
2991 if (len != 1)
2992 return 0;
2993 }
2994
2995 case NODE_CCLASS:
2996 break;
2997
2998 default:
2999 return 0;
3000 break;
3001 }
3002
3003 if (node != quant) {
3004 NODE_BODY(node) = 0;
3005 onig_node_free(node);
3006 }
3007 NODE_BODY(quant) = NULL_NODE;
3008 *rquant = quant;
3009 *rbody = body;
3010 return 1;
3011 }
3012
3013 static int
3014 make_absent_tree_for_simple_one_char_repeat(Node** node, Node* absent, Node* quant,
3015 Node* body, int possessive, ScanEnv* env)
3016 {
3017 int r;
3018 int i;
3019 int id1;
3020 int lower, upper;
3021 Node* x;
3022 Node* ns[4];
3023
3024 *node = NULL_NODE;
3025 r = ONIGERR_MEMORY;
3026 ns[0] = ns[1] = NULL_NODE;
3027 ns[2] = body, ns[3] = absent;
3028
3029 lower = QUANT_(quant)->lower;
3030 upper = QUANT_(quant)->upper;
3031 onig_node_free(quant);
3032
3033 r = node_new_save_gimmick(&ns[0], SAVE_RIGHT_RANGE, env);
3034 if (r != 0) goto err;
3035
3036 id1 = GIMMICK_(ns[0])->id;
3037
3038 r = make_absent_engine(&ns[1], id1, absent, body, lower, upper, possessive,
3039 0, env);
3040 if (r != 0) goto err;
3041
3042 ns[2] = ns[3] = NULL_NODE;
3043
3044 r = node_new_update_var_gimmick(&ns[2], UPDATE_VAR_RIGHT_RANGE_FROM_STACK,
3045 id1, env);
3046 if (r != 0) goto err;
3047
3048 x = make_list(3, ns);
3049 if (IS_NULL(x)) goto err0;
3050
3051 *node = x;
3052 return ONIG_NORMAL;
3053
3054 err0:
3055 r = ONIGERR_MEMORY;
3056 err:
3057 for (i = 0; i < 4; i++) onig_node_free(ns[i]);
3058 return r;
3059 }
3060
3061 static int
3062 make_absent_tree(Node** node, Node* absent, Node* expr, int is_range_cutter,
3063 ScanEnv* env)
3064 {
3065 int r;
3066 int i;
3067 int id1, id2;
3068 int possessive;
3069 Node* x;
3070 Node* ns[7];
3071
3072 r = ONIGERR_MEMORY;
3073 for (i = 0; i < 7; i++) ns[i] = NULL_NODE;
3074 ns[4] = expr; ns[5] = absent;
3075
3076 if (is_range_cutter == 0) {
3077 Node* quant;
3078 Node* body;
3079
3080 if (expr == NULL_NODE) {
3081 /* default expr \O* */
3082 quant = node_new_quantifier(0, INFINITE_REPEAT, 0);
3083 if (IS_NULL(quant)) goto err0;
3084
3085 r = node_new_true_anychar(&body, env);
3086 if (r != 0) {
3087 onig_node_free(quant);
3088 goto err;
3089 }
3090 possessive = 0;
3091 goto simple;
3092 }
3093 else {
3094 if (is_simple_one_char_repeat(expr, &quant, &body, &possessive, env)) {
3095 simple:
3096 r = make_absent_tree_for_simple_one_char_repeat(node, absent, quant,
3097 body, possessive, env);
3098 if (r != 0) {
3099 ns[4] = NULL_NODE;
3100 onig_node_free(quant);
3101 onig_node_free(body);
3102 goto err;
3103 }
3104
3105 return ONIG_NORMAL;
3106 }
3107 }
3108 }
3109
3110 r = node_new_save_gimmick(&ns[0], SAVE_RIGHT_RANGE, env);
3111 if (r != 0) goto err;
3112
3113 id1 = GIMMICK_(ns[0])->id;
3114
3115 r = node_new_save_gimmick(&ns[1], SAVE_S, env);
3116 if (r != 0) goto err;
3117
3118 id2 = GIMMICK_(ns[1])->id;
3119
3120 r = node_new_true_anychar(&ns[3], env);
3121 if (r != 0) goto err;
3122
3123 possessive = 1;
3124 r = make_absent_engine(&ns[2], id1, absent, ns[3], 0, INFINITE_REPEAT,
3125 possessive, is_range_cutter, env);
3126 if (r != 0) goto err;
3127
3128 ns[3] = NULL_NODE;
3129 ns[5] = NULL_NODE;
3130
3131 r = node_new_update_var_gimmick(&ns[3], UPDATE_VAR_S_FROM_STACK, id2, env);
3132 if (r != 0) goto err;
3133
3134 if (is_range_cutter != 0) {
3135 x = make_list(4, ns);
3136 if (IS_NULL(x)) goto err0;
3137 }
3138 else {
3139 r = make_absent_tail(&ns[5], &ns[6], id1, env);
3140 if (r != 0) goto err;
3141
3142 x = make_list(7, ns);
3143 if (IS_NULL(x)) goto err0;
3144 }
3145
3146 *node = x;
3147 return ONIG_NORMAL;
3148
3149 err0:
3150 r = ONIGERR_MEMORY;
3151 err:
3152 for (i = 0; i < 7; i++) onig_node_free(ns[i]);
3153 return r;
3154 }
3155
3156 extern int
3157 onig_node_str_cat(Node* node, const UChar* s, const UChar* end)
3158 {
3159 int addlen = (int )(end - s);
3160
3161 if (addlen > 0) {
3162 int len = (int )(STR_(node)->end - STR_(node)->s);
3163
3164 if (STR_(node)->capacity > 0 || (len + addlen > NODE_STRING_BUF_SIZE - 1)) {
3165 UChar* p;
3166 int capa = len + addlen + NODE_STRING_MARGIN;
3167
3168 if (capa <= STR_(node)->capacity) {
3169 onig_strcpy(STR_(node)->s + len, s, end);
3170 }
3171 else {
3172 if (STR_(node)->s == STR_(node)->buf)
3173 p = strcat_capa_from_static(STR_(node)->s, STR_(node)->end,
3174 s, end, capa);
3175 else
3176 p = strcat_capa(STR_(node)->s, STR_(node)->end, s, end, capa, STR_(node)->capacity);
3177
3178 CHECK_NULL_RETURN_MEMERR(p);
3179 STR_(node)->s = p;
3180 STR_(node)->capacity = capa;
3181 }
3182 }
3183 else {
3184 onig_strcpy(STR_(node)->s + len, s, end);
3185 }
3186 STR_(node)->end = STR_(node)->s + len + addlen;
3187 }
3188
3189 return 0;
3190 }
3191
3192 extern int
3193 onig_node_str_set(Node* node, const UChar* s, const UChar* end)
3194 {
3195 onig_node_str_clear(node);
3196 return onig_node_str_cat(node, s, end);
3197 }
3198
3199 static int
3200 node_str_cat_char(Node* node, UChar c)
3201 {
3202 UChar s[1];
3203
3204 s[0] = c;
3205 return onig_node_str_cat(node, s, s + 1);
3206 }
3207
3208 extern void
3209 onig_node_conv_to_str_node(Node* node, int flag)
3210 {
3211 NODE_SET_TYPE(node, NODE_STRING);
3212 STR_(node)->flag = flag;
3213 STR_(node)->capacity = 0;
3214 STR_(node)->s = STR_(node)->buf;
3215 STR_(node)->end = STR_(node)->buf;
3216 }
3217
3218 extern void
3219 onig_node_str_clear(Node* node)
3220 {
3221 if (STR_(node)->capacity != 0 &&
3222 IS_NOT_NULL(STR_(node)->s) && STR_(node)->s != STR_(node)->buf) {
3223 xfree(STR_(node)->s);
3224 }
3225
3226 STR_(node)->capacity = 0;
3227 STR_(node)->flag = 0;
3228 STR_(node)->s = STR_(node)->buf;
3229 STR_(node)->end = STR_(node)->buf;
3230 }
3231
3232 static Node*
3233 node_new_str(const UChar* s, const UChar* end)
3234 {
3235 Node* node = node_new();
3236 CHECK_NULL_RETURN(node);
3237
3238 NODE_SET_TYPE(node, NODE_STRING);
3239 STR_(node)->capacity = 0;
3240 STR_(node)->flag = 0;
3241 STR_(node)->s = STR_(node)->buf;
3242 STR_(node)->end = STR_(node)->buf;
3243 if (onig_node_str_cat(node, s, end)) {
3244 onig_node_free(node);
3245 return NULL;
3246 }
3247 return node;
3248 }
3249
3250 extern Node*
3251 onig_node_new_str(const UChar* s, const UChar* end)
3252 {
3253 return node_new_str(s, end);
3254 }
3255
3256 static Node*
3257 node_new_str_raw(UChar* s, UChar* end)
3258 {
3259 Node* node = node_new_str(s, end);
3260 CHECK_NULL_RETURN(node);
3261 NODE_STRING_SET_RAW(node);
3262 return node;
3263 }
3264
3265 static Node*
3266 node_new_empty(void)
3267 {
3268 return node_new_str(NULL, NULL);
3269 }
3270
3271 static Node*
3272 node_new_str_raw_char(UChar c)
3273 {
3274 int i;
3275 UChar p[1];
3276 Node* node;
3277
3278 p[0] = c;
3279 node = node_new_str_raw(p, p + 1);
3280
3281 /* clear buf tail */
3282 for (i = 1; i < NODE_STRING_BUF_SIZE; i++)
3283 STR_(node)->buf[i] = '\0';
3284
3285 return node;
3286 }
3287
3288 static Node*
3289 str_node_split_last_char(Node* node, OnigEncoding enc)
3290 {
3291 const UChar *p;
3292 Node* rn;
3293 StrNode* sn;
3294
3295 sn = STR_(node);
3296 rn = NULL_NODE;
3297 if (sn->end > sn->s) {
3298 p = onigenc_get_prev_char_head(enc, sn->s, sn->end);
3299 if (p && p > sn->s) { /* can be split. */
3300 rn = node_new_str(p, sn->end);
3301 CHECK_NULL_RETURN(rn);
3302 if (NODE_STRING_IS_RAW(node))
3303 NODE_STRING_SET_RAW(rn);
3304
3305 sn->end = (UChar* )p;
3306 }
3307 }
3308 return rn;
3309 }
3310
3311 static int
3312 str_node_can_be_split(Node* node, OnigEncoding enc)
3313 {
3314 StrNode* sn = STR_(node);
3315 if (sn->end > sn->s) {
3316 return ((enclen(enc, sn->s) < sn->end - sn->s) ? 1 : 0);
3317 }
3318 return 0;
3319 }
3320
3321 extern int
3322 onig_scan_unsigned_number(UChar** src, const UChar* end, OnigEncoding enc)
3323 {
3324 unsigned int num, val;
3325 OnigCodePoint c;
3326 UChar* p = *src;
3327 PFETCH_READY;
3328
3329 num = 0;
3330 while (! PEND) {
3331 PFETCH(c);
3332 if (IS_CODE_DIGIT_ASCII(enc, c)) {
3333 val = (unsigned int )DIGITVAL(c);
3334 if ((INT_MAX_LIMIT - val) / 10UL < num)
3335 return -1; /* overflow */
3336
3337 num = num * 10 + val;
3338 }
3339 else {
3340 PUNFETCH;
3341 break;
3342 }
3343 }
3344 *src = p;
3345 return num;
3346 }
3347
3348 static int
3349 scan_unsigned_hexadecimal_number(UChar** src, UChar* end, int minlen,
3350 int maxlen, OnigEncoding enc)
3351 {
3352 OnigCodePoint c;
3353 unsigned int num, val;
3354 int n;
3355 UChar* p = *src;
3356 PFETCH_READY;
3357
3358 num = 0;
3359 n = 0;
3360 while (! PEND && n < maxlen) {
3361 PFETCH(c);
3362 if (IS_CODE_XDIGIT_ASCII(enc, c)) {
3363 n++;
3364 val = (unsigned int )XDIGITVAL(enc,c);
3365 if ((INT_MAX_LIMIT - val) / 16UL < num)
3366 return ONIGERR_TOO_BIG_NUMBER; /* overflow */
3367
3368 num = (num << 4) + XDIGITVAL(enc,c);
3369 }
3370 else {
3371 PUNFETCH;
3372 break;
3373 }
3374 }
3375
3376 if (n < minlen)
3377 return ONIGERR_INVALID_CODE_POINT_VALUE;
3378
3379 *src = p;
3380 return num;
3381 }
3382
3383 static int
3384 scan_unsigned_octal_number(UChar** src, UChar* end, int maxlen,
3385 OnigEncoding enc)
3386 {
3387 OnigCodePoint c;
3388 unsigned int num, val;
3389 UChar* p = *src;
3390 PFETCH_READY;
3391
3392 num = 0;
3393 while (! PEND && maxlen-- != 0) {
3394 PFETCH(c);
3395 if (IS_CODE_DIGIT_ASCII(enc, c) && c < '8') {
3396 val = ODIGITVAL(c);
3397 if ((INT_MAX_LIMIT - val) / 8UL < num)
3398 return -1; /* overflow */
3399
3400 num = (num << 3) + val;
3401 }
3402 else {
3403 PUNFETCH;
3404 break;
3405 }
3406 }
3407 *src = p;
3408 return num;
3409 }
3410
3411
3412 #define BB_WRITE_CODE_POINT(bbuf,pos,code) \
3413 BB_WRITE(bbuf, pos, &(code), SIZE_CODE_POINT)
3414
3415 /* data format:
3416 [n][from-1][to-1][from-2][to-2] ... [from-n][to-n]
3417 (all data size is OnigCodePoint)
3418 */
3419 static int
3420 new_code_range(BBuf** pbuf)
3421 {
3422 #define INIT_MULTI_BYTE_RANGE_SIZE (SIZE_CODE_POINT * 5)
3423 int r;
3424 OnigCodePoint n;
3425 BBuf* bbuf;
3426
3427 bbuf = *pbuf = (BBuf* )xmalloc(sizeof(BBuf));
3428 CHECK_NULL_RETURN_MEMERR(bbuf);
3429 r = BB_INIT(bbuf, INIT_MULTI_BYTE_RANGE_SIZE);
3430 if (r != 0) {
3431 xfree(bbuf);
3432 *pbuf = 0;
3433 return r;
3434 }
3435
3436 n = 0;
3437 BB_WRITE_CODE_POINT(bbuf, 0, n);
3438 return 0;
3439 }
3440
3441 static int
3442 add_code_range_to_buf(BBuf** pbuf, OnigCodePoint from, OnigCodePoint to)
3443 {
3444 int r, inc_n, pos;
3445 int low, high, bound, x;
3446 OnigCodePoint n, *data;
3447 BBuf* bbuf;
3448
3449 if (from > to) {
3450 n = from; from = to; to = n;
3451 }
3452
3453 if (IS_NULL(*pbuf)) {
3454 r = new_code_range(pbuf);
3455 if (r != 0) return r;
3456 bbuf = *pbuf;
3457 n = 0;
3458 }
3459 else {
3460 bbuf = *pbuf;
3461 GET_CODE_POINT(n, bbuf->p);
3462 }
3463 data = (OnigCodePoint* )(bbuf->p);
3464 data++;
3465
3466 for (low = 0, bound = n; low < bound; ) {
3467 x = (low + bound) >> 1;
3468 if (from > data[x*2 + 1])
3469 low = x + 1;
3470 else
3471 bound = x;
3472 }
3473
3474 high = (to == ~((OnigCodePoint )0)) ? n : low;
3475 for (bound = n; high < bound; ) {
3476 x = (high + bound) >> 1;
3477 if (to + 1 >= data[x*2])
3478 high = x + 1;
3479 else
3480 bound = x;
3481 }
3482
3483 inc_n = low + 1 - high;
3484 if (n + inc_n > ONIG_MAX_MULTI_BYTE_RANGES_NUM)
3485 return ONIGERR_TOO_MANY_MULTI_BYTE_RANGES;
3486
3487 if (inc_n != 1) {
3488 if (from > data[low*2])
3489 from = data[low*2];
3490 if (to < data[(high - 1)*2 + 1])
3491 to = data[(high - 1)*2 + 1];
3492 }
3493
3494 if (inc_n != 0 && (OnigCodePoint )high < n) {
3495 int from_pos = SIZE_CODE_POINT * (1 + high * 2);
3496 int to_pos = SIZE_CODE_POINT * (1 + (low + 1) * 2);
3497 int size = (n - high) * 2 * SIZE_CODE_POINT;
3498
3499 if (inc_n > 0) {
3500 BB_MOVE_RIGHT(bbuf, from_pos, to_pos, size);
3501 }
3502 else {
3503 BB_MOVE_LEFT_REDUCE(bbuf, from_pos, to_pos);
3504 }
3505 }
3506
3507 pos = SIZE_CODE_POINT * (1 + low * 2);
3508 BB_ENSURE_SIZE(bbuf, pos + SIZE_CODE_POINT * 2);
3509 BB_WRITE_CODE_POINT(bbuf, pos, from);
3510 BB_WRITE_CODE_POINT(bbuf, pos + SIZE_CODE_POINT, to);
3511 n += inc_n;
3512 BB_WRITE_CODE_POINT(bbuf, 0, n);
3513
3514 return 0;
3515 }
3516
3517 static int
3518 add_code_range(BBuf** pbuf, ScanEnv* env, OnigCodePoint from, OnigCodePoint to)
3519 {
3520 if (from > to) {
3521 if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_ALLOW_EMPTY_RANGE_IN_CC))
3522 return 0;
3523 else
3524 return ONIGERR_EMPTY_RANGE_IN_CHAR_CLASS;
3525 }
3526
3527 return add_code_range_to_buf(pbuf, from, to);
3528 }
3529
3530 static int
3531 not_code_range_buf(OnigEncoding enc, BBuf* bbuf, BBuf** pbuf)
3532 {
3533 int r, i, n;
3534 OnigCodePoint pre, from, *data, to = 0;
3535
3536 *pbuf = (BBuf* )NULL;
3537 if (IS_NULL(bbuf)) {
3538 set_all:
3539 return SET_ALL_MULTI_BYTE_RANGE(enc, pbuf);
3540 }
3541
3542 data = (OnigCodePoint* )(bbuf->p);
3543 GET_CODE_POINT(n, data);
3544 data++;
3545 if (n <= 0) goto set_all;
3546
3547 r = 0;
3548 pre = MBCODE_START_POS(enc);
3549 for (i = 0; i < n; i++) {
3550 from = data[i*2];
3551 to = data[i*2+1];
3552 if (pre <= from - 1) {
3553 r = add_code_range_to_buf(pbuf, pre, from - 1);
3554 if (r != 0) return r;
3555 }
3556 if (to == ~((OnigCodePoint )0)) break;
3557 pre = to + 1;
3558 }
3559 if (to < ~((OnigCodePoint )0)) {
3560 r = add_code_range_to_buf(pbuf, to + 1, ~((OnigCodePoint )0));
3561 }
3562 return r;
3563 }
3564
3565 #define SWAP_BB_NOT(bbuf1, not1, bbuf2, not2) do {\
3566 BBuf *tbuf; \
3567 int tnot; \
3568 tnot = not1; not1 = not2; not2 = tnot; \
3569 tbuf = bbuf1; bbuf1 = bbuf2; bbuf2 = tbuf; \
3570 } while (0)
3571
3572 static int
3573 or_code_range_buf(OnigEncoding enc, BBuf* bbuf1, int not1,
3574 BBuf* bbuf2, int not2, BBuf** pbuf)
3575 {
3576 int r;
3577 OnigCodePoint i, n1, *data1;
3578 OnigCodePoint from, to;
3579
3580 *pbuf = (BBuf* )NULL;
3581 if (IS_NULL(bbuf1) && IS_NULL(bbuf2)) {
3582 if (not1 != 0 || not2 != 0)
3583 return SET_ALL_MULTI_BYTE_RANGE(enc, pbuf);
3584 return 0;
3585 }
3586
3587 r = 0;
3588 if (IS_NULL(bbuf2))
3589 SWAP_BB_NOT(bbuf1, not1, bbuf2, not2);
3590
3591 if (IS_NULL(bbuf1)) {
3592 if (not1 != 0) {
3593 return SET_ALL_MULTI_BYTE_RANGE(enc, pbuf);
3594 }
3595 else {
3596 if (not2 == 0) {
3597 return bbuf_clone(pbuf, bbuf2);
3598 }
3599 else {
3600 return not_code_range_buf(enc, bbuf2, pbuf);
3601 }
3602 }
3603 }
3604
3605 if (not1 != 0)
3606 SWAP_BB_NOT(bbuf1, not1, bbuf2, not2);
3607
3608 data1 = (OnigCodePoint* )(bbuf1->p);
3609 GET_CODE_POINT(n1, data1);
3610 data1++;
3611
3612 if (not2 == 0 && not1 == 0) { /* 1 OR 2 */
3613 r = bbuf_clone(pbuf, bbuf2);
3614 }
3615 else if (not1 == 0) { /* 1 OR (not 2) */
3616 r = not_code_range_buf(enc, bbuf2, pbuf);
3617 }
3618 if (r != 0) return r;
3619
3620 for (i = 0; i < n1; i++) {
3621 from = data1[i*2];
3622 to = data1[i*2+1];
3623 r = add_code_range_to_buf(pbuf, from, to);
3624 if (r != 0) return r;
3625 }
3626 return 0;
3627 }
3628
3629 static int
3630 and_code_range1(BBuf** pbuf, OnigCodePoint from1, OnigCodePoint to1,
3631 OnigCodePoint* data, int n)
3632 {
3633 int i, r;
3634 OnigCodePoint from2, to2;
3635
3636 for (i = 0; i < n; i++) {
3637 from2 = data[i*2];
3638 to2 = data[i*2+1];
3639 if (from2 < from1) {
3640 if (to2 < from1) continue;
3641 else {
3642 from1 = to2 + 1;
3643 }
3644 }
3645 else if (from2 <= to1) {
3646 if (to2 < to1) {
3647 if (from1 <= from2 - 1) {
3648 r = add_code_range_to_buf(pbuf, from1, from2-1);
3649 if (r != 0) return r;
3650 }
3651 from1 = to2 + 1;
3652 }
3653 else {
3654 to1 = from2 - 1;
3655 }
3656 }
3657 else {
3658 from1 = from2;
3659 }
3660 if (from1 > to1) break;
3661 }
3662 if (from1 <= to1) {
3663 r = add_code_range_to_buf(pbuf, from1, to1);
3664 if (r != 0) return r;
3665 }
3666 return 0;
3667 }
3668
3669 static int
3670 and_code_range_buf(BBuf* bbuf1, int not1, BBuf* bbuf2, int not2, BBuf** pbuf)
3671 {
3672 int r;
3673 OnigCodePoint i, j, n1, n2, *data1, *data2;
3674 OnigCodePoint from, to, from1, to1, from2, to2;
3675
3676 *pbuf = (BBuf* )NULL;
3677 if (IS_NULL(bbuf1)) {
3678 if (not1 != 0 && IS_NOT_NULL(bbuf2)) /* not1 != 0 -> not2 == 0 */
3679 return bbuf_clone(pbuf, bbuf2);
3680 return 0;
3681 }
3682 else if (IS_NULL(bbuf2)) {
3683 if (not2 != 0)
3684 return bbuf_clone(pbuf, bbuf1);
3685 return 0;
3686 }
3687
3688 if (not1 != 0)
3689 SWAP_BB_NOT(bbuf1, not1, bbuf2, not2);
3690
3691 data1 = (OnigCodePoint* )(bbuf1->p);
3692 data2 = (OnigCodePoint* )(bbuf2->p);
3693 GET_CODE_POINT(n1, data1);
3694 GET_CODE_POINT(n2, data2);
3695 data1++;
3696 data2++;
3697
3698 if (not2 == 0 && not1 == 0) { /* 1 AND 2 */
3699 for (i = 0; i < n1; i++) {
3700 from1 = data1[i*2];
3701 to1 = data1[i*2+1];
3702 for (j = 0; j < n2; j++) {
3703 from2 = data2[j*2];
3704 to2 = data2[j*2+1];
3705 if (from2 > to1) break;
3706 if (to2 < from1) continue;
3707 from = MAX(from1, from2);
3708 to = MIN(to1, to2);
3709 r = add_code_range_to_buf(pbuf, from, to);
3710 if (r != 0) return r;
3711 }
3712 }
3713 }
3714 else if (not1 == 0) { /* 1 AND (not 2) */
3715 for (i = 0; i < n1; i++) {
3716 from1 = data1[i*2];
3717 to1 = data1[i*2+1];
3718 r = and_code_range1(pbuf, from1, to1, data2, n2);
3719 if (r != 0) return r;
3720 }
3721 }
3722
3723 return 0;
3724 }
3725
3726 static int
3727 and_cclass(CClassNode* dest, CClassNode* cc, OnigEncoding enc)
3728 {
3729 int r, not1, not2;
3730 BBuf *buf1, *buf2, *pbuf;
3731 BitSetRef bsr1, bsr2;
3732 BitSet bs1, bs2;
3733
3734 not1 = IS_NCCLASS_NOT(dest);
3735 bsr1 = dest->bs;
3736 buf1 = dest->mbuf;
3737 not2 = IS_NCCLASS_NOT(cc);
3738 bsr2 = cc->bs;
3739 buf2 = cc->mbuf;
3740
3741 if (not1 != 0) {
3742 bitset_invert_to(bsr1, bs1);
3743 bsr1 = bs1;
3744 }
3745 if (not2 != 0) {
3746 bitset_invert_to(bsr2, bs2);
3747 bsr2 = bs2;
3748 }
3749 bitset_and(bsr1, bsr2);
3750 if (bsr1 != dest->bs) {
3751 bitset_copy(dest->bs, bsr1);
3752 }
3753 if (not1 != 0) {
3754 bitset_invert(dest->bs);
3755 }
3756
3757 if (! ONIGENC_IS_SINGLEBYTE(enc)) {
3758 if (not1 != 0 && not2 != 0) {
3759 r = or_code_range_buf(enc, buf1, 0, buf2, 0, &pbuf);
3760 }
3761 else {
3762 r = and_code_range_buf(buf1, not1, buf2, not2, &pbuf);
3763 if (r == 0 && not1 != 0) {
3764 BBuf *tbuf;
3765 r = not_code_range_buf(enc, pbuf, &tbuf);
3766 if (r != 0) {
3767 bbuf_free(pbuf);
3768 return r;
3769 }
3770 bbuf_free(pbuf);
3771 pbuf = tbuf;
3772 }
3773 }
3774 if (r != 0) return r;
3775
3776 dest->mbuf = pbuf;
3777 bbuf_free(buf1);
3778 return r;
3779 }
3780 return 0;
3781 }
3782
3783 static int
3784 or_cclass(CClassNode* dest, CClassNode* cc, OnigEncoding enc)
3785 {
3786 int r, not1, not2;
3787 BBuf *buf1, *buf2, *pbuf;
3788 BitSetRef bsr1, bsr2;
3789 BitSet bs1, bs2;
3790
3791 not1 = IS_NCCLASS_NOT(dest);
3792 bsr1 = dest->bs;
3793 buf1 = dest->mbuf;
3794 not2 = IS_NCCLASS_NOT(cc);
3795 bsr2 = cc->bs;
3796 buf2 = cc->mbuf;
3797
3798 if (not1 != 0) {
3799 bitset_invert_to(bsr1, bs1);
3800 bsr1 = bs1;
3801 }
3802 if (not2 != 0) {
3803 bitset_invert_to(bsr2, bs2);
3804 bsr2 = bs2;
3805 }
3806 bitset_or(bsr1, bsr2);
3807 if (bsr1 != dest->bs) {
3808 bitset_copy(dest->bs, bsr1);
3809 }
3810 if (not1 != 0) {
3811 bitset_invert(dest->bs);
3812 }
3813
3814 if (! ONIGENC_IS_SINGLEBYTE(enc)) {
3815 if (not1 != 0 && not2 != 0) {
3816 r = and_code_range_buf(buf1, 0, buf2, 0, &pbuf);
3817 }
3818 else {
3819 r = or_code_range_buf(enc, buf1, not1, buf2, not2, &pbuf);
3820 if (r == 0 && not1 != 0) {
3821 BBuf *tbuf;
3822 r = not_code_range_buf(enc, pbuf, &tbuf);
3823 if (r != 0) {
3824 bbuf_free(pbuf);
3825 return r;
3826 }
3827 bbuf_free(pbuf);
3828 pbuf = tbuf;
3829 }
3830 }
3831 if (r != 0) return r;
3832
3833 dest->mbuf = pbuf;
3834 bbuf_free(buf1);
3835 return r;
3836 }
3837 else
3838 return 0;
3839 }
3840
3841 static OnigCodePoint
3842 conv_backslash_value(OnigCodePoint c, ScanEnv* env)
3843 {
3844 if (IS_SYNTAX_OP(env->syntax, ONIG_SYN_OP_ESC_CONTROL_CHARS)) {
3845 switch (c) {
3846 case 'n': return '\n';
3847 case 't': return '\t';
3848 case 'r': return '\r';
3849 case 'f': return '\f';
3850 case 'a': return '\007';
3851 case 'b': return '\010';
3852 case 'e': return '\033';
3853 case 'v':
3854 if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_ESC_V_VTAB))
3855 return '\v';
3856 break;
3857
3858 default:
3859 break;
3860 }
3861 }
3862 return c;
3863 }
3864
3865 static int
3866 is_invalid_quantifier_target(Node* node)
3867 {
3868 switch (NODE_TYPE(node)) {
3869 case NODE_ANCHOR:
3870 case NODE_GIMMICK:
3871 return 1;
3872 break;
3873
3874 case NODE_BAG:
3875 /* allow enclosed elements */
3876 /* return is_invalid_quantifier_target(NODE_BODY(node)); */
3877 break;
3878
3879 case NODE_LIST:
3880 do {
3881 if (! is_invalid_quantifier_target(NODE_CAR(node))) return 0;
3882 } while (IS_NOT_NULL(node = NODE_CDR(node)));
3883 return 0;
3884 break;
3885
3886 case NODE_ALT:
3887 do {
3888 if (is_invalid_quantifier_target(NODE_CAR(node))) return 1;
3889 } while (IS_NOT_NULL(node = NODE_CDR(node)));
3890 break;
3891
3892 default:
3893 break;
3894 }
3895 return 0;
3896 }
3897
3898 /* ?:0, *:1, +:2, ??:3, *?:4, +?:5 */
3899 static int
3900 quantifier_type_num(QuantNode* q)
3901 {
3902 if (q->greedy) {
3903 if (q->lower == 0) {
3904 if (q->upper == 1) return 0;
3905 else if (IS_INFINITE_REPEAT(q->upper)) return 1;
3906 }
3907 else if (q->lower == 1) {
3908 if (IS_INFINITE_REPEAT(q->upper)) return 2;
3909 }
3910 }
3911 else {
3912 if (q->lower == 0) {
3913 if (q->upper == 1) return 3;
3914 else if (IS_INFINITE_REPEAT(q->upper)) return 4;
3915 }
3916 else if (q->lower == 1) {
3917 if (IS_INFINITE_REPEAT(q->upper)) return 5;
3918 }
3919 }
3920 return -1;
3921 }
3922
3923
3924 enum ReduceType {
3925 RQ_ASIS = 0, /* as is */
3926 RQ_DEL = 1, /* delete parent */
3927 RQ_A, /* to '*' */
3928 RQ_AQ, /* to '*?' */
3929 RQ_QQ, /* to '??' */
3930 RQ_P_QQ, /* to '+)??' */
3931 RQ_PQ_Q /* to '+?)?' */
3932 };
3933
3934 static enum ReduceType ReduceTypeTable[6][6] = {
3935 {RQ_DEL, RQ_A, RQ_A, RQ_QQ, RQ_AQ, RQ_ASIS}, /* '?' */
3936 {RQ_DEL, RQ_DEL, RQ_DEL, RQ_P_QQ, RQ_P_QQ, RQ_DEL}, /* '*' */
3937 {RQ_A, RQ_A, RQ_DEL, RQ_ASIS, RQ_P_QQ, RQ_DEL}, /* '+' */
3938 {RQ_DEL, RQ_AQ, RQ_AQ, RQ_DEL, RQ_AQ, RQ_AQ}, /* '??' */
3939 {RQ_DEL, RQ_DEL, RQ_DEL, RQ_DEL, RQ_DEL, RQ_DEL}, /* '*?' */
3940 {RQ_ASIS, RQ_PQ_Q, RQ_DEL, RQ_AQ, RQ_AQ, RQ_DEL} /* '+?' */
3941 };
3942
3943 extern void
3944 onig_reduce_nested_quantifier(Node* pnode, Node* cnode)
3945 {
3946 int pnum, cnum;
3947 QuantNode *p, *c;
3948
3949 p = QUANT_(pnode);
3950 c = QUANT_(cnode);
3951 pnum = quantifier_type_num(p);
3952 cnum = quantifier_type_num(c);
3953 if (pnum < 0 || cnum < 0) {
3954 if ((p->lower == p->upper) && ! IS_INFINITE_REPEAT(p->upper)) {
3955 if ((c->lower == c->upper) && ! IS_INFINITE_REPEAT(c->upper)) {
3956 int n = onig_positive_int_multiply(p->lower, c->lower);
3957 if (n >= 0) {
3958 p->lower = p->upper = n;
3959 NODE_BODY(pnode) = NODE_BODY(cnode);
3960 goto remove_cnode;
3961 }
3962 }
3963 }
3964
3965 return ;
3966 }
3967
3968 switch(ReduceTypeTable[cnum][pnum]) {
3969 case RQ_DEL:
3970 *pnode = *cnode;
3971 break;
3972 case RQ_A:
3973 NODE_BODY(pnode) = NODE_BODY(cnode);
3974 p->lower = 0; p->upper = INFINITE_REPEAT; p->greedy = 1;
3975 break;
3976 case RQ_AQ:
3977 NODE_BODY(pnode) = NODE_BODY(cnode);
3978 p->lower = 0; p->upper = INFINITE_REPEAT; p->greedy = 0;
3979 break;
3980 case RQ_QQ:
3981 NODE_BODY(pnode) = NODE_BODY(cnode);
3982 p->lower = 0; p->upper = 1; p->greedy = 0;
3983 break;
3984 case RQ_P_QQ:
3985 NODE_BODY(pnode) = cnode;
3986 p->lower = 0; p->upper = 1; p->greedy = 0;
3987 c->lower = 1; c->upper = INFINITE_REPEAT; c->greedy = 1;
3988 return ;
3989 break;
3990 case RQ_PQ_Q:
3991 NODE_BODY(pnode) = cnode;
3992 p->lower = 0; p->upper = 1; p->greedy = 1;
3993 c->lower = 1; c->upper = INFINITE_REPEAT; c->greedy = 0;
3994 return ;
3995 break;
3996 case RQ_ASIS:
3997 NODE_BODY(pnode) = cnode;
3998 return ;
3999 break;
4000 }
4001
4002 remove_cnode:
4003 NODE_BODY(cnode) = NULL_NODE;
4004 onig_node_free(cnode);
4005 }
4006
4007 static int
4008 node_new_general_newline(Node** node, ScanEnv* env)
4009 {
4010 int r;
4011 int dlen, alen;
4012 UChar buf[ONIGENC_CODE_TO_MBC_MAXLEN * 2];
4013 Node* crnl;
4014 Node* ncc;
4015 Node* x;
4016 CClassNode* cc;
4017
4018 dlen = ONIGENC_CODE_TO_MBC(env->enc, 0x0d, buf);
4019 if (dlen < 0) return dlen;
4020 alen = ONIGENC_CODE_TO_MBC(env->enc, 0x0a, buf + dlen);
4021 if (alen < 0) return alen;
4022
4023 crnl = node_new_str_raw(buf, buf + dlen + alen);
4024 CHECK_NULL_RETURN_MEMERR(crnl);
4025
4026 ncc = node_new_cclass();
4027 if (IS_NULL(ncc)) goto err2;
4028
4029 cc = CCLASS_(ncc);
4030 if (dlen == 1) {
4031 bitset_set_range(cc->bs, 0x0a, 0x0d);
4032 }
4033 else {
4034 r = add_code_range(&(cc->mbuf), env, 0x0a, 0x0d);
4035 if (r != 0) {
4036 err1:
4037 onig_node_free(ncc);
4038 err2:
4039 onig_node_free(crnl);
4040 return ONIGERR_MEMORY;
4041 }
4042 }
4043
4044 if (ONIGENC_IS_UNICODE_ENCODING(env->enc)) {
4045 r = add_code_range(&(cc->mbuf), env, 0x85, 0x85);
4046 if (r != 0) goto err1;
4047 r = add_code_range(&(cc->mbuf), env, 0x2028, 0x2029);
4048 if (r != 0) goto err1;
4049 }
4050
4051 x = node_new_bag_if_else(crnl, 0, ncc);
4052 if (IS_NULL(x)) goto err1;
4053
4054 *node = x;
4055 return 0;
4056 }
4057
4058 enum TokenSyms {
4059 TK_EOT = 0, /* end of token */
4060 TK_RAW_BYTE = 1,
4061 TK_CHAR,
4062 TK_STRING,
4063 TK_CODE_POINT,
4064 TK_ANYCHAR,
4065 TK_CHAR_TYPE,
4066 TK_BACKREF,
4067 TK_CALL,
4068 TK_ANCHOR,
4069 TK_REPEAT,
4070 TK_INTERVAL,
4071 TK_ANYCHAR_ANYTIME, /* SQL '%' == .* */
4072 TK_ALT,
4073 TK_SUBEXP_OPEN,
4074 TK_SUBEXP_CLOSE,
4075 TK_CC_OPEN,
4076 TK_QUOTE_OPEN,
4077 TK_CHAR_PROPERTY, /* \p{...}, \P{...} */
4078 TK_KEEP, /* \K */
4079 TK_GENERAL_NEWLINE, /* \R */
4080 TK_NO_NEWLINE, /* \N */
4081 TK_TRUE_ANYCHAR, /* \O */
4082 TK_TEXT_SEGMENT, /* \X */
4083
4084 /* in cc */
4085 TK_CC_CLOSE,
4086 TK_CC_RANGE,
4087 TK_POSIX_BRACKET_OPEN,
4088 TK_CC_AND, /* && */
4089 TK_CC_CC_OPEN /* [ */
4090 };
4091
4092 typedef struct {
4093 enum TokenSyms type;
4094 int escaped;
4095 int base; /* is number: 8, 16 (used in [....]) */
4096 UChar* backp;
4097 union {
4098 UChar* s;
4099 int c;
4100 OnigCodePoint code;
4101 int anchor;
4102 int subtype;
4103 struct {
4104 int lower;
4105 int upper;
4106 int greedy;
4107 int possessive;
4108 } repeat;
4109 struct {
4110 int num;
4111 int ref1;
4112 int* refs;
4113 int by_name;
4114 #ifdef USE_BACKREF_WITH_LEVEL
4115 int exist_level;
4116 int level; /* \k<name+n> */
4117 #endif
4118 } backref;
4119 struct {
4120 UChar* name;
4121 UChar* name_end;
4122 int gnum;
4123 int by_number;
4124 } call;
4125 struct {
4126 int ctype;
4127 int not;
4128 } prop;
4129 } u;
4130 } PToken;
4131
4132
4133 static int
4134 fetch_interval_quantifier(UChar** src, UChar* end, PToken* tok, ScanEnv* env)
4135 {
4136 int low, up, syn_allow, non_low = 0;
4137 int r = 0;
4138 OnigCodePoint c;
4139 OnigEncoding enc = env->enc;
4140 UChar* p = *src;
4141 PFETCH_READY;
4142
4143 syn_allow = IS_SYNTAX_BV(env->syntax, ONIG_SYN_ALLOW_INVALID_INTERVAL);
4144
4145 if (PEND) {
4146 if (syn_allow)
4147 return 1; /* "....{" : OK! */
4148 else
4149 return ONIGERR_END_PATTERN_AT_LEFT_BRACE; /* "....{" syntax error */
4150 }
4151
4152 if (! syn_allow) {
4153 c = PPEEK;
4154 if (c == ')' || c == '(' || c == '|') {
4155 return ONIGERR_END_PATTERN_AT_LEFT_BRACE;
4156 }
4157 }
4158
4159 low = onig_scan_unsigned_number(&p, end, env->enc);
4160 if (low < 0) return ONIGERR_TOO_BIG_NUMBER_FOR_REPEAT_RANGE;
4161 if (low > ONIG_MAX_REPEAT_NUM)
4162 return ONIGERR_TOO_BIG_NUMBER_FOR_REPEAT_RANGE;
4163
4164 if (p == *src) { /* can't read low */
4165 if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_ALLOW_INTERVAL_LOW_ABBREV)) {
4166 /* allow {,n} as {0,n} */
4167 low = 0;
4168 non_low = 1;
4169 }
4170 else
4171 goto invalid;
4172 }
4173
4174 if (PEND) goto invalid;
4175 PFETCH(c);
4176 if (c == ',') {
4177 UChar* prev = p;
4178 up = onig_scan_unsigned_number(&p, end, env->enc);
4179 if (up < 0) return ONIGERR_TOO_BIG_NUMBER_FOR_REPEAT_RANGE;
4180 if (up > ONIG_MAX_REPEAT_NUM)
4181 return ONIGERR_TOO_BIG_NUMBER_FOR_REPEAT_RANGE;
4182
4183 if (p == prev) {
4184 if (non_low != 0)
4185 goto invalid;
4186 up = INFINITE_REPEAT; /* {n,} : {n,infinite} */
4187 }
4188 }
4189 else {
4190 if (non_low != 0)
4191 goto invalid;
4192
4193 PUNFETCH;
4194 up = low; /* {n} : exact n times */
4195 r = 2; /* fixed */
4196 }
4197
4198 if (PEND) goto invalid;
4199 PFETCH(c);
4200 if (IS_SYNTAX_OP(env->syntax, ONIG_SYN_OP_ESC_BRACE_INTERVAL)) {
4201 if (c != MC_ESC(env->syntax)) goto invalid;
4202 PFETCH(c);
4203 }
4204 if (c != '}') goto invalid;
4205
4206 if (!IS_INFINITE_REPEAT(up) && low > up) {
4207 /* {n,m}+ supported case */
4208 if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_PLUS_POSSESSIVE_INTERVAL))
4209 return ONIGERR_UPPER_SMALLER_THAN_LOWER_IN_REPEAT_RANGE;
4210
4211 tok->u.repeat.possessive = 1;
4212 {
4213 int tmp;
4214 tmp = low; low = up; up = tmp;
4215 }
4216 }
4217 else
4218 tok->u.repeat.possessive = 0;
4219
4220 tok->type = TK_INTERVAL;
4221 tok->u.repeat.lower = low;
4222 tok->u.repeat.upper = up;
4223 *src = p;
4224 return r; /* 0: normal {n,m}, 2: fixed {n} */
4225
4226 invalid:
4227 if (syn_allow) {
4228 /* *src = p; */ /* !!! Don't do this line !!! */
4229 return 1; /* OK */
4230 }
4231 else
4232 return ONIGERR_INVALID_REPEAT_RANGE_PATTERN;
4233 }
4234
4235 /* \M-, \C-, \c, or \... */
4236 static int
4237 fetch_escaped_value(UChar** src, UChar* end, ScanEnv* env, OnigCodePoint* val)
4238 {
4239 int v;
4240 OnigCodePoint c;
4241 OnigEncoding enc = env->enc;
4242 UChar* p = *src;
4243
4244 if (PEND) return ONIGERR_END_PATTERN_AT_ESCAPE;
4245
4246 PFETCH_S(c);
4247 switch (c) {
4248 case 'M':
4249 if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_ESC_CAPITAL_M_BAR_META)) {
4250 if (PEND) return ONIGERR_END_PATTERN_AT_META;
4251 PFETCH_S(c);
4252 if (c != '-') return ONIGERR_META_CODE_SYNTAX;
4253 if (PEND) return ONIGERR_END_PATTERN_AT_META;
4254 PFETCH_S(c);
4255 if (c == MC_ESC(env->syntax)) {
4256 v = fetch_escaped_value(&p, end, env, &c);
4257 if (v < 0) return v;
4258 }
4259 c = ((c & 0xff) | 0x80);
4260 }
4261 else
4262 goto backslash;
4263 break;
4264
4265 case 'C':
4266 if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_ESC_CAPITAL_C_BAR_CONTROL)) {
4267 if (PEND) return ONIGERR_END_PATTERN_AT_CONTROL;
4268 PFETCH_S(c);
4269 if (c != '-') return ONIGERR_CONTROL_CODE_SYNTAX;
4270 goto control;
4271 }
4272 else
4273 goto backslash;
4274
4275 case 'c':
4276 if (IS_SYNTAX_OP(env->syntax, ONIG_SYN_OP_ESC_C_CONTROL)) {
4277 control:
4278 if (PEND) return ONIGERR_END_PATTERN_AT_CONTROL;
4279 PFETCH_S(c);
4280 if (c == '?') {
4281 c = 0177;
4282 }
4283 else {
4284 if (c == MC_ESC(env->syntax)) {
4285 v = fetch_escaped_value(&p, end, env, &c);
4286 if (v < 0) return v;
4287 }
4288 c &= 0x9f;
4289 }
4290 break;
4291 }
4292 /* fall through */
4293
4294 default:
4295 {
4296 backslash:
4297 c = conv_backslash_value(c, env);
4298 }
4299 break;
4300 }
4301
4302 *src = p;
4303 *val = c;
4304 return 0;
4305 }
4306
4307 static int fetch_token(PToken* tok, UChar** src, UChar* end, ScanEnv* env);
4308
4309 static OnigCodePoint
4310 get_name_end_code_point(OnigCodePoint start)
4311 {
4312 switch (start) {
4313 case '<': return (OnigCodePoint )'>'; break;
4314 case '\'': return (OnigCodePoint )'\''; break;
4315 case '(': return (OnigCodePoint )')'; break;
4316 default:
4317 break;
4318 }
4319
4320 return (OnigCodePoint )0;
4321 }
4322
4323 enum REF_NUM {
4324 IS_NOT_NUM = 0,
4325 IS_ABS_NUM = 1,
4326 IS_REL_NUM = 2
4327 };
4328
4329 #ifdef USE_BACKREF_WITH_LEVEL
4330 /*
4331 \k<name+n>, \k<name-n>
4332 \k<num+n>, \k<num-n>
4333 \k<-num+n>, \k<-num-n>
4334 \k<+num+n>, \k<+num-n>
4335 */
4336 static int
4337 fetch_name_with_level(OnigCodePoint start_code, UChar** src, UChar* end,
4338 UChar** rname_end, ScanEnv* env,
4339 int* rback_num, int* rlevel, enum REF_NUM* num_type)
4340 {
4341 int r, sign, exist_level;
4342 int digit_count;
4343 OnigCodePoint end_code;
4344 OnigCodePoint c = 0;
4345 OnigEncoding enc = env->enc;
4346 UChar *name_end;
4347 UChar *pnum_head;
4348 UChar *p = *src;
4349 PFETCH_READY;
4350
4351 *rback_num = 0;
4352 exist_level = 0;
4353 *num_type = IS_NOT_NUM;
4354 sign = 1;
4355 pnum_head = *src;
4356
4357 end_code = get_name_end_code_point(start_code);
4358
4359 digit_count = 0;
4360 name_end = end;
4361 r = 0;
4362 if (PEND) {
4363 return ONIGERR_EMPTY_GROUP_NAME;
4364 }
4365 else {
4366 PFETCH(c);
4367 if (c == end_code)
4368 return ONIGERR_EMPTY_GROUP_NAME;
4369
4370 if (IS_CODE_DIGIT_ASCII(enc, c)) {
4371 *num_type = IS_ABS_NUM;
4372 digit_count++;
4373 }
4374 else if (c == '-') {
4375 *num_type = IS_REL_NUM;
4376 sign = -1;
4377 pnum_head = p;
4378 }
4379 else if (c == '+') {
4380 *num_type = IS_REL_NUM;
4381 sign = 1;
4382 pnum_head = p;
4383 }
4384 else if (!ONIGENC_IS_CODE_WORD(enc, c)) {
4385 r = ONIGERR_INVALID_CHAR_IN_GROUP_NAME;
4386 }
4387 }
4388
4389 while (!PEND) {
4390 name_end = p;
4391 PFETCH(c);
4392 if (c == end_code || c == ')' || c == '+' || c == '-') {
4393 if (*num_type != IS_NOT_NUM && digit_count == 0)
4394 r = ONIGERR_INVALID_GROUP_NAME;
4395 break;
4396 }
4397
4398 if (*num_type != IS_NOT_NUM) {
4399 if (IS_CODE_DIGIT_ASCII(enc, c)) {
4400 digit_count++;
4401 }
4402 else {
4403 r = ONIGERR_INVALID_GROUP_NAME;
4404 *num_type = IS_NOT_NUM;
4405 }
4406 }
4407 else if (!ONIGENC_IS_CODE_WORD(enc, c)) {
4408 r = ONIGERR_INVALID_CHAR_IN_GROUP_NAME;
4409 }
4410 }
4411
4412 if (r == 0 && c != end_code) {
4413 if (c == '+' || c == '-') {
4414 int level;
4415 int flag = (c == '-' ? -1 : 1);
4416
4417 if (PEND) {
4418 r = ONIGERR_INVALID_CHAR_IN_GROUP_NAME;
4419 goto end;
4420 }
4421 PFETCH(c);
4422 if (! IS_CODE_DIGIT_ASCII(enc, c)) goto err;
4423 PUNFETCH;
4424 level = onig_scan_unsigned_number(&p, end, enc);
4425 if (level < 0) return ONIGERR_TOO_BIG_NUMBER;
4426 *rlevel = (level * flag);
4427 exist_level = 1;
4428
4429 if (!PEND) {
4430 PFETCH(c);
4431 if (c == end_code)
4432 goto end;
4433 }
4434 }
4435
4436 err:
4437 name_end = end;
4438 err2:
4439 r = ONIGERR_INVALID_GROUP_NAME;
4440 }
4441
4442 end:
4443 if (r == 0) {
4444 if (*num_type != IS_NOT_NUM) {
4445 *rback_num = onig_scan_unsigned_number(&pnum_head, name_end, enc);
4446 if (*rback_num < 0) return ONIGERR_TOO_BIG_NUMBER;
4447 else if (*rback_num == 0) {
4448 if (*num_type == IS_REL_NUM)
4449 goto err2;
4450 }
4451
4452 *rback_num *= sign;
4453 }
4454
4455 *rname_end = name_end;
4456 *src = p;
4457 return (exist_level ? 1 : 0);
4458 }
4459 else {
4460 onig_scan_env_set_error_string(env, r, *src, name_end);
4461 return r;
4462 }
4463 }
4464 #endif /* USE_BACKREF_WITH_LEVEL */
4465
4466 /*
4467 ref: 0 -> define name (don't allow number name)
4468 1 -> reference name (allow number name)
4469 */
4470 static int
4471 fetch_name(OnigCodePoint start_code, UChar** src, UChar* end,
4472 UChar** rname_end, ScanEnv* env, int* rback_num,
4473 enum REF_NUM* num_type, int ref)
4474 {
4475 int r, sign;
4476 int digit_count;
4477 OnigCodePoint end_code;
4478 OnigCodePoint c = 0;
4479 OnigEncoding enc = env->enc;
4480 UChar *name_end;
4481 UChar *pnum_head;
4482 UChar *p = *src;
4483
4484 *rback_num = 0;
4485
4486 end_code = get_name_end_code_point(start_code);
4487
4488 digit_count = 0;
4489 name_end = end;
4490 pnum_head = *src;
4491 r = 0;
4492 *num_type = IS_NOT_NUM;
4493 sign = 1;
4494 if (PEND) {
4495 return ONIGERR_EMPTY_GROUP_NAME;
4496 }
4497 else {
4498 PFETCH_S(c);
4499 if (c == end_code)
4500 return ONIGERR_EMPTY_GROUP_NAME;
4501
4502 if (IS_CODE_DIGIT_ASCII(enc, c)) {
4503 if (ref == 1)
4504 *num_type = IS_ABS_NUM;
4505 else {
4506 r = ONIGERR_INVALID_GROUP_NAME;
4507 }
4508 digit_count++;
4509 }
4510 else if (c == '-') {
4511 if (ref == 1) {
4512 *num_type = IS_REL_NUM;
4513 sign = -1;
4514 pnum_head = p;
4515 }
4516 else {
4517 r = ONIGERR_INVALID_GROUP_NAME;
4518 }
4519 }
4520 else if (c == '+') {
4521 if (ref == 1) {
4522 *num_type = IS_REL_NUM;
4523 sign = 1;
4524 pnum_head = p;
4525 }
4526 else {
4527 r = ONIGERR_INVALID_GROUP_NAME;
4528 }
4529 }
4530 else if (!ONIGENC_IS_CODE_WORD(enc, c)) {
4531 r = ONIGERR_INVALID_CHAR_IN_GROUP_NAME;
4532 }
4533 }
4534
4535 if (r == 0) {
4536 while (!PEND) {
4537 name_end = p;
4538 PFETCH_S(c);
4539 if (c == end_code || c == ')') {
4540 if (*num_type != IS_NOT_NUM && digit_count == 0)
4541 r = ONIGERR_INVALID_GROUP_NAME;
4542 break;
4543 }
4544
4545 if (*num_type != IS_NOT_NUM) {
4546 if (IS_CODE_DIGIT_ASCII(enc, c)) {
4547 digit_count++;
4548 }
4549 else {
4550 if (!ONIGENC_IS_CODE_WORD(enc, c))
4551 r = ONIGERR_INVALID_CHAR_IN_GROUP_NAME;
4552 else
4553 r = ONIGERR_INVALID_GROUP_NAME;
4554
4555 *num_type = IS_NOT_NUM;
4556 }
4557 }
4558 else {
4559 if (!ONIGENC_IS_CODE_WORD(enc, c)) {
4560 r = ONIGERR_INVALID_CHAR_IN_GROUP_NAME;
4561 }
4562 }
4563 }
4564
4565 if (c != end_code) {
4566 r = ONIGERR_INVALID_GROUP_NAME;
4567 goto err;
4568 }
4569
4570 if (*num_type != IS_NOT_NUM) {
4571 *rback_num = onig_scan_unsigned_number(&pnum_head, name_end, enc);
4572 if (*rback_num < 0) return ONIGERR_TOO_BIG_NUMBER;
4573 else if (*rback_num == 0) {
4574 if (*num_type == IS_REL_NUM) {
4575 r = ONIGERR_INVALID_GROUP_NAME;
4576 goto err;
4577 }
4578 }
4579
4580 *rback_num *= sign;
4581 }
4582
4583 *rname_end = name_end;
4584 *src = p;
4585 return 0;
4586 }
4587 else {
4588 while (!PEND) {
4589 name_end = p;
4590 PFETCH_S(c);
4591 if (c == end_code || c == ')')
4592 break;
4593 }
4594 if (PEND)
4595 name_end = end;
4596
4597 err:
4598 onig_scan_env_set_error_string(env, r, *src, name_end);
4599 return r;
4600 }
4601 }
4602
4603 static void
4604 CC_ESC_WARN(ScanEnv* env, UChar *c)
4605 {
4606 if (onig_warn == onig_null_warn) return ;
4607
4608 if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_WARN_CC_OP_NOT_ESCAPED) &&
4609 IS_SYNTAX_BV(env->syntax, ONIG_SYN_BACKSLASH_ESCAPE_IN_CC)) {
4610 UChar buf[WARN_BUFSIZE];
4611 onig_snprintf_with_pattern(buf, WARN_BUFSIZE, env->enc,
4612 env->pattern, env->pattern_end,
4613 (UChar* )"character class has '%s' without escape",
4614 c);
4615 (*onig_warn)((char* )buf);
4616 }
4617 }
4618
4619 static void
4620 CLOSE_BRACKET_WITHOUT_ESC_WARN(ScanEnv* env, UChar* c)
4621 {
4622 if (onig_warn == onig_null_warn) return ;
4623
4624 if (IS_SYNTAX_BV((env)->syntax, ONIG_SYN_WARN_CC_OP_NOT_ESCAPED)) {
4625 UChar buf[WARN_BUFSIZE];
4626 onig_snprintf_with_pattern(buf, WARN_BUFSIZE, (env)->enc,
4627 (env)->pattern, (env)->pattern_end,
4628 (UChar* )"regular expression has '%s' without escape", c);
4629 (*onig_warn)((char* )buf);
4630 }
4631 }
4632
4633 static UChar*
4634 find_str_position(OnigCodePoint s[], int n, UChar* from, UChar* to,
4635 UChar **next, OnigEncoding enc)
4636 {
4637 int i;
4638 OnigCodePoint x;
4639 UChar *q;
4640 UChar *p = from;
4641
4642 while (p < to) {
4643 x = ONIGENC_MBC_TO_CODE(enc, p, to);
4644 q = p + enclen(enc, p);
4645 if (x == s[0]) {
4646 for (i = 1; i < n && q < to; i++) {
4647 x = ONIGENC_MBC_TO_CODE(enc, q, to);
4648 if (x != s[i]) break;
4649 q += enclen(enc, q);
4650 }
4651 if (i >= n) {
4652 if (IS_NOT_NULL(next))
4653 *next = q;
4654 return p;
4655 }
4656 }
4657 p = q;
4658 }
4659 return NULL_UCHARP;
4660 }
4661
4662 static int
4663 str_exist_check_with_esc(OnigCodePoint s[], int n, UChar* from, UChar* to,
4664 OnigCodePoint bad, OnigEncoding enc, OnigSyntaxType* syn)
4665 {
4666 int i, in_esc;
4667 OnigCodePoint x;
4668 UChar *q;
4669 UChar *p = from;
4670
4671 in_esc = 0;
4672 while (p < to) {
4673 if (in_esc) {
4674 in_esc = 0;
4675 p += enclen(enc, p);
4676 }
4677 else {
4678 x = ONIGENC_MBC_TO_CODE(enc, p, to);
4679 q = p + enclen(enc, p);
4680 if (x == s[0]) {
4681 for (i = 1; i < n && q < to; i++) {
4682 x = ONIGENC_MBC_TO_CODE(enc, q, to);
4683 if (x != s[i]) break;
4684 q += enclen(enc, q);
4685 }
4686 if (i >= n) return 1;
4687 p += enclen(enc, p);
4688 }
4689 else {
4690 x = ONIGENC_MBC_TO_CODE(enc, p, to);
4691 if (x == bad) return 0;
4692 else if (x == MC_ESC(syn)) in_esc = 1;
4693 p = q;
4694 }
4695 }
4696 }
4697 return 0;
4698 }
4699
4700 static int
4701 fetch_token_in_cc(PToken* tok, UChar** src, UChar* end, ScanEnv* env)
4702 {
4703 int num;
4704 OnigCodePoint c, c2;
4705 OnigSyntaxType* syn = env->syntax;
4706 OnigEncoding enc = env->enc;
4707 UChar* prev;
4708 UChar* p = *src;
4709 PFETCH_READY;
4710
4711 if (PEND) {
4712 tok->type = TK_EOT;
4713 return tok->type;
4714 }
4715
4716 PFETCH(c);
4717 tok->type = TK_CHAR;
4718 tok->base = 0;
4719 tok->u.c = c;
4720 tok->escaped = 0;
4721
4722 if (c == ']') {
4723 tok->type = TK_CC_CLOSE;
4724 }
4725 else if (c == '-') {
4726 tok->type = TK_CC_RANGE;
4727 }
4728 else if (c == MC_ESC(syn)) {
4729 if (! IS_SYNTAX_BV(syn, ONIG_SYN_BACKSLASH_ESCAPE_IN_CC))
4730 goto end;
4731
4732 if (PEND) return ONIGERR_END_PATTERN_AT_ESCAPE;
4733
4734 PFETCH(c);
4735 tok->escaped = 1;
4736 tok->u.c = c;
4737 switch (c) {
4738 case 'w':
4739 tok->type = TK_CHAR_TYPE;
4740 tok->u.prop.ctype = ONIGENC_CTYPE_WORD;
4741 tok->u.prop.not = 0;
4742 break;
4743 case 'W':
4744 tok->type = TK_CHAR_TYPE;
4745 tok->u.prop.ctype = ONIGENC_CTYPE_WORD;
4746 tok->u.prop.not = 1;
4747 break;
4748 case 'd':
4749 tok->type = TK_CHAR_TYPE;
4750 tok->u.prop.ctype = ONIGENC_CTYPE_DIGIT;
4751 tok->u.prop.not = 0;
4752 break;
4753 case 'D':
4754 tok->type = TK_CHAR_TYPE;
4755 tok->u.prop.ctype = ONIGENC_CTYPE_DIGIT;
4756 tok->u.prop.not = 1;
4757 break;
4758 case 's':
4759 tok->type = TK_CHAR_TYPE;
4760 tok->u.prop.ctype = ONIGENC_CTYPE_SPACE;
4761 tok->u.prop.not = 0;
4762 break;
4763 case 'S':
4764 tok->type = TK_CHAR_TYPE;
4765 tok->u.prop.ctype = ONIGENC_CTYPE_SPACE;
4766 tok->u.prop.not = 1;
4767 break;
4768 case 'h':
4769 if (! IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_H_XDIGIT)) break;
4770 tok->type = TK_CHAR_TYPE;
4771 tok->u.prop.ctype = ONIGENC_CTYPE_XDIGIT;
4772 tok->u.prop.not = 0;
4773 break;
4774 case 'H':
4775 if (! IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_H_XDIGIT)) break;
4776 tok->type = TK_CHAR_TYPE;
4777 tok->u.prop.ctype = ONIGENC_CTYPE_XDIGIT;
4778 tok->u.prop.not = 1;
4779 break;
4780
4781 case 'p':
4782 case 'P':
4783 if (PEND) break;
4784
4785 c2 = PPEEK;
4786 if (c2 == '{' &&
4787 IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_P_BRACE_CHAR_PROPERTY)) {
4788 PINC;
4789 tok->type = TK_CHAR_PROPERTY;
4790 tok->u.prop.not = c == 'P';
4791
4792 if (!PEND && IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_P_BRACE_CIRCUMFLEX_NOT)) {
4793 PFETCH(c2);
4794 if (c2 == '^') {
4795 tok->u.prop.not = tok->u.prop.not == 0;
4796 }
4797 else
4798 PUNFETCH;
4799 }
4800 }
4801 break;
4802
4803 case 'o':
4804 if (PEND) break;
4805
4806 prev = p;
4807 if (PPEEK_IS('{') && IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_O_BRACE_OCTAL)) {
4808 PINC;
4809 num = scan_unsigned_octal_number(&p, end, 11, enc);
4810 if (num < 0) return ONIGERR_TOO_BIG_WIDE_CHAR_VALUE;
4811 if (!PEND) {
4812 c2 = PPEEK;
4813 if (IS_CODE_DIGIT_ASCII(enc, c2))
4814 return ONIGERR_TOO_LONG_WIDE_CHAR_VALUE;
4815 }
4816
4817 if (p > prev + enclen(enc, prev) && !PEND && (PPEEK_IS('}'))) {
4818 PINC;
4819 tok->type = TK_CODE_POINT;
4820 tok->base = 8;
4821 tok->u.code = (OnigCodePoint )num;
4822 }
4823 else {
4824 /* can't read nothing or invalid format */
4825 p = prev;
4826 }
4827 }
4828 break;
4829
4830 case 'x':
4831 if (PEND) break;
4832
4833 prev = p;
4834 if (PPEEK_IS('{') && IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_X_BRACE_HEX8)) {
4835 PINC;
4836 num = scan_unsigned_hexadecimal_number(&p, end, 0, 8, enc);
4837 if (num < 0) {
4838 if (num == ONIGERR_TOO_BIG_NUMBER)
4839 return ONIGERR_TOO_BIG_WIDE_CHAR_VALUE;
4840 else
4841 return num;
4842 }
4843 if (!PEND) {
4844 c2 = PPEEK;
4845 if (IS_CODE_XDIGIT_ASCII(enc, c2))
4846 return ONIGERR_TOO_LONG_WIDE_CHAR_VALUE;
4847 }
4848
4849 if (p > prev + enclen(enc, prev) && !PEND && (PPEEK_IS('}'))) {
4850 PINC;
4851 tok->type = TK_CODE_POINT;
4852 tok->base = 16;
4853 tok->u.code = (OnigCodePoint )num;
4854 }
4855 else {
4856 /* can't read nothing or invalid format */
4857 p = prev;
4858 }
4859 }
4860 else if (IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_X_HEX2)) {
4861 num = scan_unsigned_hexadecimal_number(&p, end, 0, 2, enc);
4862 if (num < 0) return num;
4863 if (p == prev) { /* can't read nothing. */
4864 num = 0; /* but, it's not error */
4865 }
4866 tok->type = TK_RAW_BYTE;
4867 tok->base = 16;
4868 tok->u.c = num;
4869 }
4870 break;
4871
4872 case 'u':
4873 if (PEND) break;
4874
4875 prev = p;
4876 if (IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_U_HEX4)) {
4877 num = scan_unsigned_hexadecimal_number(&p, end, 4, 4, enc);
4878 if (num < 0) return num;
4879 if (p == prev) { /* can't read nothing. */
4880 num = 0; /* but, it's not error */
4881 }
4882 tok->type = TK_CODE_POINT;
4883 tok->base = 16;
4884 tok->u.code = (OnigCodePoint )num;
4885 }
4886 break;
4887
4888 case '0':
4889 case '1': case '2': case '3': case '4': case '5': case '6': case '7':
4890 if (IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_OCTAL3)) {
4891 PUNFETCH;
4892 prev = p;
4893 num = scan_unsigned_octal_number(&p, end, 3, enc);
4894 if (num < 0 || num >= 256) return ONIGERR_TOO_BIG_NUMBER;
4895 if (p == prev) { /* can't read nothing. */
4896 num = 0; /* but, it's not error */
4897 }
4898 tok->type = TK_RAW_BYTE;
4899 tok->base = 8;
4900 tok->u.c = num;
4901 }
4902 break;
4903
4904 default:
4905 PUNFETCH;
4906 num = fetch_escaped_value(&p, end, env, &c2);
4907 if (num < 0) return num;
4908 if (tok->u.c != c2) {
4909 tok->u.code = c2;
4910 tok->type = TK_CODE_POINT;
4911 }
4912 break;
4913 }
4914 }
4915 else if (c == '[') {
4916 if (IS_SYNTAX_OP(syn, ONIG_SYN_OP_POSIX_BRACKET) && (PPEEK_IS(':'))) {
4917 OnigCodePoint send[] = { (OnigCodePoint )':', (OnigCodePoint )']' };
4918 tok->backp = p; /* point at '[' is read */
4919 PINC;
4920 if (str_exist_check_with_esc(send, 2, p, end,
4921 (OnigCodePoint )']', enc, syn)) {
4922 tok->type = TK_POSIX_BRACKET_OPEN;
4923 }
4924 else {
4925 PUNFETCH;
4926 goto cc_in_cc;
4927 }
4928 }
4929 else {
4930 cc_in_cc:
4931 if (IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_CCLASS_SET_OP)) {
4932 tok->type = TK_CC_CC_OPEN;
4933 }
4934 else {
4935 CC_ESC_WARN(env, (UChar* )"[");
4936 }
4937 }
4938 }
4939 else if (c == '&') {
4940 if (IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_CCLASS_SET_OP) &&
4941 !PEND && (PPEEK_IS('&'))) {
4942 PINC;
4943 tok->type = TK_CC_AND;
4944 }
4945 }
4946
4947 end:
4948 *src = p;
4949 return tok->type;
4950 }
4951
4952 static int
4953 fetch_token(PToken* tok, UChar** src, UChar* end, ScanEnv* env)
4954 {
4955 int r, num;
4956 OnigCodePoint c;
4957 OnigEncoding enc = env->enc;
4958 OnigSyntaxType* syn = env->syntax;
4959 UChar* prev;
4960 UChar* p = *src;
4961 PFETCH_READY;
4962
4963 start:
4964 if (PEND) {
4965 tok->type = TK_EOT;
4966 return tok->type;
4967 }
4968
4969 tok->type = TK_STRING;
4970 tok->base = 0;
4971 tok->backp = p;
4972
4973 PFETCH(c);
4974 if (IS_MC_ESC_CODE(c, syn)) {
4975 if (PEND) return ONIGERR_END_PATTERN_AT_ESCAPE;
4976
4977 tok->backp = p;
4978 PFETCH(c);
4979
4980 tok->u.c = c;
4981 tok->escaped = 1;
4982 switch (c) {
4983 case '*':
4984 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_ASTERISK_ZERO_INF)) break;
4985 tok->type = TK_REPEAT;
4986 tok->u.repeat.lower = 0;
4987 tok->u.repeat.upper = INFINITE_REPEAT;
4988 goto greedy_check;
4989 break;
4990
4991 case '+':
4992 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_PLUS_ONE_INF)) break;
4993 tok->type = TK_REPEAT;
4994 tok->u.repeat.lower = 1;
4995 tok->u.repeat.upper = INFINITE_REPEAT;
4996 goto greedy_check;
4997 break;
4998
4999 case '?':
5000 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_QMARK_ZERO_ONE)) break;
5001 tok->type = TK_REPEAT;
5002 tok->u.repeat.lower = 0;
5003 tok->u.repeat.upper = 1;
5004 greedy_check:
5005 tok->u.repeat.possessive = 0;
5006 greedy_check2:
5007 if (!PEND && PPEEK_IS('?') &&
5008 IS_SYNTAX_OP(syn, ONIG_SYN_OP_QMARK_NON_GREEDY) &&
5009 tok->u.repeat.possessive == 0) {
5010 PFETCH(c);
5011 tok->u.repeat.greedy = 0;
5012 tok->u.repeat.possessive = 0;
5013 }
5014 else {
5015 possessive_check:
5016 tok->u.repeat.greedy = 1;
5017 if (!PEND && PPEEK_IS('+') &&
5018 ((IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_PLUS_POSSESSIVE_REPEAT) &&
5019 tok->type != TK_INTERVAL) ||
5020 (IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_PLUS_POSSESSIVE_INTERVAL) &&
5021 tok->type == TK_INTERVAL)) &&
5022 tok->u.repeat.possessive == 0) {
5023 PFETCH(c);
5024 tok->u.repeat.possessive = 1;
5025 }
5026 }
5027 break;
5028
5029 case '{':
5030 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_BRACE_INTERVAL)) break;
5031 r = fetch_interval_quantifier(&p, end, tok, env);
5032 if (r < 0) return r; /* error */
5033 if (r == 0) goto greedy_check2;
5034 else if (r == 2) { /* {n} */
5035 if (IS_SYNTAX_BV(syn, ONIG_SYN_FIXED_INTERVAL_IS_GREEDY_ONLY))
5036 goto possessive_check;
5037
5038 goto greedy_check2;
5039 }
5040 /* r == 1 : normal char */
5041 break;
5042
5043 case '|':
5044 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_VBAR_ALT)) break;
5045 tok->type = TK_ALT;
5046 break;
5047
5048 case '(':
5049 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_LPAREN_SUBEXP)) break;
5050 tok->type = TK_SUBEXP_OPEN;
5051 break;
5052
5053 case ')':
5054 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_LPAREN_SUBEXP)) break;
5055 tok->type = TK_SUBEXP_CLOSE;
5056 break;
5057
5058 case 'w':
5059 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_W_WORD)) break;
5060 tok->type = TK_CHAR_TYPE;
5061 tok->u.prop.ctype = ONIGENC_CTYPE_WORD;
5062 tok->u.prop.not = 0;
5063 break;
5064
5065 case 'W':
5066 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_W_WORD)) break;
5067 tok->type = TK_CHAR_TYPE;
5068 tok->u.prop.ctype = ONIGENC_CTYPE_WORD;
5069 tok->u.prop.not = 1;
5070 break;
5071
5072 case 'b':
5073 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_B_WORD_BOUND)) break;
5074 tok->type = TK_ANCHOR;
5075 tok->u.anchor = ANCR_WORD_BOUNDARY;
5076 break;
5077
5078 case 'B':
5079 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_B_WORD_BOUND)) break;
5080 tok->type = TK_ANCHOR;
5081 tok->u.anchor = ANCR_NO_WORD_BOUNDARY;
5082 break;
5083
5084 case 'y':
5085 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP2_ESC_X_Y_TEXT_SEGMENT)) break;
5086 tok->type = TK_ANCHOR;
5087 tok->u.anchor = ANCR_TEXT_SEGMENT_BOUNDARY;
5088 break;
5089
5090 case 'Y':
5091 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP2_ESC_X_Y_TEXT_SEGMENT)) break;
5092 tok->type = TK_ANCHOR;
5093 tok->u.anchor = ANCR_NO_TEXT_SEGMENT_BOUNDARY;
5094 break;
5095
5096 #ifdef USE_WORD_BEGIN_END
5097 case '<':
5098 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_LTGT_WORD_BEGIN_END)) break;
5099 tok->type = TK_ANCHOR;
5100 tok->u.anchor = ANCR_WORD_BEGIN;
5101 break;
5102
5103 case '>':
5104 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_LTGT_WORD_BEGIN_END)) break;
5105 tok->type = TK_ANCHOR;
5106 tok->u.anchor = ANCR_WORD_END;
5107 break;
5108 #endif
5109
5110 case 's':
5111 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_S_WHITE_SPACE)) break;
5112 tok->type = TK_CHAR_TYPE;
5113 tok->u.prop.ctype = ONIGENC_CTYPE_SPACE;
5114 tok->u.prop.not = 0;
5115 break;
5116
5117 case 'S':
5118 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_S_WHITE_SPACE)) break;
5119 tok->type = TK_CHAR_TYPE;
5120 tok->u.prop.ctype = ONIGENC_CTYPE_SPACE;
5121 tok->u.prop.not = 1;
5122 break;
5123
5124 case 'd':
5125 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_D_DIGIT)) break;
5126 tok->type = TK_CHAR_TYPE;
5127 tok->u.prop.ctype = ONIGENC_CTYPE_DIGIT;
5128 tok->u.prop.not = 0;
5129 break;
5130
5131 case 'D':
5132 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_D_DIGIT)) break;
5133 tok->type = TK_CHAR_TYPE;
5134 tok->u.prop.ctype = ONIGENC_CTYPE_DIGIT;
5135 tok->u.prop.not = 1;
5136 break;
5137
5138 case 'h':
5139 if (! IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_H_XDIGIT)) break;
5140 tok->type = TK_CHAR_TYPE;
5141 tok->u.prop.ctype = ONIGENC_CTYPE_XDIGIT;
5142 tok->u.prop.not = 0;
5143 break;
5144
5145 case 'H':
5146 if (! IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_H_XDIGIT)) break;
5147 tok->type = TK_CHAR_TYPE;
5148 tok->u.prop.ctype = ONIGENC_CTYPE_XDIGIT;
5149 tok->u.prop.not = 1;
5150 break;
5151
5152 case 'K':
5153 if (! IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_CAPITAL_K_KEEP)) break;
5154 tok->type = TK_KEEP;
5155 break;
5156
5157 case 'R':
5158 if (! IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_CAPITAL_R_GENERAL_NEWLINE)) break;
5159 tok->type = TK_GENERAL_NEWLINE;
5160 break;
5161
5162 case 'N':
5163 if (! IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_CAPITAL_N_O_SUPER_DOT)) break;
5164 tok->type = TK_NO_NEWLINE;
5165 break;
5166
5167 case 'O':
5168 if (! IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_CAPITAL_N_O_SUPER_DOT)) break;
5169 tok->type = TK_TRUE_ANYCHAR;
5170 break;
5171
5172 case 'X':
5173 if (! IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_X_Y_TEXT_SEGMENT)) break;
5174 tok->type = TK_TEXT_SEGMENT;
5175 break;
5176
5177 case 'A':
5178 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_AZ_BUF_ANCHOR)) break;
5179 begin_buf:
5180 tok->type = TK_ANCHOR;
5181 tok->u.subtype = ANCR_BEGIN_BUF;
5182 break;
5183
5184 case 'Z':
5185 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_AZ_BUF_ANCHOR)) break;
5186 tok->type = TK_ANCHOR;
5187 tok->u.subtype = ANCR_SEMI_END_BUF;
5188 break;
5189
5190 case 'z':
5191 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_AZ_BUF_ANCHOR)) break;
5192 end_buf:
5193 tok->type = TK_ANCHOR;
5194 tok->u.subtype = ANCR_END_BUF;
5195 break;
5196
5197 case 'G':
5198 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_CAPITAL_G_BEGIN_ANCHOR)) break;
5199 tok->type = TK_ANCHOR;
5200 tok->u.subtype = ANCR_BEGIN_POSITION;
5201 break;
5202
5203 case '`':
5204 if (! IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_GNU_BUF_ANCHOR)) break;
5205 goto begin_buf;
5206 break;
5207
5208 case '\'':
5209 if (! IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_GNU_BUF_ANCHOR)) break;
5210 goto end_buf;
5211 break;
5212
5213 case 'o':
5214 if (PEND) break;
5215
5216 prev = p;
5217 if (PPEEK_IS('{') && IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_O_BRACE_OCTAL)) {
5218 PINC;
5219 num = scan_unsigned_octal_number(&p, end, 11, enc);
5220 if (num < 0) return ONIGERR_TOO_BIG_WIDE_CHAR_VALUE;
5221 if (!PEND) {
5222 if (IS_CODE_DIGIT_ASCII(enc, PPEEK))
5223 return ONIGERR_TOO_LONG_WIDE_CHAR_VALUE;
5224 }
5225
5226 if ((p > prev + enclen(enc, prev)) && !PEND && PPEEK_IS('}')) {
5227 PINC;
5228 tok->type = TK_CODE_POINT;
5229 tok->u.code = (OnigCodePoint )num;
5230 }
5231 else {
5232 /* can't read nothing or invalid format */
5233 p = prev;
5234 }
5235 }
5236 break;
5237
5238 case 'x':
5239 if (PEND) break;
5240
5241 prev = p;
5242 if (PPEEK_IS('{') && IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_X_BRACE_HEX8)) {
5243 PINC;
5244 num = scan_unsigned_hexadecimal_number(&p, end, 0, 8, enc);
5245 if (num < 0) {
5246 if (num == ONIGERR_TOO_BIG_NUMBER)
5247 return ONIGERR_TOO_BIG_WIDE_CHAR_VALUE;
5248 else
5249 return num;
5250 }
5251 if (!PEND) {
5252 if (IS_CODE_XDIGIT_ASCII(enc, PPEEK))
5253 return ONIGERR_TOO_LONG_WIDE_CHAR_VALUE;
5254 }
5255
5256 if ((p > prev + enclen(enc, prev)) && !PEND && PPEEK_IS('}')) {
5257 PINC;
5258 tok->type = TK_CODE_POINT;
5259 tok->u.code = (OnigCodePoint )num;
5260 }
5261 else {
5262 /* can't read nothing or invalid format */
5263 p = prev;
5264 }
5265 }
5266 else if (IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_X_HEX2)) {
5267 num = scan_unsigned_hexadecimal_number(&p, end, 0, 2, enc);
5268 if (num < 0) return num;
5269 if (p == prev) { /* can't read nothing. */
5270 num = 0; /* but, it's not error */
5271 }
5272 tok->type = TK_RAW_BYTE;
5273 tok->base = 16;
5274 tok->u.c = num;
5275 }
5276 break;
5277
5278 case 'u':
5279 if (PEND) break;
5280
5281 prev = p;
5282 if (IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_U_HEX4)) {
5283 num = scan_unsigned_hexadecimal_number(&p, end, 4, 4, enc);
5284 if (num < 0) return num;
5285 if (p == prev) { /* can't read nothing. */
5286 num = 0; /* but, it's not error */
5287 }
5288 tok->type = TK_CODE_POINT;
5289 tok->base = 16;
5290 tok->u.code = (OnigCodePoint )num;
5291 }
5292 break;
5293
5294 case '1': case '2': case '3': case '4':
5295 case '5': case '6': case '7': case '8': case '9':
5296 PUNFETCH;
5297 prev = p;
5298 num = onig_scan_unsigned_number(&p, end, enc);
5299 if (num < 0 || num > ONIG_MAX_BACKREF_NUM) {
5300 goto skip_backref;
5301 }
5302
5303 if (IS_SYNTAX_OP(syn, ONIG_SYN_OP_DECIMAL_BACKREF) &&
5304 (num <= env->num_mem || num <= 9)) { /* This spec. from GNU regex */
5305 if (IS_SYNTAX_BV(syn, ONIG_SYN_STRICT_CHECK_BACKREF)) {
5306 if (num > env->num_mem || IS_NULL(SCANENV_MEMENV(env)[num].node))
5307 return ONIGERR_INVALID_BACKREF;
5308 }
5309
5310 tok->type = TK_BACKREF;
5311 tok->u.backref.num = 1;
5312 tok->u.backref.ref1 = num;
5313 tok->u.backref.by_name = 0;
5314 #ifdef USE_BACKREF_WITH_LEVEL
5315 tok->u.backref.exist_level = 0;
5316 #endif
5317 break;
5318 }
5319
5320 skip_backref:
5321 if (c == '8' || c == '9') {
5322 /* normal char */
5323 p = prev; PINC;
5324 break;
5325 }
5326
5327 p = prev;
5328 /* fall through */
5329 case '0':
5330 if (IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_OCTAL3)) {
5331 prev = p;
5332 num = scan_unsigned_octal_number(&p, end, (c == '0' ? 2:3), enc);
5333 if (num < 0 || num >= 256) return ONIGERR_TOO_BIG_NUMBER;
5334 if (p == prev) { /* can't read nothing. */
5335 num = 0; /* but, it's not error */
5336 }
5337 tok->type = TK_RAW_BYTE;
5338 tok->base = 8;
5339 tok->u.c = num;
5340 }
5341 else if (c != '0') {
5342 PINC;
5343 }
5344 break;
5345
5346 case 'k':
5347 if (!PEND && IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_K_NAMED_BACKREF)) {
5348 PFETCH(c);
5349 if (c == '<' || c == '\'') {
5350 UChar* name_end;
5351 int* backs;
5352 int back_num;
5353 enum REF_NUM num_type;
5354
5355 prev = p;
5356
5357 #ifdef USE_BACKREF_WITH_LEVEL
5358 name_end = NULL_UCHARP; /* no need. escape gcc warning. */
5359 r = fetch_name_with_level((OnigCodePoint )c, &p, end, &name_end,
5360 env, &back_num, &tok->u.backref.level, &num_type);
5361 if (r == 1) tok->u.backref.exist_level = 1;
5362 else tok->u.backref.exist_level = 0;
5363 #else
5364 r = fetch_name(c, &p, end, &name_end, env, &back_num, &num_type, 1);
5365 #endif
5366 if (r < 0) return r;
5367
5368 if (num_type != IS_NOT_NUM) {
5369 if (num_type == IS_REL_NUM) {
5370 back_num = backref_rel_to_abs(back_num, env);
5371 }
5372 if (back_num <= 0)
5373 return ONIGERR_INVALID_BACKREF;
5374
5375 if (IS_SYNTAX_BV(syn, ONIG_SYN_STRICT_CHECK_BACKREF)) {
5376 if (back_num > env->num_mem ||
5377 IS_NULL(SCANENV_MEMENV(env)[back_num].node))
5378 return ONIGERR_INVALID_BACKREF;
5379 }
5380 tok->type = TK_BACKREF;
5381 tok->u.backref.by_name = 0;
5382 tok->u.backref.num = 1;
5383 tok->u.backref.ref1 = back_num;
5384 }
5385 else {
5386 num = name_to_group_numbers(env, prev, name_end, &backs);
5387 if (num <= 0) {
5388 return ONIGERR_UNDEFINED_NAME_REFERENCE;
5389 }
5390 if (IS_SYNTAX_BV(syn, ONIG_SYN_STRICT_CHECK_BACKREF)) {
5391 int i;
5392 for (i = 0; i < num; i++) {
5393 if (backs[i] > env->num_mem ||
5394 IS_NULL(SCANENV_MEMENV(env)[backs[i]].node))
5395 return ONIGERR_INVALID_BACKREF;
5396 }
5397 }
5398
5399 tok->type = TK_BACKREF;
5400 tok->u.backref.by_name = 1;
5401 if (num == 1) {
5402 tok->u.backref.num = 1;
5403 tok->u.backref.ref1 = backs[0];
5404 }
5405 else {
5406 tok->u.backref.num = num;
5407 tok->u.backref.refs = backs;
5408 }
5409 }
5410 }
5411 else
5412 PUNFETCH;
5413 }
5414 break;
5415
5416 #ifdef USE_CALL
5417 case 'g':
5418 if (!PEND && IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_G_SUBEXP_CALL)) {
5419 PFETCH(c);
5420 if (c == '<' || c == '\'') {
5421 int gnum;
5422 UChar* name_end;
5423 enum REF_NUM num_type;
5424
5425 prev = p;
5426 r = fetch_name((OnigCodePoint )c, &p, end, &name_end, env,
5427 &gnum, &num_type, 1);
5428 if (r < 0) return r;
5429
5430 if (num_type != IS_NOT_NUM) {
5431 if (num_type == IS_REL_NUM) {
5432 gnum = backref_rel_to_abs(gnum, env);
5433 if (gnum < 0) {
5434 onig_scan_env_set_error_string(env, ONIGERR_UNDEFINED_NAME_REFERENCE,
5435 prev, name_end);
5436 return ONIGERR_UNDEFINED_GROUP_REFERENCE;
5437 }
5438 }
5439 tok->u.call.by_number = 1;
5440 tok->u.call.gnum = gnum;
5441 }
5442 else {
5443 tok->u.call.by_number = 0;
5444 tok->u.call.gnum = 0;
5445 }
5446
5447 tok->type = TK_CALL;
5448 tok->u.call.name = prev;
5449 tok->u.call.name_end = name_end;
5450 }
5451 else
5452 PUNFETCH;
5453 }
5454 break;
5455 #endif
5456
5457 case 'Q':
5458 if (IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_CAPITAL_Q_QUOTE)) {
5459 tok->type = TK_QUOTE_OPEN;
5460 }
5461 break;
5462
5463 case 'p':
5464 case 'P':
5465 if (!PEND && PPEEK_IS('{') &&
5466 IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_P_BRACE_CHAR_PROPERTY)) {
5467 PINC;
5468 tok->type = TK_CHAR_PROPERTY;
5469 tok->u.prop.not = c == 'P';
5470
5471 if (!PEND &&
5472 IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_P_BRACE_CIRCUMFLEX_NOT)) {
5473 PFETCH(c);
5474 if (c == '^') {
5475 tok->u.prop.not = tok->u.prop.not == 0;
5476 }
5477 else
5478 PUNFETCH;
5479 }
5480 }
5481 break;
5482
5483 default:
5484 {
5485 OnigCodePoint c2;
5486
5487 PUNFETCH;
5488 num = fetch_escaped_value(&p, end, env, &c2);
5489 if (num < 0) return num;
5490 /* set_raw: */
5491 if (tok->u.c != c2) {
5492 tok->type = TK_CODE_POINT;
5493 tok->u.code = c2;
5494 }
5495 else { /* string */
5496 p = tok->backp + enclen(enc, tok->backp);
5497 }
5498 }
5499 break;
5500 }
5501 }
5502 else {
5503 tok->u.c = c;
5504 tok->escaped = 0;
5505
5506 #ifdef USE_VARIABLE_META_CHARS
5507 if ((c != ONIG_INEFFECTIVE_META_CHAR) &&
5508 IS_SYNTAX_OP(syn, ONIG_SYN_OP_VARIABLE_META_CHARACTERS)) {
5509 if (c == MC_ANYCHAR(syn))
5510 goto any_char;
5511 else if (c == MC_ANYTIME(syn))
5512 goto anytime;
5513 else if (c == MC_ZERO_OR_ONE_TIME(syn))
5514 goto zero_or_one_time;
5515 else if (c == MC_ONE_OR_MORE_TIME(syn))
5516 goto one_or_more_time;
5517 else if (c == MC_ANYCHAR_ANYTIME(syn)) {
5518 tok->type = TK_ANYCHAR_ANYTIME;
5519 goto out;
5520 }
5521 }
5522 #endif
5523
5524 switch (c) {
5525 case '.':
5526 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_DOT_ANYCHAR)) break;
5527 #ifdef USE_VARIABLE_META_CHARS
5528 any_char:
5529 #endif
5530 tok->type = TK_ANYCHAR;
5531 break;
5532
5533 case '*':
5534 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ASTERISK_ZERO_INF)) break;
5535 #ifdef USE_VARIABLE_META_CHARS
5536 anytime:
5537 #endif
5538 tok->type = TK_REPEAT;
5539 tok->u.repeat.lower = 0;
5540 tok->u.repeat.upper = INFINITE_REPEAT;
5541 goto greedy_check;
5542 break;
5543
5544 case '+':
5545 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_PLUS_ONE_INF)) break;
5546 #ifdef USE_VARIABLE_META_CHARS
5547 one_or_more_time:
5548 #endif
5549 tok->type = TK_REPEAT;
5550 tok->u.repeat.lower = 1;
5551 tok->u.repeat.upper = INFINITE_REPEAT;
5552 goto greedy_check;
5553 break;
5554
5555 case '?':
5556 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_QMARK_ZERO_ONE)) break;
5557 #ifdef USE_VARIABLE_META_CHARS
5558 zero_or_one_time:
5559 #endif
5560 tok->type = TK_REPEAT;
5561 tok->u.repeat.lower = 0;
5562 tok->u.repeat.upper = 1;
5563 goto greedy_check;
5564 break;
5565
5566 case '{':
5567 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_BRACE_INTERVAL)) break;
5568 r = fetch_interval_quantifier(&p, end, tok, env);
5569 if (r < 0) return r; /* error */
5570 if (r == 0) goto greedy_check2;
5571 else if (r == 2) { /* {n} */
5572 if (IS_SYNTAX_BV(syn, ONIG_SYN_FIXED_INTERVAL_IS_GREEDY_ONLY))
5573 goto possessive_check;
5574
5575 goto greedy_check2;
5576 }
5577 /* r == 1 : normal char */
5578 break;
5579
5580 case '|':
5581 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_VBAR_ALT)) break;
5582 tok->type = TK_ALT;
5583 break;
5584
5585 case '(':
5586 if (!PEND && PPEEK_IS('?') &&
5587 IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_QMARK_GROUP_EFFECT)) {
5588 PINC;
5589 if (! PEND) {
5590 c = PPEEK;
5591 if (c == '#') {
5592 PFETCH(c);
5593 while (1) {
5594 if (PEND) return ONIGERR_END_PATTERN_IN_GROUP;
5595 PFETCH(c);
5596 if (c == MC_ESC(syn)) {
5597 if (! PEND) PFETCH(c);
5598 }
5599 else {
5600 if (c == ')') break;
5601 }
5602 }
5603 goto start;
5604 }
5605 else if (IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_QMARK_PERL_SUBEXP_CALL)) {
5606 int gnum;
5607 UChar* name;
5608 UChar* name_end;
5609 enum REF_NUM num_type;
5610
5611 switch (c) {
5612 case '&':
5613 {
5614 PINC;
5615 name = p;
5616 r = fetch_name((OnigCodePoint )'(', &p, end, &name_end, env, &gnum,
5617 &num_type, 0);
5618 if (r < 0) return r;
5619
5620 tok->type = TK_CALL;
5621 tok->u.call.by_number = 0;
5622 tok->u.call.gnum = 0;
5623 tok->u.call.name = name;
5624 tok->u.call.name_end = name_end;
5625 }
5626 break;
5627
5628 case 'R':
5629 tok->type = TK_CALL;
5630 tok->u.call.by_number = 1;
5631 tok->u.call.gnum = 0;
5632 tok->u.call.name = p;
5633 PINC;
5634 if (! PPEEK_IS(')')) return ONIGERR_UNDEFINED_GROUP_OPTION;
5635 tok->u.call.name_end = p;
5636 break;
5637
5638 case '-':
5639 case '+':
5640 goto lparen_qmark_num;
5641 break;
5642 default:
5643 if (! ONIGENC_IS_CODE_DIGIT(enc, c)) goto lparen_qmark_end;
5644
5645 lparen_qmark_num:
5646 {
5647 name = p;
5648 r = fetch_name((OnigCodePoint )'(', &p, end, &name_end, env,
5649 &gnum, &num_type, 1);
5650 if (r < 0) return r;
5651
5652 if (num_type == IS_NOT_NUM) {
5653 return ONIGERR_INVALID_GROUP_NAME;
5654 }
5655 else {
5656 if (num_type == IS_REL_NUM) {
5657 gnum = backref_rel_to_abs(gnum, env);
5658 if (gnum < 0) {
5659 onig_scan_env_set_error_string(env,
5660 ONIGERR_UNDEFINED_NAME_REFERENCE, name, name_end);
5661 return ONIGERR_UNDEFINED_GROUP_REFERENCE;
5662 }
5663 }
5664 tok->u.call.by_number = 1;
5665 tok->u.call.gnum = gnum;
5666 }
5667
5668 tok->type = TK_CALL;
5669 tok->u.call.name = name;
5670 tok->u.call.name_end = name_end;
5671 }
5672 break;
5673 }
5674 }
5675 }
5676 lparen_qmark_end:
5677 PUNFETCH;
5678 }
5679
5680 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_LPAREN_SUBEXP)) break;
5681 tok->type = TK_SUBEXP_OPEN;
5682 break;
5683
5684 case ')':
5685 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_LPAREN_SUBEXP)) break;
5686 tok->type = TK_SUBEXP_CLOSE;
5687 break;
5688
5689 case '^':
5690 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_LINE_ANCHOR)) break;
5691 tok->type = TK_ANCHOR;
5692 tok->u.subtype = (IS_SINGLELINE(env->options)
5693 ? ANCR_BEGIN_BUF : ANCR_BEGIN_LINE);
5694 break;
5695
5696 case '$':
5697 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_LINE_ANCHOR)) break;
5698 tok->type = TK_ANCHOR;
5699 tok->u.subtype = (IS_SINGLELINE(env->options)
5700 ? ANCR_SEMI_END_BUF : ANCR_END_LINE);
5701 break;
5702
5703 case '[':
5704 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_BRACKET_CC)) break;
5705 tok->type = TK_CC_OPEN;
5706 break;
5707
5708 case ']':
5709 if (*src > env->pattern) /* /].../ is allowed. */
5710 CLOSE_BRACKET_WITHOUT_ESC_WARN(env, (UChar* )"]");
5711 break;
5712
5713 case '#':
5714 if (IS_EXTEND(env->options)) {
5715 while (!PEND) {
5716 PFETCH(c);
5717 if (ONIGENC_IS_CODE_NEWLINE(enc, c))
5718 break;
5719 }
5720 goto start;
5721 break;
5722 }
5723 break;
5724
5725 case ' ': case '\t': case '\n': case '\r': case '\f':
5726 if (IS_EXTEND(env->options))
5727 goto start;
5728 break;
5729
5730 default:
5731 /* string */
5732 break;
5733 }
5734 }
5735
5736 #ifdef USE_VARIABLE_META_CHARS
5737 out:
5738 #endif
5739 *src = p;
5740 return tok->type;
5741 }
5742
5743 static int
5744 add_ctype_to_cc_by_range(CClassNode* cc, int ctype ARG_UNUSED, int not,
5745 OnigEncoding enc ARG_UNUSED, OnigCodePoint sb_out,
5746 const OnigCodePoint mbr[])
5747 {
5748 int i, r;
5749 OnigCodePoint j;
5750
5751 int n = ONIGENC_CODE_RANGE_NUM(mbr);
5752
5753 if (not == 0) {
5754 for (i = 0; i < n; i++) {
5755 for (j = ONIGENC_CODE_RANGE_FROM(mbr, i);
5756 j <= ONIGENC_CODE_RANGE_TO(mbr, i); j++) {
5757 if (j >= sb_out) {
5758 if (j > ONIGENC_CODE_RANGE_FROM(mbr, i)) {
5759 r = add_code_range_to_buf(&(cc->mbuf), j,
5760 ONIGENC_CODE_RANGE_TO(mbr, i));
5761 if (r != 0) return r;
5762 i++;
5763 }
5764
5765 goto sb_end;
5766 }
5767 BITSET_SET_BIT(cc->bs, j);
5768 }
5769 }
5770
5771 sb_end:
5772 for ( ; i < n; i++) {
5773 r = add_code_range_to_buf(&(cc->mbuf),
5774 ONIGENC_CODE_RANGE_FROM(mbr, i),
5775 ONIGENC_CODE_RANGE_TO(mbr, i));
5776 if (r != 0) return r;
5777 }
5778 }
5779 else {
5780 OnigCodePoint prev = 0;
5781
5782 for (i = 0; i < n; i++) {
5783 for (j = prev; j < ONIGENC_CODE_RANGE_FROM(mbr, i); j++) {
5784 if (j >= sb_out) {
5785 goto sb_end2;
5786 }
5787 BITSET_SET_BIT(cc->bs, j);
5788 }
5789 prev = ONIGENC_CODE_RANGE_TO(mbr, i) + 1;
5790 }
5791 for (j = prev; j < sb_out; j++) {
5792 BITSET_SET_BIT(cc->bs, j);
5793 }
5794
5795 sb_end2:
5796 prev = sb_out;
5797
5798 for (i = 0; i < n; i++) {
5799 if (prev < ONIGENC_CODE_RANGE_FROM(mbr, i)) {
5800 r = add_code_range_to_buf(&(cc->mbuf), prev,
5801 ONIGENC_CODE_RANGE_FROM(mbr, i) - 1);
5802 if (r != 0) return r;
5803 }
5804 prev = ONIGENC_CODE_RANGE_TO(mbr, i) + 1;
5805 if (prev == 0) goto end;
5806 }
5807
5808 r = add_code_range_to_buf(&(cc->mbuf), prev, MAX_CODE_POINT);
5809 if (r != 0) return r;
5810 }
5811
5812 end:
5813 return 0;
5814 }
5815
5816 static int
5817 add_ctype_to_cc_by_range_limit(CClassNode* cc, int ctype ARG_UNUSED, int not,
5818 OnigEncoding enc ARG_UNUSED,
5819 OnigCodePoint sb_out,
5820 const OnigCodePoint mbr[], OnigCodePoint limit)
5821 {
5822 int i, r;
5823 OnigCodePoint j;
5824 OnigCodePoint from;
5825 OnigCodePoint to;
5826
5827 int n = ONIGENC_CODE_RANGE_NUM(mbr);
5828
5829 if (not == 0) {
5830 for (i = 0; i < n; i++) {
5831 for (j = ONIGENC_CODE_RANGE_FROM(mbr, i);
5832 j <= ONIGENC_CODE_RANGE_TO(mbr, i); j++) {
5833 if (j > limit) goto end;
5834 if (j >= sb_out) {
5835 if (j > ONIGENC_CODE_RANGE_FROM(mbr, i)) {
5836 to = ONIGENC_CODE_RANGE_TO(mbr, i);
5837 if (to > limit) to = limit;
5838 r = add_code_range_to_buf(&(cc->mbuf), j, to);
5839 if (r != 0) return r;
5840 i++;
5841 }
5842
5843 goto sb_end;
5844 }
5845 BITSET_SET_BIT(cc->bs, j);
5846 }
5847 }
5848
5849 sb_end:
5850 for ( ; i < n; i++) {
5851 from = ONIGENC_CODE_RANGE_FROM(mbr, i);
5852 to = ONIGENC_CODE_RANGE_TO(mbr, i);
5853 if (from > limit) break;
5854 if (to > limit) to = limit;
5855 r = add_code_range_to_buf(&(cc->mbuf), from, to);
5856 if (r != 0) return r;
5857 }
5858 }
5859 else {
5860 OnigCodePoint prev = 0;
5861
5862 for (i = 0; i < n; i++) {
5863 from = ONIGENC_CODE_RANGE_FROM(mbr, i);
5864 if (from > limit) {
5865 for (j = prev; j < sb_out; j++) {
5866 BITSET_SET_BIT(cc->bs, j);
5867 }
5868 goto sb_end2;
5869 }
5870 for (j = prev; j < from; j++) {
5871 if (j >= sb_out) goto sb_end2;
5872 BITSET_SET_BIT(cc->bs, j);
5873 }
5874 prev = ONIGENC_CODE_RANGE_TO(mbr, i);
5875 if (prev > limit) prev = limit;
5876 prev++;
5877 if (prev == 0) goto end;
5878 }
5879 for (j = prev; j < sb_out; j++) {
5880 BITSET_SET_BIT(cc->bs, j);
5881 }
5882
5883 sb_end2:
5884 prev = sb_out;
5885
5886 for (i = 0; i < n; i++) {
5887 from = ONIGENC_CODE_RANGE_FROM(mbr, i);
5888 if (from > limit) goto last;
5889
5890 if (prev < from) {
5891 r = add_code_range_to_buf(&(cc->mbuf), prev, from - 1);
5892 if (r != 0) return r;
5893 }
5894 prev = ONIGENC_CODE_RANGE_TO(mbr, i);
5895 if (prev > limit) prev = limit;
5896 prev++;
5897 if (prev == 0) goto end;
5898 }
5899
5900 last:
5901 r = add_code_range_to_buf(&(cc->mbuf), prev, MAX_CODE_POINT);
5902 if (r != 0) return r;
5903 }
5904
5905 end:
5906 return 0;
5907 }
5908
5909 static int
5910 add_ctype_to_cc(CClassNode* cc, int ctype, int not, ScanEnv* env)
5911 {
5912 #define ASCII_LIMIT 127
5913
5914 int c, r;
5915 int ascii_mode;
5916 const OnigCodePoint *ranges;
5917 OnigCodePoint limit;
5918 OnigCodePoint sb_out;
5919 OnigEncoding enc = env->enc;
5920
5921 ascii_mode = IS_ASCII_MODE_CTYPE_OPTION(ctype, env->options);
5922
5923 r = ONIGENC_GET_CTYPE_CODE_RANGE(enc, ctype, &sb_out, &ranges);
5924 if (r == 0) {
5925 if (ascii_mode == 0)
5926 r = add_ctype_to_cc_by_range(cc, ctype, not, env->enc, sb_out, ranges);
5927 else
5928 r = add_ctype_to_cc_by_range_limit(cc, ctype, not, env->enc, sb_out,
5929 ranges, ASCII_LIMIT);
5930 return r;
5931 }
5932 else if (r != ONIG_NO_SUPPORT_CONFIG) {
5933 return r;
5934 }
5935
5936 r = 0;
5937 limit = ascii_mode ? ASCII_LIMIT : SINGLE_BYTE_SIZE;
5938
5939 switch (ctype) {
5940 case ONIGENC_CTYPE_ALPHA:
5941 case ONIGENC_CTYPE_BLANK:
5942 case ONIGENC_CTYPE_CNTRL:
5943 case ONIGENC_CTYPE_DIGIT:
5944 case ONIGENC_CTYPE_LOWER:
5945 case ONIGENC_CTYPE_PUNCT:
5946 case ONIGENC_CTYPE_SPACE:
5947 case ONIGENC_CTYPE_UPPER:
5948 case ONIGENC_CTYPE_XDIGIT:
5949 case ONIGENC_CTYPE_ASCII:
5950 case ONIGENC_CTYPE_ALNUM:
5951 if (not != 0) {
5952 for (c = 0; c < (int )limit; c++) {
5953 if (! ONIGENC_IS_CODE_CTYPE(enc, (OnigCodePoint )c, ctype))
5954 BITSET_SET_BIT(cc->bs, c);
5955 }
5956 for (c = limit; c < SINGLE_BYTE_SIZE; c++) {
5957 BITSET_SET_BIT(cc->bs, c);
5958 }
5959
5960 ADD_ALL_MULTI_BYTE_RANGE(enc, cc->mbuf);
5961 }
5962 else {
5963 for (c = 0; c < (int )limit; c++) {
5964 if (ONIGENC_IS_CODE_CTYPE(enc, (OnigCodePoint )c, ctype))
5965 BITSET_SET_BIT(cc->bs, c);
5966 }
5967 }
5968 break;
5969
5970 case ONIGENC_CTYPE_GRAPH:
5971 case ONIGENC_CTYPE_PRINT:
5972 case ONIGENC_CTYPE_WORD:
5973 if (not != 0) {
5974 for (c = 0; c < (int )limit; c++) {
5975 if (ONIGENC_CODE_TO_MBCLEN(enc, c) > 0 /* check invalid code point */
5976 && ! ONIGENC_IS_CODE_CTYPE(enc, (OnigCodePoint )c, ctype))
5977 BITSET_SET_BIT(cc->bs, c);
5978 }
5979 for (c = limit; c < SINGLE_BYTE_SIZE; c++) {
5980 if (ONIGENC_CODE_TO_MBCLEN(enc, c) > 0)
5981 BITSET_SET_BIT(cc->bs, c);
5982 }
5983 }
5984 else {
5985 for (c = 0; c < (int )limit; c++) {
5986 if (ONIGENC_IS_CODE_CTYPE(enc, (OnigCodePoint )c, ctype))
5987 BITSET_SET_BIT(cc->bs, c);
5988 }
5989 if (ascii_mode == 0)
5990 ADD_ALL_MULTI_BYTE_RANGE(enc, cc->mbuf);
5991 }
5992 break;
5993
5994 default:
5995 return ONIGERR_PARSER_BUG;
5996 break;
5997 }
5998
5999 return r;
6000 }
6001
6002 static int
6003 parse_posix_bracket(CClassNode* cc, UChar** src, UChar* end, ScanEnv* env)
6004 {
6005 #define POSIX_BRACKET_CHECK_LIMIT_LENGTH 20
6006 #define POSIX_BRACKET_NAME_MIN_LEN 4
6007
6008 static PosixBracketEntryType PBS[] = {
6009 { (UChar* )"alnum", ONIGENC_CTYPE_ALNUM, 5 },
6010 { (UChar* )"alpha", ONIGENC_CTYPE_ALPHA, 5 },
6011 { (UChar* )"blank", ONIGENC_CTYPE_BLANK, 5 },
6012 { (UChar* )"cntrl", ONIGENC_CTYPE_CNTRL, 5 },
6013 { (UChar* )"digit", ONIGENC_CTYPE_DIGIT, 5 },
6014 { (UChar* )"graph", ONIGENC_CTYPE_GRAPH, 5 },
6015 { (UChar* )"lower", ONIGENC_CTYPE_LOWER, 5 },
6016 { (UChar* )"print", ONIGENC_CTYPE_PRINT, 5 },
6017 { (UChar* )"punct", ONIGENC_CTYPE_PUNCT, 5 },
6018 { (UChar* )"space", ONIGENC_CTYPE_SPACE, 5 },
6019 { (UChar* )"upper", ONIGENC_CTYPE_UPPER, 5 },
6020 { (UChar* )"xdigit", ONIGENC_CTYPE_XDIGIT, 6 },
6021 { (UChar* )"ascii", ONIGENC_CTYPE_ASCII, 5 },
6022 { (UChar* )"word", ONIGENC_CTYPE_WORD, 4 },
6023 { (UChar* )NULL, -1, 0 }
6024 };
6025
6026 PosixBracketEntryType *pb;
6027 int not, i, r;
6028 OnigCodePoint c;
6029 OnigEncoding enc = env->enc;
6030 UChar *p = *src;
6031
6032 if (PPEEK_IS('^')) {
6033 PINC_S;
6034 not = 1;
6035 }
6036 else
6037 not = 0;
6038
6039 if (onigenc_strlen(enc, p, end) < POSIX_BRACKET_NAME_MIN_LEN + 3)
6040 goto not_posix_bracket;
6041
6042 for (pb = PBS; IS_NOT_NULL(pb->name); pb++) {
6043 if (onigenc_with_ascii_strncmp(enc, p, end, pb->name, pb->len) == 0) {
6044 p = (UChar* )onigenc_step(enc, p, end, pb->len);
6045 if (onigenc_with_ascii_strncmp(enc, p, end, (UChar* )":]", 2) != 0)
6046 return ONIGERR_INVALID_POSIX_BRACKET_TYPE;
6047
6048 r = add_ctype_to_cc(cc, pb->ctype, not, env);
6049 if (r != 0) return r;
6050
6051 PINC_S; PINC_S;
6052 *src = p;
6053 return 0;
6054 }
6055 }
6056
6057 not_posix_bracket:
6058 c = 0;
6059 i = 0;
6060 while (!PEND && ((c = PPEEK) != ':') && c != ']') {
6061 PINC_S;
6062 if (++i > POSIX_BRACKET_CHECK_LIMIT_LENGTH) break;
6063 }
6064 if (c == ':' && ! PEND) {
6065 PINC_S;
6066 if (! PEND) {
6067 PFETCH_S(c);
6068 if (c == ']')
6069 return ONIGERR_INVALID_POSIX_BRACKET_TYPE;
6070 }
6071 }
6072
6073 return 1; /* 1: is not POSIX bracket, but no error. */
6074 }
6075
6076 static int
6077 fetch_char_property_to_ctype(UChar** src, UChar* end, ScanEnv* env)
6078 {
6079 int r;
6080 OnigCodePoint c;
6081 OnigEncoding enc = env->enc;
6082 UChar *prev, *start, *p = *src;
6083
6084 r = 0;
6085 start = prev = p;
6086
6087 while (!PEND) {
6088 prev = p;
6089 PFETCH_S(c);
6090 if (c == '}') {
6091 r = ONIGENC_PROPERTY_NAME_TO_CTYPE(enc, start, prev);
6092 if (r < 0) break;
6093
6094 *src = p;
6095 return r;
6096 }
6097 else if (c == '(' || c == ')' || c == '{' || c == '|') {
6098 r = ONIGERR_INVALID_CHAR_PROPERTY_NAME;
6099 break;
6100 }
6101 }
6102
6103 onig_scan_env_set_error_string(env, r, *src, prev);
6104 return r;
6105 }
6106
6107 static int
6108 parse_char_property(Node** np, PToken* tok, UChar** src, UChar* end, ScanEnv* env)
6109 {
6110 int r, ctype;
6111 CClassNode* cc;
6112
6113 ctype = fetch_char_property_to_ctype(src, end, env);
6114 if (ctype < 0) return ctype;
6115
6116 *np = node_new_cclass();
6117 CHECK_NULL_RETURN_MEMERR(*np);
6118 cc = CCLASS_(*np);
6119 r = add_ctype_to_cc(cc, ctype, 0, env);
6120 if (r != 0) return r;
6121 if (tok->u.prop.not != 0) NCCLASS_SET_NOT(cc);
6122
6123 return 0;
6124 }
6125
6126
6127 enum CCSTATE {
6128 CCS_VALUE,
6129 CCS_RANGE,
6130 CCS_COMPLETE,
6131 CCS_START
6132 };
6133
6134 enum CCVALTYPE {
6135 CCV_SB,
6136 CCV_CODE_POINT,
6137 CCV_CLASS
6138 };
6139
6140 static int
6141 next_state_class(CClassNode* cc, OnigCodePoint* vs, enum CCVALTYPE* type,
6142 enum CCSTATE* state, ScanEnv* env)
6143 {
6144 int r;
6145
6146 if (*state == CCS_RANGE)
6147 return ONIGERR_CHAR_CLASS_VALUE_AT_END_OF_RANGE;
6148
6149 if (*state == CCS_VALUE && *type != CCV_CLASS) {
6150 if (*type == CCV_SB)
6151 BITSET_SET_BIT(cc->bs, (int )(*vs));
6152 else if (*type == CCV_CODE_POINT) {
6153 r = add_code_range(&(cc->mbuf), env, *vs, *vs);
6154 if (r < 0) return r;
6155 }
6156 }
6157
6158 *state = CCS_VALUE;
6159 *type = CCV_CLASS;
6160 return 0;
6161 }
6162
6163 static int
6164 next_state_val(CClassNode* cc, OnigCodePoint *from, OnigCodePoint to,
6165 int* from_israw, int to_israw,
6166 enum CCVALTYPE intype, enum CCVALTYPE* type,
6167 enum CCSTATE* state, ScanEnv* env)
6168 {
6169 int r;
6170
6171 switch (*state) {
6172 case CCS_VALUE:
6173 if (*type == CCV_SB) {
6174 if (*from > 0xff)
6175 return ONIGERR_INVALID_CODE_POINT_VALUE;
6176
6177 BITSET_SET_BIT(cc->bs, (int )(*from));
6178 }
6179 else if (*type == CCV_CODE_POINT) {
6180 r = add_code_range(&(cc->mbuf), env, *from, *from);
6181 if (r < 0) return r;
6182 }
6183 break;
6184
6185 case CCS_RANGE:
6186 if (intype == *type) {
6187 if (intype == CCV_SB) {
6188 if (*from > 0xff || to > 0xff)
6189 return ONIGERR_INVALID_CODE_POINT_VALUE;
6190
6191 if (*from > to) {
6192 if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_ALLOW_EMPTY_RANGE_IN_CC))
6193 goto ccs_range_end;
6194 else
6195 return ONIGERR_EMPTY_RANGE_IN_CHAR_CLASS;
6196 }
6197 bitset_set_range(cc->bs, (int )*from, (int )to);
6198 }
6199 else {
6200 r = add_code_range(&(cc->mbuf), env, *from, to);
6201 if (r < 0) return r;
6202 }
6203 }
6204 else {
6205 if (*from > to) {
6206 if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_ALLOW_EMPTY_RANGE_IN_CC))
6207 goto ccs_range_end;
6208 else
6209 return ONIGERR_EMPTY_RANGE_IN_CHAR_CLASS;
6210 }
6211 bitset_set_range(cc->bs, (int )*from, (int )(to < 0xff ? to : 0xff));
6212 r = add_code_range(&(cc->mbuf), env, (OnigCodePoint )*from, to);
6213 if (r < 0) return r;
6214 }
6215 ccs_range_end:
6216 *state = CCS_COMPLETE;
6217 break;
6218
6219 case CCS_COMPLETE:
6220 case CCS_START:
6221 *state = CCS_VALUE;
6222 break;
6223
6224 default:
6225 break;
6226 }
6227
6228 *from_israw = to_israw;
6229 *from = to;
6230 *type = intype;
6231 return 0;
6232 }
6233
6234 static int
6235 code_exist_check(OnigCodePoint c, UChar* from, UChar* end, int ignore_escaped,
6236 ScanEnv* env)
6237 {
6238 int in_esc;
6239 OnigCodePoint code;
6240 OnigEncoding enc = env->enc;
6241 UChar* p = from;
6242
6243 in_esc = 0;
6244 while (! PEND) {
6245 if (ignore_escaped && in_esc) {
6246 in_esc = 0;
6247 }
6248 else {
6249 PFETCH_S(code);
6250 if (code == c) return 1;
6251 if (code == MC_ESC(env->syntax)) in_esc = 1;
6252 }
6253 }
6254 return 0;
6255 }
6256
6257 static int
6258 parse_char_class(Node** np, PToken* tok, UChar** src, UChar* end, ScanEnv* env)
6259 {
6260 int r, neg, len, fetched, and_start;
6261 OnigCodePoint v, vs;
6262 UChar *p;
6263 Node* node;
6264 CClassNode *cc, *prev_cc;
6265 CClassNode work_cc;
6266
6267 enum CCSTATE state;
6268 enum CCVALTYPE val_type, in_type;
6269 int val_israw, in_israw;
6270
6271 *np = NULL_NODE;
6272 env->parse_depth++;
6273 if (env->parse_depth > ParseDepthLimit)
6274 return ONIGERR_PARSE_DEPTH_LIMIT_OVER;
6275
6276 prev_cc = (CClassNode* )NULL;
6277 r = fetch_token_in_cc(tok, src, end, env);
6278 if (r == TK_CHAR && tok->u.c == '^' && tok->escaped == 0) {
6279 neg = 1;
6280 r = fetch_token_in_cc(tok, src, end, env);
6281 }
6282 else {
6283 neg = 0;
6284 }
6285
6286 if (r < 0) return r;
6287 if (r == TK_CC_CLOSE) {
6288 if (! code_exist_check((OnigCodePoint )']',
6289 *src, env->pattern_end, 1, env))
6290 return ONIGERR_EMPTY_CHAR_CLASS;
6291
6292 CC_ESC_WARN(env, (UChar* )"]");
6293 r = tok->type = TK_CHAR; /* allow []...] */
6294 }
6295
6296 *np = node = node_new_cclass();
6297 CHECK_NULL_RETURN_MEMERR(node);
6298 cc = CCLASS_(node);
6299
6300 and_start = 0;
6301 state = CCS_START;
6302 p = *src;
6303 while (r != TK_CC_CLOSE) {
6304 fetched = 0;
6305 switch (r) {
6306 case TK_CHAR:
6307 any_char_in:
6308 len = ONIGENC_CODE_TO_MBCLEN(env->enc, tok->u.c);
6309 if (len > 1) {
6310 in_type = CCV_CODE_POINT;
6311 }
6312 else if (len < 0) {
6313 r = len;
6314 goto err;
6315 }
6316 else {
6317 /* sb_char: */
6318 in_type = CCV_SB;
6319 }
6320 v = (OnigCodePoint )tok->u.c;
6321 in_israw = 0;
6322 goto val_entry2;
6323 break;
6324
6325 case TK_RAW_BYTE:
6326 /* tok->base != 0 : octal or hexadec. */
6327 if (! ONIGENC_IS_SINGLEBYTE(env->enc) && tok->base != 0) {
6328 int i, j;
6329 UChar buf[ONIGENC_CODE_TO_MBC_MAXLEN];
6330 UChar* bufe = buf + ONIGENC_CODE_TO_MBC_MAXLEN;
6331 UChar* psave = p;
6332 int base = tok->base;
6333
6334 buf[0] = tok->u.c;
6335 for (i = 1; i < ONIGENC_MBC_MAXLEN(env->enc); i++) {
6336 r = fetch_token_in_cc(tok, &p, end, env);
6337 if (r < 0) goto err;
6338 if (r != TK_RAW_BYTE || tok->base != base) {
6339 fetched = 1;
6340 break;
6341 }
6342 buf[i] = tok->u.c;
6343 }
6344
6345 if (i < ONIGENC_MBC_MINLEN(env->enc)) {
6346 r = ONIGERR_TOO_SHORT_MULTI_BYTE_STRING;
6347 goto err;
6348 }
6349
6350 /* clear buf tail */
6351 for (j = i; j < ONIGENC_CODE_TO_MBC_MAXLEN; j++) buf[j] = '\0';
6352
6353 len = enclen(env->enc, buf);
6354 if (i < len) {
6355 r = ONIGERR_TOO_SHORT_MULTI_BYTE_STRING;
6356 goto err;
6357 }
6358 else if (i > len) { /* fetch back */
6359 p = psave;
6360 for (i = 1; i < len; i++) {
6361 r = fetch_token_in_cc(tok, &p, end, env);
6362 }
6363 fetched = 0;
6364 }
6365
6366 if (i == 1) {
6367 v = (OnigCodePoint )buf[0];
6368 goto raw_single;
6369 }
6370 else {
6371 v = ONIGENC_MBC_TO_CODE(env->enc, buf, bufe);
6372 in_type = CCV_CODE_POINT;
6373 }
6374 }
6375 else {
6376 v = (OnigCodePoint )tok->u.c;
6377 raw_single:
6378 in_type = CCV_SB;
6379 }
6380 in_israw = 1;
6381 goto val_entry2;
6382 break;
6383
6384 case TK_CODE_POINT:
6385 v = tok->u.code;
6386 in_israw = 1;
6387 val_entry:
6388 len = ONIGENC_CODE_TO_MBCLEN(env->enc, v);
6389 if (len < 0) {
6390 if (state != CCS_RANGE ||
6391 ! IS_SYNTAX_BV(env->syntax,
6392 ONIG_SYN_ALLOW_INVALID_CODE_END_OF_RANGE_IN_CC) ||
6393 v < 0x100 || ONIGENC_MBC_MAXLEN(env->enc) == 1) {
6394 r = len;
6395 goto err;
6396 }
6397 }
6398 in_type = (len == 1 ? CCV_SB : CCV_CODE_POINT);
6399 val_entry2:
6400 r = next_state_val(cc, &vs, v, &val_israw, in_israw, in_type, &val_type,
6401 &state, env);
6402 if (r != 0) goto err;
6403 break;
6404
6405 case TK_POSIX_BRACKET_OPEN:
6406 r = parse_posix_bracket(cc, &p, end, env);
6407 if (r < 0) goto err;
6408 if (r == 1) { /* is not POSIX bracket */
6409 CC_ESC_WARN(env, (UChar* )"[");
6410 p = tok->backp;
6411 v = (OnigCodePoint )tok->u.c;
6412 in_israw = 0;
6413 goto val_entry;
6414 }
6415 goto next_class;
6416 break;
6417
6418 case TK_CHAR_TYPE:
6419 r = add_ctype_to_cc(cc, tok->u.prop.ctype, tok->u.prop.not, env);
6420 if (r != 0) goto err;
6421
6422 next_class:
6423 r = next_state_class(cc, &vs, &val_type, &state, env);
6424 if (r != 0) goto err;
6425 break;
6426
6427 case TK_CHAR_PROPERTY:
6428 {
6429 int ctype = fetch_char_property_to_ctype(&p, end, env);
6430 if (ctype < 0) {
6431 r = ctype;
6432 goto err;
6433 }
6434 r = add_ctype_to_cc(cc, ctype, tok->u.prop.not, env);
6435 if (r != 0) goto err;
6436 goto next_class;
6437 }
6438 break;
6439
6440 case TK_CC_RANGE:
6441 if (state == CCS_VALUE) {
6442 r = fetch_token_in_cc(tok, &p, end, env);
6443 if (r < 0) goto err;
6444 fetched = 1;
6445 if (r == TK_CC_CLOSE) { /* allow [x-] */
6446 range_end_val:
6447 v = (OnigCodePoint )'-';
6448 in_israw = 0;
6449 goto val_entry;
6450 }
6451 else if (r == TK_CC_AND) {
6452 CC_ESC_WARN(env, (UChar* )"-");
6453 goto range_end_val;
6454 }
6455
6456 if (val_type == CCV_CLASS) {
6457 r = ONIGERR_UNMATCHED_RANGE_SPECIFIER_IN_CHAR_CLASS;
6458 goto err;
6459 }
6460
6461 state = CCS_RANGE;
6462 }
6463 else if (state == CCS_START) {
6464 /* [-xa] is allowed */
6465 v = (OnigCodePoint )tok->u.c;
6466 in_israw = 0;
6467
6468 r = fetch_token_in_cc(tok, &p, end, env);
6469 if (r < 0) goto err;
6470 fetched = 1;
6471 /* [--x] or [a&&-x] is warned. */
6472 if (r == TK_CC_RANGE || and_start != 0)
6473 CC_ESC_WARN(env, (UChar* )"-");
6474
6475 goto val_entry;
6476 }
6477 else if (state == CCS_RANGE) {
6478 CC_ESC_WARN(env, (UChar* )"-");
6479 goto any_char_in; /* [!--x] is allowed */
6480 }
6481 else { /* CCS_COMPLETE */
6482 r = fetch_token_in_cc(tok, &p, end, env);
6483 if (r < 0) goto err;
6484 fetched = 1;
6485 if (r == TK_CC_CLOSE) goto range_end_val; /* allow [a-b-] */
6486 else if (r == TK_CC_AND) {
6487 CC_ESC_WARN(env, (UChar* )"-");
6488 goto range_end_val;
6489 }
6490
6491 if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_ALLOW_DOUBLE_RANGE_OP_IN_CC)) {
6492 CC_ESC_WARN(env, (UChar* )"-");
6493 goto range_end_val; /* [0-9-a] is allowed as [0-9\-a] */
6494 }
6495 r = ONIGERR_UNMATCHED_RANGE_SPECIFIER_IN_CHAR_CLASS;
6496 goto err;
6497 }
6498 break;
6499
6500 case TK_CC_CC_OPEN: /* [ */
6501 {
6502 Node *anode;
6503 CClassNode* acc;
6504
6505 r = parse_char_class(&anode, tok, &p, end, env);
6506 if (r != 0) {
6507 onig_node_free(anode);
6508 goto cc_open_err;
6509 }
6510 acc = CCLASS_(anode);
6511 r = or_cclass(cc, acc, env->enc);
6512 onig_node_free(anode);
6513
6514 cc_open_err:
6515 if (r != 0) goto err;
6516 }
6517 break;
6518
6519 case TK_CC_AND: /* && */
6520 {
6521 if (state == CCS_VALUE) {
6522 r = next_state_val(cc, &vs, 0, &val_israw, 0, val_type,
6523 &val_type, &state, env);
6524 if (r != 0) goto err;
6525 }
6526 /* initialize local variables */
6527 and_start = 1;
6528 state = CCS_START;
6529
6530 if (IS_NOT_NULL(prev_cc)) {
6531 r = and_cclass(prev_cc, cc, env->enc);
6532 if (r != 0) goto err;
6533 bbuf_free(cc->mbuf);
6534 }
6535 else {
6536 prev_cc = cc;
6537 cc = &work_cc;
6538 }
6539 initialize_cclass(cc);
6540 }
6541 break;
6542
6543 case TK_EOT:
6544 r = ONIGERR_PREMATURE_END_OF_CHAR_CLASS;
6545 goto err;
6546 break;
6547 default:
6548 r = ONIGERR_PARSER_BUG;
6549 goto err;
6550 break;
6551 }
6552
6553 if (fetched)
6554 r = tok->type;
6555 else {
6556 r = fetch_token_in_cc(tok, &p, end, env);
6557 if (r < 0) goto err;
6558 }
6559 }
6560
6561 if (state == CCS_VALUE) {
6562 r = next_state_val(cc, &vs, 0, &val_israw, 0, val_type,
6563 &val_type, &state, env);
6564 if (r != 0) goto err;
6565 }
6566
6567 if (IS_NOT_NULL(prev_cc)) {
6568 r = and_cclass(prev_cc, cc, env->enc);
6569 if (r != 0) goto err;
6570 bbuf_free(cc->mbuf);
6571 cc = prev_cc;
6572 }
6573
6574 if (neg != 0)
6575 NCCLASS_SET_NOT(cc);
6576 else
6577 NCCLASS_CLEAR_NOT(cc);
6578 if (IS_NCCLASS_NOT(cc) &&
6579 IS_SYNTAX_BV(env->syntax, ONIG_SYN_NOT_NEWLINE_IN_NEGATIVE_CC)) {
6580 int is_empty = (IS_NULL(cc->mbuf) ? 1 : 0);
6581 if (is_empty != 0)
6582 BITSET_IS_EMPTY(cc->bs, is_empty);
6583
6584 if (is_empty == 0) {
6585 #define NEWLINE_CODE 0x0a
6586
6587 if (ONIGENC_IS_CODE_NEWLINE(env->enc, NEWLINE_CODE)) {
6588 if (ONIGENC_CODE_TO_MBCLEN(env->enc, NEWLINE_CODE) == 1)
6589 BITSET_SET_BIT(cc->bs, NEWLINE_CODE);
6590 else
6591 add_code_range(&(cc->mbuf), env, NEWLINE_CODE, NEWLINE_CODE);
6592 }
6593 }
6594 }
6595 *src = p;
6596 env->parse_depth--;
6597 return 0;
6598
6599 err:
6600 if (cc != CCLASS_(*np))
6601 bbuf_free(cc->mbuf);
6602 return r;
6603 }
6604
6605 static int parse_subexp(Node** top, PToken* tok, int term,
6606 UChar** src, UChar* end, ScanEnv* env, int group_head);
6607
6608 #ifdef USE_CALLOUT
6609
6610 /* (?{...}[tag][+-]) (?{{...}}[tag][+-]) */
6611 static int
6612 parse_callout_of_contents(Node** np, int cterm, UChar** src, UChar* end, ScanEnv* env)
6613 {
6614 int r;
6615 int i;
6616 int in;
6617 int num;
6618 OnigCodePoint c;
6619 UChar* code_start;
6620 UChar* code_end;
6621 UChar* contents;
6622 UChar* tag_start;
6623 UChar* tag_end;
6624 int brace_nest;
6625 CalloutListEntry* e;
6626 RegexExt* ext;
6627 OnigEncoding enc = env->enc;
6628 UChar* p = *src;
6629
6630 if (PEND) return ONIGERR_INVALID_CALLOUT_PATTERN;
6631
6632 brace_nest = 0;
6633 while (PPEEK_IS('{')) {
6634 brace_nest++;
6635 PINC_S;
6636 if (PEND) return ONIGERR_INVALID_CALLOUT_PATTERN;
6637 }
6638
6639 in = ONIG_CALLOUT_IN_PROGRESS;
6640 code_start = p;
6641 while (1) {
6642 if (PEND) return ONIGERR_INVALID_CALLOUT_PATTERN;
6643
6644 code_end = p;
6645 PFETCH_S(c);
6646 if (c == '}') {
6647 i = brace_nest;
6648 while (i > 0) {
6649 if (PEND) return ONIGERR_INVALID_CALLOUT_PATTERN;
6650 PFETCH_S(c);
6651 if (c == '}') i--;
6652 else break;
6653 }
6654 if (i == 0) break;
6655 }
6656 }
6657
6658 if (PEND) return ONIGERR_END_PATTERN_IN_GROUP;
6659
6660 PFETCH_S(c);
6661 if (c == '[') {
6662 if (PEND) return ONIGERR_END_PATTERN_IN_GROUP;
6663 tag_end = tag_start = p;
6664 while (! PEND) {
6665 if (PEND) return ONIGERR_END_PATTERN_IN_GROUP;
6666 tag_end = p;
6667 PFETCH_S(c);
6668 if (c == ']') break;
6669 }
6670 if (! is_allowed_callout_tag_name(enc, tag_start, tag_end))
6671 return ONIGERR_INVALID_CALLOUT_TAG_NAME;
6672
6673 if (PEND) return ONIGERR_END_PATTERN_IN_GROUP;
6674 PFETCH_S(c);
6675 }
6676 else {
6677 tag_start = tag_end = 0;
6678 }
6679
6680 if (c == 'X') {
6681 in |= ONIG_CALLOUT_IN_RETRACTION;
6682 if (PEND) return ONIGERR_END_PATTERN_IN_GROUP;
6683 PFETCH_S(c);
6684 }
6685 else if (c == '<') {
6686 in = ONIG_CALLOUT_IN_RETRACTION;
6687 if (PEND) return ONIGERR_END_PATTERN_IN_GROUP;
6688 PFETCH_S(c);
6689 }
6690 else if (c == '>') { /* no needs (default) */
6691 if (PEND) return ONIGERR_END_PATTERN_IN_GROUP;
6692 PFETCH_S(c);
6693 }
6694
6695 if (c != cterm)
6696 return ONIGERR_INVALID_CALLOUT_PATTERN;
6697
6698 r = reg_callout_list_entry(env, &num);
6699 if (r != 0) return r;
6700
6701 ext = onig_get_regex_ext(env->reg);
6702 CHECK_NULL_RETURN_MEMERR(ext);
6703 if (IS_NULL(ext->pattern)) {
6704 r = onig_ext_set_pattern(env->reg, env->pattern, env->pattern_end);
6705 if (r != ONIG_NORMAL) return r;
6706 }
6707
6708 if (tag_start != tag_end) {
6709 r = callout_tag_entry(env, env->reg, tag_start, tag_end, num);
6710 if (r != ONIG_NORMAL) return r;
6711 }
6712
6713 contents = onigenc_strdup(enc, code_start, code_end);
6714 CHECK_NULL_RETURN_MEMERR(contents);
6715
6716 r = node_new_callout(np, ONIG_CALLOUT_OF_CONTENTS, num, ONIG_NON_NAME_ID, env);
6717 if (r != 0) {
6718 xfree(contents);
6719 return r;
6720 }
6721
6722 e = onig_reg_callout_list_at(env->reg, num);
6723 if (IS_NULL(e)) {
6724 xfree(contents);
6725 return ONIGERR_MEMORY;
6726 }
6727
6728 e->of = ONIG_CALLOUT_OF_CONTENTS;
6729 e->in = in;
6730 e->name_id = ONIG_NON_NAME_ID;
6731 e->u.content.start = contents;
6732 e->u.content.end = contents + (code_end - code_start);
6733
6734 *src = p;
6735 return 0;
6736 }
6737
6738 static long
6739 parse_long(OnigEncoding enc, UChar* s, UChar* end, int sign_on, long max, long* rl)
6740 {
6741 long v;
6742 long d;
6743 int flag;
6744 UChar* p;
6745 OnigCodePoint c;
6746
6747 if (s >= end) return ONIGERR_INVALID_CALLOUT_ARG;
6748
6749 flag = 1;
6750 v = 0;
6751 p = s;
6752 while (p < end) {
6753 c = ONIGENC_MBC_TO_CODE(enc, p, end);
6754 p += ONIGENC_MBC_ENC_LEN(enc, p);
6755 if (c >= '0' && c <= '9') {
6756 d = (long )(c - '0');
6757 if (v > (max - d) / 10)
6758 return ONIGERR_INVALID_CALLOUT_ARG;
6759
6760 v = v * 10 + d;
6761 }
6762 else if (sign_on != 0 && (c == '-' || c == '+')) {
6763 if (c == '-') flag = -1;
6764 }
6765 else
6766 return ONIGERR_INVALID_CALLOUT_ARG;
6767
6768 sign_on = 0;
6769 }
6770
6771 *rl = flag * v;
6772 return ONIG_NORMAL;
6773 }
6774
6775 static int
6776 parse_callout_args(int skip_mode, int cterm, UChar** src, UChar* end,
6777 unsigned int types[], OnigValue vals[], ScanEnv* env)
6778 {
6779 #define MAX_CALLOUT_ARG_BYTE_LENGTH 128
6780
6781 int r;
6782 int n;
6783 int esc;
6784 int cn;
6785 UChar* s;
6786 UChar* e;
6787 UChar* eesc;
6788 OnigCodePoint c;
6789 UChar* bufend;
6790 UChar buf[MAX_CALLOUT_ARG_BYTE_LENGTH];
6791 OnigEncoding enc = env->enc;
6792 UChar* p = *src;
6793
6794 if (PEND) return ONIGERR_INVALID_CALLOUT_PATTERN;
6795
6796 n = 0;
6797 while (n < ONIG_CALLOUT_MAX_ARGS_NUM) {
6798 c = 0;
6799 cn = 0;
6800 esc = 0;
6801 eesc = 0;
6802 bufend = buf;
6803 s = e = p;
6804 while (1) {
6805 if (PEND) return ONIGERR_INVALID_CALLOUT_PATTERN;
6806
6807 e = p;
6808 PFETCH_S(c);
6809 if (esc != 0) {
6810 esc = 0;
6811 if (c == '\\' || c == cterm || c == ',') {
6812 /* */
6813 }
6814 else {
6815 e = eesc;
6816 cn++;
6817 }
6818 goto add_char;
6819 }
6820 else {
6821 if (c == '\\') {
6822 esc = 1;
6823 eesc = e;
6824 }
6825 else if (c == cterm || c == ',')
6826 break;
6827 else {
6828 size_t clen;
6829
6830 add_char:
6831 if (skip_mode == 0) {
6832 clen = p - e;
6833 if (bufend + clen > buf + MAX_CALLOUT_ARG_BYTE_LENGTH)
6834 return ONIGERR_INVALID_CALLOUT_ARG; /* too long argument */
6835
6836 xmemcpy(bufend, e, clen);
6837 bufend += clen;
6838 }
6839 cn++;
6840 }
6841 }
6842 }
6843
6844 if (cn != 0) {
6845 if (skip_mode == 0) {
6846 if ((types[n] & ONIG_TYPE_LONG) != 0) {
6847 int fixed = 0;
6848 if (cn > 0) {
6849 long rl;
6850 r = parse_long(enc, buf, bufend, 1, LONG_MAX, &rl);
6851 if (r == ONIG_NORMAL) {
6852 vals[n].l = rl;
6853 fixed = 1;
6854 types[n] = ONIG_TYPE_LONG;
6855 }
6856 }
6857
6858 if (fixed == 0) {
6859 types[n] = (types[n] & ~ONIG_TYPE_LONG);
6860 if (types[n] == ONIG_TYPE_VOID)
6861 return ONIGERR_INVALID_CALLOUT_ARG;
6862 }
6863 }
6864
6865 switch (types[n]) {
6866 case ONIG_TYPE_LONG:
6867 break;
6868
6869 case ONIG_TYPE_CHAR:
6870 if (cn != 1) return ONIGERR_INVALID_CALLOUT_ARG;
6871 vals[n].c = ONIGENC_MBC_TO_CODE(enc, buf, bufend);
6872 break;
6873
6874 case ONIG_TYPE_STRING:
6875 {
6876 UChar* rs = onigenc_strdup(enc, buf, bufend);
6877 CHECK_NULL_RETURN_MEMERR(rs);
6878 vals[n].s.start = rs;
6879 vals[n].s.end = rs + (e - s);
6880 }
6881 break;
6882
6883 case ONIG_TYPE_TAG:
6884 if (eesc != 0 || ! is_allowed_callout_tag_name(enc, s, e))
6885 return ONIGERR_INVALID_CALLOUT_TAG_NAME;
6886
6887 vals[n].s.start = s;
6888 vals[n].s.end = e;
6889 break;
6890
6891 case ONIG_TYPE_VOID:
6892 case ONIG_TYPE_POINTER:
6893 return ONIGERR_PARSER_BUG;
6894 break;
6895 }
6896 }
6897
6898 n++;
6899 }
6900
6901 if (c == cterm) break;
6902 }
6903
6904 if (c != cterm) return ONIGERR_INVALID_CALLOUT_PATTERN;
6905
6906 *src = p;
6907 return n;
6908 }
6909
6910 /* (*name[TAG]) (*name[TAG]{a,b,..}) */
6911 static int
6912 parse_callout_of_name(Node** np, int cterm, UChar** src, UChar* end, ScanEnv* env)
6913 {
6914 int r;
6915 int i;
6916 int in;
6917 int num;
6918 int name_id;
6919 int arg_num;
6920 int max_arg_num;
6921 int opt_arg_num;
6922 int is_not_single;
6923 OnigCodePoint c;
6924 UChar* name_start;
6925 UChar* name_end;
6926 UChar* tag_start;
6927 UChar* tag_end;
6928 Node* node;
6929 CalloutListEntry* e;
6930 RegexExt* ext;
6931 unsigned int types[ONIG_CALLOUT_MAX_ARGS_NUM];
6932 OnigValue vals[ONIG_CALLOUT_MAX_ARGS_NUM];
6933 OnigEncoding enc = env->enc;
6934 UChar* p = *src;
6935
6936 /* PFETCH_READY; */
6937 if (PEND) return ONIGERR_INVALID_CALLOUT_PATTERN;
6938
6939 node = 0;
6940 name_start = p;
6941 while (1) {
6942 if (PEND) return ONIGERR_END_PATTERN_IN_GROUP;
6943 name_end = p;
6944 PFETCH_S(c);
6945 if (c == cterm || c == '[' || c == '{') break;
6946 }
6947
6948 if (! is_allowed_callout_name(enc, name_start, name_end))
6949 return ONIGERR_INVALID_CALLOUT_NAME;
6950
6951 if (c == '[') {
6952 if (PEND) return ONIGERR_END_PATTERN_IN_GROUP;
6953 tag_end = tag_start = p;
6954 while (! PEND) {
6955 if (PEND) return ONIGERR_END_PATTERN_IN_GROUP;
6956 tag_end = p;
6957 PFETCH_S(c);
6958 if (c == ']') break;
6959 }
6960 if (! is_allowed_callout_tag_name(enc, tag_start, tag_end))
6961 return ONIGERR_INVALID_CALLOUT_TAG_NAME;
6962
6963 if (PEND) return ONIGERR_END_PATTERN_IN_GROUP;
6964 PFETCH_S(c);
6965 }
6966 else {
6967 tag_start = tag_end = 0;
6968 }
6969
6970 if (c == '{') {
6971 UChar* save;
6972
6973 if (PEND) return ONIGERR_END_PATTERN_IN_GROUP;
6974
6975 /* read for single check only */
6976 save = p;
6977 arg_num = parse_callout_args(1, '}', &p, end, 0, 0, env);
6978 if (arg_num < 0) return arg_num;
6979
6980 is_not_single = PPEEK_IS(cterm) ? 0 : 1;
6981 p = save;
6982 r = get_callout_name_id_by_name(enc, is_not_single, name_start, name_end,
6983 &name_id);
6984 if (r != ONIG_NORMAL) return r;
6985
6986 max_arg_num = get_callout_arg_num_by_name_id(name_id);
6987 for (i = 0; i < max_arg_num; i++) {
6988 types[i] = get_callout_arg_type_by_name_id(name_id, i);
6989 }
6990
6991 arg_num = parse_callout_args(0, '}', &p, end, types, vals, env);
6992 if (arg_num < 0) return arg_num;
6993
6994 if (PEND) return ONIGERR_END_PATTERN_IN_GROUP;
6995 PFETCH_S(c);
6996 }
6997 else {
6998 arg_num = 0;
6999
7000 is_not_single = 0;
7001 r = get_callout_name_id_by_name(enc, is_not_single, name_start, name_end,
7002 &name_id);
7003 if (r != ONIG_NORMAL) return r;
7004
7005 max_arg_num = get_callout_arg_num_by_name_id(name_id);
7006 for (i = 0; i < max_arg_num; i++) {
7007 types[i] = get_callout_arg_type_by_name_id(name_id, i);
7008 }
7009 }
7010
7011 in = onig_get_callout_in_by_name_id(name_id);
7012 opt_arg_num = get_callout_opt_arg_num_by_name_id(name_id);
7013 if (arg_num > max_arg_num || arg_num < (max_arg_num - opt_arg_num))
7014 return ONIGERR_INVALID_CALLOUT_ARG;
7015
7016 if (c != cterm)
7017 return ONIGERR_INVALID_CALLOUT_PATTERN;
7018
7019 r = reg_callout_list_entry(env, &num);
7020 if (r != 0) return r;
7021
7022 ext = onig_get_regex_ext(env->reg);
7023 CHECK_NULL_RETURN_MEMERR(ext);
7024 if (IS_NULL(ext->pattern)) {
7025 r = onig_ext_set_pattern(env->reg, env->pattern, env->pattern_end);
7026 if (r != ONIG_NORMAL) return r;
7027 }
7028
7029 if (tag_start != tag_end) {
7030 r = callout_tag_entry(env, env->reg, tag_start, tag_end, num);
7031 if (r != ONIG_NORMAL) return r;
7032 }
7033
7034 r = node_new_callout(&node, ONIG_CALLOUT_OF_NAME, num, name_id, env);
7035 if (r != ONIG_NORMAL) return r;
7036
7037 e = onig_reg_callout_list_at(env->reg, num);
7038 CHECK_NULL_RETURN_MEMERR(e);
7039
7040 e->of = ONIG_CALLOUT_OF_NAME;
7041 e->in = in;
7042 e->name_id = name_id;
7043 e->type = onig_get_callout_type_by_name_id(name_id);
7044 e->start_func = onig_get_callout_start_func_by_name_id(name_id);
7045 e->end_func = onig_get_callout_end_func_by_name_id(name_id);
7046 e->u.arg.num = max_arg_num;
7047 e->u.arg.passed_num = arg_num;
7048 for (i = 0; i < max_arg_num; i++) {
7049 e->u.arg.types[i] = types[i];
7050 if (i < arg_num)
7051 e->u.arg.vals[i] = vals[i];
7052 else
7053 e->u.arg.vals[i] = get_callout_opt_default_by_name_id(name_id, i);
7054 }
7055
7056 *np = node;
7057 *src = p;
7058 return 0;
7059 }
7060 #endif
7061
7062 static int
7063 parse_bag(Node** np, PToken* tok, int term, UChar** src, UChar* end,
7064 ScanEnv* env)
7065 {
7066 int r, num;
7067 Node *target;
7068 OnigOptionType option;
7069 OnigCodePoint c;
7070 int list_capture;
7071 OnigEncoding enc = env->enc;
7072
7073 UChar* p = *src;
7074 PFETCH_READY;
7075
7076 *np = NULL;
7077 if (PEND) return ONIGERR_END_PATTERN_WITH_UNMATCHED_PARENTHESIS;
7078
7079 option = env->options;
7080 c = PPEEK;
7081 if (c == '?' && IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_QMARK_GROUP_EFFECT)) {
7082 PINC;
7083 if (PEND) return ONIGERR_END_PATTERN_IN_GROUP;
7084
7085 PFETCH(c);
7086 switch (c) {
7087 case ':': /* (?:...) grouping only */
7088 group:
7089 r = fetch_token(tok, &p, end, env);
7090 if (r < 0) return r;
7091 r = parse_subexp(np, tok, term, &p, end, env, 0);
7092 if (r < 0) return r;
7093 *src = p;
7094 return 1; /* group */
7095 break;
7096
7097 case '=':
7098 *np = onig_node_new_anchor(ANCR_PREC_READ, 0);
7099 break;
7100 case '!': /* preceding read */
7101 *np = onig_node_new_anchor(ANCR_PREC_READ_NOT, 0);
7102 break;
7103 case '>': /* (?>...) stop backtrack */
7104 *np = node_new_bag(BAG_STOP_BACKTRACK);
7105 break;
7106
7107 case '\'':
7108 if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_QMARK_LT_NAMED_GROUP)) {
7109 goto named_group1;
7110 }
7111 else
7112 return ONIGERR_UNDEFINED_GROUP_OPTION;
7113 break;
7114
7115 case '<': /* look behind (?<=...), (?<!...) */
7116 if (PEND) return ONIGERR_END_PATTERN_WITH_UNMATCHED_PARENTHESIS;
7117 PFETCH(c);
7118 if (c == '=')
7119 *np = onig_node_new_anchor(ANCR_LOOK_BEHIND, 0);
7120 else if (c == '!')
7121 *np = onig_node_new_anchor(ANCR_LOOK_BEHIND_NOT, 0);
7122 else {
7123 if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_QMARK_LT_NAMED_GROUP)) {
7124 UChar *name;
7125 UChar *name_end;
7126 enum REF_NUM num_type;
7127
7128 PUNFETCH;
7129 c = '<';
7130
7131 named_group1:
7132 list_capture = 0;
7133
7134 named_group2:
7135 name = p;
7136 r = fetch_name((OnigCodePoint )c, &p, end, &name_end, env, &num,
7137 &num_type, 0);
7138 if (r < 0) return r;
7139
7140 num = scan_env_add_mem_entry(env);
7141 if (num < 0) return num;
7142 if (list_capture != 0 && num >= (int )MEM_STATUS_BITS_NUM)
7143 return ONIGERR_GROUP_NUMBER_OVER_FOR_CAPTURE_HISTORY;
7144
7145 r = name_add(env->reg, name, name_end, num, env);
7146 if (r != 0) return r;
7147 *np = node_new_memory(1);
7148 CHECK_NULL_RETURN_MEMERR(*np);
7149 BAG_(*np)->m.regnum = num;
7150 if (list_capture != 0)
7151 MEM_STATUS_ON_SIMPLE(env->capture_history, num);
7152 env->num_named++;
7153 }
7154 else {
7155 return ONIGERR_UNDEFINED_GROUP_OPTION;
7156 }
7157 }
7158 break;
7159
7160 case '~':
7161 if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_QMARK_TILDE_ABSENT_GROUP)) {
7162 Node* absent;
7163 Node* expr;
7164 int head_bar;
7165 int is_range_cutter;
7166
7167 if (PEND) return ONIGERR_END_PATTERN_IN_GROUP;
7168
7169 if (PPEEK_IS('|')) { /* (?~|generator|absent) */
7170 PINC;
7171 if (PEND) return ONIGERR_END_PATTERN_IN_GROUP;
7172
7173 head_bar = 1;
7174 if (PPEEK_IS(')')) { /* (?~|) : range clear */
7175 PINC;
7176 r = make_range_clear(np, env);
7177 if (r != 0) return r;
7178 goto end;
7179 }
7180 }
7181 else
7182 head_bar = 0;
7183
7184 r = fetch_token(tok, &p, end, env);
7185 if (r < 0) return r;
7186 r = parse_subexp(&absent, tok, term, &p, end, env, 1);
7187 if (r < 0) {
7188 onig_node_free(absent);
7189 return r;
7190 }
7191
7192 expr = NULL_NODE;
7193 is_range_cutter = 0;
7194 if (head_bar != 0) {
7195 Node* top = absent;
7196 if (NODE_TYPE(top) != NODE_ALT || IS_NULL(NODE_CDR(top))) {
7197 expr = NULL_NODE;
7198 is_range_cutter = 1;
7199 /* return ONIGERR_INVALID_ABSENT_GROUP_GENERATOR_PATTERN; */
7200 }
7201 else {
7202 absent = NODE_CAR(top);
7203 expr = NODE_CDR(top);
7204 NODE_CAR(top) = NULL_NODE;
7205 NODE_CDR(top) = NULL_NODE;
7206 onig_node_free(top);
7207 if (IS_NULL(NODE_CDR(expr))) {
7208 top = expr;
7209 expr = NODE_CAR(top);
7210 NODE_CAR(top) = NULL_NODE;
7211 onig_node_free(top);
7212 }
7213 }
7214 }
7215
7216 r = make_absent_tree(np, absent, expr, is_range_cutter, env);
7217 if (r != 0) {
7218 return r;
7219 }
7220 goto end;
7221 }
7222 else {
7223 return ONIGERR_UNDEFINED_GROUP_OPTION;
7224 }
7225 break;
7226
7227 #ifdef USE_CALLOUT
7228 case '{':
7229 if (! IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_QMARK_BRACE_CALLOUT_CONTENTS))
7230 return ONIGERR_UNDEFINED_GROUP_OPTION;
7231
7232 r = parse_callout_of_contents(np, ')', &p, end, env);
7233 if (r != 0) return r;
7234
7235 goto end;
7236 break;
7237 #endif
7238
7239 case '(':
7240 /* (?()...) */
7241 if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_QMARK_LPAREN_IF_ELSE)) {
7242 UChar *prev;
7243 Node* condition;
7244 int condition_is_checker;
7245
7246 if (PEND) return ONIGERR_END_PATTERN_IN_GROUP;
7247 PFETCH(c);
7248 if (PEND) return ONIGERR_END_PATTERN_IN_GROUP;
7249
7250 if (IS_CODE_DIGIT_ASCII(enc, c)
7251 || c == '-' || c == '+' || c == '<' || c == '\'') {
7252 UChar* name_end;
7253 int back_num;
7254 int exist_level;
7255 int level;
7256 enum REF_NUM num_type;
7257 int is_enclosed;
7258
7259 is_enclosed = (c == '<' || c == '\'') ? 1 : 0;
7260 if (! is_enclosed)
7261 PUNFETCH;
7262 prev = p;
7263 exist_level = 0;
7264 #ifdef USE_BACKREF_WITH_LEVEL
7265 name_end = NULL_UCHARP; /* no need. escape gcc warning. */
7266 r = fetch_name_with_level(
7267 (OnigCodePoint )(is_enclosed != 0 ? c : '('),
7268 &p, end, &name_end,
7269 env, &back_num, &level, &num_type);
7270 if (r == 1) exist_level = 1;
7271 #else
7272 r = fetch_name((OnigCodePoint )(is_enclosed != 0 ? c : '('),
7273 &p, end, &name_end, env, &back_num, &num_type, 1);
7274 #endif
7275 if (r < 0) {
7276 if (is_enclosed == 0) {
7277 goto any_condition;
7278 }
7279 else
7280 return r;
7281 }
7282
7283 condition_is_checker = 1;
7284 if (num_type != IS_NOT_NUM) {
7285 if (num_type == IS_REL_NUM) {
7286 back_num = backref_rel_to_abs(back_num, env);
7287 }
7288 if (back_num <= 0)
7289 return ONIGERR_INVALID_BACKREF;
7290
7291 if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_STRICT_CHECK_BACKREF)) {
7292 if (back_num > env->num_mem ||
7293 IS_NULL(SCANENV_MEMENV(env)[back_num].node))
7294 return ONIGERR_INVALID_BACKREF;
7295 }
7296
7297 condition = node_new_backref_checker(1, &back_num, 0,
7298 #ifdef USE_BACKREF_WITH_LEVEL
7299 exist_level, level,
7300 #endif
7301 env);
7302 }
7303 else {
7304 int num;
7305 int* backs;
7306
7307 num = name_to_group_numbers(env, prev, name_end, &backs);
7308 if (num <= 0) {
7309 return ONIGERR_UNDEFINED_NAME_REFERENCE;
7310 }
7311 if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_STRICT_CHECK_BACKREF)) {
7312 int i;
7313 for (i = 0; i < num; i++) {
7314 if (backs[i] > env->num_mem ||
7315 IS_NULL(SCANENV_MEMENV(env)[backs[i]].node))
7316 return ONIGERR_INVALID_BACKREF;
7317 }
7318 }
7319
7320 condition = node_new_backref_checker(num, backs, 1,
7321 #ifdef USE_BACKREF_WITH_LEVEL
7322 exist_level, level,
7323 #endif
7324 env);
7325 }
7326
7327 if (is_enclosed != 0) {
7328 if (PEND) goto err_if_else;
7329 PFETCH(c);
7330 if (c != ')') goto err_if_else;
7331 }
7332 }
7333 #ifdef USE_CALLOUT
7334 else if (c == '?') {
7335 if (IS_SYNTAX_OP2(env->syntax,
7336 ONIG_SYN_OP2_QMARK_BRACE_CALLOUT_CONTENTS)) {
7337 if (! PEND && PPEEK_IS('{')) {
7338 /* condition part is callouts of contents: (?(?{...})THEN|ELSE) */
7339 condition_is_checker = 0;
7340 PFETCH(c);
7341 r = parse_callout_of_contents(&condition, ')', &p, end, env);
7342 if (r != 0) return r;
7343 goto end_condition;
7344 }
7345 }
7346 goto any_condition;
7347 }
7348 else if (c == '*' &&
7349 IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_ASTERISK_CALLOUT_NAME)) {
7350 condition_is_checker = 0;
7351 r = parse_callout_of_name(&condition, ')', &p, end, env);
7352 if (r != 0) return r;
7353 goto end_condition;
7354 }
7355 #endif
7356 else {
7357 any_condition:
7358 PUNFETCH;
7359 condition_is_checker = 0;
7360 r = fetch_token(tok, &p, end, env);
7361 if (r < 0) return r;
7362 r = parse_subexp(&condition, tok, term, &p, end, env, 0);
7363 if (r < 0) {
7364 onig_node_free(condition);
7365 return r;
7366 }
7367 }
7368
7369 #ifdef USE_CALLOUT
7370 end_condition:
7371 #endif
7372 CHECK_NULL_RETURN_MEMERR(condition);
7373
7374 if (PEND) {
7375 err_if_else:
7376 onig_node_free(condition);
7377 return ONIGERR_END_PATTERN_IN_GROUP;
7378 }
7379
7380 if (PPEEK_IS(')')) { /* case: empty body: make backref checker */
7381 if (condition_is_checker == 0) {
7382 onig_node_free(condition);
7383 return ONIGERR_INVALID_IF_ELSE_SYNTAX;
7384 }
7385 PFETCH(c);
7386 *np = condition;
7387 }
7388 else { /* if-else */
7389 int then_is_empty;
7390 Node *Then, *Else;
7391
7392 Then = 0;
7393 if (PPEEK_IS('|')) {
7394 PFETCH(c);
7395 then_is_empty = 1;
7396 }
7397 else
7398 then_is_empty = 0;
7399
7400 r = fetch_token(tok, &p, end, env);
7401 if (r < 0) {
7402 onig_node_free(condition);
7403 return r;
7404 }
7405 r = parse_subexp(&target, tok, term, &p, end, env, 1);
7406 if (r < 0) {
7407 onig_node_free(condition);
7408 onig_node_free(target);
7409 return r;
7410 }
7411
7412 if (then_is_empty != 0) {
7413 Else = target;
7414 }
7415 else {
7416 if (NODE_TYPE(target) == NODE_ALT) {
7417 Then = NODE_CAR(target);
7418 if (NODE_CDR(NODE_CDR(target)) == NULL_NODE) {
7419 Else = NODE_CAR(NODE_CDR(target));
7420 cons_node_free_alone(NODE_CDR(target));
7421 }
7422 else {
7423 Else = NODE_CDR(target);
7424 }
7425 cons_node_free_alone(target);
7426 }
7427 else {
7428 Then = target;
7429 Else = 0;
7430 }
7431 }
7432
7433 *np = node_new_bag_if_else(condition, Then, Else);
7434 if (IS_NULL(*np)) {
7435 onig_node_free(condition);
7436 onig_node_free(Then);
7437 onig_node_free(Else);
7438 return ONIGERR_MEMORY;
7439 }
7440 }
7441 goto end;
7442 }
7443 else {
7444 return ONIGERR_UNDEFINED_GROUP_OPTION;
7445 }
7446 break;
7447
7448 #ifdef USE_CAPTURE_HISTORY
7449 case '@':
7450 if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_ATMARK_CAPTURE_HISTORY)) {
7451 if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_QMARK_LT_NAMED_GROUP)) {
7452 PFETCH(c);
7453 if (c == '<' || c == '\'') {
7454 list_capture = 1;
7455 goto named_group2; /* (?@<name>...) */
7456 }
7457 PUNFETCH;
7458 }
7459
7460 *np = node_new_memory(0);
7461 CHECK_NULL_RETURN_MEMERR(*np);
7462 num = scan_env_add_mem_entry(env);
7463 if (num < 0) {
7464 return num;
7465 }
7466 else if (num >= (int )MEM_STATUS_BITS_NUM) {
7467 return ONIGERR_GROUP_NUMBER_OVER_FOR_CAPTURE_HISTORY;
7468 }
7469 BAG_(*np)->m.regnum = num;
7470 MEM_STATUS_ON_SIMPLE(env->capture_history, num);
7471 }
7472 else {
7473 return ONIGERR_UNDEFINED_GROUP_OPTION;
7474 }
7475 break;
7476 #endif
7477
7478 #ifdef USE_POSIXLINE_OPTION
7479 case 'p':
7480 #endif
7481 case '-': case 'i': case 'm': case 's': case 'x':
7482 case 'W': case 'D': case 'S': case 'P':
7483 case 'y':
7484 {
7485 int neg = 0;
7486
7487 while (1) {
7488 switch (c) {
7489 case ':':
7490 case ')':
7491 break;
7492
7493 case '-': neg = 1; break;
7494 case 'x': OPTION_NEGATE(option, ONIG_OPTION_EXTEND, neg); break;
7495 case 'i': OPTION_NEGATE(option, ONIG_OPTION_IGNORECASE, neg); break;
7496 case 's':
7497 if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_OPTION_PERL)) {
7498 OPTION_NEGATE(option, ONIG_OPTION_MULTILINE, neg);
7499 }
7500 else
7501 return ONIGERR_UNDEFINED_GROUP_OPTION;
7502 break;
7503
7504 case 'm':
7505 if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_OPTION_PERL)) {
7506 OPTION_NEGATE(option, ONIG_OPTION_SINGLELINE, (neg == 0 ? 1 : 0));
7507 }
7508 else if (IS_SYNTAX_OP2(env->syntax,
7509 ONIG_SYN_OP2_OPTION_ONIGURUMA|ONIG_SYN_OP2_OPTION_RUBY)) {
7510 OPTION_NEGATE(option, ONIG_OPTION_MULTILINE, neg);
7511 }
7512 else
7513 return ONIGERR_UNDEFINED_GROUP_OPTION;
7514 break;
7515 #ifdef USE_POSIXLINE_OPTION
7516 case 'p':
7517 OPTION_NEGATE(option, ONIG_OPTION_MULTILINE|ONIG_OPTION_SINGLELINE, neg);
7518 break;
7519 #endif
7520 case 'W': OPTION_NEGATE(option, ONIG_OPTION_WORD_IS_ASCII, neg); break;
7521 case 'D': OPTION_NEGATE(option, ONIG_OPTION_DIGIT_IS_ASCII, neg); break;
7522 case 'S': OPTION_NEGATE(option, ONIG_OPTION_SPACE_IS_ASCII, neg); break;
7523 case 'P': OPTION_NEGATE(option, ONIG_OPTION_POSIX_IS_ASCII, neg); break;
7524
7525 case 'y': /* y{g}, y{w} */
7526 {
7527 if (! IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_OPTION_ONIGURUMA))
7528 return ONIGERR_UNDEFINED_GROUP_OPTION;
7529
7530 if (neg != 0) return ONIGERR_UNDEFINED_GROUP_OPTION;
7531
7532 if (PEND) return ONIGERR_END_PATTERN_IN_GROUP;
7533 if (! PPEEK_IS('{')) return ONIGERR_UNDEFINED_GROUP_OPTION;
7534 PFETCH(c);
7535 if (PEND) return ONIGERR_END_PATTERN_IN_GROUP;
7536 PFETCH(c);
7537 switch (c) {
7538 case 'g':
7539 if (! ONIGENC_IS_UNICODE_ENCODING(enc))
7540 return ONIGERR_UNDEFINED_GROUP_OPTION;
7541
7542 OPTION_NEGATE(option, ONIG_OPTION_TEXT_SEGMENT_EXTENDED_GRAPHEME_CLUSTER, 0);
7543 OPTION_NEGATE(option, ONIG_OPTION_TEXT_SEGMENT_WORD, 1);
7544 break;
7545 #ifdef USE_UNICODE_WORD_BREAK
7546 case 'w':
7547 if (! ONIGENC_IS_UNICODE_ENCODING(enc))
7548 return ONIGERR_UNDEFINED_GROUP_OPTION;
7549
7550 OPTION_NEGATE(option, ONIG_OPTION_TEXT_SEGMENT_WORD, 0);
7551 OPTION_NEGATE(option, ONIG_OPTION_TEXT_SEGMENT_EXTENDED_GRAPHEME_CLUSTER, 1);
7552 break;
7553 #endif
7554 default:
7555 return ONIGERR_UNDEFINED_GROUP_OPTION;
7556 break;
7557 }
7558 if (PEND) return ONIGERR_END_PATTERN_IN_GROUP;
7559 PFETCH(c);
7560 if (c != '}')
7561 return ONIGERR_UNDEFINED_GROUP_OPTION;
7562 break;
7563 } /* case 'y' */
7564
7565 default:
7566 return ONIGERR_UNDEFINED_GROUP_OPTION;
7567 }
7568
7569 if (c == ')') {
7570 *np = node_new_option(option);
7571 CHECK_NULL_RETURN_MEMERR(*np);
7572 *src = p;
7573 return 2; /* option only */
7574 }
7575 else if (c == ':') {
7576 OnigOptionType prev = env->options;
7577
7578 env->options = option;
7579 r = fetch_token(tok, &p, end, env);
7580 if (r < 0) return r;
7581 r = parse_subexp(&target, tok, term, &p, end, env, 0);
7582 env->options = prev;
7583 if (r < 0) {
7584 onig_node_free(target);
7585 return r;
7586 }
7587 *np = node_new_option(option);
7588 CHECK_NULL_RETURN_MEMERR(*np);
7589 NODE_BODY(*np) = target;
7590 *src = p;
7591 return 0;
7592 }
7593
7594 if (PEND) return ONIGERR_END_PATTERN_IN_GROUP;
7595 PFETCH(c);
7596 } /* while (1) */
7597 }
7598 break;
7599
7600 default:
7601 return ONIGERR_UNDEFINED_GROUP_OPTION;
7602 }
7603 }
7604 #ifdef USE_CALLOUT
7605 else if (c == '*' &&
7606 IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_ASTERISK_CALLOUT_NAME)) {
7607 PINC;
7608 r = parse_callout_of_name(np, ')', &p, end, env);
7609 if (r != 0) return r;
7610
7611 goto end;
7612 }
7613 #endif
7614 else {
7615 if (ONIG_IS_OPTION_ON(env->options, ONIG_OPTION_DONT_CAPTURE_GROUP))
7616 goto group;
7617
7618 *np = node_new_memory(0);
7619 CHECK_NULL_RETURN_MEMERR(*np);
7620 num = scan_env_add_mem_entry(env);
7621 if (num < 0) return num;
7622 BAG_(*np)->m.regnum = num;
7623 }
7624
7625 CHECK_NULL_RETURN_MEMERR(*np);
7626 r = fetch_token(tok, &p, end, env);
7627 if (r < 0) return r;
7628 r = parse_subexp(&target, tok, term, &p, end, env, 0);
7629 if (r < 0) {
7630 onig_node_free(target);
7631 return r;
7632 }
7633
7634 NODE_BODY(*np) = target;
7635
7636 if (NODE_TYPE(*np) == NODE_BAG) {
7637 if (BAG_(*np)->type == BAG_MEMORY) {
7638 /* Don't move this to previous of parse_subexp() */
7639 r = scan_env_set_mem_node(env, BAG_(*np)->m.regnum, *np);
7640 if (r != 0) return r;
7641 }
7642 }
7643
7644 end:
7645 *src = p;
7646 return 0;
7647 }
7648
7649 static const char* PopularQStr[] = {
7650 "?", "*", "+", "??", "*?", "+?"
7651 };
7652
7653 static const char* ReduceQStr[] = {
7654 "", "", "*", "*?", "??", "+ and ??", "+? and ?"
7655 };
7656
7657 static int
7658 set_quantifier(Node* qnode, Node* target, int group, ScanEnv* env)
7659 {
7660 QuantNode* qn;
7661
7662 qn = QUANT_(qnode);
7663 if (qn->lower == 1 && qn->upper == 1)
7664 return 1;
7665
7666 switch (NODE_TYPE(target)) {
7667 case NODE_STRING:
7668 if (group == 0) {
7669 if (str_node_can_be_split(target, env->enc)) {
7670 Node* n = str_node_split_last_char(target, env->enc);
7671 if (IS_NOT_NULL(n)) {
7672 NODE_BODY(qnode) = n;
7673 return 2;
7674 }
7675 }
7676 }
7677 break;
7678
7679 case NODE_QUANT:
7680 { /* check redundant double repeat. */
7681 /* verbose warn (?:.?)? etc... but not warn (.?)? etc... */
7682 QuantNode* qnt = QUANT_(target);
7683 int nestq_num = quantifier_type_num(qn);
7684 int targetq_num = quantifier_type_num(qnt);
7685
7686 #ifdef USE_WARNING_REDUNDANT_NESTED_REPEAT_OPERATOR
7687 if (targetq_num >= 0 && nestq_num >= 0 &&
7688 IS_SYNTAX_BV(env->syntax, ONIG_SYN_WARN_REDUNDANT_NESTED_REPEAT)) {
7689 UChar buf[WARN_BUFSIZE];
7690
7691 switch(ReduceTypeTable[targetq_num][nestq_num]) {
7692 case RQ_ASIS:
7693 break;
7694
7695 case RQ_DEL:
7696 if (onig_verb_warn != onig_null_warn) {
7697 onig_snprintf_with_pattern(buf, WARN_BUFSIZE, env->enc,
7698 env->pattern, env->pattern_end,
7699 (UChar* )"redundant nested repeat operator");
7700 (*onig_verb_warn)((char* )buf);
7701 }
7702 goto warn_exit;
7703 break;
7704
7705 default:
7706 if (onig_verb_warn != onig_null_warn) {
7707 onig_snprintf_with_pattern(buf, WARN_BUFSIZE, env->enc,
7708 env->pattern, env->pattern_end,
7709 (UChar* )"nested repeat operator %s and %s was replaced with '%s'",
7710 PopularQStr[targetq_num], PopularQStr[nestq_num],
7711 ReduceQStr[ReduceTypeTable[targetq_num][nestq_num]]);
7712 (*onig_verb_warn)((char* )buf);
7713 }
7714 goto warn_exit;
7715 break;
7716 }
7717 }
7718
7719 warn_exit:
7720 #endif
7721 if (targetq_num >= 0 && nestq_num < 0) {
7722 if (targetq_num == 1 || targetq_num == 2) { /* * or + */
7723 /* (?:a*){n,m}, (?:a+){n,m} => (?:a*){n,n}, (?:a+){n,n} */
7724 if (! IS_INFINITE_REPEAT(qn->upper) && qn->upper > 1 && qn->greedy) {
7725 qn->upper = (qn->lower == 0 ? 1 : qn->lower);
7726 }
7727 }
7728 }
7729 else {
7730 NODE_BODY(qnode) = target;
7731 onig_reduce_nested_quantifier(qnode, target);
7732 goto q_exit;
7733 }
7734 }
7735 break;
7736
7737 default:
7738 break;
7739 }
7740
7741 NODE_BODY(qnode) = target;
7742 q_exit:
7743 return 0;
7744 }
7745
7746
7747 #ifndef CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS
7748 static int
7749 clear_not_flag_cclass(CClassNode* cc, OnigEncoding enc)
7750 {
7751 BBuf *tbuf;
7752 int r;
7753
7754 if (IS_NCCLASS_NOT(cc)) {
7755 bitset_invert(cc->bs);
7756
7757 if (! ONIGENC_IS_SINGLEBYTE(enc)) {
7758 r = not_code_range_buf(enc, cc->mbuf, &tbuf);
7759 if (r != 0) return r;
7760
7761 bbuf_free(cc->mbuf);
7762 cc->mbuf = tbuf;
7763 }
7764
7765 NCCLASS_CLEAR_NOT(cc);
7766 }
7767
7768 return 0;
7769 }
7770 #endif /* CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS */
7771
7772 typedef struct {
7773 ScanEnv* env;
7774 CClassNode* cc;
7775 Node* alt_root;
7776 Node** ptail;
7777 } IApplyCaseFoldArg;
7778
7779 static int
7780 i_apply_case_fold(OnigCodePoint from, OnigCodePoint to[], int to_len, void* arg)
7781 {
7782 IApplyCaseFoldArg* iarg;
7783 ScanEnv* env;
7784 CClassNode* cc;
7785 BitSetRef bs;
7786
7787 iarg = (IApplyCaseFoldArg* )arg;
7788 env = iarg->env;
7789 cc = iarg->cc;
7790 bs = cc->bs;
7791
7792 if (to_len == 1) {
7793 int is_in = onig_is_code_in_cc(env->enc, from, cc);
7794 #ifdef CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS
7795 if ((is_in != 0 && !IS_NCCLASS_NOT(cc)) ||
7796 (is_in == 0 && IS_NCCLASS_NOT(cc))) {
7797 if (ONIGENC_MBC_MINLEN(env->enc) > 1 || *to >= SINGLE_BYTE_SIZE) {
7798 add_code_range(&(cc->mbuf), env, *to, *to);
7799 }
7800 else {
7801 BITSET_SET_BIT(bs, *to);
7802 }
7803 }
7804 #else
7805 if (is_in != 0) {
7806 if (ONIGENC_MBC_MINLEN(env->enc) > 1 || *to >= SINGLE_BYTE_SIZE) {
7807 if (IS_NCCLASS_NOT(cc)) clear_not_flag_cclass(cc, env->enc);
7808 add_code_range(&(cc->mbuf), env, *to, *to);
7809 }
7810 else {
7811 if (IS_NCCLASS_NOT(cc)) {
7812 BITSET_CLEAR_BIT(bs, *to);
7813 }
7814 else
7815 BITSET_SET_BIT(bs, *to);
7816 }
7817 }
7818 #endif /* CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS */
7819 }
7820 else {
7821 int r, i, len;
7822 UChar buf[ONIGENC_CODE_TO_MBC_MAXLEN];
7823 Node *snode = NULL_NODE;
7824
7825 if (onig_is_code_in_cc(env->enc, from, cc)
7826 #ifdef CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS
7827 && !IS_NCCLASS_NOT(cc)
7828 #endif
7829 ) {
7830 for (i = 0; i < to_len; i++) {
7831 len = ONIGENC_CODE_TO_MBC(env->enc, to[i], buf);
7832 if (i == 0) {
7833 snode = onig_node_new_str(buf, buf + len);
7834 CHECK_NULL_RETURN_MEMERR(snode);
7835
7836 /* char-class expanded multi-char only
7837 compare with string folded at match time. */
7838 NODE_STRING_SET_AMBIG(snode);
7839 }
7840 else {
7841 r = onig_node_str_cat(snode, buf, buf + len);
7842 if (r < 0) {
7843 onig_node_free(snode);
7844 return r;
7845 }
7846 }
7847 }
7848
7849 *(iarg->ptail) = onig_node_new_alt(snode, NULL_NODE);
7850 CHECK_NULL_RETURN_MEMERR(*(iarg->ptail));
7851 iarg->ptail = &(NODE_CDR((*(iarg->ptail))));
7852 }
7853 }
7854
7855 return 0;
7856 }
7857
7858 static int
7859 parse_exp(Node** np, PToken* tok, int term, UChar** src, UChar* end,
7860 ScanEnv* env, int group_head)
7861 {
7862 int r, len, group;
7863 Node* qn;
7864 Node** tp;
7865 unsigned int parse_depth;
7866
7867 group = 0;
7868 *np = NULL;
7869 if (tok->type == (enum TokenSyms )term)
7870 goto end_of_token;
7871
7872 parse_depth = env->parse_depth;
7873
7874 switch (tok->type) {
7875 case TK_ALT:
7876 case TK_EOT:
7877 end_of_token:
7878 *np = node_new_empty();
7879 CHECK_NULL_RETURN_MEMERR(*np);
7880 return tok->type;
7881 break;
7882
7883 case TK_SUBEXP_OPEN:
7884 r = parse_bag(np, tok, TK_SUBEXP_CLOSE, src, end, env);
7885 if (r < 0) return r;
7886 if (r == 1) { /* group */
7887 if (group_head == 0)
7888 group = 1;
7889 else {
7890 Node* target = *np;
7891 *np = node_new_group(target);
7892 if (IS_NULL(*np)) {
7893 onig_node_free(target);
7894 return ONIGERR_MEMORY;
7895 }
7896 group = 2;
7897 }
7898 }
7899 else if (r == 2) { /* option only */
7900 Node* target;
7901 OnigOptionType prev = env->options;
7902
7903 env->options = BAG_(*np)->o.options;
7904 r = fetch_token(tok, src, end, env);
7905 if (r < 0) return r;
7906 r = parse_subexp(&target, tok, term, src, end, env, 0);
7907 env->options = prev;
7908 if (r < 0) {
7909 onig_node_free(target);
7910 return r;
7911 }
7912 NODE_BODY(*np) = target;
7913 return tok->type;
7914 }
7915 break;
7916
7917 case TK_SUBEXP_CLOSE:
7918 if (! IS_SYNTAX_BV(env->syntax, ONIG_SYN_ALLOW_UNMATCHED_CLOSE_SUBEXP))
7919 return ONIGERR_UNMATCHED_CLOSE_PARENTHESIS;
7920
7921 if (tok->escaped) goto tk_raw_byte;
7922 else goto tk_byte;
7923 break;
7924
7925 case TK_STRING:
7926 tk_byte:
7927 {
7928 *np = node_new_str(tok->backp, *src);
7929 CHECK_NULL_RETURN_MEMERR(*np);
7930
7931 while (1) {
7932 r = fetch_token(tok, src, end, env);
7933 if (r < 0) return r;
7934 if (r != TK_STRING) break;
7935
7936 r = onig_node_str_cat(*np, tok->backp, *src);
7937 if (r < 0) return r;
7938 }
7939
7940 string_end:
7941 tp = np;
7942 goto repeat;
7943 }
7944 break;
7945
7946 case TK_RAW_BYTE:
7947 tk_raw_byte:
7948 {
7949 *np = node_new_str_raw_char((UChar )tok->u.c);
7950 CHECK_NULL_RETURN_MEMERR(*np);
7951 len = 1;
7952 while (1) {
7953 if (len >= ONIGENC_MBC_MINLEN(env->enc)) {
7954 if (len == enclen(env->enc, STR_(*np)->s)) {
7955 r = fetch_token(tok, src, end, env);
7956 goto tk_raw_byte_end;
7957 }
7958 }
7959
7960 r = fetch_token(tok, src, end, env);
7961 if (r < 0) return r;
7962 if (r != TK_RAW_BYTE)
7963 return ONIGERR_TOO_SHORT_MULTI_BYTE_STRING;
7964
7965 r = node_str_cat_char(*np, (UChar )tok->u.c);
7966 if (r < 0) return r;
7967
7968 len++;
7969 }
7970
7971 tk_raw_byte_end:
7972 if (! ONIGENC_IS_VALID_MBC_STRING(env->enc, STR_(*np)->s, STR_(*np)->end))
7973 return ONIGERR_INVALID_WIDE_CHAR_VALUE;
7974
7975 NODE_STRING_CLEAR_RAW(*np);
7976 goto string_end;
7977 }
7978 break;
7979
7980 case TK_CODE_POINT:
7981 {
7982 UChar buf[ONIGENC_CODE_TO_MBC_MAXLEN];
7983 len = ONIGENC_CODE_TO_MBC(env->enc, tok->u.code, buf);
7984 if (len < 0) return len;
7985 #ifdef NUMBERED_CHAR_IS_NOT_CASE_AMBIG
7986 *np = node_new_str_raw(buf, buf + len);
7987 #else
7988 *np = node_new_str(buf, buf + len);
7989 #endif
7990 CHECK_NULL_RETURN_MEMERR(*np);
7991 }
7992 break;
7993
7994 case TK_QUOTE_OPEN:
7995 {
7996 OnigCodePoint end_op[2];
7997 UChar *qstart, *qend, *nextp;
7998
7999 end_op[0] = (OnigCodePoint )MC_ESC(env->syntax);
8000 end_op[1] = (OnigCodePoint )'E';
8001 qstart = *src;
8002 qend = find_str_position(end_op, 2, qstart, end, &nextp, env->enc);
8003 if (IS_NULL(qend)) {
8004 nextp = qend = end;
8005 }
8006 *np = node_new_str(qstart, qend);
8007 CHECK_NULL_RETURN_MEMERR(*np);
8008 *src = nextp;
8009 }
8010 break;
8011
8012 case TK_CHAR_TYPE:
8013 {
8014 switch (tok->u.prop.ctype) {
8015 case ONIGENC_CTYPE_WORD:
8016 *np = node_new_ctype(tok->u.prop.ctype, tok->u.prop.not, env->options);
8017 CHECK_NULL_RETURN_MEMERR(*np);
8018 break;
8019
8020 case ONIGENC_CTYPE_SPACE:
8021 case ONIGENC_CTYPE_DIGIT:
8022 case ONIGENC_CTYPE_XDIGIT:
8023 {
8024 CClassNode* cc;
8025
8026 *np = node_new_cclass();
8027 CHECK_NULL_RETURN_MEMERR(*np);
8028 cc = CCLASS_(*np);
8029 add_ctype_to_cc(cc, tok->u.prop.ctype, 0, env);
8030 if (tok->u.prop.not != 0) NCCLASS_SET_NOT(cc);
8031 }
8032 break;
8033
8034 default:
8035 return ONIGERR_PARSER_BUG;
8036 break;
8037 }
8038 }
8039 break;
8040
8041 case TK_CHAR_PROPERTY:
8042 r = parse_char_property(np, tok, src, end, env);
8043 if (r != 0) return r;
8044 break;
8045
8046 case TK_CC_OPEN:
8047 {
8048 CClassNode* cc;
8049
8050 r = parse_char_class(np, tok, src, end, env);
8051 if (r != 0) return r;
8052
8053 cc = CCLASS_(*np);
8054 if (IS_IGNORECASE(env->options)) {
8055 IApplyCaseFoldArg iarg;
8056
8057 iarg.env = env;
8058 iarg.cc = cc;
8059 iarg.alt_root = NULL_NODE;
8060 iarg.ptail = &(iarg.alt_root);
8061
8062 r = ONIGENC_APPLY_ALL_CASE_FOLD(env->enc, env->case_fold_flag,
8063 i_apply_case_fold, &iarg);
8064 if (r != 0) {
8065 onig_node_free(iarg.alt_root);
8066 return r;
8067 }
8068 if (IS_NOT_NULL(iarg.alt_root)) {
8069 Node* work = onig_node_new_alt(*np, iarg.alt_root);
8070 if (IS_NULL(work)) {
8071 onig_node_free(iarg.alt_root);
8072 return ONIGERR_MEMORY;
8073 }
8074 *np = work;
8075 }
8076 }
8077 }
8078 break;
8079
8080 case TK_ANYCHAR:
8081 *np = node_new_anychar();
8082 CHECK_NULL_RETURN_MEMERR(*np);
8083 break;
8084
8085 case TK_ANYCHAR_ANYTIME:
8086 *np = node_new_anychar();
8087 CHECK_NULL_RETURN_MEMERR(*np);
8088 qn = node_new_quantifier(0, INFINITE_REPEAT, 0);
8089 CHECK_NULL_RETURN_MEMERR(qn);
8090 NODE_BODY(qn) = *np;
8091 *np = qn;
8092 break;
8093
8094 case TK_BACKREF:
8095 len = tok->u.backref.num;
8096 *np = node_new_backref(len,
8097 (len > 1 ? tok->u.backref.refs : &(tok->u.backref.ref1)),
8098 tok->u.backref.by_name,
8099 #ifdef USE_BACKREF_WITH_LEVEL
8100 tok->u.backref.exist_level,
8101 tok->u.backref.level,
8102 #endif
8103 env);
8104 CHECK_NULL_RETURN_MEMERR(*np);
8105 break;
8106
8107 #ifdef USE_CALL
8108 case TK_CALL:
8109 {
8110 int gnum = tok->u.call.gnum;
8111
8112 *np = node_new_call(tok->u.call.name, tok->u.call.name_end,
8113 gnum, tok->u.call.by_number);
8114 CHECK_NULL_RETURN_MEMERR(*np);
8115 env->num_call++;
8116 if (tok->u.call.by_number != 0 && gnum == 0) {
8117 env->has_call_zero = 1;
8118 }
8119 }
8120 break;
8121 #endif
8122
8123 case TK_ANCHOR:
8124 {
8125 int ascii_mode =
8126 IS_WORD_ASCII(env->options) && IS_WORD_ANCHOR_TYPE(tok->u.anchor) ? 1 : 0;
8127 *np = onig_node_new_anchor(tok->u.anchor, ascii_mode);
8128 CHECK_NULL_RETURN_MEMERR(*np);
8129 }
8130 break;
8131
8132 case TK_REPEAT:
8133 case TK_INTERVAL:
8134 if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_CONTEXT_INDEP_REPEAT_OPS)) {
8135 if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_CONTEXT_INVALID_REPEAT_OPS))
8136 return ONIGERR_TARGET_OF_REPEAT_OPERATOR_NOT_SPECIFIED;
8137 else {
8138 *np = node_new_empty();
8139 CHECK_NULL_RETURN_MEMERR(*np);
8140 }
8141 }
8142 else {
8143 goto tk_byte;
8144 }
8145 break;
8146
8147 case TK_KEEP:
8148 r = node_new_keep(np, env);
8149 if (r < 0) return r;
8150 break;
8151
8152 case TK_GENERAL_NEWLINE:
8153 r = node_new_general_newline(np, env);
8154 if (r < 0) return r;
8155 break;
8156
8157 case TK_NO_NEWLINE:
8158 r = node_new_no_newline(np, env);
8159 if (r < 0) return r;
8160 break;
8161
8162 case TK_TRUE_ANYCHAR:
8163 r = node_new_true_anychar(np, env);
8164 if (r < 0) return r;
8165 break;
8166
8167 case TK_TEXT_SEGMENT:
8168 r = make_text_segment(np, env);
8169 if (r < 0) return r;
8170 break;
8171
8172 default:
8173 return ONIGERR_PARSER_BUG;
8174 break;
8175 }
8176
8177 {
8178 tp = np;
8179
8180 re_entry:
8181 r = fetch_token(tok, src, end, env);
8182 if (r < 0) return r;
8183
8184 repeat:
8185 if (r == TK_REPEAT || r == TK_INTERVAL) {
8186 Node* target;
8187
8188 if (is_invalid_quantifier_target(*tp))
8189 return ONIGERR_TARGET_OF_REPEAT_OPERATOR_INVALID;
8190
8191 parse_depth++;
8192 if (parse_depth > ParseDepthLimit)
8193 return ONIGERR_PARSE_DEPTH_LIMIT_OVER;
8194
8195 qn = node_new_quantifier(tok->u.repeat.lower, tok->u.repeat.upper,
8196 r == TK_INTERVAL);
8197 CHECK_NULL_RETURN_MEMERR(qn);
8198 QUANT_(qn)->greedy = tok->u.repeat.greedy;
8199 if (group == 2) {
8200 target = node_drop_group(*tp);
8201 *tp = NULL_NODE;
8202 }
8203 else {
8204 target = *tp;
8205 }
8206 r = set_quantifier(qn, target, group, env);
8207 if (r < 0) {
8208 onig_node_free(qn);
8209 return r;
8210 }
8211
8212 if (tok->u.repeat.possessive != 0) {
8213 Node* en;
8214 en = node_new_bag(BAG_STOP_BACKTRACK);
8215 if (IS_NULL(en)) {
8216 onig_node_free(qn);
8217 return ONIGERR_MEMORY;
8218 }
8219 NODE_BODY(en) = qn;
8220 qn = en;
8221 }
8222
8223 if (r == 0) {
8224 *tp = qn;
8225 }
8226 else if (r == 1) { /* x{1,1} ==> x */
8227 onig_node_free(qn);
8228 *tp = target;
8229 }
8230 else if (r == 2) { /* split case: /abc+/ */
8231 Node *tmp;
8232
8233 *tp = node_new_list(*tp, NULL);
8234 if (IS_NULL(*tp)) {
8235 onig_node_free(qn);
8236 return ONIGERR_MEMORY;
8237 }
8238 tmp = NODE_CDR(*tp) = node_new_list(qn, NULL);
8239 if (IS_NULL(tmp)) {
8240 onig_node_free(qn);
8241 return ONIGERR_MEMORY;
8242 }
8243 tp = &(NODE_CAR(tmp));
8244 }
8245 group = 0;
8246 goto re_entry;
8247 }
8248 }
8249
8250 return r;
8251 }
8252
8253 static int
8254 parse_branch(Node** top, PToken* tok, int term, UChar** src, UChar* end,
8255 ScanEnv* env, int group_head)
8256 {
8257 int r;
8258 Node *node, **headp;
8259
8260 *top = NULL;
8261 r = parse_exp(&node, tok, term, src, end, env, group_head);
8262 if (r < 0) {
8263 onig_node_free(node);
8264 return r;
8265 }
8266
8267 if (r == TK_EOT || r == term || r == TK_ALT) {
8268 *top = node;
8269 }
8270 else {
8271 *top = node_new_list(node, NULL);
8272 if (IS_NULL(*top)) {
8273 onig_node_free(node);
8274 return ONIGERR_MEMORY;
8275 }
8276
8277 headp = &(NODE_CDR(*top));
8278 while (r != TK_EOT && r != term && r != TK_ALT) {
8279 r = parse_exp(&node, tok, term, src, end, env, 0);
8280 if (r < 0) {
8281 onig_node_free(node);
8282 return r;
8283 }
8284
8285 if (NODE_TYPE(node) == NODE_LIST) {
8286 *headp = node;
8287 while (IS_NOT_NULL(NODE_CDR(node))) node = NODE_CDR(node);
8288 headp = &(NODE_CDR(node));
8289 }
8290 else {
8291 *headp = node_new_list(node, NULL);
8292 headp = &(NODE_CDR(*headp));
8293 }
8294 }
8295 }
8296
8297 return r;
8298 }
8299
8300 /* term_tok: TK_EOT or TK_SUBEXP_CLOSE */
8301 static int
8302 parse_subexp(Node** top, PToken* tok, int term, UChar** src, UChar* end,
8303 ScanEnv* env, int group_head)
8304 {
8305 int r;
8306 Node *node, **headp;
8307
8308 *top = NULL;
8309 env->parse_depth++;
8310 if (env->parse_depth > ParseDepthLimit)
8311 return ONIGERR_PARSE_DEPTH_LIMIT_OVER;
8312
8313 r = parse_branch(&node, tok, term, src, end, env, group_head);
8314 if (r < 0) {
8315 onig_node_free(node);
8316 return r;
8317 }
8318
8319 if (r == term) {
8320 *top = node;
8321 }
8322 else if (r == TK_ALT) {
8323 *top = onig_node_new_alt(node, NULL);
8324 if (IS_NULL(*top)) {
8325 onig_node_free(node);
8326 return ONIGERR_MEMORY;
8327 }
8328
8329 headp = &(NODE_CDR(*top));
8330 while (r == TK_ALT) {
8331 r = fetch_token(tok, src, end, env);
8332 if (r < 0) return r;
8333 r = parse_branch(&node, tok, term, src, end, env, 0);
8334 if (r < 0) {
8335 onig_node_free(node);
8336 return r;
8337 }
8338 *headp = onig_node_new_alt(node, NULL);
8339 if (IS_NULL(*headp)) {
8340 onig_node_free(node);
8341 onig_node_free(*top);
8342 return ONIGERR_MEMORY;
8343 }
8344
8345 headp = &(NODE_CDR(*headp));
8346 }
8347
8348 if (tok->type != (enum TokenSyms )term)
8349 goto err;
8350 }
8351 else {
8352 onig_node_free(node);
8353 err:
8354 if (term == TK_SUBEXP_CLOSE)
8355 return ONIGERR_END_PATTERN_WITH_UNMATCHED_PARENTHESIS;
8356 else
8357 return ONIGERR_PARSER_BUG;
8358 }
8359
8360 env->parse_depth--;
8361 return r;
8362 }
8363
8364 static int
8365 parse_regexp(Node** top, UChar** src, UChar* end, ScanEnv* env)
8366 {
8367 int r;
8368 PToken tok;
8369
8370 r = fetch_token(&tok, src, end, env);
8371 if (r < 0) return r;
8372 r = parse_subexp(top, &tok, TK_EOT, src, end, env, 0);
8373 if (r < 0) return r;
8374
8375 return 0;
8376 }
8377
8378 #ifdef USE_CALL
8379 static int
8380 make_call_zero_body(Node* node, ScanEnv* env, Node** rnode)
8381 {
8382 int r;
8383
8384 Node* x = node_new_memory(0 /* 0: is not named */);
8385 CHECK_NULL_RETURN_MEMERR(x);
8386
8387 NODE_BODY(x) = node;
8388 BAG_(x)->m.regnum = 0;
8389 r = scan_env_set_mem_node(env, 0, x);
8390 if (r != 0) {
8391 onig_node_free(x);
8392 return r;
8393 }
8394
8395 *rnode = x;
8396 return 0;
8397 }
8398 #endif
8399
8400 extern int
8401 onig_parse_tree(Node** root, const UChar* pattern, const UChar* end,
8402 regex_t* reg, ScanEnv* env)
8403 {
8404 int r;
8405 UChar* p;
8406 #ifdef USE_CALLOUT
8407 RegexExt* ext;
8408 #endif
8409
8410 names_clear(reg);
8411
8412 scan_env_clear(env);
8413 env->options = reg->options;
8414 env->case_fold_flag = reg->case_fold_flag;
8415 env->enc = reg->enc;
8416 env->syntax = reg->syntax;
8417 env->pattern = (UChar* )pattern;
8418 env->pattern_end = (UChar* )end;
8419 env->reg = reg;
8420
8421 *root = NULL;
8422
8423 if (! ONIGENC_IS_VALID_MBC_STRING(env->enc, pattern, end))
8424 return ONIGERR_INVALID_WIDE_CHAR_VALUE;
8425
8426 p = (UChar* )pattern;
8427 r = parse_regexp(root, &p, (UChar* )end, env);
8428
8429 #ifdef USE_CALL
8430 if (r != 0) return r;
8431
8432 if (env->has_call_zero != 0) {
8433 Node* zero_node;
8434 r = make_call_zero_body(*root, env, &zero_node);
8435 if (r != 0) return r;
8436
8437 *root = zero_node;
8438 }
8439 #endif
8440
8441 reg->num_mem = env->num_mem;
8442
8443 #ifdef USE_CALLOUT
8444 ext = reg->extp;
8445 if (IS_NOT_NULL(ext) && ext->callout_num > 0) {
8446 r = setup_ext_callout_list_values(reg);
8447 }
8448 #endif
8449
8450 return r;
8451 }
8452
8453 extern void
8454 onig_scan_env_set_error_string(ScanEnv* env, int ecode ARG_UNUSED,
8455 UChar* arg, UChar* arg_end)
8456 {
8457 env->error = arg;
8458 env->error_end = arg_end;
8459 }