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