Ruby: regparse.c Source File

Ruby 3.3.4p94 (2024-07-09 revision be1089c8ec5ba40e09b1553e36b3174bf4014d9d)

regparse.c
1/**********************************************************************
2 regparse.c - Onigmo (Oniguruma-mod) (regular expression library)
3**********************************************************************/
4/*-
5 * Copyright (c) 2002-2008 K.Kosako <sndgk393 AT ybb DOT ne DOT jp>
6 * Copyright (c) 2011-2016 K.Takata <kentkt AT csc DOT jp>
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31// Suppress some false-positive compiler warnings
32#if defined(__GNUC__) && __GNUC__ >= 12
33#pragma GCC diagnostic ignored "-Warray-bounds"
34#pragma GCC diagnostic ignored "-Wstringop-overflow"
35#pragma GCC diagnostic ignored "-Wrestrict"
36#endif
37
38#include "regparse.h"
39#include <stdarg.h>
40#include "internal/sanitizers.h"
41
42#define WARN_BUFSIZE 256
43
44#define CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS
45
46
47const OnigSyntaxType OnigSyntaxRuby = {
48 (( SYN_GNU_REGEX_OP | ONIG_SYN_OP_QMARK_NON_GREEDY |
49 ONIG_SYN_OP_ESC_OCTAL3 | ONIG_SYN_OP_ESC_X_HEX2 |
50 ONIG_SYN_OP_ESC_X_BRACE_HEX8 | ONIG_SYN_OP_ESC_CONTROL_CHARS |
51 ONIG_SYN_OP_ESC_C_CONTROL )
52 & ~ONIG_SYN_OP_ESC_LTGT_WORD_BEGIN_END )
53 , ( ONIG_SYN_OP2_QMARK_GROUP_EFFECT |
54 ONIG_SYN_OP2_OPTION_RUBY |
55 ONIG_SYN_OP2_QMARK_LT_NAMED_GROUP | ONIG_SYN_OP2_ESC_K_NAMED_BACKREF |
56 ONIG_SYN_OP2_ESC_G_SUBEXP_CALL |
57 ONIG_SYN_OP2_ESC_P_BRACE_CHAR_PROPERTY |
58 ONIG_SYN_OP2_ESC_P_BRACE_CIRCUMFLEX_NOT |
59 ONIG_SYN_OP2_PLUS_POSSESSIVE_REPEAT |
60 ONIG_SYN_OP2_CCLASS_SET_OP | ONIG_SYN_OP2_ESC_CAPITAL_C_BAR_CONTROL |
61 ONIG_SYN_OP2_ESC_CAPITAL_M_BAR_META | ONIG_SYN_OP2_ESC_V_VTAB |
62 ONIG_SYN_OP2_ESC_H_XDIGIT |
63#ifndef RUBY
64 ONIG_SYN_OP2_ESC_U_HEX4 |
65#endif
66 ONIG_SYN_OP2_ESC_CAPITAL_X_EXTENDED_GRAPHEME_CLUSTER |
67 ONIG_SYN_OP2_QMARK_LPAREN_CONDITION |
68 ONIG_SYN_OP2_ESC_CAPITAL_R_LINEBREAK |
69 ONIG_SYN_OP2_ESC_CAPITAL_K_KEEP |
70 ONIG_SYN_OP2_QMARK_TILDE_ABSENT )
71 , ( SYN_GNU_REGEX_BV |
72 ONIG_SYN_ALLOW_INTERVAL_LOW_ABBREV |
73 ONIG_SYN_DIFFERENT_LEN_ALT_LOOK_BEHIND |
74 ONIG_SYN_CAPTURE_ONLY_NAMED_GROUP |
75 ONIG_SYN_ALLOW_MULTIPLEX_DEFINITION_NAME |
76 ONIG_SYN_FIXED_INTERVAL_IS_GREEDY_ONLY |
77 ONIG_SYN_WARN_CC_OP_NOT_ESCAPED |
78 ONIG_SYN_WARN_CC_DUP |
79 ONIG_SYN_WARN_REDUNDANT_NESTED_REPEAT )
80 , ( ONIG_OPTION_ASCII_RANGE | ONIG_OPTION_POSIX_BRACKET_ALL_RANGE |
81 ONIG_OPTION_WORD_BOUND_ALL_RANGE )
82 ,
83 {
84 (OnigCodePoint )'\\' /* esc */
85 , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* anychar '.' */
86 , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* anytime '*' */
87 , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* zero or one time '?' */
88 , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* one or more time '+' */
89 , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* anychar anytime */
90 }
91};
92
93const OnigSyntaxType* OnigDefaultSyntax = ONIG_SYNTAX_RUBY;
94
95extern void onig_null_warn(const char* s ARG_UNUSED) { }
96
97#ifdef DEFAULT_WARN_FUNCTION
98static OnigWarnFunc onig_warn = (OnigWarnFunc )DEFAULT_WARN_FUNCTION;
99#else
100static OnigWarnFunc onig_warn = onig_null_warn;
101#endif
102
103#ifdef DEFAULT_VERB_WARN_FUNCTION
104static OnigWarnFunc onig_verb_warn = (OnigWarnFunc )DEFAULT_VERB_WARN_FUNCTION;
105#else
106static OnigWarnFunc onig_verb_warn = onig_null_warn;
107#endif
108
109extern void onig_set_warn_func(OnigWarnFunc f)
110{
111 onig_warn = f;
112}
113
114extern void onig_set_verb_warn_func(OnigWarnFunc f)
115{
116 onig_verb_warn = f;
117}
118
119static void CC_DUP_WARN(ScanEnv *env, OnigCodePoint from, OnigCodePoint to);
120
121
122static unsigned int ParseDepthLimit = DEFAULT_PARSE_DEPTH_LIMIT;
123
124extern unsigned int
125onig_get_parse_depth_limit(void)
126{
127 return ParseDepthLimit;
128}
129
130extern int
131onig_set_parse_depth_limit(unsigned int depth)
132{
133 if (depth == 0)
134 ParseDepthLimit = DEFAULT_PARSE_DEPTH_LIMIT;
135 else
136 ParseDepthLimit = depth;
137 return 0;
138}
139
140
141static void
142bbuf_free(BBuf* bbuf)
143{
144 if (IS_NOT_NULL(bbuf)) {
145 xfree(bbuf->p);
146 xfree(bbuf);
147 }
148}
149
150static int
151bbuf_clone(BBuf** rto, BBuf* from)
152{
153 int r;
154 BBuf *to;
155
156 *rto = to = (BBuf* )xmalloc(sizeof(BBuf));
157 CHECK_NULL_RETURN_MEMERR(to);
158 r = BBUF_INIT(to, from->alloc);
159 if (r != 0) return r;
160 to->used = from->used;
161 xmemcpy(to->p, from->p, from->used);
162 return 0;
163}
164
165#define BACKREF_REL_TO_ABS(rel_no, env) \
166 ((env)->num_mem + 1 + (rel_no))
167
168#define ONOFF(v,f,negative) (negative) ? ((v) &= ~(f)) : ((v) |= (f))
169
170#define MBCODE_START_POS(enc) \
171 (OnigCodePoint )(ONIGENC_MBC_MINLEN(enc) > 1 ? 0 : 0x80)
172
173#define SET_ALL_MULTI_BYTE_RANGE(enc, pbuf) \
174 add_code_range_to_buf(pbuf, env, MBCODE_START_POS(enc), ONIG_LAST_CODE_POINT)
175
176#define ADD_ALL_MULTI_BYTE_RANGE(enc, mbuf) do {\
177 if (! ONIGENC_IS_SINGLEBYTE(enc)) {\
178 r = SET_ALL_MULTI_BYTE_RANGE(enc, &(mbuf));\
179 if (r) return r;\
180 }\
181} while (0)
182
183
184#define BITSET_SET_BIT_CHKDUP(bs, pos) do { \
185 if (BITSET_AT(bs, pos)) CC_DUP_WARN(env, pos, pos); \
186 BS_ROOM(bs, pos) |= BS_BIT(pos); \
187} while (0)
188
189#define BITSET_IS_EMPTY(bs,empty) do {\
190 int i;\
191 empty = 1;\
192 for (i = 0; i < BITSET_SIZE; i++) {\
193 if ((bs)[i] != 0) {\
194 empty = 0; break;\
195 }\
196 }\
197} while (0)
198
199static void
200bitset_set_range(ScanEnv *env, BitSetRef bs, int from, int to)
201{
202 int i;
203 for (i = from; i <= to && i < SINGLE_BYTE_SIZE; i++) {
204 BITSET_SET_BIT_CHKDUP(bs, i);
205 }
206}
207
208#if 0
209static void
210bitset_set_all(BitSetRef bs)
211{
212 int i;
213 for (i = 0; i < BITSET_SIZE; i++) { bs[i] = ~((Bits )0); }
214}
215#endif
216
217static void
218bitset_invert(BitSetRef bs)
219{
220 int i;
221 for (i = 0; i < BITSET_SIZE; i++) { bs[i] = ~(bs[i]); }
222}
223
224static void
225bitset_invert_to(BitSetRef from, BitSetRef to)
226{
227 int i;
228 for (i = 0; i < BITSET_SIZE; i++) { to[i] = ~(from[i]); }
229}
230
231static void
232bitset_and(BitSetRef dest, BitSetRef bs)
233{
234 int i;
235 for (i = 0; i < BITSET_SIZE; i++) { dest[i] &= bs[i]; }
236}
237
238static void
239bitset_or(BitSetRef dest, BitSetRef bs)
240{
241 int i;
242 for (i = 0; i < BITSET_SIZE; i++) { dest[i] |= bs[i]; }
243}
244
245static void
246bitset_copy(BitSetRef dest, BitSetRef bs)
247{
248 int i;
249 for (i = 0; i < BITSET_SIZE; i++) { dest[i] = bs[i]; }
250}
251
252#if defined(USE_NAMED_GROUP) && !defined(USE_ST_LIBRARY)
253extern int
254onig_strncmp(const UChar* s1, const UChar* s2, size_t n)
255{
256 int x;
257
258 while (n-- > 0) {
259 x = *s2++ - *s1++;
260 if (x) return x;
261 }
262 return 0;
263}
264#endif
265
266extern void
267onig_strcpy(UChar* dest, const UChar* src, const UChar* end)
268{
269 ptrdiff_t len = end - src;
270 if (len > 0) {
271 xmemcpy(dest, src, len);
272 dest[len] = (UChar )0;
273 }
274}
275
276#ifdef USE_NAMED_GROUP
277static UChar*
278strdup_with_null(OnigEncoding enc, const UChar* s, const UChar* end)
279{
280 ptrdiff_t slen;
281 int term_len, i;
282 UChar *r;
283
284 slen = end - s;
285 term_len = ONIGENC_MBC_MINLEN(enc);
286
287 r = (UChar* )xmalloc(slen + term_len);
288 CHECK_NULL_RETURN(r);
289 xmemcpy(r, s, slen);
290
291 for (i = 0; i < term_len; i++)
292 r[slen + i] = (UChar )0;
293
294 return r;
295}
296#endif
297
298/* scan pattern methods */
299#define PEND_VALUE 0
300
301#ifdef __GNUC__
302/* get rid of Wunused-but-set-variable and Wuninitialized */
303# define PFETCH_READY UChar* pfetch_prev = NULL; (void)pfetch_prev
304#else
305# define PFETCH_READY UChar* pfetch_prev
306#endif
307#define PEND (p < end ? 0 : 1)
308#define PUNFETCH p = pfetch_prev
309#define PINC do { \
310 pfetch_prev = p; \
311 p += enclen(enc, p, end); \
312} while (0)
313#define PFETCH(c) do { \
314 c = ((enc->max_enc_len == 1) ? *p : ONIGENC_MBC_TO_CODE(enc, p, end)); \
315 pfetch_prev = p; \
316 p += enclen(enc, p, end); \
317} while (0)
318
319#define PINC_S do { \
320 p += enclen(enc, p, end); \
321} while (0)
322#define PFETCH_S(c) do { \
323 c = ((enc->max_enc_len == 1) ? *p : ONIGENC_MBC_TO_CODE(enc, p, end)); \
324 p += enclen(enc, p, end); \
325} while (0)
326
327#define PPEEK (p < end ? ONIGENC_MBC_TO_CODE(enc, p, end) : PEND_VALUE)
328#define PPEEK_IS(c) (PPEEK == (OnigCodePoint )c)
329
330static UChar*
331strcat_capa(UChar* dest, UChar* dest_end, const UChar* src, const UChar* src_end,
332 size_t capa)
333{
334 UChar* r;
335
336 if (dest)
337 r = (UChar* )xrealloc(dest, capa + 1);
338 else
339 r = (UChar* )xmalloc(capa + 1);
340
341 CHECK_NULL_RETURN(r);
342 onig_strcpy(r + (dest_end - dest), src, src_end);
343 return r;
344}
345
346/* dest on static area */
347static UChar*
348strcat_capa_from_static(UChar* dest, UChar* dest_end,
349 const UChar* src, const UChar* src_end, size_t capa)
350{
351 UChar* r;
352
353 r = (UChar* )xmalloc(capa + 1);
354 CHECK_NULL_RETURN(r);
355 onig_strcpy(r, dest, dest_end);
356 onig_strcpy(r + (dest_end - dest), src, src_end);
357 return r;
358}
359
360
361#ifdef USE_ST_LIBRARY
362
363# ifdef RUBY
364# include "ruby/st.h"
365# else
366# include "st.h"
367# endif
368
369 typedef struct {
370 const UChar* s;
371 const UChar* end;
372} st_str_end_key;
373
374static int
375str_end_cmp(st_data_t xp, st_data_t yp)
376{
377 const st_str_end_key *x, *y;
378 const UChar *p, *q;
379 int c;
380
381 x = (const st_str_end_key *)xp;
382 y = (const st_str_end_key *)yp;
383 if ((x->end - x->s) != (y->end - y->s))
384 return 1;
385
386 p = x->s;
387 q = y->s;
388 while (p < x->end) {
389 c = (int )*p - (int )*q;
390 if (c != 0) return c;
391
392 p++; q++;
393 }
394
395 return 0;
396}
397
398NO_SANITIZE("unsigned-integer-overflow", static st_index_t str_end_hash(st_data_t xp));
399
400static st_index_t
401str_end_hash(st_data_t xp)
402{
403 const st_str_end_key *x = (const st_str_end_key *)xp;
404 const UChar *p;
405 st_index_t val = 0;
406
407 p = x->s;
408 while (p < x->end) {
409 val = val * 997 + (int )*p++;
410 }
411
412 return val + (val >> 5);
413}
414
415extern hash_table_type*
416onig_st_init_strend_table_with_size(st_index_t size)
417{
418 static const struct st_hash_type hashType = {
419 str_end_cmp,
420 str_end_hash,
421 };
422
423 return (hash_table_type* )
424 onig_st_init_table_with_size(&hashType, size);
425}
426
427extern int
428onig_st_lookup_strend(hash_table_type* table, const UChar* str_key,
429 const UChar* end_key, hash_data_type *value)
430{
431 st_str_end_key key;
432
433 key.s = (UChar* )str_key;
434 key.end = (UChar* )end_key;
435
436 return onig_st_lookup(table, (st_data_t )(&key), value);
437}
438
439extern int
440onig_st_insert_strend(hash_table_type* table, const UChar* str_key,
441 const UChar* end_key, hash_data_type value)
442{
443 st_str_end_key* key;
444 int result;
445
446 key = (st_str_end_key* )xmalloc(sizeof(st_str_end_key));
447 key->s = (UChar* )str_key;
448 key->end = (UChar* )end_key;
449 result = onig_st_insert(table, (st_data_t )key, value);
450 if (result) {
451 xfree(key);
452 }
453 return result;
454}
455
456#endif /* USE_ST_LIBRARY */
457
458
459#ifdef USE_NAMED_GROUP
460
461# define INIT_NAME_BACKREFS_ALLOC_NUM 8
462
463 typedef struct {
464 UChar* name;
465 size_t name_len; /* byte length */
466 int back_num; /* number of backrefs */
467 int back_alloc;
468 int back_ref1;
469 int* back_refs;
470} NameEntry;
471
472# ifdef USE_ST_LIBRARY
473
474typedef st_table NameTable;
475typedef st_data_t HashDataType; /* 1.6 st.h doesn't define st_data_t type */
476
477# ifdef ONIG_DEBUG
478static int
479i_print_name_entry(HashDataType key_, HashDataType e_, HashDataType arg_)
480{
481 UChar* key = (UChar *)key_;
482 NameEntry* e = (NameEntry *)e_;
483 void* arg = (void *)arg_;
484 int i;
485 FILE* fp = (FILE* )arg;
486
487 fprintf(fp, "%s: ", e->name);
488 if (e->back_num == 0)
489 fputs("-", fp);
490 else if (e->back_num == 1)
491 fprintf(fp, "%d", e->back_ref1);
492 else {
493 for (i = 0; i < e->back_num; i++) {
494 if (i > 0) fprintf(fp, ", ");
495 fprintf(fp, "%d", e->back_refs[i]);
496 }
497 }
498 fputs("\n", fp);
499 return ST_CONTINUE;
500}
501
502extern int
503onig_print_names(FILE* fp, regex_t* reg)
504{
505 NameTable* t = (NameTable* )reg->name_table;
506
507 if (IS_NOT_NULL(t)) {
508 fprintf(fp, "name table\n");
509 onig_st_foreach(t, i_print_name_entry, (HashDataType )fp);
510 fputs("\n", fp);
511 }
512 return 0;
513}
514# endif /* ONIG_DEBUG */
515
516static int
517i_free_name_entry(HashDataType key_, HashDataType e_, HashDataType arg_ ARG_UNUSED)
518{
519 UChar* key = (UChar *)key_;
520 NameEntry* e = (NameEntry *)e_;
521 xfree(e->name);
522 xfree(e->back_refs);
523 xfree(key);
524 xfree(e);
525 return ST_DELETE;
526}
527
528static int
529names_clear(regex_t* reg)
530{
531 NameTable* t = (NameTable* )reg->name_table;
532
533 if (IS_NOT_NULL(t)) {
534 onig_st_foreach(t, i_free_name_entry, 0);
535 }
536 return 0;
537}
538
539extern int
540onig_names_free(regex_t* reg)
541{
542 int r;
543 NameTable* t;
544
545 r = names_clear(reg);
546 if (r) return r;
547
548 t = (NameTable* )reg->name_table;
549 if (IS_NOT_NULL(t)) onig_st_free_table(t);
550 reg->name_table = (void* )NULL;
551 return 0;
552}
553
554static int
555copy_named_captures_iter(const OnigUChar *name, const OnigUChar *name_end,
556 int back_num, int *back_refs, OnigRegex regex, void *arg)
557{
558 NameTable *copy_table = (NameTable *)arg;
559 NameEntry *entry_copy = (NameEntry* )xmalloc(sizeof(NameEntry));
560 if (IS_NULL(entry_copy)) return -1;
561
562 entry_copy->name_len = name_end - name;
563 entry_copy->back_num = back_num;
564 entry_copy->back_alloc = back_num;
565 entry_copy->back_ref1 = back_refs[0];
566 entry_copy->back_refs = xmalloc(back_num * (sizeof(int*)));
567 if (IS_NULL(entry_copy->back_refs)) {
568 xfree(entry_copy);
569 return -1;
570 }
571 memcpy(entry_copy->back_refs, back_refs, back_num * sizeof(back_refs[0]));
572
573 UChar *new_name = strdup_with_null(regex->enc, name, name_end);
574 if (IS_NULL(new_name)) {
575 xfree(entry_copy->back_refs);
576 xfree(entry_copy);
577 return -1;
578 }
579 entry_copy->name = new_name;
580
581 if (onig_st_insert_strend(copy_table, new_name, (new_name + entry_copy->name_len), (hash_data_type)entry_copy)) {
582 xfree(entry_copy->name);
583 xfree(entry_copy->back_refs);
584 xfree(entry_copy);
585 return -1;
586 }
587 return 0;
588}
589
590extern int
591onig_names_copy(regex_t* reg, regex_t* oreg)
592{
593 NameTable* table = oreg->name_table;
594 if (table) {
595 NameTable * t = onig_st_init_strend_table_with_size(onig_number_of_names(oreg));
596 CHECK_NULL_RETURN_MEMERR(t);
597 if (onig_foreach_name(oreg, copy_named_captures_iter, (void*)t)) {
598 onig_st_free_table(t);
599 return ONIGERR_MEMORY;
600 }
601 reg->name_table = t;
602 }
603 return 0;
604}
605
606static NameEntry*
607name_find(regex_t* reg, const UChar* name, const UChar* name_end)
608{
609 NameEntry* e;
610 NameTable* t = (NameTable* )reg->name_table;
611
612 e = (NameEntry* )NULL;
613 if (IS_NOT_NULL(t)) {
614 onig_st_lookup_strend(t, name, name_end, (HashDataType* )((void* )(&e)));
615 }
616 return e;
617}
618
619 typedef struct {
620 int (*func)(const UChar*, const UChar*,int,int*,regex_t*,void*);
621 regex_t* reg;
622 void* arg;
623 int ret;
624 OnigEncoding enc;
625} INamesArg;
626
627static int
628i_names(HashDataType key_ ARG_UNUSED, HashDataType e_, HashDataType arg_)
629{
630 NameEntry* e = (NameEntry *)e_;
631 INamesArg* arg = (INamesArg *)arg_;
632 int r = (*(arg->func))(e->name,
633 e->name + e->name_len,
634 e->back_num,
635 (e->back_num > 1 ? e->back_refs : &(e->back_ref1)),
636 arg->reg, arg->arg);
637 if (r != 0) {
638 arg->ret = r;
639 return ST_STOP;
640 }
641 return ST_CONTINUE;
642}
643
644extern int
645onig_foreach_name(regex_t* reg,
646 int (*func)(const UChar*, const UChar*,int,int*,regex_t*,void*), void* arg)
647{
648 INamesArg narg;
649 NameTable* t = (NameTable* )reg->name_table;
650
651 narg.ret = 0;
652 if (IS_NOT_NULL(t)) {
653 narg.func = func;
654 narg.reg = reg;
655 narg.arg = arg;
656 narg.enc = reg->enc; /* should be pattern encoding. */
657 onig_st_foreach(t, i_names, (HashDataType )&narg);
658 }
659 return narg.ret;
660}
661
662static int
663i_renumber_name(HashDataType key_ ARG_UNUSED, HashDataType e_, HashDataType map_)
664{
665 NameEntry* e = (NameEntry *)e_;
666 GroupNumRemap* map = (GroupNumRemap *)map_;
667 int i;
668
669 if (e->back_num > 1) {
670 for (i = 0; i < e->back_num; i++) {
671 e->back_refs[i] = map[e->back_refs[i]].new_val;
672 }
673 }
674 else if (e->back_num == 1) {
675 e->back_ref1 = map[e->back_ref1].new_val;
676 }
677
678 return ST_CONTINUE;
679}
680
681extern int
682onig_renumber_name_table(regex_t* reg, GroupNumRemap* map)
683{
684 NameTable* t = (NameTable* )reg->name_table;
685
686 if (IS_NOT_NULL(t)) {
687 onig_st_foreach(t, i_renumber_name, (HashDataType )map);
688 }
689 return 0;
690}
691
692
693extern int
694onig_number_of_names(const regex_t* reg)
695{
696 NameTable* t = (NameTable* )reg->name_table;
697
698 if (IS_NOT_NULL(t))
699 return (int )t->num_entries;
700 else
701 return 0;
702}
703
704# else /* USE_ST_LIBRARY */
705
706# define INIT_NAMES_ALLOC_NUM 8
707
708typedef struct {
709 NameEntry* e;
710 int num;
711 int alloc;
712} NameTable;
713
714# ifdef ONIG_DEBUG
715extern int
716onig_print_names(FILE* fp, regex_t* reg)
717{
718 int i, j;
719 NameEntry* e;
720 NameTable* t = (NameTable* )reg->name_table;
721
722 if (IS_NOT_NULL(t) && t->num > 0) {
723 fprintf(fp, "name table\n");
724 for (i = 0; i < t->num; i++) {
725 e = &(t->e[i]);
726 fprintf(fp, "%s: ", e->name);
727 if (e->back_num == 0) {
728 fputs("-", fp);
729 }
730 else if (e->back_num == 1) {
731 fprintf(fp, "%d", e->back_ref1);
732 }
733 else {
734 for (j = 0; j < e->back_num; j++) {
735 if (j > 0) fprintf(fp, ", ");
736 fprintf(fp, "%d", e->back_refs[j]);
737 }
738 }
739 fputs("\n", fp);
740 }
741 fputs("\n", fp);
742 }
743 return 0;
744}
745# endif
746
747static int
748names_clear(regex_t* reg)
749{
750 int i;
751 NameEntry* e;
752 NameTable* t = (NameTable* )reg->name_table;
753
754 if (IS_NOT_NULL(t)) {
755 for (i = 0; i < t->num; i++) {
756 e = &(t->e[i]);
757 if (IS_NOT_NULL(e->name)) {
758 xfree(e->name);
759 e->name = NULL;
760 e->name_len = 0;
761 e->back_num = 0;
762 e->back_alloc = 0;
763 xfree(e->back_refs);
764 e->back_refs = (int* )NULL;
765 }
766 }
767 if (IS_NOT_NULL(t->e)) {
768 xfree(t->e);
769 t->e = NULL;
770 }
771 t->num = 0;
772 }
773 return 0;
774}
775
776extern int
777onig_names_free(regex_t* reg)
778{
779 int r;
780 NameTable* t;
781
782 r = names_clear(reg);
783 if (r) return r;
784
785 t = (NameTable* )reg->name_table;
786 xfree(t);
787 reg->name_table = NULL;
788 return 0;
789}
790
791extern int
792onig_names_copy(regex_t* reg, regex_t* oreg)
793{
794 NameTable* ot = oreg->name_table;
795 if (ot) {
796 OnigEncoding enc = oreg->enc;
797 int i, num = ot->num;
798 NameTable* t = xmalloc(sizeof(*t));
799 CHECK_NULL_RETURN_MEMERR(t);
800 *t = *ot;
801 t->e = xmalloc(t->alloc * sizeof(t->e[0]));
802 if (IS_NULL(t->e)) {
803 xfree(t);
804 return ONIGERR_MEMORY;
805 }
806 t->num = 0;
807 reg->name_table = t;
808 for (i = 0; i < num; t->num = ++i) {
809 NameEntry* oe = &(ot->e[i]);
810 NameEntry* e = &(t->e[i]);
811 *e = *oe;
812 e->name = NULL;
813 e->back_refs = NULL;
814 e->name = strdup_with_null(enc, oe->name, oe->name + e->name_len);
815 if (IS_NULL(e->name)) {
816 onig_names_free(reg);
817 return ONIGERR_MEMORY;
818 }
819 e->back_refs = xmalloc(e->back_alloc * sizeof(e->back_refs[0]));
820 if (IS_NULL(e->back_refs)) {
821 xfree(e->name);
822 onig_names_free(reg);
823 return ONIGERR_MEMORY;
824 }
825 memcpy(e->back_refs, oe->back_refs, e->back_num * sizeof(e->back_refs[0]));
826 e->back_ref1 = e->back_refs[0];
827 }
828 }
829 return 0;
830}
831
832static NameEntry*
833name_find(regex_t* reg, const UChar* name, const UChar* name_end)
834{
835 int i;
836 size_t len;
837 NameEntry* e;
838 NameTable* t = (NameTable* )reg->name_table;
839
840 if (IS_NOT_NULL(t)) {
841 len = name_end - name;
842 for (i = 0; i < t->num; i++) {
843 e = &(t->e[i]);
844 if (len == e->name_len && onig_strncmp(name, e->name, len) == 0)
845 return e;
846 }
847 }
848 return (NameEntry* )NULL;
849}
850
851extern int
852onig_foreach_name(regex_t* reg,
853 int (*func)(const UChar*, const UChar*,int,int*,regex_t*,void*), void* arg)
854{
855 int i, r;
856 NameEntry* e;
857 NameTable* t = (NameTable* )reg->name_table;
858
859 if (IS_NOT_NULL(t)) {
860 for (i = 0; i < t->num; i++) {
861 e = &(t->e[i]);
862 r = (*func)(e->name, e->name + e->name_len, e->back_num,
863 (e->back_num > 1 ? e->back_refs : &(e->back_ref1)),
864 reg, arg);
865 if (r != 0) return r;
866 }
867 }
868 return 0;
869}
870
871extern int
872onig_renumber_name_table(regex_t* reg, GroupNumRemap* map)
873{
874 int i, j;
875 NameEntry* e;
876 NameTable* t = (NameTable* )reg->name_table;
877
878 if (IS_NOT_NULL(t)) {
879 for (i = 0; i < t->num; i++) {
880 e = &(t->e[i]);
881
882 if (e->back_num > 1) {
883 for (j = 0; j < e->back_num; j++) {
884 e->back_refs[j] = map[e->back_refs[j]].new_val;
885 }
886 }
887 else if (e->back_num == 1) {
888 e->back_ref1 = map[e->back_ref1].new_val;
889 }
890 }
891 }
892 return 0;
893}
894
895extern int
896onig_number_of_names(const regex_t* reg)
897{
898 NameTable* t = (NameTable* )reg->name_table;
899
900 if (IS_NOT_NULL(t))
901 return t->num;
902 else
903 return 0;
904}
905
906# endif /* else USE_ST_LIBRARY */
907
908static int
909name_add(regex_t* reg, UChar* name, UChar* name_end, int backref, ScanEnv* env)
910{
911 int alloc;
912 NameEntry* e;
913 NameTable* t = (NameTable* )reg->name_table;
914
915 if (name_end - name <= 0)
916 return ONIGERR_EMPTY_GROUP_NAME;
917
918 e = name_find(reg, name, name_end);
919 if (IS_NULL(e)) {
920# ifdef USE_ST_LIBRARY
921 if (IS_NULL(t)) {
922 t = onig_st_init_strend_table_with_size(5);
923 reg->name_table = (void* )t;
924 }
925 e = (NameEntry* )xmalloc(sizeof(NameEntry));
926 CHECK_NULL_RETURN_MEMERR(e);
927
928 e->name = strdup_with_null(reg->enc, name, name_end);
929 if (IS_NULL(e->name)) {
930 xfree(e);
931 return ONIGERR_MEMORY;
932 }
933 onig_st_insert_strend(t, e->name, (e->name + (name_end - name)),
934 (HashDataType )e);
935
936 e->name_len = name_end - name;
937 e->back_num = 0;
938 e->back_alloc = 0;
939 e->back_refs = (int* )NULL;
940
941# else
942
943 if (IS_NULL(t)) {
944 alloc = INIT_NAMES_ALLOC_NUM;
945 t = (NameTable* )xmalloc(sizeof(NameTable));
946 CHECK_NULL_RETURN_MEMERR(t);
947 t->e = NULL;
948 t->alloc = 0;
949 t->num = 0;
950
951 t->e = (NameEntry* )xmalloc(sizeof(NameEntry) * alloc);
952 if (IS_NULL(t->e)) {
953 xfree(t);
954 return ONIGERR_MEMORY;
955 }
956 t->alloc = alloc;
957 reg->name_table = t;
958 goto clear;
959 }
960 else if (t->num == t->alloc) {
961 int i;
962 NameEntry* p;
963
964 alloc = t->alloc * 2;
965 p = (NameEntry* )xrealloc(t->e, sizeof(NameEntry) * alloc);
966 CHECK_NULL_RETURN_MEMERR(p);
967 t->e = p;
968 t->alloc = alloc;
969
970 clear:
971 for (i = t->num; i < t->alloc; i++) {
972 t->e[i].name = NULL;
973 t->e[i].name_len = 0;
974 t->e[i].back_num = 0;
975 t->e[i].back_alloc = 0;
976 t->e[i].back_refs = (int* )NULL;
977 }
978 }
979 e = &(t->e[t->num]);
980 t->num++;
981 e->name = strdup_with_null(reg->enc, name, name_end);
982 if (IS_NULL(e->name)) return ONIGERR_MEMORY;
983 e->name_len = name_end - name;
984# endif
985 }
986
987 if (e->back_num >= 1 &&
988 ! IS_SYNTAX_BV(env->syntax, ONIG_SYN_ALLOW_MULTIPLEX_DEFINITION_NAME)) {
989 onig_scan_env_set_error_string(env, ONIGERR_MULTIPLEX_DEFINED_NAME,
990 name, name_end);
991 return ONIGERR_MULTIPLEX_DEFINED_NAME;
992 }
993
994 e->back_num++;
995 if (e->back_num == 1) {
996 e->back_ref1 = backref;
997 }
998 else {
999 if (e->back_num == 2) {
1000 alloc = INIT_NAME_BACKREFS_ALLOC_NUM;
1001 e->back_refs = (int* )xmalloc(sizeof(int) * alloc);
1002 CHECK_NULL_RETURN_MEMERR(e->back_refs);
1003 e->back_alloc = alloc;
1004 e->back_refs[0] = e->back_ref1;
1005 e->back_refs[1] = backref;
1006 }
1007 else {
1008 if (e->back_num > e->back_alloc) {
1009 int* p;
1010 alloc = e->back_alloc * 2;
1011 p = (int* )xrealloc(e->back_refs, sizeof(int) * alloc);
1012 CHECK_NULL_RETURN_MEMERR(p);
1013 e->back_refs = p;
1014 e->back_alloc = alloc;
1015 }
1016 e->back_refs[e->back_num - 1] = backref;
1017 }
1018 }
1019
1020 return 0;
1021}
1022
1023extern int
1024onig_name_to_group_numbers(regex_t* reg, const UChar* name,
1025 const UChar* name_end, int** nums)
1026{
1027 NameEntry* e = name_find(reg, name, name_end);
1028
1029 if (IS_NULL(e)) return ONIGERR_UNDEFINED_NAME_REFERENCE;
1030
1031 switch (e->back_num) {
1032 case 0:
1033 *nums = 0;
1034 break;
1035 case 1:
1036 *nums = &(e->back_ref1);
1037 break;
1038 default:
1039 *nums = e->back_refs;
1040 break;
1041 }
1042 return e->back_num;
1043}
1044
1045extern int
1046onig_name_to_backref_number(regex_t* reg, const UChar* name,
1047 const UChar* name_end, const OnigRegion *region)
1048{
1049 int i, n, *nums;
1050
1051 n = onig_name_to_group_numbers(reg, name, name_end, &nums);
1052 if (n < 0)
1053 return n;
1054 else if (n == 0)
1055 return ONIGERR_PARSER_BUG;
1056 else if (n == 1)
1057 return nums[0];
1058 else {
1059 if (IS_NOT_NULL(region)) {
1060 for (i = n - 1; i >= 0; i--) {
1061 if (region->beg[nums[i]] != ONIG_REGION_NOTPOS)
1062 return nums[i];
1063 }
1064 }
1065 return nums[n - 1];
1066 }
1067}
1068
1069#else /* USE_NAMED_GROUP */
1070
1071extern int
1072onig_name_to_group_numbers(regex_t* reg, const UChar* name,
1073 const UChar* name_end, int** nums)
1074{
1075 return ONIG_NO_SUPPORT_CONFIG;
1076}
1077
1078extern int
1079onig_name_to_backref_number(regex_t* reg, const UChar* name,
1080 const UChar* name_end, const OnigRegion* region)
1081{
1082 return ONIG_NO_SUPPORT_CONFIG;
1083}
1084
1085extern int
1086onig_foreach_name(regex_t* reg,
1087 int (*func)(const UChar*, const UChar*,int,int*,regex_t*,void*), void* arg)
1088{
1089 return ONIG_NO_SUPPORT_CONFIG;
1090}
1091
1092extern int
1093onig_number_of_names(const regex_t* reg)
1094{
1095 return 0;
1096}
1097
1098extern int
1099onig_names_copy(regex_t* reg, regex_t* oreg)
1100{
1101 return 0;
1102}
1103#endif /* else USE_NAMED_GROUP */
1104
1105extern int
1106onig_noname_group_capture_is_active(const regex_t* reg)
1107{
1108 if (ONIG_IS_OPTION_ON(reg->options, ONIG_OPTION_DONT_CAPTURE_GROUP))
1109 return 0;
1110
1111#ifdef USE_NAMED_GROUP
1112 if (onig_number_of_names(reg) > 0 &&
1113 IS_SYNTAX_BV(reg->syntax, ONIG_SYN_CAPTURE_ONLY_NAMED_GROUP) &&
1114 !ONIG_IS_OPTION_ON(reg->options, ONIG_OPTION_CAPTURE_GROUP)) {
1115 return 0;
1116 }
1117#endif
1118
1119 return 1;
1120}
1121
1122
1123#define INIT_SCANENV_MEMNODES_ALLOC_SIZE 16
1124
1125static void
1126scan_env_clear(ScanEnv* env)
1127{
1128 int i;
1129
1130 BIT_STATUS_CLEAR(env->capture_history);
1131 BIT_STATUS_CLEAR(env->bt_mem_start);
1132 BIT_STATUS_CLEAR(env->bt_mem_end);
1133 BIT_STATUS_CLEAR(env->backrefed_mem);
1134 env->error = (UChar* )NULL;
1135 env->error_end = (UChar* )NULL;
1136 env->num_call = 0;
1137 env->num_mem = 0;
1138#ifdef USE_NAMED_GROUP
1139 env->num_named = 0;
1140#endif
1141 env->mem_alloc = 0;
1142 env->mem_nodes_dynamic = (Node** )NULL;
1143
1144 for (i = 0; i < SCANENV_MEMNODES_SIZE; i++)
1145 env->mem_nodes_static[i] = NULL_NODE;
1146
1147#ifdef USE_COMBINATION_EXPLOSION_CHECK
1148 env->num_comb_exp_check = 0;
1149 env->comb_exp_max_regnum = 0;
1150 env->curr_max_regnum = 0;
1151 env->has_recursion = 0;
1152#endif
1153 env->parse_depth = 0;
1154 env->warnings_flag = 0;
1155}
1156
1157static int
1158scan_env_add_mem_entry(ScanEnv* env)
1159{
1160 int i, need, alloc;
1161 Node** p;
1162
1163 need = env->num_mem + 1;
1164 if (need > ONIG_MAX_CAPTURE_GROUP_NUM)
1165 return ONIGERR_TOO_MANY_CAPTURE_GROUPS;
1166 if (need >= SCANENV_MEMNODES_SIZE) {
1167 if (env->mem_alloc <= need) {
1168 if (IS_NULL(env->mem_nodes_dynamic)) {
1169 alloc = INIT_SCANENV_MEMNODES_ALLOC_SIZE;
1170 p = (Node** )xmalloc(sizeof(Node*) * alloc);
1171 CHECK_NULL_RETURN_MEMERR(p);
1172 xmemcpy(p, env->mem_nodes_static,
1173 sizeof(Node*) * SCANENV_MEMNODES_SIZE);
1174 }
1175 else {
1176 alloc = env->mem_alloc * 2;
1177 p = (Node** )xrealloc(env->mem_nodes_dynamic, sizeof(Node*) * alloc);
1178 CHECK_NULL_RETURN_MEMERR(p);
1179 }
1180
1181 for (i = env->num_mem + 1; i < alloc; i++)
1182 p[i] = NULL_NODE;
1183
1184 env->mem_nodes_dynamic = p;
1185 env->mem_alloc = alloc;
1186 }
1187 }
1188
1189 env->num_mem++;
1190 return env->num_mem;
1191}
1192
1193static int
1194scan_env_set_mem_node(ScanEnv* env, int num, Node* node)
1195{
1196 if (env->num_mem >= num)
1197 SCANENV_MEM_NODES(env)[num] = node;
1198 else
1199 return ONIGERR_PARSER_BUG;
1200 return 0;
1201}
1202
1203
1204extern void
1205onig_node_free(Node* node)
1206{
1207 start:
1208 if (IS_NULL(node)) return ;
1209
1210 switch (NTYPE(node)) {
1211 case NT_STR:
1212 if (NSTR(node)->capa != 0 &&
1213 IS_NOT_NULL(NSTR(node)->s) && NSTR(node)->s != NSTR(node)->buf) {
1214 xfree(NSTR(node)->s);
1215 }
1216 break;
1217
1218 case NT_LIST:
1219 case NT_ALT:
1220 onig_node_free(NCAR(node));
1221 {
1222 Node* next_node = NCDR(node);
1223
1224 xfree(node);
1225 node = next_node;
1226 goto start;
1227 }
1228 break;
1229
1230 case NT_CCLASS:
1231 {
1232 CClassNode* cc = NCCLASS(node);
1233
1234 bbuf_free(cc->mbuf);
1235 }
1236 break;
1237
1238 case NT_QTFR:
1239 onig_node_free(NQTFR(node)->target);
1240 break;
1241
1242 case NT_ENCLOSE:
1243 onig_node_free(NENCLOSE(node)->target);
1244 break;
1245
1246 case NT_BREF:
1247 xfree(NBREF(node)->back_dynamic);
1248 break;
1249
1250 case NT_ANCHOR:
1251 onig_node_free(NANCHOR(node)->target);
1252 break;
1253 }
1254
1255 xfree(node);
1256}
1257
1258static Node*
1259node_new(void)
1260{
1261 Node* node;
1262
1263 node = (Node* )xmalloc(sizeof(Node));
1264 /* xmemset(node, 0, sizeof(Node)); */
1265 return node;
1266}
1267
1268static void
1269initialize_cclass(CClassNode* cc)
1270{
1271 BITSET_CLEAR(cc->bs);
1272 /* cc->base.flags = 0; */
1273 cc->flags = 0;
1274 cc->mbuf = NULL;
1275}
1276
1277static Node*
1278node_new_cclass(void)
1279{
1280 Node* node = node_new();
1281 CHECK_NULL_RETURN(node);
1282
1283 SET_NTYPE(node, NT_CCLASS);
1284 initialize_cclass(NCCLASS(node));
1285 return node;
1286}
1287
1288static Node*
1289node_new_ctype(int type, int not, int ascii_range)
1290{
1291 Node* node = node_new();
1292 CHECK_NULL_RETURN(node);
1293
1294 SET_NTYPE(node, NT_CTYPE);
1295 NCTYPE(node)->ctype = type;
1296 NCTYPE(node)->not = not;
1297 NCTYPE(node)->ascii_range = ascii_range;
1298 return node;
1299}
1300
1301static Node*
1302node_new_anychar(void)
1303{
1304 Node* node = node_new();
1305 CHECK_NULL_RETURN(node);
1306
1307 SET_NTYPE(node, NT_CANY);
1308 return node;
1309}
1310
1311static Node*
1312node_new_list(Node* left, Node* right)
1313{
1314 Node* node = node_new();
1315 CHECK_NULL_RETURN(node);
1316
1317 SET_NTYPE(node, NT_LIST);
1318 NCAR(node) = left;
1319 NCDR(node) = right;
1320 return node;
1321}
1322
1323extern Node*
1324onig_node_new_list(Node* left, Node* right)
1325{
1326 return node_new_list(left, right);
1327}
1328
1329extern Node*
1330onig_node_list_add(Node* list, Node* x)
1331{
1332 Node *n;
1333
1334 n = onig_node_new_list(x, NULL);
1335 if (IS_NULL(n)) return NULL_NODE;
1336
1337 if (IS_NOT_NULL(list)) {
1338 while (IS_NOT_NULL(NCDR(list)))
1339 list = NCDR(list);
1340
1341 NCDR(list) = n;
1342 }
1343
1344 return n;
1345}
1346
1347extern Node*
1348onig_node_new_alt(Node* left, Node* right)
1349{
1350 Node* node = node_new();
1351 CHECK_NULL_RETURN(node);
1352
1353 SET_NTYPE(node, NT_ALT);
1354 NCAR(node) = left;
1355 NCDR(node) = right;
1356 return node;
1357}
1358
1359extern Node*
1360onig_node_new_anchor(int type)
1361{
1362 Node* node = node_new();
1363 CHECK_NULL_RETURN(node);
1364
1365 SET_NTYPE(node, NT_ANCHOR);
1366 NANCHOR(node)->type = type;
1367 NANCHOR(node)->target = NULL;
1368 NANCHOR(node)->char_len = -1;
1369 NANCHOR(node)->ascii_range = 0;
1370 return node;
1371}
1372
1373static Node*
1374node_new_backref(int back_num, int* backrefs, int by_name,
1375#ifdef USE_BACKREF_WITH_LEVEL
1376 int exist_level, int nest_level,
1377#endif
1378 ScanEnv* env)
1379{
1380 int i;
1381 Node* node = node_new();
1382
1383 CHECK_NULL_RETURN(node);
1384
1385 SET_NTYPE(node, NT_BREF);
1386 NBREF(node)->state = 0;
1387 NBREF(node)->back_num = back_num;
1388 NBREF(node)->back_dynamic = (int* )NULL;
1389 if (by_name != 0)
1390 NBREF(node)->state |= NST_NAME_REF;
1391
1392#ifdef USE_BACKREF_WITH_LEVEL
1393 if (exist_level != 0) {
1394 NBREF(node)->state |= NST_NEST_LEVEL;
1395 NBREF(node)->nest_level = nest_level;
1396 }
1397#endif
1398
1399 for (i = 0; i < back_num; i++) {
1400 if (backrefs[i] <= env->num_mem &&
1401 IS_NULL(SCANENV_MEM_NODES(env)[backrefs[i]])) {
1402 NBREF(node)->state |= NST_RECURSION; /* /...(\1).../ */
1403 break;
1404 }
1405 }
1406
1407 if (back_num <= NODE_BACKREFS_SIZE) {
1408 for (i = 0; i < back_num; i++)
1409 NBREF(node)->back_static[i] = backrefs[i];
1410 }
1411 else {
1412 int* p = (int* )xmalloc(sizeof(int) * back_num);
1413 if (IS_NULL(p)) {
1414 onig_node_free(node);
1415 return NULL;
1416 }
1417 NBREF(node)->back_dynamic = p;
1418 for (i = 0; i < back_num; i++)
1419 p[i] = backrefs[i];
1420 }
1421 return node;
1422}
1423
1424#ifdef USE_SUBEXP_CALL
1425static Node*
1426node_new_call(UChar* name, UChar* name_end, int gnum)
1427{
1428 Node* node = node_new();
1429 CHECK_NULL_RETURN(node);
1430
1431 SET_NTYPE(node, NT_CALL);
1432 NCALL(node)->state = 0;
1433 NCALL(node)->target = NULL_NODE;
1434 NCALL(node)->name = name;
1435 NCALL(node)->name_end = name_end;
1436 NCALL(node)->group_num = gnum; /* call by number if gnum != 0 */
1437 return node;
1438}
1439#endif
1440
1441static Node*
1442node_new_quantifier(int lower, int upper, int by_number)
1443{
1444 Node* node = node_new();
1445 CHECK_NULL_RETURN(node);
1446
1447 SET_NTYPE(node, NT_QTFR);
1448 NQTFR(node)->state = 0;
1449 NQTFR(node)->target = NULL;
1450 NQTFR(node)->lower = lower;
1451 NQTFR(node)->upper = upper;
1452 NQTFR(node)->greedy = 1;
1453 NQTFR(node)->target_empty_info = NQ_TARGET_ISNOT_EMPTY;
1454 NQTFR(node)->head_exact = NULL_NODE;
1455 NQTFR(node)->next_head_exact = NULL_NODE;
1456 NQTFR(node)->is_referred = 0;
1457 if (by_number != 0)
1458 NQTFR(node)->state |= NST_BY_NUMBER;
1459
1460#ifdef USE_COMBINATION_EXPLOSION_CHECK
1461 NQTFR(node)->comb_exp_check_num = 0;
1462#endif
1463
1464 return node;
1465}
1466
1467static Node*
1468node_new_enclose(int type)
1469{
1470 Node* node = node_new();
1471 CHECK_NULL_RETURN(node);
1472
1473 SET_NTYPE(node, NT_ENCLOSE);
1474 NENCLOSE(node)->type = type;
1475 NENCLOSE(node)->state = 0;
1476 NENCLOSE(node)->regnum = 0;
1477 NENCLOSE(node)->option = 0;
1478 NENCLOSE(node)->target = NULL;
1479 NENCLOSE(node)->call_addr = -1;
1480 NENCLOSE(node)->opt_count = 0;
1481 return node;
1482}
1483
1484extern Node*
1485onig_node_new_enclose(int type)
1486{
1487 return node_new_enclose(type);
1488}
1489
1490static Node*
1491node_new_enclose_memory(OnigOptionType option, int is_named)
1492{
1493 Node* node = node_new_enclose(ENCLOSE_MEMORY);
1494 CHECK_NULL_RETURN(node);
1495 if (is_named != 0)
1496 SET_ENCLOSE_STATUS(node, NST_NAMED_GROUP);
1497
1498#ifdef USE_SUBEXP_CALL
1499 NENCLOSE(node)->option = option;
1500#endif
1501 return node;
1502}
1503
1504static Node*
1505node_new_option(OnigOptionType option)
1506{
1507 Node* node = node_new_enclose(ENCLOSE_OPTION);
1508 CHECK_NULL_RETURN(node);
1509 NENCLOSE(node)->option = option;
1510 return node;
1511}
1512
1513extern int
1514onig_node_str_cat(Node* node, const UChar* s, const UChar* end)
1515{
1516 ptrdiff_t addlen = end - s;
1517
1518 if (addlen > 0) {
1519 ptrdiff_t len = NSTR(node)->end - NSTR(node)->s;
1520
1521 if (NSTR(node)->capa > 0 || (len + addlen > NODE_STR_BUF_SIZE - 1)) {
1522 UChar* p;
1523 ptrdiff_t capa = len + addlen + NODE_STR_MARGIN;
1524
1525 if (capa <= NSTR(node)->capa) {
1526 onig_strcpy(NSTR(node)->s + len, s, end);
1527 }
1528 else {
1529 if (NSTR(node)->s == NSTR(node)->buf)
1530 p = strcat_capa_from_static(NSTR(node)->s, NSTR(node)->end,
1531 s, end, capa);
1532 else
1533 p = strcat_capa(NSTR(node)->s, NSTR(node)->end, s, end, capa);
1534
1535 CHECK_NULL_RETURN_MEMERR(p);
1536 NSTR(node)->s = p;
1537 NSTR(node)->capa = (int )capa;
1538 }
1539 }
1540 else {
1541 onig_strcpy(NSTR(node)->s + len, s, end);
1542 }
1543 NSTR(node)->end = NSTR(node)->s + len + addlen;
1544 }
1545
1546 return 0;
1547}
1548
1549extern int
1550onig_node_str_set(Node* node, const UChar* s, const UChar* end)
1551{
1552 onig_node_str_clear(node);
1553 return onig_node_str_cat(node, s, end);
1554}
1555
1556static int
1557node_str_cat_char(Node* node, UChar c)
1558{
1559 UChar s[1];
1560
1561 s[0] = c;
1562 return onig_node_str_cat(node, s, s + 1);
1563}
1564
1565static int
1566node_str_cat_codepoint(Node* node, OnigEncoding enc, OnigCodePoint c)
1567{
1568 UChar buf[ONIGENC_CODE_TO_MBC_MAXLEN];
1569 int num = ONIGENC_CODE_TO_MBC(enc, c, buf);
1570 if (num < 0) return num;
1571 return onig_node_str_cat(node, buf, buf + num);
1572}
1573
1574#if 0
1575extern void
1576onig_node_conv_to_str_node(Node* node, int flag)
1577{
1578 SET_NTYPE(node, NT_STR);
1579 NSTR(node)->flag = flag;
1580 NSTR(node)->capa = 0;
1581 NSTR(node)->s = NSTR(node)->buf;
1582 NSTR(node)->end = NSTR(node)->buf;
1583}
1584#endif
1585
1586extern void
1587onig_node_str_clear(Node* node)
1588{
1589 if (NSTR(node)->capa != 0 &&
1590 IS_NOT_NULL(NSTR(node)->s) && NSTR(node)->s != NSTR(node)->buf) {
1591 xfree(NSTR(node)->s);
1592 }
1593
1594 NSTR(node)->capa = 0;
1595 NSTR(node)->flag = 0;
1596 NSTR(node)->s = NSTR(node)->buf;
1597 NSTR(node)->end = NSTR(node)->buf;
1598}
1599
1600static Node*
1601node_new_str(const UChar* s, const UChar* end)
1602{
1603 Node* node = node_new();
1604 CHECK_NULL_RETURN(node);
1605
1606 SET_NTYPE(node, NT_STR);
1607 NSTR(node)->capa = 0;
1608 NSTR(node)->flag = 0;
1609 NSTR(node)->s = NSTR(node)->buf;
1610 NSTR(node)->end = NSTR(node)->buf;
1611 if (onig_node_str_cat(node, s, end)) {
1612 onig_node_free(node);
1613 return NULL;
1614 }
1615 return node;
1616}
1617
1618extern Node*
1619onig_node_new_str(const UChar* s, const UChar* end)
1620{
1621 return node_new_str(s, end);
1622}
1623
1624static Node*
1625node_new_str_raw(UChar* s, UChar* end)
1626{
1627 Node* node = node_new_str(s, end);
1628 if (IS_NOT_NULL(node))
1629 NSTRING_SET_RAW(node);
1630 return node;
1631}
1632
1633static Node*
1634node_new_empty(void)
1635{
1636 return node_new_str(NULL, NULL);
1637}
1638
1639static Node*
1640node_new_str_raw_char(UChar c)
1641{
1642 UChar p[1];
1643
1644 p[0] = c;
1645 return node_new_str_raw(p, p + 1);
1646}
1647
1648static Node*
1649str_node_split_last_char(StrNode* sn, OnigEncoding enc)
1650{
1651 const UChar *p;
1652 Node* n = NULL_NODE;
1653
1654 if (sn->end > sn->s) {
1655 p = onigenc_get_prev_char_head(enc, sn->s, sn->end, sn->end);
1656 if (p && p > sn->s) { /* can be split. */
1657 n = node_new_str(p, sn->end);
1658 if (IS_NOT_NULL(n) && (sn->flag & NSTR_RAW) != 0)
1659 NSTRING_SET_RAW(n);
1660 sn->end = (UChar* )p;
1661 }
1662 }
1663 return n;
1664}
1665
1666static int
1667str_node_can_be_split(StrNode* sn, OnigEncoding enc)
1668{
1669 if (sn->end > sn->s) {
1670 return ((enclen(enc, sn->s, sn->end) < sn->end - sn->s) ? 1 : 0);
1671 }
1672 return 0;
1673}
1674
1675#ifdef USE_PAD_TO_SHORT_BYTE_CHAR
1676static int
1677node_str_head_pad(StrNode* sn, int num, UChar val)
1678{
1679 UChar buf[NODE_STR_BUF_SIZE];
1680 int i, len;
1681
1682 len = sn->end - sn->s;
1683 onig_strcpy(buf, sn->s, sn->end);
1684 onig_strcpy(&(sn->s[num]), buf, buf + len);
1685 sn->end += num;
1686
1687 for (i = 0; i < num; i++) {
1688 sn->s[i] = val;
1689 }
1690}
1691#endif
1692
1693extern int
1694onig_scan_unsigned_number(UChar** src, const UChar* end, OnigEncoding enc)
1695{
1696 unsigned int num, val;
1697 OnigCodePoint c;
1698 UChar* p = *src;
1699 PFETCH_READY;
1700
1701 num = 0;
1702 while (!PEND) {
1703 PFETCH(c);
1704 if (ONIGENC_IS_CODE_DIGIT(enc, c)) {
1705 val = (unsigned int )DIGITVAL(c);
1706 if ((INT_MAX_LIMIT - val) / 10UL < num)
1707 return -1; /* overflow */
1708
1709 num = num * 10 + val;
1710 }
1711 else {
1712 PUNFETCH;
1713 break;
1714 }
1715 }
1716 *src = p;
1717 return num;
1718}
1719
1720static int
1721scan_unsigned_hexadecimal_number(UChar** src, UChar* end, int minlen,
1722 int maxlen, OnigEncoding enc)
1723{
1724 OnigCodePoint c;
1725 unsigned int num, val;
1726 int restlen;
1727 UChar* p = *src;
1728 PFETCH_READY;
1729
1730 restlen = maxlen - minlen;
1731 num = 0;
1732 while (!PEND && maxlen-- != 0) {
1733 PFETCH(c);
1734 if (ONIGENC_IS_CODE_XDIGIT(enc, c)) {
1735 val = (unsigned int )XDIGITVAL(enc,c);
1736 if ((INT_MAX_LIMIT - val) / 16UL < num)
1737 return -1; /* overflow */
1738
1739 num = (num << 4) + XDIGITVAL(enc,c);
1740 }
1741 else {
1742 PUNFETCH;
1743 maxlen++;
1744 break;
1745 }
1746 }
1747 if (maxlen > restlen)
1748 return -2; /* not enough digits */
1749 *src = p;
1750 return num;
1751}
1752
1753static int
1754scan_unsigned_octal_number(UChar** src, UChar* end, int maxlen,
1755 OnigEncoding enc)
1756{
1757 OnigCodePoint c;
1758 unsigned int num, val;
1759 UChar* p = *src;
1760 PFETCH_READY;
1761
1762 num = 0;
1763 while (!PEND && maxlen-- != 0) {
1764 PFETCH(c);
1765 if (ONIGENC_IS_CODE_DIGIT(enc, c) && c < '8') {
1766 val = ODIGITVAL(c);
1767 if ((INT_MAX_LIMIT - val) / 8UL < num)
1768 return -1; /* overflow */
1769
1770 num = (num << 3) + val;
1771 }
1772 else {
1773 PUNFETCH;
1774 break;
1775 }
1776 }
1777 *src = p;
1778 return num;
1779}
1780
1781
1782#define BBUF_WRITE_CODE_POINT(bbuf,pos,code) \
1783 BBUF_WRITE(bbuf, pos, &(code), SIZE_CODE_POINT)
1784
1785/* data format:
1786 [n][from-1][to-1][from-2][to-2] ... [from-n][to-n]
1787 (all data size is OnigCodePoint)
1788 */
1789static int
1790new_code_range(BBuf** pbuf)
1791{
1792#define INIT_MULTI_BYTE_RANGE_SIZE (SIZE_CODE_POINT * 5)
1793 int r;
1794 OnigCodePoint n;
1795 BBuf* bbuf;
1796
1797 bbuf = *pbuf = (BBuf* )xmalloc(sizeof(BBuf));
1798 CHECK_NULL_RETURN_MEMERR(*pbuf);
1799 r = BBUF_INIT(*pbuf, INIT_MULTI_BYTE_RANGE_SIZE);
1800 if (r) return r;
1801
1802 n = 0;
1803 BBUF_WRITE_CODE_POINT(bbuf, 0, n);
1804 return 0;
1805}
1806
1807static int
1808add_code_range_to_buf0(BBuf** pbuf, ScanEnv* env, OnigCodePoint from, OnigCodePoint to,
1809 int checkdup)
1810{
1811 int r, inc_n, pos;
1812 OnigCodePoint low, high, bound, x;
1813 OnigCodePoint n, *data;
1814 BBuf* bbuf;
1815
1816 if (from > to) {
1817 n = from; from = to; to = n;
1818 }
1819
1820 if (IS_NULL(*pbuf)) {
1821 r = new_code_range(pbuf);
1822 if (r) return r;
1823 bbuf = *pbuf;
1824 n = 0;
1825 }
1826 else {
1827 bbuf = *pbuf;
1828 GET_CODE_POINT(n, bbuf->p);
1829 }
1830 data = (OnigCodePoint* )(bbuf->p);
1831 data++;
1832
1833 bound = (from == 0) ? 0 : n;
1834 for (low = 0; low < bound; ) {
1835 x = (low + bound) >> 1;
1836 if (from - 1 > data[x*2 + 1])
1837 low = x + 1;
1838 else
1839 bound = x;
1840 }
1841
1842 high = (to == ONIG_LAST_CODE_POINT) ? n : low;
1843 for (bound = n; high < bound; ) {
1844 x = (high + bound) >> 1;
1845 if (to + 1 >= data[x*2])
1846 high = x + 1;
1847 else
1848 bound = x;
1849 }
1850 /* data[(low-1)*2+1] << from <= data[low*2]
1851 * data[(high-1)*2+1] <= to << data[high*2]
1852 */
1853
1854 inc_n = low + 1 - high;
1855 if (n + inc_n > ONIG_MAX_MULTI_BYTE_RANGES_NUM)
1856 return ONIGERR_TOO_MANY_MULTI_BYTE_RANGES;
1857
1858 if (inc_n != 1) {
1859 if (checkdup && from <= data[low*2+1]
1860 && (data[low*2] <= from || data[low*2+1] <= to))
1861 CC_DUP_WARN(env, from, to);
1862 if (from > data[low*2])
1863 from = data[low*2];
1864 if (to < data[(high - 1)*2 + 1])
1865 to = data[(high - 1)*2 + 1];
1866 }
1867
1868 if (inc_n != 0) {
1869 int from_pos = SIZE_CODE_POINT * (1 + high * 2);
1870 int to_pos = SIZE_CODE_POINT * (1 + (low + 1) * 2);
1871
1872 if (inc_n > 0) {
1873 if (high < n) {
1874 int size = (n - high) * 2 * SIZE_CODE_POINT;
1875 BBUF_MOVE_RIGHT(bbuf, from_pos, to_pos, size);
1876 }
1877 }
1878 else {
1879 BBUF_MOVE_LEFT_REDUCE(bbuf, from_pos, to_pos);
1880 }
1881 }
1882
1883 pos = SIZE_CODE_POINT * (1 + low * 2);
1884 BBUF_ENSURE_SIZE(bbuf, pos + SIZE_CODE_POINT * 2);
1885 BBUF_WRITE_CODE_POINT(bbuf, pos, from);
1886 BBUF_WRITE_CODE_POINT(bbuf, pos + SIZE_CODE_POINT, to);
1887 n += inc_n;
1888 BBUF_WRITE_CODE_POINT(bbuf, 0, n);
1889
1890 return 0;
1891}
1892
1893static int
1894add_code_range_to_buf(BBuf** pbuf, ScanEnv* env, OnigCodePoint from, OnigCodePoint to)
1895{
1896 return add_code_range_to_buf0(pbuf, env, from, to, 1);
1897}
1898
1899static int
1900add_code_range0(BBuf** pbuf, ScanEnv* env, OnigCodePoint from, OnigCodePoint to, int checkdup)
1901{
1902 if (from > to) {
1903 if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_ALLOW_EMPTY_RANGE_IN_CC))
1904 return 0;
1905 else
1906 return ONIGERR_EMPTY_RANGE_IN_CHAR_CLASS;
1907 }
1908
1909 return add_code_range_to_buf0(pbuf, env, from, to, checkdup);
1910}
1911
1912static int
1913add_code_range(BBuf** pbuf, ScanEnv* env, OnigCodePoint from, OnigCodePoint to)
1914{
1915 return add_code_range0(pbuf, env, from, to, 1);
1916}
1917
1918static int
1919not_code_range_buf(OnigEncoding enc, BBuf* bbuf, BBuf** pbuf, ScanEnv* env)
1920{
1921 int r, i, n;
1922 OnigCodePoint pre, from, *data, to = 0;
1923
1924 *pbuf = (BBuf* )NULL;
1925 if (IS_NULL(bbuf)) {
1926 set_all:
1927 return SET_ALL_MULTI_BYTE_RANGE(enc, pbuf);
1928 }
1929
1930 data = (OnigCodePoint* )(bbuf->p);
1931 GET_CODE_POINT(n, data);
1932 data++;
1933 if (n <= 0) goto set_all;
1934
1935 r = 0;
1936 pre = MBCODE_START_POS(enc);
1937 for (i = 0; i < n; i++) {
1938 from = data[i*2];
1939 to = data[i*2+1];
1940 if (pre <= from - 1) {
1941 r = add_code_range_to_buf(pbuf, env, pre, from - 1);
1942 if (r != 0) return r;
1943 }
1944 if (to == ONIG_LAST_CODE_POINT) break;
1945 pre = to + 1;
1946 }
1947 if (to < ONIG_LAST_CODE_POINT) {
1948 r = add_code_range_to_buf(pbuf, env, to + 1, ONIG_LAST_CODE_POINT);
1949 }
1950 return r;
1951}
1952
1953#define SWAP_BBUF_NOT(bbuf1, not1, bbuf2, not2) do {\
1954 BBuf *tbuf; \
1955 int tnot; \
1956 tnot = not1; not1 = not2; not2 = tnot; \
1957 tbuf = bbuf1; bbuf1 = bbuf2; bbuf2 = tbuf; \
1958} while (0)
1959
1960static int
1961or_code_range_buf(OnigEncoding enc, BBuf* bbuf1, int not1,
1962 BBuf* bbuf2, int not2, BBuf** pbuf, ScanEnv* env)
1963{
1964 int r;
1965 OnigCodePoint i, n1, *data1;
1966 OnigCodePoint from, to;
1967
1968 *pbuf = (BBuf* )NULL;
1969 if (IS_NULL(bbuf1) && IS_NULL(bbuf2)) {
1970 if (not1 != 0 || not2 != 0)
1971 return SET_ALL_MULTI_BYTE_RANGE(enc, pbuf);
1972 return 0;
1973 }
1974
1975 r = 0;
1976 if (IS_NULL(bbuf2))
1977 SWAP_BBUF_NOT(bbuf1, not1, bbuf2, not2);
1978
1979 if (IS_NULL(bbuf1)) {
1980 if (not1 != 0) {
1981 return SET_ALL_MULTI_BYTE_RANGE(enc, pbuf);
1982 }
1983 else {
1984 if (not2 == 0) {
1985 return bbuf_clone(pbuf, bbuf2);
1986 }
1987 else {
1988 return not_code_range_buf(enc, bbuf2, pbuf, env);
1989 }
1990 }
1991 }
1992
1993 if (not1 != 0)
1994 SWAP_BBUF_NOT(bbuf1, not1, bbuf2, not2);
1995
1996 data1 = (OnigCodePoint* )(bbuf1->p);
1997 GET_CODE_POINT(n1, data1);
1998 data1++;
1999
2000 if (not2 == 0 && not1 == 0) { /* 1 OR 2 */
2001 r = bbuf_clone(pbuf, bbuf2);
2002 }
2003 else if (not1 == 0) { /* 1 OR (not 2) */
2004 r = not_code_range_buf(enc, bbuf2, pbuf, env);
2005 }
2006 if (r != 0) return r;
2007
2008 for (i = 0; i < n1; i++) {
2009 from = data1[i*2];
2010 to = data1[i*2+1];
2011 r = add_code_range_to_buf(pbuf, env, from, to);
2012 if (r != 0) return r;
2013 }
2014 return 0;
2015}
2016
2017static int
2018and_code_range1(BBuf** pbuf, ScanEnv* env, OnigCodePoint from1, OnigCodePoint to1,
2019 OnigCodePoint* data, int n)
2020{
2021 int i, r;
2022 OnigCodePoint from2, to2;
2023
2024 for (i = 0; i < n; i++) {
2025 from2 = data[i*2];
2026 to2 = data[i*2+1];
2027 if (from2 < from1) {
2028 if (to2 < from1) continue;
2029 else {
2030 from1 = to2 + 1;
2031 }
2032 }
2033 else if (from2 <= to1) {
2034 if (to2 < to1) {
2035 if (from1 <= from2 - 1) {
2036 r = add_code_range_to_buf(pbuf, env, from1, from2-1);
2037 if (r != 0) return r;
2038 }
2039 from1 = to2 + 1;
2040 }
2041 else {
2042 to1 = from2 - 1;
2043 }
2044 }
2045 else {
2046 from1 = from2;
2047 }
2048 if (from1 > to1) break;
2049 }
2050 if (from1 <= to1) {
2051 r = add_code_range_to_buf(pbuf, env, from1, to1);
2052 if (r != 0) return r;
2053 }
2054 return 0;
2055}
2056
2057static int
2058and_code_range_buf(BBuf* bbuf1, int not1, BBuf* bbuf2, int not2, BBuf** pbuf, ScanEnv* env)
2059{
2060 int r;
2061 OnigCodePoint i, j, n1, n2, *data1, *data2;
2062 OnigCodePoint from, to, from1, to1, from2, to2;
2063
2064 *pbuf = (BBuf* )NULL;
2065 if (IS_NULL(bbuf1)) {
2066 if (not1 != 0 && IS_NOT_NULL(bbuf2)) /* not1 != 0 -> not2 == 0 */
2067 return bbuf_clone(pbuf, bbuf2);
2068 return 0;
2069 }
2070 else if (IS_NULL(bbuf2)) {
2071 if (not2 != 0)
2072 return bbuf_clone(pbuf, bbuf1);
2073 return 0;
2074 }
2075
2076 if (not1 != 0)
2077 SWAP_BBUF_NOT(bbuf1, not1, bbuf2, not2);
2078
2079 data1 = (OnigCodePoint* )(bbuf1->p);
2080 data2 = (OnigCodePoint* )(bbuf2->p);
2081 GET_CODE_POINT(n1, data1);
2082 GET_CODE_POINT(n2, data2);
2083 data1++;
2084 data2++;
2085
2086 if (not2 == 0 && not1 == 0) { /* 1 AND 2 */
2087 for (i = 0; i < n1; i++) {
2088 from1 = data1[i*2];
2089 to1 = data1[i*2+1];
2090 for (j = 0; j < n2; j++) {
2091 from2 = data2[j*2];
2092 to2 = data2[j*2+1];
2093 if (from2 > to1) break;
2094 if (to2 < from1) continue;
2095 from = MAX(from1, from2);
2096 to = MIN(to1, to2);
2097 r = add_code_range_to_buf(pbuf, env, from, to);
2098 if (r != 0) return r;
2099 }
2100 }
2101 }
2102 else if (not1 == 0) { /* 1 AND (not 2) */
2103 for (i = 0; i < n1; i++) {
2104 from1 = data1[i*2];
2105 to1 = data1[i*2+1];
2106 r = and_code_range1(pbuf, env, from1, to1, data2, n2);
2107 if (r != 0) return r;
2108 }
2109 }
2110
2111 return 0;
2112}
2113
2114static int
2115and_cclass(CClassNode* dest, CClassNode* cc, ScanEnv* env)
2116{
2117 OnigEncoding enc = env->enc;
2118 int r, not1, not2;
2119 BBuf *buf1, *buf2, *pbuf = 0;
2120 BitSetRef bsr1, bsr2;
2121 BitSet bs1, bs2;
2122
2123 not1 = IS_NCCLASS_NOT(dest);
2124 bsr1 = dest->bs;
2125 buf1 = dest->mbuf;
2126 not2 = IS_NCCLASS_NOT(cc);
2127 bsr2 = cc->bs;
2128 buf2 = cc->mbuf;
2129
2130 if (not1 != 0) {
2131 bitset_invert_to(bsr1, bs1);
2132 bsr1 = bs1;
2133 }
2134 if (not2 != 0) {
2135 bitset_invert_to(bsr2, bs2);
2136 bsr2 = bs2;
2137 }
2138 bitset_and(bsr1, bsr2);
2139 if (bsr1 != dest->bs) {
2140 bitset_copy(dest->bs, bsr1);
2141 bsr1 = dest->bs;
2142 }
2143 if (not1 != 0) {
2144 bitset_invert(dest->bs);
2145 }
2146
2147 if (! ONIGENC_IS_SINGLEBYTE(enc)) {
2148 if (not1 != 0 && not2 != 0) {
2149 r = or_code_range_buf(enc, buf1, 0, buf2, 0, &pbuf, env);
2150 }
2151 else {
2152 r = and_code_range_buf(buf1, not1, buf2, not2, &pbuf, env);
2153 if (r == 0 && not1 != 0) {
2154 BBuf *tbuf = 0;
2155 r = not_code_range_buf(enc, pbuf, &tbuf, env);
2156 bbuf_free(pbuf);
2157 pbuf = tbuf;
2158 }
2159 }
2160 if (r != 0) {
2161 bbuf_free(pbuf);
2162 return r;
2163 }
2164
2165 dest->mbuf = pbuf;
2166 bbuf_free(buf1);
2167 return r;
2168 }
2169 return 0;
2170}
2171
2172static int
2173or_cclass(CClassNode* dest, CClassNode* cc, ScanEnv* env)
2174{
2175 OnigEncoding enc = env->enc;
2176 int r, not1, not2;
2177 BBuf *buf1, *buf2, *pbuf = 0;
2178 BitSetRef bsr1, bsr2;
2179 BitSet bs1, bs2;
2180
2181 not1 = IS_NCCLASS_NOT(dest);
2182 bsr1 = dest->bs;
2183 buf1 = dest->mbuf;
2184 not2 = IS_NCCLASS_NOT(cc);
2185 bsr2 = cc->bs;
2186 buf2 = cc->mbuf;
2187
2188 if (not1 != 0) {
2189 bitset_invert_to(bsr1, bs1);
2190 bsr1 = bs1;
2191 }
2192 if (not2 != 0) {
2193 bitset_invert_to(bsr2, bs2);
2194 bsr2 = bs2;
2195 }
2196 bitset_or(bsr1, bsr2);
2197 if (bsr1 != dest->bs) {
2198 bitset_copy(dest->bs, bsr1);
2199 bsr1 = dest->bs;
2200 }
2201 if (not1 != 0) {
2202 bitset_invert(dest->bs);
2203 }
2204
2205 if (! ONIGENC_IS_SINGLEBYTE(enc)) {
2206 if (not1 != 0 && not2 != 0) {
2207 r = and_code_range_buf(buf1, 0, buf2, 0, &pbuf, env);
2208 }
2209 else {
2210 r = or_code_range_buf(enc, buf1, not1, buf2, not2, &pbuf, env);
2211 if (r == 0 && not1 != 0) {
2212 BBuf *tbuf = 0;
2213 r = not_code_range_buf(enc, pbuf, &tbuf, env);
2214 bbuf_free(pbuf);
2215 pbuf = tbuf;
2216 }
2217 }
2218 if (r != 0) {
2219 bbuf_free(pbuf);
2220 return r;
2221 }
2222
2223 dest->mbuf = pbuf;
2224 bbuf_free(buf1);
2225 return r;
2226 }
2227 else
2228 return 0;
2229}
2230
2231static void UNKNOWN_ESC_WARN(ScanEnv *env, int c);
2232
2233static OnigCodePoint
2234conv_backslash_value(OnigCodePoint c, ScanEnv* env)
2235{
2236 if (IS_SYNTAX_OP(env->syntax, ONIG_SYN_OP_ESC_CONTROL_CHARS)) {
2237 switch (c) {
2238 case 'n': return '\n';
2239 case 't': return '\t';
2240 case 'r': return '\r';
2241 case 'f': return '\f';
2242 case 'a': return '\007';
2243 case 'b': return '\010';
2244 case 'e': return '\033';
2245 case 'v':
2246 if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_ESC_V_VTAB))
2247 return '\v';
2248 break;
2249
2250 default:
2251 if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'))
2252 UNKNOWN_ESC_WARN(env, c);
2253 break;
2254 }
2255 }
2256 return c;
2257}
2258
2259#ifdef USE_NO_INVALID_QUANTIFIER
2260# define is_invalid_quantifier_target(node) 0
2261#else
2262static int
2263is_invalid_quantifier_target(Node* node)
2264{
2265 switch (NTYPE(node)) {
2266 case NT_ANCHOR:
2267 return 1;
2268 break;
2269
2270 case NT_ENCLOSE:
2271 /* allow enclosed elements */
2272 /* return is_invalid_quantifier_target(NENCLOSE(node)->target); */
2273 break;
2274
2275 case NT_LIST:
2276 do {
2277 if (! is_invalid_quantifier_target(NCAR(node))) return 0;
2278 } while (IS_NOT_NULL(node = NCDR(node)));
2279 return 0;
2280 break;
2281
2282 case NT_ALT:
2283 do {
2284 if (is_invalid_quantifier_target(NCAR(node))) return 1;
2285 } while (IS_NOT_NULL(node = NCDR(node)));
2286 break;
2287
2288 default:
2289 break;
2290 }
2291 return 0;
2292}
2293#endif
2294
2295/* ?:0, *:1, +:2, ??:3, *?:4, +?:5 */
2296static int
2297popular_quantifier_num(QtfrNode* q)
2298{
2299 if (q->greedy) {
2300 if (q->lower == 0) {
2301 if (q->upper == 1) return 0;
2302 else if (IS_REPEAT_INFINITE(q->upper)) return 1;
2303 }
2304 else if (q->lower == 1) {
2305 if (IS_REPEAT_INFINITE(q->upper)) return 2;
2306 }
2307 }
2308 else {
2309 if (q->lower == 0) {
2310 if (q->upper == 1) return 3;
2311 else if (IS_REPEAT_INFINITE(q->upper)) return 4;
2312 }
2313 else if (q->lower == 1) {
2314 if (IS_REPEAT_INFINITE(q->upper)) return 5;
2315 }
2316 }
2317 return -1;
2318}
2319
2320
2321enum ReduceType {
2322 RQ_ASIS = 0, /* as is */
2323 RQ_DEL = 1, /* delete parent */
2324 RQ_A, /* to '*' */
2325 RQ_AQ, /* to '*?' */
2326 RQ_QQ, /* to '??' */
2327 RQ_P_QQ, /* to '+)??' */
2328};
2329
2330static enum ReduceType const ReduceTypeTable[6][6] = {
2331/* '?', '*', '+', '??', '*?', '+?' p / c */
2332 {RQ_DEL, RQ_A, RQ_A, RQ_QQ, RQ_AQ, RQ_ASIS}, /* '?' */
2333 {RQ_DEL, RQ_DEL, RQ_DEL, RQ_P_QQ, RQ_P_QQ, RQ_DEL}, /* '*' */
2334 {RQ_A, RQ_A, RQ_DEL, RQ_ASIS, RQ_P_QQ, RQ_DEL}, /* '+' */
2335 {RQ_DEL, RQ_AQ, RQ_AQ, RQ_DEL, RQ_AQ, RQ_AQ}, /* '??' */
2336 {RQ_DEL, RQ_DEL, RQ_DEL, RQ_DEL, RQ_DEL, RQ_DEL}, /* '*?' */
2337 {RQ_ASIS, RQ_ASIS, RQ_ASIS, RQ_AQ, RQ_AQ, RQ_DEL} /* '+?' */
2338};
2339
2340extern void
2341onig_reduce_nested_quantifier(Node* pnode, Node* cnode)
2342{
2343 int pnum, cnum;
2344 QtfrNode *p, *c;
2345
2346 p = NQTFR(pnode);
2347 c = NQTFR(cnode);
2348 pnum = popular_quantifier_num(p);
2349 cnum = popular_quantifier_num(c);
2350 if (pnum < 0 || cnum < 0) return ;
2351
2352 switch (ReduceTypeTable[cnum][pnum]) {
2353 case RQ_DEL:
2354 *pnode = *cnode;
2355 break;
2356 case RQ_A:
2357 p->target = c->target;
2358 p->lower = 0; p->upper = REPEAT_INFINITE; p->greedy = 1;
2359 break;
2360 case RQ_AQ:
2361 p->target = c->target;
2362 p->lower = 0; p->upper = REPEAT_INFINITE; p->greedy = 0;
2363 break;
2364 case RQ_QQ:
2365 p->target = c->target;
2366 p->lower = 0; p->upper = 1; p->greedy = 0;
2367 break;
2368 case RQ_P_QQ:
2369 p->target = cnode;
2370 p->lower = 0; p->upper = 1; p->greedy = 0;
2371 c->lower = 1; c->upper = REPEAT_INFINITE; c->greedy = 1;
2372 return ;
2373 break;
2374 case RQ_ASIS:
2375 p->target = cnode;
2376 return ;
2377 break;
2378 }
2379
2380 c->target = NULL_NODE;
2381 onig_node_free(cnode);
2382}
2383
2384
2385enum TokenSyms {
2386 TK_EOT = 0, /* end of token */
2387 TK_RAW_BYTE = 1,
2388 TK_CHAR,
2389 TK_STRING,
2390 TK_CODE_POINT,
2391 TK_ANYCHAR,
2392 TK_CHAR_TYPE,
2393 TK_BACKREF,
2394 TK_CALL,
2395 TK_ANCHOR,
2396 TK_OP_REPEAT,
2397 TK_INTERVAL,
2398 TK_ANYCHAR_ANYTIME, /* SQL '%' == .* */
2399 TK_ALT,
2400 TK_SUBEXP_OPEN,
2401 TK_SUBEXP_CLOSE,
2402 TK_CC_OPEN,
2403 TK_QUOTE_OPEN,
2404 TK_CHAR_PROPERTY, /* \p{...}, \P{...} */
2405 TK_LINEBREAK,
2406 TK_EXTENDED_GRAPHEME_CLUSTER,
2407 TK_KEEP,
2408 /* in cc */
2409 TK_CC_CLOSE,
2410 TK_CC_RANGE,
2411 TK_POSIX_BRACKET_OPEN,
2412 TK_CC_AND, /* && */
2413 TK_CC_CC_OPEN /* [ */
2414};
2415
2416 typedef struct {
2417 enum TokenSyms type;
2418 int escaped;
2419 int base; /* is number: 8, 16 (used in [....]) */
2420 UChar* backp;
2421 union {
2422 UChar* s;
2423 int c;
2424 OnigCodePoint code;
2425 struct {
2426 int subtype;
2427 int ascii_range;
2428 } anchor;
2429 struct {
2430 int lower;
2431 int upper;
2432 int greedy;
2433 int possessive;
2434 } repeat;
2435 struct {
2436 int num;
2437 int ref1;
2438 int* refs;
2439 int by_name;
2440#ifdef USE_BACKREF_WITH_LEVEL
2441 int exist_level;
2442 int level; /* \k<name+n> */
2443#endif
2444 } backref;
2445 struct {
2446 UChar* name;
2447 UChar* name_end;
2448 int gnum;
2449 int rel;
2450 } call;
2451 struct {
2452 int ctype;
2453 int not;
2454 } prop;
2455 } u;
2456} OnigToken;
2457
2458
2459static int
2460fetch_range_quantifier(UChar** src, UChar* end, OnigToken* tok, ScanEnv* env)
2461{
2462 int low, up, syn_allow, non_low = 0;
2463 int r = 0;
2464 OnigCodePoint c;
2465 OnigEncoding enc = env->enc;
2466 UChar* p = *src;
2467 PFETCH_READY;
2468
2469 syn_allow = IS_SYNTAX_BV(env->syntax, ONIG_SYN_ALLOW_INVALID_INTERVAL);
2470
2471 if (PEND) {
2472 if (syn_allow)
2473 return 1; /* "....{" : OK! */
2474 else
2475 return ONIGERR_END_PATTERN_AT_LEFT_BRACE; /* "....{" syntax error */
2476 }
2477
2478 if (! syn_allow) {
2479 c = PPEEK;
2480 if (c == ')' || c == '(' || c == '|') {
2481 return ONIGERR_END_PATTERN_AT_LEFT_BRACE;
2482 }
2483 }
2484
2485 low = onig_scan_unsigned_number(&p, end, env->enc);
2486 if (low < 0) return ONIGERR_TOO_BIG_NUMBER_FOR_REPEAT_RANGE;
2487 if (low > ONIG_MAX_REPEAT_NUM)
2488 return ONIGERR_TOO_BIG_NUMBER_FOR_REPEAT_RANGE;
2489
2490 if (p == *src) { /* can't read low */
2491 if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_ALLOW_INTERVAL_LOW_ABBREV)) {
2492 /* allow {,n} as {0,n} */
2493 low = 0;
2494 non_low = 1;
2495 }
2496 else
2497 goto invalid;
2498 }
2499
2500 if (PEND) goto invalid;
2501 PFETCH(c);
2502 if (c == ',') {
2503 UChar* prev = p;
2504 up = onig_scan_unsigned_number(&p, end, env->enc);
2505 if (up < 0) return ONIGERR_TOO_BIG_NUMBER_FOR_REPEAT_RANGE;
2506 if (up > ONIG_MAX_REPEAT_NUM)
2507 return ONIGERR_TOO_BIG_NUMBER_FOR_REPEAT_RANGE;
2508
2509 if (p == prev) {
2510 if (non_low != 0)
2511 goto invalid;
2512 up = REPEAT_INFINITE; /* {n,} : {n,infinite} */
2513 }
2514 }
2515 else {
2516 if (non_low != 0)
2517 goto invalid;
2518
2519 PUNFETCH;
2520 up = low; /* {n} : exact n times */
2521 r = 2; /* fixed */
2522 }
2523
2524 if (PEND) goto invalid;
2525 PFETCH(c);
2526 if (IS_SYNTAX_OP(env->syntax, ONIG_SYN_OP_ESC_BRACE_INTERVAL)) {
2527 if (c != MC_ESC(env->syntax)) goto invalid;
2528 if (PEND) goto invalid;
2529 PFETCH(c);
2530 }
2531 if (c != '}') goto invalid;
2532
2533 if (!IS_REPEAT_INFINITE(up) && low > up) {
2534 return ONIGERR_UPPER_SMALLER_THAN_LOWER_IN_REPEAT_RANGE;
2535 }
2536
2537 tok->type = TK_INTERVAL;
2538 tok->u.repeat.lower = low;
2539 tok->u.repeat.upper = up;
2540 *src = p;
2541 return r; /* 0: normal {n,m}, 2: fixed {n} */
2542
2543 invalid:
2544 if (syn_allow)
2545 return 1; /* OK */
2546 else
2547 return ONIGERR_INVALID_REPEAT_RANGE_PATTERN;
2548}
2549
2550/* \M-, \C-, \c, or \... */
2551static int
2552fetch_escaped_value(UChar** src, UChar* end, ScanEnv* env, OnigCodePoint* val)
2553{
2554 int v;
2555 OnigCodePoint c;
2556 OnigEncoding enc = env->enc;
2557 UChar* p = *src;
2558
2559 if (PEND) return ONIGERR_END_PATTERN_AT_ESCAPE;
2560
2561 PFETCH_S(c);
2562 switch (c) {
2563 case 'M':
2564 if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_ESC_CAPITAL_M_BAR_META)) {
2565 if (PEND) return ONIGERR_END_PATTERN_AT_META;
2566 PFETCH_S(c);
2567 if (c != '-') return ONIGERR_META_CODE_SYNTAX;
2568 if (PEND) return ONIGERR_END_PATTERN_AT_META;
2569 PFETCH_S(c);
2570 if (c == MC_ESC(env->syntax)) {
2571 v = fetch_escaped_value(&p, end, env, &c);
2572 if (v < 0) return v;
2573 }
2574 c = ((c & 0xff) | 0x80);
2575 }
2576 else
2577 goto backslash;
2578 break;
2579
2580 case 'C':
2581 if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_ESC_CAPITAL_C_BAR_CONTROL)) {
2582 if (PEND) return ONIGERR_END_PATTERN_AT_CONTROL;
2583 PFETCH_S(c);
2584 if (c != '-') return ONIGERR_CONTROL_CODE_SYNTAX;
2585 goto control;
2586 }
2587 else
2588 goto backslash;
2589
2590 case 'c':
2591 if (IS_SYNTAX_OP(env->syntax, ONIG_SYN_OP_ESC_C_CONTROL)) {
2592 control:
2593 if (PEND) return ONIGERR_END_PATTERN_AT_CONTROL;
2594 PFETCH_S(c);
2595 if (c == '?') {
2596 c = 0177;
2597 }
2598 else {
2599 if (c == MC_ESC(env->syntax)) {
2600 v = fetch_escaped_value(&p, end, env, &c);
2601 if (v < 0) return v;
2602 }
2603 c &= 0x9f;
2604 }
2605 break;
2606 }
2607 /* fall through */
2608
2609 default:
2610 {
2611 backslash:
2612 c = conv_backslash_value(c, env);
2613 }
2614 break;
2615 }
2616
2617 *src = p;
2618 *val = c;
2619 return 0;
2620}
2621
2622static int fetch_token(OnigToken* tok, UChar** src, UChar* end, ScanEnv* env);
2623
2624static OnigCodePoint
2625get_name_end_code_point(OnigCodePoint start)
2626{
2627 switch (start) {
2628 case '<': return (OnigCodePoint )'>'; break;
2629 case '\'': return (OnigCodePoint )'\''; break;
2630 case '(': return (OnigCodePoint )')'; break;
2631 case '{': return (OnigCodePoint )'}'; break;
2632 default:
2633 break;
2634 }
2635
2636 return (OnigCodePoint )0;
2637}
2638
2639#ifdef USE_NAMED_GROUP
2640# ifdef RUBY
2641# define ONIGENC_IS_CODE_NAME(enc, c) TRUE
2642# else
2643# define ONIGENC_IS_CODE_NAME(enc, c) ONIGENC_IS_CODE_WORD(enc, c)
2644# endif
2645
2646# ifdef USE_BACKREF_WITH_LEVEL
2647/*
2648 \k<name+n>, \k<name-n>
2649 \k<num+n>, \k<num-n>
2650 \k<-num+n>, \k<-num-n>
2651*/
2652static int
2653fetch_name_with_level(OnigCodePoint start_code, UChar** src, UChar* end,
2654 UChar** rname_end, ScanEnv* env,
2655 int* rback_num, int* rlevel)
2656{
2657 int r, sign, is_num, exist_level;
2658 OnigCodePoint end_code;
2659 OnigCodePoint c = 0;
2660 OnigEncoding enc = env->enc;
2661 UChar *name_end;
2662 UChar *pnum_head;
2663 UChar *p = *src;
2664 PFETCH_READY;
2665
2666 *rback_num = 0;
2667 is_num = exist_level = 0;
2668 sign = 1;
2669 pnum_head = *src;
2670
2671 end_code = get_name_end_code_point(start_code);
2672
2673 name_end = end;
2674 r = 0;
2675 if (PEND) {
2676 return ONIGERR_EMPTY_GROUP_NAME;
2677 }
2678 else {
2679 PFETCH(c);
2680 if (c == end_code)
2681 return ONIGERR_EMPTY_GROUP_NAME;
2682
2683 if (ONIGENC_IS_CODE_DIGIT(enc, c)) {
2684 is_num = 1;
2685 }
2686 else if (c == '-') {
2687 is_num = 2;
2688 sign = -1;
2689 pnum_head = p;
2690 }
2691 else if (!ONIGENC_IS_CODE_NAME(enc, c)) {
2692 r = ONIGERR_INVALID_CHAR_IN_GROUP_NAME;
2693 }
2694 }
2695
2696 while (!PEND) {
2697 name_end = p;
2698 PFETCH(c);
2699 if (c == end_code || c == ')' || c == '+' || c == '-') {
2700 if (is_num == 2) r = ONIGERR_INVALID_GROUP_NAME;
2701 break;
2702 }
2703
2704 if (is_num != 0) {
2705 if (ONIGENC_IS_CODE_DIGIT(enc, c)) {
2706 is_num = 1;
2707 }
2708 else {
2709 r = ONIGERR_INVALID_GROUP_NAME;
2710 is_num = 0;
2711 }
2712 }
2713 else if (!ONIGENC_IS_CODE_NAME(enc, c)) {
2714 r = ONIGERR_INVALID_CHAR_IN_GROUP_NAME;
2715 }
2716 }
2717
2718 if (r == 0 && c != end_code) {
2719 if (c == '+' || c == '-') {
2720 int level;
2721 int flag = (c == '-' ? -1 : 1);
2722
2723 if (PEND) {
2724 r = ONIGERR_INVALID_CHAR_IN_GROUP_NAME;
2725 goto end;
2726 }
2727 PFETCH(c);
2728 if (! ONIGENC_IS_CODE_DIGIT(enc, c)) goto err;
2729 PUNFETCH;
2730 level = onig_scan_unsigned_number(&p, end, enc);
2731 if (level < 0) return ONIGERR_TOO_BIG_NUMBER;
2732 *rlevel = (level * flag);
2733 exist_level = 1;
2734
2735 if (!PEND) {
2736 PFETCH(c);
2737 if (c == end_code)
2738 goto end;
2739 }
2740 }
2741
2742 err:
2743 r = ONIGERR_INVALID_GROUP_NAME;
2744 name_end = end;
2745 }
2746
2747 end:
2748 if (r == 0) {
2749 if (is_num != 0) {
2750 *rback_num = onig_scan_unsigned_number(&pnum_head, name_end, enc);
2751 if (*rback_num < 0) return ONIGERR_TOO_BIG_NUMBER;
2752 else if (*rback_num == 0) goto err;
2753
2754 *rback_num *= sign;
2755 }
2756
2757 *rname_end = name_end;
2758 *src = p;
2759 return (exist_level ? 1 : 0);
2760 }
2761 else {
2762 onig_scan_env_set_error_string(env, r, *src, name_end);
2763 return r;
2764 }
2765}
2766# endif /* USE_BACKREF_WITH_LEVEL */
2767
2768/*
2769 ref: 0 -> define name (don't allow number name)
2770 1 -> reference name (allow number name)
2771*/
2772static int
2773fetch_name(OnigCodePoint start_code, UChar** src, UChar* end,
2774 UChar** rname_end, ScanEnv* env, int* rback_num, int ref)
2775{
2776 int r, is_num, sign;
2777 OnigCodePoint end_code;
2778 OnigCodePoint c = 0;
2779 OnigEncoding enc = env->enc;
2780 UChar *name_end;
2781 UChar *pnum_head;
2782 UChar *p = *src;
2783
2784 *rback_num = 0;
2785
2786 end_code = get_name_end_code_point(start_code);
2787
2788 name_end = end;
2789 pnum_head = *src;
2790 r = 0;
2791 is_num = 0;
2792 sign = 1;
2793 if (PEND) {
2794 return ONIGERR_EMPTY_GROUP_NAME;
2795 }
2796 else {
2797 PFETCH_S(c);
2798 if (c == end_code)
2799 return ONIGERR_EMPTY_GROUP_NAME;
2800
2801 if (ONIGENC_IS_CODE_DIGIT(enc, c)) {
2802 if (ref == 1)
2803 is_num = 1;
2804 else {
2805 r = ONIGERR_INVALID_GROUP_NAME;
2806 is_num = 0;
2807 }
2808 }
2809 else if (c == '-') {
2810 if (ref == 1) {
2811 is_num = 2;
2812 sign = -1;
2813 pnum_head = p;
2814 }
2815 else {
2816 r = ONIGERR_INVALID_GROUP_NAME;
2817 is_num = 0;
2818 }
2819 }
2820 else if (!ONIGENC_IS_CODE_NAME(enc, c)) {
2821 r = ONIGERR_INVALID_CHAR_IN_GROUP_NAME;
2822 }
2823 }
2824
2825 if (r == 0) {
2826 while (!PEND) {
2827 name_end = p;
2828 PFETCH_S(c);
2829 if (c == end_code || c == ')') {
2830 if (is_num == 2) {
2831 r = ONIGERR_INVALID_GROUP_NAME;
2832 goto teardown;
2833 }
2834 break;
2835 }
2836
2837 if (is_num != 0) {
2838 if (ONIGENC_IS_CODE_DIGIT(enc, c)) {
2839 is_num = 1;
2840 }
2841 else {
2842 if (!ONIGENC_IS_CODE_WORD(enc, c))
2843 r = ONIGERR_INVALID_CHAR_IN_GROUP_NAME;
2844 else
2845 r = ONIGERR_INVALID_GROUP_NAME;
2846 goto teardown;
2847 }
2848 }
2849 else {
2850 if (!ONIGENC_IS_CODE_NAME(enc, c)) {
2851 r = ONIGERR_INVALID_CHAR_IN_GROUP_NAME;
2852 goto teardown;
2853 }
2854 }
2855 }
2856
2857 if (c != end_code) {
2858 r = ONIGERR_INVALID_GROUP_NAME;
2859 name_end = end;
2860 goto err;
2861 }
2862
2863 if (is_num != 0) {
2864 *rback_num = onig_scan_unsigned_number(&pnum_head, name_end, enc);
2865 if (*rback_num < 0) return ONIGERR_TOO_BIG_NUMBER;
2866 else if (*rback_num == 0) {
2867 r = ONIGERR_INVALID_GROUP_NAME;
2868 goto err;
2869 }
2870
2871 *rback_num *= sign;
2872 }
2873
2874 *rname_end = name_end;
2875 *src = p;
2876 return 0;
2877 }
2878 else {
2879teardown:
2880 while (!PEND) {
2881 name_end = p;
2882 PFETCH_S(c);
2883 if (c == end_code || c == ')')
2884 break;
2885 }
2886 if (PEND)
2887 name_end = end;
2888
2889 err:
2890 onig_scan_env_set_error_string(env, r, *src, name_end);
2891 return r;
2892 }
2893}
2894#else
2895static int
2896fetch_name(OnigCodePoint start_code, UChar** src, UChar* end,
2897 UChar** rname_end, ScanEnv* env, int* rback_num, int ref)
2898{
2899 int r, is_num, sign;
2900 OnigCodePoint end_code;
2901 OnigCodePoint c = 0;
2902 UChar *name_end;
2903 OnigEncoding enc = env->enc;
2904 UChar *pnum_head;
2905 UChar *p = *src;
2906 PFETCH_READY;
2907
2908 *rback_num = 0;
2909
2910 end_code = get_name_end_code_point(start_code);
2911
2912 *rname_end = name_end = end;
2913 r = 0;
2914 pnum_head = *src;
2915 is_num = 0;
2916 sign = 1;
2917
2918 if (PEND) {
2919 return ONIGERR_EMPTY_GROUP_NAME;
2920 }
2921 else {
2922 PFETCH(c);
2923 if (c == end_code)
2924 return ONIGERR_EMPTY_GROUP_NAME;
2925
2926 if (ONIGENC_IS_CODE_DIGIT(enc, c)) {
2927 is_num = 1;
2928 }
2929 else if (c == '-') {
2930 is_num = 2;
2931 sign = -1;
2932 pnum_head = p;
2933 }
2934 else {
2935 r = ONIGERR_INVALID_CHAR_IN_GROUP_NAME;
2936 }
2937 }
2938
2939 while (!PEND) {
2940 name_end = p;
2941
2942 PFETCH(c);
2943 if (c == end_code || c == ')') break;
2944 if (! ONIGENC_IS_CODE_DIGIT(enc, c))
2945 r = ONIGERR_INVALID_CHAR_IN_GROUP_NAME;
2946 }
2947 if (r == 0 && c != end_code) {
2948 r = ONIGERR_INVALID_GROUP_NAME;
2949 name_end = end;
2950 }
2951
2952 if (r == 0) {
2953 *rback_num = onig_scan_unsigned_number(&pnum_head, name_end, enc);
2954 if (*rback_num < 0) return ONIGERR_TOO_BIG_NUMBER;
2955 else if (*rback_num == 0) {
2956 r = ONIGERR_INVALID_GROUP_NAME;
2957 goto err;
2958 }
2959 *rback_num *= sign;
2960
2961 *rname_end = name_end;
2962 *src = p;
2963 return 0;
2964 }
2965 else {
2966 err:
2967 onig_scan_env_set_error_string(env, r, *src, name_end);
2968 return r;
2969 }
2970}
2971#endif /* USE_NAMED_GROUP */
2972
2973
2974#ifdef PRINTF_ARGS
2975PRINTF_ARGS(static void onig_syntax_warn(ScanEnv *env, const char *fmt, ...), 2, 3);
2976#endif
2977
2978static void
2979onig_syntax_warn(ScanEnv *env, const char *fmt, ...)
2980{
2981 va_list args;
2982 UChar buf[WARN_BUFSIZE];
2983 va_start(args, fmt);
2984 onig_vsnprintf_with_pattern(buf, WARN_BUFSIZE, env->enc,
2985 env->pattern, env->pattern_end,
2986 fmt, args);
2987 va_end(args);
2988#ifdef RUBY
2989 if (env->sourcefile == NULL)
2990 rb_warn("%s", (char *)buf);
2991 else
2992 rb_compile_warn(env->sourcefile, env->sourceline, "%s", (char *)buf);
2993#else
2994 (*onig_warn)((char* )buf);
2995#endif
2996}
2997
2998static void
2999CC_ESC_WARN(ScanEnv *env, UChar *c)
3000{
3001 if (onig_warn == onig_null_warn) return ;
3002
3003 if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_WARN_CC_OP_NOT_ESCAPED) &&
3004 IS_SYNTAX_BV(env->syntax, ONIG_SYN_BACKSLASH_ESCAPE_IN_CC)) {
3005 onig_syntax_warn(env, "character class has '%s' without escape", c);
3006 }
3007}
3008
3009static void
3010CLOSE_BRACKET_WITHOUT_ESC_WARN(ScanEnv* env, UChar* c)
3011{
3012 if (onig_warn == onig_null_warn) return ;
3013
3014 if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_WARN_CC_OP_NOT_ESCAPED)) {
3015 onig_syntax_warn(env, "regular expression has '%s' without escape", c);
3016 }
3017}
3018
3019#ifndef RTEST
3020# define RTEST(v) 1
3021#endif
3022
3023static void
3024CC_DUP_WARN(ScanEnv *env, OnigCodePoint from ARG_UNUSED, OnigCodePoint to ARG_UNUSED)
3025{
3026 if (onig_warn == onig_null_warn || !RTEST(ruby_verbose)) return ;
3027
3028 if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_WARN_CC_DUP) &&
3029 !(env->warnings_flag & ONIG_SYN_WARN_CC_DUP)) {
3030#ifdef WARN_ALL_CC_DUP
3031 onig_syntax_warn(env, "character class has duplicated range: %04x-%04x", from, to);
3032#else
3033 env->warnings_flag |= ONIG_SYN_WARN_CC_DUP;
3034 onig_syntax_warn(env, "character class has duplicated range");
3035#endif
3036 }
3037}
3038
3039static void
3040UNKNOWN_ESC_WARN(ScanEnv *env, int c)
3041{
3042 if (onig_warn == onig_null_warn || !RTEST(ruby_verbose)) return ;
3043 onig_syntax_warn(env, "Unknown escape \\%c is ignored", c);
3044}
3045
3046static UChar*
3047find_str_position(OnigCodePoint s[], int n, UChar* from, UChar* to,
3048 UChar **next, OnigEncoding enc)
3049{
3050 int i;
3051 OnigCodePoint x;
3052 UChar *q;
3053 UChar *p = from;
3054
3055 while (p < to) {
3056 x = ONIGENC_MBC_TO_CODE(enc, p, to);
3057 q = p + enclen(enc, p, to);
3058 if (x == s[0]) {
3059 for (i = 1; i < n && q < to; i++) {
3060 x = ONIGENC_MBC_TO_CODE(enc, q, to);
3061 if (x != s[i]) break;
3062 q += enclen(enc, q, to);
3063 }
3064 if (i >= n) {
3065 if (IS_NOT_NULL(next))
3066 *next = q;
3067 return p;
3068 }
3069 }
3070 p = q;
3071 }
3072 return NULL_UCHARP;
3073}
3074
3075static int
3076str_exist_check_with_esc(OnigCodePoint s[], int n, UChar* from, UChar* to,
3077 OnigCodePoint bad, OnigEncoding enc, const OnigSyntaxType* syn)
3078{
3079 int i, in_esc;
3080 OnigCodePoint x;
3081 UChar *q;
3082 UChar *p = from;
3083
3084 in_esc = 0;
3085 while (p < to) {
3086 if (in_esc) {
3087 in_esc = 0;
3088 p += enclen(enc, p, to);
3089 }
3090 else {
3091 x = ONIGENC_MBC_TO_CODE(enc, p, to);
3092 q = p + enclen(enc, p, to);
3093 if (x == s[0]) {
3094 for (i = 1; i < n && q < to; i++) {
3095 x = ONIGENC_MBC_TO_CODE(enc, q, to);
3096 if (x != s[i]) break;
3097 q += enclen(enc, q, to);
3098 }
3099 if (i >= n) return 1;
3100 p += enclen(enc, p, to);
3101 }
3102 else {
3103 x = ONIGENC_MBC_TO_CODE(enc, p, to);
3104 if (x == bad) return 0;
3105 else if (x == MC_ESC(syn)) in_esc = 1;
3106 p = q;
3107 }
3108 }
3109 }
3110 return 0;
3111}
3112
3113static int
3114fetch_token_in_cc(OnigToken* tok, UChar** src, UChar* end, ScanEnv* env)
3115{
3116 int num;
3117 OnigCodePoint c, c2;
3118 const OnigSyntaxType* syn = env->syntax;
3119 OnigEncoding enc = env->enc;
3120 UChar* prev;
3121 UChar* p = *src;
3122 PFETCH_READY;
3123
3124 if (PEND) {
3125 tok->type = TK_EOT;
3126 return tok->type;
3127 }
3128
3129 PFETCH(c);
3130 tok->type = TK_CHAR;
3131 tok->base = 0;
3132 tok->u.c = c;
3133 tok->escaped = 0;
3134
3135 if (c == ']') {
3136 tok->type = TK_CC_CLOSE;
3137 }
3138 else if (c == '-') {
3139 tok->type = TK_CC_RANGE;
3140 }
3141 else if (c == MC_ESC(syn)) {
3142 if (! IS_SYNTAX_BV(syn, ONIG_SYN_BACKSLASH_ESCAPE_IN_CC))
3143 goto end;
3144
3145 if (PEND) return ONIGERR_END_PATTERN_AT_ESCAPE;
3146
3147 PFETCH(c);
3148 tok->escaped = 1;
3149 tok->u.c = c;
3150 switch (c) {
3151 case 'w':
3152 tok->type = TK_CHAR_TYPE;
3153 tok->u.prop.ctype = ONIGENC_CTYPE_WORD;
3154 tok->u.prop.not = 0;
3155 break;
3156 case 'W':
3157 tok->type = TK_CHAR_TYPE;
3158 tok->u.prop.ctype = ONIGENC_CTYPE_WORD;
3159 tok->u.prop.not = 1;
3160 break;
3161 case 'd':
3162 tok->type = TK_CHAR_TYPE;
3163 tok->u.prop.ctype = ONIGENC_CTYPE_DIGIT;
3164 tok->u.prop.not = 0;
3165 break;
3166 case 'D':
3167 tok->type = TK_CHAR_TYPE;
3168 tok->u.prop.ctype = ONIGENC_CTYPE_DIGIT;
3169 tok->u.prop.not = 1;
3170 break;
3171 case 's':
3172 tok->type = TK_CHAR_TYPE;
3173 tok->u.prop.ctype = ONIGENC_CTYPE_SPACE;
3174 tok->u.prop.not = 0;
3175 break;
3176 case 'S':
3177 tok->type = TK_CHAR_TYPE;
3178 tok->u.prop.ctype = ONIGENC_CTYPE_SPACE;
3179 tok->u.prop.not = 1;
3180 break;
3181 case 'h':
3182 if (! IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_H_XDIGIT)) break;
3183 tok->type = TK_CHAR_TYPE;
3184 tok->u.prop.ctype = ONIGENC_CTYPE_XDIGIT;
3185 tok->u.prop.not = 0;
3186 break;
3187 case 'H':
3188 if (! IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_H_XDIGIT)) break;
3189 tok->type = TK_CHAR_TYPE;
3190 tok->u.prop.ctype = ONIGENC_CTYPE_XDIGIT;
3191 tok->u.prop.not = 1;
3192 break;
3193
3194 case 'p':
3195 case 'P':
3196 if (PEND) break;
3197
3198 c2 = PPEEK;
3199 if (c2 == '{' &&
3200 IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_P_BRACE_CHAR_PROPERTY)) {
3201 PINC;
3202 tok->type = TK_CHAR_PROPERTY;
3203 tok->u.prop.not = (c == 'P' ? 1 : 0);
3204
3205 if (!PEND && IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_P_BRACE_CIRCUMFLEX_NOT)) {
3206 PFETCH(c2);
3207 if (c2 == '^') {
3208 tok->u.prop.not = (tok->u.prop.not == 0 ? 1 : 0);
3209 }
3210 else
3211 PUNFETCH;
3212 }
3213 }
3214 else {
3215 onig_syntax_warn(env, "invalid Unicode Property \\%c", c);
3216 }
3217 break;
3218
3219 case 'x':
3220 if (PEND) break;
3221
3222 prev = p;
3223 if (PPEEK_IS('{') && IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_X_BRACE_HEX8)) {
3224 PINC;
3225 num = scan_unsigned_hexadecimal_number(&p, end, 0, 8, enc);
3226 if (num < 0) return ONIGERR_TOO_BIG_WIDE_CHAR_VALUE;
3227 if (!PEND) {
3228 c2 = PPEEK;
3229 if (ONIGENC_IS_CODE_XDIGIT(enc, c2))
3230 return ONIGERR_TOO_LONG_WIDE_CHAR_VALUE;
3231 }
3232
3233 if (p > prev + enclen(enc, prev, end) && !PEND && (PPEEK_IS('}'))) {
3234 PINC;
3235 tok->type = TK_CODE_POINT;
3236 tok->base = 16;
3237 tok->u.code = (OnigCodePoint )num;
3238 }
3239 else {
3240 /* can't read nothing or invalid format */
3241 p = prev;
3242 }
3243 }
3244 else if (IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_X_HEX2)) {
3245 num = scan_unsigned_hexadecimal_number(&p, end, 0, 2, enc);
3246 if (num < 0) return ONIGERR_TOO_BIG_NUMBER;
3247 if (p == prev) { /* can't read nothing. */
3248 num = 0; /* but, it's not error */
3249 }
3250 tok->type = TK_RAW_BYTE;
3251 tok->base = 16;
3252 tok->u.c = num;
3253 }
3254 break;
3255
3256 case 'u':
3257 if (PEND) break;
3258
3259 prev = p;
3260 if (IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_U_HEX4)) {
3261 num = scan_unsigned_hexadecimal_number(&p, end, 4, 4, enc);
3262 if (num < -1) return ONIGERR_TOO_SHORT_DIGITS;
3263 else if (num < 0) return ONIGERR_TOO_BIG_NUMBER;
3264 if (p == prev) { /* can't read nothing. */
3265 num = 0; /* but, it's not error */
3266 }
3267 tok->type = TK_CODE_POINT;
3268 tok->base = 16;
3269 tok->u.code = (OnigCodePoint )num;
3270 }
3271 break;
3272
3273 case 'o':
3274 if (PEND) break;
3275
3276 prev = p;
3277 if (PPEEK_IS('{') && IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_O_BRACE_OCTAL)) {
3278 PINC;
3279 num = scan_unsigned_octal_number(&p, end, 11, enc);
3280 if (num < 0) return ONIGERR_TOO_BIG_WIDE_CHAR_VALUE;
3281 if (!PEND) {
3282 c2 = PPEEK;
3283 if (ONIGENC_IS_CODE_DIGIT(enc, c2) && c2 < '8')
3284 return ONIGERR_TOO_LONG_WIDE_CHAR_VALUE;
3285 }
3286
3287 if (p > prev + enclen(enc, prev, end) && !PEND && (PPEEK_IS('}'))) {
3288 PINC;
3289 tok->type = TK_CODE_POINT;
3290 tok->base = 8;
3291 tok->u.code = (OnigCodePoint )num;
3292 }
3293 else {
3294 /* can't read nothing or invalid format */
3295 p = prev;
3296 }
3297 }
3298 break;
3299
3300 case '0':
3301 case '1': case '2': case '3': case '4': case '5': case '6': case '7':
3302 if (IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_OCTAL3)) {
3303 PUNFETCH;
3304 prev = p;
3305 num = scan_unsigned_octal_number(&p, end, 3, enc);
3306 if (num < 0 || 0xff < num) return ONIGERR_TOO_BIG_NUMBER;
3307 if (p == prev) { /* can't read nothing. */
3308 num = 0; /* but, it's not error */
3309 }
3310 tok->type = TK_RAW_BYTE;
3311 tok->base = 8;
3312 tok->u.c = num;
3313 }
3314 break;
3315
3316 default:
3317 PUNFETCH;
3318 num = fetch_escaped_value(&p, end, env, &c2);
3319 if (num < 0) return num;
3320 if ((OnigCodePoint )tok->u.c != c2) {
3321 tok->u.code = (OnigCodePoint )c2;
3322 tok->type = TK_CODE_POINT;
3323 }
3324 break;
3325 }
3326 }
3327 else if (c == '[') {
3328 if (IS_SYNTAX_OP(syn, ONIG_SYN_OP_POSIX_BRACKET) && (PPEEK_IS(':'))) {
3329 OnigCodePoint send[] = { (OnigCodePoint )':', (OnigCodePoint )']' };
3330 tok->backp = p; /* point at '[' is read */
3331 PINC;
3332 if (str_exist_check_with_esc(send, 2, p, end,
3333 (OnigCodePoint )']', enc, syn)) {
3334 tok->type = TK_POSIX_BRACKET_OPEN;
3335 }
3336 else {
3337 PUNFETCH;
3338 goto cc_in_cc;
3339 }
3340 }
3341 else {
3342 cc_in_cc:
3343 if (IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_CCLASS_SET_OP)) {
3344 tok->type = TK_CC_CC_OPEN;
3345 }
3346 else {
3347 CC_ESC_WARN(env, (UChar* )"[");
3348 }
3349 }
3350 }
3351 else if (c == '&') {
3352 if (IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_CCLASS_SET_OP) &&
3353 !PEND && (PPEEK_IS('&'))) {
3354 PINC;
3355 tok->type = TK_CC_AND;
3356 }
3357 }
3358
3359 end:
3360 *src = p;
3361 return tok->type;
3362}
3363
3364#ifdef USE_NAMED_GROUP
3365static int
3366fetch_named_backref_token(OnigCodePoint c, OnigToken* tok, UChar** src,
3367 UChar* end, ScanEnv* env)
3368{
3369 int r, num;
3370 const OnigSyntaxType* syn = env->syntax;
3371 UChar* prev;
3372 UChar* p = *src;
3373 UChar* name_end;
3374 int* backs;
3375 int back_num;
3376
3377 prev = p;
3378
3379# ifdef USE_BACKREF_WITH_LEVEL
3380 name_end = NULL_UCHARP; /* no need. escape gcc warning. */
3381 r = fetch_name_with_level(c, &p, end, &name_end,
3382 env, &back_num, &tok->u.backref.level);
3383 if (r == 1) tok->u.backref.exist_level = 1;
3384 else tok->u.backref.exist_level = 0;
3385# else
3386 r = fetch_name(&p, end, &name_end, env, &back_num, 1);
3387# endif
3388 if (r < 0) return r;
3389
3390 if (back_num != 0) {
3391 if (back_num < 0) {
3392 back_num = BACKREF_REL_TO_ABS(back_num, env);
3393 if (back_num <= 0)
3394 return ONIGERR_INVALID_BACKREF;
3395 }
3396
3397 if (IS_SYNTAX_BV(syn, ONIG_SYN_STRICT_CHECK_BACKREF)) {
3398 if (back_num > env->num_mem ||
3399 IS_NULL(SCANENV_MEM_NODES(env)[back_num]))
3400 return ONIGERR_INVALID_BACKREF;
3401 }
3402 tok->type = TK_BACKREF;
3403 tok->u.backref.by_name = 0;
3404 tok->u.backref.num = 1;
3405 tok->u.backref.ref1 = back_num;
3406 }
3407 else {
3408 num = onig_name_to_group_numbers(env->reg, prev, name_end, &backs);
3409 if (num <= 0) {
3410 onig_scan_env_set_error_string(env,
3411 ONIGERR_UNDEFINED_NAME_REFERENCE, prev, name_end);
3412 return ONIGERR_UNDEFINED_NAME_REFERENCE;
3413 }
3414 if (IS_SYNTAX_BV(syn, ONIG_SYN_STRICT_CHECK_BACKREF)) {
3415 int i;
3416 for (i = 0; i < num; i++) {
3417 if (backs[i] > env->num_mem ||
3418 IS_NULL(SCANENV_MEM_NODES(env)[backs[i]]))
3419 return ONIGERR_INVALID_BACKREF;
3420 }
3421 }
3422
3423 tok->type = TK_BACKREF;
3424 tok->u.backref.by_name = 1;
3425 if (num == 1 || IS_SYNTAX_BV(syn, ONIG_SYN_USE_LEFT_MOST_NAMED_GROUP)) {
3426 tok->u.backref.num = 1;
3427 tok->u.backref.ref1 = backs[0];
3428 }
3429 else {
3430 tok->u.backref.num = num;
3431 tok->u.backref.refs = backs;
3432 }
3433 }
3434 *src = p;
3435 return 0;
3436}
3437#endif
3438
3439static int
3440fetch_token(OnigToken* tok, UChar** src, UChar* end, ScanEnv* env)
3441{
3442 int r, num;
3443 OnigCodePoint c;
3444 OnigEncoding enc = env->enc;
3445 const OnigSyntaxType* syn = env->syntax;
3446 UChar* prev;
3447 UChar* p = *src;
3448 PFETCH_READY;
3449
3450 start:
3451 if (PEND) {
3452 tok->type = TK_EOT;
3453 return tok->type;
3454 }
3455
3456 tok->type = TK_STRING;
3457 tok->base = 0;
3458 tok->backp = p;
3459
3460 PFETCH(c);
3461 if (IS_MC_ESC_CODE(c, syn)) {
3462 if (PEND) return ONIGERR_END_PATTERN_AT_ESCAPE;
3463
3464 tok->backp = p;
3465 PFETCH(c);
3466
3467 tok->u.c = c;
3468 tok->escaped = 1;
3469 switch (c) {
3470 case '*':
3471 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_ASTERISK_ZERO_INF)) break;
3472 tok->type = TK_OP_REPEAT;
3473 tok->u.repeat.lower = 0;
3474 tok->u.repeat.upper = REPEAT_INFINITE;
3475 goto greedy_check;
3476 break;
3477
3478 case '+':
3479 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_PLUS_ONE_INF)) break;
3480 tok->type = TK_OP_REPEAT;
3481 tok->u.repeat.lower = 1;
3482 tok->u.repeat.upper = REPEAT_INFINITE;
3483 goto greedy_check;
3484 break;
3485
3486 case '?':
3487 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_QMARK_ZERO_ONE)) break;
3488 tok->type = TK_OP_REPEAT;
3489 tok->u.repeat.lower = 0;
3490 tok->u.repeat.upper = 1;
3491 greedy_check:
3492 if (!PEND && PPEEK_IS('?') &&
3493 IS_SYNTAX_OP(syn, ONIG_SYN_OP_QMARK_NON_GREEDY)) {
3494 PFETCH(c);
3495 tok->u.repeat.greedy = 0;
3496 tok->u.repeat.possessive = 0;
3497 }
3498 else {
3499 possessive_check:
3500 if (!PEND && PPEEK_IS('+') &&
3501 ((IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_PLUS_POSSESSIVE_REPEAT) &&
3502 tok->type != TK_INTERVAL) ||
3503 (IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_PLUS_POSSESSIVE_INTERVAL) &&
3504 tok->type == TK_INTERVAL))) {
3505 PFETCH(c);
3506 tok->u.repeat.greedy = 1;
3507 tok->u.repeat.possessive = 1;
3508 }
3509 else {
3510 tok->u.repeat.greedy = 1;
3511 tok->u.repeat.possessive = 0;
3512 }
3513 }
3514 break;
3515
3516 case '{':
3517 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_BRACE_INTERVAL)) break;
3518 r = fetch_range_quantifier(&p, end, tok, env);
3519 if (r < 0) return r; /* error */
3520 if (r == 0) goto greedy_check;
3521 else if (r == 2) { /* {n} */
3522 if (IS_SYNTAX_BV(syn, ONIG_SYN_FIXED_INTERVAL_IS_GREEDY_ONLY))
3523 goto possessive_check;
3524
3525 goto greedy_check;
3526 }
3527 /* r == 1 : normal char */
3528 break;
3529
3530 case '|':
3531 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_VBAR_ALT)) break;
3532 tok->type = TK_ALT;
3533 break;
3534
3535 case '(':
3536 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_LPAREN_SUBEXP)) break;
3537 tok->type = TK_SUBEXP_OPEN;
3538 break;
3539
3540 case ')':
3541 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_LPAREN_SUBEXP)) break;
3542 tok->type = TK_SUBEXP_CLOSE;
3543 break;
3544
3545 case 'w':
3546 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_W_WORD)) break;
3547 tok->type = TK_CHAR_TYPE;
3548 tok->u.prop.ctype = ONIGENC_CTYPE_WORD;
3549 tok->u.prop.not = 0;
3550 break;
3551
3552 case 'W':
3553 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_W_WORD)) break;
3554 tok->type = TK_CHAR_TYPE;
3555 tok->u.prop.ctype = ONIGENC_CTYPE_WORD;
3556 tok->u.prop.not = 1;
3557 break;
3558
3559 case 'b':
3560 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_B_WORD_BOUND)) break;
3561 tok->type = TK_ANCHOR;
3562 tok->u.anchor.subtype = ANCHOR_WORD_BOUND;
3563 tok->u.anchor.ascii_range = IS_ASCII_RANGE(env->option)
3564 && ! IS_WORD_BOUND_ALL_RANGE(env->option);
3565 break;
3566
3567 case 'B':
3568 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_B_WORD_BOUND)) break;
3569 tok->type = TK_ANCHOR;
3570 tok->u.anchor.subtype = ANCHOR_NOT_WORD_BOUND;
3571 tok->u.anchor.ascii_range = IS_ASCII_RANGE(env->option)
3572 && ! IS_WORD_BOUND_ALL_RANGE(env->option);
3573 break;
3574
3575#ifdef USE_WORD_BEGIN_END
3576 case '<':
3577 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_LTGT_WORD_BEGIN_END)) break;
3578 tok->type = TK_ANCHOR;
3579 tok->u.anchor.subtype = ANCHOR_WORD_BEGIN;
3580 tok->u.anchor.ascii_range = IS_ASCII_RANGE(env->option);
3581 break;
3582
3583 case '>':
3584 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_LTGT_WORD_BEGIN_END)) break;
3585 tok->type = TK_ANCHOR;
3586 tok->u.anchor.subtype = ANCHOR_WORD_END;
3587 tok->u.anchor.ascii_range = IS_ASCII_RANGE(env->option);
3588 break;
3589#endif
3590
3591 case 's':
3592 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_S_WHITE_SPACE)) break;
3593 tok->type = TK_CHAR_TYPE;
3594 tok->u.prop.ctype = ONIGENC_CTYPE_SPACE;
3595 tok->u.prop.not = 0;
3596 break;
3597
3598 case 'S':
3599 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_S_WHITE_SPACE)) break;
3600 tok->type = TK_CHAR_TYPE;
3601 tok->u.prop.ctype = ONIGENC_CTYPE_SPACE;
3602 tok->u.prop.not = 1;
3603 break;
3604
3605 case 'd':
3606 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_D_DIGIT)) break;
3607 tok->type = TK_CHAR_TYPE;
3608 tok->u.prop.ctype = ONIGENC_CTYPE_DIGIT;
3609 tok->u.prop.not = 0;
3610 break;
3611
3612 case 'D':
3613 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_D_DIGIT)) break;
3614 tok->type = TK_CHAR_TYPE;
3615 tok->u.prop.ctype = ONIGENC_CTYPE_DIGIT;
3616 tok->u.prop.not = 1;
3617 break;
3618
3619 case 'h':
3620 if (! IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_H_XDIGIT)) break;
3621 tok->type = TK_CHAR_TYPE;
3622 tok->u.prop.ctype = ONIGENC_CTYPE_XDIGIT;
3623 tok->u.prop.not = 0;
3624 break;
3625
3626 case 'H':
3627 if (! IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_H_XDIGIT)) break;
3628 tok->type = TK_CHAR_TYPE;
3629 tok->u.prop.ctype = ONIGENC_CTYPE_XDIGIT;
3630 tok->u.prop.not = 1;
3631 break;
3632
3633 case 'A':
3634 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_AZ_BUF_ANCHOR)) break;
3635 begin_buf:
3636 tok->type = TK_ANCHOR;
3637 tok->u.anchor.subtype = ANCHOR_BEGIN_BUF;
3638 break;
3639
3640 case 'Z':
3641 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_AZ_BUF_ANCHOR)) break;
3642 tok->type = TK_ANCHOR;
3643 tok->u.anchor.subtype = ANCHOR_SEMI_END_BUF;
3644 break;
3645
3646 case 'z':
3647 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_AZ_BUF_ANCHOR)) break;
3648 end_buf:
3649 tok->type = TK_ANCHOR;
3650 tok->u.anchor.subtype = ANCHOR_END_BUF;
3651 break;
3652
3653 case 'G':
3654 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_CAPITAL_G_BEGIN_ANCHOR)) break;
3655 tok->type = TK_ANCHOR;
3656 tok->u.anchor.subtype = ANCHOR_BEGIN_POSITION;
3657 break;
3658
3659 case '`':
3660 if (! IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_GNU_BUF_ANCHOR)) break;
3661 goto begin_buf;
3662 break;
3663
3664 case '\'':
3665 if (! IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_GNU_BUF_ANCHOR)) break;
3666 goto end_buf;
3667 break;
3668
3669 case 'x':
3670 if (PEND) break;
3671
3672 prev = p;
3673 if (PPEEK_IS('{') && IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_X_BRACE_HEX8)) {
3674 PINC;
3675 num = scan_unsigned_hexadecimal_number(&p, end, 0, 8, enc);
3676 if (num < 0) return ONIGERR_TOO_BIG_WIDE_CHAR_VALUE;
3677 if (!PEND) {
3678 if (ONIGENC_IS_CODE_XDIGIT(enc, PPEEK))
3679 return ONIGERR_TOO_LONG_WIDE_CHAR_VALUE;
3680 }
3681
3682 if ((p > prev + enclen(enc, prev, end)) && !PEND && PPEEK_IS('}')) {
3683 PINC;
3684 tok->type = TK_CODE_POINT;
3685 tok->u.code = (OnigCodePoint )num;
3686 }
3687 else {
3688 /* can't read nothing or invalid format */
3689 p = prev;
3690 }
3691 }
3692 else if (IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_X_HEX2)) {
3693 num = scan_unsigned_hexadecimal_number(&p, end, 0, 2, enc);
3694 if (num < 0) return ONIGERR_TOO_BIG_NUMBER;
3695 if (p == prev) { /* can't read nothing. */
3696 num = 0; /* but, it's not error */
3697 }
3698 tok->type = TK_RAW_BYTE;
3699 tok->base = 16;
3700 tok->u.c = num;
3701 }
3702 break;
3703
3704 case 'u':
3705 if (PEND) break;
3706
3707 prev = p;
3708 if (IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_U_HEX4)) {
3709 num = scan_unsigned_hexadecimal_number(&p, end, 4, 4, enc);
3710 if (num < -1) return ONIGERR_TOO_SHORT_DIGITS;
3711 else if (num < 0) return ONIGERR_TOO_BIG_NUMBER;
3712 if (p == prev) { /* can't read nothing. */
3713 num = 0; /* but, it's not error */
3714 }
3715 tok->type = TK_CODE_POINT;
3716 tok->base = 16;
3717 tok->u.code = (OnigCodePoint )num;
3718 }
3719 break;
3720
3721 case 'o':
3722 if (PEND) break;
3723
3724 prev = p;
3725 if (PPEEK_IS('{') && IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_O_BRACE_OCTAL)) {
3726 PINC;
3727 num = scan_unsigned_octal_number(&p, end, 11, enc);
3728 if (num < 0) return ONIGERR_TOO_BIG_WIDE_CHAR_VALUE;
3729 if (!PEND) {
3730 OnigCodePoint c = PPEEK;
3731 if (ONIGENC_IS_CODE_DIGIT(enc, c) && c < '8')
3732 return ONIGERR_TOO_LONG_WIDE_CHAR_VALUE;
3733 }
3734
3735 if ((p > prev + enclen(enc, prev, end)) && !PEND && PPEEK_IS('}')) {
3736 PINC;
3737 tok->type = TK_CODE_POINT;
3738 tok->u.code = (OnigCodePoint )num;
3739 }
3740 else {
3741 /* can't read nothing or invalid format */
3742 p = prev;
3743 }
3744 }
3745 break;
3746
3747 case '1': case '2': case '3': case '4':
3748 case '5': case '6': case '7': case '8': case '9':
3749 PUNFETCH;
3750 prev = p;
3751 num = onig_scan_unsigned_number(&p, end, enc);
3752 if (num < 0 || num > ONIG_MAX_BACKREF_NUM) {
3753 goto skip_backref;
3754 }
3755
3756 if (IS_SYNTAX_OP(syn, ONIG_SYN_OP_DECIMAL_BACKREF) &&
3757 (num <= env->num_mem || num <= 9)) { /* This spec. from GNU regex */
3758 if (IS_SYNTAX_BV(syn, ONIG_SYN_STRICT_CHECK_BACKREF)) {
3759 if (num > env->num_mem || IS_NULL(SCANENV_MEM_NODES(env)[num]))
3760 return ONIGERR_INVALID_BACKREF;
3761 }
3762
3763 tok->type = TK_BACKREF;
3764 tok->u.backref.num = 1;
3765 tok->u.backref.ref1 = num;
3766 tok->u.backref.by_name = 0;
3767#ifdef USE_BACKREF_WITH_LEVEL
3768 tok->u.backref.exist_level = 0;
3769#endif
3770 break;
3771 }
3772
3773 skip_backref:
3774 if (c == '8' || c == '9') {
3775 /* normal char */
3776 p = prev; PINC;
3777 break;
3778 }
3779
3780 p = prev;
3781 /* fall through */
3782 case '0':
3783 if (IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_OCTAL3)) {
3784 prev = p;
3785 num = scan_unsigned_octal_number(&p, end, (c == '0' ? 2:3), enc);
3786 if (num < 0 || 0xff < num) return ONIGERR_TOO_BIG_NUMBER;
3787 if (p == prev) { /* can't read nothing. */
3788 num = 0; /* but, it's not error */
3789 }
3790 tok->type = TK_RAW_BYTE;
3791 tok->base = 8;
3792 tok->u.c = num;
3793 }
3794 else if (c != '0') {
3795 PINC;
3796 }
3797 break;
3798
3799#ifdef USE_NAMED_GROUP
3800 case 'k':
3801 if (!PEND && IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_K_NAMED_BACKREF)) {
3802 PFETCH(c);
3803 if (c == '<' || c == '\'') {
3804 r = fetch_named_backref_token(c, tok, &p, end, env);
3805 if (r < 0) return r;
3806 }
3807 else {
3808 PUNFETCH;
3809 onig_syntax_warn(env, "invalid back reference");
3810 }
3811 }
3812 break;
3813#endif
3814
3815#if defined(USE_SUBEXP_CALL) || defined(USE_NAMED_GROUP)
3816 case 'g':
3817# ifdef USE_NAMED_GROUP
3818 if (!PEND && IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_G_BRACE_BACKREF)) {
3819 PFETCH(c);
3820 if (c == '{') {
3821 r = fetch_named_backref_token(c, tok, &p, end, env);
3822 if (r < 0) return r;
3823 }
3824 else
3825 PUNFETCH;
3826 }
3827# endif
3828# ifdef USE_SUBEXP_CALL
3829 if (!PEND && IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_G_SUBEXP_CALL)) {
3830 PFETCH(c);
3831 if (c == '<' || c == '\'') {
3832 int gnum = -1, rel = 0;
3833 UChar* name_end;
3834 OnigCodePoint cnext;
3835
3836 cnext = PPEEK;
3837 if (cnext == '0') {
3838 PINC;
3839 if (PPEEK_IS(get_name_end_code_point(c))) { /* \g<0>, \g'0' */
3840 PINC;
3841 name_end = p;
3842 gnum = 0;
3843 }
3844 }
3845 else if (cnext == '+') {
3846 PINC;
3847 rel = 1;
3848 }
3849 prev = p;
3850 if (gnum < 0) {
3851 r = fetch_name((OnigCodePoint )c, &p, end, &name_end, env, &gnum, 1);
3852 if (r < 0) return r;
3853 }
3854
3855 tok->type = TK_CALL;
3856 tok->u.call.name = prev;
3857 tok->u.call.name_end = name_end;
3858 tok->u.call.gnum = gnum;
3859 tok->u.call.rel = rel;
3860 }
3861 else {
3862 onig_syntax_warn(env, "invalid subexp call");
3863 PUNFETCH;
3864 }
3865 }
3866# endif
3867 break;
3868#endif
3869
3870 case 'Q':
3871 if (IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_CAPITAL_Q_QUOTE)) {
3872 tok->type = TK_QUOTE_OPEN;
3873 }
3874 break;
3875
3876 case 'p':
3877 case 'P':
3878 if (PPEEK_IS('{') &&
3879 IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_P_BRACE_CHAR_PROPERTY)) {
3880 PINC;
3881 tok->type = TK_CHAR_PROPERTY;
3882 tok->u.prop.not = (c == 'P' ? 1 : 0);
3883
3884 if (!PEND && IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_P_BRACE_CIRCUMFLEX_NOT)) {
3885 PFETCH(c);
3886 if (c == '^') {
3887 tok->u.prop.not = (tok->u.prop.not == 0 ? 1 : 0);
3888 }
3889 else
3890 PUNFETCH;
3891 }
3892 }
3893 else {
3894 onig_syntax_warn(env, "invalid Unicode Property \\%c", c);
3895 }
3896 break;
3897
3898 case 'R':
3899 if (IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_CAPITAL_R_LINEBREAK)) {
3900 tok->type = TK_LINEBREAK;
3901 }
3902 break;
3903
3904 case 'X':
3905 if (IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_CAPITAL_X_EXTENDED_GRAPHEME_CLUSTER)) {
3906 tok->type = TK_EXTENDED_GRAPHEME_CLUSTER;
3907 }
3908 break;
3909
3910 case 'K':
3911 if (IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_CAPITAL_K_KEEP)) {
3912 tok->type = TK_KEEP;
3913 }
3914 break;
3915
3916 default:
3917 {
3918 OnigCodePoint c2;
3919
3920 PUNFETCH;
3921 num = fetch_escaped_value(&p, end, env, &c2);
3922 if (num < 0) return num;
3923 /* set_raw: */
3924 if ((OnigCodePoint )tok->u.c != c2) {
3925 tok->type = TK_CODE_POINT;
3926 tok->u.code = (OnigCodePoint )c2;
3927 }
3928 else { /* string */
3929 p = tok->backp + enclen(enc, tok->backp, end);
3930 }
3931 }
3932 break;
3933 }
3934 }
3935 else {
3936 tok->u.c = c;
3937 tok->escaped = 0;
3938
3939#ifdef USE_VARIABLE_META_CHARS
3940 if ((c != ONIG_INEFFECTIVE_META_CHAR) &&
3941 IS_SYNTAX_OP(syn, ONIG_SYN_OP_VARIABLE_META_CHARACTERS)) {
3942 if (c == MC_ANYCHAR(syn))
3943 goto any_char;
3944 else if (c == MC_ANYTIME(syn))
3945 goto anytime;
3946 else if (c == MC_ZERO_OR_ONE_TIME(syn))
3947 goto zero_or_one_time;
3948 else if (c == MC_ONE_OR_MORE_TIME(syn))
3949 goto one_or_more_time;
3950 else if (c == MC_ANYCHAR_ANYTIME(syn)) {
3951 tok->type = TK_ANYCHAR_ANYTIME;
3952 goto out;
3953 }
3954 }
3955#endif
3956
3957 switch (c) {
3958 case '.':
3959 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_DOT_ANYCHAR)) break;
3960#ifdef USE_VARIABLE_META_CHARS
3961 any_char:
3962#endif
3963 tok->type = TK_ANYCHAR;
3964 break;
3965
3966 case '*':
3967 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ASTERISK_ZERO_INF)) break;
3968#ifdef USE_VARIABLE_META_CHARS
3969 anytime:
3970#endif
3971 tok->type = TK_OP_REPEAT;
3972 tok->u.repeat.lower = 0;
3973 tok->u.repeat.upper = REPEAT_INFINITE;
3974 goto greedy_check;
3975 break;
3976
3977 case '+':
3978 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_PLUS_ONE_INF)) break;
3979#ifdef USE_VARIABLE_META_CHARS
3980 one_or_more_time:
3981#endif
3982 tok->type = TK_OP_REPEAT;
3983 tok->u.repeat.lower = 1;
3984 tok->u.repeat.upper = REPEAT_INFINITE;
3985 goto greedy_check;
3986 break;
3987
3988 case '?':
3989 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_QMARK_ZERO_ONE)) break;
3990#ifdef USE_VARIABLE_META_CHARS
3991 zero_or_one_time:
3992#endif
3993 tok->type = TK_OP_REPEAT;
3994 tok->u.repeat.lower = 0;
3995 tok->u.repeat.upper = 1;
3996 goto greedy_check;
3997 break;
3998
3999 case '{':
4000 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_BRACE_INTERVAL)) break;
4001 r = fetch_range_quantifier(&p, end, tok, env);
4002 if (r < 0) return r; /* error */
4003 if (r == 0) goto greedy_check;
4004 else if (r == 2) { /* {n} */
4005 if (IS_SYNTAX_BV(syn, ONIG_SYN_FIXED_INTERVAL_IS_GREEDY_ONLY))
4006 goto possessive_check;
4007
4008 goto greedy_check;
4009 }
4010 /* r == 1 : normal char */
4011 break;
4012
4013 case '|':
4014 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_VBAR_ALT)) break;
4015 tok->type = TK_ALT;
4016 break;
4017
4018 case '(':
4019 if (PPEEK_IS('?') &&
4020 IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_QMARK_GROUP_EFFECT)) {
4021 PINC;
4022 if (PPEEK_IS('#')) {
4023 PFETCH(c);
4024 while (1) {
4025 if (PEND) return ONIGERR_END_PATTERN_IN_GROUP;
4026 PFETCH(c);
4027 if (c == MC_ESC(syn)) {
4028 if (!PEND) PFETCH(c);
4029 }
4030 else {
4031 if (c == ')') break;
4032 }
4033 }
4034 goto start;
4035 }
4036#ifdef USE_PERL_SUBEXP_CALL
4037 /* (?&name), (?n), (?R), (?0), (?+n), (?-n) */
4038 c = PPEEK;
4039 if ((c == '&' || c == 'R' || ONIGENC_IS_CODE_DIGIT(enc, c)) &&
4040 IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_QMARK_SUBEXP_CALL)) {
4041 /* (?&name), (?n), (?R), (?0) */
4042 int gnum;
4043 UChar *name;
4044 UChar *name_end;
4045
4046 if (c == 'R' || c == '0') {
4047 PINC; /* skip 'R' / '0' */
4048 if (!PPEEK_IS(')')) return ONIGERR_INVALID_GROUP_NAME;
4049 PINC; /* skip ')' */
4050 name_end = name = p;
4051 gnum = 0;
4052 }
4053 else {
4054 int numref = 1;
4055 if (c == '&') { /* (?&name) */
4056 PINC;
4057 numref = 0; /* don't allow number name */
4058 }
4059 name = p;
4060 r = fetch_name((OnigCodePoint )'(', &p, end, &name_end, env, &gnum, numref);
4061 if (r < 0) return r;
4062 }
4063
4064 tok->type = TK_CALL;
4065 tok->u.call.name = name;
4066 tok->u.call.name_end = name_end;
4067 tok->u.call.gnum = gnum;
4068 tok->u.call.rel = 0;
4069 break;
4070 }
4071 else if ((c == '-' || c == '+') &&
4072 IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_QMARK_SUBEXP_CALL)) {
4073 /* (?+n), (?-n) */
4074 int gnum;
4075 UChar *name;
4076 UChar *name_end;
4077 OnigCodePoint cnext;
4078 PFETCH_READY;
4079
4080 PINC; /* skip '-' / '+' */
4081 cnext = PPEEK;
4082 if (ONIGENC_IS_CODE_DIGIT(enc, cnext)) {
4083 if (c == '-') PUNFETCH;
4084 name = p;
4085 r = fetch_name((OnigCodePoint )'(', &p, end, &name_end, env, &gnum, 1);
4086 if (r < 0) return r;
4087
4088 tok->type = TK_CALL;
4089 tok->u.call.name = name;
4090 tok->u.call.name_end = name_end;
4091 tok->u.call.gnum = gnum;
4092 tok->u.call.rel = 1;
4093 break;
4094 }
4095 }
4096#endif /* USE_PERL_SUBEXP_CALL */
4097#ifdef USE_CAPITAL_P_NAMED_GROUP
4098 if (PPEEK_IS('P') &&
4099 IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_QMARK_CAPITAL_P_NAMED_GROUP)) {
4100 int gnum;
4101 UChar *name;
4102 UChar *name_end;
4103 PFETCH_READY;
4104
4105 PINC; /* skip 'P' */
4106 if (PEND) return ONIGERR_UNDEFINED_GROUP_OPTION;
4107 PFETCH(c);
4108 if (c == '=') { /* (?P=name): backref */
4109 r = fetch_named_backref_token((OnigCodePoint )'(', tok, &p, end, env);
4110 if (r < 0) return r;
4111 break;
4112 }
4113 else if (c == '>') { /* (?P>name): subexp call */
4114 name = p;
4115 r = fetch_name((OnigCodePoint )'(', &p, end, &name_end, env, &gnum, 0);
4116 if (r < 0) return r;
4117
4118 tok->type = TK_CALL;
4119 tok->u.call.name = name;
4120 tok->u.call.name_end = name_end;
4121 tok->u.call.gnum = gnum;
4122 tok->u.call.rel = 0;
4123 break;
4124 }
4125 }
4126#endif /* USE_CAPITAL_P_NAMED_GROUP */
4127 PUNFETCH;
4128 }
4129
4130 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_LPAREN_SUBEXP)) break;
4131 tok->type = TK_SUBEXP_OPEN;
4132 break;
4133
4134 case ')':
4135 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_LPAREN_SUBEXP)) break;
4136 tok->type = TK_SUBEXP_CLOSE;
4137 break;
4138
4139 case '^':
4140 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_LINE_ANCHOR)) break;
4141 tok->type = TK_ANCHOR;
4142 tok->u.anchor.subtype = (IS_SINGLELINE(env->option)
4143 ? ANCHOR_BEGIN_BUF : ANCHOR_BEGIN_LINE);
4144 break;
4145
4146 case '$':
4147 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_LINE_ANCHOR)) break;
4148 tok->type = TK_ANCHOR;
4149 tok->u.anchor.subtype = (IS_SINGLELINE(env->option)
4150 ? ANCHOR_SEMI_END_BUF : ANCHOR_END_LINE);
4151 break;
4152
4153 case '[':
4154 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_BRACKET_CC)) break;
4155 tok->type = TK_CC_OPEN;
4156 break;
4157
4158 case ']':
4159 if (*src > env->pattern) /* /].../ is allowed. */
4160 CLOSE_BRACKET_WITHOUT_ESC_WARN(env, (UChar* )"]");
4161 break;
4162
4163 case '#':
4164 if (IS_EXTEND(env->option)) {
4165 while (!PEND) {
4166 PFETCH(c);
4167 if (ONIGENC_IS_CODE_NEWLINE(enc, c))
4168 break;
4169 }
4170 goto start;
4171 break;
4172 }
4173 break;
4174
4175 case ' ': case '\t': case '\n': case '\r': case '\f':
4176 if (IS_EXTEND(env->option))
4177 goto start;
4178 break;
4179
4180 default:
4181 /* string */
4182 break;
4183 }
4184 }
4185
4186#ifdef USE_VARIABLE_META_CHARS
4187 out:
4188#endif
4189 *src = p;
4190 return tok->type;
4191}
4192
4193static int
4194add_ctype_to_cc_by_range(CClassNode* cc, int ctype ARG_UNUSED, int not,
4195 ScanEnv* env,
4196 OnigCodePoint sb_out, const OnigCodePoint mbr[])
4197{
4198 int i, r;
4199 OnigCodePoint j;
4200
4201 int n = ONIGENC_CODE_RANGE_NUM(mbr);
4202
4203 if (not == 0) {
4204 for (i = 0; i < n; i++) {
4205 for (j = ONIGENC_CODE_RANGE_FROM(mbr, i);
4206 j <= ONIGENC_CODE_RANGE_TO(mbr, i); j++) {
4207 if (j >= sb_out) {
4208 if (j > ONIGENC_CODE_RANGE_FROM(mbr, i)) {
4209 r = add_code_range_to_buf(&(cc->mbuf), env, j,
4210 ONIGENC_CODE_RANGE_TO(mbr, i));
4211 if (r != 0) return r;
4212 i++;
4213 }
4214
4215 goto sb_end;
4216 }
4217 BITSET_SET_BIT_CHKDUP(cc->bs, j);
4218 }
4219 }
4220
4221 sb_end:
4222 for ( ; i < n; i++) {
4223 r = add_code_range_to_buf(&(cc->mbuf), env,
4224 ONIGENC_CODE_RANGE_FROM(mbr, i),
4225 ONIGENC_CODE_RANGE_TO(mbr, i));
4226 if (r != 0) return r;
4227 }
4228 }
4229 else {
4230 OnigCodePoint prev = 0;
4231
4232 for (i = 0; i < n; i++) {
4233 for (j = prev;
4234 j < ONIGENC_CODE_RANGE_FROM(mbr, i); j++) {
4235 if (j >= sb_out) {
4236 goto sb_end2;
4237 }
4238 BITSET_SET_BIT_CHKDUP(cc->bs, j);
4239 }
4240 prev = ONIGENC_CODE_RANGE_TO(mbr, i) + 1;
4241 }
4242 for (j = prev; j < sb_out; j++) {
4243 BITSET_SET_BIT_CHKDUP(cc->bs, j);
4244 }
4245
4246 sb_end2:
4247 prev = sb_out;
4248
4249 for (i = 0; i < n; i++) {
4250 if (prev < ONIGENC_CODE_RANGE_FROM(mbr, i)) {
4251 r = add_code_range_to_buf(&(cc->mbuf), env, prev,
4252 ONIGENC_CODE_RANGE_FROM(mbr, i) - 1);
4253 if (r != 0) return r;
4254 }
4255 prev = ONIGENC_CODE_RANGE_TO(mbr, i) + 1;
4256 }
4257 if (prev < 0x7fffffff) {
4258 r = add_code_range_to_buf(&(cc->mbuf), env, prev, 0x7fffffff);
4259 if (r != 0) return r;
4260 }
4261 }
4262
4263 return 0;
4264}
4265
4266static int
4267add_ctype_to_cc(CClassNode* cc, int ctype, int not, int ascii_range, ScanEnv* env)
4268{
4269 int maxcode;
4270 int c, r;
4271 const OnigCodePoint *ranges;
4272 OnigCodePoint sb_out;
4273 OnigEncoding enc = env->enc;
4274
4275 r = ONIGENC_GET_CTYPE_CODE_RANGE(enc, ctype, &sb_out, &ranges);
4276 if (r == 0) {
4277 if (ascii_range) {
4278 CClassNode ccwork;
4279 initialize_cclass(&ccwork);
4280 r = add_ctype_to_cc_by_range(&ccwork, ctype, not, env, sb_out,
4281 ranges);
4282 if (r == 0) {
4283 if (not) {
4284 r = add_code_range_to_buf0(&(ccwork.mbuf), env, 0x80, ONIG_LAST_CODE_POINT, FALSE);
4285 }
4286 else {
4287 CClassNode ccascii;
4288 initialize_cclass(&ccascii);
4289 if (ONIGENC_MBC_MINLEN(env->enc) > 1) {
4290 r = add_code_range(&(ccascii.mbuf), env, 0x00, 0x7F);
4291 }
4292 else {
4293 bitset_set_range(env, ccascii.bs, 0x00, 0x7F);
4294 r = 0;
4295 }
4296 if (r == 0) {
4297 r = and_cclass(&ccwork, &ccascii, env);
4298 }
4299 if (IS_NOT_NULL(ccascii.mbuf)) bbuf_free(ccascii.mbuf);
4300 }
4301 if (r == 0) {
4302 r = or_cclass(cc, &ccwork, env);
4303 }
4304 if (IS_NOT_NULL(ccwork.mbuf)) bbuf_free(ccwork.mbuf);
4305 }
4306 }
4307 else {
4308 r = add_ctype_to_cc_by_range(cc, ctype, not, env, sb_out, ranges);
4309 }
4310 return r;
4311 }
4312 else if (r != ONIG_NO_SUPPORT_CONFIG) {
4313 return r;
4314 }
4315
4316 maxcode = ascii_range ? 0x80 : SINGLE_BYTE_SIZE;
4317 r = 0;
4318 switch (ctype) {
4319 case ONIGENC_CTYPE_ALPHA:
4320 case ONIGENC_CTYPE_BLANK:
4321 case ONIGENC_CTYPE_CNTRL:
4322 case ONIGENC_CTYPE_DIGIT:
4323 case ONIGENC_CTYPE_LOWER:
4324 case ONIGENC_CTYPE_PUNCT:
4325 case ONIGENC_CTYPE_SPACE:
4326 case ONIGENC_CTYPE_UPPER:
4327 case ONIGENC_CTYPE_XDIGIT:
4328 case ONIGENC_CTYPE_ASCII:
4329 case ONIGENC_CTYPE_ALNUM:
4330 if (not != 0) {
4331 for (c = 0; c < SINGLE_BYTE_SIZE; c++) {
4332 if (! ONIGENC_IS_CODE_CTYPE(enc, (OnigCodePoint )c, ctype))
4333 BITSET_SET_BIT_CHKDUP(cc->bs, c);
4334 }
4335 ADD_ALL_MULTI_BYTE_RANGE(enc, cc->mbuf);
4336 }
4337 else {
4338 for (c = 0; c < SINGLE_BYTE_SIZE; c++) {
4339 if (ONIGENC_IS_CODE_CTYPE(enc, (OnigCodePoint )c, ctype))
4340 BITSET_SET_BIT_CHKDUP(cc->bs, c);
4341 }
4342 }
4343 break;
4344
4345 case ONIGENC_CTYPE_GRAPH:
4346 case ONIGENC_CTYPE_PRINT:
4347 if (not != 0) {
4348 for (c = 0; c < SINGLE_BYTE_SIZE; c++) {
4349 if (! ONIGENC_IS_CODE_CTYPE(enc, (OnigCodePoint )c, ctype)
4350 || c >= maxcode)
4351 BITSET_SET_BIT_CHKDUP(cc->bs, c);
4352 }
4353 if (ascii_range)
4354 ADD_ALL_MULTI_BYTE_RANGE(enc, cc->mbuf);
4355 }
4356 else {
4357 for (c = 0; c < maxcode; c++) {
4358 if (ONIGENC_IS_CODE_CTYPE(enc, (OnigCodePoint )c, ctype))
4359 BITSET_SET_BIT_CHKDUP(cc->bs, c);
4360 }
4361 if (! ascii_range)
4362 ADD_ALL_MULTI_BYTE_RANGE(enc, cc->mbuf);
4363 }
4364 break;
4365
4366 case ONIGENC_CTYPE_WORD:
4367 if (not == 0) {
4368 for (c = 0; c < maxcode; c++) {
4369 if (ONIGENC_IS_CODE_WORD(enc, c)) BITSET_SET_BIT_CHKDUP(cc->bs, c);
4370 }
4371 if (! ascii_range)
4372 ADD_ALL_MULTI_BYTE_RANGE(enc, cc->mbuf);
4373 }
4374 else {
4375 for (c = 0; c < SINGLE_BYTE_SIZE; c++) {
4376 if ((ONIGENC_CODE_TO_MBCLEN(enc, c) > 0) /* check invalid code point */
4377 && (! ONIGENC_IS_CODE_WORD(enc, c) || c >= maxcode))
4378 BITSET_SET_BIT_CHKDUP(cc->bs, c);
4379 }
4380 if (ascii_range)
4381 ADD_ALL_MULTI_BYTE_RANGE(enc, cc->mbuf);
4382 }
4383 break;
4384
4385 default:
4386 return ONIGERR_PARSER_BUG;
4387 break;
4388 }
4389
4390 return r;
4391}
4392
4393static int
4394parse_posix_bracket(CClassNode* cc, CClassNode* asc_cc,
4395 UChar** src, UChar* end, ScanEnv* env)
4396{
4397#define POSIX_BRACKET_CHECK_LIMIT_LENGTH 20
4398#define POSIX_BRACKET_NAME_MIN_LEN 4
4399
4400 static const PosixBracketEntryType PBS[] = {
4401 POSIX_BRACKET_ENTRY_INIT("alnum", ONIGENC_CTYPE_ALNUM),
4402 POSIX_BRACKET_ENTRY_INIT("alpha", ONIGENC_CTYPE_ALPHA),
4403 POSIX_BRACKET_ENTRY_INIT("blank", ONIGENC_CTYPE_BLANK),
4404 POSIX_BRACKET_ENTRY_INIT("cntrl", ONIGENC_CTYPE_CNTRL),
4405 POSIX_BRACKET_ENTRY_INIT("digit", ONIGENC_CTYPE_DIGIT),
4406 POSIX_BRACKET_ENTRY_INIT("graph", ONIGENC_CTYPE_GRAPH),
4407 POSIX_BRACKET_ENTRY_INIT("lower", ONIGENC_CTYPE_LOWER),
4408 POSIX_BRACKET_ENTRY_INIT("print", ONIGENC_CTYPE_PRINT),
4409 POSIX_BRACKET_ENTRY_INIT("punct", ONIGENC_CTYPE_PUNCT),
4410 POSIX_BRACKET_ENTRY_INIT("space", ONIGENC_CTYPE_SPACE),
4411 POSIX_BRACKET_ENTRY_INIT("upper", ONIGENC_CTYPE_UPPER),
4412 POSIX_BRACKET_ENTRY_INIT("xdigit", ONIGENC_CTYPE_XDIGIT),
4413 POSIX_BRACKET_ENTRY_INIT("ascii", ONIGENC_CTYPE_ASCII),
4414 POSIX_BRACKET_ENTRY_INIT("word", ONIGENC_CTYPE_WORD),
4415 };
4416
4417 const PosixBracketEntryType *pb;
4418 int not, i, r;
4419 int ascii_range;
4420 OnigCodePoint c;
4421 OnigEncoding enc = env->enc;
4422 UChar *p = *src;
4423
4424 if (PPEEK_IS('^')) {
4425 PINC_S;
4426 not = 1;
4427 }
4428 else
4429 not = 0;
4430
4431 if (onigenc_strlen(enc, p, end) < POSIX_BRACKET_NAME_MIN_LEN + 3)
4432 goto not_posix_bracket;
4433
4434 ascii_range = IS_ASCII_RANGE(env->option) &&
4435 ! IS_POSIX_BRACKET_ALL_RANGE(env->option);
4436 for (pb = PBS; pb < PBS + numberof(PBS); pb++) {
4437 if (onigenc_with_ascii_strncmp(enc, p, end, pb->name, pb->len) == 0) {
4438 p = (UChar* )onigenc_step(enc, p, end, pb->len);
4439 if (onigenc_with_ascii_strncmp(enc, p, end, (UChar* )":]", 2) != 0)
4440 return ONIGERR_INVALID_POSIX_BRACKET_TYPE;
4441
4442 r = add_ctype_to_cc(cc, pb->ctype, not, ascii_range, env);
4443 if (r != 0) return r;
4444
4445 if (IS_NOT_NULL(asc_cc)) {
4446 if (pb->ctype != ONIGENC_CTYPE_WORD &&
4447 pb->ctype != ONIGENC_CTYPE_ASCII &&
4448 !ascii_range)
4449 r = add_ctype_to_cc(asc_cc, pb->ctype, not, ascii_range, env);
4450 if (r != 0) return r;
4451 }
4452
4453 PINC_S; PINC_S;
4454 *src = p;
4455 return 0;
4456 }
4457 }
4458
4459 not_posix_bracket:
4460 c = 0;
4461 i = 0;
4462 while (!PEND && ((c = PPEEK) != ':') && c != ']') {
4463 PINC_S;
4464 if (++i > POSIX_BRACKET_CHECK_LIMIT_LENGTH) break;
4465 }
4466 if (c == ':' && ! PEND) {
4467 PINC_S;
4468 if (! PEND) {
4469 PFETCH_S(c);
4470 if (c == ']')
4471 return ONIGERR_INVALID_POSIX_BRACKET_TYPE;
4472 }
4473 }
4474
4475 return 1; /* 1: is not POSIX bracket, but no error. */
4476}
4477
4478static int
4479fetch_char_property_to_ctype(UChar** src, UChar* end, ScanEnv* env)
4480{
4481 int r;
4482 OnigCodePoint c;
4483 OnigEncoding enc = env->enc;
4484 UChar *prev, *start, *p = *src;
4485
4486 r = ONIGERR_INVALID_CHAR_PROPERTY_NAME;
4487 start = prev = p;
4488
4489 while (!PEND) {
4490 prev = p;
4491 PFETCH_S(c);
4492 if (c == '}') {
4493 r = ONIGENC_PROPERTY_NAME_TO_CTYPE(enc, start, prev);
4494 if (r < 0) break;
4495
4496 *src = p;
4497 return r;
4498 }
4499 else if (c == '(' || c == ')' || c == '{' || c == '|') {
4500 break;
4501 }
4502 }
4503
4504 onig_scan_env_set_error_string(env, r, *src, prev);
4505 return r;
4506}
4507
4508static int cclass_case_fold(Node** np, CClassNode* cc, CClassNode* asc_cc, ScanEnv* env);
4509
4510static int
4511parse_char_property(Node** np, OnigToken* tok, UChar** src, UChar* end,
4512 ScanEnv* env)
4513{
4514 int r, ctype;
4515 CClassNode* cc;
4516
4517 ctype = fetch_char_property_to_ctype(src, end, env);
4518 if (ctype < 0) return ctype;
4519
4520 *np = node_new_cclass();
4521 CHECK_NULL_RETURN_MEMERR(*np);
4522 cc = NCCLASS(*np);
4523 r = add_ctype_to_cc(cc, ctype, 0, 0, env);
4524 if (r != 0) return r;
4525 if (tok->u.prop.not != 0) NCCLASS_SET_NOT(cc);
4526
4527 if (IS_IGNORECASE(env->option)) {
4528 if (ctype != ONIGENC_CTYPE_ASCII)
4529 r = cclass_case_fold(np, cc, cc, env);
4530 }
4531 return r;
4532}
4533
4534
4535enum CCSTATE {
4536 CCS_VALUE,
4537 CCS_RANGE,
4538 CCS_COMPLETE,
4539 CCS_START
4540};
4541
4542enum CCVALTYPE {
4543 CCV_SB,
4544 CCV_CODE_POINT,
4545 CCV_CLASS
4546};
4547
4548static int
4549next_state_class(CClassNode* cc, CClassNode* asc_cc,
4550 OnigCodePoint* vs, enum CCVALTYPE* type,
4551 enum CCSTATE* state, ScanEnv* env)
4552{
4553 int r;
4554
4555 if (*state == CCS_RANGE)
4556 return ONIGERR_CHAR_CLASS_VALUE_AT_END_OF_RANGE;
4557
4558 if (*state == CCS_VALUE && *type != CCV_CLASS) {
4559 if (*type == CCV_SB) {
4560 BITSET_SET_BIT_CHKDUP(cc->bs, (int )(*vs));
4561 if (IS_NOT_NULL(asc_cc))
4562 BITSET_SET_BIT(asc_cc->bs, (int )(*vs));
4563 }
4564 else if (*type == CCV_CODE_POINT) {
4565 r = add_code_range(&(cc->mbuf), env, *vs, *vs);
4566 if (r < 0) return r;
4567 if (IS_NOT_NULL(asc_cc)) {
4568 r = add_code_range0(&(asc_cc->mbuf), env, *vs, *vs, 0);
4569 if (r < 0) return r;
4570 }
4571 }
4572 }
4573
4574 *state = CCS_VALUE;
4575 *type = CCV_CLASS;
4576 return 0;
4577}
4578
4579static int
4580next_state_val(CClassNode* cc, CClassNode* asc_cc,
4581 OnigCodePoint *from, OnigCodePoint to,
4582 int* from_israw, int to_israw,
4583 enum CCVALTYPE intype, enum CCVALTYPE* type,
4584 enum CCSTATE* state, ScanEnv* env)
4585{
4586 int r;
4587
4588 switch (*state) {
4589 case CCS_VALUE:
4590 if (*type == CCV_SB) {
4591 BITSET_SET_BIT_CHKDUP(cc->bs, (int )(*from));
4592 if (IS_NOT_NULL(asc_cc))
4593 BITSET_SET_BIT(asc_cc->bs, (int )(*from));
4594 }
4595 else if (*type == CCV_CODE_POINT) {
4596 r = add_code_range(&(cc->mbuf), env, *from, *from);
4597 if (r < 0) return r;
4598 if (IS_NOT_NULL(asc_cc)) {
4599 r = add_code_range0(&(asc_cc->mbuf), env, *from, *from, 0);
4600 if (r < 0) return r;
4601 }
4602 }
4603 break;
4604
4605 case CCS_RANGE:
4606 if (intype == *type) {
4607 if (intype == CCV_SB) {
4608 if (*from > 0xff || to > 0xff)
4609 return ONIGERR_INVALID_CODE_POINT_VALUE;
4610
4611 if (*from > to) {
4612 if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_ALLOW_EMPTY_RANGE_IN_CC))
4613 goto ccs_range_end;
4614 else
4615 return ONIGERR_EMPTY_RANGE_IN_CHAR_CLASS;
4616 }
4617 bitset_set_range(env, cc->bs, (int )*from, (int )to);
4618 if (IS_NOT_NULL(asc_cc))
4619 bitset_set_range(env, asc_cc->bs, (int )*from, (int )to);
4620 }
4621 else {
4622 r = add_code_range(&(cc->mbuf), env, *from, to);
4623 if (r < 0) return r;
4624 if (IS_NOT_NULL(asc_cc)) {
4625 r = add_code_range0(&(asc_cc->mbuf), env, *from, to, 0);
4626 if (r < 0) return r;
4627 }
4628 }
4629 }
4630 else {
4631 if (*from > to) {
4632 if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_ALLOW_EMPTY_RANGE_IN_CC))
4633 goto ccs_range_end;
4634 else
4635 return ONIGERR_EMPTY_RANGE_IN_CHAR_CLASS;
4636 }
4637 bitset_set_range(env, cc->bs, (int )*from, (int )(to < 0xff ? to : 0xff));
4638 r = add_code_range(&(cc->mbuf), env, (OnigCodePoint )*from, to);
4639 if (r < 0) return r;
4640 if (IS_NOT_NULL(asc_cc)) {
4641 bitset_set_range(env, asc_cc->bs, (int )*from, (int )(to < 0xff ? to : 0xff));
4642 r = add_code_range0(&(asc_cc->mbuf), env, (OnigCodePoint )*from, to, 0);
4643 if (r < 0) return r;
4644 }
4645 }
4646 ccs_range_end:
4647 *state = CCS_COMPLETE;
4648 break;
4649
4650 case CCS_COMPLETE:
4651 case CCS_START:
4652 *state = CCS_VALUE;
4653 break;
4654
4655 default:
4656 break;
4657 }
4658
4659 *from_israw = to_israw;
4660 *from = to;
4661 *type = intype;
4662 return 0;
4663}
4664
4665static int
4666code_exist_check(OnigCodePoint c, UChar* from, UChar* end, int ignore_escaped,
4667 ScanEnv* env)
4668{
4669 int in_esc;
4670 OnigCodePoint code;
4671 OnigEncoding enc = env->enc;
4672 UChar* p = from;
4673
4674 in_esc = 0;
4675 while (! PEND) {
4676 if (ignore_escaped && in_esc) {
4677 in_esc = 0;
4678 }
4679 else {
4680 PFETCH_S(code);
4681 if (code == c) return 1;
4682 if (code == MC_ESC(env->syntax)) in_esc = 1;
4683 }
4684 }
4685 return 0;
4686}
4687
4688static int
4689parse_char_class(Node** np, Node** asc_np, OnigToken* tok, UChar** src, UChar* end,
4690 ScanEnv* env)
4691{
4692 int r, neg, len, fetched, and_start;
4693 OnigCodePoint v, vs;
4694 UChar *p;
4695 Node* node;
4696 Node* asc_node;
4697 CClassNode *cc, *prev_cc;
4698 CClassNode *asc_cc, *asc_prev_cc;
4699 CClassNode work_cc, asc_work_cc;
4700
4701 enum CCSTATE state;
4702 enum CCVALTYPE val_type, in_type;
4703 int val_israw, in_israw;
4704
4705 *np = *asc_np = NULL_NODE;
4706 env->parse_depth++;
4707 if (env->parse_depth > ParseDepthLimit)
4708 return ONIGERR_PARSE_DEPTH_LIMIT_OVER;
4709 prev_cc = asc_prev_cc = (CClassNode* )NULL;
4710 r = fetch_token_in_cc(tok, src, end, env);
4711 if (r == TK_CHAR && tok->u.c == '^' && tok->escaped == 0) {
4712 neg = 1;
4713 r = fetch_token_in_cc(tok, src, end, env);
4714 }
4715 else {
4716 neg = 0;
4717 }
4718
4719 if (r < 0) return r;
4720 if (r == TK_CC_CLOSE) {
4721 if (! code_exist_check((OnigCodePoint )']',
4722 *src, env->pattern_end, 1, env))
4723 return ONIGERR_EMPTY_CHAR_CLASS;
4724
4725 CC_ESC_WARN(env, (UChar* )"]");
4726 r = tok->type = TK_CHAR; /* allow []...] */
4727 }
4728
4729 *np = node = node_new_cclass();
4730 CHECK_NULL_RETURN_MEMERR(node);
4731 cc = NCCLASS(node);
4732
4733 if (IS_IGNORECASE(env->option)) {
4734 *asc_np = asc_node = node_new_cclass();
4735 CHECK_NULL_RETURN_MEMERR(asc_node);
4736 asc_cc = NCCLASS(asc_node);
4737 }
4738 else {
4739 asc_node = NULL_NODE;
4740 asc_cc = NULL;
4741 }
4742
4743 and_start = 0;
4744 state = CCS_START;
4745 p = *src;
4746 while (r != TK_CC_CLOSE) {
4747 fetched = 0;
4748 switch (r) {
4749 case TK_CHAR:
4750 if ((tok->u.code >= SINGLE_BYTE_SIZE) ||
4751 (len = ONIGENC_CODE_TO_MBCLEN(env->enc, tok->u.c)) > 1) {
4752 in_type = CCV_CODE_POINT;
4753 }
4754 else if (len < 0) {
4755 r = len;
4756 goto err;
4757 }
4758 else {
4759 sb_char:
4760 in_type = CCV_SB;
4761 }
4762 v = (OnigCodePoint )tok->u.c;
4763 in_israw = 0;
4764 goto val_entry2;
4765 break;
4766
4767 case TK_RAW_BYTE:
4768 /* tok->base != 0 : octal or hexadec. */
4769 if (! ONIGENC_IS_SINGLEBYTE(env->enc) && tok->base != 0) {
4770 UChar buf[ONIGENC_CODE_TO_MBC_MAXLEN];
4771 UChar* bufe = buf + ONIGENC_CODE_TO_MBC_MAXLEN;
4772 UChar* psave = p;
4773 int i, base = tok->base;
4774
4775 buf[0] = (UChar )tok->u.c;
4776 for (i = 1; i < ONIGENC_MBC_MAXLEN(env->enc); i++) {
4777 r = fetch_token_in_cc(tok, &p, end, env);
4778 if (r < 0) goto err;
4779 if (r != TK_RAW_BYTE || tok->base != base) {
4780 fetched = 1;
4781 break;
4782 }
4783 buf[i] = (UChar )tok->u.c;
4784 }
4785
4786 if (i < ONIGENC_MBC_MINLEN(env->enc)) {
4787 r = ONIGERR_TOO_SHORT_MULTI_BYTE_STRING;
4788 goto err;
4789 }
4790
4791 len = enclen(env->enc, buf, buf + i);
4792 if (i < len) {
4793 r = ONIGERR_TOO_SHORT_MULTI_BYTE_STRING;
4794 goto err;
4795 }
4796 else if (i > len) { /* fetch back */
4797 p = psave;
4798 for (i = 1; i < len; i++) {
4799 (void)fetch_token_in_cc(tok, &p, end, env);
4800 /* no need to check the return value (already checked above) */
4801 }
4802 fetched = 0;
4803 }
4804
4805 if (i == 1) {
4806 v = (OnigCodePoint )buf[0];
4807 goto raw_single;
4808 }
4809 else {
4810 v = ONIGENC_MBC_TO_CODE(env->enc, buf, bufe);
4811 in_type = CCV_CODE_POINT;
4812 }
4813 }
4814 else {
4815 v = (OnigCodePoint )tok->u.c;
4816 raw_single:
4817 in_type = CCV_SB;
4818 }
4819 in_israw = 1;
4820 goto val_entry2;
4821 break;
4822
4823 case TK_CODE_POINT:
4824 v = tok->u.code;
4825 in_israw = 1;
4826 val_entry:
4827 len = ONIGENC_CODE_TO_MBCLEN(env->enc, v);
4828 if (len < 0) {
4829 r = len;
4830 goto err;
4831 }
4832 in_type = (len == 1 ? CCV_SB : CCV_CODE_POINT);
4833 val_entry2:
4834 r = next_state_val(cc, asc_cc, &vs, v, &val_israw, in_israw, in_type, &val_type,
4835 &state, env);
4836 if (r != 0) goto err;
4837 break;
4838
4839 case TK_POSIX_BRACKET_OPEN:
4840 r = parse_posix_bracket(cc, asc_cc, &p, end, env);
4841 if (r < 0) goto err;
4842 if (r == 1) { /* is not POSIX bracket */
4843 CC_ESC_WARN(env, (UChar* )"[");
4844 p = tok->backp;
4845 v = (OnigCodePoint )tok->u.c;
4846 in_israw = 0;
4847 goto val_entry;
4848 }
4849 goto next_class;
4850 break;
4851
4852 case TK_CHAR_TYPE:
4853 r = add_ctype_to_cc(cc, tok->u.prop.ctype, tok->u.prop.not,
4854 IS_ASCII_RANGE(env->option), env);
4855 if (r != 0) return r;
4856 if (IS_NOT_NULL(asc_cc)) {
4857 if (tok->u.prop.ctype != ONIGENC_CTYPE_WORD)
4858 r = add_ctype_to_cc(asc_cc, tok->u.prop.ctype, tok->u.prop.not,
4859 IS_ASCII_RANGE(env->option), env);
4860 if (r != 0) return r;
4861 }
4862
4863 next_class:
4864 r = next_state_class(cc, asc_cc, &vs, &val_type, &state, env);
4865 if (r != 0) goto err;
4866 break;
4867
4868 case TK_CHAR_PROPERTY:
4869 {
4870 int ctype;
4871
4872 ctype = fetch_char_property_to_ctype(&p, end, env);
4873 if (ctype < 0) return ctype;
4874 r = add_ctype_to_cc(cc, ctype, tok->u.prop.not, 0, env);
4875 if (r != 0) return r;
4876 if (IS_NOT_NULL(asc_cc)) {
4877 if (ctype != ONIGENC_CTYPE_ASCII)
4878 r = add_ctype_to_cc(asc_cc, ctype, tok->u.prop.not, 0, env);
4879 if (r != 0) return r;
4880 }
4881 goto next_class;
4882 }
4883 break;
4884
4885 case TK_CC_RANGE:
4886 if (state == CCS_VALUE) {
4887 r = fetch_token_in_cc(tok, &p, end, env);
4888 if (r < 0) goto err;
4889 fetched = 1;
4890 if (r == TK_CC_CLOSE) { /* allow [x-] */
4891 range_end_val:
4892 v = (OnigCodePoint )'-';
4893 in_israw = 0;
4894 goto val_entry;
4895 }
4896 else if (r == TK_CC_AND) {
4897 CC_ESC_WARN(env, (UChar* )"-");
4898 goto range_end_val;
4899 }
4900
4901 if (val_type == CCV_CLASS) {
4902 r = ONIGERR_UNMATCHED_RANGE_SPECIFIER_IN_CHAR_CLASS;
4903 goto err;
4904 }
4905
4906 state = CCS_RANGE;
4907 }
4908 else if (state == CCS_START) {
4909 /* [-xa] is allowed */
4910 v = (OnigCodePoint )tok->u.c;
4911 in_israw = 0;
4912
4913 r = fetch_token_in_cc(tok, &p, end, env);
4914 if (r < 0) goto err;
4915 fetched = 1;
4916 /* [--x] or [a&&-x] is warned. */
4917 if (r == TK_CC_RANGE || and_start != 0)
4918 CC_ESC_WARN(env, (UChar* )"-");
4919
4920 goto val_entry;
4921 }
4922 else if (state == CCS_RANGE) {
4923 CC_ESC_WARN(env, (UChar* )"-");
4924 goto sb_char; /* [!--x] is allowed */
4925 }
4926 else { /* CCS_COMPLETE */
4927 r = fetch_token_in_cc(tok, &p, end, env);
4928 if (r < 0) goto err;
4929 fetched = 1;
4930 if (r == TK_CC_CLOSE) goto range_end_val; /* allow [a-b-] */
4931 else if (r == TK_CC_AND) {
4932 CC_ESC_WARN(env, (UChar* )"-");
4933 goto range_end_val;
4934 }
4935
4936 if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_ALLOW_DOUBLE_RANGE_OP_IN_CC)) {
4937 CC_ESC_WARN(env, (UChar* )"-");
4938 goto range_end_val; /* [0-9-a] is allowed as [0-9\-a] */
4939 }
4940 r = ONIGERR_UNMATCHED_RANGE_SPECIFIER_IN_CHAR_CLASS;
4941 goto err;
4942 }
4943 break;
4944
4945 case TK_CC_CC_OPEN: /* [ */
4946 {
4947 Node *anode, *aasc_node;
4948 CClassNode* acc;
4949
4950 r = parse_char_class(&anode, &aasc_node, tok, &p, end, env);
4951 if (r == 0) {
4952 acc = NCCLASS(anode);
4953 r = or_cclass(cc, acc, env);
4954 }
4955 if (r == 0 && IS_NOT_NULL(aasc_node)) {
4956 acc = NCCLASS(aasc_node);
4957 r = or_cclass(asc_cc, acc, env);
4958 }
4959 onig_node_free(anode);
4960 onig_node_free(aasc_node);
4961 if (r != 0) goto err;
4962 }
4963 break;
4964
4965 case TK_CC_AND: /* && */
4966 {
4967 if (state == CCS_VALUE) {
4968 r = next_state_val(cc, asc_cc, &vs, 0, &val_israw, 0, val_type,
4969 &val_type, &state, env);
4970 if (r != 0) goto err;
4971 }
4972 /* initialize local variables */
4973 and_start = 1;
4974 state = CCS_START;
4975
4976 if (IS_NOT_NULL(prev_cc)) {
4977 r = and_cclass(prev_cc, cc, env);
4978 if (r != 0) goto err;
4979 bbuf_free(cc->mbuf);
4980 if (IS_NOT_NULL(asc_cc)) {
4981 r = and_cclass(asc_prev_cc, asc_cc, env);
4982 if (r != 0) goto err;
4983 bbuf_free(asc_cc->mbuf);
4984 }
4985 }
4986 else {
4987 prev_cc = cc;
4988 cc = &work_cc;
4989 if (IS_NOT_NULL(asc_cc)) {
4990 asc_prev_cc = asc_cc;
4991 asc_cc = &asc_work_cc;
4992 }
4993 }
4994 initialize_cclass(cc);
4995 if (IS_NOT_NULL(asc_cc))
4996 initialize_cclass(asc_cc);
4997 }
4998 break;
4999
5000 case TK_EOT:
5001 r = ONIGERR_PREMATURE_END_OF_CHAR_CLASS;
5002 goto err;
5003 break;
5004 default:
5005 r = ONIGERR_PARSER_BUG;
5006 goto err;
5007 break;
5008 }
5009
5010 if (fetched)
5011 r = tok->type;
5012 else {
5013 r = fetch_token_in_cc(tok, &p, end, env);
5014 if (r < 0) goto err;
5015 }
5016 }
5017
5018 if (state == CCS_VALUE) {
5019 r = next_state_val(cc, asc_cc, &vs, 0, &val_israw, 0, val_type,
5020 &val_type, &state, env);
5021 if (r != 0) goto err;
5022 }
5023
5024 if (IS_NOT_NULL(prev_cc)) {
5025 r = and_cclass(prev_cc, cc, env);
5026 if (r != 0) goto err;
5027 bbuf_free(cc->mbuf);
5028 cc = prev_cc;
5029 if (IS_NOT_NULL(asc_cc)) {
5030 r = and_cclass(asc_prev_cc, asc_cc, env);
5031 if (r != 0) goto err;
5032 bbuf_free(asc_cc->mbuf);
5033 asc_cc = asc_prev_cc;
5034 }
5035 }
5036
5037 if (neg != 0) {
5038 NCCLASS_SET_NOT(cc);
5039 if (IS_NOT_NULL(asc_cc))
5040 NCCLASS_SET_NOT(asc_cc);
5041 }
5042 else {
5043 NCCLASS_CLEAR_NOT(cc);
5044 if (IS_NOT_NULL(asc_cc))
5045 NCCLASS_CLEAR_NOT(asc_cc);
5046 }
5047 if (IS_NCCLASS_NOT(cc) &&
5048 IS_SYNTAX_BV(env->syntax, ONIG_SYN_NOT_NEWLINE_IN_NEGATIVE_CC)) {
5049 int is_empty;
5050
5051 is_empty = (IS_NULL(cc->mbuf) ? 1 : 0);
5052 if (is_empty != 0)
5053 BITSET_IS_EMPTY(cc->bs, is_empty);
5054
5055 if (is_empty == 0) {
5056#define NEWLINE_CODE 0x0a
5057
5058 if (ONIGENC_IS_CODE_NEWLINE(env->enc, NEWLINE_CODE)) {
5059 if (ONIGENC_CODE_TO_MBCLEN(env->enc, NEWLINE_CODE) == 1)
5060 BITSET_SET_BIT_CHKDUP(cc->bs, NEWLINE_CODE);
5061 else {
5062 r = add_code_range(&(cc->mbuf), env, NEWLINE_CODE, NEWLINE_CODE);
5063 if (r < 0) goto err;
5064 }
5065 }
5066 }
5067 }
5068 *src = p;
5069 env->parse_depth--;
5070 return 0;
5071
5072 err:
5073 if (cc != NCCLASS(*np))
5074 bbuf_free(cc->mbuf);
5075 if (IS_NOT_NULL(asc_cc) && (asc_cc != NCCLASS(*asc_np)))
5076 bbuf_free(asc_cc->mbuf);
5077 return r;
5078}
5079
5080static int parse_subexp(Node** top, OnigToken* tok, int term,
5081 UChar** src, UChar* end, ScanEnv* env);
5082
5083static int
5084parse_enclose(Node** np, OnigToken* tok, int term, UChar** src, UChar* end,
5085 ScanEnv* env)
5086{
5087 int r = 0, num;
5088 Node *target, *work1 = NULL, *work2 = NULL;
5089 OnigOptionType option;
5090 OnigCodePoint c;
5091 OnigEncoding enc = env->enc;
5092
5093#ifdef USE_NAMED_GROUP
5094 int list_capture;
5095#endif
5096
5097 UChar* p = *src;
5098 PFETCH_READY;
5099
5100 *np = NULL;
5101 if (PEND) return ONIGERR_END_PATTERN_WITH_UNMATCHED_PARENTHESIS;
5102
5103 option = env->option;
5104 if (PPEEK_IS('?') &&
5105 IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_QMARK_GROUP_EFFECT)) {
5106 PINC;
5107 if (PEND) return ONIGERR_END_PATTERN_IN_GROUP;
5108
5109 PFETCH(c);
5110 switch (c) {
5111 case ':': /* (?:...) grouping only */
5112 group:
5113 r = fetch_token(tok, &p, end, env);
5114 if (r < 0) return r;
5115 r = parse_subexp(np, tok, term, &p, end, env);
5116 if (r < 0) return r;
5117 *src = p;
5118 return 1; /* group */
5119 break;
5120
5121 case '=':
5122 *np = onig_node_new_anchor(ANCHOR_PREC_READ);
5123 break;
5124 case '!': /* preceding read */
5125 *np = onig_node_new_anchor(ANCHOR_PREC_READ_NOT);
5126 break;
5127 case '>': /* (?>...) stop backtrack */
5128 *np = node_new_enclose(ENCLOSE_STOP_BACKTRACK);
5129 break;
5130 case '~': /* (?~...) absent operator */
5131 if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_QMARK_TILDE_ABSENT)) {
5132 *np = node_new_enclose(ENCLOSE_ABSENT);
5133 }
5134 else {
5135 return ONIGERR_UNDEFINED_GROUP_OPTION;
5136 }
5137 break;
5138
5139#ifdef USE_NAMED_GROUP
5140 case '\'':
5141 if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_QMARK_LT_NAMED_GROUP)) {
5142 goto named_group1;
5143 }
5144 else
5145 return ONIGERR_UNDEFINED_GROUP_OPTION;
5146 break;
5147
5148# ifdef USE_CAPITAL_P_NAMED_GROUP
5149 case 'P': /* (?P<name>...) */
5150 if (!PEND &&
5151 IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_QMARK_CAPITAL_P_NAMED_GROUP)) {
5152 PFETCH(c);
5153 if (c == '<') goto named_group1;
5154 }
5155 return ONIGERR_UNDEFINED_GROUP_OPTION;
5156 break;
5157# endif
5158#endif
5159
5160 case '<': /* look behind (?<=...), (?<!...) */
5161 if (PEND) return ONIGERR_END_PATTERN_WITH_UNMATCHED_PARENTHESIS;
5162 PFETCH(c);
5163 if (c == '=')
5164 *np = onig_node_new_anchor(ANCHOR_LOOK_BEHIND);
5165 else if (c == '!')
5166 *np = onig_node_new_anchor(ANCHOR_LOOK_BEHIND_NOT);
5167#ifdef USE_NAMED_GROUP
5168 else { /* (?<name>...) */
5169 if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_QMARK_LT_NAMED_GROUP)) {
5170 UChar *name;
5171 UChar *name_end;
5172
5173 PUNFETCH;
5174 c = '<';
5175
5176 named_group1:
5177 list_capture = 0;
5178
5179# ifdef USE_CAPTURE_HISTORY
5180 named_group2:
5181# endif
5182 name = p;
5183 r = fetch_name((OnigCodePoint )c, &p, end, &name_end, env, &num, 0);
5184 if (r < 0) return r;
5185
5186 num = scan_env_add_mem_entry(env);
5187 if (num < 0) return num;
5188 if (list_capture != 0 && num >= (int )BIT_STATUS_BITS_NUM)
5189 return ONIGERR_GROUP_NUMBER_OVER_FOR_CAPTURE_HISTORY;
5190
5191 r = name_add(env->reg, name, name_end, num, env);
5192 if (r != 0) return r;
5193 *np = node_new_enclose_memory(env->option, 1);
5194 CHECK_NULL_RETURN_MEMERR(*np);
5195 NENCLOSE(*np)->regnum = num;
5196 if (list_capture != 0)
5197 BIT_STATUS_ON_AT_SIMPLE(env->capture_history, num);
5198 env->num_named++;
5199 }
5200 else {
5201 return ONIGERR_UNDEFINED_GROUP_OPTION;
5202 }
5203 }
5204#else
5205 else {
5206 return ONIGERR_UNDEFINED_GROUP_OPTION;
5207 }
5208#endif
5209 break;
5210
5211#ifdef USE_CAPTURE_HISTORY
5212 case '@':
5213 if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_ATMARK_CAPTURE_HISTORY)) {
5214# ifdef USE_NAMED_GROUP
5215 if (!PEND &&
5216 IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_QMARK_LT_NAMED_GROUP)) {
5217 PFETCH(c);
5218 if (c == '<' || c == '\'') {
5219 list_capture = 1;
5220 goto named_group2; /* (?@<name>...) */
5221 }
5222 PUNFETCH;
5223 }
5224# endif
5225 *np = node_new_enclose_memory(env->option, 0);
5226 CHECK_NULL_RETURN_MEMERR(*np);
5227 num = scan_env_add_mem_entry(env);
5228 if (num < 0) return num;
5229 if (num >= (int )BIT_STATUS_BITS_NUM)
5230 return ONIGERR_GROUP_NUMBER_OVER_FOR_CAPTURE_HISTORY;
5231
5232 NENCLOSE(*np)->regnum = num;
5233 BIT_STATUS_ON_AT_SIMPLE(env->capture_history, num);
5234 }
5235 else {
5236 return ONIGERR_UNDEFINED_GROUP_OPTION;
5237 }
5238 break;
5239#endif /* USE_CAPTURE_HISTORY */
5240
5241 case '(': /* conditional expression: (?(cond)yes), (?(cond)yes|no) */
5242 if (!PEND &&
5243 IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_QMARK_LPAREN_CONDITION)) {
5244 UChar *name = NULL;
5245 UChar *name_end;
5246 PFETCH(c);
5247 if (ONIGENC_IS_CODE_DIGIT(enc, c)) { /* (n) */
5248 PUNFETCH;
5249 r = fetch_name((OnigCodePoint )'(', &p, end, &name_end, env, &num, 1);
5250 if (r < 0) return r;
5251#if 0
5252 /* Relative number is not currently supported. (same as Perl) */
5253 if (num < 0) {
5254 num = BACKREF_REL_TO_ABS(num, env);
5255 if (num <= 0)
5256 return ONIGERR_INVALID_BACKREF;
5257 }
5258#endif
5259 if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_STRICT_CHECK_BACKREF)) {
5260 if (num > env->num_mem ||
5261 IS_NULL(SCANENV_MEM_NODES(env)[num]))
5262 return ONIGERR_INVALID_BACKREF;
5263 }
5264 }
5265#ifdef USE_NAMED_GROUP
5266 else if (c == '<' || c == '\'') { /* (<name>), ('name') */
5267 name = p;
5268 r = fetch_named_backref_token(c, tok, &p, end, env);
5269 if (r < 0) return r;
5270 if (!PPEEK_IS(')')) return ONIGERR_UNDEFINED_GROUP_OPTION;
5271 PINC;
5272
5273 if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_USE_LEFT_MOST_NAMED_GROUP)) {
5274 num = tok->u.backref.ref1;
5275 }
5276 else {
5277 /* FIXME:
5278 * Use left most named group for now. This is the same as Perl.
5279 * However this should use the same strategy as normal back-
5280 * references on Ruby syntax; search right to left. */
5281 int len = tok->u.backref.num;
5282 num = len > 1 ? tok->u.backref.refs[0] : tok->u.backref.ref1;
5283 }
5284 }
5285#endif
5286 else
5287 return ONIGERR_INVALID_CONDITION_PATTERN;
5288 *np = node_new_enclose(ENCLOSE_CONDITION);
5289 CHECK_NULL_RETURN_MEMERR(*np);
5290 NENCLOSE(*np)->regnum = num;
5291 if (IS_NOT_NULL(name)) NENCLOSE(*np)->state |= NST_NAME_REF;
5292 }
5293 else
5294 return ONIGERR_UNDEFINED_GROUP_OPTION;
5295 break;
5296
5297#if 0
5298 case '|': /* branch reset: (?|...) */
5299 if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_QMARK_VBAR_BRANCH_RESET)) {
5300 /* TODO */
5301 }
5302 else
5303 return ONIGERR_UNDEFINED_GROUP_OPTION;
5304 break;
5305#endif
5306
5307 case '^': /* loads default options */
5308 if (!PEND && IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_OPTION_PERL)) {
5309 /* d-imsx */
5310 ONOFF(option, ONIG_OPTION_ASCII_RANGE, 1);
5311 ONOFF(option, ONIG_OPTION_IGNORECASE, 1);
5312 ONOFF(option, ONIG_OPTION_SINGLELINE, 0);
5313 ONOFF(option, ONIG_OPTION_MULTILINE, 1);
5314 ONOFF(option, ONIG_OPTION_EXTEND, 1);
5315 PFETCH(c);
5316 }
5317#if 0
5318 else if (!PEND && IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_OPTION_RUBY)) {
5319 /* d-imx */
5320 ONOFF(option, ONIG_OPTION_ASCII_RANGE, 0);
5321 ONOFF(option, ONIG_OPTION_POSIX_BRACKET_ALL_RANGE, 0);
5322 ONOFF(option, ONIG_OPTION_WORD_BOUND_ALL_RANGE, 0);
5323 ONOFF(option, ONIG_OPTION_IGNORECASE, 1);
5324 ONOFF(option, ONIG_OPTION_MULTILINE, 1);
5325 ONOFF(option, ONIG_OPTION_EXTEND, 1);
5326 PFETCH(c);
5327 }
5328#endif
5329 else {
5330 return ONIGERR_UNDEFINED_GROUP_OPTION;
5331 }
5332 /* fall through */
5333#ifdef USE_POSIXLINE_OPTION
5334 case 'p':
5335#endif
5336 case '-': case 'i': case 'm': case 's': case 'x':
5337 case 'a': case 'd': case 'l': case 'u':
5338 {
5339 int neg = 0;
5340
5341 while (1) {
5342 switch (c) {
5343 case ':':
5344 case ')':
5345 break;
5346
5347 case '-': neg = 1; break;
5348 case 'x': ONOFF(option, ONIG_OPTION_EXTEND, neg); break;
5349 case 'i': ONOFF(option, ONIG_OPTION_IGNORECASE, neg); break;
5350 case 's':
5351 if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_OPTION_PERL)) {
5352 ONOFF(option, ONIG_OPTION_MULTILINE, neg);
5353 }
5354 else
5355 return ONIGERR_UNDEFINED_GROUP_OPTION;
5356 break;
5357
5358 case 'm':
5359 if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_OPTION_PERL)) {
5360 ONOFF(option, ONIG_OPTION_SINGLELINE, (neg == 0 ? 1 : 0));
5361 }
5362 else if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_OPTION_RUBY)) {
5363 ONOFF(option, ONIG_OPTION_MULTILINE, neg);
5364 }
5365 else
5366 return ONIGERR_UNDEFINED_GROUP_OPTION;
5367 break;
5368#ifdef USE_POSIXLINE_OPTION
5369 case 'p':
5370 ONOFF(option, ONIG_OPTION_MULTILINE|ONIG_OPTION_SINGLELINE, neg);
5371 break;
5372#endif
5373
5374 case 'a': /* limits \d, \s, \w and POSIX brackets to ASCII range */
5375 if ((IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_OPTION_PERL) ||
5376 IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_OPTION_RUBY)) &&
5377 (neg == 0)) {
5378 ONOFF(option, ONIG_OPTION_ASCII_RANGE, 0);
5379 ONOFF(option, ONIG_OPTION_POSIX_BRACKET_ALL_RANGE, 1);
5380 ONOFF(option, ONIG_OPTION_WORD_BOUND_ALL_RANGE, 1);
5381 }
5382 else
5383 return ONIGERR_UNDEFINED_GROUP_OPTION;
5384 break;
5385
5386 case 'u':
5387 if ((IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_OPTION_PERL) ||
5388 IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_OPTION_RUBY)) &&
5389 (neg == 0)) {
5390 ONOFF(option, ONIG_OPTION_ASCII_RANGE, 1);
5391 ONOFF(option, ONIG_OPTION_POSIX_BRACKET_ALL_RANGE, 1);
5392 ONOFF(option, ONIG_OPTION_WORD_BOUND_ALL_RANGE, 1);
5393 }
5394 else
5395 return ONIGERR_UNDEFINED_GROUP_OPTION;
5396 break;
5397
5398 case 'd':
5399 if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_OPTION_PERL) &&
5400 (neg == 0)) {
5401 ONOFF(option, ONIG_OPTION_ASCII_RANGE, 1);
5402 }
5403 else if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_OPTION_RUBY) &&
5404 (neg == 0)) {
5405 ONOFF(option, ONIG_OPTION_ASCII_RANGE, 0);
5406 ONOFF(option, ONIG_OPTION_POSIX_BRACKET_ALL_RANGE, 0);
5407 ONOFF(option, ONIG_OPTION_WORD_BOUND_ALL_RANGE, 0);
5408 }
5409 else
5410 return ONIGERR_UNDEFINED_GROUP_OPTION;
5411 break;
5412
5413 case 'l':
5414 if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_OPTION_PERL) && (neg == 0)) {
5415 ONOFF(option, ONIG_OPTION_ASCII_RANGE, 1);
5416 }
5417 else
5418 return ONIGERR_UNDEFINED_GROUP_OPTION;
5419 break;
5420
5421 default:
5422 return ONIGERR_UNDEFINED_GROUP_OPTION;
5423 }
5424
5425 if (c == ')') {
5426 *np = node_new_option(option);
5427 CHECK_NULL_RETURN_MEMERR(*np);
5428 *src = p;
5429 return 2; /* option only */
5430 }
5431 else if (c == ':') {
5432 OnigOptionType prev = env->option;
5433
5434 env->option = option;
5435 r = fetch_token(tok, &p, end, env);
5436 if (r < 0) {
5437 env->option = prev;
5438 return r;
5439 }
5440 r = parse_subexp(&target, tok, term, &p, end, env);
5441 env->option = prev;
5442 if (r < 0) return r;
5443 *np = node_new_option(option);
5444 CHECK_NULL_RETURN_MEMERR(*np);
5445 NENCLOSE(*np)->target = target;
5446 *src = p;
5447 return 0;
5448 }
5449
5450 if (PEND) return ONIGERR_END_PATTERN_IN_GROUP;
5451 PFETCH(c);
5452 }
5453 }
5454 break;
5455
5456 default:
5457 return ONIGERR_UNDEFINED_GROUP_OPTION;
5458 }
5459 }
5460 else {
5461 if (ONIG_IS_OPTION_ON(env->option, ONIG_OPTION_DONT_CAPTURE_GROUP))
5462 goto group;
5463
5464 *np = node_new_enclose_memory(env->option, 0);
5465 CHECK_NULL_RETURN_MEMERR(*np);
5466 num = scan_env_add_mem_entry(env);
5467 if (num < 0) return num;
5468 NENCLOSE(*np)->regnum = num;
5469 }
5470
5471 CHECK_NULL_RETURN_MEMERR(*np);
5472 r = fetch_token(tok, &p, end, env);
5473 if (r < 0) return r;
5474 r = parse_subexp(&target, tok, term, &p, end, env);
5475 if (r < 0) {
5476 onig_node_free(target);
5477 return r;
5478 }
5479
5480 if (NTYPE(*np) == NT_ANCHOR)
5481 NANCHOR(*np)->target = target;
5482 else {
5483 NENCLOSE(*np)->target = target;
5484 if (NENCLOSE(*np)->type == ENCLOSE_MEMORY) {
5485 /* Don't move this to previous of parse_subexp() */
5486 r = scan_env_set_mem_node(env, NENCLOSE(*np)->regnum, *np);
5487 if (r != 0) return r;
5488 }
5489 else if (NENCLOSE(*np)->type == ENCLOSE_CONDITION) {
5490 if (NTYPE(target) != NT_ALT) {
5491 /* convert (?(cond)yes) to (?(cond)yes|empty) */
5492 work1 = node_new_empty();
5493 if (IS_NULL(work1)) goto err;
5494 work2 = onig_node_new_alt(work1, NULL_NODE);
5495 if (IS_NULL(work2)) goto err;
5496 work1 = onig_node_new_alt(target, work2);
5497 if (IS_NULL(work1)) goto err;
5498 NENCLOSE(*np)->target = work1;
5499 }
5500 }
5501 }
5502
5503 *src = p;
5504 return 0;
5505
5506 err:
5507 onig_node_free(work1);
5508 onig_node_free(work2);
5509 onig_node_free(*np);
5510 *np = NULL;
5511 return ONIGERR_MEMORY;
5512}
5513
5514static const char* const PopularQStr[] = {
5515 "?", "*", "+", "??", "*?", "+?"
5516};
5517
5518static const char* const ReduceQStr[] = {
5519 "", "", "*", "*?", "??", "+ and ??", "+? and ?"
5520};
5521
5522static int
5523set_quantifier(Node* qnode, Node* target, int group, ScanEnv* env)
5524{
5525 QtfrNode* qn;
5526
5527 qn = NQTFR(qnode);
5528 if (qn->lower == 1 && qn->upper == 1) {
5529 return 1;
5530 }
5531
5532 switch (NTYPE(target)) {
5533 case NT_STR:
5534 if (! group) {
5535 StrNode* sn = NSTR(target);
5536 if (str_node_can_be_split(sn, env->enc)) {
5537 Node* n = str_node_split_last_char(sn, env->enc);
5538 if (IS_NOT_NULL(n)) {
5539 qn->target = n;
5540 return 2;
5541 }
5542 }
5543 }
5544 break;
5545
5546 case NT_QTFR:
5547 { /* check redundant double repeat. */
5548 /* verbose warn (?:.?)? etc... but not warn (.?)? etc... */
5549 QtfrNode* qnt = NQTFR(target);
5550 int nestq_num = popular_quantifier_num(qn);
5551 int targetq_num = popular_quantifier_num(qnt);
5552
5553#ifdef USE_WARNING_REDUNDANT_NESTED_REPEAT_OPERATOR
5554 if (nestq_num >= 0 && targetq_num >= 0 &&
5555 IS_SYNTAX_BV(env->syntax, ONIG_SYN_WARN_REDUNDANT_NESTED_REPEAT)) {
5556 switch (ReduceTypeTable[targetq_num][nestq_num]) {
5557 case RQ_ASIS:
5558 break;
5559
5560 case RQ_DEL:
5561 if (onig_warn != onig_null_warn) {
5562 onig_syntax_warn(env, "regular expression has redundant nested repeat operator '%s'",
5563 PopularQStr[targetq_num]);
5564 }
5565 goto warn_exit;
5566 break;
5567
5568 default:
5569 if (onig_warn != onig_null_warn) {
5570 onig_syntax_warn(env, "nested repeat operator '%s' and '%s' was replaced with '%s' in regular expression",
5571 PopularQStr[targetq_num], PopularQStr[nestq_num],
5572 ReduceQStr[ReduceTypeTable[targetq_num][nestq_num]]);
5573 }
5574 goto warn_exit;
5575 break;
5576 }
5577 }
5578
5579 warn_exit:
5580#endif
5581 if (targetq_num >= 0) {
5582 if (nestq_num >= 0) {
5583 onig_reduce_nested_quantifier(qnode, target);
5584 goto q_exit;
5585 }
5586 else if (targetq_num == 1 || targetq_num == 2) { /* * or + */
5587 /* (?:a*){n,m}, (?:a+){n,m} => (?:a*){n,n}, (?:a+){n,n} */
5588 if (! IS_REPEAT_INFINITE(qn->upper) && qn->upper > 1 && qn->greedy) {
5589 qn->upper = (qn->lower == 0 ? 1 : qn->lower);
5590 }
5591 }
5592 }
5593 }
5594 break;
5595
5596 default:
5597 break;
5598 }
5599
5600 qn->target = target;
5601 q_exit:
5602 return 0;
5603}
5604
5605
5606#ifndef CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS
5607static int
5608clear_not_flag_cclass(CClassNode* cc, OnigEncoding enc)
5609{
5610 BBuf *tbuf;
5611 int r;
5612
5613 if (IS_NCCLASS_NOT(cc)) {
5614 bitset_invert(cc->bs);
5615
5616 if (! ONIGENC_IS_SINGLEBYTE(enc)) {
5617 r = not_code_range_buf(enc, cc->mbuf, &tbuf);
5618 if (r != 0) return r;
5619
5620 bbuf_free(cc->mbuf);
5621 cc->mbuf = tbuf;
5622 }
5623
5624 NCCLASS_CLEAR_NOT(cc);
5625 }
5626
5627 return 0;
5628}
5629#endif /* CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS */
5630
5631 typedef struct {
5632 ScanEnv* env;
5633 CClassNode* cc;
5634 CClassNode* asc_cc;
5635 Node* alt_root;
5636 Node** ptail;
5637} IApplyCaseFoldArg;
5638
5639static int
5640i_apply_case_fold(OnigCodePoint from, OnigCodePoint to[],
5641 int to_len, void* arg)
5642{
5643 IApplyCaseFoldArg* iarg;
5644 ScanEnv* env;
5645 CClassNode* cc;
5646 CClassNode* asc_cc;
5647 BitSetRef bs;
5648 int add_flag, r;
5649
5650 iarg = (IApplyCaseFoldArg* )arg;
5651 env = iarg->env;
5652 cc = iarg->cc;
5653 asc_cc = iarg->asc_cc;
5654 bs = cc->bs;
5655
5656 if (IS_NULL(asc_cc)) {
5657 add_flag = 0;
5658 }
5659 else if (ONIGENC_IS_ASCII_CODE(from) == ONIGENC_IS_ASCII_CODE(*to)) {
5660 add_flag = 1;
5661 }
5662 else {
5663 add_flag = onig_is_code_in_cc(env->enc, from, asc_cc);
5664 if (IS_NCCLASS_NOT(asc_cc))
5665 add_flag = !add_flag;
5666 }
5667
5668 if (to_len == 1) {
5669 int is_in = onig_is_code_in_cc(env->enc, from, cc);
5670#ifdef CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS
5671 if ((is_in != 0 && !IS_NCCLASS_NOT(cc)) ||
5672 (is_in == 0 && IS_NCCLASS_NOT(cc))) {
5673 if (add_flag) {
5674 if (ONIGENC_MBC_MINLEN(env->enc) > 1 || *to >= SINGLE_BYTE_SIZE) {
5675 r = add_code_range0(&(cc->mbuf), env, *to, *to, 0);
5676 if (r < 0) return r;
5677 }
5678 else {
5679 BITSET_SET_BIT(bs, *to);
5680 }
5681 }
5682 }
5683#else
5684 if (is_in != 0) {
5685 if (add_flag) {
5686 if (ONIGENC_MBC_MINLEN(env->enc) > 1 || *to >= SINGLE_BYTE_SIZE) {
5687 if (IS_NCCLASS_NOT(cc)) clear_not_flag_cclass(cc, env->enc);
5688 r = add_code_range0(&(cc->mbuf), env, *to, *to, 0);
5689 if (r < 0) return r;
5690 }
5691 else {
5692 if (IS_NCCLASS_NOT(cc)) {
5693 BITSET_CLEAR_BIT(bs, *to);
5694 }
5695 else {
5696 BITSET_SET_BIT(bs, *to);
5697 }
5698 }
5699 }
5700 }
5701#endif /* CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS */
5702 }
5703 else {
5704 int r, i, len;
5705 UChar buf[ONIGENC_CODE_TO_MBC_MAXLEN];
5706 Node *snode = NULL_NODE;
5707
5708 if (onig_is_code_in_cc(env->enc, from, cc)
5709#ifdef CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS
5710 && !IS_NCCLASS_NOT(cc)
5711#endif
5712 ) {
5713 for (i = 0; i < to_len; i++) {
5714 len = ONIGENC_CODE_TO_MBC(env->enc, to[i], buf);
5715 if (i == 0) {
5716 snode = onig_node_new_str(buf, buf + len);
5717 CHECK_NULL_RETURN_MEMERR(snode);
5718
5719 /* char-class expanded multi-char only
5720 compare with string folded at match time. */
5721 NSTRING_SET_AMBIG(snode);
5722 }
5723 else {
5724 r = onig_node_str_cat(snode, buf, buf + len);
5725 if (r < 0) {
5726 onig_node_free(snode);
5727 return r;
5728 }
5729 }
5730 }
5731
5732 *(iarg->ptail) = onig_node_new_alt(snode, NULL_NODE);
5733 CHECK_NULL_RETURN_MEMERR(*(iarg->ptail));
5734 iarg->ptail = &(NCDR((*(iarg->ptail))));
5735 }
5736 }
5737
5738 return 0;
5739}
5740
5741static int
5742cclass_case_fold(Node** np, CClassNode* cc, CClassNode* asc_cc, ScanEnv* env)
5743{
5744 int r;
5745 IApplyCaseFoldArg iarg;
5746
5747 iarg.env = env;
5748 iarg.cc = cc;
5749 iarg.asc_cc = asc_cc;
5750 iarg.alt_root = NULL_NODE;
5751 iarg.ptail = &(iarg.alt_root);
5752
5753 r = ONIGENC_APPLY_ALL_CASE_FOLD(env->enc, env->case_fold_flag,
5754 i_apply_case_fold, &iarg);
5755 if (r != 0) {
5756 onig_node_free(iarg.alt_root);
5757 return r;
5758 }
5759 if (IS_NOT_NULL(iarg.alt_root)) {
5760 Node* work = onig_node_new_alt(*np, iarg.alt_root);
5761 if (IS_NULL(work)) {
5762 onig_node_free(iarg.alt_root);
5763 return ONIGERR_MEMORY;
5764 }
5765 *np = work;
5766 }
5767 return r;
5768}
5769
5770static int
5771node_linebreak(Node** np, ScanEnv* env)
5772{
5773 /* same as (?>\x0D\x0A|[\x0A-\x0D\x{85}\x{2028}\x{2029}]) */
5774 Node* left = NULL;
5775 Node* right = NULL;
5776 Node* target1 = NULL;
5777 Node* target2 = NULL;
5778 CClassNode* cc;
5779 int num1, num2, r;
5780 UChar buf[ONIGENC_CODE_TO_MBC_MAXLEN * 2];
5781
5782 /* \x0D\x0A */
5783 num1 = ONIGENC_CODE_TO_MBC(env->enc, 0x0D, buf);
5784 if (num1 < 0) return num1;
5785 num2 = ONIGENC_CODE_TO_MBC(env->enc, 0x0A, buf + num1);
5786 if (num2 < 0) return num2;
5787 left = node_new_str_raw(buf, buf + num1 + num2);
5788 if (IS_NULL(left)) goto err;
5789
5790 /* [\x0A-\x0D] or [\x0A-\x0D\x{85}\x{2028}\x{2029}] */
5791 right = node_new_cclass();
5792 if (IS_NULL(right)) goto err;
5793 cc = NCCLASS(right);
5794 if (ONIGENC_MBC_MINLEN(env->enc) > 1) {
5795 r = add_code_range(&(cc->mbuf), env, 0x0A, 0x0D);
5796 if (r != 0) goto err;
5797 }
5798 else {
5799 bitset_set_range(env, cc->bs, 0x0A, 0x0D);
5800 }
5801
5802 /* TODO: move this block to enc/unicode.c */
5803 if (ONIGENC_IS_UNICODE(env->enc)) {
5804 /* UTF-8, UTF-16BE/LE, UTF-32BE/LE */
5805 r = add_code_range(&(cc->mbuf), env, 0x85, 0x85);
5806 if (r != 0) goto err;
5807 r = add_code_range(&(cc->mbuf), env, 0x2028, 0x2029);
5808 if (r != 0) goto err;
5809 }
5810
5811 /* ...|... */
5812 target1 = onig_node_new_alt(right, NULL_NODE);
5813 if (IS_NULL(target1)) goto err;
5814 right = NULL;
5815 target2 = onig_node_new_alt(left, target1);
5816 if (IS_NULL(target2)) goto err;
5817 left = NULL;
5818 target1 = NULL;
5819
5820 /* (?>...) */
5821 *np = node_new_enclose(ENCLOSE_STOP_BACKTRACK);
5822 if (IS_NULL(*np)) goto err;
5823 NENCLOSE(*np)->target = target2;
5824 return ONIG_NORMAL;
5825
5826 err:
5827 onig_node_free(left);
5828 onig_node_free(right);
5829 onig_node_free(target1);
5830 onig_node_free(target2);
5831 return ONIGERR_MEMORY;
5832}
5833
5834static int
5835propname2ctype(ScanEnv* env, const char* propname)
5836{
5837 UChar* name = (UChar* )propname;
5838 UChar* name_end = name + strlen(propname);
5839 int ctype = env->enc->property_name_to_ctype(ONIG_ENCODING_ASCII,
5840 name, name_end);
5841 if (ctype < 0) {
5842 onig_scan_env_set_error_string(env, ctype, name, name_end);
5843 }
5844 return ctype;
5845}
5846
5847static int
5848add_property_to_cc(CClassNode* cc, const char* propname, int not, ScanEnv* env)
5849{
5850 int ctype = propname2ctype(env, propname);
5851 if (ctype < 0) return ctype;
5852 return add_ctype_to_cc(cc, ctype, not, 0, env);
5853}
5854
5855/*
5856 * helper methods for node_extended_grapheme_cluster (/\X/)
5857 */
5858static int
5859create_property_node(Node **np, ScanEnv* env, const char* propname)
5860{
5861 int r;
5862 CClassNode* cc;
5863
5864 *np = node_new_cclass();
5865 if (IS_NULL(*np)) return ONIGERR_MEMORY;
5866 cc = NCCLASS(*np);
5867 r = add_property_to_cc(cc, propname, 0, env);
5868 if (r != 0)
5869 onig_node_free(*np);
5870 return r;
5871}
5872
5873static int
5874quantify_node(Node **np, int lower, int upper)
5875{
5876 Node* tmp = node_new_quantifier(lower, upper, 0);
5877 if (IS_NULL(tmp)) return ONIGERR_MEMORY;
5878 NQTFR(tmp)->target = *np;
5879 *np = tmp;
5880 return 0;
5881}
5882
5883static int
5884quantify_property_node(Node **np, ScanEnv* env, const char* propname, char repetitions)
5885{
5886 int r;
5887 int lower = 0;
5888 int upper = REPEAT_INFINITE;
5889
5890 r = create_property_node(np, env, propname);
5891 if (r != 0) return r;
5892 switch (repetitions) {
5893 case '?': upper = 1; break;
5894 case '+': lower = 1; break;
5895 case '*': break;
5896 case '2': lower = upper = 2; break;
5897 default : return ONIGERR_PARSER_BUG;
5898 }
5899 return quantify_node(np, lower, upper);
5900}
5901
5902#define LIST 0
5903#define ALT 1
5904
5905/* IMPORTANT: Make sure node_array ends with NULL_NODE */
5906static int
5907create_node_from_array(int kind, Node **np, Node **node_array)
5908{
5909 Node* tmp = NULL_NODE;
5910 int i = 0;
5911
5912 while (node_array[i] != NULL_NODE) i++;
5913 while (--i >= 0) {
5914 *np = kind==LIST ? node_new_list(node_array[i], tmp)
5915 : onig_node_new_alt(node_array[i], tmp);
5916 if (IS_NULL(*np)) {
5917 while (i >= 0) {
5918 onig_node_free(node_array[i]);
5919 node_array[i--] = NULL_NODE;
5920 }
5921 onig_node_free(tmp);
5922 return ONIGERR_MEMORY;
5923 }
5924 else
5925 node_array[i] = NULL_NODE;
5926 tmp = *np;
5927 }
5928 return 0;
5929}
5930
5931#define R_ERR(call) r=(call);if(r!=0)goto err
5932
5933/* Memory layout for common node array:
5934 * The main purpose is to be able to easily free all leftover nodes
5935 * after an error. As a side effect, we share some memory.
5936 *
5937 * The layout is as shown below (each line corresponds to one call of
5938 * create_node_from_array()). Because create_node_from_array sets all
5939 * nodes of the source to NULL_NODE, we can overlap the target array
5940 * as long as we do not override the actual target location.
5941 *
5942 * Target Array name Index
5943 *
5944 * node_array 0 1 2 3 4 5 6 7 8 9 A B C D E F
5945 * top_alts alts[5] 0 1 2 3 4*
5946 * alts+1 list[4] 0 1 2 3*
5947 * list+1 core_alts[7] 0 1 2 3 4 5 6*
5948 * core_alts+0 H_list[4] 0 1 2 3*
5949 * H_list+1 H_alt2[4] 0 1 2 3*
5950 * h_alt2+1 H_list2[3] 0 1 2*
5951 * core_alts+4 XP_list[4] 0 1 2 3*
5952 * XP_list+1 Ex_list[4] 0 1 2 3*
5953 */
5954#define NODE_COMMON_SIZE 16
5955
5956static int
5957node_extended_grapheme_cluster(Node** np, ScanEnv* env)
5958{
5959 Node* tmp = NULL;
5960 Node* np1 = NULL;
5961 Node* top_alt = NULL;
5962 int r = 0;
5963 int num1;
5964 int i;
5965 int any_target_position;
5966 UChar buf[ONIGENC_CODE_TO_MBC_MAXLEN * 2];
5967 OnigOptionType option;
5968 /* node_common is function-global so that we can free all nodes
5969 * in case of error. Unused slots are set to NULL_NODE at all times. */
5970 Node *node_common[NODE_COMMON_SIZE];
5971 Node **alts = node_common+0; /* size: 5 */
5972
5973 for (i=0; i<NODE_COMMON_SIZE; i++)
5974 node_common[i] = NULL_NODE;
5975
5976 /* CRLF, common for both Unicode and non-Unicode */
5977 /* \x0D\x0A */
5978 r = ONIGENC_CODE_TO_MBC(env->enc, 0x0D, buf);
5979 if (r < 0) goto err;
5980 num1 = r;
5981 r = ONIGENC_CODE_TO_MBC(env->enc, 0x0A, buf + num1);
5982 if (r < 0) goto err;
5983 alts[0] = node_new_str_raw(buf, buf + num1 + r);
5984 if (IS_NULL(alts[0])) goto err;
5985
5986#ifdef USE_UNICODE_PROPERTIES
5987 if (ONIGENC_IS_UNICODE(env->enc)) { /* UTF-8, UTF-16BE/LE, UTF-32BE/LE */
5988 CClassNode* cc;
5989
5990 if (propname2ctype(env, "Grapheme_Cluster_Break=Extend") < 0) goto err;
5991 /* Unicode 11.0.0
5992 * CRLF (already done)
5993 * | [Control CR LF]
5994 * | precore* core postcore*
5995 * | . (to catch invalid stuff, because this seems to be spec for String#grapheme_clusters) */
5996
5997 /* [Control CR LF] (CR and LF are not in the spec, but this is a conformed fix) */
5998 alts[1] = node_new_cclass();
5999 if (IS_NULL(alts[1])) goto err;
6000 cc = NCCLASS(alts[1]);
6001 R_ERR(add_property_to_cc(cc, "Grapheme_Cluster_Break=Control", 0, env));
6002 if (ONIGENC_MBC_MINLEN(env->enc) > 1) { /* UTF-16/UTF-32 */
6003 R_ERR(add_code_range(&(cc->mbuf), env, 0x000A, 0x000A)); /* CR */
6004 R_ERR(add_code_range(&(cc->mbuf), env, 0x000D, 0x000D)); /* LF */
6005 }
6006 else {
6007 BITSET_SET_BIT(cc->bs, 0x0a);
6008 BITSET_SET_BIT(cc->bs, 0x0d);
6009 }
6010
6011 /* precore* core postcore* */
6012 {
6013 Node **list = alts + 3; /* size: 4 */
6014
6015 /* precore*; precore := Prepend */
6016 R_ERR(quantify_property_node(list+0, env, "Grapheme_Cluster_Break=Prepend", '*'));
6017
6018 /* core := hangul-syllable
6019 * | ri-sequence
6020 * | xpicto-sequence
6021 * | [^Control CR LF] */
6022 {
6023 Node **core_alts = list + 2; /* size: 7 */
6024
6025 /* hangul-syllable :=
6026 * L* (V+ | LV V* | LVT) T*
6027 * | L+
6028 * | T+ */
6029 /* hangul-syllable is an alternative (would be called H_alt)
6030 * inside an alternative, but we flatten it into core_alts */
6031
6032 /* L* (V+ | LV V* | LVT) T* */
6033 {
6034 Node **H_list = core_alts + 1; /* size: 4 */
6035 R_ERR(quantify_property_node(H_list+0, env, "Grapheme_Cluster_Break=L", '*'));
6036
6037 /* V+ | LV V* | LVT */
6038 {
6039 Node **H_alt2 = H_list + 2; /* size: 4 */
6040 R_ERR(quantify_property_node(H_alt2+0, env, "Grapheme_Cluster_Break=V", '+'));
6041
6042 /* LV V* */
6043 {
6044 Node **H_list2 = H_alt2 + 2; /* size: 3 */
6045
6046 R_ERR(create_property_node(H_list2+0, env, "Grapheme_Cluster_Break=LV"));
6047 R_ERR(quantify_property_node(H_list2+1, env, "Grapheme_Cluster_Break=V", '*'));
6048 R_ERR(create_node_from_array(LIST, H_alt2+1, H_list2));
6049 }
6050
6051 R_ERR(create_property_node(H_alt2+2, env, "Grapheme_Cluster_Break=LVT"));
6052 R_ERR(create_node_from_array(ALT, H_list+1, H_alt2));
6053 }
6054
6055 R_ERR(quantify_property_node(H_list+2, env, "Grapheme_Cluster_Break=T", '*'));
6056 R_ERR(create_node_from_array(LIST, core_alts+0, H_list));
6057 }
6058
6059 R_ERR(quantify_property_node(core_alts+1, env, "Grapheme_Cluster_Break=L", '+'));
6060 R_ERR(quantify_property_node(core_alts+2, env, "Grapheme_Cluster_Break=T", '+'));
6061 /* end of hangul-syllable */
6062
6063 /* ri-sequence := RI RI */
6064 R_ERR(quantify_property_node(core_alts+3, env, "Regional_Indicator", '2'));
6065
6066 /* xpicto-sequence := \p{Extended_Pictographic} (Extend* ZWJ \p{Extended_Pictographic})* */
6067 {
6068 Node **XP_list = core_alts + 5; /* size: 3 */
6069 R_ERR(create_property_node(XP_list+0, env, "Extended_Pictographic"));
6070
6071 /* (Extend* ZWJ \p{Extended_Pictographic})* */
6072 {
6073 Node **Ex_list = XP_list + 2; /* size: 4 */
6074 /* assert(Ex_list+4 == node_common+NODE_COMMON_SIZE); */
6075 R_ERR(quantify_property_node(Ex_list+0, env, "Grapheme_Cluster_Break=Extend", '*'));
6076
6077 /* ZWJ (ZERO WIDTH JOINER) */
6078 r = ONIGENC_CODE_TO_MBC(env->enc, 0x200D, buf);
6079 if (r < 0) goto err;
6080 Ex_list[1] = node_new_str_raw(buf, buf + r);
6081 if (IS_NULL(Ex_list[1])) goto err;
6082
6083 R_ERR(create_property_node(Ex_list+2, env, "Extended_Pictographic"));
6084 R_ERR(create_node_from_array(LIST, XP_list+1, Ex_list));
6085 }
6086 R_ERR(quantify_node(XP_list+1, 0, REPEAT_INFINITE)); /* TODO: Check about node freeing */
6087
6088 R_ERR(create_node_from_array(LIST, core_alts+4, XP_list));
6089 }
6090
6091 /* [^Control CR LF] */
6092 core_alts[5] = node_new_cclass();
6093 if (IS_NULL(core_alts[5])) goto err;
6094 cc = NCCLASS(core_alts[5]);
6095 if (ONIGENC_MBC_MINLEN(env->enc) > 1) { /* UTF-16/UTF-32 */
6096 BBuf *inverted_buf = NULL;
6097
6098 /* TODO: fix false warning */
6099 const int dup_not_warned = env->warnings_flag | ~ONIG_SYN_WARN_CC_DUP;
6100 env->warnings_flag |= ONIG_SYN_WARN_CC_DUP;
6101
6102 /* Start with a positive buffer and invert at the end.
6103 * Otherwise, adding single-character ranges work the wrong way. */
6104 R_ERR(add_property_to_cc(cc, "Grapheme_Cluster_Break=Control", 0, env));
6105 R_ERR(add_code_range(&(cc->mbuf), env, 0x000A, 0x000A)); /* CR */
6106 R_ERR(add_code_range(&(cc->mbuf), env, 0x000D, 0x000D)); /* LF */
6107 R_ERR(not_code_range_buf(env->enc, cc->mbuf, &inverted_buf, env));
6108 bbuf_free(cc->mbuf);
6109 cc->mbuf = inverted_buf;
6110
6111 env->warnings_flag &= dup_not_warned; /* TODO: fix false warning */
6112 }
6113 else {
6114 R_ERR(add_property_to_cc(cc, "Grapheme_Cluster_Break=Control", 1, env));
6115 BITSET_CLEAR_BIT(cc->bs, 0x0a);
6116 BITSET_CLEAR_BIT(cc->bs, 0x0d);
6117 }
6118
6119 R_ERR(create_node_from_array(ALT, list+1, core_alts));
6120 }
6121
6122 /* postcore*; postcore = [Extend ZWJ SpacingMark] */
6123 R_ERR(create_property_node(list+2, env, "Grapheme_Cluster_Break=Extend"));
6124 cc = NCCLASS(list[2]);
6125 R_ERR(add_property_to_cc(cc, "Grapheme_Cluster_Break=SpacingMark", 0, env));
6126 R_ERR(add_code_range(&(cc->mbuf), env, 0x200D, 0x200D));
6127 R_ERR(quantify_node(list+2, 0, REPEAT_INFINITE));
6128
6129 R_ERR(create_node_from_array(LIST, alts+2, list));
6130 }
6131
6132 any_target_position = 3;
6133 }
6134 else
6135#endif /* USE_UNICODE_PROPERTIES */
6136 {
6137 any_target_position = 1;
6138 }
6139
6140 /* PerlSyntax: (?s:.), RubySyntax: (?m:.), common for both Unicode and non-Unicode */
6141 /* Not in Unicode spec (UAX #29), but added to catch invalid stuff,
6142 * because this is Ruby spec for String#grapheme_clusters. */
6143 np1 = node_new_anychar();
6144 if (IS_NULL(np1)) goto err;
6145
6146 option = env->option;
6147 ONOFF(option, ONIG_OPTION_MULTILINE, 0);
6148 tmp = node_new_option(option);
6149 if (IS_NULL(tmp)) goto err;
6150 NENCLOSE(tmp)->target = np1;
6151 alts[any_target_position] = tmp;
6152 np1 = NULL;
6153
6154 R_ERR(create_node_from_array(ALT, &top_alt, alts));
6155
6156 /* (?>): For efficiency, because there is no text piece
6157 * that is not in a grapheme cluster, and there is only one way
6158 * to split a string into grapheme clusters. */
6159 tmp = node_new_enclose(ENCLOSE_STOP_BACKTRACK);
6160 if (IS_NULL(tmp)) goto err;
6161 NENCLOSE(tmp)->target = top_alt;
6162 np1 = tmp;
6163
6164#ifdef USE_UNICODE_PROPERTIES
6165 if (ONIGENC_IS_UNICODE(env->enc)) {
6166 /* Don't ignore case. */
6167 option = env->option;
6168 ONOFF(option, ONIG_OPTION_IGNORECASE, 1);
6169 *np = node_new_option(option);
6170 if (IS_NULL(*np)) goto err;
6171 NENCLOSE(*np)->target = np1;
6172 }
6173 else
6174#endif
6175 {
6176 *np = np1;
6177 }
6178 return ONIG_NORMAL;
6179
6180 err:
6181 onig_node_free(np1);
6182 for (i=0; i<NODE_COMMON_SIZE; i++)
6183 onig_node_free(node_common[i]);
6184 return (r == 0) ? ONIGERR_MEMORY : r;
6185}
6186#undef R_ERR
6187
6188static int
6189countbits(unsigned int bits)
6190{
6191 bits = (bits & 0x55555555) + ((bits >> 1) & 0x55555555);
6192 bits = (bits & 0x33333333) + ((bits >> 2) & 0x33333333);
6193 bits = (bits & 0x0f0f0f0f) + ((bits >> 4) & 0x0f0f0f0f);
6194 bits = (bits & 0x00ff00ff) + ((bits >> 8) & 0x00ff00ff);
6195 return (bits & 0x0000ffff) + ((bits >>16) & 0x0000ffff);
6196}
6197
6198static int
6199is_onechar_cclass(CClassNode* cc, OnigCodePoint* code)
6200{
6201 const OnigCodePoint not_found = ONIG_LAST_CODE_POINT;
6202 OnigCodePoint c = not_found;
6203 int i;
6204 BBuf *bbuf = cc->mbuf;
6205
6206 if (IS_NCCLASS_NOT(cc)) return 0;
6207
6208 /* check bbuf */
6209 if (IS_NOT_NULL(bbuf)) {
6210 OnigCodePoint n, *data;
6211 GET_CODE_POINT(n, bbuf->p);
6212 data = (OnigCodePoint* )(bbuf->p) + 1;
6213 if ((n == 1) && (data[0] == data[1])) {
6214 /* only one char found in the bbuf, save the code point. */
6215 c = data[0];
6216 if (((c < SINGLE_BYTE_SIZE) && BITSET_AT(cc->bs, c))) {
6217 /* skip if c is included in the bitset */
6218 c = not_found;
6219 }
6220 }
6221 else {
6222 return 0; /* the bbuf contains multiple chars */
6223 }
6224 }
6225
6226 /* check bitset */
6227 for (i = 0; i < BITSET_SIZE; i++) {
6228 Bits b1 = cc->bs[i];
6229 if (b1 != 0) {
6230 if (((b1 & (b1 - 1)) == 0) && (c == not_found)) {
6231 c = BITS_IN_ROOM * i + countbits(b1 - 1);
6232 } else {
6233 return 0; /* the character class contains multiple chars */
6234 }
6235 }
6236 }
6237
6238 if (c != not_found) {
6239 *code = c;
6240 return 1;
6241 }
6242
6243 /* the character class contains no char. */
6244 return 0;
6245}
6246
6247
6248static int
6249parse_exp(Node** np, OnigToken* tok, int term,
6250 UChar** src, UChar* end, ScanEnv* env)
6251{
6252 int r, len, group = 0;
6253 Node* qn;
6254 Node** targetp;
6255
6256 *np = NULL;
6257 if (tok->type == (enum TokenSyms )term)
6258 goto end_of_token;
6259
6260 switch (tok->type) {
6261 case TK_ALT:
6262 case TK_EOT:
6263 end_of_token:
6264 *np = node_new_empty();
6265 return tok->type;
6266 break;
6267
6268 case TK_SUBEXP_OPEN:
6269 r = parse_enclose(np, tok, TK_SUBEXP_CLOSE, src, end, env);
6270 if (r < 0) return r;
6271 if (r == 1) group = 1;
6272 else if (r == 2) { /* option only */
6273 Node* target;
6274 OnigOptionType prev = env->option;
6275
6276 env->option = NENCLOSE(*np)->option;
6277 r = fetch_token(tok, src, end, env);
6278 if (r < 0) {
6279 env->option = prev;
6280 return r;
6281 }
6282 r = parse_subexp(&target, tok, term, src, end, env);
6283 env->option = prev;
6284 if (r < 0) {
6285 onig_node_free(target);
6286 return r;
6287 }
6288 NENCLOSE(*np)->target = target;
6289 return tok->type;
6290 }
6291 break;
6292
6293 case TK_SUBEXP_CLOSE:
6294 if (! IS_SYNTAX_BV(env->syntax, ONIG_SYN_ALLOW_UNMATCHED_CLOSE_SUBEXP))
6295 return ONIGERR_UNMATCHED_CLOSE_PARENTHESIS;
6296
6297 if (tok->escaped) goto tk_raw_byte;
6298 else goto tk_byte;
6299 break;
6300
6301 case TK_LINEBREAK:
6302 r = node_linebreak(np, env);
6303 if (r < 0) return r;
6304 break;
6305
6306 case TK_EXTENDED_GRAPHEME_CLUSTER:
6307 r = node_extended_grapheme_cluster(np, env);
6308 if (r < 0) return r;
6309 break;
6310
6311 case TK_KEEP:
6312 *np = onig_node_new_anchor(ANCHOR_KEEP);
6313 CHECK_NULL_RETURN_MEMERR(*np);
6314 break;
6315
6316 case TK_STRING:
6317 tk_byte:
6318 {
6319 *np = node_new_str(tok->backp, *src);
6320 CHECK_NULL_RETURN_MEMERR(*np);
6321
6322 string_loop:
6323 while (1) {
6324 r = fetch_token(tok, src, end, env);
6325 if (r < 0) return r;
6326 if (r == TK_STRING) {
6327 r = onig_node_str_cat(*np, tok->backp, *src);
6328 }
6329#ifndef NUMBERED_CHAR_IS_NOT_CASE_AMBIG
6330 else if (r == TK_CODE_POINT) {
6331 r = node_str_cat_codepoint(*np, env->enc, tok->u.code);
6332 }
6333#endif
6334 else {
6335 break;
6336 }
6337 if (r < 0) return r;
6338 }
6339
6340 string_end:
6341 targetp = np;
6342 goto repeat;
6343 }
6344 break;
6345
6346 case TK_RAW_BYTE:
6347 tk_raw_byte:
6348 {
6349 *np = node_new_str_raw_char((UChar )tok->u.c);
6350 CHECK_NULL_RETURN_MEMERR(*np);
6351 len = 1;
6352 while (1) {
6353 if (len >= ONIGENC_MBC_MINLEN(env->enc)) {
6354 if (len == enclen(env->enc, NSTR(*np)->s, NSTR(*np)->end)) {
6355 r = fetch_token(tok, src, end, env);
6356 NSTRING_CLEAR_RAW(*np);
6357 goto string_end;
6358 }
6359 }
6360
6361 r = fetch_token(tok, src, end, env);
6362 if (r < 0) return r;
6363 if (r != TK_RAW_BYTE) {
6364 /* Don't use this, it is wrong for little endian encodings. */
6365#ifdef USE_PAD_TO_SHORT_BYTE_CHAR
6366 int rem;
6367 if (len < ONIGENC_MBC_MINLEN(env->enc)) {
6368 rem = ONIGENC_MBC_MINLEN(env->enc) - len;
6369 (void )node_str_head_pad(NSTR(*np), rem, (UChar )0);
6370 if (len + rem == enclen(env->enc, NSTR(*np)->s)) {
6371 NSTRING_CLEAR_RAW(*np);
6372 goto string_end;
6373 }
6374 }
6375#endif
6376 return ONIGERR_TOO_SHORT_MULTI_BYTE_STRING;
6377 }
6378
6379 r = node_str_cat_char(*np, (UChar )tok->u.c);
6380 if (r < 0) return r;
6381
6382 len++;
6383 }
6384 }
6385 break;
6386
6387 case TK_CODE_POINT:
6388 {
6389 *np = node_new_empty();
6390 CHECK_NULL_RETURN_MEMERR(*np);
6391 r = node_str_cat_codepoint(*np, env->enc, tok->u.code);
6392 if (r != 0) return r;
6393#ifdef NUMBERED_CHAR_IS_NOT_CASE_AMBIG
6394 NSTRING_SET_RAW(*np);
6395#else
6396 goto string_loop;
6397#endif
6398 }
6399 break;
6400
6401 case TK_QUOTE_OPEN:
6402 {
6403 OnigCodePoint end_op[2];
6404 UChar *qstart, *qend, *nextp;
6405
6406 end_op[0] = (OnigCodePoint )MC_ESC(env->syntax);
6407 end_op[1] = (OnigCodePoint )'E';
6408 qstart = *src;
6409 qend = find_str_position(end_op, 2, qstart, end, &nextp, env->enc);
6410 if (IS_NULL(qend)) {
6411 nextp = qend = end;
6412 }
6413 *np = node_new_str(qstart, qend);
6414 CHECK_NULL_RETURN_MEMERR(*np);
6415 *src = nextp;
6416 }
6417 break;
6418
6419 case TK_CHAR_TYPE:
6420 {
6421 switch (tok->u.prop.ctype) {
6422 case ONIGENC_CTYPE_WORD:
6423 *np = node_new_ctype(tok->u.prop.ctype, tok->u.prop.not,
6424 IS_ASCII_RANGE(env->option));
6425 CHECK_NULL_RETURN_MEMERR(*np);
6426 break;
6427
6428 case ONIGENC_CTYPE_SPACE:
6429 case ONIGENC_CTYPE_DIGIT:
6430 case ONIGENC_CTYPE_XDIGIT:
6431 {
6432 CClassNode* cc;
6433
6434 *np = node_new_cclass();
6435 CHECK_NULL_RETURN_MEMERR(*np);
6436 cc = NCCLASS(*np);
6437 r = add_ctype_to_cc(cc, tok->u.prop.ctype, 0,
6438 IS_ASCII_RANGE(env->option), env);
6439 if (r != 0) return r;
6440 if (tok->u.prop.not != 0) NCCLASS_SET_NOT(cc);
6441 }
6442 break;
6443
6444 default:
6445 return ONIGERR_PARSER_BUG;
6446 break;
6447 }
6448 }
6449 break;
6450
6451 case TK_CHAR_PROPERTY:
6452 r = parse_char_property(np, tok, src, end, env);
6453 if (r != 0) return r;
6454 break;
6455
6456 case TK_CC_OPEN:
6457 {
6458 Node *asc_node;
6459 CClassNode* cc;
6460 OnigCodePoint code;
6461
6462 r = parse_char_class(np, &asc_node, tok, src, end, env);
6463 if (r != 0) {
6464 onig_node_free(asc_node);
6465 return r;
6466 }
6467
6468 cc = NCCLASS(*np);
6469 if (is_onechar_cclass(cc, &code)) {
6470 onig_node_free(*np);
6471 onig_node_free(asc_node);
6472 *np = node_new_empty();
6473 CHECK_NULL_RETURN_MEMERR(*np);
6474 r = node_str_cat_codepoint(*np, env->enc, code);
6475 if (r != 0) return r;
6476 goto string_loop;
6477 }
6478 if (IS_IGNORECASE(env->option)) {
6479 r = cclass_case_fold(np, cc, NCCLASS(asc_node), env);
6480 if (r != 0) {
6481 onig_node_free(asc_node);
6482 return r;
6483 }
6484 }
6485 onig_node_free(asc_node);
6486 }
6487 break;
6488
6489 case TK_ANYCHAR:
6490 *np = node_new_anychar();
6491 CHECK_NULL_RETURN_MEMERR(*np);
6492 break;
6493
6494 case TK_ANYCHAR_ANYTIME:
6495 *np = node_new_anychar();
6496 CHECK_NULL_RETURN_MEMERR(*np);
6497 qn = node_new_quantifier(0, REPEAT_INFINITE, 0);
6498 CHECK_NULL_RETURN_MEMERR(qn);
6499 NQTFR(qn)->target = *np;
6500 *np = qn;
6501 break;
6502
6503 case TK_BACKREF:
6504 len = tok->u.backref.num;
6505 *np = node_new_backref(len,
6506 (len > 1 ? tok->u.backref.refs : &(tok->u.backref.ref1)),
6507 tok->u.backref.by_name,
6508#ifdef USE_BACKREF_WITH_LEVEL
6509 tok->u.backref.exist_level,
6510 tok->u.backref.level,
6511#endif
6512 env);
6513 CHECK_NULL_RETURN_MEMERR(*np);
6514 break;
6515
6516#ifdef USE_SUBEXP_CALL
6517 case TK_CALL:
6518 {
6519 int gnum = tok->u.call.gnum;
6520
6521 if (gnum < 0 || tok->u.call.rel != 0) {
6522 if (gnum > 0) gnum--;
6523 gnum = BACKREF_REL_TO_ABS(gnum, env);
6524 if (gnum <= 0)
6525 return ONIGERR_INVALID_BACKREF;
6526 }
6527 *np = node_new_call(tok->u.call.name, tok->u.call.name_end, gnum);
6528 CHECK_NULL_RETURN_MEMERR(*np);
6529 env->num_call++;
6530 }
6531 break;
6532#endif
6533
6534 case TK_ANCHOR:
6535 *np = onig_node_new_anchor(tok->u.anchor.subtype);
6536 CHECK_NULL_RETURN_MEMERR(*np);
6537 NANCHOR(*np)->ascii_range = tok->u.anchor.ascii_range;
6538 break;
6539
6540 case TK_OP_REPEAT:
6541 case TK_INTERVAL:
6542 if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_CONTEXT_INDEP_REPEAT_OPS)) {
6543 if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_CONTEXT_INVALID_REPEAT_OPS))
6544 return ONIGERR_TARGET_OF_REPEAT_OPERATOR_NOT_SPECIFIED;
6545 else
6546 *np = node_new_empty();
6547 }
6548 else {
6549 goto tk_byte;
6550 }
6551 break;
6552
6553 default:
6554 return ONIGERR_PARSER_BUG;
6555 break;
6556 }
6557
6558 {
6559 targetp = np;
6560
6561 re_entry:
6562 r = fetch_token(tok, src, end, env);
6563 if (r < 0) return r;
6564
6565 repeat:
6566 if (r == TK_OP_REPEAT || r == TK_INTERVAL) {
6567 if (is_invalid_quantifier_target(*targetp))
6568 return ONIGERR_TARGET_OF_REPEAT_OPERATOR_INVALID;
6569
6570 qn = node_new_quantifier(tok->u.repeat.lower, tok->u.repeat.upper,
6571 (r == TK_INTERVAL ? 1 : 0));
6572 CHECK_NULL_RETURN_MEMERR(qn);
6573 NQTFR(qn)->greedy = tok->u.repeat.greedy;
6574 r = set_quantifier(qn, *targetp, group, env);
6575 if (r < 0) {
6576 onig_node_free(qn);
6577 return r;
6578 }
6579
6580 if (tok->u.repeat.possessive != 0) {
6581 Node* en;
6582 en = node_new_enclose(ENCLOSE_STOP_BACKTRACK);
6583 if (IS_NULL(en)) {
6584 onig_node_free(qn);
6585 return ONIGERR_MEMORY;
6586 }
6587 NENCLOSE(en)->target = qn;
6588 qn = en;
6589 }
6590
6591 if (r == 0) {
6592 *targetp = qn;
6593 }
6594 else if (r == 1) {
6595 onig_node_free(qn);
6596 }
6597 else if (r == 2) { /* split case: /abc+/ */
6598 Node *tmp;
6599
6600 *targetp = node_new_list(*targetp, NULL);
6601 if (IS_NULL(*targetp)) {
6602 onig_node_free(qn);
6603 return ONIGERR_MEMORY;
6604 }
6605 tmp = NCDR(*targetp) = node_new_list(qn, NULL);
6606 if (IS_NULL(tmp)) {
6607 onig_node_free(qn);
6608 return ONIGERR_MEMORY;
6609 }
6610 targetp = &(NCAR(tmp));
6611 }
6612 goto re_entry;
6613 }
6614 }
6615
6616 return r;
6617}
6618
6619static int
6620parse_branch(Node** top, OnigToken* tok, int term,
6621 UChar** src, UChar* end, ScanEnv* env)
6622{
6623 int r;
6624 Node *node, **headp;
6625
6626 *top = NULL;
6627 r = parse_exp(&node, tok, term, src, end, env);
6628 if (r < 0) {
6629 onig_node_free(node);
6630 return r;
6631 }
6632
6633 if (r == TK_EOT || r == term || r == TK_ALT) {
6634 *top = node;
6635 }
6636 else {
6637 *top = node_new_list(node, NULL);
6638 headp = &(NCDR(*top));
6639 while (r != TK_EOT && r != term && r != TK_ALT) {
6640 r = parse_exp(&node, tok, term, src, end, env);
6641 if (r < 0) {
6642 onig_node_free(node);
6643 return r;
6644 }
6645
6646 if (NTYPE(node) == NT_LIST) {
6647 *headp = node;
6648 while (IS_NOT_NULL(NCDR(node))) node = NCDR(node);
6649 headp = &(NCDR(node));
6650 }
6651 else {
6652 *headp = node_new_list(node, NULL);
6653 headp = &(NCDR(*headp));
6654 }
6655 }
6656 }
6657
6658 return r;
6659}
6660
6661/* term_tok: TK_EOT or TK_SUBEXP_CLOSE */
6662static int
6663parse_subexp(Node** top, OnigToken* tok, int term,
6664 UChar** src, UChar* end, ScanEnv* env)
6665{
6666 int r;
6667 Node *node, **headp;
6668
6669 *top = NULL;
6670 env->parse_depth++;
6671 if (env->parse_depth > ParseDepthLimit)
6672 return ONIGERR_PARSE_DEPTH_LIMIT_OVER;
6673 r = parse_branch(&node, tok, term, src, end, env);
6674 if (r < 0) {
6675 onig_node_free(node);
6676 return r;
6677 }
6678
6679 if (r == term) {
6680 *top = node;
6681 }
6682 else if (r == TK_ALT) {
6683 *top = onig_node_new_alt(node, NULL);
6684 headp = &(NCDR(*top));
6685 while (r == TK_ALT) {
6686 r = fetch_token(tok, src, end, env);
6687 if (r < 0) return r;
6688 r = parse_branch(&node, tok, term, src, end, env);
6689 if (r < 0) {
6690 onig_node_free(node);
6691 return r;
6692 }
6693
6694 *headp = onig_node_new_alt(node, NULL);
6695 headp = &(NCDR(*headp));
6696 }
6697
6698 if (tok->type != (enum TokenSyms )term)
6699 goto err;
6700 }
6701 else {
6702 onig_node_free(node);
6703 err:
6704 if (term == TK_SUBEXP_CLOSE)
6705 return ONIGERR_END_PATTERN_WITH_UNMATCHED_PARENTHESIS;
6706 else
6707 return ONIGERR_PARSER_BUG;
6708 }
6709
6710 env->parse_depth--;
6711 return r;
6712}
6713
6714static int
6715parse_regexp(Node** top, UChar** src, UChar* end, ScanEnv* env)
6716{
6717 int r;
6718 OnigToken tok;
6719
6720 r = fetch_token(&tok, src, end, env);
6721 if (r < 0) return r;
6722 r = parse_subexp(top, &tok, TK_EOT, src, end, env);
6723 if (r < 0) return r;
6724
6725#ifdef USE_SUBEXP_CALL
6726 if (env->num_call > 0) {
6727 /* Capture the pattern itself. It is used for (?R), (?0) and \g<0>. */
6728 const int num = 0;
6729 Node* np;
6730 np = node_new_enclose_memory(env->option, 0);
6731 CHECK_NULL_RETURN_MEMERR(np);
6732 NENCLOSE(np)->regnum = num;
6733 NENCLOSE(np)->target = *top;
6734 r = scan_env_set_mem_node(env, num, np);
6735 if (r != 0) {
6736 onig_node_free(np);
6737 return r;
6738 }
6739 *top = np;
6740 }
6741#endif
6742 return 0;
6743}
6744
6745extern int
6746onig_parse_make_tree(Node** root, const UChar* pattern, const UChar* end,
6747 regex_t* reg, ScanEnv* env)
6748{
6749 int r;
6750 UChar* p;
6751
6752#ifdef USE_NAMED_GROUP
6753 names_clear(reg);
6754#endif
6755
6756 scan_env_clear(env);
6757 env->option = reg->options;
6758 env->case_fold_flag = reg->case_fold_flag;
6759 env->enc = reg->enc;
6760 env->syntax = reg->syntax;
6761 env->pattern = (UChar* )pattern;
6762 env->pattern_end = (UChar* )end;
6763 env->reg = reg;
6764
6765 *root = NULL;
6766 p = (UChar* )pattern;
6767 r = parse_regexp(root, &p, (UChar* )end, env);
6768 reg->num_mem = env->num_mem;
6769 return r;
6770}
6771
6772extern void
6773onig_scan_env_set_error_string(ScanEnv* env, int ecode ARG_UNUSED,
6774 UChar* arg, UChar* arg_end)
6775{
6776 env->error = arg;
6777 env->error_end = arg_end;
6778}
xfree
#define xfree
Old name of ruby_xfree.
Definition xmalloc.h:58
xrealloc
#define xrealloc
Old name of ruby_xrealloc.
Definition xmalloc.h:56
xmalloc
#define xmalloc
Old name of ruby_xmalloc.
Definition xmalloc.h:53
ruby_verbose
#define ruby_verbose
This variable controls whether the interpreter is in debug mode.
Definition error.h:471
rb_warn
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
Definition error.c:423
capa
int capa
Designed capacity of the buffer.
Definition io.h:11
len
int len
Length of the buffer.
Definition io.h:8
ruby::backward::cxxanyargs::type
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition cxxanyargs.hpp:56
RTEST
#define RTEST
This is an old name of RB_TEST.
Definition special_consts.h:51
stdarg.h
Defines old _.
__sFILE
Definition vsnprintf.c:160
_BBuf
Definition regint.h:466
_Node
Definition regparse.h:265
CClassNode
Definition regint.h:829
GroupNumRemap
Definition regparse.h:335
IApplyCaseFoldArg
Definition regparse.c:5631
INamesArg
Definition regparse.c:619
NameEntry
Definition regparse.c:463
OnigEncodingTypeST
Definition onigmo.h:160
OnigSyntaxType
Definition onigmo.h:479
OnigToken
Definition regparse.c:2416
PosixBracketEntryType
Definition regenc.h:119
QtfrNode
Definition regparse.h:179
ScanEnv
Definition regparse.h:290
StrNode
Definition regparse.h:170
re_pattern_buffer
Definition onigmo.h:755
re_registers
Definition onigmo.h:716
st_hash_type
Definition st.h:61
st_str_end_key
Definition regparse.c:369
st_table
Definition st.h:79