Ruby: regparse.c Source File

Ruby 3.2.5p208 (2024-07-26 revision 31d0f1a2e7dbfb60731d1f05b868e1d578cda493)

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