* io.c (rb_open_file): encoding in mode string was ignored if perm is
[ruby-svn.git] / regparse.c
blob3d2c57a25a439e10bb703375436381c554961dc6
1 /**********************************************************************
2 regparse.c - Oniguruma (regular expression library)
3 **********************************************************************/
4 /*-
5 * Copyright (c) 2002-2007 K.Kosako <sndgk393 AT ybb DOT ne DOT jp>
6 * All rights reserved.
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.
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.
30 #include "regparse.h"
32 #define WARN_BUFSIZE 256
34 #define CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS
37 const OnigSyntaxType OnigSyntaxRuby = {
38 (( SYN_GNU_REGEX_OP | ONIG_SYN_OP_QMARK_NON_GREEDY |
39 ONIG_SYN_OP_ESC_OCTAL3 | ONIG_SYN_OP_ESC_X_HEX2 |
40 ONIG_SYN_OP_ESC_X_BRACE_HEX8 | ONIG_SYN_OP_ESC_CONTROL_CHARS |
41 ONIG_SYN_OP_ESC_C_CONTROL )
42 & ~ONIG_SYN_OP_ESC_LTGT_WORD_BEGIN_END )
43 , ( ONIG_SYN_OP2_QMARK_GROUP_EFFECT |
44 ONIG_SYN_OP2_OPTION_RUBY |
45 ONIG_SYN_OP2_QMARK_LT_NAMED_GROUP | ONIG_SYN_OP2_ESC_K_NAMED_BACKREF |
46 ONIG_SYN_OP2_ESC_G_SUBEXP_CALL |
47 ONIG_SYN_OP2_ESC_P_BRACE_CHAR_PROPERTY |
48 ONIG_SYN_OP2_ESC_P_BRACE_CIRCUMFLEX_NOT |
49 ONIG_SYN_OP2_PLUS_POSSESSIVE_REPEAT |
50 ONIG_SYN_OP2_CCLASS_SET_OP | ONIG_SYN_OP2_ESC_CAPITAL_C_BAR_CONTROL |
51 ONIG_SYN_OP2_ESC_CAPITAL_M_BAR_META | ONIG_SYN_OP2_ESC_V_VTAB |
52 ONIG_SYN_OP2_ESC_H_XDIGIT )
53 , ( SYN_GNU_REGEX_BV |
54 ONIG_SYN_ALLOW_INTERVAL_LOW_ABBREV |
55 ONIG_SYN_DIFFERENT_LEN_ALT_LOOK_BEHIND |
56 ONIG_SYN_CAPTURE_ONLY_NAMED_GROUP |
57 ONIG_SYN_ALLOW_MULTIPLEX_DEFINITION_NAME |
58 ONIG_SYN_FIXED_INTERVAL_IS_GREEDY_ONLY |
59 ONIG_SYN_WARN_CC_OP_NOT_ESCAPED |
60 ONIG_SYN_WARN_REDUNDANT_NESTED_REPEAT )
61 , ONIG_OPTION_NONE
64 (OnigCodePoint )'\\' /* esc */
65 , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* anychar '.' */
66 , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* anytime '*' */
67 , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* zero or one time '?' */
68 , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* one or more time '+' */
69 , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* anychar anytime */
73 const OnigSyntaxType* OnigDefaultSyntax = ONIG_SYNTAX_RUBY;
75 extern void onig_null_warn(const char* s ARG_UNUSED) { }
77 #ifdef DEFAULT_WARN_FUNCTION
78 static OnigWarnFunc onig_warn = (OnigWarnFunc )DEFAULT_WARN_FUNCTION;
79 #else
80 static OnigWarnFunc onig_warn = onig_null_warn;
81 #endif
83 #ifdef DEFAULT_VERB_WARN_FUNCTION
84 static OnigWarnFunc onig_verb_warn = (OnigWarnFunc )DEFAULT_VERB_WARN_FUNCTION;
85 #else
86 static OnigWarnFunc onig_verb_warn = onig_null_warn;
87 #endif
89 extern void onig_set_warn_func(OnigWarnFunc f)
91 onig_warn = f;
94 extern void onig_set_verb_warn_func(OnigWarnFunc f)
96 onig_verb_warn = f;
99 static void
100 bbuf_free(BBuf* bbuf)
102 if (IS_NOT_NULL(bbuf)) {
103 if (IS_NOT_NULL(bbuf->p)) xfree(bbuf->p);
104 xfree(bbuf);
108 static int
109 bbuf_clone(BBuf** rto, BBuf* from)
111 int r;
112 BBuf *to;
114 *rto = to = (BBuf* )xmalloc(sizeof(BBuf));
115 CHECK_NULL_RETURN_MEMERR(to);
116 r = BBUF_INIT(to, from->alloc);
117 if (r != 0) return r;
118 to->used = from->used;
119 xmemcpy(to->p, from->p, from->used);
120 return 0;
123 #define BACKREF_REL_TO_ABS(rel_no, env) \
124 ((env)->num_mem + 1 + (rel_no))
126 #define ONOFF(v,f,negative) (negative) ? ((v) &= ~(f)) : ((v) |= (f))
128 #define MBCODE_START_POS(enc) \
129 (OnigCodePoint )(ONIGENC_MBC_MINLEN(enc) > 1 ? 0 : 0x80)
131 #define SET_ALL_MULTI_BYTE_RANGE(enc, pbuf) \
132 add_code_range_to_buf(pbuf, MBCODE_START_POS(enc), ~((OnigCodePoint )0))
134 #define ADD_ALL_MULTI_BYTE_RANGE(enc, mbuf) do {\
135 if (! ONIGENC_IS_SINGLEBYTE(enc)) {\
136 r = SET_ALL_MULTI_BYTE_RANGE(enc, &(mbuf));\
137 if (r) return r;\
139 } while (0)
142 #define BITSET_IS_EMPTY(bs,empty) do {\
143 int i;\
144 empty = 1;\
145 for (i = 0; i < (int )BITSET_SIZE; i++) {\
146 if ((bs)[i] != 0) {\
147 empty = 0; break;\
150 } while (0)
152 static void
153 bitset_set_range(BitSetRef bs, int from, int to)
155 int i;
156 for (i = from; i <= to && i < SINGLE_BYTE_SIZE; i++) {
157 BITSET_SET_BIT(bs, i);
161 #if 0
162 static void
163 bitset_set_all(BitSetRef bs)
165 int i;
166 for (i = 0; i < BITSET_SIZE; i++) { bs[i] = ~((Bits )0); }
168 #endif
170 static void
171 bitset_invert(BitSetRef bs)
173 int i;
174 for (i = 0; i < (int )BITSET_SIZE; i++) { bs[i] = ~(bs[i]); }
177 static void
178 bitset_invert_to(BitSetRef from, BitSetRef to)
180 int i;
181 for (i = 0; i < (int )BITSET_SIZE; i++) { to[i] = ~(from[i]); }
184 static void
185 bitset_and(BitSetRef dest, BitSetRef bs)
187 int i;
188 for (i = 0; i < (int )BITSET_SIZE; i++) { dest[i] &= bs[i]; }
191 static void
192 bitset_or(BitSetRef dest, BitSetRef bs)
194 int i;
195 for (i = 0; i < (int )BITSET_SIZE; i++) { dest[i] |= bs[i]; }
198 static void
199 bitset_copy(BitSetRef dest, BitSetRef bs)
201 int i;
202 for (i = 0; i < (int )BITSET_SIZE; i++) { dest[i] = bs[i]; }
205 extern int
206 onig_strncmp(const UChar* s1, const UChar* s2, int n)
208 int x;
210 while (n-- > 0) {
211 x = *s2++ - *s1++;
212 if (x) return x;
214 return 0;
217 extern void
218 onig_strcpy(UChar* dest, const UChar* src, const UChar* end)
220 int len = end - src;
221 if (len > 0) {
222 xmemcpy(dest, src, len);
223 dest[len] = (UChar )0;
227 #ifdef USE_NAMED_GROUP
228 static UChar*
229 strdup_with_null(OnigEncoding enc, UChar* s, UChar* end)
231 int slen, term_len, i;
232 UChar *r;
234 slen = end - s;
235 term_len = ONIGENC_MBC_MINLEN(enc);
237 r = (UChar* )xmalloc(slen + term_len);
238 CHECK_NULL_RETURN(r);
239 xmemcpy(r, s, slen);
241 for (i = 0; i < term_len; i++)
242 r[slen + i] = (UChar )0;
244 return r;
246 #endif
248 /* scan pattern methods */
249 #define PEND_VALUE 0
251 #define PFETCH_READY UChar* pfetch_prev
252 #define PEND (p < end ? 0 : 1)
253 #define PUNFETCH p = pfetch_prev
254 #define PINC do { \
255 pfetch_prev = p; \
256 p += enclen(enc, p, end); \
257 } while (0)
258 #define PFETCH(c) do { \
259 c = ((enc->max_enc_len == 1) ? *p : ONIGENC_MBC_TO_CODE(enc, p, end)); \
260 pfetch_prev = p; \
261 p += enclen(enc, p, end); \
262 } while (0)
264 #define PPEEK (p < end ? ONIGENC_MBC_TO_CODE(enc, p, end) : PEND_VALUE)
265 #define PPEEK_IS(c) (PPEEK == (OnigCodePoint )c)
267 static UChar*
268 strcat_capa(UChar* dest, UChar* dest_end, const UChar* src, const UChar* src_end,
269 int capa)
271 UChar* r;
273 if (dest)
274 r = (UChar* )xrealloc(dest, capa + 1);
275 else
276 r = (UChar* )xmalloc(capa + 1);
278 CHECK_NULL_RETURN(r);
279 onig_strcpy(r + (dest_end - dest), src, src_end);
280 return r;
283 /* dest on static area */
284 static UChar*
285 strcat_capa_from_static(UChar* dest, UChar* dest_end,
286 const UChar* src, const UChar* src_end, int capa)
288 UChar* r;
290 r = (UChar* )xmalloc(capa + 1);
291 CHECK_NULL_RETURN(r);
292 onig_strcpy(r, dest, dest_end);
293 onig_strcpy(r + (dest_end - dest), src, src_end);
294 return r;
298 #ifdef USE_ST_LIBRARY
300 #include "ruby/st.h"
302 typedef struct {
303 UChar* s;
304 UChar* end;
305 } st_str_end_key;
307 static int
308 str_end_cmp(st_str_end_key* x, st_str_end_key* y)
310 UChar *p, *q;
311 int c;
313 if ((x->end - x->s) != (y->end - y->s))
314 return 1;
316 p = x->s;
317 q = y->s;
318 while (p < x->end) {
319 c = (int )*p - (int )*q;
320 if (c != 0) return c;
322 p++; q++;
325 return 0;
328 static int
329 str_end_hash(st_str_end_key* x)
331 UChar *p;
332 int val = 0;
334 p = x->s;
335 while (p < x->end) {
336 val = val * 997 + (int )*p++;
339 return val + (val >> 5);
342 extern hash_table_type*
343 onig_st_init_strend_table_with_size(int size)
345 static const struct st_hash_type hashType = {
346 str_end_cmp,
347 str_end_hash,
350 return (hash_table_type* )
351 onig_st_init_table_with_size(&hashType, size);
354 extern int
355 onig_st_lookup_strend(hash_table_type* table, const UChar* str_key,
356 const UChar* end_key, hash_data_type *value)
358 st_str_end_key key;
360 key.s = (UChar* )str_key;
361 key.end = (UChar* )end_key;
363 return onig_st_lookup(table, (st_data_t )(&key), value);
366 extern int
367 onig_st_insert_strend(hash_table_type* table, const UChar* str_key,
368 const UChar* end_key, hash_data_type value)
370 st_str_end_key* key;
371 int result;
373 key = (st_str_end_key* )xmalloc(sizeof(st_str_end_key));
374 key->s = (UChar* )str_key;
375 key->end = (UChar* )end_key;
376 result = onig_st_insert(table, (st_data_t )key, value);
377 if (result) {
378 xfree(key);
380 return result;
383 #endif /* USE_ST_LIBRARY */
386 #ifdef USE_NAMED_GROUP
388 #define INIT_NAME_BACKREFS_ALLOC_NUM 8
390 typedef struct {
391 UChar* name;
392 int name_len; /* byte length */
393 int back_num; /* number of backrefs */
394 int back_alloc;
395 int back_ref1;
396 int* back_refs;
397 } NameEntry;
399 #ifdef USE_ST_LIBRARY
401 typedef st_table NameTable;
402 typedef st_data_t HashDataType; /* 1.6 st.h doesn't define st_data_t type */
404 #define NAMEBUF_SIZE 24
405 #define NAMEBUF_SIZE_1 25
407 #ifdef ONIG_DEBUG
408 static int
409 i_print_name_entry(UChar* key, NameEntry* e, void* arg)
411 int i;
412 FILE* fp = (FILE* )arg;
414 fprintf(fp, "%s: ", e->name);
415 if (e->back_num == 0)
416 fputs("-", fp);
417 else if (e->back_num == 1)
418 fprintf(fp, "%d", e->back_ref1);
419 else {
420 for (i = 0; i < e->back_num; i++) {
421 if (i > 0) fprintf(fp, ", ");
422 fprintf(fp, "%d", e->back_refs[i]);
425 fputs("\n", fp);
426 return ST_CONTINUE;
429 extern int
430 onig_print_names(FILE* fp, regex_t* reg)
432 NameTable* t = (NameTable* )reg->name_table;
434 if (IS_NOT_NULL(t)) {
435 fprintf(fp, "name table\n");
436 onig_st_foreach(t, i_print_name_entry, (HashDataType )fp);
437 fputs("\n", fp);
439 return 0;
441 #endif /* ONIG_DEBUG */
443 static int
444 i_free_name_entry(UChar* key, NameEntry* e, void* arg ARG_UNUSED)
446 xfree(e->name);
447 if (IS_NOT_NULL(e->back_refs)) xfree(e->back_refs);
448 xfree(key);
449 xfree(e);
450 return ST_DELETE;
453 static int
454 names_clear(regex_t* reg)
456 NameTable* t = (NameTable* )reg->name_table;
458 if (IS_NOT_NULL(t)) {
459 onig_st_foreach(t, i_free_name_entry, 0);
461 return 0;
464 extern int
465 onig_names_free(regex_t* reg)
467 int r;
468 NameTable* t;
470 r = names_clear(reg);
471 if (r) return r;
473 t = (NameTable* )reg->name_table;
474 if (IS_NOT_NULL(t)) onig_st_free_table(t);
475 reg->name_table = (void* )NULL;
476 return 0;
479 static NameEntry*
480 name_find(regex_t* reg, const UChar* name, const UChar* name_end)
482 NameEntry* e;
483 NameTable* t = (NameTable* )reg->name_table;
485 e = (NameEntry* )NULL;
486 if (IS_NOT_NULL(t)) {
487 onig_st_lookup_strend(t, name, name_end, (HashDataType* )((void* )(&e)));
489 return e;
492 typedef struct {
493 int (*func)(const UChar*, const UChar*,int,int*,regex_t*,void*);
494 regex_t* reg;
495 void* arg;
496 int ret;
497 OnigEncoding enc;
498 } INamesArg;
500 static int
501 i_names(UChar* key ARG_UNUSED, NameEntry* e, INamesArg* arg)
503 int r = (*(arg->func))(e->name,
504 e->name + e->name_len,
505 e->back_num,
506 (e->back_num > 1 ? e->back_refs : &(e->back_ref1)),
507 arg->reg, arg->arg);
508 if (r != 0) {
509 arg->ret = r;
510 return ST_STOP;
512 return ST_CONTINUE;
515 extern int
516 onig_foreach_name(regex_t* reg,
517 int (*func)(const UChar*, const UChar*,int,int*,regex_t*,void*), void* arg)
519 INamesArg narg;
520 NameTable* t = (NameTable* )reg->name_table;
522 narg.ret = 0;
523 if (IS_NOT_NULL(t)) {
524 narg.func = func;
525 narg.reg = reg;
526 narg.arg = arg;
527 narg.enc = reg->enc; /* should be pattern encoding. */
528 onig_st_foreach(t, i_names, (HashDataType )&narg);
530 return narg.ret;
533 static int
534 i_renumber_name(UChar* key ARG_UNUSED, NameEntry* e, GroupNumRemap* map)
536 int i;
538 if (e->back_num > 1) {
539 for (i = 0; i < e->back_num; i++) {
540 e->back_refs[i] = map[e->back_refs[i]].new_val;
543 else if (e->back_num == 1) {
544 e->back_ref1 = map[e->back_ref1].new_val;
547 return ST_CONTINUE;
550 extern int
551 onig_renumber_name_table(regex_t* reg, GroupNumRemap* map)
553 NameTable* t = (NameTable* )reg->name_table;
555 if (IS_NOT_NULL(t)) {
556 onig_st_foreach(t, i_renumber_name, (HashDataType )map);
558 return 0;
562 extern int
563 onig_number_of_names(regex_t* reg)
565 NameTable* t = (NameTable* )reg->name_table;
567 if (IS_NOT_NULL(t))
568 return t->num_entries;
569 else
570 return 0;
573 #else /* USE_ST_LIBRARY */
575 #define INIT_NAMES_ALLOC_NUM 8
577 typedef struct {
578 NameEntry* e;
579 int num;
580 int alloc;
581 } NameTable;
583 #ifdef ONIG_DEBUG
584 extern int
585 onig_print_names(FILE* fp, regex_t* reg)
587 int i, j;
588 NameEntry* e;
589 NameTable* t = (NameTable* )reg->name_table;
591 if (IS_NOT_NULL(t) && t->num > 0) {
592 fprintf(fp, "name table\n");
593 for (i = 0; i < t->num; i++) {
594 e = &(t->e[i]);
595 fprintf(fp, "%s: ", e->name);
596 if (e->back_num == 0) {
597 fputs("-", fp);
599 else if (e->back_num == 1) {
600 fprintf(fp, "%d", e->back_ref1);
602 else {
603 for (j = 0; j < e->back_num; j++) {
604 if (j > 0) fprintf(fp, ", ");
605 fprintf(fp, "%d", e->back_refs[j]);
608 fputs("\n", fp);
610 fputs("\n", fp);
612 return 0;
614 #endif
616 static int
617 names_clear(regex_t* reg)
619 int i;
620 NameEntry* e;
621 NameTable* t = (NameTable* )reg->name_table;
623 if (IS_NOT_NULL(t)) {
624 for (i = 0; i < t->num; i++) {
625 e = &(t->e[i]);
626 if (IS_NOT_NULL(e->name)) {
627 xfree(e->name);
628 e->name = NULL;
629 e->name_len = 0;
630 e->back_num = 0;
631 e->back_alloc = 0;
632 if (IS_NOT_NULL(e->back_refs)) xfree(e->back_refs);
633 e->back_refs = (int* )NULL;
636 if (IS_NOT_NULL(t->e)) {
637 xfree(t->e);
638 t->e = NULL;
640 t->num = 0;
642 return 0;
645 extern int
646 onig_names_free(regex_t* reg)
648 int r;
649 NameTable* t;
651 r = names_clear(reg);
652 if (r) return r;
654 t = (NameTable* )reg->name_table;
655 if (IS_NOT_NULL(t)) xfree(t);
656 reg->name_table = NULL;
657 return 0;
660 static NameEntry*
661 name_find(regex_t* reg, UChar* name, UChar* name_end)
663 int i, len;
664 NameEntry* e;
665 NameTable* t = (NameTable* )reg->name_table;
667 if (IS_NOT_NULL(t)) {
668 len = name_end - name;
669 for (i = 0; i < t->num; i++) {
670 e = &(t->e[i]);
671 if (len == e->name_len && onig_strncmp(name, e->name, len) == 0)
672 return e;
675 return (NameEntry* )NULL;
678 extern int
679 onig_foreach_name(regex_t* reg,
680 int (*func)(const UChar*, const UChar*,int,int*,regex_t*,void*), void* arg)
682 int i, r;
683 NameEntry* e;
684 NameTable* t = (NameTable* )reg->name_table;
686 if (IS_NOT_NULL(t)) {
687 for (i = 0; i < t->num; i++) {
688 e = &(t->e[i]);
689 r = (*func)(e->name, e->name + e->name_len, e->back_num,
690 (e->back_num > 1 ? e->back_refs : &(e->back_ref1)),
691 reg, arg);
692 if (r != 0) return r;
695 return 0;
698 extern int
699 onig_number_of_names(regex_t* reg)
701 NameTable* t = (NameTable* )reg->name_table;
703 if (IS_NOT_NULL(t))
704 return t->num;
705 else
706 return 0;
709 #endif /* else USE_ST_LIBRARY */
711 static int
712 name_add(regex_t* reg, UChar* name, UChar* name_end, int backref, ScanEnv* env)
714 int alloc;
715 NameEntry* e;
716 NameTable* t = (NameTable* )reg->name_table;
718 if (name_end - name <= 0)
719 return ONIGERR_EMPTY_GROUP_NAME;
721 e = name_find(reg, name, name_end);
722 if (IS_NULL(e)) {
723 #ifdef USE_ST_LIBRARY
724 if (IS_NULL(t)) {
725 t = onig_st_init_strend_table_with_size(5);
726 reg->name_table = (void* )t;
728 e = (NameEntry* )xmalloc(sizeof(NameEntry));
729 CHECK_NULL_RETURN_MEMERR(e);
731 e->name = strdup_with_null(reg->enc, name, name_end);
732 if (IS_NULL(e->name)) {
733 xfree(e);
734 return ONIGERR_MEMORY;
736 onig_st_insert_strend(t, e->name, (e->name + (name_end - name)),
737 (HashDataType )e);
739 e->name_len = name_end - name;
740 e->back_num = 0;
741 e->back_alloc = 0;
742 e->back_refs = (int* )NULL;
744 #else
746 if (IS_NULL(t)) {
747 alloc = INIT_NAMES_ALLOC_NUM;
748 t = (NameTable* )xmalloc(sizeof(NameTable));
749 CHECK_NULL_RETURN_MEMERR(t);
750 t->e = NULL;
751 t->alloc = 0;
752 t->num = 0;
754 t->e = (NameEntry* )xmalloc(sizeof(NameEntry) * alloc);
755 if (IS_NULL(t->e)) {
756 xfree(t);
757 return ONIGERR_MEMORY;
759 t->alloc = alloc;
760 reg->name_table = t;
761 goto clear;
763 else if (t->num == t->alloc) {
764 int i;
766 alloc = t->alloc * 2;
767 t->e = (NameEntry* )xrealloc(t->e, sizeof(NameEntry) * alloc);
768 CHECK_NULL_RETURN_MEMERR(t->e);
769 t->alloc = alloc;
771 clear:
772 for (i = t->num; i < t->alloc; i++) {
773 t->e[i].name = NULL;
774 t->e[i].name_len = 0;
775 t->e[i].back_num = 0;
776 t->e[i].back_alloc = 0;
777 t->e[i].back_refs = (int* )NULL;
780 e = &(t->e[t->num]);
781 t->num++;
782 e->name = strdup_with_null(reg->enc, name, name_end);
783 e->name_len = name_end - name;
784 #endif
787 if (e->back_num >= 1 &&
788 ! IS_SYNTAX_BV(env->syntax, ONIG_SYN_ALLOW_MULTIPLEX_DEFINITION_NAME)) {
789 onig_scan_env_set_error_string(env, ONIGERR_MULTIPLEX_DEFINED_NAME,
790 name, name_end);
791 return ONIGERR_MULTIPLEX_DEFINED_NAME;
794 e->back_num++;
795 if (e->back_num == 1) {
796 e->back_ref1 = backref;
798 else {
799 if (e->back_num == 2) {
800 alloc = INIT_NAME_BACKREFS_ALLOC_NUM;
801 e->back_refs = (int* )xmalloc(sizeof(int) * alloc);
802 CHECK_NULL_RETURN_MEMERR(e->back_refs);
803 e->back_alloc = alloc;
804 e->back_refs[0] = e->back_ref1;
805 e->back_refs[1] = backref;
807 else {
808 if (e->back_num > e->back_alloc) {
809 alloc = e->back_alloc * 2;
810 e->back_refs = (int* )xrealloc(e->back_refs, sizeof(int) * alloc);
811 CHECK_NULL_RETURN_MEMERR(e->back_refs);
812 e->back_alloc = alloc;
814 e->back_refs[e->back_num - 1] = backref;
818 return 0;
821 extern int
822 onig_name_to_group_numbers(regex_t* reg, const UChar* name,
823 const UChar* name_end, int** nums)
825 NameEntry* e = name_find(reg, name, name_end);
827 if (IS_NULL(e)) return ONIGERR_UNDEFINED_NAME_REFERENCE;
829 switch (e->back_num) {
830 case 0:
831 *nums = 0;
832 break;
833 case 1:
834 *nums = &(e->back_ref1);
835 break;
836 default:
837 *nums = e->back_refs;
838 break;
840 return e->back_num;
843 extern int
844 onig_name_to_backref_number(regex_t* reg, const UChar* name,
845 const UChar* name_end, OnigRegion *region)
847 int i, n, *nums;
849 n = onig_name_to_group_numbers(reg, name, name_end, &nums);
850 if (n < 0)
851 return n;
852 else if (n == 0)
853 return ONIGERR_PARSER_BUG;
854 else if (n == 1)
855 return nums[0];
856 else {
857 if (IS_NOT_NULL(region)) {
858 for (i = n - 1; i >= 0; i--) {
859 if (region->beg[nums[i]] != ONIG_REGION_NOTPOS)
860 return nums[i];
863 return nums[n - 1];
867 #else /* USE_NAMED_GROUP */
869 extern int
870 onig_name_to_group_numbers(regex_t* reg, const UChar* name,
871 const UChar* name_end, int** nums)
873 return ONIG_NO_SUPPORT_CONFIG;
876 extern int
877 onig_name_to_backref_number(regex_t* reg, const UChar* name,
878 const UChar* name_end, OnigRegion* region)
880 return ONIG_NO_SUPPORT_CONFIG;
883 extern int
884 onig_foreach_name(regex_t* reg,
885 int (*func)(const UChar*, const UChar*,int,int*,regex_t*,void*), void* arg)
887 return ONIG_NO_SUPPORT_CONFIG;
890 extern int
891 onig_number_of_names(regex_t* reg)
893 return 0;
895 #endif /* else USE_NAMED_GROUP */
897 extern int
898 onig_noname_group_capture_is_active(regex_t* reg)
900 if (ONIG_IS_OPTION_ON(reg->options, ONIG_OPTION_DONT_CAPTURE_GROUP))
901 return 0;
903 #ifdef USE_NAMED_GROUP
904 if (onig_number_of_names(reg) > 0 &&
905 IS_SYNTAX_BV(reg->syntax, ONIG_SYN_CAPTURE_ONLY_NAMED_GROUP) &&
906 !ONIG_IS_OPTION_ON(reg->options, ONIG_OPTION_CAPTURE_GROUP)) {
907 return 0;
909 #endif
911 return 1;
915 #define INIT_SCANENV_MEMNODES_ALLOC_SIZE 16
917 static void
918 scan_env_clear(ScanEnv* env)
920 int i;
922 BIT_STATUS_CLEAR(env->capture_history);
923 BIT_STATUS_CLEAR(env->bt_mem_start);
924 BIT_STATUS_CLEAR(env->bt_mem_end);
925 BIT_STATUS_CLEAR(env->backrefed_mem);
926 env->error = (UChar* )NULL;
927 env->error_end = (UChar* )NULL;
928 env->num_call = 0;
929 env->num_mem = 0;
930 #ifdef USE_NAMED_GROUP
931 env->num_named = 0;
932 #endif
933 env->mem_alloc = 0;
934 env->mem_nodes_dynamic = (Node** )NULL;
936 for (i = 0; i < SCANENV_MEMNODES_SIZE; i++)
937 env->mem_nodes_static[i] = NULL_NODE;
939 #ifdef USE_COMBINATION_EXPLOSION_CHECK
940 env->num_comb_exp_check = 0;
941 env->comb_exp_max_regnum = 0;
942 env->curr_max_regnum = 0;
943 env->has_recursion = 0;
944 #endif
947 static int
948 scan_env_add_mem_entry(ScanEnv* env)
950 int i, need, alloc;
951 Node** p;
953 need = env->num_mem + 1;
954 if (need >= SCANENV_MEMNODES_SIZE) {
955 if (env->mem_alloc <= need) {
956 if (IS_NULL(env->mem_nodes_dynamic)) {
957 alloc = INIT_SCANENV_MEMNODES_ALLOC_SIZE;
958 p = (Node** )xmalloc(sizeof(Node*) * alloc);
959 xmemcpy(p, env->mem_nodes_static,
960 sizeof(Node*) * SCANENV_MEMNODES_SIZE);
962 else {
963 alloc = env->mem_alloc * 2;
964 p = (Node** )xrealloc(env->mem_nodes_dynamic, sizeof(Node*) * alloc);
966 CHECK_NULL_RETURN_MEMERR(p);
968 for (i = env->num_mem + 1; i < alloc; i++)
969 p[i] = NULL_NODE;
971 env->mem_nodes_dynamic = p;
972 env->mem_alloc = alloc;
976 env->num_mem++;
977 return env->num_mem;
980 static int
981 scan_env_set_mem_node(ScanEnv* env, int num, Node* node)
983 if (env->num_mem >= num)
984 SCANENV_MEM_NODES(env)[num] = node;
985 else
986 return ONIGERR_PARSER_BUG;
987 return 0;
991 #ifdef USE_PARSE_TREE_NODE_RECYCLE
992 typedef struct _FreeNode {
993 struct _FreeNode* next;
994 } FreeNode;
996 static FreeNode* FreeNodeList = (FreeNode* )NULL;
997 #endif
999 extern void
1000 onig_node_free(Node* node)
1002 start:
1003 if (IS_NULL(node)) return ;
1005 switch (NTYPE(node)) {
1006 case NT_STR:
1007 if (NSTR(node)->capa != 0 &&
1008 IS_NOT_NULL(NSTR(node)->s) && NSTR(node)->s != NSTR(node)->buf) {
1009 xfree(NSTR(node)->s);
1011 break;
1013 case NT_LIST:
1014 case NT_ALT:
1015 onig_node_free(NCAR(node));
1017 Node* next_node = NCDR(node);
1019 #ifdef USE_PARSE_TREE_NODE_RECYCLE
1021 FreeNode* n = (FreeNode* )node;
1023 THREAD_ATOMIC_START;
1024 n->next = FreeNodeList;
1025 FreeNodeList = n;
1026 THREAD_ATOMIC_END;
1028 #else
1029 xfree(node);
1030 #endif
1031 node = next_node;
1032 goto start;
1034 break;
1036 case NT_CCLASS:
1038 CClassNode* cc = NCCLASS(node);
1040 if (IS_NCCLASS_SHARE(cc)) return ;
1041 if (cc->mbuf)
1042 bbuf_free(cc->mbuf);
1044 break;
1046 case NT_QTFR:
1047 if (NQTFR(node)->target)
1048 onig_node_free(NQTFR(node)->target);
1049 break;
1051 case NT_ENCLOSE:
1052 if (NENCLOSE(node)->target)
1053 onig_node_free(NENCLOSE(node)->target);
1054 break;
1056 case NT_BREF:
1057 if (IS_NOT_NULL(NBREF(node)->back_dynamic))
1058 xfree(NBREF(node)->back_dynamic);
1059 break;
1061 case NT_ANCHOR:
1062 if (NANCHOR(node)->target)
1063 onig_node_free(NANCHOR(node)->target);
1064 break;
1067 #ifdef USE_PARSE_TREE_NODE_RECYCLE
1069 FreeNode* n = (FreeNode* )node;
1071 THREAD_ATOMIC_START;
1072 n->next = FreeNodeList;
1073 FreeNodeList = n;
1074 THREAD_ATOMIC_END;
1076 #else
1077 xfree(node);
1078 #endif
1081 #ifdef USE_PARSE_TREE_NODE_RECYCLE
1082 extern int
1083 onig_free_node_list(void)
1085 FreeNode* n;
1087 /* THREAD_ATOMIC_START; */
1088 while (IS_NOT_NULL(FreeNodeList)) {
1089 n = FreeNodeList;
1090 FreeNodeList = FreeNodeList->next;
1091 xfree(n);
1093 /* THREAD_ATOMIC_END; */
1094 return 0;
1096 #endif
1098 static Node*
1099 node_new(void)
1101 Node* node;
1103 #ifdef USE_PARSE_TREE_NODE_RECYCLE
1104 THREAD_ATOMIC_START;
1105 if (IS_NOT_NULL(FreeNodeList)) {
1106 node = (Node* )FreeNodeList;
1107 FreeNodeList = FreeNodeList->next;
1108 THREAD_ATOMIC_END;
1109 return node;
1111 THREAD_ATOMIC_END;
1112 #endif
1114 node = (Node* )xmalloc(sizeof(Node));
1115 /* xmemset(node, 0, sizeof(Node)); */
1116 return node;
1120 static void
1121 initialize_cclass(CClassNode* cc)
1123 BITSET_CLEAR(cc->bs);
1124 /* cc->base.flags = 0; */
1125 cc->flags = 0;
1126 cc->mbuf = NULL;
1129 static Node*
1130 node_new_cclass(void)
1132 Node* node = node_new();
1133 CHECK_NULL_RETURN(node);
1135 SET_NTYPE(node, NT_CCLASS);
1136 initialize_cclass(NCCLASS(node));
1137 return node;
1140 static Node*
1141 node_new_cclass_by_codepoint_range(int not, OnigCodePoint sb_out,
1142 const OnigCodePoint ranges[])
1144 int n, i;
1145 CClassNode* cc;
1146 OnigCodePoint j;
1148 Node* node = node_new_cclass();
1149 CHECK_NULL_RETURN(node);
1151 cc = NCCLASS(node);
1152 if (not != 0) NCCLASS_SET_NOT(cc);
1154 BITSET_CLEAR(cc->bs);
1155 if (sb_out > 0 && IS_NOT_NULL(ranges)) {
1156 n = ONIGENC_CODE_RANGE_NUM(ranges);
1157 for (i = 0; i < n; i++) {
1158 for (j = ONIGENC_CODE_RANGE_FROM(ranges, i);
1159 j <= (OnigCodePoint )ONIGENC_CODE_RANGE_TO(ranges, i); j++) {
1160 if (j >= sb_out) goto sb_end;
1162 BITSET_SET_BIT(cc->bs, j);
1167 sb_end:
1168 if (IS_NULL(ranges)) {
1169 is_null:
1170 cc->mbuf = NULL;
1172 else {
1173 BBuf* bbuf;
1175 n = ONIGENC_CODE_RANGE_NUM(ranges);
1176 if (n == 0) goto is_null;
1178 bbuf = (BBuf* )xmalloc(sizeof(BBuf));
1179 CHECK_NULL_RETURN(bbuf);
1180 bbuf->alloc = n + 1;
1181 bbuf->used = n + 1;
1182 bbuf->p = (UChar* )((void* )ranges);
1184 cc->mbuf = bbuf;
1187 return node;
1190 static Node*
1191 node_new_ctype(int type, int not)
1193 Node* node = node_new();
1194 CHECK_NULL_RETURN(node);
1196 SET_NTYPE(node, NT_CTYPE);
1197 NCTYPE(node)->ctype = type;
1198 NCTYPE(node)->not = not;
1199 return node;
1202 static Node*
1203 node_new_anychar(void)
1205 Node* node = node_new();
1206 CHECK_NULL_RETURN(node);
1208 SET_NTYPE(node, NT_CANY);
1209 return node;
1212 static Node*
1213 node_new_list(Node* left, Node* right)
1215 Node* node = node_new();
1216 CHECK_NULL_RETURN(node);
1218 SET_NTYPE(node, NT_LIST);
1219 NCAR(node) = left;
1220 NCDR(node) = right;
1221 return node;
1224 extern Node*
1225 onig_node_new_list(Node* left, Node* right)
1227 return node_new_list(left, right);
1230 extern Node*
1231 onig_node_list_add(Node* list, Node* x)
1233 Node *n;
1235 n = onig_node_new_list(x, NULL);
1236 if (IS_NULL(n)) return NULL_NODE;
1238 if (IS_NOT_NULL(list)) {
1239 while (IS_NOT_NULL(NCDR(list)))
1240 list = NCDR(list);
1242 NCDR(list) = n;
1245 return n;
1248 extern Node*
1249 onig_node_new_alt(Node* left, Node* right)
1251 Node* node = node_new();
1252 CHECK_NULL_RETURN(node);
1254 SET_NTYPE(node, NT_ALT);
1255 NCAR(node) = left;
1256 NCDR(node) = right;
1257 return node;
1260 extern Node*
1261 onig_node_new_anchor(int type)
1263 Node* node = node_new();
1264 CHECK_NULL_RETURN(node);
1266 SET_NTYPE(node, NT_ANCHOR);
1267 NANCHOR(node)->type = type;
1268 NANCHOR(node)->target = NULL;
1269 NANCHOR(node)->char_len = -1;
1270 return node;
1273 static Node*
1274 node_new_backref(int back_num, int* backrefs, int by_name,
1275 #ifdef USE_BACKREF_WITH_LEVEL
1276 int exist_level, int nest_level,
1277 #endif
1278 ScanEnv* env)
1280 int i;
1281 Node* node = node_new();
1283 CHECK_NULL_RETURN(node);
1285 SET_NTYPE(node, NT_BREF);
1286 NBREF(node)->state = 0;
1287 NBREF(node)->back_num = back_num;
1288 NBREF(node)->back_dynamic = (int* )NULL;
1289 if (by_name != 0)
1290 NBREF(node)->state |= NST_NAME_REF;
1292 #ifdef USE_BACKREF_WITH_LEVEL
1293 if (exist_level != 0) {
1294 NBREF(node)->state |= NST_NEST_LEVEL;
1295 NBREF(node)->nest_level = nest_level;
1297 #endif
1299 for (i = 0; i < back_num; i++) {
1300 if (backrefs[i] <= env->num_mem &&
1301 IS_NULL(SCANENV_MEM_NODES(env)[backrefs[i]])) {
1302 NBREF(node)->state |= NST_RECURSION; /* /...(\1).../ */
1303 break;
1307 if (back_num <= NODE_BACKREFS_SIZE) {
1308 for (i = 0; i < back_num; i++)
1309 NBREF(node)->back_static[i] = backrefs[i];
1311 else {
1312 int* p = (int* )xmalloc(sizeof(int) * back_num);
1313 if (IS_NULL(p)) {
1314 onig_node_free(node);
1315 return NULL;
1317 NBREF(node)->back_dynamic = p;
1318 for (i = 0; i < back_num; i++)
1319 p[i] = backrefs[i];
1321 return node;
1324 #ifdef USE_SUBEXP_CALL
1325 static Node*
1326 node_new_call(UChar* name, UChar* name_end, int gnum)
1328 Node* node = node_new();
1329 CHECK_NULL_RETURN(node);
1331 SET_NTYPE(node, NT_CALL);
1332 NCALL(node)->state = 0;
1333 NCALL(node)->target = NULL_NODE;
1334 NCALL(node)->name = name;
1335 NCALL(node)->name_end = name_end;
1336 NCALL(node)->group_num = gnum; /* call by number if gnum != 0 */
1337 return node;
1339 #endif
1341 static Node*
1342 node_new_quantifier(int lower, int upper, int by_number)
1344 Node* node = node_new();
1345 CHECK_NULL_RETURN(node);
1347 SET_NTYPE(node, NT_QTFR);
1348 NQTFR(node)->state = 0;
1349 NQTFR(node)->target = NULL;
1350 NQTFR(node)->lower = lower;
1351 NQTFR(node)->upper = upper;
1352 NQTFR(node)->greedy = 1;
1353 NQTFR(node)->target_empty_info = NQ_TARGET_ISNOT_EMPTY;
1354 NQTFR(node)->head_exact = NULL_NODE;
1355 NQTFR(node)->next_head_exact = NULL_NODE;
1356 NQTFR(node)->is_refered = 0;
1357 if (by_number != 0)
1358 NQTFR(node)->state |= NST_BY_NUMBER;
1360 #ifdef USE_COMBINATION_EXPLOSION_CHECK
1361 NQTFR(node)->comb_exp_check_num = 0;
1362 #endif
1364 return node;
1367 static Node*
1368 node_new_enclose(int type)
1370 Node* node = node_new();
1371 CHECK_NULL_RETURN(node);
1373 SET_NTYPE(node, NT_ENCLOSE);
1374 NENCLOSE(node)->type = type;
1375 NENCLOSE(node)->state = 0;
1376 NENCLOSE(node)->regnum = 0;
1377 NENCLOSE(node)->option = 0;
1378 NENCLOSE(node)->target = NULL;
1379 NENCLOSE(node)->call_addr = -1;
1380 NENCLOSE(node)->opt_count = 0;
1381 return node;
1384 extern Node*
1385 onig_node_new_enclose(int type)
1387 return node_new_enclose(type);
1390 static Node*
1391 node_new_enclose_memory(OnigOptionType option, int is_named)
1393 Node* node = node_new_enclose(ENCLOSE_MEMORY);
1394 CHECK_NULL_RETURN(node);
1395 if (is_named != 0)
1396 SET_ENCLOSE_STATUS(node, NST_NAMED_GROUP);
1398 #ifdef USE_SUBEXP_CALL
1399 NENCLOSE(node)->option = option;
1400 #endif
1401 return node;
1404 static Node*
1405 node_new_option(OnigOptionType option)
1407 Node* node = node_new_enclose(ENCLOSE_OPTION);
1408 CHECK_NULL_RETURN(node);
1409 NENCLOSE(node)->option = option;
1410 return node;
1413 extern int
1414 onig_node_str_cat(Node* node, const UChar* s, const UChar* end)
1416 int addlen = end - s;
1418 if (addlen > 0) {
1419 int len = NSTR(node)->end - NSTR(node)->s;
1421 if (NSTR(node)->capa > 0 || (len + addlen > NODE_STR_BUF_SIZE - 1)) {
1422 UChar* p;
1423 int capa = len + addlen + NODE_STR_MARGIN;
1425 if (capa <= NSTR(node)->capa) {
1426 onig_strcpy(NSTR(node)->s + len, s, end);
1428 else {
1429 if (NSTR(node)->s == NSTR(node)->buf)
1430 p = strcat_capa_from_static(NSTR(node)->s, NSTR(node)->end,
1431 s, end, capa);
1432 else
1433 p = strcat_capa(NSTR(node)->s, NSTR(node)->end, s, end, capa);
1435 CHECK_NULL_RETURN_MEMERR(p);
1436 NSTR(node)->s = p;
1437 NSTR(node)->capa = capa;
1440 else {
1441 onig_strcpy(NSTR(node)->s + len, s, end);
1443 NSTR(node)->end = NSTR(node)->s + len + addlen;
1446 return 0;
1449 extern int
1450 onig_node_str_set(Node* node, const UChar* s, const UChar* end)
1452 onig_node_str_clear(node);
1453 return onig_node_str_cat(node, s, end);
1456 static int
1457 node_str_cat_char(Node* node, UChar c)
1459 UChar s[1];
1461 s[0] = c;
1462 return onig_node_str_cat(node, s, s + 1);
1465 extern void
1466 onig_node_conv_to_str_node(Node* node, int flag)
1468 SET_NTYPE(node, NT_STR);
1469 NSTR(node)->flag = flag;
1470 NSTR(node)->capa = 0;
1471 NSTR(node)->s = NSTR(node)->buf;
1472 NSTR(node)->end = NSTR(node)->buf;
1475 extern void
1476 onig_node_str_clear(Node* node)
1478 if (NSTR(node)->capa != 0 &&
1479 IS_NOT_NULL(NSTR(node)->s) && NSTR(node)->s != NSTR(node)->buf) {
1480 xfree(NSTR(node)->s);
1483 NSTR(node)->capa = 0;
1484 NSTR(node)->flag = 0;
1485 NSTR(node)->s = NSTR(node)->buf;
1486 NSTR(node)->end = NSTR(node)->buf;
1489 static Node*
1490 node_new_str(const UChar* s, const UChar* end)
1492 Node* node = node_new();
1493 CHECK_NULL_RETURN(node);
1495 SET_NTYPE(node, NT_STR);
1496 NSTR(node)->capa = 0;
1497 NSTR(node)->flag = 0;
1498 NSTR(node)->s = NSTR(node)->buf;
1499 NSTR(node)->end = NSTR(node)->buf;
1500 if (onig_node_str_cat(node, s, end)) {
1501 onig_node_free(node);
1502 return NULL;
1504 return node;
1507 extern Node*
1508 onig_node_new_str(const UChar* s, const UChar* end)
1510 return node_new_str(s, end);
1513 static Node*
1514 node_new_str_raw(UChar* s, UChar* end)
1516 Node* node = node_new_str(s, end);
1517 NSTRING_SET_RAW(node);
1518 return node;
1521 static Node*
1522 node_new_empty(void)
1524 return node_new_str(NULL, NULL);
1527 static Node*
1528 node_new_str_raw_char(UChar c)
1530 UChar p[1];
1532 p[0] = c;
1533 return node_new_str_raw(p, p + 1);
1536 static Node*
1537 str_node_split_last_char(StrNode* sn, OnigEncoding enc)
1539 const UChar *p;
1540 Node* n = NULL_NODE;
1542 if (sn->end > sn->s) {
1543 p = onigenc_get_prev_char_head(enc, sn->s, sn->end);
1544 if (p && p > sn->s) { /* can be splitted. */
1545 n = node_new_str(p, sn->end);
1546 if ((sn->flag & NSTR_RAW) != 0)
1547 NSTRING_SET_RAW(n);
1548 sn->end = (UChar* )p;
1551 return n;
1554 static int
1555 str_node_can_be_split(StrNode* sn, OnigEncoding enc)
1557 if (sn->end > sn->s) {
1558 return ((enclen(enc, sn->s, sn->end) < sn->end - sn->s) ? 1 : 0);
1560 return 0;
1563 #ifdef USE_PAD_TO_SHORT_BYTE_CHAR
1564 static int
1565 node_str_head_pad(StrNode* sn, int num, UChar val)
1567 UChar buf[NODE_STR_BUF_SIZE];
1568 int i, len;
1570 len = sn->end - sn->s;
1571 onig_strcpy(buf, sn->s, sn->end);
1572 onig_strcpy(&(sn->s[num]), buf, buf + len);
1573 sn->end += num;
1575 for (i = 0; i < num; i++) {
1576 sn->s[i] = val;
1579 #endif
1581 extern int
1582 onig_scan_unsigned_number(UChar** src, const UChar* end, OnigEncoding enc)
1584 unsigned int num, val;
1585 OnigCodePoint c;
1586 UChar* p = *src;
1587 PFETCH_READY;
1589 num = 0;
1590 while (!PEND) {
1591 PFETCH(c);
1592 if (ONIGENC_IS_CODE_DIGIT(enc, c)) {
1593 val = (unsigned int )DIGITVAL(c);
1594 if ((INT_MAX_LIMIT - val) / 10UL < num)
1595 return -1; /* overflow */
1597 num = num * 10 + val;
1599 else {
1600 PUNFETCH;
1601 break;
1604 *src = p;
1605 return num;
1608 static int
1609 scan_unsigned_hexadecimal_number(UChar** src, UChar* end, int maxlen,
1610 OnigEncoding enc)
1612 OnigCodePoint c;
1613 unsigned int num, val;
1614 UChar* p = *src;
1615 PFETCH_READY;
1617 num = 0;
1618 while (!PEND && maxlen-- != 0) {
1619 PFETCH(c);
1620 if (ONIGENC_IS_CODE_XDIGIT(enc, c)) {
1621 val = (unsigned int )XDIGITVAL(enc,c);
1622 if ((INT_MAX_LIMIT - val) / 16UL < num)
1623 return -1; /* overflow */
1625 num = (num << 4) + XDIGITVAL(enc,c);
1627 else {
1628 PUNFETCH;
1629 break;
1632 *src = p;
1633 return num;
1636 static int
1637 scan_unsigned_octal_number(UChar** src, UChar* end, int maxlen,
1638 OnigEncoding enc)
1640 OnigCodePoint c;
1641 unsigned int num, val;
1642 UChar* p = *src;
1643 PFETCH_READY;
1645 num = 0;
1646 while (!PEND && maxlen-- != 0) {
1647 PFETCH(c);
1648 if (ONIGENC_IS_CODE_DIGIT(enc, c) && c < '8') {
1649 val = ODIGITVAL(c);
1650 if ((INT_MAX_LIMIT - val) / 8UL < num)
1651 return -1; /* overflow */
1653 num = (num << 3) + val;
1655 else {
1656 PUNFETCH;
1657 break;
1660 *src = p;
1661 return num;
1665 #define BBUF_WRITE_CODE_POINT(bbuf,pos,code) \
1666 BBUF_WRITE(bbuf, pos, &(code), SIZE_CODE_POINT)
1668 /* data format:
1669 [n][from-1][to-1][from-2][to-2] ... [from-n][to-n]
1670 (all data size is OnigCodePoint)
1672 static int
1673 new_code_range(BBuf** pbuf)
1675 #define INIT_MULTI_BYTE_RANGE_SIZE (SIZE_CODE_POINT * 5)
1676 int r;
1677 OnigCodePoint n;
1678 BBuf* bbuf;
1680 bbuf = *pbuf = (BBuf* )xmalloc(sizeof(BBuf));
1681 CHECK_NULL_RETURN_MEMERR(*pbuf);
1682 r = BBUF_INIT(*pbuf, INIT_MULTI_BYTE_RANGE_SIZE);
1683 if (r) return r;
1685 n = 0;
1686 BBUF_WRITE_CODE_POINT(bbuf, 0, n);
1687 return 0;
1690 static int
1691 add_code_range_to_buf(BBuf** pbuf, OnigCodePoint from, OnigCodePoint to)
1693 int r, inc_n, pos;
1694 int low, high, bound, x;
1695 OnigCodePoint n, *data;
1696 BBuf* bbuf;
1698 if (from > to) {
1699 n = from; from = to; to = n;
1702 if (IS_NULL(*pbuf)) {
1703 r = new_code_range(pbuf);
1704 if (r) return r;
1705 bbuf = *pbuf;
1706 n = 0;
1708 else {
1709 bbuf = *pbuf;
1710 GET_CODE_POINT(n, bbuf->p);
1712 data = (OnigCodePoint* )(bbuf->p);
1713 data++;
1715 for (low = 0, bound = n; low < bound; ) {
1716 x = (low + bound) >> 1;
1717 if (from > data[x*2 + 1])
1718 low = x + 1;
1719 else
1720 bound = x;
1723 for (high = low, bound = n; high < bound; ) {
1724 x = (high + bound) >> 1;
1725 if (to >= data[x*2] - 1)
1726 high = x + 1;
1727 else
1728 bound = x;
1731 inc_n = low + 1 - high;
1732 if (n + inc_n > ONIG_MAX_MULTI_BYTE_RANGES_NUM)
1733 return ONIGERR_TOO_MANY_MULTI_BYTE_RANGES;
1735 if (inc_n != 1) {
1736 if (from > data[low*2])
1737 from = data[low*2];
1738 if (to < data[(high - 1)*2 + 1])
1739 to = data[(high - 1)*2 + 1];
1742 if (inc_n != 0 && (OnigCodePoint )high < n) {
1743 int from_pos = SIZE_CODE_POINT * (1 + high * 2);
1744 int to_pos = SIZE_CODE_POINT * (1 + (low + 1) * 2);
1745 int size = (n - high) * 2 * SIZE_CODE_POINT;
1747 if (inc_n > 0) {
1748 BBUF_MOVE_RIGHT(bbuf, from_pos, to_pos, size);
1750 else {
1751 BBUF_MOVE_LEFT_REDUCE(bbuf, from_pos, to_pos);
1755 pos = SIZE_CODE_POINT * (1 + low * 2);
1756 BBUF_ENSURE_SIZE(bbuf, pos + SIZE_CODE_POINT * 2);
1757 BBUF_WRITE_CODE_POINT(bbuf, pos, from);
1758 BBUF_WRITE_CODE_POINT(bbuf, pos + SIZE_CODE_POINT, to);
1759 n += inc_n;
1760 BBUF_WRITE_CODE_POINT(bbuf, 0, n);
1762 return 0;
1765 static int
1766 add_code_range(BBuf** pbuf, ScanEnv* env, OnigCodePoint from, OnigCodePoint to)
1768 if (from > to) {
1769 if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_ALLOW_EMPTY_RANGE_IN_CC))
1770 return 0;
1771 else
1772 return ONIGERR_EMPTY_RANGE_IN_CHAR_CLASS;
1775 return add_code_range_to_buf(pbuf, from, to);
1778 static int
1779 not_code_range_buf(OnigEncoding enc, BBuf* bbuf, BBuf** pbuf)
1781 int r, i, n;
1782 OnigCodePoint pre, from, *data, to = 0;
1784 *pbuf = (BBuf* )NULL;
1785 if (IS_NULL(bbuf)) {
1786 set_all:
1787 return SET_ALL_MULTI_BYTE_RANGE(enc, pbuf);
1790 data = (OnigCodePoint* )(bbuf->p);
1791 GET_CODE_POINT(n, data);
1792 data++;
1793 if (n <= 0) goto set_all;
1795 r = 0;
1796 pre = MBCODE_START_POS(enc);
1797 for (i = 0; i < n; i++) {
1798 from = data[i*2];
1799 to = data[i*2+1];
1800 if (pre <= from - 1) {
1801 r = add_code_range_to_buf(pbuf, pre, from - 1);
1802 if (r != 0) return r;
1804 if (to == ~((OnigCodePoint )0)) break;
1805 pre = to + 1;
1807 if (to < ~((OnigCodePoint )0)) {
1808 r = add_code_range_to_buf(pbuf, to + 1, ~((OnigCodePoint )0));
1810 return r;
1813 #define SWAP_BBUF_NOT(bbuf1, not1, bbuf2, not2) do {\
1814 BBuf *tbuf; \
1815 int tnot; \
1816 tnot = not1; not1 = not2; not2 = tnot; \
1817 tbuf = bbuf1; bbuf1 = bbuf2; bbuf2 = tbuf; \
1818 } while (0)
1820 static int
1821 or_code_range_buf(OnigEncoding enc, BBuf* bbuf1, int not1,
1822 BBuf* bbuf2, int not2, BBuf** pbuf)
1824 int r;
1825 OnigCodePoint i, n1, *data1;
1826 OnigCodePoint from, to;
1828 *pbuf = (BBuf* )NULL;
1829 if (IS_NULL(bbuf1) && IS_NULL(bbuf2)) {
1830 if (not1 != 0 || not2 != 0)
1831 return SET_ALL_MULTI_BYTE_RANGE(enc, pbuf);
1832 return 0;
1835 r = 0;
1836 if (IS_NULL(bbuf2))
1837 SWAP_BBUF_NOT(bbuf1, not1, bbuf2, not2);
1839 if (IS_NULL(bbuf1)) {
1840 if (not1 != 0) {
1841 return SET_ALL_MULTI_BYTE_RANGE(enc, pbuf);
1843 else {
1844 if (not2 == 0) {
1845 return bbuf_clone(pbuf, bbuf2);
1847 else {
1848 return not_code_range_buf(enc, bbuf2, pbuf);
1853 if (not1 != 0)
1854 SWAP_BBUF_NOT(bbuf1, not1, bbuf2, not2);
1856 data1 = (OnigCodePoint* )(bbuf1->p);
1857 GET_CODE_POINT(n1, data1);
1858 data1++;
1860 if (not2 == 0 && not1 == 0) { /* 1 OR 2 */
1861 r = bbuf_clone(pbuf, bbuf2);
1863 else if (not1 == 0) { /* 1 OR (not 2) */
1864 r = not_code_range_buf(enc, bbuf2, pbuf);
1866 if (r != 0) return r;
1868 for (i = 0; i < n1; i++) {
1869 from = data1[i*2];
1870 to = data1[i*2+1];
1871 r = add_code_range_to_buf(pbuf, from, to);
1872 if (r != 0) return r;
1874 return 0;
1877 static int
1878 and_code_range1(BBuf** pbuf, OnigCodePoint from1, OnigCodePoint to1,
1879 OnigCodePoint* data, int n)
1881 int i, r;
1882 OnigCodePoint from2, to2;
1884 for (i = 0; i < n; i++) {
1885 from2 = data[i*2];
1886 to2 = data[i*2+1];
1887 if (from2 < from1) {
1888 if (to2 < from1) continue;
1889 else {
1890 from1 = to2 + 1;
1893 else if (from2 <= to1) {
1894 if (to2 < to1) {
1895 if (from1 <= from2 - 1) {
1896 r = add_code_range_to_buf(pbuf, from1, from2-1);
1897 if (r != 0) return r;
1899 from1 = to2 + 1;
1901 else {
1902 to1 = from2 - 1;
1905 else {
1906 from1 = from2;
1908 if (from1 > to1) break;
1910 if (from1 <= to1) {
1911 r = add_code_range_to_buf(pbuf, from1, to1);
1912 if (r != 0) return r;
1914 return 0;
1917 static int
1918 and_code_range_buf(BBuf* bbuf1, int not1, BBuf* bbuf2, int not2, BBuf** pbuf)
1920 int r;
1921 OnigCodePoint i, j, n1, n2, *data1, *data2;
1922 OnigCodePoint from, to, from1, to1, from2, to2;
1924 *pbuf = (BBuf* )NULL;
1925 if (IS_NULL(bbuf1)) {
1926 if (not1 != 0 && IS_NOT_NULL(bbuf2)) /* not1 != 0 -> not2 == 0 */
1927 return bbuf_clone(pbuf, bbuf2);
1928 return 0;
1930 else if (IS_NULL(bbuf2)) {
1931 if (not2 != 0)
1932 return bbuf_clone(pbuf, bbuf1);
1933 return 0;
1936 if (not1 != 0)
1937 SWAP_BBUF_NOT(bbuf1, not1, bbuf2, not2);
1939 data1 = (OnigCodePoint* )(bbuf1->p);
1940 data2 = (OnigCodePoint* )(bbuf2->p);
1941 GET_CODE_POINT(n1, data1);
1942 GET_CODE_POINT(n2, data2);
1943 data1++;
1944 data2++;
1946 if (not2 == 0 && not1 == 0) { /* 1 AND 2 */
1947 for (i = 0; i < n1; i++) {
1948 from1 = data1[i*2];
1949 to1 = data1[i*2+1];
1950 for (j = 0; j < n2; j++) {
1951 from2 = data2[j*2];
1952 to2 = data2[j*2+1];
1953 if (from2 > to1) break;
1954 if (to2 < from1) continue;
1955 from = MAX(from1, from2);
1956 to = MIN(to1, to2);
1957 r = add_code_range_to_buf(pbuf, from, to);
1958 if (r != 0) return r;
1962 else if (not1 == 0) { /* 1 AND (not 2) */
1963 for (i = 0; i < n1; i++) {
1964 from1 = data1[i*2];
1965 to1 = data1[i*2+1];
1966 r = and_code_range1(pbuf, from1, to1, data2, n2);
1967 if (r != 0) return r;
1971 return 0;
1974 static int
1975 and_cclass(CClassNode* dest, CClassNode* cc, OnigEncoding enc)
1977 int r, not1, not2;
1978 BBuf *buf1, *buf2, *pbuf;
1979 BitSetRef bsr1, bsr2;
1980 BitSet bs1, bs2;
1982 not1 = IS_NCCLASS_NOT(dest);
1983 bsr1 = dest->bs;
1984 buf1 = dest->mbuf;
1985 not2 = IS_NCCLASS_NOT(cc);
1986 bsr2 = cc->bs;
1987 buf2 = cc->mbuf;
1989 if (not1 != 0) {
1990 bitset_invert_to(bsr1, bs1);
1991 bsr1 = bs1;
1993 if (not2 != 0) {
1994 bitset_invert_to(bsr2, bs2);
1995 bsr2 = bs2;
1997 bitset_and(bsr1, bsr2);
1998 if (bsr1 != dest->bs) {
1999 bitset_copy(dest->bs, bsr1);
2000 bsr1 = dest->bs;
2002 if (not1 != 0) {
2003 bitset_invert(dest->bs);
2006 if (! ONIGENC_IS_SINGLEBYTE(enc)) {
2007 if (not1 != 0 && not2 != 0) {
2008 r = or_code_range_buf(enc, buf1, 0, buf2, 0, &pbuf);
2010 else {
2011 r = and_code_range_buf(buf1, not1, buf2, not2, &pbuf);
2012 if (r == 0 && not1 != 0) {
2013 BBuf *tbuf;
2014 r = not_code_range_buf(enc, pbuf, &tbuf);
2015 if (r != 0) {
2016 bbuf_free(pbuf);
2017 return r;
2019 bbuf_free(pbuf);
2020 pbuf = tbuf;
2023 if (r != 0) return r;
2025 dest->mbuf = pbuf;
2026 bbuf_free(buf1);
2027 return r;
2029 return 0;
2032 static int
2033 or_cclass(CClassNode* dest, CClassNode* cc, OnigEncoding enc)
2035 int r, not1, not2;
2036 BBuf *buf1, *buf2, *pbuf;
2037 BitSetRef bsr1, bsr2;
2038 BitSet bs1, bs2;
2040 not1 = IS_NCCLASS_NOT(dest);
2041 bsr1 = dest->bs;
2042 buf1 = dest->mbuf;
2043 not2 = IS_NCCLASS_NOT(cc);
2044 bsr2 = cc->bs;
2045 buf2 = cc->mbuf;
2047 if (not1 != 0) {
2048 bitset_invert_to(bsr1, bs1);
2049 bsr1 = bs1;
2051 if (not2 != 0) {
2052 bitset_invert_to(bsr2, bs2);
2053 bsr2 = bs2;
2055 bitset_or(bsr1, bsr2);
2056 if (bsr1 != dest->bs) {
2057 bitset_copy(dest->bs, bsr1);
2058 bsr1 = dest->bs;
2060 if (not1 != 0) {
2061 bitset_invert(dest->bs);
2064 if (! ONIGENC_IS_SINGLEBYTE(enc)) {
2065 if (not1 != 0 && not2 != 0) {
2066 r = and_code_range_buf(buf1, 0, buf2, 0, &pbuf);
2068 else {
2069 r = or_code_range_buf(enc, buf1, not1, buf2, not2, &pbuf);
2070 if (r == 0 && not1 != 0) {
2071 BBuf *tbuf;
2072 r = not_code_range_buf(enc, pbuf, &tbuf);
2073 if (r != 0) {
2074 bbuf_free(pbuf);
2075 return r;
2077 bbuf_free(pbuf);
2078 pbuf = tbuf;
2081 if (r != 0) return r;
2083 dest->mbuf = pbuf;
2084 bbuf_free(buf1);
2085 return r;
2087 else
2088 return 0;
2091 static int
2092 conv_backslash_value(int c, ScanEnv* env)
2094 if (IS_SYNTAX_OP(env->syntax, ONIG_SYN_OP_ESC_CONTROL_CHARS)) {
2095 switch (c) {
2096 case 'n': return '\n';
2097 case 't': return '\t';
2098 case 'r': return '\r';
2099 case 'f': return '\f';
2100 case 'a': return '\007';
2101 case 'b': return '\010';
2102 case 'e': return '\033';
2103 case 'v':
2104 if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_ESC_V_VTAB))
2105 return '\v';
2106 break;
2108 default:
2109 break;
2112 return c;
2115 static int
2116 is_invalid_quantifier_target(Node* node)
2118 switch (NTYPE(node)) {
2119 case NT_ANCHOR:
2120 return 1;
2121 break;
2123 case NT_ENCLOSE:
2124 /* allow enclosed elements */
2125 /* return is_invalid_quantifier_target(NENCLOSE(node)->target); */
2126 break;
2128 case NT_LIST:
2129 do {
2130 if (! is_invalid_quantifier_target(NCAR(node))) return 0;
2131 } while (IS_NOT_NULL(node = NCDR(node)));
2132 return 0;
2133 break;
2135 case NT_ALT:
2136 do {
2137 if (is_invalid_quantifier_target(NCAR(node))) return 1;
2138 } while (IS_NOT_NULL(node = NCDR(node)));
2139 break;
2141 default:
2142 break;
2144 return 0;
2147 /* ?:0, *:1, +:2, ??:3, *?:4, +?:5 */
2148 static int
2149 popular_quantifier_num(QtfrNode* q)
2151 if (q->greedy) {
2152 if (q->lower == 0) {
2153 if (q->upper == 1) return 0;
2154 else if (IS_REPEAT_INFINITE(q->upper)) return 1;
2156 else if (q->lower == 1) {
2157 if (IS_REPEAT_INFINITE(q->upper)) return 2;
2160 else {
2161 if (q->lower == 0) {
2162 if (q->upper == 1) return 3;
2163 else if (IS_REPEAT_INFINITE(q->upper)) return 4;
2165 else if (q->lower == 1) {
2166 if (IS_REPEAT_INFINITE(q->upper)) return 5;
2169 return -1;
2173 enum ReduceType {
2174 RQ_ASIS = 0, /* as is */
2175 RQ_DEL = 1, /* delete parent */
2176 RQ_A, /* to '*' */
2177 RQ_AQ, /* to '*?' */
2178 RQ_QQ, /* to '??' */
2179 RQ_P_QQ, /* to '+)??' */
2180 RQ_PQ_Q /* to '+?)?' */
2183 static enum ReduceType const ReduceTypeTable[6][6] = {
2184 {RQ_DEL, RQ_A, RQ_A, RQ_QQ, RQ_AQ, RQ_ASIS}, /* '?' */
2185 {RQ_DEL, RQ_DEL, RQ_DEL, RQ_P_QQ, RQ_P_QQ, RQ_DEL}, /* '*' */
2186 {RQ_A, RQ_A, RQ_DEL, RQ_ASIS, RQ_P_QQ, RQ_DEL}, /* '+' */
2187 {RQ_DEL, RQ_AQ, RQ_AQ, RQ_DEL, RQ_AQ, RQ_AQ}, /* '??' */
2188 {RQ_DEL, RQ_DEL, RQ_DEL, RQ_DEL, RQ_DEL, RQ_DEL}, /* '*?' */
2189 {RQ_ASIS, RQ_PQ_Q, RQ_DEL, RQ_AQ, RQ_AQ, RQ_DEL} /* '+?' */
2192 extern void
2193 onig_reduce_nested_quantifier(Node* pnode, Node* cnode)
2195 int pnum, cnum;
2196 QtfrNode *p, *c;
2198 p = NQTFR(pnode);
2199 c = NQTFR(cnode);
2200 pnum = popular_quantifier_num(p);
2201 cnum = popular_quantifier_num(c);
2202 if (pnum < 0 || cnum < 0) return ;
2204 switch(ReduceTypeTable[cnum][pnum]) {
2205 case RQ_DEL:
2206 *pnode = *cnode;
2207 break;
2208 case RQ_A:
2209 p->target = c->target;
2210 p->lower = 0; p->upper = REPEAT_INFINITE; p->greedy = 1;
2211 break;
2212 case RQ_AQ:
2213 p->target = c->target;
2214 p->lower = 0; p->upper = REPEAT_INFINITE; p->greedy = 0;
2215 break;
2216 case RQ_QQ:
2217 p->target = c->target;
2218 p->lower = 0; p->upper = 1; p->greedy = 0;
2219 break;
2220 case RQ_P_QQ:
2221 p->target = cnode;
2222 p->lower = 0; p->upper = 1; p->greedy = 0;
2223 c->lower = 1; c->upper = REPEAT_INFINITE; c->greedy = 1;
2224 return ;
2225 break;
2226 case RQ_PQ_Q:
2227 p->target = cnode;
2228 p->lower = 0; p->upper = 1; p->greedy = 1;
2229 c->lower = 1; c->upper = REPEAT_INFINITE; c->greedy = 0;
2230 return ;
2231 break;
2232 case RQ_ASIS:
2233 p->target = cnode;
2234 return ;
2235 break;
2238 c->target = NULL_NODE;
2239 onig_node_free(cnode);
2243 enum TokenSyms {
2244 TK_EOT = 0, /* end of token */
2245 TK_RAW_BYTE = 1,
2246 TK_CHAR,
2247 TK_STRING,
2248 TK_CODE_POINT,
2249 TK_ANYCHAR,
2250 TK_CHAR_TYPE,
2251 TK_BACKREF,
2252 TK_CALL,
2253 TK_ANCHOR,
2254 TK_OP_REPEAT,
2255 TK_INTERVAL,
2256 TK_ANYCHAR_ANYTIME, /* SQL '%' == .* */
2257 TK_ALT,
2258 TK_SUBEXP_OPEN,
2259 TK_SUBEXP_CLOSE,
2260 TK_CC_OPEN,
2261 TK_QUOTE_OPEN,
2262 TK_CHAR_PROPERTY, /* \p{...}, \P{...} */
2263 /* in cc */
2264 TK_CC_CLOSE,
2265 TK_CC_RANGE,
2266 TK_POSIX_BRACKET_OPEN,
2267 TK_CC_AND, /* && */
2268 TK_CC_CC_OPEN /* [ */
2271 typedef struct {
2272 enum TokenSyms type;
2273 int escaped;
2274 int base; /* is number: 8, 16 (used in [....]) */
2275 UChar* backp;
2276 union {
2277 UChar* s;
2278 int c;
2279 OnigCodePoint code;
2280 int anchor;
2281 int subtype;
2282 struct {
2283 int lower;
2284 int upper;
2285 int greedy;
2286 int possessive;
2287 } repeat;
2288 struct {
2289 int num;
2290 int ref1;
2291 int* refs;
2292 int by_name;
2293 #ifdef USE_BACKREF_WITH_LEVEL
2294 int exist_level;
2295 int level; /* \k<name+n> */
2296 #endif
2297 } backref;
2298 struct {
2299 UChar* name;
2300 UChar* name_end;
2301 int gnum;
2302 } call;
2303 struct {
2304 int ctype;
2305 int not;
2306 } prop;
2307 } u;
2308 } OnigToken;
2311 static int
2312 fetch_range_quantifier(UChar** src, UChar* end, OnigToken* tok, ScanEnv* env)
2314 int low, up, syn_allow, non_low = 0;
2315 int r = 0;
2316 OnigCodePoint c;
2317 OnigEncoding enc = env->enc;
2318 UChar* p = *src;
2319 PFETCH_READY;
2321 syn_allow = IS_SYNTAX_BV(env->syntax, ONIG_SYN_ALLOW_INVALID_INTERVAL);
2323 if (PEND) {
2324 if (syn_allow)
2325 return 1; /* "....{" : OK! */
2326 else
2327 return ONIGERR_END_PATTERN_AT_LEFT_BRACE; /* "....{" syntax error */
2330 if (! syn_allow) {
2331 c = PPEEK;
2332 if (c == ')' || c == '(' || c == '|') {
2333 return ONIGERR_END_PATTERN_AT_LEFT_BRACE;
2337 low = onig_scan_unsigned_number(&p, end, env->enc);
2338 if (low < 0) return ONIGERR_TOO_BIG_NUMBER_FOR_REPEAT_RANGE;
2339 if (low > ONIG_MAX_REPEAT_NUM)
2340 return ONIGERR_TOO_BIG_NUMBER_FOR_REPEAT_RANGE;
2342 if (p == *src) { /* can't read low */
2343 if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_ALLOW_INTERVAL_LOW_ABBREV)) {
2344 /* allow {,n} as {0,n} */
2345 low = 0;
2346 non_low = 1;
2348 else
2349 goto invalid;
2352 if (PEND) goto invalid;
2353 PFETCH(c);
2354 if (c == ',') {
2355 UChar* prev = p;
2356 up = onig_scan_unsigned_number(&p, end, env->enc);
2357 if (up < 0) return ONIGERR_TOO_BIG_NUMBER_FOR_REPEAT_RANGE;
2358 if (up > ONIG_MAX_REPEAT_NUM)
2359 return ONIGERR_TOO_BIG_NUMBER_FOR_REPEAT_RANGE;
2361 if (p == prev) {
2362 if (non_low != 0)
2363 goto invalid;
2364 up = REPEAT_INFINITE; /* {n,} : {n,infinite} */
2367 else {
2368 if (non_low != 0)
2369 goto invalid;
2371 PUNFETCH;
2372 up = low; /* {n} : exact n times */
2373 r = 2; /* fixed */
2376 if (PEND) goto invalid;
2377 PFETCH(c);
2378 if (IS_SYNTAX_OP(env->syntax, ONIG_SYN_OP_ESC_BRACE_INTERVAL)) {
2379 if (c != MC_ESC(env->syntax)) goto invalid;
2380 PFETCH(c);
2382 if (c != '}') goto invalid;
2384 if (!IS_REPEAT_INFINITE(up) && low > up) {
2385 return ONIGERR_UPPER_SMALLER_THAN_LOWER_IN_REPEAT_RANGE;
2388 tok->type = TK_INTERVAL;
2389 tok->u.repeat.lower = low;
2390 tok->u.repeat.upper = up;
2391 *src = p;
2392 return r; /* 0: normal {n,m}, 2: fixed {n} */
2394 invalid:
2395 if (syn_allow)
2396 return 1; /* OK */
2397 else
2398 return ONIGERR_INVALID_REPEAT_RANGE_PATTERN;
2401 /* \M-, \C-, \c, or \... */
2402 static int
2403 fetch_escaped_value(UChar** src, UChar* end, ScanEnv* env)
2405 int v;
2406 OnigCodePoint c;
2407 OnigEncoding enc = env->enc;
2408 UChar* p = *src;
2409 PFETCH_READY;
2411 if (PEND) return ONIGERR_END_PATTERN_AT_ESCAPE;
2413 PFETCH(c);
2414 switch (c) {
2415 case 'M':
2416 if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_ESC_CAPITAL_M_BAR_META)) {
2417 if (PEND) return ONIGERR_END_PATTERN_AT_META;
2418 PFETCH(c);
2419 if (c != '-') return ONIGERR_META_CODE_SYNTAX;
2420 if (PEND) return ONIGERR_END_PATTERN_AT_META;
2421 PFETCH(c);
2422 if (c == MC_ESC(env->syntax)) {
2423 v = fetch_escaped_value(&p, end, env);
2424 if (v < 0) return v;
2425 c = (OnigCodePoint )v;
2427 c = ((c & 0xff) | 0x80);
2429 else
2430 goto backslash;
2431 break;
2433 case 'C':
2434 if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_ESC_CAPITAL_C_BAR_CONTROL)) {
2435 if (PEND) return ONIGERR_END_PATTERN_AT_CONTROL;
2436 PFETCH(c);
2437 if (c != '-') return ONIGERR_CONTROL_CODE_SYNTAX;
2438 goto control;
2440 else
2441 goto backslash;
2443 case 'c':
2444 if (IS_SYNTAX_OP(env->syntax, ONIG_SYN_OP_ESC_C_CONTROL)) {
2445 control:
2446 if (PEND) return ONIGERR_END_PATTERN_AT_CONTROL;
2447 PFETCH(c);
2448 if (c == '?') {
2449 c = 0177;
2451 else {
2452 if (c == MC_ESC(env->syntax)) {
2453 v = fetch_escaped_value(&p, end, env);
2454 if (v < 0) return v;
2455 c = (OnigCodePoint )v;
2457 c &= 0x9f;
2459 break;
2461 /* fall through */
2463 default:
2465 backslash:
2466 c = conv_backslash_value(c, env);
2468 break;
2471 *src = p;
2472 return c;
2475 static int fetch_token(OnigToken* tok, UChar** src, UChar* end, ScanEnv* env);
2477 static OnigCodePoint
2478 get_name_end_code_point(OnigCodePoint start)
2480 switch (start) {
2481 case '<': return (OnigCodePoint )'>'; break;
2482 case '\'': return (OnigCodePoint )'\''; break;
2483 default:
2484 break;
2487 return (OnigCodePoint )0;
2490 #ifdef USE_NAMED_GROUP
2491 #ifdef USE_BACKREF_WITH_LEVEL
2493 \k<name+n>, \k<name-n>
2494 \k<num+n>, \k<num-n>
2495 \k<-num+n>, \k<-num-n>
2497 static int
2498 fetch_name_with_level(OnigCodePoint start_code, UChar** src, UChar* end,
2499 UChar** rname_end, ScanEnv* env,
2500 int* rback_num, int* rlevel)
2502 int r, sign, is_num, exist_level;
2503 OnigCodePoint end_code;
2504 OnigCodePoint c = 0;
2505 OnigEncoding enc = env->enc;
2506 UChar *name_end;
2507 UChar *pnum_head;
2508 UChar *p = *src;
2509 PFETCH_READY;
2511 *rback_num = 0;
2512 is_num = exist_level = 0;
2513 sign = 1;
2514 pnum_head = *src;
2516 end_code = get_name_end_code_point(start_code);
2518 name_end = end;
2519 r = 0;
2520 if (PEND) {
2521 return ONIGERR_EMPTY_GROUP_NAME;
2523 else {
2524 PFETCH(c);
2525 if (c == end_code)
2526 return ONIGERR_EMPTY_GROUP_NAME;
2528 if (ONIGENC_IS_CODE_DIGIT(enc, c)) {
2529 is_num = 1;
2531 else if (c == '-') {
2532 is_num = 2;
2533 sign = -1;
2534 pnum_head = p;
2536 else if (!ONIGENC_IS_CODE_WORD(enc, c)) {
2537 r = ONIGERR_INVALID_CHAR_IN_GROUP_NAME;
2541 while (!PEND) {
2542 name_end = p;
2543 PFETCH(c);
2544 if (c == end_code || c == ')' || c == '+' || c == '-') {
2545 if (is_num == 2) r = ONIGERR_INVALID_GROUP_NAME;
2546 break;
2549 if (is_num != 0) {
2550 if (ONIGENC_IS_CODE_DIGIT(enc, c)) {
2551 is_num = 1;
2553 else {
2554 r = ONIGERR_INVALID_GROUP_NAME;
2555 is_num = 0;
2558 else if (!ONIGENC_IS_CODE_WORD(enc, c)) {
2559 r = ONIGERR_INVALID_CHAR_IN_GROUP_NAME;
2563 if (r == 0 && c != end_code) {
2564 if (c == '+' || c == '-') {
2565 int level;
2566 int flag = (c == '-' ? -1 : 1);
2568 PFETCH(c);
2569 if (! ONIGENC_IS_CODE_DIGIT(enc, c)) goto err;
2570 PUNFETCH;
2571 level = onig_scan_unsigned_number(&p, end, enc);
2572 if (level < 0) return ONIGERR_TOO_BIG_NUMBER;
2573 *rlevel = (level * flag);
2574 exist_level = 1;
2576 PFETCH(c);
2577 if (c == end_code)
2578 goto end;
2581 err:
2582 r = ONIGERR_INVALID_GROUP_NAME;
2583 name_end = end;
2586 end:
2587 if (r == 0) {
2588 if (is_num != 0) {
2589 *rback_num = onig_scan_unsigned_number(&pnum_head, name_end, enc);
2590 if (*rback_num < 0) return ONIGERR_TOO_BIG_NUMBER;
2591 else if (*rback_num == 0) goto err;
2593 *rback_num *= sign;
2596 *rname_end = name_end;
2597 *src = p;
2598 return (exist_level ? 1 : 0);
2600 else {
2601 onig_scan_env_set_error_string(env, r, *src, name_end);
2602 return r;
2605 #endif /* USE_BACKREF_WITH_LEVEL */
2608 def: 0 -> define name (don't allow number name)
2609 1 -> reference name (allow number name)
2611 static int
2612 fetch_name(OnigCodePoint start_code, UChar** src, UChar* end,
2613 UChar** rname_end, ScanEnv* env, int* rback_num, int ref)
2615 int r, is_num, sign;
2616 OnigCodePoint end_code;
2617 OnigCodePoint c = 0;
2618 OnigEncoding enc = env->enc;
2619 UChar *name_end;
2620 UChar *pnum_head;
2621 UChar *p = *src;
2622 PFETCH_READY;
2624 *rback_num = 0;
2626 end_code = get_name_end_code_point(start_code);
2628 name_end = end;
2629 pnum_head = *src;
2630 r = 0;
2631 is_num = 0;
2632 sign = 1;
2633 if (PEND) {
2634 return ONIGERR_EMPTY_GROUP_NAME;
2636 else {
2637 PFETCH(c);
2638 if (c == end_code)
2639 return ONIGERR_EMPTY_GROUP_NAME;
2641 if (ONIGENC_IS_CODE_DIGIT(enc, c)) {
2642 if (ref == 1)
2643 is_num = 1;
2644 else {
2645 r = ONIGERR_INVALID_GROUP_NAME;
2646 is_num = 0;
2649 else if (c == '-') {
2650 if (ref == 1) {
2651 is_num = 2;
2652 sign = -1;
2653 pnum_head = p;
2655 else {
2656 r = ONIGERR_INVALID_GROUP_NAME;
2657 is_num = 0;
2660 else if (!ONIGENC_IS_CODE_WORD(enc, c)) {
2661 r = ONIGERR_INVALID_CHAR_IN_GROUP_NAME;
2665 if (r == 0) {
2666 while (!PEND) {
2667 name_end = p;
2668 PFETCH(c);
2669 if (c == end_code || c == ')') {
2670 if (is_num == 2) r = ONIGERR_INVALID_GROUP_NAME;
2671 break;
2674 if (is_num != 0) {
2675 if (ONIGENC_IS_CODE_DIGIT(enc, c)) {
2676 is_num = 1;
2678 else {
2679 if (!ONIGENC_IS_CODE_WORD(enc, c))
2680 r = ONIGERR_INVALID_CHAR_IN_GROUP_NAME;
2681 else
2682 r = ONIGERR_INVALID_GROUP_NAME;
2684 is_num = 0;
2687 else {
2688 if (!ONIGENC_IS_CODE_WORD(enc, c)) {
2689 r = ONIGERR_INVALID_CHAR_IN_GROUP_NAME;
2694 if (c != end_code) {
2695 r = ONIGERR_INVALID_GROUP_NAME;
2696 name_end = end;
2699 if (is_num != 0) {
2700 *rback_num = onig_scan_unsigned_number(&pnum_head, name_end, enc);
2701 if (*rback_num < 0) return ONIGERR_TOO_BIG_NUMBER;
2702 else if (*rback_num == 0) {
2703 r = ONIGERR_INVALID_GROUP_NAME;
2704 goto err;
2707 *rback_num *= sign;
2710 *rname_end = name_end;
2711 *src = p;
2712 return 0;
2714 else {
2715 while (!PEND) {
2716 name_end = p;
2717 PFETCH(c);
2718 if (c == end_code || c == ')')
2719 break;
2721 if (PEND)
2722 name_end = end;
2724 err:
2725 onig_scan_env_set_error_string(env, r, *src, name_end);
2726 return r;
2729 #else
2730 static int
2731 fetch_name(OnigCodePoint start_code, UChar** src, UChar* end,
2732 UChar** rname_end, ScanEnv* env, int* rback_num, int ref)
2734 int r, is_num, sign;
2735 OnigCodePoint end_code;
2736 OnigCodePoint c = 0;
2737 UChar *name_end;
2738 OnigEncoding enc = env->enc;
2739 UChar *pnum_head;
2740 UChar *p = *src;
2741 PFETCH_READY;
2743 *rback_num = 0;
2745 end_code = get_name_end_code_point(start_code);
2747 *rname_end = name_end = end;
2748 r = 0;
2749 pnum_head = *src;
2750 is_num = 0;
2751 sign = 1;
2753 if (PEND) {
2754 return ONIGERR_EMPTY_GROUP_NAME;
2756 else {
2757 PFETCH(c);
2758 if (c == end_code)
2759 return ONIGERR_EMPTY_GROUP_NAME;
2761 if (ONIGENC_IS_CODE_DIGIT(enc, c)) {
2762 is_num = 1;
2764 else if (c == '-') {
2765 is_num = 2;
2766 sign = -1;
2767 pnum_head = p;
2769 else {
2770 r = ONIGERR_INVALID_CHAR_IN_GROUP_NAME;
2774 while (!PEND) {
2775 name_end = p;
2777 PFETCH(c);
2778 if (c == end_code || c == ')') break;
2779 if (! ONIGENC_IS_CODE_DIGIT(enc, c))
2780 r = ONIGERR_INVALID_CHAR_IN_GROUP_NAME;
2782 if (r == 0 && c != end_code) {
2783 r = ONIGERR_INVALID_GROUP_NAME;
2784 name_end = end;
2787 if (r == 0) {
2788 *rback_num = onig_scan_unsigned_number(&pnum_head, name_end, enc);
2789 if (*rback_num < 0) return ONIGERR_TOO_BIG_NUMBER;
2790 else if (*rback_num == 0) {
2791 r = ONIGERR_INVALID_GROUP_NAME;
2792 goto err;
2794 *rback_num *= sign;
2796 *rname_end = name_end;
2797 *src = p;
2798 return 0;
2800 else {
2801 err:
2802 onig_scan_env_set_error_string(env, r, *src, name_end);
2803 return r;
2806 #endif /* USE_NAMED_GROUP */
2808 static void
2809 CC_ESC_WARN(ScanEnv* env, UChar *c)
2811 if (onig_warn == onig_null_warn) return ;
2813 if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_WARN_CC_OP_NOT_ESCAPED) &&
2814 IS_SYNTAX_BV(env->syntax, ONIG_SYN_BACKSLASH_ESCAPE_IN_CC)) {
2815 UChar buf[WARN_BUFSIZE];
2816 onig_snprintf_with_pattern(buf, WARN_BUFSIZE, env->enc,
2817 env->pattern, env->pattern_end,
2818 (UChar* )"character class has '%s' without escape", c);
2819 (*onig_warn)((char* )buf);
2823 static void
2824 CLOSE_BRACKET_WITHOUT_ESC_WARN(ScanEnv* env, UChar* c)
2826 if (onig_warn == onig_null_warn) return ;
2828 if (IS_SYNTAX_BV((env)->syntax, ONIG_SYN_WARN_CC_OP_NOT_ESCAPED)) {
2829 UChar buf[WARN_BUFSIZE];
2830 onig_snprintf_with_pattern(buf, WARN_BUFSIZE, (env)->enc,
2831 (env)->pattern, (env)->pattern_end,
2832 (UChar* )"regular expression has '%s' without escape", c);
2833 (*onig_warn)((char* )buf);
2837 static UChar*
2838 find_str_position(OnigCodePoint s[], int n, UChar* from, UChar* to,
2839 UChar **next, OnigEncoding enc)
2841 int i;
2842 OnigCodePoint x;
2843 UChar *q;
2844 UChar *p = from;
2846 while (p < to) {
2847 x = ONIGENC_MBC_TO_CODE(enc, p, to);
2848 q = p + enclen(enc, p, to);
2849 if (x == s[0]) {
2850 for (i = 1; i < n && q < to; i++) {
2851 x = ONIGENC_MBC_TO_CODE(enc, q, to);
2852 if (x != s[i]) break;
2853 q += enclen(enc, q, to);
2855 if (i >= n) {
2856 if (IS_NOT_NULL(next))
2857 *next = q;
2858 return p;
2861 p = q;
2863 return NULL_UCHARP;
2866 static int
2867 str_exist_check_with_esc(OnigCodePoint s[], int n, UChar* from, UChar* to,
2868 OnigCodePoint bad, OnigEncoding enc, const OnigSyntaxType* syn)
2870 int i, in_esc;
2871 OnigCodePoint x;
2872 UChar *q;
2873 UChar *p = from;
2875 in_esc = 0;
2876 while (p < to) {
2877 if (in_esc) {
2878 in_esc = 0;
2879 p += enclen(enc, p, to);
2881 else {
2882 x = ONIGENC_MBC_TO_CODE(enc, p, to);
2883 q = p + enclen(enc, p, to);
2884 if (x == s[0]) {
2885 for (i = 1; i < n && q < to; i++) {
2886 x = ONIGENC_MBC_TO_CODE(enc, q, to);
2887 if (x != s[i]) break;
2888 q += enclen(enc, q, to);
2890 if (i >= n) return 1;
2891 p += enclen(enc, p, to);
2893 else {
2894 x = ONIGENC_MBC_TO_CODE(enc, p, to);
2895 if (x == bad) return 0;
2896 else if (x == MC_ESC(syn)) in_esc = 1;
2897 p = q;
2901 return 0;
2904 static int
2905 fetch_token_in_cc(OnigToken* tok, UChar** src, UChar* end, ScanEnv* env)
2907 int num;
2908 OnigCodePoint c, c2;
2909 const OnigSyntaxType* syn = env->syntax;
2910 OnigEncoding enc = env->enc;
2911 UChar* prev;
2912 UChar* p = *src;
2913 PFETCH_READY;
2915 if (PEND) {
2916 tok->type = TK_EOT;
2917 return tok->type;
2920 PFETCH(c);
2921 tok->type = TK_CHAR;
2922 tok->base = 0;
2923 tok->u.c = c;
2924 tok->escaped = 0;
2926 if (c == ']') {
2927 tok->type = TK_CC_CLOSE;
2929 else if (c == '-') {
2930 tok->type = TK_CC_RANGE;
2932 else if (c == MC_ESC(syn)) {
2933 if (! IS_SYNTAX_BV(syn, ONIG_SYN_BACKSLASH_ESCAPE_IN_CC))
2934 goto end;
2936 if (PEND) return ONIGERR_END_PATTERN_AT_ESCAPE;
2938 PFETCH(c);
2939 tok->escaped = 1;
2940 tok->u.c = c;
2941 switch (c) {
2942 case 'w':
2943 tok->type = TK_CHAR_TYPE;
2944 tok->u.prop.ctype = ONIGENC_CTYPE_WORD;
2945 tok->u.prop.not = 0;
2946 break;
2947 case 'W':
2948 tok->type = TK_CHAR_TYPE;
2949 tok->u.prop.ctype = ONIGENC_CTYPE_WORD;
2950 tok->u.prop.not = 1;
2951 break;
2952 case 'd':
2953 tok->type = TK_CHAR_TYPE;
2954 tok->u.prop.ctype = ONIGENC_CTYPE_DIGIT;
2955 tok->u.prop.not = 0;
2956 break;
2957 case 'D':
2958 tok->type = TK_CHAR_TYPE;
2959 tok->u.prop.ctype = ONIGENC_CTYPE_DIGIT;
2960 tok->u.prop.not = 1;
2961 break;
2962 case 's':
2963 tok->type = TK_CHAR_TYPE;
2964 tok->u.prop.ctype = ONIGENC_CTYPE_SPACE;
2965 tok->u.prop.not = 0;
2966 break;
2967 case 'S':
2968 tok->type = TK_CHAR_TYPE;
2969 tok->u.prop.ctype = ONIGENC_CTYPE_SPACE;
2970 tok->u.prop.not = 1;
2971 break;
2972 case 'h':
2973 if (! IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_H_XDIGIT)) break;
2974 tok->type = TK_CHAR_TYPE;
2975 tok->u.prop.ctype = ONIGENC_CTYPE_XDIGIT;
2976 tok->u.prop.not = 0;
2977 break;
2978 case 'H':
2979 if (! IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_H_XDIGIT)) break;
2980 tok->type = TK_CHAR_TYPE;
2981 tok->u.prop.ctype = ONIGENC_CTYPE_XDIGIT;
2982 tok->u.prop.not = 1;
2983 break;
2985 case 'p':
2986 case 'P':
2987 c2 = PPEEK;
2988 if (c2 == '{' &&
2989 IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_P_BRACE_CHAR_PROPERTY)) {
2990 PINC;
2991 tok->type = TK_CHAR_PROPERTY;
2992 tok->u.prop.not = (c == 'P' ? 1 : 0);
2994 if (IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_P_BRACE_CIRCUMFLEX_NOT)) {
2995 PFETCH(c2);
2996 if (c2 == '^') {
2997 tok->u.prop.not = (tok->u.prop.not == 0 ? 1 : 0);
2999 else
3000 PUNFETCH;
3003 break;
3005 case 'x':
3006 if (PEND) break;
3008 prev = p;
3009 if (PPEEK_IS('{') && IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_X_BRACE_HEX8)) {
3010 PINC;
3011 num = scan_unsigned_hexadecimal_number(&p, end, 8, enc);
3012 if (num < 0) return ONIGERR_TOO_BIG_WIDE_CHAR_VALUE;
3013 if (!PEND) {
3014 c2 = PPEEK;
3015 if (ONIGENC_IS_CODE_XDIGIT(enc, c2))
3016 return ONIGERR_TOO_LONG_WIDE_CHAR_VALUE;
3019 if (p > prev + enclen(enc, prev, end) && !PEND && (PPEEK_IS('}'))) {
3020 PINC;
3021 tok->type = TK_CODE_POINT;
3022 tok->base = 16;
3023 tok->u.code = (OnigCodePoint )num;
3025 else {
3026 /* can't read nothing or invalid format */
3027 p = prev;
3030 else if (IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_X_HEX2)) {
3031 num = scan_unsigned_hexadecimal_number(&p, end, 2, enc);
3032 if (num < 0) return ONIGERR_TOO_BIG_NUMBER;
3033 if (p == prev) { /* can't read nothing. */
3034 num = 0; /* but, it's not error */
3036 tok->type = TK_RAW_BYTE;
3037 tok->base = 16;
3038 tok->u.c = num;
3040 break;
3042 case 'u':
3043 if (PEND) break;
3045 prev = p;
3046 if (IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_U_HEX4)) {
3047 num = scan_unsigned_hexadecimal_number(&p, end, 4, enc);
3048 if (num < 0) return ONIGERR_TOO_BIG_NUMBER;
3049 if (p == prev) { /* can't read nothing. */
3050 num = 0; /* but, it's not error */
3052 tok->type = TK_CODE_POINT;
3053 tok->base = 16;
3054 tok->u.code = (OnigCodePoint )num;
3056 break;
3058 case '0':
3059 case '1': case '2': case '3': case '4': case '5': case '6': case '7':
3060 if (IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_OCTAL3)) {
3061 PUNFETCH;
3062 prev = p;
3063 num = scan_unsigned_octal_number(&p, end, 3, enc);
3064 if (num < 0) return ONIGERR_TOO_BIG_NUMBER;
3065 if (p == prev) { /* can't read nothing. */
3066 num = 0; /* but, it's not error */
3068 tok->type = TK_RAW_BYTE;
3069 tok->base = 8;
3070 tok->u.c = num;
3072 break;
3074 default:
3075 PUNFETCH;
3076 num = fetch_escaped_value(&p, end, env);
3077 if (num < 0) return num;
3078 if (tok->u.c != num) {
3079 tok->u.code = (OnigCodePoint )num;
3080 tok->type = TK_CODE_POINT;
3082 break;
3085 else if (c == '[') {
3086 if (IS_SYNTAX_OP(syn, ONIG_SYN_OP_POSIX_BRACKET) && (PPEEK_IS(':'))) {
3087 OnigCodePoint send[] = { (OnigCodePoint )':', (OnigCodePoint )']' };
3088 tok->backp = p; /* point at '[' is readed */
3089 PINC;
3090 if (str_exist_check_with_esc(send, 2, p, end,
3091 (OnigCodePoint )']', enc, syn)) {
3092 tok->type = TK_POSIX_BRACKET_OPEN;
3094 else {
3095 PUNFETCH;
3096 goto cc_in_cc;
3099 else {
3100 cc_in_cc:
3101 if (IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_CCLASS_SET_OP)) {
3102 tok->type = TK_CC_CC_OPEN;
3104 else {
3105 CC_ESC_WARN(env, (UChar* )"[");
3109 else if (c == '&') {
3110 if (IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_CCLASS_SET_OP) &&
3111 !PEND && (PPEEK_IS('&'))) {
3112 PINC;
3113 tok->type = TK_CC_AND;
3117 end:
3118 *src = p;
3119 return tok->type;
3122 static int
3123 fetch_token(OnigToken* tok, UChar** src, UChar* end, ScanEnv* env)
3125 int r, num;
3126 OnigCodePoint c;
3127 OnigEncoding enc = env->enc;
3128 const OnigSyntaxType* syn = env->syntax;
3129 UChar* prev;
3130 UChar* p = *src;
3131 PFETCH_READY;
3133 start:
3134 if (PEND) {
3135 tok->type = TK_EOT;
3136 return tok->type;
3139 tok->type = TK_STRING;
3140 tok->base = 0;
3141 tok->backp = p;
3143 PFETCH(c);
3144 if (IS_MC_ESC_CODE(c, syn)) {
3145 if (PEND) return ONIGERR_END_PATTERN_AT_ESCAPE;
3147 tok->backp = p;
3148 PFETCH(c);
3150 tok->u.c = c;
3151 tok->escaped = 1;
3152 switch (c) {
3153 case '*':
3154 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_ASTERISK_ZERO_INF)) break;
3155 tok->type = TK_OP_REPEAT;
3156 tok->u.repeat.lower = 0;
3157 tok->u.repeat.upper = REPEAT_INFINITE;
3158 goto greedy_check;
3159 break;
3161 case '+':
3162 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_PLUS_ONE_INF)) break;
3163 tok->type = TK_OP_REPEAT;
3164 tok->u.repeat.lower = 1;
3165 tok->u.repeat.upper = REPEAT_INFINITE;
3166 goto greedy_check;
3167 break;
3169 case '?':
3170 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_QMARK_ZERO_ONE)) break;
3171 tok->type = TK_OP_REPEAT;
3172 tok->u.repeat.lower = 0;
3173 tok->u.repeat.upper = 1;
3174 greedy_check:
3175 if (!PEND && PPEEK_IS('?') &&
3176 IS_SYNTAX_OP(syn, ONIG_SYN_OP_QMARK_NON_GREEDY)) {
3177 PFETCH(c);
3178 tok->u.repeat.greedy = 0;
3179 tok->u.repeat.possessive = 0;
3181 else {
3182 possessive_check:
3183 if (!PEND && PPEEK_IS('+') &&
3184 ((IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_PLUS_POSSESSIVE_REPEAT) &&
3185 tok->type != TK_INTERVAL) ||
3186 (IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_PLUS_POSSESSIVE_INTERVAL) &&
3187 tok->type == TK_INTERVAL))) {
3188 PFETCH(c);
3189 tok->u.repeat.greedy = 1;
3190 tok->u.repeat.possessive = 1;
3192 else {
3193 tok->u.repeat.greedy = 1;
3194 tok->u.repeat.possessive = 0;
3197 break;
3199 case '{':
3200 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_BRACE_INTERVAL)) break;
3201 r = fetch_range_quantifier(&p, end, tok, env);
3202 if (r < 0) return r; /* error */
3203 if (r == 0) goto greedy_check;
3204 else if (r == 2) { /* {n} */
3205 if (IS_SYNTAX_BV(syn, ONIG_SYN_FIXED_INTERVAL_IS_GREEDY_ONLY))
3206 goto possessive_check;
3208 goto greedy_check;
3210 /* r == 1 : normal char */
3211 break;
3213 case '|':
3214 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_VBAR_ALT)) break;
3215 tok->type = TK_ALT;
3216 break;
3218 case '(':
3219 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_LPAREN_SUBEXP)) break;
3220 tok->type = TK_SUBEXP_OPEN;
3221 break;
3223 case ')':
3224 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_LPAREN_SUBEXP)) break;
3225 tok->type = TK_SUBEXP_CLOSE;
3226 break;
3228 case 'w':
3229 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_W_WORD)) break;
3230 tok->type = TK_CHAR_TYPE;
3231 tok->u.prop.ctype = ONIGENC_CTYPE_WORD;
3232 tok->u.prop.not = 0;
3233 break;
3235 case 'W':
3236 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_W_WORD)) break;
3237 tok->type = TK_CHAR_TYPE;
3238 tok->u.prop.ctype = ONIGENC_CTYPE_WORD;
3239 tok->u.prop.not = 1;
3240 break;
3242 case 'b':
3243 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_B_WORD_BOUND)) break;
3244 tok->type = TK_ANCHOR;
3245 tok->u.anchor = ANCHOR_WORD_BOUND;
3246 break;
3248 case 'B':
3249 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_B_WORD_BOUND)) break;
3250 tok->type = TK_ANCHOR;
3251 tok->u.anchor = ANCHOR_NOT_WORD_BOUND;
3252 break;
3254 #ifdef USE_WORD_BEGIN_END
3255 case '<':
3256 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_LTGT_WORD_BEGIN_END)) break;
3257 tok->type = TK_ANCHOR;
3258 tok->u.anchor = ANCHOR_WORD_BEGIN;
3259 break;
3261 case '>':
3262 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_LTGT_WORD_BEGIN_END)) break;
3263 tok->type = TK_ANCHOR;
3264 tok->u.anchor = ANCHOR_WORD_END;
3265 break;
3266 #endif
3268 case 's':
3269 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_S_WHITE_SPACE)) break;
3270 tok->type = TK_CHAR_TYPE;
3271 tok->u.prop.ctype = ONIGENC_CTYPE_SPACE;
3272 tok->u.prop.not = 0;
3273 break;
3275 case 'S':
3276 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_S_WHITE_SPACE)) break;
3277 tok->type = TK_CHAR_TYPE;
3278 tok->u.prop.ctype = ONIGENC_CTYPE_SPACE;
3279 tok->u.prop.not = 1;
3280 break;
3282 case 'd':
3283 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_D_DIGIT)) break;
3284 tok->type = TK_CHAR_TYPE;
3285 tok->u.prop.ctype = ONIGENC_CTYPE_DIGIT;
3286 tok->u.prop.not = 0;
3287 break;
3289 case 'D':
3290 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_D_DIGIT)) break;
3291 tok->type = TK_CHAR_TYPE;
3292 tok->u.prop.ctype = ONIGENC_CTYPE_DIGIT;
3293 tok->u.prop.not = 1;
3294 break;
3296 case 'h':
3297 if (! IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_H_XDIGIT)) break;
3298 tok->type = TK_CHAR_TYPE;
3299 tok->u.prop.ctype = ONIGENC_CTYPE_XDIGIT;
3300 tok->u.prop.not = 0;
3301 break;
3303 case 'H':
3304 if (! IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_H_XDIGIT)) break;
3305 tok->type = TK_CHAR_TYPE;
3306 tok->u.prop.ctype = ONIGENC_CTYPE_XDIGIT;
3307 tok->u.prop.not = 1;
3308 break;
3310 case 'A':
3311 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_AZ_BUF_ANCHOR)) break;
3312 begin_buf:
3313 tok->type = TK_ANCHOR;
3314 tok->u.subtype = ANCHOR_BEGIN_BUF;
3315 break;
3317 case 'Z':
3318 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_AZ_BUF_ANCHOR)) break;
3319 tok->type = TK_ANCHOR;
3320 tok->u.subtype = ANCHOR_SEMI_END_BUF;
3321 break;
3323 case 'z':
3324 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_AZ_BUF_ANCHOR)) break;
3325 end_buf:
3326 tok->type = TK_ANCHOR;
3327 tok->u.subtype = ANCHOR_END_BUF;
3328 break;
3330 case 'G':
3331 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_CAPITAL_G_BEGIN_ANCHOR)) break;
3332 tok->type = TK_ANCHOR;
3333 tok->u.subtype = ANCHOR_BEGIN_POSITION;
3334 break;
3336 case '`':
3337 if (! IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_GNU_BUF_ANCHOR)) break;
3338 goto begin_buf;
3339 break;
3341 case '\'':
3342 if (! IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_GNU_BUF_ANCHOR)) break;
3343 goto end_buf;
3344 break;
3346 case 'x':
3347 if (PEND) break;
3349 prev = p;
3350 if (PPEEK_IS('{') && IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_X_BRACE_HEX8)) {
3351 PINC;
3352 num = scan_unsigned_hexadecimal_number(&p, end, 8, enc);
3353 if (num < 0) return ONIGERR_TOO_BIG_WIDE_CHAR_VALUE;
3354 if (!PEND) {
3355 if (ONIGENC_IS_CODE_XDIGIT(enc, PPEEK))
3356 return ONIGERR_TOO_LONG_WIDE_CHAR_VALUE;
3359 if ((p > prev + enclen(enc, prev, end)) && !PEND && PPEEK_IS('}')) {
3360 PINC;
3361 tok->type = TK_CODE_POINT;
3362 tok->u.code = (OnigCodePoint )num;
3364 else {
3365 /* can't read nothing or invalid format */
3366 p = prev;
3369 else if (IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_X_HEX2)) {
3370 num = scan_unsigned_hexadecimal_number(&p, end, 2, enc);
3371 if (num < 0) return ONIGERR_TOO_BIG_NUMBER;
3372 if (p == prev) { /* can't read nothing. */
3373 num = 0; /* but, it's not error */
3375 tok->type = TK_RAW_BYTE;
3376 tok->base = 16;
3377 tok->u.c = num;
3379 break;
3381 case 'u':
3382 if (PEND) break;
3384 prev = p;
3385 if (IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_U_HEX4)) {
3386 num = scan_unsigned_hexadecimal_number(&p, end, 4, enc);
3387 if (num < 0) return ONIGERR_TOO_BIG_NUMBER;
3388 if (p == prev) { /* can't read nothing. */
3389 num = 0; /* but, it's not error */
3391 tok->type = TK_CODE_POINT;
3392 tok->base = 16;
3393 tok->u.code = (OnigCodePoint )num;
3395 break;
3397 case '1': case '2': case '3': case '4':
3398 case '5': case '6': case '7': case '8': case '9':
3399 PUNFETCH;
3400 prev = p;
3401 num = onig_scan_unsigned_number(&p, end, enc);
3402 if (num < 0 || num > ONIG_MAX_BACKREF_NUM) {
3403 goto skip_backref;
3406 if (IS_SYNTAX_OP(syn, ONIG_SYN_OP_DECIMAL_BACKREF) &&
3407 (num <= env->num_mem || num <= 9)) { /* This spec. from GNU regex */
3408 if (IS_SYNTAX_BV(syn, ONIG_SYN_STRICT_CHECK_BACKREF)) {
3409 if (num > env->num_mem || IS_NULL(SCANENV_MEM_NODES(env)[num]))
3410 return ONIGERR_INVALID_BACKREF;
3413 tok->type = TK_BACKREF;
3414 tok->u.backref.num = 1;
3415 tok->u.backref.ref1 = num;
3416 tok->u.backref.by_name = 0;
3417 #ifdef USE_BACKREF_WITH_LEVEL
3418 tok->u.backref.exist_level = 0;
3419 #endif
3420 break;
3423 skip_backref:
3424 if (c == '8' || c == '9') {
3425 /* normal char */
3426 p = prev; PINC;
3427 break;
3430 p = prev;
3431 /* fall through */
3432 case '0':
3433 if (IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_OCTAL3)) {
3434 prev = p;
3435 num = scan_unsigned_octal_number(&p, end, (c == '0' ? 2:3), enc);
3436 if (num < 0) return ONIGERR_TOO_BIG_NUMBER;
3437 if (p == prev) { /* can't read nothing. */
3438 num = 0; /* but, it's not error */
3440 tok->type = TK_RAW_BYTE;
3441 tok->base = 8;
3442 tok->u.c = num;
3444 else if (c != '0') {
3445 PINC;
3447 break;
3449 #ifdef USE_NAMED_GROUP
3450 case 'k':
3451 if (IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_K_NAMED_BACKREF)) {
3452 PFETCH(c);
3453 if (c == '<' || c == '\'') {
3454 UChar* name_end;
3455 int* backs;
3456 int back_num;
3458 prev = p;
3460 #ifdef USE_BACKREF_WITH_LEVEL
3461 name_end = NULL_UCHARP; /* no need. escape gcc warning. */
3462 r = fetch_name_with_level((OnigCodePoint )c, &p, end, &name_end,
3463 env, &back_num, &tok->u.backref.level);
3464 if (r == 1) tok->u.backref.exist_level = 1;
3465 else tok->u.backref.exist_level = 0;
3466 #else
3467 r = fetch_name(&p, end, &name_end, env, &back_num, 1);
3468 #endif
3469 if (r < 0) return r;
3471 if (back_num != 0) {
3472 if (back_num < 0) {
3473 back_num = BACKREF_REL_TO_ABS(back_num, env);
3474 if (back_num <= 0)
3475 return ONIGERR_INVALID_BACKREF;
3478 if (IS_SYNTAX_BV(syn, ONIG_SYN_STRICT_CHECK_BACKREF)) {
3479 if (back_num > env->num_mem ||
3480 IS_NULL(SCANENV_MEM_NODES(env)[back_num]))
3481 return ONIGERR_INVALID_BACKREF;
3483 tok->type = TK_BACKREF;
3484 tok->u.backref.by_name = 0;
3485 tok->u.backref.num = 1;
3486 tok->u.backref.ref1 = back_num;
3488 else {
3489 num = onig_name_to_group_numbers(env->reg, prev, name_end, &backs);
3490 if (num <= 0) {
3491 onig_scan_env_set_error_string(env,
3492 ONIGERR_UNDEFINED_NAME_REFERENCE, prev, name_end);
3493 return ONIGERR_UNDEFINED_NAME_REFERENCE;
3495 if (IS_SYNTAX_BV(syn, ONIG_SYN_STRICT_CHECK_BACKREF)) {
3496 int i;
3497 for (i = 0; i < num; i++) {
3498 if (backs[i] > env->num_mem ||
3499 IS_NULL(SCANENV_MEM_NODES(env)[backs[i]]))
3500 return ONIGERR_INVALID_BACKREF;
3504 tok->type = TK_BACKREF;
3505 tok->u.backref.by_name = 1;
3506 if (num == 1) {
3507 tok->u.backref.num = 1;
3508 tok->u.backref.ref1 = backs[0];
3510 else {
3511 tok->u.backref.num = num;
3512 tok->u.backref.refs = backs;
3516 else
3517 PUNFETCH;
3519 break;
3520 #endif
3522 #ifdef USE_SUBEXP_CALL
3523 case 'g':
3524 if (IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_G_SUBEXP_CALL)) {
3525 PFETCH(c);
3526 if (c == '<' || c == '\'') {
3527 int gnum;
3528 UChar* name_end;
3530 prev = p;
3531 r = fetch_name((OnigCodePoint )c, &p, end, &name_end, env, &gnum, 1);
3532 if (r < 0) return r;
3534 tok->type = TK_CALL;
3535 tok->u.call.name = prev;
3536 tok->u.call.name_end = name_end;
3537 tok->u.call.gnum = gnum;
3539 else
3540 PUNFETCH;
3542 break;
3543 #endif
3545 case 'Q':
3546 if (IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_CAPITAL_Q_QUOTE)) {
3547 tok->type = TK_QUOTE_OPEN;
3549 break;
3551 case 'p':
3552 case 'P':
3553 if (PPEEK_IS('{') &&
3554 IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_P_BRACE_CHAR_PROPERTY)) {
3555 PINC;
3556 tok->type = TK_CHAR_PROPERTY;
3557 tok->u.prop.not = (c == 'P' ? 1 : 0);
3559 if (IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_P_BRACE_CIRCUMFLEX_NOT)) {
3560 PFETCH(c);
3561 if (c == '^') {
3562 tok->u.prop.not = (tok->u.prop.not == 0 ? 1 : 0);
3564 else
3565 PUNFETCH;
3568 break;
3570 default:
3571 PUNFETCH;
3572 num = fetch_escaped_value(&p, end, env);
3573 if (num < 0) return num;
3574 /* set_raw: */
3575 if (tok->u.c != num) {
3576 tok->type = TK_CODE_POINT;
3577 tok->u.code = (OnigCodePoint )num;
3579 else { /* string */
3580 p = tok->backp + enclen(enc, tok->backp, end);
3582 break;
3585 else {
3586 tok->u.c = c;
3587 tok->escaped = 0;
3589 #ifdef USE_VARIABLE_META_CHARS
3590 if ((c != ONIG_INEFFECTIVE_META_CHAR) &&
3591 IS_SYNTAX_OP(syn, ONIG_SYN_OP_VARIABLE_META_CHARACTERS)) {
3592 if (c == MC_ANYCHAR(syn))
3593 goto any_char;
3594 else if (c == MC_ANYTIME(syn))
3595 goto anytime;
3596 else if (c == MC_ZERO_OR_ONE_TIME(syn))
3597 goto zero_or_one_time;
3598 else if (c == MC_ONE_OR_MORE_TIME(syn))
3599 goto one_or_more_time;
3600 else if (c == MC_ANYCHAR_ANYTIME(syn)) {
3601 tok->type = TK_ANYCHAR_ANYTIME;
3602 goto out;
3605 #endif
3607 switch (c) {
3608 case '.':
3609 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_DOT_ANYCHAR)) break;
3610 #ifdef USE_VARIABLE_META_CHARS
3611 any_char:
3612 #endif
3613 tok->type = TK_ANYCHAR;
3614 break;
3616 case '*':
3617 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ASTERISK_ZERO_INF)) break;
3618 #ifdef USE_VARIABLE_META_CHARS
3619 anytime:
3620 #endif
3621 tok->type = TK_OP_REPEAT;
3622 tok->u.repeat.lower = 0;
3623 tok->u.repeat.upper = REPEAT_INFINITE;
3624 goto greedy_check;
3625 break;
3627 case '+':
3628 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_PLUS_ONE_INF)) break;
3629 #ifdef USE_VARIABLE_META_CHARS
3630 one_or_more_time:
3631 #endif
3632 tok->type = TK_OP_REPEAT;
3633 tok->u.repeat.lower = 1;
3634 tok->u.repeat.upper = REPEAT_INFINITE;
3635 goto greedy_check;
3636 break;
3638 case '?':
3639 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_QMARK_ZERO_ONE)) break;
3640 #ifdef USE_VARIABLE_META_CHARS
3641 zero_or_one_time:
3642 #endif
3643 tok->type = TK_OP_REPEAT;
3644 tok->u.repeat.lower = 0;
3645 tok->u.repeat.upper = 1;
3646 goto greedy_check;
3647 break;
3649 case '{':
3650 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_BRACE_INTERVAL)) break;
3651 r = fetch_range_quantifier(&p, end, tok, env);
3652 if (r < 0) return r; /* error */
3653 if (r == 0) goto greedy_check;
3654 else if (r == 2) { /* {n} */
3655 if (IS_SYNTAX_BV(syn, ONIG_SYN_FIXED_INTERVAL_IS_GREEDY_ONLY))
3656 goto possessive_check;
3658 goto greedy_check;
3660 /* r == 1 : normal char */
3661 break;
3663 case '|':
3664 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_VBAR_ALT)) break;
3665 tok->type = TK_ALT;
3666 break;
3668 case '(':
3669 if (PPEEK_IS('?') &&
3670 IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_QMARK_GROUP_EFFECT)) {
3671 PINC;
3672 if (PPEEK_IS('#')) {
3673 PFETCH(c);
3674 while (1) {
3675 if (PEND) return ONIGERR_END_PATTERN_IN_GROUP;
3676 PFETCH(c);
3677 if (c == MC_ESC(syn)) {
3678 if (!PEND) PFETCH(c);
3680 else {
3681 if (c == ')') break;
3684 goto start;
3686 PUNFETCH;
3689 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_LPAREN_SUBEXP)) break;
3690 tok->type = TK_SUBEXP_OPEN;
3691 break;
3693 case ')':
3694 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_LPAREN_SUBEXP)) break;
3695 tok->type = TK_SUBEXP_CLOSE;
3696 break;
3698 case '^':
3699 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_LINE_ANCHOR)) break;
3700 tok->type = TK_ANCHOR;
3701 tok->u.subtype = (IS_SINGLELINE(env->option)
3702 ? ANCHOR_BEGIN_BUF : ANCHOR_BEGIN_LINE);
3703 break;
3705 case '$':
3706 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_LINE_ANCHOR)) break;
3707 tok->type = TK_ANCHOR;
3708 tok->u.subtype = (IS_SINGLELINE(env->option)
3709 ? ANCHOR_SEMI_END_BUF : ANCHOR_END_LINE);
3710 break;
3712 case '[':
3713 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_BRACKET_CC)) break;
3714 tok->type = TK_CC_OPEN;
3715 break;
3717 case ']':
3718 if (*src > env->pattern) /* /].../ is allowed. */
3719 CLOSE_BRACKET_WITHOUT_ESC_WARN(env, (UChar* )"]");
3720 break;
3722 case '#':
3723 if (IS_EXTEND(env->option)) {
3724 while (!PEND) {
3725 PFETCH(c);
3726 if (ONIGENC_IS_CODE_NEWLINE(enc, c))
3727 break;
3729 goto start;
3730 break;
3732 break;
3734 case ' ': case '\t': case '\n': case '\r': case '\f':
3735 if (IS_EXTEND(env->option))
3736 goto start;
3737 break;
3739 default:
3740 /* string */
3741 break;
3745 #ifdef USE_VARIABLE_META_CHARS
3746 out:
3747 #endif
3748 *src = p;
3749 return tok->type;
3752 static int
3753 add_ctype_to_cc_by_range(CClassNode* cc, int ctype ARG_UNUSED, int not,
3754 OnigEncoding enc ARG_UNUSED,
3755 OnigCodePoint sb_out, const OnigCodePoint mbr[])
3757 int i, r;
3758 OnigCodePoint j;
3760 int n = ONIGENC_CODE_RANGE_NUM(mbr);
3762 if (not == 0) {
3763 for (i = 0; i < n; i++) {
3764 for (j = ONIGENC_CODE_RANGE_FROM(mbr, i);
3765 j <= ONIGENC_CODE_RANGE_TO(mbr, i); j++) {
3766 if (j >= sb_out) {
3767 if (j == ONIGENC_CODE_RANGE_TO(mbr, i)) i++;
3768 else if (j > ONIGENC_CODE_RANGE_FROM(mbr, i)) {
3769 r = add_code_range_to_buf(&(cc->mbuf), j,
3770 ONIGENC_CODE_RANGE_TO(mbr, i));
3771 if (r != 0) return r;
3772 i++;
3775 goto sb_end;
3777 BITSET_SET_BIT(cc->bs, j);
3781 sb_end:
3782 for ( ; i < n; i++) {
3783 r = add_code_range_to_buf(&(cc->mbuf),
3784 ONIGENC_CODE_RANGE_FROM(mbr, i),
3785 ONIGENC_CODE_RANGE_TO(mbr, i));
3786 if (r != 0) return r;
3789 else {
3790 OnigCodePoint prev = 0;
3792 for (i = 0; i < n; i++) {
3793 for (j = prev;
3794 j < ONIGENC_CODE_RANGE_FROM(mbr, i); j++) {
3795 if (j >= sb_out) {
3796 goto sb_end2;
3798 BITSET_SET_BIT(cc->bs, j);
3800 prev = ONIGENC_CODE_RANGE_TO(mbr, i) + 1;
3802 for (j = prev; j < sb_out; j++) {
3803 BITSET_SET_BIT(cc->bs, j);
3806 sb_end2:
3807 prev = sb_out;
3809 for (i = 0; i < n; i++) {
3810 if (prev < ONIGENC_CODE_RANGE_FROM(mbr, i)) {
3811 r = add_code_range_to_buf(&(cc->mbuf), prev,
3812 ONIGENC_CODE_RANGE_FROM(mbr, i) - 1);
3813 if (r != 0) return r;
3815 prev = ONIGENC_CODE_RANGE_TO(mbr, i) + 1;
3817 if (prev < 0x7fffffff) {
3818 r = add_code_range_to_buf(&(cc->mbuf), prev, 0x7fffffff);
3819 if (r != 0) return r;
3823 return 0;
3826 static int
3827 add_ctype_to_cc(CClassNode* cc, int ctype, int not, ScanEnv* env)
3829 int c, r;
3830 const OnigCodePoint *ranges;
3831 OnigCodePoint sb_out;
3832 OnigEncoding enc = env->enc;
3834 r = ONIGENC_GET_CTYPE_CODE_RANGE(enc, ctype, &sb_out, &ranges);
3835 if (r == 0) {
3836 return add_ctype_to_cc_by_range(cc, ctype, not, env->enc, sb_out, ranges);
3838 else if (r != ONIG_NO_SUPPORT_CONFIG) {
3839 return r;
3842 r = 0;
3843 switch (ctype) {
3844 case ONIGENC_CTYPE_ALPHA:
3845 case ONIGENC_CTYPE_BLANK:
3846 case ONIGENC_CTYPE_CNTRL:
3847 case ONIGENC_CTYPE_DIGIT:
3848 case ONIGENC_CTYPE_LOWER:
3849 case ONIGENC_CTYPE_PUNCT:
3850 case ONIGENC_CTYPE_SPACE:
3851 case ONIGENC_CTYPE_UPPER:
3852 case ONIGENC_CTYPE_XDIGIT:
3853 case ONIGENC_CTYPE_ASCII:
3854 case ONIGENC_CTYPE_ALNUM:
3855 if (not != 0) {
3856 for (c = 0; c < SINGLE_BYTE_SIZE; c++) {
3857 if (! ONIGENC_IS_CODE_CTYPE(enc, (OnigCodePoint )c, ctype))
3858 BITSET_SET_BIT(cc->bs, c);
3860 ADD_ALL_MULTI_BYTE_RANGE(enc, cc->mbuf);
3862 else {
3863 for (c = 0; c < SINGLE_BYTE_SIZE; c++) {
3864 if (ONIGENC_IS_CODE_CTYPE(enc, (OnigCodePoint )c, ctype))
3865 BITSET_SET_BIT(cc->bs, c);
3868 break;
3870 case ONIGENC_CTYPE_GRAPH:
3871 case ONIGENC_CTYPE_PRINT:
3872 if (not != 0) {
3873 for (c = 0; c < SINGLE_BYTE_SIZE; c++) {
3874 if (! ONIGENC_IS_CODE_CTYPE(enc, (OnigCodePoint )c, ctype))
3875 BITSET_SET_BIT(cc->bs, c);
3878 else {
3879 for (c = 0; c < SINGLE_BYTE_SIZE; c++) {
3880 if (ONIGENC_IS_CODE_CTYPE(enc, (OnigCodePoint )c, ctype))
3881 BITSET_SET_BIT(cc->bs, c);
3883 ADD_ALL_MULTI_BYTE_RANGE(enc, cc->mbuf);
3885 break;
3887 case ONIGENC_CTYPE_WORD:
3888 if (not == 0) {
3889 for (c = 0; c < SINGLE_BYTE_SIZE; c++) {
3890 if (IS_CODE_SB_WORD(enc, c)) BITSET_SET_BIT(cc->bs, c);
3892 ADD_ALL_MULTI_BYTE_RANGE(enc, cc->mbuf);
3894 else {
3895 for (c = 0; c < SINGLE_BYTE_SIZE; c++) {
3896 if ((ONIGENC_CODE_TO_MBCLEN(enc, c) > 0) /* check invalid code point */
3897 && ! ONIGENC_IS_CODE_WORD(enc, c))
3898 BITSET_SET_BIT(cc->bs, c);
3901 break;
3903 default:
3904 return ONIGERR_PARSER_BUG;
3905 break;
3908 return r;
3911 static int
3912 parse_posix_bracket(CClassNode* cc, UChar** src, UChar* end, ScanEnv* env)
3914 #define POSIX_BRACKET_CHECK_LIMIT_LENGTH 20
3915 #define POSIX_BRACKET_NAME_MIN_LEN 4
3917 static const PosixBracketEntryType PBS[] = {
3918 { (UChar* )"alnum", ONIGENC_CTYPE_ALNUM, 5 },
3919 { (UChar* )"alpha", ONIGENC_CTYPE_ALPHA, 5 },
3920 { (UChar* )"blank", ONIGENC_CTYPE_BLANK, 5 },
3921 { (UChar* )"cntrl", ONIGENC_CTYPE_CNTRL, 5 },
3922 { (UChar* )"digit", ONIGENC_CTYPE_DIGIT, 5 },
3923 { (UChar* )"graph", ONIGENC_CTYPE_GRAPH, 5 },
3924 { (UChar* )"lower", ONIGENC_CTYPE_LOWER, 5 },
3925 { (UChar* )"print", ONIGENC_CTYPE_PRINT, 5 },
3926 { (UChar* )"punct", ONIGENC_CTYPE_PUNCT, 5 },
3927 { (UChar* )"space", ONIGENC_CTYPE_SPACE, 5 },
3928 { (UChar* )"upper", ONIGENC_CTYPE_UPPER, 5 },
3929 { (UChar* )"xdigit", ONIGENC_CTYPE_XDIGIT, 6 },
3930 { (UChar* )"ascii", ONIGENC_CTYPE_ASCII, 5 },
3931 { (UChar* )"word", ONIGENC_CTYPE_WORD, 4 },
3932 { (UChar* )NULL, -1, 0 }
3935 const PosixBracketEntryType *pb;
3936 int not, i, r;
3937 OnigCodePoint c;
3938 OnigEncoding enc = env->enc;
3939 UChar *p = *src;
3940 PFETCH_READY;
3942 if (PPEEK_IS('^')) {
3943 PINC;
3944 not = 1;
3946 else
3947 not = 0;
3949 if (onigenc_strlen(enc, p, end) < POSIX_BRACKET_NAME_MIN_LEN + 3)
3950 goto not_posix_bracket;
3952 for (pb = PBS; IS_NOT_NULL(pb->name); pb++) {
3953 if (onigenc_with_ascii_strncmp(enc, p, end, pb->name, pb->len) == 0) {
3954 p = (UChar* )onigenc_step(enc, p, end, pb->len);
3955 if (onigenc_with_ascii_strncmp(enc, p, end, (UChar* )":]", 2) != 0)
3956 return ONIGERR_INVALID_POSIX_BRACKET_TYPE;
3958 r = add_ctype_to_cc(cc, pb->ctype, not, env);
3959 if (r != 0) return r;
3961 PINC; PINC;
3962 *src = p;
3963 return 0;
3967 not_posix_bracket:
3968 c = 0;
3969 i = 0;
3970 while (!PEND && ((c = PPEEK) != ':') && c != ']') {
3971 PINC;
3972 if (++i > POSIX_BRACKET_CHECK_LIMIT_LENGTH) break;
3974 if (c == ':' && ! PEND) {
3975 PINC;
3976 if (! PEND) {
3977 PFETCH(c);
3978 if (c == ']')
3979 return ONIGERR_INVALID_POSIX_BRACKET_TYPE;
3983 return 1; /* 1: is not POSIX bracket, but no error. */
3986 static int
3987 fetch_char_property_to_ctype(UChar** src, UChar* end, ScanEnv* env)
3989 int r;
3990 OnigCodePoint c;
3991 OnigEncoding enc = env->enc;
3992 UChar *prev, *start, *p = *src;
3993 PFETCH_READY;
3995 r = 0;
3996 start = prev = p;
3998 while (!PEND) {
3999 prev = p;
4000 PFETCH(c);
4001 if (c == '}') {
4002 r = ONIGENC_PROPERTY_NAME_TO_CTYPE(enc, start, prev);
4003 if (r < 0) break;
4005 *src = p;
4006 return r;
4008 else if (c == '(' || c == ')' || c == '{' || c == '|') {
4009 r = ONIGERR_INVALID_CHAR_PROPERTY_NAME;
4010 break;
4014 onig_scan_env_set_error_string(env, r, *src, prev);
4015 return r;
4018 static int
4019 parse_char_property(Node** np, OnigToken* tok, UChar** src, UChar* end,
4020 ScanEnv* env)
4022 int r, ctype;
4023 CClassNode* cc;
4025 ctype = fetch_char_property_to_ctype(src, end, env);
4026 if (ctype < 0) return ctype;
4028 *np = node_new_cclass();
4029 CHECK_NULL_RETURN_MEMERR(*np);
4030 cc = NCCLASS(*np);
4031 r = add_ctype_to_cc(cc, ctype, 0, env);
4032 if (r != 0) return r;
4033 if (tok->u.prop.not != 0) NCCLASS_SET_NOT(cc);
4035 return 0;
4039 enum CCSTATE {
4040 CCS_VALUE,
4041 CCS_RANGE,
4042 CCS_COMPLETE,
4043 CCS_START
4046 enum CCVALTYPE {
4047 CCV_SB,
4048 CCV_CODE_POINT,
4049 CCV_CLASS
4052 static int
4053 next_state_class(CClassNode* cc, OnigCodePoint* vs, enum CCVALTYPE* type,
4054 enum CCSTATE* state, ScanEnv* env)
4056 int r;
4058 if (*state == CCS_RANGE)
4059 return ONIGERR_CHAR_CLASS_VALUE_AT_END_OF_RANGE;
4061 if (*state == CCS_VALUE && *type != CCV_CLASS) {
4062 if (*type == CCV_SB)
4063 BITSET_SET_BIT(cc->bs, (int )(*vs));
4064 else if (*type == CCV_CODE_POINT) {
4065 r = add_code_range(&(cc->mbuf), env, *vs, *vs);
4066 if (r < 0) return r;
4070 *state = CCS_VALUE;
4071 *type = CCV_CLASS;
4072 return 0;
4075 static int
4076 next_state_val(CClassNode* cc, OnigCodePoint *vs, OnigCodePoint v,
4077 int* vs_israw, int v_israw,
4078 enum CCVALTYPE intype, enum CCVALTYPE* type,
4079 enum CCSTATE* state, ScanEnv* env)
4081 int r;
4083 switch (*state) {
4084 case CCS_VALUE:
4085 if (*type == CCV_SB)
4086 BITSET_SET_BIT(cc->bs, (int )(*vs));
4087 else if (*type == CCV_CODE_POINT) {
4088 r = add_code_range(&(cc->mbuf), env, *vs, *vs);
4089 if (r < 0) return r;
4091 break;
4093 case CCS_RANGE:
4094 if (intype == *type) {
4095 if (intype == CCV_SB) {
4096 if (*vs > 0xff || v > 0xff)
4097 return ONIGERR_INVALID_CODE_POINT_VALUE;
4099 if (*vs > v) {
4100 if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_ALLOW_EMPTY_RANGE_IN_CC))
4101 goto ccs_range_end;
4102 else
4103 return ONIGERR_EMPTY_RANGE_IN_CHAR_CLASS;
4105 bitset_set_range(cc->bs, (int )*vs, (int )v);
4107 else {
4108 r = add_code_range(&(cc->mbuf), env, *vs, v);
4109 if (r < 0) return r;
4112 else {
4113 #if 0
4114 if (intype == CCV_CODE_POINT && *type == CCV_SB) {
4115 #endif
4116 if (*vs > v) {
4117 if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_ALLOW_EMPTY_RANGE_IN_CC))
4118 goto ccs_range_end;
4119 else
4120 return ONIGERR_EMPTY_RANGE_IN_CHAR_CLASS;
4122 bitset_set_range(cc->bs, (int )*vs, (int )(v < 0xff ? v : 0xff));
4123 r = add_code_range(&(cc->mbuf), env, (OnigCodePoint )*vs, v);
4124 if (r < 0) return r;
4125 #if 0
4127 else
4128 return ONIGERR_MISMATCH_CODE_LENGTH_IN_CLASS_RANGE;
4129 #endif
4131 ccs_range_end:
4132 *state = CCS_COMPLETE;
4133 break;
4135 case CCS_COMPLETE:
4136 case CCS_START:
4137 *state = CCS_VALUE;
4138 break;
4140 default:
4141 break;
4144 *vs_israw = v_israw;
4145 *vs = v;
4146 *type = intype;
4147 return 0;
4150 static int
4151 code_exist_check(OnigCodePoint c, UChar* from, UChar* end, int ignore_escaped,
4152 ScanEnv* env)
4154 int in_esc;
4155 OnigCodePoint code;
4156 OnigEncoding enc = env->enc;
4157 UChar* p = from;
4158 PFETCH_READY;
4160 in_esc = 0;
4161 while (! PEND) {
4162 if (ignore_escaped && in_esc) {
4163 in_esc = 0;
4165 else {
4166 PFETCH(code);
4167 if (code == c) return 1;
4168 if (code == MC_ESC(env->syntax)) in_esc = 1;
4171 return 0;
4174 static int
4175 parse_char_class(Node** np, OnigToken* tok, UChar** src, UChar* end,
4176 ScanEnv* env)
4178 int r, neg, len, fetched, and_start;
4179 OnigCodePoint v, vs;
4180 UChar *p;
4181 Node* node;
4182 CClassNode *cc, *prev_cc;
4183 CClassNode work_cc;
4185 enum CCSTATE state;
4186 enum CCVALTYPE val_type, in_type;
4187 int val_israw, in_israw;
4189 prev_cc = (CClassNode* )NULL;
4190 *np = NULL_NODE;
4191 r = fetch_token_in_cc(tok, src, end, env);
4192 if (r == TK_CHAR && tok->u.c == '^' && tok->escaped == 0) {
4193 neg = 1;
4194 r = fetch_token_in_cc(tok, src, end, env);
4196 else {
4197 neg = 0;
4200 if (r < 0) return r;
4201 if (r == TK_CC_CLOSE) {
4202 if (! code_exist_check((OnigCodePoint )']',
4203 *src, env->pattern_end, 1, env))
4204 return ONIGERR_EMPTY_CHAR_CLASS;
4206 CC_ESC_WARN(env, (UChar* )"]");
4207 r = tok->type = TK_CHAR; /* allow []...] */
4210 *np = node = node_new_cclass();
4211 CHECK_NULL_RETURN_MEMERR(node);
4212 cc = NCCLASS(node);
4214 and_start = 0;
4215 state = CCS_START;
4216 p = *src;
4217 while (r != TK_CC_CLOSE) {
4218 fetched = 0;
4219 switch (r) {
4220 case TK_CHAR:
4221 len = ONIGENC_CODE_TO_MBCLEN(env->enc, tok->u.c);
4222 if (len > 1) {
4223 in_type = CCV_CODE_POINT;
4225 else if (len < 0) {
4226 r = len;
4227 goto err;
4229 else {
4230 sb_char:
4231 in_type = CCV_SB;
4233 v = (OnigCodePoint )tok->u.c;
4234 in_israw = 0;
4235 goto val_entry2;
4236 break;
4238 case TK_RAW_BYTE:
4239 /* tok->base != 0 : octal or hexadec. */
4240 if (! ONIGENC_IS_SINGLEBYTE(env->enc) && tok->base != 0) {
4241 UChar buf[ONIGENC_CODE_TO_MBC_MAXLEN];
4242 UChar* bufe = buf + ONIGENC_CODE_TO_MBC_MAXLEN;
4243 UChar* psave = p;
4244 int i, base = tok->base;
4246 buf[0] = tok->u.c;
4247 for (i = 1; i < ONIGENC_MBC_MAXLEN(env->enc); i++) {
4248 r = fetch_token_in_cc(tok, &p, end, env);
4249 if (r < 0) goto err;
4250 if (r != TK_RAW_BYTE || tok->base != base) {
4251 fetched = 1;
4252 break;
4254 buf[i] = tok->u.c;
4257 if (i < ONIGENC_MBC_MINLEN(env->enc)) {
4258 r = ONIGERR_TOO_SHORT_MULTI_BYTE_STRING;
4259 goto err;
4262 len = enclen(env->enc, buf, buf+i);
4263 if (i < len) {
4264 r = ONIGERR_TOO_SHORT_MULTI_BYTE_STRING;
4265 goto err;
4267 else if (i > len) { /* fetch back */
4268 p = psave;
4269 for (i = 1; i < len; i++) {
4270 r = fetch_token_in_cc(tok, &p, end, env);
4272 fetched = 0;
4275 if (i == 1) {
4276 v = (OnigCodePoint )buf[0];
4277 goto raw_single;
4279 else {
4280 v = ONIGENC_MBC_TO_CODE(env->enc, buf, bufe);
4281 in_type = CCV_CODE_POINT;
4284 else {
4285 v = (OnigCodePoint )tok->u.c;
4286 raw_single:
4287 in_type = CCV_SB;
4289 in_israw = 1;
4290 goto val_entry2;
4291 break;
4293 case TK_CODE_POINT:
4294 v = tok->u.code;
4295 in_israw = 1;
4296 val_entry:
4297 len = ONIGENC_CODE_TO_MBCLEN(env->enc, v);
4298 if (len < 0) {
4299 r = len;
4300 goto err;
4302 in_type = (len == 1 ? CCV_SB : CCV_CODE_POINT);
4303 val_entry2:
4304 r = next_state_val(cc, &vs, v, &val_israw, in_israw, in_type, &val_type,
4305 &state, env);
4306 if (r != 0) goto err;
4307 break;
4309 case TK_POSIX_BRACKET_OPEN:
4310 r = parse_posix_bracket(cc, &p, end, env);
4311 if (r < 0) goto err;
4312 if (r == 1) { /* is not POSIX bracket */
4313 CC_ESC_WARN(env, (UChar* )"[");
4314 p = tok->backp;
4315 v = (OnigCodePoint )tok->u.c;
4316 in_israw = 0;
4317 goto val_entry;
4319 goto next_class;
4320 break;
4322 case TK_CHAR_TYPE:
4323 r = add_ctype_to_cc(cc, tok->u.prop.ctype, tok->u.prop.not, env);
4324 if (r != 0) return r;
4326 next_class:
4327 r = next_state_class(cc, &vs, &val_type, &state, env);
4328 if (r != 0) goto err;
4329 break;
4331 case TK_CHAR_PROPERTY:
4333 int ctype;
4335 ctype = fetch_char_property_to_ctype(&p, end, env);
4336 if (ctype < 0) return ctype;
4337 r = add_ctype_to_cc(cc, ctype, tok->u.prop.not, env);
4338 if (r != 0) return r;
4339 goto next_class;
4341 break;
4343 case TK_CC_RANGE:
4344 if (state == CCS_VALUE) {
4345 r = fetch_token_in_cc(tok, &p, end, env);
4346 if (r < 0) goto err;
4347 fetched = 1;
4348 if (r == TK_CC_CLOSE) { /* allow [x-] */
4349 range_end_val:
4350 v = (OnigCodePoint )'-';
4351 in_israw = 0;
4352 goto val_entry;
4354 else if (r == TK_CC_AND) {
4355 CC_ESC_WARN(env, (UChar* )"-");
4356 goto range_end_val;
4358 state = CCS_RANGE;
4360 else if (state == CCS_START) {
4361 /* [-xa] is allowed */
4362 v = (OnigCodePoint )tok->u.c;
4363 in_israw = 0;
4365 r = fetch_token_in_cc(tok, &p, end, env);
4366 if (r < 0) goto err;
4367 fetched = 1;
4368 /* [--x] or [a&&-x] is warned. */
4369 if (r == TK_CC_RANGE || and_start != 0)
4370 CC_ESC_WARN(env, (UChar* )"-");
4372 goto val_entry;
4374 else if (state == CCS_RANGE) {
4375 CC_ESC_WARN(env, (UChar* )"-");
4376 goto sb_char; /* [!--x] is allowed */
4378 else { /* CCS_COMPLETE */
4379 r = fetch_token_in_cc(tok, &p, end, env);
4380 if (r < 0) goto err;
4381 fetched = 1;
4382 if (r == TK_CC_CLOSE) goto range_end_val; /* allow [a-b-] */
4383 else if (r == TK_CC_AND) {
4384 CC_ESC_WARN(env, (UChar* )"-");
4385 goto range_end_val;
4388 if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_ALLOW_DOUBLE_RANGE_OP_IN_CC)) {
4389 CC_ESC_WARN(env, (UChar* )"-");
4390 goto sb_char; /* [0-9-a] is allowed as [0-9\-a] */
4392 r = ONIGERR_UNMATCHED_RANGE_SPECIFIER_IN_CHAR_CLASS;
4393 goto err;
4395 break;
4397 case TK_CC_CC_OPEN: /* [ */
4399 Node *anode;
4400 CClassNode* acc;
4402 r = parse_char_class(&anode, tok, &p, end, env);
4403 if (r != 0) goto cc_open_err;
4404 acc = NCCLASS(anode);
4405 r = or_cclass(cc, acc, env->enc);
4407 onig_node_free(anode);
4408 cc_open_err:
4409 if (r != 0) goto err;
4411 break;
4413 case TK_CC_AND: /* && */
4415 if (state == CCS_VALUE) {
4416 r = next_state_val(cc, &vs, 0, &val_israw, 0, val_type,
4417 &val_type, &state, env);
4418 if (r != 0) goto err;
4420 /* initialize local variables */
4421 and_start = 1;
4422 state = CCS_START;
4424 if (IS_NOT_NULL(prev_cc)) {
4425 r = and_cclass(prev_cc, cc, env->enc);
4426 if (r != 0) goto err;
4427 bbuf_free(cc->mbuf);
4429 else {
4430 prev_cc = cc;
4431 cc = &work_cc;
4433 initialize_cclass(cc);
4435 break;
4437 case TK_EOT:
4438 r = ONIGERR_PREMATURE_END_OF_CHAR_CLASS;
4439 goto err;
4440 break;
4441 default:
4442 r = ONIGERR_PARSER_BUG;
4443 goto err;
4444 break;
4447 if (fetched)
4448 r = tok->type;
4449 else {
4450 r = fetch_token_in_cc(tok, &p, end, env);
4451 if (r < 0) goto err;
4455 if (state == CCS_VALUE) {
4456 r = next_state_val(cc, &vs, 0, &val_israw, 0, val_type,
4457 &val_type, &state, env);
4458 if (r != 0) goto err;
4461 if (IS_NOT_NULL(prev_cc)) {
4462 r = and_cclass(prev_cc, cc, env->enc);
4463 if (r != 0) goto err;
4464 bbuf_free(cc->mbuf);
4465 cc = prev_cc;
4468 if (neg != 0)
4469 NCCLASS_SET_NOT(cc);
4470 else
4471 NCCLASS_CLEAR_NOT(cc);
4472 if (IS_NCCLASS_NOT(cc) &&
4473 IS_SYNTAX_BV(env->syntax, ONIG_SYN_NOT_NEWLINE_IN_NEGATIVE_CC)) {
4474 int is_empty;
4476 is_empty = (IS_NULL(cc->mbuf) ? 1 : 0);
4477 if (is_empty != 0)
4478 BITSET_IS_EMPTY(cc->bs, is_empty);
4480 if (is_empty == 0) {
4481 #define NEWLINE_CODE 0x0a
4483 if (ONIGENC_IS_CODE_NEWLINE(env->enc, NEWLINE_CODE)) {
4484 if (ONIGENC_CODE_TO_MBCLEN(env->enc, NEWLINE_CODE) == 1)
4485 BITSET_SET_BIT(cc->bs, NEWLINE_CODE);
4486 else
4487 add_code_range(&(cc->mbuf), env, NEWLINE_CODE, NEWLINE_CODE);
4491 *src = p;
4492 return 0;
4494 err:
4495 if (cc != NCCLASS(*np))
4496 bbuf_free(cc->mbuf);
4497 onig_node_free(*np);
4498 return r;
4501 static int parse_subexp(Node** top, OnigToken* tok, int term,
4502 UChar** src, UChar* end, ScanEnv* env);
4504 static int
4505 parse_enclose(Node** np, OnigToken* tok, int term, UChar** src, UChar* end,
4506 ScanEnv* env)
4508 int r, num;
4509 Node *target;
4510 OnigOptionType option;
4511 OnigCodePoint c;
4512 OnigEncoding enc = env->enc;
4514 #ifdef USE_NAMED_GROUP
4515 int list_capture;
4516 #endif
4518 UChar* p = *src;
4519 PFETCH_READY;
4521 *np = NULL;
4522 if (PEND) return ONIGERR_END_PATTERN_WITH_UNMATCHED_PARENTHESIS;
4524 option = env->option;
4525 if (PPEEK_IS('?') &&
4526 IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_QMARK_GROUP_EFFECT)) {
4527 PINC;
4528 if (PEND) return ONIGERR_END_PATTERN_IN_GROUP;
4530 PFETCH(c);
4531 switch (c) {
4532 case ':': /* (?:...) grouping only */
4533 group:
4534 r = fetch_token(tok, &p, end, env);
4535 if (r < 0) return r;
4536 r = parse_subexp(np, tok, term, &p, end, env);
4537 if (r < 0) return r;
4538 *src = p;
4539 return 1; /* group */
4540 break;
4542 case '=':
4543 *np = onig_node_new_anchor(ANCHOR_PREC_READ);
4544 break;
4545 case '!': /* preceding read */
4546 *np = onig_node_new_anchor(ANCHOR_PREC_READ_NOT);
4547 break;
4548 case '>': /* (?>...) stop backtrack */
4549 *np = node_new_enclose(ENCLOSE_STOP_BACKTRACK);
4550 break;
4552 #ifdef USE_NAMED_GROUP
4553 case '\'':
4554 if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_QMARK_LT_NAMED_GROUP)) {
4555 goto named_group1;
4557 else
4558 return ONIGERR_UNDEFINED_GROUP_OPTION;
4559 break;
4560 #endif
4562 case '<': /* look behind (?<=...), (?<!...) */
4563 PFETCH(c);
4564 if (c == '=')
4565 *np = onig_node_new_anchor(ANCHOR_LOOK_BEHIND);
4566 else if (c == '!')
4567 *np = onig_node_new_anchor(ANCHOR_LOOK_BEHIND_NOT);
4568 #ifdef USE_NAMED_GROUP
4569 else {
4570 if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_QMARK_LT_NAMED_GROUP)) {
4571 UChar *name;
4572 UChar *name_end;
4574 PUNFETCH;
4575 c = '<';
4577 named_group1:
4578 list_capture = 0;
4580 named_group2:
4581 name = p;
4582 r = fetch_name((OnigCodePoint )c, &p, end, &name_end, env, &num, 0);
4583 if (r < 0) return r;
4585 num = scan_env_add_mem_entry(env);
4586 if (num < 0) return num;
4587 if (list_capture != 0 && num >= (int )BIT_STATUS_BITS_NUM)
4588 return ONIGERR_GROUP_NUMBER_OVER_FOR_CAPTURE_HISTORY;
4590 r = name_add(env->reg, name, name_end, num, env);
4591 if (r != 0) return r;
4592 *np = node_new_enclose_memory(env->option, 1);
4593 CHECK_NULL_RETURN_MEMERR(*np);
4594 NENCLOSE(*np)->regnum = num;
4595 if (list_capture != 0)
4596 BIT_STATUS_ON_AT_SIMPLE(env->capture_history, num);
4597 env->num_named++;
4599 else {
4600 return ONIGERR_UNDEFINED_GROUP_OPTION;
4603 #else
4604 else {
4605 return ONIGERR_UNDEFINED_GROUP_OPTION;
4607 #endif
4608 break;
4610 case '@':
4611 if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_ATMARK_CAPTURE_HISTORY)) {
4612 #ifdef USE_NAMED_GROUP
4613 if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_QMARK_LT_NAMED_GROUP)) {
4614 PFETCH(c);
4615 if (c == '<' || c == '\'') {
4616 list_capture = 1;
4617 goto named_group2; /* (?@<name>...) */
4619 PUNFETCH;
4621 #endif
4622 *np = node_new_enclose_memory(env->option, 0);
4623 CHECK_NULL_RETURN_MEMERR(*np);
4624 num = scan_env_add_mem_entry(env);
4625 if (num < 0) {
4626 onig_node_free(*np);
4627 return num;
4629 else if (num >= (int )BIT_STATUS_BITS_NUM) {
4630 onig_node_free(*np);
4631 return ONIGERR_GROUP_NUMBER_OVER_FOR_CAPTURE_HISTORY;
4633 NENCLOSE(*np)->regnum = num;
4634 BIT_STATUS_ON_AT_SIMPLE(env->capture_history, num);
4636 else {
4637 return ONIGERR_UNDEFINED_GROUP_OPTION;
4639 break;
4641 #ifdef USE_POSIXLINE_OPTION
4642 case 'p':
4643 #endif
4644 case '-': case 'i': case 'm': case 's': case 'x':
4646 int neg = 0;
4648 while (1) {
4649 switch (c) {
4650 case ':':
4651 case ')':
4652 break;
4654 case '-': neg = 1; break;
4655 case 'x': ONOFF(option, ONIG_OPTION_EXTEND, neg); break;
4656 case 'i': ONOFF(option, ONIG_OPTION_IGNORECASE, neg); break;
4657 case 's':
4658 if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_OPTION_PERL)) {
4659 ONOFF(option, ONIG_OPTION_MULTILINE, neg);
4661 else
4662 return ONIGERR_UNDEFINED_GROUP_OPTION;
4663 break;
4665 case 'm':
4666 if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_OPTION_PERL)) {
4667 ONOFF(option, ONIG_OPTION_SINGLELINE, (neg == 0 ? 1 : 0));
4669 else if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_OPTION_RUBY)) {
4670 ONOFF(option, ONIG_OPTION_MULTILINE, neg);
4672 else
4673 return ONIGERR_UNDEFINED_GROUP_OPTION;
4674 break;
4675 #ifdef USE_POSIXLINE_OPTION
4676 case 'p':
4677 ONOFF(option, ONIG_OPTION_MULTILINE|ONIG_OPTION_SINGLELINE, neg);
4678 break;
4679 #endif
4680 default:
4681 return ONIGERR_UNDEFINED_GROUP_OPTION;
4684 if (c == ')') {
4685 *np = node_new_option(option);
4686 CHECK_NULL_RETURN_MEMERR(*np);
4687 *src = p;
4688 return 2; /* option only */
4690 else if (c == ':') {
4691 OnigOptionType prev = env->option;
4693 env->option = option;
4694 r = fetch_token(tok, &p, end, env);
4695 if (r < 0) return r;
4696 r = parse_subexp(&target, tok, term, &p, end, env);
4697 env->option = prev;
4698 if (r < 0) return r;
4699 *np = node_new_option(option);
4700 CHECK_NULL_RETURN_MEMERR(*np);
4701 NENCLOSE(*np)->target = target;
4702 *src = p;
4703 return 0;
4706 if (PEND) return ONIGERR_END_PATTERN_IN_GROUP;
4707 PFETCH(c);
4710 break;
4712 default:
4713 return ONIGERR_UNDEFINED_GROUP_OPTION;
4716 else {
4717 if (ONIG_IS_OPTION_ON(env->option, ONIG_OPTION_DONT_CAPTURE_GROUP))
4718 goto group;
4720 *np = node_new_enclose_memory(env->option, 0);
4721 CHECK_NULL_RETURN_MEMERR(*np);
4722 num = scan_env_add_mem_entry(env);
4723 if (num < 0) return num;
4724 NENCLOSE(*np)->regnum = num;
4727 CHECK_NULL_RETURN_MEMERR(*np);
4728 r = fetch_token(tok, &p, end, env);
4729 if (r < 0) return r;
4730 r = parse_subexp(&target, tok, term, &p, end, env);
4731 if (r < 0) return r;
4733 if (NTYPE(*np) == NT_ANCHOR)
4734 NANCHOR(*np)->target = target;
4735 else {
4736 NENCLOSE(*np)->target = target;
4737 if (NENCLOSE(*np)->type == ENCLOSE_MEMORY) {
4738 /* Don't move this to previous of parse_subexp() */
4739 r = scan_env_set_mem_node(env, NENCLOSE(*np)->regnum, *np);
4740 if (r != 0) return r;
4744 *src = p;
4745 return 0;
4748 static const char* const PopularQStr[] = {
4749 "?", "*", "+", "??", "*?", "+?"
4752 static const char* const ReduceQStr[] = {
4753 "", "", "*", "*?", "??", "+ and ??", "+? and ?"
4756 static int
4757 set_quantifier(Node* qnode, Node* target, int group, ScanEnv* env)
4759 QtfrNode* qn;
4761 qn = NQTFR(qnode);
4762 if (qn->lower == 1 && qn->upper == 1) {
4763 return 1;
4766 switch (NTYPE(target)) {
4767 case NT_STR:
4768 if (! group) {
4769 StrNode* sn = NSTR(target);
4770 if (str_node_can_be_split(sn, env->enc)) {
4771 Node* n = str_node_split_last_char(sn, env->enc);
4772 if (IS_NOT_NULL(n)) {
4773 qn->target = n;
4774 return 2;
4778 break;
4780 case NT_QTFR:
4781 { /* check redundant double repeat. */
4782 /* verbose warn (?:.?)? etc... but not warn (.?)? etc... */
4783 QtfrNode* qnt = NQTFR(target);
4784 int nestq_num = popular_quantifier_num(qn);
4785 int targetq_num = popular_quantifier_num(qnt);
4787 #ifdef USE_WARNING_REDUNDANT_NESTED_REPEAT_OPERATOR
4788 if (!IS_QUANTIFIER_BY_NUMBER(qn) && !IS_QUANTIFIER_BY_NUMBER(qnt) &&
4789 IS_SYNTAX_BV(env->syntax, ONIG_SYN_WARN_REDUNDANT_NESTED_REPEAT)) {
4790 UChar buf[WARN_BUFSIZE];
4792 switch(ReduceTypeTable[targetq_num][nestq_num]) {
4793 case RQ_ASIS:
4794 break;
4796 case RQ_DEL:
4797 if (onig_verb_warn != onig_null_warn) {
4798 onig_snprintf_with_pattern(buf, WARN_BUFSIZE, env->enc,
4799 env->pattern, env->pattern_end,
4800 (UChar* )"redundant nested repeat operator");
4801 (*onig_verb_warn)((char* )buf);
4803 goto warn_exit;
4804 break;
4806 default:
4807 if (onig_verb_warn != onig_null_warn) {
4808 onig_snprintf_with_pattern(buf, WARN_BUFSIZE, env->enc,
4809 env->pattern, env->pattern_end,
4810 (UChar* )"nested repeat operator %s and %s was replaced with '%s'",
4811 PopularQStr[targetq_num], PopularQStr[nestq_num],
4812 ReduceQStr[ReduceTypeTable[targetq_num][nestq_num]]);
4813 (*onig_verb_warn)((char* )buf);
4815 goto warn_exit;
4816 break;
4820 warn_exit:
4821 #endif
4822 if (targetq_num >= 0) {
4823 if (nestq_num >= 0) {
4824 onig_reduce_nested_quantifier(qnode, target);
4825 goto q_exit;
4827 else if (targetq_num == 1 || targetq_num == 2) { /* * or + */
4828 /* (?:a*){n,m}, (?:a+){n,m} => (?:a*){n,n}, (?:a+){n,n} */
4829 if (! IS_REPEAT_INFINITE(qn->upper) && qn->upper > 1 && qn->greedy) {
4830 qn->upper = (qn->lower == 0 ? 1 : qn->lower);
4835 break;
4837 default:
4838 break;
4841 qn->target = target;
4842 q_exit:
4843 return 0;
4847 #ifdef USE_SHARED_CCLASS_TABLE
4849 #define THRESHOLD_RANGE_NUM_FOR_SHARE_CCLASS 8
4851 /* for ctype node hash table */
4853 typedef struct {
4854 OnigEncoding enc;
4855 int not;
4856 int type;
4857 } type_cclass_key;
4859 static int type_cclass_cmp(type_cclass_key* x, type_cclass_key* y)
4861 if (x->type != y->type) return 1;
4862 if (x->enc != y->enc) return 1;
4863 if (x->not != y->not) return 1;
4864 return 0;
4867 static int type_cclass_hash(type_cclass_key* key)
4869 int i, val;
4870 UChar *p;
4872 val = 0;
4874 p = (UChar* )&(key->enc);
4875 for (i = 0; i < (int )sizeof(key->enc); i++) {
4876 val = val * 997 + (int )*p++;
4879 p = (UChar* )(&key->type);
4880 for (i = 0; i < (int )sizeof(key->type); i++) {
4881 val = val * 997 + (int )*p++;
4884 val += key->not;
4885 return val + (val >> 5);
4888 static const struct st_hash_type type_type_cclass_hash = {
4889 type_cclass_cmp,
4890 type_cclass_hash,
4893 static st_table* OnigTypeCClassTable;
4896 static int
4897 i_free_shared_class(type_cclass_key* key, Node* node, void* arg ARG_UNUSED)
4899 if (IS_NOT_NULL(node)) {
4900 CClassNode* cc = NCCLASS(node);
4901 if (IS_NOT_NULL(cc->mbuf)) xfree(cc->mbuf);
4902 xfree(node);
4905 if (IS_NOT_NULL(key)) xfree(key);
4906 return ST_DELETE;
4909 extern int
4910 onig_free_shared_cclass_table(void)
4912 THREAD_ATOMIC_START;
4913 if (IS_NOT_NULL(OnigTypeCClassTable)) {
4914 onig_st_foreach(OnigTypeCClassTable, i_free_shared_class, 0);
4915 onig_st_free_table(OnigTypeCClassTable);
4916 OnigTypeCClassTable = NULL;
4918 THREAD_ATOMIC_END;
4920 return 0;
4923 #endif /* USE_SHARED_CCLASS_TABLE */
4926 #ifndef CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS
4927 static int
4928 clear_not_flag_cclass(CClassNode* cc, OnigEncoding enc)
4930 BBuf *tbuf;
4931 int r;
4933 if (IS_NCCLASS_NOT(cc)) {
4934 bitset_invert(cc->bs);
4936 if (! ONIGENC_IS_SINGLEBYTE(enc)) {
4937 r = not_code_range_buf(enc, cc->mbuf, &tbuf);
4938 if (r != 0) return r;
4940 bbuf_free(cc->mbuf);
4941 cc->mbuf = tbuf;
4944 NCCLASS_CLEAR_NOT(cc);
4947 return 0;
4949 #endif /* CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS */
4951 typedef struct {
4952 ScanEnv* env;
4953 CClassNode* cc;
4954 Node* alt_root;
4955 Node** ptail;
4956 } IApplyCaseFoldArg;
4958 static int
4959 i_apply_case_fold(OnigCodePoint from, OnigCodePoint to[],
4960 int to_len, void* arg)
4962 IApplyCaseFoldArg* iarg;
4963 ScanEnv* env;
4964 CClassNode* cc;
4965 BitSetRef bs;
4967 iarg = (IApplyCaseFoldArg* )arg;
4968 env = iarg->env;
4969 cc = iarg->cc;
4970 bs = cc->bs;
4972 if (to_len == 1) {
4973 int is_in = onig_is_code_in_cc(env->enc, from, cc);
4974 #ifdef CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS
4975 if ((is_in != 0 && !IS_NCCLASS_NOT(cc)) ||
4976 (is_in == 0 && IS_NCCLASS_NOT(cc))) {
4977 if (ONIGENC_MBC_MINLEN(env->enc) > 1 || *to >= SINGLE_BYTE_SIZE) {
4978 add_code_range(&(cc->mbuf), env, *to, *to);
4980 else {
4981 BITSET_SET_BIT(bs, *to);
4984 #else
4985 if (is_in != 0) {
4986 if (ONIGENC_MBC_MINLEN(env->enc) > 1 || *to >= SINGLE_BYTE_SIZE) {
4987 if (IS_NCCLASS_NOT(cc)) clear_not_flag_cclass(cc, env->enc);
4988 add_code_range(&(cc->mbuf), env, *to, *to);
4990 else {
4991 if (IS_NCCLASS_NOT(cc)) {
4992 BITSET_CLEAR_BIT(bs, *to);
4994 else
4995 BITSET_SET_BIT(bs, *to);
4998 #endif /* CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS */
5000 else {
5001 int r, i, len;
5002 UChar buf[ONIGENC_CODE_TO_MBC_MAXLEN];
5003 Node *snode = NULL_NODE;
5005 if (onig_is_code_in_cc(env->enc, from, cc)
5006 #ifdef CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS
5007 && !IS_NCCLASS_NOT(cc)
5008 #endif
5010 for (i = 0; i < to_len; i++) {
5011 len = ONIGENC_CODE_TO_MBC(env->enc, to[i], buf);
5012 if (i == 0) {
5013 snode = onig_node_new_str(buf, buf + len);
5014 CHECK_NULL_RETURN_MEMERR(snode);
5016 /* char-class expanded multi-char only
5017 compare with string folded at match time. */
5018 NSTRING_SET_AMBIG(snode);
5020 else {
5021 r = onig_node_str_cat(snode, buf, buf + len);
5022 if (r < 0) {
5023 onig_node_free(snode);
5024 return r;
5029 *(iarg->ptail) = onig_node_new_alt(snode, NULL_NODE);
5030 CHECK_NULL_RETURN_MEMERR(*(iarg->ptail));
5031 iarg->ptail = &(NCDR((*(iarg->ptail))));
5035 return 0;
5038 static int
5039 parse_exp(Node** np, OnigToken* tok, int term,
5040 UChar** src, UChar* end, ScanEnv* env)
5042 int r, len, group = 0;
5043 Node* qn;
5044 Node** targetp;
5046 *np = NULL;
5047 if (tok->type == (enum TokenSyms )term)
5048 goto end_of_token;
5050 switch (tok->type) {
5051 case TK_ALT:
5052 case TK_EOT:
5053 end_of_token:
5054 *np = node_new_empty();
5055 return tok->type;
5056 break;
5058 case TK_SUBEXP_OPEN:
5059 r = parse_enclose(np, tok, TK_SUBEXP_CLOSE, src, end, env);
5060 if (r < 0) return r;
5061 if (r == 1) group = 1;
5062 else if (r == 2) { /* option only */
5063 Node* target;
5064 OnigOptionType prev = env->option;
5066 env->option = NENCLOSE(*np)->option;
5067 r = fetch_token(tok, src, end, env);
5068 if (r < 0) return r;
5069 r = parse_subexp(&target, tok, term, src, end, env);
5070 env->option = prev;
5071 if (r < 0) return r;
5072 NENCLOSE(*np)->target = target;
5073 return tok->type;
5075 break;
5077 case TK_SUBEXP_CLOSE:
5078 if (! IS_SYNTAX_BV(env->syntax, ONIG_SYN_ALLOW_UNMATCHED_CLOSE_SUBEXP))
5079 return ONIGERR_UNMATCHED_CLOSE_PARENTHESIS;
5081 if (tok->escaped) goto tk_raw_byte;
5082 else goto tk_byte;
5083 break;
5085 case TK_STRING:
5086 tk_byte:
5088 *np = node_new_str(tok->backp, *src);
5089 CHECK_NULL_RETURN_MEMERR(*np);
5091 while (1) {
5092 r = fetch_token(tok, src, end, env);
5093 if (r < 0) return r;
5094 if (r != TK_STRING) break;
5096 r = onig_node_str_cat(*np, tok->backp, *src);
5097 if (r < 0) return r;
5100 string_end:
5101 targetp = np;
5102 goto repeat;
5104 break;
5106 case TK_RAW_BYTE:
5107 tk_raw_byte:
5109 *np = node_new_str_raw_char((UChar )tok->u.c);
5110 CHECK_NULL_RETURN_MEMERR(*np);
5111 len = 1;
5112 while (1) {
5113 if (len >= ONIGENC_MBC_MINLEN(env->enc)) {
5114 if (len == enclen(env->enc, NSTR(*np)->s, NSTR(*np)->end)) {
5115 r = fetch_token(tok, src, end, env);
5116 NSTRING_CLEAR_RAW(*np);
5117 goto string_end;
5121 r = fetch_token(tok, src, end, env);
5122 if (r < 0) return r;
5123 if (r != TK_RAW_BYTE) {
5124 /* Don't use this, it is wrong for little endian encodings. */
5125 #ifdef USE_PAD_TO_SHORT_BYTE_CHAR
5126 int rem;
5127 if (len < ONIGENC_MBC_MINLEN(env->enc)) {
5128 rem = ONIGENC_MBC_MINLEN(env->enc) - len;
5129 (void )node_str_head_pad(NSTR(*np), rem, (UChar )0);
5130 if (len + rem == enclen(env->enc, NSTR(*np)->s)) {
5131 NSTRING_CLEAR_RAW(*np);
5132 goto string_end;
5135 #endif
5136 return ONIGERR_TOO_SHORT_MULTI_BYTE_STRING;
5139 r = node_str_cat_char(*np, (UChar )tok->u.c);
5140 if (r < 0) return r;
5142 len++;
5145 break;
5147 case TK_CODE_POINT:
5149 UChar buf[ONIGENC_CODE_TO_MBC_MAXLEN];
5150 int num = ONIGENC_CODE_TO_MBC(env->enc, tok->u.code, buf);
5151 if (num < 0) return num;
5152 #ifdef NUMBERED_CHAR_IS_NOT_CASE_AMBIG
5153 *np = node_new_str_raw(buf, buf + num);
5154 #else
5155 *np = node_new_str(buf, buf + num);
5156 #endif
5157 CHECK_NULL_RETURN_MEMERR(*np);
5159 break;
5161 case TK_QUOTE_OPEN:
5163 OnigCodePoint end_op[2];
5164 UChar *qstart, *qend, *nextp;
5166 end_op[0] = (OnigCodePoint )MC_ESC(env->syntax);
5167 end_op[1] = (OnigCodePoint )'E';
5168 qstart = *src;
5169 qend = find_str_position(end_op, 2, qstart, end, &nextp, env->enc);
5170 if (IS_NULL(qend)) {
5171 nextp = qend = end;
5173 *np = node_new_str(qstart, qend);
5174 CHECK_NULL_RETURN_MEMERR(*np);
5175 *src = nextp;
5177 break;
5179 case TK_CHAR_TYPE:
5181 switch (tok->u.prop.ctype) {
5182 case ONIGENC_CTYPE_WORD:
5183 *np = node_new_ctype(tok->u.prop.ctype, tok->u.prop.not);
5184 CHECK_NULL_RETURN_MEMERR(*np);
5185 break;
5187 case ONIGENC_CTYPE_SPACE:
5188 case ONIGENC_CTYPE_DIGIT:
5189 case ONIGENC_CTYPE_XDIGIT:
5191 CClassNode* cc;
5193 #ifdef USE_SHARED_CCLASS_TABLE
5194 const OnigCodePoint *mbr;
5195 OnigCodePoint sb_out;
5197 r = ONIGENC_GET_CTYPE_CODE_RANGE(env->enc, tok->u.prop.ctype,
5198 &sb_out, &mbr);
5199 if (r == 0 &&
5200 ONIGENC_CODE_RANGE_NUM(mbr)
5201 >= THRESHOLD_RANGE_NUM_FOR_SHARE_CCLASS) {
5202 type_cclass_key key;
5203 type_cclass_key* new_key;
5205 key.enc = env->enc;
5206 key.not = tok->u.prop.not;
5207 key.type = tok->u.prop.ctype;
5209 THREAD_ATOMIC_START;
5211 if (IS_NULL(OnigTypeCClassTable)) {
5212 OnigTypeCClassTable
5213 = onig_st_init_table_with_size(&type_type_cclass_hash, 10);
5214 if (IS_NULL(OnigTypeCClassTable)) {
5215 THREAD_ATOMIC_END;
5216 return ONIGERR_MEMORY;
5219 else {
5220 if (onig_st_lookup(OnigTypeCClassTable, (st_data_t )&key,
5221 (st_data_t* )np)) {
5222 THREAD_ATOMIC_END;
5223 break;
5227 *np = node_new_cclass_by_codepoint_range(tok->u.prop.not,
5228 sb_out, mbr);
5229 if (IS_NULL(*np)) {
5230 THREAD_ATOMIC_END;
5231 return ONIGERR_MEMORY;
5234 cc = NCCLASS(*np);
5235 NCCLASS_SET_SHARE(cc);
5236 new_key = (type_cclass_key* )xmalloc(sizeof(type_cclass_key));
5237 xmemcpy(new_key, &key, sizeof(type_cclass_key));
5238 onig_st_add_direct(OnigTypeCClassTable, (st_data_t )new_key,
5239 (st_data_t )*np);
5241 THREAD_ATOMIC_END;
5243 else {
5244 #endif
5245 *np = node_new_cclass();
5246 CHECK_NULL_RETURN_MEMERR(*np);
5247 cc = NCCLASS(*np);
5248 add_ctype_to_cc(cc, tok->u.prop.ctype, 0, env);
5249 if (tok->u.prop.not != 0) NCCLASS_SET_NOT(cc);
5250 #ifdef USE_SHARED_CCLASS_TABLE
5252 #endif
5254 break;
5256 default:
5257 return ONIGERR_PARSER_BUG;
5258 break;
5261 break;
5263 case TK_CHAR_PROPERTY:
5264 r = parse_char_property(np, tok, src, end, env);
5265 if (r != 0) return r;
5266 break;
5268 case TK_CC_OPEN:
5270 CClassNode* cc;
5272 r = parse_char_class(np, tok, src, end, env);
5273 if (r != 0) return r;
5275 cc = NCCLASS(*np);
5276 if (IS_IGNORECASE(env->option)) {
5277 IApplyCaseFoldArg iarg;
5279 iarg.env = env;
5280 iarg.cc = cc;
5281 iarg.alt_root = NULL_NODE;
5282 iarg.ptail = &(iarg.alt_root);
5284 r = ONIGENC_APPLY_ALL_CASE_FOLD(env->enc, env->case_fold_flag,
5285 i_apply_case_fold, &iarg);
5286 if (r != 0) {
5287 onig_node_free(iarg.alt_root);
5288 return r;
5290 if (IS_NOT_NULL(iarg.alt_root)) {
5291 Node* work = onig_node_new_alt(*np, iarg.alt_root);
5292 if (IS_NULL(work)) {
5293 onig_node_free(iarg.alt_root);
5294 return ONIGERR_MEMORY;
5296 *np = work;
5300 break;
5302 case TK_ANYCHAR:
5303 *np = node_new_anychar();
5304 CHECK_NULL_RETURN_MEMERR(*np);
5305 break;
5307 case TK_ANYCHAR_ANYTIME:
5308 *np = node_new_anychar();
5309 CHECK_NULL_RETURN_MEMERR(*np);
5310 qn = node_new_quantifier(0, REPEAT_INFINITE, 0);
5311 CHECK_NULL_RETURN_MEMERR(qn);
5312 NQTFR(qn)->target = *np;
5313 *np = qn;
5314 break;
5316 case TK_BACKREF:
5317 len = tok->u.backref.num;
5318 *np = node_new_backref(len,
5319 (len > 1 ? tok->u.backref.refs : &(tok->u.backref.ref1)),
5320 tok->u.backref.by_name,
5321 #ifdef USE_BACKREF_WITH_LEVEL
5322 tok->u.backref.exist_level,
5323 tok->u.backref.level,
5324 #endif
5325 env);
5326 CHECK_NULL_RETURN_MEMERR(*np);
5327 break;
5329 #ifdef USE_SUBEXP_CALL
5330 case TK_CALL:
5332 int gnum = tok->u.call.gnum;
5334 if (gnum < 0) {
5335 gnum = BACKREF_REL_TO_ABS(gnum, env);
5336 if (gnum <= 0)
5337 return ONIGERR_INVALID_BACKREF;
5339 *np = node_new_call(tok->u.call.name, tok->u.call.name_end, gnum);
5340 CHECK_NULL_RETURN_MEMERR(*np);
5341 env->num_call++;
5343 break;
5344 #endif
5346 case TK_ANCHOR:
5347 *np = onig_node_new_anchor(tok->u.anchor);
5348 break;
5350 case TK_OP_REPEAT:
5351 case TK_INTERVAL:
5352 if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_CONTEXT_INDEP_REPEAT_OPS)) {
5353 if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_CONTEXT_INVALID_REPEAT_OPS))
5354 return ONIGERR_TARGET_OF_REPEAT_OPERATOR_NOT_SPECIFIED;
5355 else
5356 *np = node_new_empty();
5358 else {
5359 goto tk_byte;
5361 break;
5363 default:
5364 return ONIGERR_PARSER_BUG;
5365 break;
5369 targetp = np;
5371 re_entry:
5372 r = fetch_token(tok, src, end, env);
5373 if (r < 0) return r;
5375 repeat:
5376 if (r == TK_OP_REPEAT || r == TK_INTERVAL) {
5377 if (is_invalid_quantifier_target(*targetp))
5378 return ONIGERR_TARGET_OF_REPEAT_OPERATOR_INVALID;
5380 qn = node_new_quantifier(tok->u.repeat.lower, tok->u.repeat.upper,
5381 (r == TK_INTERVAL ? 1 : 0));
5382 CHECK_NULL_RETURN_MEMERR(qn);
5383 NQTFR(qn)->greedy = tok->u.repeat.greedy;
5384 r = set_quantifier(qn, *targetp, group, env);
5385 if (r < 0) return r;
5387 if (tok->u.repeat.possessive != 0) {
5388 Node* en;
5389 en = node_new_enclose(ENCLOSE_STOP_BACKTRACK);
5390 CHECK_NULL_RETURN_MEMERR(en);
5391 NENCLOSE(en)->target = qn;
5392 qn = en;
5395 if (r == 0) {
5396 *targetp = qn;
5398 else if (r == 1) {
5399 onig_node_free(qn);
5401 else if (r == 2) { /* split case: /abc+/ */
5402 Node *tmp;
5404 *targetp = node_new_list(*targetp, NULL);
5405 CHECK_NULL_RETURN_MEMERR(*targetp);
5406 tmp = NCDR(*targetp) = node_new_list(qn, NULL);
5407 CHECK_NULL_RETURN_MEMERR(tmp);
5408 targetp = &(NCAR(tmp));
5410 goto re_entry;
5414 return r;
5417 static int
5418 parse_branch(Node** top, OnigToken* tok, int term,
5419 UChar** src, UChar* end, ScanEnv* env)
5421 int r;
5422 Node *node, **headp;
5424 *top = NULL;
5425 r = parse_exp(&node, tok, term, src, end, env);
5426 if (r < 0) return r;
5428 if (r == TK_EOT || r == term || r == TK_ALT) {
5429 *top = node;
5431 else {
5432 *top = node_new_list(node, NULL);
5433 headp = &(NCDR(*top));
5434 while (r != TK_EOT && r != term && r != TK_ALT) {
5435 r = parse_exp(&node, tok, term, src, end, env);
5436 if (r < 0) return r;
5438 if (NTYPE(node) == NT_LIST) {
5439 *headp = node;
5440 while (IS_NOT_NULL(NCDR(node))) node = NCDR(node);
5441 headp = &(NCDR(node));
5443 else {
5444 *headp = node_new_list(node, NULL);
5445 headp = &(NCDR(*headp));
5450 return r;
5453 /* term_tok: TK_EOT or TK_SUBEXP_CLOSE */
5454 static int
5455 parse_subexp(Node** top, OnigToken* tok, int term,
5456 UChar** src, UChar* end, ScanEnv* env)
5458 int r;
5459 Node *node, **headp;
5461 *top = NULL;
5462 r = parse_branch(&node, tok, term, src, end, env);
5463 if (r < 0) {
5464 onig_node_free(node);
5465 return r;
5468 if (r == term) {
5469 *top = node;
5471 else if (r == TK_ALT) {
5472 *top = onig_node_new_alt(node, NULL);
5473 headp = &(NCDR(*top));
5474 while (r == TK_ALT) {
5475 r = fetch_token(tok, src, end, env);
5476 if (r < 0) return r;
5477 r = parse_branch(&node, tok, term, src, end, env);
5478 if (r < 0) return r;
5480 *headp = onig_node_new_alt(node, NULL);
5481 headp = &(NCDR(*headp));
5484 if (tok->type != (enum TokenSyms )term)
5485 goto err;
5487 else {
5488 err:
5489 if (term == TK_SUBEXP_CLOSE)
5490 return ONIGERR_END_PATTERN_WITH_UNMATCHED_PARENTHESIS;
5491 else
5492 return ONIGERR_PARSER_BUG;
5495 return r;
5498 static int
5499 parse_regexp(Node** top, UChar** src, UChar* end, ScanEnv* env)
5501 int r;
5502 OnigToken tok;
5504 r = fetch_token(&tok, src, end, env);
5505 if (r < 0) return r;
5506 r = parse_subexp(top, &tok, TK_EOT, src, end, env);
5507 if (r < 0) return r;
5508 return 0;
5511 extern int
5512 onig_parse_make_tree(Node** root, const UChar* pattern, const UChar* end,
5513 regex_t* reg, ScanEnv* env)
5515 int r;
5516 UChar* p;
5518 #ifdef USE_NAMED_GROUP
5519 names_clear(reg);
5520 #endif
5522 scan_env_clear(env);
5523 env->option = reg->options;
5524 env->case_fold_flag = reg->case_fold_flag;
5525 env->enc = reg->enc;
5526 env->syntax = reg->syntax;
5527 env->pattern = (UChar* )pattern;
5528 env->pattern_end = (UChar* )end;
5529 env->reg = reg;
5531 *root = NULL;
5532 p = (UChar* )pattern;
5533 r = parse_regexp(root, &p, (UChar* )end, env);
5534 reg->num_mem = env->num_mem;
5535 return r;
5538 extern void
5539 onig_scan_env_set_error_string(ScanEnv* env, int ecode ARG_UNUSED,
5540 UChar* arg, UChar* arg_end)
5542 env->error = arg;
5543 env->error_end = arg_end;