1 /**********************************************************************
2 regparse.c - Onigmo (Oniguruma-mod) (regular expression library)
3 **********************************************************************/
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>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
34 #define WARN_BUFSIZE 256
36 #define CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS
39 const OnigSyntaxType OnigSyntaxRuby
= {
40 (( SYN_GNU_REGEX_OP
| ONIG_SYN_OP_QMARK_NON_GREEDY
|
41 ONIG_SYN_OP_ESC_OCTAL3
| ONIG_SYN_OP_ESC_X_HEX2
|
42 ONIG_SYN_OP_ESC_X_BRACE_HEX8
| ONIG_SYN_OP_ESC_CONTROL_CHARS
|
43 ONIG_SYN_OP_ESC_C_CONTROL
)
44 & ~ONIG_SYN_OP_ESC_LTGT_WORD_BEGIN_END
)
45 , ( ONIG_SYN_OP2_QMARK_GROUP_EFFECT
|
46 ONIG_SYN_OP2_OPTION_RUBY
|
47 ONIG_SYN_OP2_QMARK_LT_NAMED_GROUP
| ONIG_SYN_OP2_ESC_K_NAMED_BACKREF
|
48 ONIG_SYN_OP2_ESC_G_SUBEXP_CALL
|
49 ONIG_SYN_OP2_ESC_P_BRACE_CHAR_PROPERTY
|
50 ONIG_SYN_OP2_ESC_P_BRACE_CIRCUMFLEX_NOT
|
51 ONIG_SYN_OP2_PLUS_POSSESSIVE_REPEAT
|
52 ONIG_SYN_OP2_CCLASS_SET_OP
| ONIG_SYN_OP2_ESC_CAPITAL_C_BAR_CONTROL
|
53 ONIG_SYN_OP2_ESC_CAPITAL_M_BAR_META
| ONIG_SYN_OP2_ESC_V_VTAB
|
54 ONIG_SYN_OP2_ESC_H_XDIGIT
|
56 ONIG_SYN_OP2_ESC_U_HEX4
|
58 ONIG_SYN_OP2_ESC_CAPITAL_X_EXTENDED_GRAPHEME_CLUSTER
|
59 ONIG_SYN_OP2_QMARK_LPAREN_CONDITION
|
60 ONIG_SYN_OP2_ESC_CAPITAL_R_LINEBREAK
|
61 ONIG_SYN_OP2_ESC_CAPITAL_K_KEEP
|
62 ONIG_SYN_OP2_QMARK_TILDE_ABSENT
)
63 , ( SYN_GNU_REGEX_BV
|
64 ONIG_SYN_ALLOW_INTERVAL_LOW_ABBREV
|
65 ONIG_SYN_DIFFERENT_LEN_ALT_LOOK_BEHIND
|
66 ONIG_SYN_CAPTURE_ONLY_NAMED_GROUP
|
67 ONIG_SYN_ALLOW_MULTIPLEX_DEFINITION_NAME
|
68 ONIG_SYN_FIXED_INTERVAL_IS_GREEDY_ONLY
|
69 ONIG_SYN_WARN_CC_OP_NOT_ESCAPED
|
70 ONIG_SYN_WARN_CC_DUP
|
71 ONIG_SYN_WARN_REDUNDANT_NESTED_REPEAT
)
72 , ( ONIG_OPTION_ASCII_RANGE
| ONIG_OPTION_POSIX_BRACKET_ALL_RANGE
|
73 ONIG_OPTION_WORD_BOUND_ALL_RANGE
)
76 (OnigCodePoint
)'\\' /* esc */
77 , (OnigCodePoint
)ONIG_INEFFECTIVE_META_CHAR
/* anychar '.' */
78 , (OnigCodePoint
)ONIG_INEFFECTIVE_META_CHAR
/* anytime '*' */
79 , (OnigCodePoint
)ONIG_INEFFECTIVE_META_CHAR
/* zero or one time '?' */
80 , (OnigCodePoint
)ONIG_INEFFECTIVE_META_CHAR
/* one or more time '+' */
81 , (OnigCodePoint
)ONIG_INEFFECTIVE_META_CHAR
/* anychar anytime */
85 const OnigSyntaxType
* OnigDefaultSyntax
= ONIG_SYNTAX_RUBY
;
87 extern void onig_null_warn(const char* s ARG_UNUSED
) { }
89 #ifdef DEFAULT_WARN_FUNCTION
90 static OnigWarnFunc onig_warn
= (OnigWarnFunc
)DEFAULT_WARN_FUNCTION
;
92 static OnigWarnFunc onig_warn
= onig_null_warn
;
95 #ifdef DEFAULT_VERB_WARN_FUNCTION
96 static OnigWarnFunc onig_verb_warn
= (OnigWarnFunc
)DEFAULT_VERB_WARN_FUNCTION
;
98 static OnigWarnFunc onig_verb_warn
= onig_null_warn
;
101 extern void onig_set_warn_func(OnigWarnFunc f
)
106 extern void onig_set_verb_warn_func(OnigWarnFunc f
)
111 static void CC_DUP_WARN(ScanEnv
*env
, OnigCodePoint from
, OnigCodePoint to
);
114 static unsigned int ParseDepthLimit
= DEFAULT_PARSE_DEPTH_LIMIT
;
117 onig_get_parse_depth_limit(void)
119 return ParseDepthLimit
;
123 onig_set_parse_depth_limit(unsigned int depth
)
126 ParseDepthLimit
= DEFAULT_PARSE_DEPTH_LIMIT
;
128 ParseDepthLimit
= depth
;
134 bbuf_free(BBuf
* bbuf
)
136 if (IS_NOT_NULL(bbuf
)) {
137 if (IS_NOT_NULL(bbuf
->p
)) xfree(bbuf
->p
);
143 bbuf_clone(BBuf
** rto
, BBuf
* from
)
148 *rto
= to
= (BBuf
* )xmalloc(sizeof(BBuf
));
149 CHECK_NULL_RETURN_MEMERR(to
);
150 r
= BBUF_INIT(to
, from
->alloc
);
151 if (r
!= 0) return r
;
152 to
->used
= from
->used
;
153 xmemcpy(to
->p
, from
->p
, from
->used
);
157 #define BACKREF_REL_TO_ABS(rel_no, env) \
158 ((env)->num_mem + 1 + (rel_no))
160 #define ONOFF(v,f,negative) (negative) ? ((v) &= ~(f)) : ((v) |= (f))
162 #define MBCODE_START_POS(enc) \
163 (OnigCodePoint )(ONIGENC_MBC_MINLEN(enc) > 1 ? 0 : 0x80)
165 #define SET_ALL_MULTI_BYTE_RANGE(enc, pbuf) \
166 add_code_range_to_buf(pbuf, env, MBCODE_START_POS(enc), ONIG_LAST_CODE_POINT)
168 #define ADD_ALL_MULTI_BYTE_RANGE(enc, mbuf) do {\
169 if (! ONIGENC_IS_SINGLEBYTE(enc)) {\
170 r = SET_ALL_MULTI_BYTE_RANGE(enc, &(mbuf));\
176 #define BITSET_SET_BIT_CHKDUP(bs, pos) do { \
177 if (BITSET_AT(bs, pos)) CC_DUP_WARN(env, pos, pos); \
178 BS_ROOM(bs, pos) |= BS_BIT(pos); \
181 #define BITSET_IS_EMPTY(bs,empty) do {\
184 for (i = 0; i < BITSET_SIZE; i++) {\
192 bitset_set_range(ScanEnv
*env
, BitSetRef bs
, int from
, int to
)
195 for (i
= from
; i
<= to
&& i
< SINGLE_BYTE_SIZE
; i
++) {
196 BITSET_SET_BIT_CHKDUP(bs
, i
);
202 bitset_set_all(BitSetRef bs
)
205 for (i
= 0; i
< BITSET_SIZE
; i
++) { bs
[i
] = ~((Bits
)0); }
210 bitset_invert(BitSetRef bs
)
213 for (i
= 0; i
< BITSET_SIZE
; i
++) { bs
[i
] = ~(bs
[i
]); }
217 bitset_invert_to(BitSetRef from
, BitSetRef to
)
220 for (i
= 0; i
< BITSET_SIZE
; i
++) { to
[i
] = ~(from
[i
]); }
224 bitset_and(BitSetRef dest
, BitSetRef bs
)
227 for (i
= 0; i
< BITSET_SIZE
; i
++) { dest
[i
] &= bs
[i
]; }
231 bitset_or(BitSetRef dest
, BitSetRef bs
)
234 for (i
= 0; i
< BITSET_SIZE
; i
++) { dest
[i
] |= bs
[i
]; }
238 bitset_copy(BitSetRef dest
, BitSetRef bs
)
241 for (i
= 0; i
< BITSET_SIZE
; i
++) { dest
[i
] = bs
[i
]; }
244 #if defined(USE_NAMED_GROUP) && !defined(USE_ST_LIBRARY)
246 onig_strncmp(const UChar
* s1
, const UChar
* s2
, int n
)
259 onig_strcpy(UChar
* dest
, const UChar
* src
, const UChar
* end
)
261 ptrdiff_t len
= end
- src
;
263 xmemcpy(dest
, src
, len
);
264 dest
[len
] = (UChar
)0;
268 #ifdef USE_NAMED_GROUP
270 strdup_with_null(OnigEncoding enc
, UChar
* s
, UChar
* end
)
277 term_len
= ONIGENC_MBC_MINLEN(enc
);
279 r
= (UChar
* )xmalloc(slen
+ term_len
);
280 CHECK_NULL_RETURN(r
);
283 for (i
= 0; i
< term_len
; i
++)
284 r
[slen
+ i
] = (UChar
)0;
290 /* scan pattern methods */
294 /* get rid of Wunused-but-set-variable and Wuninitialized */
295 # define PFETCH_READY UChar* pfetch_prev = NULL; (void)pfetch_prev
297 # define PFETCH_READY UChar* pfetch_prev
299 #define PEND (p < end ? 0 : 1)
300 #define PUNFETCH p = pfetch_prev
303 p += enclen(enc, p, end); \
305 #define PFETCH(c) do { \
306 c = ((enc->max_enc_len == 1) ? *p : ONIGENC_MBC_TO_CODE(enc, p, end)); \
308 p += enclen(enc, p, end); \
311 #define PINC_S do { \
312 p += enclen(enc, p, end); \
314 #define PFETCH_S(c) do { \
315 c = ((enc->max_enc_len == 1) ? *p : ONIGENC_MBC_TO_CODE(enc, p, end)); \
316 p += enclen(enc, p, end); \
319 #define PPEEK (p < end ? ONIGENC_MBC_TO_CODE(enc, p, end) : PEND_VALUE)
320 #define PPEEK_IS(c) (PPEEK == (OnigCodePoint )c)
323 strcat_capa(UChar
* dest
, UChar
* dest_end
, const UChar
* src
, const UChar
* src_end
,
329 r
= (UChar
* )xrealloc(dest
, capa
+ 1);
331 r
= (UChar
* )xmalloc(capa
+ 1);
333 CHECK_NULL_RETURN(r
);
334 onig_strcpy(r
+ (dest_end
- dest
), src
, src_end
);
338 /* dest on static area */
340 strcat_capa_from_static(UChar
* dest
, UChar
* dest_end
,
341 const UChar
* src
, const UChar
* src_end
, size_t capa
)
345 r
= (UChar
* )xmalloc(capa
+ 1);
346 CHECK_NULL_RETURN(r
);
347 onig_strcpy(r
, dest
, dest_end
);
348 onig_strcpy(r
+ (dest_end
- dest
), src
, src_end
);
353 #ifdef USE_ST_LIBRARY
356 # include "ruby/st.h"
367 str_end_cmp(st_data_t xp
, st_data_t yp
)
369 const st_str_end_key
*x
, *y
;
373 x
= (const st_str_end_key
*)xp
;
374 y
= (const st_str_end_key
*)yp
;
375 if ((x
->end
- x
->s
) != (y
->end
- y
->s
))
381 c
= (int )*p
- (int )*q
;
382 if (c
!= 0) return c
;
391 str_end_hash(st_data_t xp
)
393 const st_str_end_key
*x
= (const st_str_end_key
*)xp
;
399 val
= val
* 997 + (int )*p
++;
402 return val
+ (val
>> 5);
405 extern hash_table_type
*
406 onig_st_init_strend_table_with_size(st_index_t size
)
408 static const struct st_hash_type hashType
= {
413 return (hash_table_type
* )
414 onig_st_init_table_with_size(&hashType
, size
);
418 onig_st_lookup_strend(hash_table_type
* table
, const UChar
* str_key
,
419 const UChar
* end_key
, hash_data_type
*value
)
423 key
.s
= (UChar
* )str_key
;
424 key
.end
= (UChar
* )end_key
;
426 return onig_st_lookup(table
, (st_data_t
)(&key
), value
);
430 onig_st_insert_strend(hash_table_type
* table
, const UChar
* str_key
,
431 const UChar
* end_key
, hash_data_type value
)
436 key
= (st_str_end_key
* )xmalloc(sizeof(st_str_end_key
));
437 key
->s
= (UChar
* )str_key
;
438 key
->end
= (UChar
* )end_key
;
439 result
= onig_st_insert(table
, (st_data_t
)key
, value
);
446 #endif /* USE_ST_LIBRARY */
449 #ifdef USE_NAMED_GROUP
451 # define INIT_NAME_BACKREFS_ALLOC_NUM 8
455 size_t name_len
; /* byte length */
456 int back_num
; /* number of backrefs */
462 # ifdef USE_ST_LIBRARY
464 typedef st_table NameTable
;
465 typedef st_data_t HashDataType
; /* 1.6 st.h doesn't define st_data_t type */
469 i_print_name_entry(UChar
* key
, NameEntry
* e
, void* arg
)
472 FILE* fp
= (FILE* )arg
;
474 fprintf(fp
, "%s: ", e
->name
);
475 if (e
->back_num
== 0)
477 else if (e
->back_num
== 1)
478 fprintf(fp
, "%d", e
->back_ref1
);
480 for (i
= 0; i
< e
->back_num
; i
++) {
481 if (i
> 0) fprintf(fp
, ", ");
482 fprintf(fp
, "%d", e
->back_refs
[i
]);
490 onig_print_names(FILE* fp
, regex_t
* reg
)
492 NameTable
* t
= (NameTable
* )reg
->name_table
;
494 if (IS_NOT_NULL(t
)) {
495 fprintf(fp
, "name table\n");
496 onig_st_foreach(t
, (st_foreach_callback_func
*)i_print_name_entry
, (HashDataType
)fp
);
501 # endif /* ONIG_DEBUG */
504 i_free_name_entry(UChar
* key
, NameEntry
* e
, void* arg ARG_UNUSED
)
507 if (IS_NOT_NULL(e
->back_refs
)) xfree(e
->back_refs
);
514 names_clear(regex_t
* reg
)
516 NameTable
* t
= (NameTable
* )reg
->name_table
;
518 if (IS_NOT_NULL(t
)) {
519 onig_st_foreach(t
, (st_foreach_callback_func
*)i_free_name_entry
, 0);
525 onig_names_free(regex_t
* reg
)
530 r
= names_clear(reg
);
533 t
= (NameTable
* )reg
->name_table
;
534 if (IS_NOT_NULL(t
)) onig_st_free_table(t
);
535 reg
->name_table
= (void* )NULL
;
540 name_find(regex_t
* reg
, const UChar
* name
, const UChar
* name_end
)
543 NameTable
* t
= (NameTable
* )reg
->name_table
;
545 e
= (NameEntry
* )NULL
;
546 if (IS_NOT_NULL(t
)) {
547 onig_st_lookup_strend(t
, name
, name_end
, (HashDataType
* )((void* )(&e
)));
553 int (*func
)(const UChar
*, const UChar
*,int,int*,regex_t
*,void*);
561 i_names(UChar
* key ARG_UNUSED
, NameEntry
* e
, INamesArg
* arg
)
563 int r
= (*(arg
->func
))(e
->name
,
564 e
->name
+ e
->name_len
,
566 (e
->back_num
> 1 ? e
->back_refs
: &(e
->back_ref1
)),
576 onig_foreach_name(regex_t
* reg
,
577 int (*func
)(const UChar
*, const UChar
*,int,int*,regex_t
*,void*), void* arg
)
580 NameTable
* t
= (NameTable
* )reg
->name_table
;
583 if (IS_NOT_NULL(t
)) {
587 narg
.enc
= reg
->enc
; /* should be pattern encoding. */
588 onig_st_foreach(t
, (st_foreach_callback_func
*)i_names
, (HashDataType
)&narg
);
594 i_renumber_name(UChar
* key ARG_UNUSED
, NameEntry
* e
, GroupNumRemap
* map
)
598 if (e
->back_num
> 1) {
599 for (i
= 0; i
< e
->back_num
; i
++) {
600 e
->back_refs
[i
] = map
[e
->back_refs
[i
]].new_val
;
603 else if (e
->back_num
== 1) {
604 e
->back_ref1
= map
[e
->back_ref1
].new_val
;
611 onig_renumber_name_table(regex_t
* reg
, GroupNumRemap
* map
)
613 NameTable
* t
= (NameTable
* )reg
->name_table
;
615 if (IS_NOT_NULL(t
)) {
616 onig_st_foreach(t
, (st_foreach_callback_func
*)i_renumber_name
, (HashDataType
)map
);
623 onig_number_of_names(const regex_t
* reg
)
625 NameTable
* t
= (NameTable
* )reg
->name_table
;
628 return (int )t
->num_entries
;
633 # else /* USE_ST_LIBRARY */
635 # define INIT_NAMES_ALLOC_NUM 8
645 onig_print_names(FILE* fp
, regex_t
* reg
)
649 NameTable
* t
= (NameTable
* )reg
->name_table
;
651 if (IS_NOT_NULL(t
) && t
->num
> 0) {
652 fprintf(fp
, "name table\n");
653 for (i
= 0; i
< t
->num
; i
++) {
655 fprintf(fp
, "%s: ", e
->name
);
656 if (e
->back_num
== 0) {
659 else if (e
->back_num
== 1) {
660 fprintf(fp
, "%d", e
->back_ref1
);
663 for (j
= 0; j
< e
->back_num
; j
++) {
664 if (j
> 0) fprintf(fp
, ", ");
665 fprintf(fp
, "%d", e
->back_refs
[j
]);
677 names_clear(regex_t
* reg
)
681 NameTable
* t
= (NameTable
* )reg
->name_table
;
683 if (IS_NOT_NULL(t
)) {
684 for (i
= 0; i
< t
->num
; i
++) {
686 if (IS_NOT_NULL(e
->name
)) {
692 if (IS_NOT_NULL(e
->back_refs
)) xfree(e
->back_refs
);
693 e
->back_refs
= (int* )NULL
;
696 if (IS_NOT_NULL(t
->e
)) {
706 onig_names_free(regex_t
* reg
)
711 r
= names_clear(reg
);
714 t
= (NameTable
* )reg
->name_table
;
715 if (IS_NOT_NULL(t
)) xfree(t
);
716 reg
->name_table
= NULL
;
721 name_find(regex_t
* reg
, const UChar
* name
, const UChar
* name_end
)
725 NameTable
* t
= (NameTable
* )reg
->name_table
;
727 if (IS_NOT_NULL(t
)) {
728 len
= name_end
- name
;
729 for (i
= 0; i
< t
->num
; i
++) {
731 if (len
== e
->name_len
&& onig_strncmp(name
, e
->name
, len
) == 0)
735 return (NameEntry
* )NULL
;
739 onig_foreach_name(regex_t
* reg
,
740 int (*func
)(const UChar
*, const UChar
*,int,int*,regex_t
*,void*), void* arg
)
744 NameTable
* t
= (NameTable
* )reg
->name_table
;
746 if (IS_NOT_NULL(t
)) {
747 for (i
= 0; i
< t
->num
; i
++) {
749 r
= (*func
)(e
->name
, e
->name
+ e
->name_len
, e
->back_num
,
750 (e
->back_num
> 1 ? e
->back_refs
: &(e
->back_ref1
)),
752 if (r
!= 0) return r
;
759 onig_number_of_names(const regex_t
* reg
)
761 NameTable
* t
= (NameTable
* )reg
->name_table
;
769 # endif /* else USE_ST_LIBRARY */
772 name_add(regex_t
* reg
, UChar
* name
, UChar
* name_end
, int backref
, ScanEnv
* env
)
776 NameTable
* t
= (NameTable
* )reg
->name_table
;
778 if (name_end
- name
<= 0)
779 return ONIGERR_EMPTY_GROUP_NAME
;
781 e
= name_find(reg
, name
, name_end
);
783 # ifdef USE_ST_LIBRARY
785 t
= onig_st_init_strend_table_with_size(5);
786 reg
->name_table
= (void* )t
;
788 e
= (NameEntry
* )xmalloc(sizeof(NameEntry
));
789 CHECK_NULL_RETURN_MEMERR(e
);
791 e
->name
= strdup_with_null(reg
->enc
, name
, name_end
);
792 if (IS_NULL(e
->name
)) {
794 return ONIGERR_MEMORY
;
796 onig_st_insert_strend(t
, e
->name
, (e
->name
+ (name_end
- name
)),
799 e
->name_len
= name_end
- name
;
802 e
->back_refs
= (int* )NULL
;
807 alloc
= INIT_NAMES_ALLOC_NUM
;
808 t
= (NameTable
* )xmalloc(sizeof(NameTable
));
809 CHECK_NULL_RETURN_MEMERR(t
);
814 t
->e
= (NameEntry
* )xmalloc(sizeof(NameEntry
) * alloc
);
817 return ONIGERR_MEMORY
;
823 else if (t
->num
== t
->alloc
) {
827 alloc
= t
->alloc
* 2;
828 p
= (NameEntry
* )xrealloc(t
->e
, sizeof(NameEntry
) * alloc
);
829 CHECK_NULL_RETURN_MEMERR(p
);
834 for (i
= t
->num
; i
< t
->alloc
; i
++) {
836 t
->e
[i
].name_len
= 0;
837 t
->e
[i
].back_num
= 0;
838 t
->e
[i
].back_alloc
= 0;
839 t
->e
[i
].back_refs
= (int* )NULL
;
844 e
->name
= strdup_with_null(reg
->enc
, name
, name_end
);
845 if (IS_NULL(e
->name
)) return ONIGERR_MEMORY
;
846 e
->name_len
= name_end
- name
;
850 if (e
->back_num
>= 1 &&
851 ! IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_ALLOW_MULTIPLEX_DEFINITION_NAME
)) {
852 onig_scan_env_set_error_string(env
, ONIGERR_MULTIPLEX_DEFINED_NAME
,
854 return ONIGERR_MULTIPLEX_DEFINED_NAME
;
858 if (e
->back_num
== 1) {
859 e
->back_ref1
= backref
;
862 if (e
->back_num
== 2) {
863 alloc
= INIT_NAME_BACKREFS_ALLOC_NUM
;
864 e
->back_refs
= (int* )xmalloc(sizeof(int) * alloc
);
865 CHECK_NULL_RETURN_MEMERR(e
->back_refs
);
866 e
->back_alloc
= alloc
;
867 e
->back_refs
[0] = e
->back_ref1
;
868 e
->back_refs
[1] = backref
;
871 if (e
->back_num
> e
->back_alloc
) {
873 alloc
= e
->back_alloc
* 2;
874 p
= (int* )xrealloc(e
->back_refs
, sizeof(int) * alloc
);
875 CHECK_NULL_RETURN_MEMERR(p
);
877 e
->back_alloc
= alloc
;
879 e
->back_refs
[e
->back_num
- 1] = backref
;
887 onig_name_to_group_numbers(regex_t
* reg
, const UChar
* name
,
888 const UChar
* name_end
, int** nums
)
890 NameEntry
* e
= name_find(reg
, name
, name_end
);
892 if (IS_NULL(e
)) return ONIGERR_UNDEFINED_NAME_REFERENCE
;
894 switch (e
->back_num
) {
899 *nums
= &(e
->back_ref1
);
902 *nums
= e
->back_refs
;
909 onig_name_to_backref_number(regex_t
* reg
, const UChar
* name
,
910 const UChar
* name_end
, const OnigRegion
*region
)
914 n
= onig_name_to_group_numbers(reg
, name
, name_end
, &nums
);
918 return ONIGERR_PARSER_BUG
;
922 if (IS_NOT_NULL(region
)) {
923 for (i
= n
- 1; i
>= 0; i
--) {
924 if (region
->beg
[nums
[i
]] != ONIG_REGION_NOTPOS
)
932 #else /* USE_NAMED_GROUP */
935 onig_name_to_group_numbers(regex_t
* reg
, const UChar
* name
,
936 const UChar
* name_end
, int** nums
)
938 return ONIG_NO_SUPPORT_CONFIG
;
942 onig_name_to_backref_number(regex_t
* reg
, const UChar
* name
,
943 const UChar
* name_end
, const OnigRegion
* region
)
945 return ONIG_NO_SUPPORT_CONFIG
;
949 onig_foreach_name(regex_t
* reg
,
950 int (*func
)(const UChar
*, const UChar
*,int,int*,regex_t
*,void*), void* arg
)
952 return ONIG_NO_SUPPORT_CONFIG
;
956 onig_number_of_names(const regex_t
* reg
)
960 #endif /* else USE_NAMED_GROUP */
963 onig_noname_group_capture_is_active(const regex_t
* reg
)
965 if (ONIG_IS_OPTION_ON(reg
->options
, ONIG_OPTION_DONT_CAPTURE_GROUP
))
968 #ifdef USE_NAMED_GROUP
969 if (onig_number_of_names(reg
) > 0 &&
970 IS_SYNTAX_BV(reg
->syntax
, ONIG_SYN_CAPTURE_ONLY_NAMED_GROUP
) &&
971 !ONIG_IS_OPTION_ON(reg
->options
, ONIG_OPTION_CAPTURE_GROUP
)) {
980 #define INIT_SCANENV_MEMNODES_ALLOC_SIZE 16
983 scan_env_clear(ScanEnv
* env
)
987 BIT_STATUS_CLEAR(env
->capture_history
);
988 BIT_STATUS_CLEAR(env
->bt_mem_start
);
989 BIT_STATUS_CLEAR(env
->bt_mem_end
);
990 BIT_STATUS_CLEAR(env
->backrefed_mem
);
991 env
->error
= (UChar
* )NULL
;
992 env
->error_end
= (UChar
* )NULL
;
995 #ifdef USE_NAMED_GROUP
999 env
->mem_nodes_dynamic
= (Node
** )NULL
;
1001 for (i
= 0; i
< SCANENV_MEMNODES_SIZE
; i
++)
1002 env
->mem_nodes_static
[i
] = NULL_NODE
;
1004 #ifdef USE_COMBINATION_EXPLOSION_CHECK
1005 env
->num_comb_exp_check
= 0;
1006 env
->comb_exp_max_regnum
= 0;
1007 env
->curr_max_regnum
= 0;
1008 env
->has_recursion
= 0;
1010 env
->parse_depth
= 0;
1011 env
->warnings_flag
= 0;
1015 scan_env_add_mem_entry(ScanEnv
* env
)
1020 need
= env
->num_mem
+ 1;
1021 if (need
> ONIG_MAX_CAPTURE_GROUP_NUM
)
1022 return ONIGERR_TOO_MANY_CAPTURE_GROUPS
;
1023 if (need
>= SCANENV_MEMNODES_SIZE
) {
1024 if (env
->mem_alloc
<= need
) {
1025 if (IS_NULL(env
->mem_nodes_dynamic
)) {
1026 alloc
= INIT_SCANENV_MEMNODES_ALLOC_SIZE
;
1027 p
= (Node
** )xmalloc(sizeof(Node
*) * alloc
);
1028 CHECK_NULL_RETURN_MEMERR(p
);
1029 xmemcpy(p
, env
->mem_nodes_static
,
1030 sizeof(Node
*) * SCANENV_MEMNODES_SIZE
);
1033 alloc
= env
->mem_alloc
* 2;
1034 p
= (Node
** )xrealloc(env
->mem_nodes_dynamic
, sizeof(Node
*) * alloc
);
1035 CHECK_NULL_RETURN_MEMERR(p
);
1038 for (i
= env
->num_mem
+ 1; i
< alloc
; i
++)
1041 env
->mem_nodes_dynamic
= p
;
1042 env
->mem_alloc
= alloc
;
1047 return env
->num_mem
;
1051 scan_env_set_mem_node(ScanEnv
* env
, int num
, Node
* node
)
1053 if (env
->num_mem
>= num
)
1054 SCANENV_MEM_NODES(env
)[num
] = node
;
1056 return ONIGERR_PARSER_BUG
;
1062 onig_node_free(Node
* node
)
1065 if (IS_NULL(node
)) return ;
1067 switch (NTYPE(node
)) {
1069 if (NSTR(node
)->capa
!= 0 &&
1070 IS_NOT_NULL(NSTR(node
)->s
) && NSTR(node
)->s
!= NSTR(node
)->buf
) {
1071 xfree(NSTR(node
)->s
);
1077 onig_node_free(NCAR(node
));
1079 Node
* next_node
= NCDR(node
);
1089 CClassNode
* cc
= NCCLASS(node
);
1092 bbuf_free(cc
->mbuf
);
1097 if (NQTFR(node
)->target
)
1098 onig_node_free(NQTFR(node
)->target
);
1102 if (NENCLOSE(node
)->target
)
1103 onig_node_free(NENCLOSE(node
)->target
);
1107 if (IS_NOT_NULL(NBREF(node
)->back_dynamic
))
1108 xfree(NBREF(node
)->back_dynamic
);
1112 if (NANCHOR(node
)->target
)
1113 onig_node_free(NANCHOR(node
)->target
);
1125 node
= (Node
* )xmalloc(sizeof(Node
));
1126 /* xmemset(node, 0, sizeof(Node)); */
1131 initialize_cclass(CClassNode
* cc
)
1133 BITSET_CLEAR(cc
->bs
);
1134 /* cc->base.flags = 0; */
1140 node_new_cclass(void)
1142 Node
* node
= node_new();
1143 CHECK_NULL_RETURN(node
);
1145 SET_NTYPE(node
, NT_CCLASS
);
1146 initialize_cclass(NCCLASS(node
));
1151 node_new_ctype(int type
, int not, int ascii_range
)
1153 Node
* node
= node_new();
1154 CHECK_NULL_RETURN(node
);
1156 SET_NTYPE(node
, NT_CTYPE
);
1157 NCTYPE(node
)->ctype
= type
;
1158 NCTYPE(node
)->not = not;
1159 NCTYPE(node
)->ascii_range
= ascii_range
;
1164 node_new_anychar(void)
1166 Node
* node
= node_new();
1167 CHECK_NULL_RETURN(node
);
1169 SET_NTYPE(node
, NT_CANY
);
1174 node_new_list(Node
* left
, Node
* right
)
1176 Node
* node
= node_new();
1177 CHECK_NULL_RETURN(node
);
1179 SET_NTYPE(node
, NT_LIST
);
1186 onig_node_new_list(Node
* left
, Node
* right
)
1188 return node_new_list(left
, right
);
1192 onig_node_list_add(Node
* list
, Node
* x
)
1196 n
= onig_node_new_list(x
, NULL
);
1197 if (IS_NULL(n
)) return NULL_NODE
;
1199 if (IS_NOT_NULL(list
)) {
1200 while (IS_NOT_NULL(NCDR(list
)))
1210 onig_node_new_alt(Node
* left
, Node
* right
)
1212 Node
* node
= node_new();
1213 CHECK_NULL_RETURN(node
);
1215 SET_NTYPE(node
, NT_ALT
);
1222 onig_node_new_anchor(int type
)
1224 Node
* node
= node_new();
1225 CHECK_NULL_RETURN(node
);
1227 SET_NTYPE(node
, NT_ANCHOR
);
1228 NANCHOR(node
)->type
= type
;
1229 NANCHOR(node
)->target
= NULL
;
1230 NANCHOR(node
)->char_len
= -1;
1231 NANCHOR(node
)->ascii_range
= 0;
1236 node_new_backref(int back_num
, int* backrefs
, int by_name
,
1237 #ifdef USE_BACKREF_WITH_LEVEL
1238 int exist_level
, int nest_level
,
1243 Node
* node
= node_new();
1245 CHECK_NULL_RETURN(node
);
1247 SET_NTYPE(node
, NT_BREF
);
1248 NBREF(node
)->state
= 0;
1249 NBREF(node
)->back_num
= back_num
;
1250 NBREF(node
)->back_dynamic
= (int* )NULL
;
1252 NBREF(node
)->state
|= NST_NAME_REF
;
1254 #ifdef USE_BACKREF_WITH_LEVEL
1255 if (exist_level
!= 0) {
1256 NBREF(node
)->state
|= NST_NEST_LEVEL
;
1257 NBREF(node
)->nest_level
= nest_level
;
1261 for (i
= 0; i
< back_num
; i
++) {
1262 if (backrefs
[i
] <= env
->num_mem
&&
1263 IS_NULL(SCANENV_MEM_NODES(env
)[backrefs
[i
]])) {
1264 NBREF(node
)->state
|= NST_RECURSION
; /* /...(\1).../ */
1269 if (back_num
<= NODE_BACKREFS_SIZE
) {
1270 for (i
= 0; i
< back_num
; i
++)
1271 NBREF(node
)->back_static
[i
] = backrefs
[i
];
1274 int* p
= (int* )xmalloc(sizeof(int) * back_num
);
1276 onig_node_free(node
);
1279 NBREF(node
)->back_dynamic
= p
;
1280 for (i
= 0; i
< back_num
; i
++)
1286 #ifdef USE_SUBEXP_CALL
1288 node_new_call(UChar
* name
, UChar
* name_end
, int gnum
)
1290 Node
* node
= node_new();
1291 CHECK_NULL_RETURN(node
);
1293 SET_NTYPE(node
, NT_CALL
);
1294 NCALL(node
)->state
= 0;
1295 NCALL(node
)->target
= NULL_NODE
;
1296 NCALL(node
)->name
= name
;
1297 NCALL(node
)->name_end
= name_end
;
1298 NCALL(node
)->group_num
= gnum
; /* call by number if gnum != 0 */
1304 node_new_quantifier(int lower
, int upper
, int by_number
)
1306 Node
* node
= node_new();
1307 CHECK_NULL_RETURN(node
);
1309 SET_NTYPE(node
, NT_QTFR
);
1310 NQTFR(node
)->state
= 0;
1311 NQTFR(node
)->target
= NULL
;
1312 NQTFR(node
)->lower
= lower
;
1313 NQTFR(node
)->upper
= upper
;
1314 NQTFR(node
)->greedy
= 1;
1315 NQTFR(node
)->target_empty_info
= NQ_TARGET_ISNOT_EMPTY
;
1316 NQTFR(node
)->head_exact
= NULL_NODE
;
1317 NQTFR(node
)->next_head_exact
= NULL_NODE
;
1318 NQTFR(node
)->is_referred
= 0;
1320 NQTFR(node
)->state
|= NST_BY_NUMBER
;
1322 #ifdef USE_COMBINATION_EXPLOSION_CHECK
1323 NQTFR(node
)->comb_exp_check_num
= 0;
1330 node_new_enclose(int type
)
1332 Node
* node
= node_new();
1333 CHECK_NULL_RETURN(node
);
1335 SET_NTYPE(node
, NT_ENCLOSE
);
1336 NENCLOSE(node
)->type
= type
;
1337 NENCLOSE(node
)->state
= 0;
1338 NENCLOSE(node
)->regnum
= 0;
1339 NENCLOSE(node
)->option
= 0;
1340 NENCLOSE(node
)->target
= NULL
;
1341 NENCLOSE(node
)->call_addr
= -1;
1342 NENCLOSE(node
)->opt_count
= 0;
1347 onig_node_new_enclose(int type
)
1349 return node_new_enclose(type
);
1353 node_new_enclose_memory(OnigOptionType option
, int is_named
)
1355 Node
* node
= node_new_enclose(ENCLOSE_MEMORY
);
1356 CHECK_NULL_RETURN(node
);
1358 SET_ENCLOSE_STATUS(node
, NST_NAMED_GROUP
);
1360 #ifdef USE_SUBEXP_CALL
1361 NENCLOSE(node
)->option
= option
;
1367 node_new_option(OnigOptionType option
)
1369 Node
* node
= node_new_enclose(ENCLOSE_OPTION
);
1370 CHECK_NULL_RETURN(node
);
1371 NENCLOSE(node
)->option
= option
;
1376 onig_node_str_cat(Node
* node
, const UChar
* s
, const UChar
* end
)
1378 ptrdiff_t addlen
= end
- s
;
1381 ptrdiff_t len
= NSTR(node
)->end
- NSTR(node
)->s
;
1383 if (NSTR(node
)->capa
> 0 || (len
+ addlen
> NODE_STR_BUF_SIZE
- 1)) {
1385 ptrdiff_t capa
= len
+ addlen
+ NODE_STR_MARGIN
;
1387 if (capa
<= NSTR(node
)->capa
) {
1388 onig_strcpy(NSTR(node
)->s
+ len
, s
, end
);
1391 if (NSTR(node
)->s
== NSTR(node
)->buf
)
1392 p
= strcat_capa_from_static(NSTR(node
)->s
, NSTR(node
)->end
,
1395 p
= strcat_capa(NSTR(node
)->s
, NSTR(node
)->end
, s
, end
, capa
);
1397 CHECK_NULL_RETURN_MEMERR(p
);
1399 NSTR(node
)->capa
= (int )capa
;
1403 onig_strcpy(NSTR(node
)->s
+ len
, s
, end
);
1405 NSTR(node
)->end
= NSTR(node
)->s
+ len
+ addlen
;
1412 onig_node_str_set(Node
* node
, const UChar
* s
, const UChar
* end
)
1414 onig_node_str_clear(node
);
1415 return onig_node_str_cat(node
, s
, end
);
1419 node_str_cat_char(Node
* node
, UChar c
)
1424 return onig_node_str_cat(node
, s
, s
+ 1);
1428 node_str_cat_codepoint(Node
* node
, OnigEncoding enc
, OnigCodePoint c
)
1430 UChar buf
[ONIGENC_CODE_TO_MBC_MAXLEN
];
1431 int num
= ONIGENC_CODE_TO_MBC(enc
, c
, buf
);
1432 if (num
< 0) return num
;
1433 return onig_node_str_cat(node
, buf
, buf
+ num
);
1438 onig_node_conv_to_str_node(Node
* node
, int flag
)
1440 SET_NTYPE(node
, NT_STR
);
1441 NSTR(node
)->flag
= flag
;
1442 NSTR(node
)->capa
= 0;
1443 NSTR(node
)->s
= NSTR(node
)->buf
;
1444 NSTR(node
)->end
= NSTR(node
)->buf
;
1449 onig_node_str_clear(Node
* node
)
1451 if (NSTR(node
)->capa
!= 0 &&
1452 IS_NOT_NULL(NSTR(node
)->s
) && NSTR(node
)->s
!= NSTR(node
)->buf
) {
1453 xfree(NSTR(node
)->s
);
1456 NSTR(node
)->capa
= 0;
1457 NSTR(node
)->flag
= 0;
1458 NSTR(node
)->s
= NSTR(node
)->buf
;
1459 NSTR(node
)->end
= NSTR(node
)->buf
;
1463 node_new_str(const UChar
* s
, const UChar
* end
)
1465 Node
* node
= node_new();
1466 CHECK_NULL_RETURN(node
);
1468 SET_NTYPE(node
, NT_STR
);
1469 NSTR(node
)->capa
= 0;
1470 NSTR(node
)->flag
= 0;
1471 NSTR(node
)->s
= NSTR(node
)->buf
;
1472 NSTR(node
)->end
= NSTR(node
)->buf
;
1473 if (onig_node_str_cat(node
, s
, end
)) {
1474 onig_node_free(node
);
1481 onig_node_new_str(const UChar
* s
, const UChar
* end
)
1483 return node_new_str(s
, end
);
1487 node_new_str_raw(UChar
* s
, UChar
* end
)
1489 Node
* node
= node_new_str(s
, end
);
1490 if (IS_NOT_NULL(node
))
1491 NSTRING_SET_RAW(node
);
1496 node_new_empty(void)
1498 return node_new_str(NULL
, NULL
);
1502 node_new_str_raw_char(UChar c
)
1507 return node_new_str_raw(p
, p
+ 1);
1511 str_node_split_last_char(StrNode
* sn
, OnigEncoding enc
)
1514 Node
* n
= NULL_NODE
;
1516 if (sn
->end
> sn
->s
) {
1517 p
= onigenc_get_prev_char_head(enc
, sn
->s
, sn
->end
, sn
->end
);
1518 if (p
&& p
> sn
->s
) { /* can be split. */
1519 n
= node_new_str(p
, sn
->end
);
1520 if (IS_NOT_NULL(n
) && (sn
->flag
& NSTR_RAW
) != 0)
1522 sn
->end
= (UChar
* )p
;
1529 str_node_can_be_split(StrNode
* sn
, OnigEncoding enc
)
1531 if (sn
->end
> sn
->s
) {
1532 return ((enclen(enc
, sn
->s
, sn
->end
) < sn
->end
- sn
->s
) ? 1 : 0);
1537 #ifdef USE_PAD_TO_SHORT_BYTE_CHAR
1539 node_str_head_pad(StrNode
* sn
, int num
, UChar val
)
1541 UChar buf
[NODE_STR_BUF_SIZE
];
1544 len
= sn
->end
- sn
->s
;
1545 onig_strcpy(buf
, sn
->s
, sn
->end
);
1546 onig_strcpy(&(sn
->s
[num
]), buf
, buf
+ len
);
1549 for (i
= 0; i
< num
; i
++) {
1556 onig_scan_unsigned_number(UChar
** src
, const UChar
* end
, OnigEncoding enc
)
1558 unsigned int num
, val
;
1566 if (ONIGENC_IS_CODE_DIGIT(enc
, c
)) {
1567 val
= (unsigned int )DIGITVAL(c
);
1568 if ((INT_MAX_LIMIT
- val
) / 10UL < num
)
1569 return -1; /* overflow */
1571 num
= num
* 10 + val
;
1583 scan_unsigned_hexadecimal_number(UChar
** src
, UChar
* end
, int minlen
,
1584 int maxlen
, OnigEncoding enc
)
1587 unsigned int num
, val
;
1592 restlen
= maxlen
- minlen
;
1594 while (!PEND
&& maxlen
-- != 0) {
1596 if (ONIGENC_IS_CODE_XDIGIT(enc
, c
)) {
1597 val
= (unsigned int )XDIGITVAL(enc
,c
);
1598 if ((INT_MAX_LIMIT
- val
) / 16UL < num
)
1599 return -1; /* overflow */
1601 num
= (num
<< 4) + XDIGITVAL(enc
,c
);
1609 if (maxlen
> restlen
)
1610 return -2; /* not enough digits */
1616 scan_unsigned_octal_number(UChar
** src
, UChar
* end
, int maxlen
,
1620 unsigned int num
, val
;
1625 while (!PEND
&& maxlen
-- != 0) {
1627 if (ONIGENC_IS_CODE_DIGIT(enc
, c
) && c
< '8') {
1629 if ((INT_MAX_LIMIT
- val
) / 8UL < num
)
1630 return -1; /* overflow */
1632 num
= (num
<< 3) + val
;
1644 #define BBUF_WRITE_CODE_POINT(bbuf,pos,code) \
1645 BBUF_WRITE(bbuf, pos, &(code), SIZE_CODE_POINT)
1648 [n][from-1][to-1][from-2][to-2] ... [from-n][to-n]
1649 (all data size is OnigCodePoint)
1652 new_code_range(BBuf
** pbuf
)
1654 #define INIT_MULTI_BYTE_RANGE_SIZE (SIZE_CODE_POINT * 5)
1659 bbuf
= *pbuf
= (BBuf
* )xmalloc(sizeof(BBuf
));
1660 CHECK_NULL_RETURN_MEMERR(*pbuf
);
1661 r
= BBUF_INIT(*pbuf
, INIT_MULTI_BYTE_RANGE_SIZE
);
1665 BBUF_WRITE_CODE_POINT(bbuf
, 0, n
);
1670 add_code_range_to_buf0(BBuf
** pbuf
, ScanEnv
* env
, OnigCodePoint from
, OnigCodePoint to
,
1674 OnigCodePoint low
, high
, bound
, x
;
1675 OnigCodePoint n
, *data
;
1679 n
= from
; from
= to
; to
= n
;
1682 if (IS_NULL(*pbuf
)) {
1683 r
= new_code_range(pbuf
);
1690 GET_CODE_POINT(n
, bbuf
->p
);
1692 data
= (OnigCodePoint
* )(bbuf
->p
);
1695 bound
= (from
== 0) ? 0 : n
;
1696 for (low
= 0; low
< bound
; ) {
1697 x
= (low
+ bound
) >> 1;
1698 if (from
- 1 > data
[x
*2 + 1])
1704 high
= (to
== ONIG_LAST_CODE_POINT
) ? n
: low
;
1705 for (bound
= n
; high
< bound
; ) {
1706 x
= (high
+ bound
) >> 1;
1707 if (to
+ 1 >= data
[x
*2])
1712 /* data[(low-1)*2+1] << from <= data[low*2]
1713 * data[(high-1)*2+1] <= to << data[high*2]
1716 inc_n
= low
+ 1 - high
;
1717 if (n
+ inc_n
> ONIG_MAX_MULTI_BYTE_RANGES_NUM
)
1718 return ONIGERR_TOO_MANY_MULTI_BYTE_RANGES
;
1721 if (checkdup
&& from
<= data
[low
*2+1]
1722 && (data
[low
*2] <= from
|| data
[low
*2+1] <= to
))
1723 CC_DUP_WARN(env
, from
, to
);
1724 if (from
> data
[low
*2])
1726 if (to
< data
[(high
- 1)*2 + 1])
1727 to
= data
[(high
- 1)*2 + 1];
1731 int from_pos
= SIZE_CODE_POINT
* (1 + high
* 2);
1732 int to_pos
= SIZE_CODE_POINT
* (1 + (low
+ 1) * 2);
1736 int size
= (n
- high
) * 2 * SIZE_CODE_POINT
;
1737 BBUF_MOVE_RIGHT(bbuf
, from_pos
, to_pos
, size
);
1741 BBUF_MOVE_LEFT_REDUCE(bbuf
, from_pos
, to_pos
);
1745 pos
= SIZE_CODE_POINT
* (1 + low
* 2);
1746 BBUF_ENSURE_SIZE(bbuf
, pos
+ SIZE_CODE_POINT
* 2);
1747 BBUF_WRITE_CODE_POINT(bbuf
, pos
, from
);
1748 BBUF_WRITE_CODE_POINT(bbuf
, pos
+ SIZE_CODE_POINT
, to
);
1750 BBUF_WRITE_CODE_POINT(bbuf
, 0, n
);
1756 add_code_range_to_buf(BBuf
** pbuf
, ScanEnv
* env
, OnigCodePoint from
, OnigCodePoint to
)
1758 return add_code_range_to_buf0(pbuf
, env
, from
, to
, 1);
1762 add_code_range0(BBuf
** pbuf
, ScanEnv
* env
, OnigCodePoint from
, OnigCodePoint to
, int checkdup
)
1765 if (IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_ALLOW_EMPTY_RANGE_IN_CC
))
1768 return ONIGERR_EMPTY_RANGE_IN_CHAR_CLASS
;
1771 return add_code_range_to_buf0(pbuf
, env
, from
, to
, checkdup
);
1775 add_code_range(BBuf
** pbuf
, ScanEnv
* env
, OnigCodePoint from
, OnigCodePoint to
)
1777 return add_code_range0(pbuf
, env
, from
, to
, 1);
1781 not_code_range_buf(OnigEncoding enc
, BBuf
* bbuf
, BBuf
** pbuf
, ScanEnv
* env
)
1784 OnigCodePoint pre
, from
, *data
, to
= 0;
1786 *pbuf
= (BBuf
* )NULL
;
1787 if (IS_NULL(bbuf
)) {
1789 return SET_ALL_MULTI_BYTE_RANGE(enc
, pbuf
);
1792 data
= (OnigCodePoint
* )(bbuf
->p
);
1793 GET_CODE_POINT(n
, data
);
1795 if (n
<= 0) goto set_all
;
1798 pre
= MBCODE_START_POS(enc
);
1799 for (i
= 0; i
< n
; i
++) {
1802 if (pre
<= from
- 1) {
1803 r
= add_code_range_to_buf(pbuf
, env
, pre
, from
- 1);
1804 if (r
!= 0) return r
;
1806 if (to
== ONIG_LAST_CODE_POINT
) break;
1809 if (to
< ONIG_LAST_CODE_POINT
) {
1810 r
= add_code_range_to_buf(pbuf
, env
, to
+ 1, ONIG_LAST_CODE_POINT
);
1815 #define SWAP_BBUF_NOT(bbuf1, not1, bbuf2, not2) do {\
1818 tnot = not1; not1 = not2; not2 = tnot; \
1819 tbuf = bbuf1; bbuf1 = bbuf2; bbuf2 = tbuf; \
1823 or_code_range_buf(OnigEncoding enc
, BBuf
* bbuf1
, int not1
,
1824 BBuf
* bbuf2
, int not2
, BBuf
** pbuf
, ScanEnv
* env
)
1827 OnigCodePoint i
, n1
, *data1
;
1828 OnigCodePoint from
, to
;
1830 *pbuf
= (BBuf
* )NULL
;
1831 if (IS_NULL(bbuf1
) && IS_NULL(bbuf2
)) {
1832 if (not1
!= 0 || not2
!= 0)
1833 return SET_ALL_MULTI_BYTE_RANGE(enc
, pbuf
);
1839 SWAP_BBUF_NOT(bbuf1
, not1
, bbuf2
, not2
);
1841 if (IS_NULL(bbuf1
)) {
1843 return SET_ALL_MULTI_BYTE_RANGE(enc
, pbuf
);
1847 return bbuf_clone(pbuf
, bbuf2
);
1850 return not_code_range_buf(enc
, bbuf2
, pbuf
, env
);
1856 SWAP_BBUF_NOT(bbuf1
, not1
, bbuf2
, not2
);
1858 data1
= (OnigCodePoint
* )(bbuf1
->p
);
1859 GET_CODE_POINT(n1
, data1
);
1862 if (not2
== 0 && not1
== 0) { /* 1 OR 2 */
1863 r
= bbuf_clone(pbuf
, bbuf2
);
1865 else if (not1
== 0) { /* 1 OR (not 2) */
1866 r
= not_code_range_buf(enc
, bbuf2
, pbuf
, env
);
1868 if (r
!= 0) return r
;
1870 for (i
= 0; i
< n1
; i
++) {
1873 r
= add_code_range_to_buf(pbuf
, env
, from
, to
);
1874 if (r
!= 0) return r
;
1880 and_code_range1(BBuf
** pbuf
, ScanEnv
* env
, OnigCodePoint from1
, OnigCodePoint to1
,
1881 OnigCodePoint
* data
, int n
)
1884 OnigCodePoint from2
, to2
;
1886 for (i
= 0; i
< n
; i
++) {
1889 if (from2
< from1
) {
1890 if (to2
< from1
) continue;
1895 else if (from2
<= to1
) {
1897 if (from1
<= from2
- 1) {
1898 r
= add_code_range_to_buf(pbuf
, env
, from1
, from2
-1);
1899 if (r
!= 0) return r
;
1910 if (from1
> to1
) break;
1913 r
= add_code_range_to_buf(pbuf
, env
, from1
, to1
);
1914 if (r
!= 0) return r
;
1920 and_code_range_buf(BBuf
* bbuf1
, int not1
, BBuf
* bbuf2
, int not2
, BBuf
** pbuf
, ScanEnv
* env
)
1923 OnigCodePoint i
, j
, n1
, n2
, *data1
, *data2
;
1924 OnigCodePoint from
, to
, from1
, to1
, from2
, to2
;
1926 *pbuf
= (BBuf
* )NULL
;
1927 if (IS_NULL(bbuf1
)) {
1928 if (not1
!= 0 && IS_NOT_NULL(bbuf2
)) /* not1 != 0 -> not2 == 0 */
1929 return bbuf_clone(pbuf
, bbuf2
);
1932 else if (IS_NULL(bbuf2
)) {
1934 return bbuf_clone(pbuf
, bbuf1
);
1939 SWAP_BBUF_NOT(bbuf1
, not1
, bbuf2
, not2
);
1941 data1
= (OnigCodePoint
* )(bbuf1
->p
);
1942 data2
= (OnigCodePoint
* )(bbuf2
->p
);
1943 GET_CODE_POINT(n1
, data1
);
1944 GET_CODE_POINT(n2
, data2
);
1948 if (not2
== 0 && not1
== 0) { /* 1 AND 2 */
1949 for (i
= 0; i
< n1
; i
++) {
1952 for (j
= 0; j
< n2
; j
++) {
1955 if (from2
> to1
) break;
1956 if (to2
< from1
) continue;
1957 from
= MAX(from1
, from2
);
1959 r
= add_code_range_to_buf(pbuf
, env
, from
, to
);
1960 if (r
!= 0) return r
;
1964 else if (not1
== 0) { /* 1 AND (not 2) */
1965 for (i
= 0; i
< n1
; i
++) {
1968 r
= and_code_range1(pbuf
, env
, from1
, to1
, data2
, n2
);
1969 if (r
!= 0) return r
;
1977 and_cclass(CClassNode
* dest
, CClassNode
* cc
, ScanEnv
* env
)
1979 OnigEncoding enc
= env
->enc
;
1981 BBuf
*buf1
, *buf2
, *pbuf
= 0;
1982 BitSetRef bsr1
, bsr2
;
1985 not1
= IS_NCCLASS_NOT(dest
);
1988 not2
= IS_NCCLASS_NOT(cc
);
1993 bitset_invert_to(bsr1
, bs1
);
1997 bitset_invert_to(bsr2
, bs2
);
2000 bitset_and(bsr1
, bsr2
);
2001 if (bsr1
!= dest
->bs
) {
2002 bitset_copy(dest
->bs
, bsr1
);
2006 bitset_invert(dest
->bs
);
2009 if (! ONIGENC_IS_SINGLEBYTE(enc
)) {
2010 if (not1
!= 0 && not2
!= 0) {
2011 r
= or_code_range_buf(enc
, buf1
, 0, buf2
, 0, &pbuf
, env
);
2014 r
= and_code_range_buf(buf1
, not1
, buf2
, not2
, &pbuf
, env
);
2015 if (r
== 0 && not1
!= 0) {
2017 r
= not_code_range_buf(enc
, pbuf
, &tbuf
, env
);
2035 or_cclass(CClassNode
* dest
, CClassNode
* cc
, ScanEnv
* env
)
2037 OnigEncoding enc
= env
->enc
;
2039 BBuf
*buf1
, *buf2
, *pbuf
= 0;
2040 BitSetRef bsr1
, bsr2
;
2043 not1
= IS_NCCLASS_NOT(dest
);
2046 not2
= IS_NCCLASS_NOT(cc
);
2051 bitset_invert_to(bsr1
, bs1
);
2055 bitset_invert_to(bsr2
, bs2
);
2058 bitset_or(bsr1
, bsr2
);
2059 if (bsr1
!= dest
->bs
) {
2060 bitset_copy(dest
->bs
, bsr1
);
2064 bitset_invert(dest
->bs
);
2067 if (! ONIGENC_IS_SINGLEBYTE(enc
)) {
2068 if (not1
!= 0 && not2
!= 0) {
2069 r
= and_code_range_buf(buf1
, 0, buf2
, 0, &pbuf
, env
);
2072 r
= or_code_range_buf(enc
, buf1
, not1
, buf2
, not2
, &pbuf
, env
);
2073 if (r
== 0 && not1
!= 0) {
2075 r
= not_code_range_buf(enc
, pbuf
, &tbuf
, env
);
2093 static void UNKNOWN_ESC_WARN(ScanEnv
*env
, int c
);
2095 static OnigCodePoint
2096 conv_backslash_value(OnigCodePoint c
, ScanEnv
* env
)
2098 if (IS_SYNTAX_OP(env
->syntax
, ONIG_SYN_OP_ESC_CONTROL_CHARS
)) {
2100 case 'n': return '\n';
2101 case 't': return '\t';
2102 case 'r': return '\r';
2103 case 'f': return '\f';
2104 case 'a': return '\007';
2105 case 'b': return '\010';
2106 case 'e': return '\033';
2108 if (IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_ESC_V_VTAB
))
2113 if (('a' <= c
&& c
<= 'z') || ('A' <= c
&& c
<= 'Z'))
2114 UNKNOWN_ESC_WARN(env
, c
);
2121 #ifdef USE_NO_INVALID_QUANTIFIER
2122 # define is_invalid_quantifier_target(node) 0
2125 is_invalid_quantifier_target(Node
* node
)
2127 switch (NTYPE(node
)) {
2133 /* allow enclosed elements */
2134 /* return is_invalid_quantifier_target(NENCLOSE(node)->target); */
2139 if (! is_invalid_quantifier_target(NCAR(node
))) return 0;
2140 } while (IS_NOT_NULL(node
= NCDR(node
)));
2146 if (is_invalid_quantifier_target(NCAR(node
))) return 1;
2147 } while (IS_NOT_NULL(node
= NCDR(node
)));
2157 /* ?:0, *:1, +:2, ??:3, *?:4, +?:5 */
2159 popular_quantifier_num(QtfrNode
* q
)
2162 if (q
->lower
== 0) {
2163 if (q
->upper
== 1) return 0;
2164 else if (IS_REPEAT_INFINITE(q
->upper
)) return 1;
2166 else if (q
->lower
== 1) {
2167 if (IS_REPEAT_INFINITE(q
->upper
)) return 2;
2171 if (q
->lower
== 0) {
2172 if (q
->upper
== 1) return 3;
2173 else if (IS_REPEAT_INFINITE(q
->upper
)) return 4;
2175 else if (q
->lower
== 1) {
2176 if (IS_REPEAT_INFINITE(q
->upper
)) return 5;
2184 RQ_ASIS
= 0, /* as is */
2185 RQ_DEL
= 1, /* delete parent */
2187 RQ_AQ
, /* to '*?' */
2188 RQ_QQ
, /* to '??' */
2189 RQ_P_QQ
, /* to '+)??' */
2192 static enum ReduceType
const ReduceTypeTable
[6][6] = {
2193 /* '?', '*', '+', '??', '*?', '+?' p / c */
2194 {RQ_DEL
, RQ_A
, RQ_A
, RQ_QQ
, RQ_AQ
, RQ_ASIS
}, /* '?' */
2195 {RQ_DEL
, RQ_DEL
, RQ_DEL
, RQ_P_QQ
, RQ_P_QQ
, RQ_DEL
}, /* '*' */
2196 {RQ_A
, RQ_A
, RQ_DEL
, RQ_ASIS
, RQ_P_QQ
, RQ_DEL
}, /* '+' */
2197 {RQ_DEL
, RQ_AQ
, RQ_AQ
, RQ_DEL
, RQ_AQ
, RQ_AQ
}, /* '??' */
2198 {RQ_DEL
, RQ_DEL
, RQ_DEL
, RQ_DEL
, RQ_DEL
, RQ_DEL
}, /* '*?' */
2199 {RQ_ASIS
, RQ_ASIS
, RQ_ASIS
, RQ_AQ
, RQ_AQ
, RQ_DEL
} /* '+?' */
2203 onig_reduce_nested_quantifier(Node
* pnode
, Node
* cnode
)
2210 pnum
= popular_quantifier_num(p
);
2211 cnum
= popular_quantifier_num(c
);
2212 if (pnum
< 0 || cnum
< 0) return ;
2214 switch (ReduceTypeTable
[cnum
][pnum
]) {
2219 p
->target
= c
->target
;
2220 p
->lower
= 0; p
->upper
= REPEAT_INFINITE
; p
->greedy
= 1;
2223 p
->target
= c
->target
;
2224 p
->lower
= 0; p
->upper
= REPEAT_INFINITE
; p
->greedy
= 0;
2227 p
->target
= c
->target
;
2228 p
->lower
= 0; p
->upper
= 1; p
->greedy
= 0;
2232 p
->lower
= 0; p
->upper
= 1; p
->greedy
= 0;
2233 c
->lower
= 1; c
->upper
= REPEAT_INFINITE
; c
->greedy
= 1;
2242 c
->target
= NULL_NODE
;
2243 onig_node_free(cnode
);
2248 TK_EOT
= 0, /* end of token */
2260 TK_ANYCHAR_ANYTIME
, /* SQL '%' == .* */
2266 TK_CHAR_PROPERTY
, /* \p{...}, \P{...} */
2268 TK_EXTENDED_GRAPHEME_CLUSTER
,
2273 TK_POSIX_BRACKET_OPEN
,
2275 TK_CC_CC_OPEN
/* [ */
2279 enum TokenSyms type
;
2281 int base
; /* is number: 8, 16 (used in [....]) */
2302 #ifdef USE_BACKREF_WITH_LEVEL
2304 int level
; /* \k<name+n> */
2322 fetch_range_quantifier(UChar
** src
, UChar
* end
, OnigToken
* tok
, ScanEnv
* env
)
2324 int low
, up
, syn_allow
, non_low
= 0;
2327 OnigEncoding enc
= env
->enc
;
2331 syn_allow
= IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_ALLOW_INVALID_INTERVAL
);
2335 return 1; /* "....{" : OK! */
2337 return ONIGERR_END_PATTERN_AT_LEFT_BRACE
; /* "....{" syntax error */
2342 if (c
== ')' || c
== '(' || c
== '|') {
2343 return ONIGERR_END_PATTERN_AT_LEFT_BRACE
;
2347 low
= onig_scan_unsigned_number(&p
, end
, env
->enc
);
2348 if (low
< 0) return ONIGERR_TOO_BIG_NUMBER_FOR_REPEAT_RANGE
;
2349 if (low
> ONIG_MAX_REPEAT_NUM
)
2350 return ONIGERR_TOO_BIG_NUMBER_FOR_REPEAT_RANGE
;
2352 if (p
== *src
) { /* can't read low */
2353 if (IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_ALLOW_INTERVAL_LOW_ABBREV
)) {
2354 /* allow {,n} as {0,n} */
2362 if (PEND
) goto invalid
;
2366 up
= onig_scan_unsigned_number(&p
, end
, env
->enc
);
2367 if (up
< 0) return ONIGERR_TOO_BIG_NUMBER_FOR_REPEAT_RANGE
;
2368 if (up
> ONIG_MAX_REPEAT_NUM
)
2369 return ONIGERR_TOO_BIG_NUMBER_FOR_REPEAT_RANGE
;
2374 up
= REPEAT_INFINITE
; /* {n,} : {n,infinite} */
2382 up
= low
; /* {n} : exact n times */
2386 if (PEND
) goto invalid
;
2388 if (IS_SYNTAX_OP(env
->syntax
, ONIG_SYN_OP_ESC_BRACE_INTERVAL
)) {
2389 if (c
!= MC_ESC(env
->syntax
)) goto invalid
;
2390 if (PEND
) goto invalid
;
2393 if (c
!= '}') goto invalid
;
2395 if (!IS_REPEAT_INFINITE(up
) && low
> up
) {
2396 return ONIGERR_UPPER_SMALLER_THAN_LOWER_IN_REPEAT_RANGE
;
2399 tok
->type
= TK_INTERVAL
;
2400 tok
->u
.repeat
.lower
= low
;
2401 tok
->u
.repeat
.upper
= up
;
2403 return r
; /* 0: normal {n,m}, 2: fixed {n} */
2409 return ONIGERR_INVALID_REPEAT_RANGE_PATTERN
;
2412 /* \M-, \C-, \c, or \... */
2414 fetch_escaped_value(UChar
** src
, UChar
* end
, ScanEnv
* env
, OnigCodePoint
* val
)
2418 OnigEncoding enc
= env
->enc
;
2421 if (PEND
) return ONIGERR_END_PATTERN_AT_ESCAPE
;
2426 if (IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_ESC_CAPITAL_M_BAR_META
)) {
2427 if (PEND
) return ONIGERR_END_PATTERN_AT_META
;
2429 if (c
!= '-') return ONIGERR_META_CODE_SYNTAX
;
2430 if (PEND
) return ONIGERR_END_PATTERN_AT_META
;
2432 if (c
== MC_ESC(env
->syntax
)) {
2433 v
= fetch_escaped_value(&p
, end
, env
, &c
);
2434 if (v
< 0) return v
;
2436 c
= ((c
& 0xff) | 0x80);
2443 if (IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_ESC_CAPITAL_C_BAR_CONTROL
)) {
2444 if (PEND
) return ONIGERR_END_PATTERN_AT_CONTROL
;
2446 if (c
!= '-') return ONIGERR_CONTROL_CODE_SYNTAX
;
2453 if (IS_SYNTAX_OP(env
->syntax
, ONIG_SYN_OP_ESC_C_CONTROL
)) {
2455 if (PEND
) return ONIGERR_END_PATTERN_AT_CONTROL
;
2461 if (c
== MC_ESC(env
->syntax
)) {
2462 v
= fetch_escaped_value(&p
, end
, env
, &c
);
2463 if (v
< 0) return v
;
2474 c
= conv_backslash_value(c
, env
);
2484 static int fetch_token(OnigToken
* tok
, UChar
** src
, UChar
* end
, ScanEnv
* env
);
2486 static OnigCodePoint
2487 get_name_end_code_point(OnigCodePoint start
)
2490 case '<': return (OnigCodePoint
)'>'; break;
2491 case '\'': return (OnigCodePoint
)'\''; break;
2492 case '(': return (OnigCodePoint
)')'; break;
2493 case '{': return (OnigCodePoint
)'}'; break;
2498 return (OnigCodePoint
)0;
2501 #ifdef USE_NAMED_GROUP
2503 # define ONIGENC_IS_CODE_NAME(enc, c) TRUE
2505 # define ONIGENC_IS_CODE_NAME(enc, c) ONIGENC_IS_CODE_WORD(enc, c)
2508 # ifdef USE_BACKREF_WITH_LEVEL
2510 \k<name+n>, \k<name-n>
2511 \k<num+n>, \k<num-n>
2512 \k<-num+n>, \k<-num-n>
2515 fetch_name_with_level(OnigCodePoint start_code
, UChar
** src
, UChar
* end
,
2516 UChar
** rname_end
, ScanEnv
* env
,
2517 int* rback_num
, int* rlevel
)
2519 int r
, sign
, is_num
, exist_level
;
2520 OnigCodePoint end_code
;
2521 OnigCodePoint c
= 0;
2522 OnigEncoding enc
= env
->enc
;
2529 is_num
= exist_level
= 0;
2533 end_code
= get_name_end_code_point(start_code
);
2538 return ONIGERR_EMPTY_GROUP_NAME
;
2543 return ONIGERR_EMPTY_GROUP_NAME
;
2545 if (ONIGENC_IS_CODE_DIGIT(enc
, c
)) {
2548 else if (c
== '-') {
2553 else if (!ONIGENC_IS_CODE_NAME(enc
, c
)) {
2554 r
= ONIGERR_INVALID_CHAR_IN_GROUP_NAME
;
2561 if (c
== end_code
|| c
== ')' || c
== '+' || c
== '-') {
2562 if (is_num
== 2) r
= ONIGERR_INVALID_GROUP_NAME
;
2567 if (ONIGENC_IS_CODE_DIGIT(enc
, c
)) {
2571 r
= ONIGERR_INVALID_GROUP_NAME
;
2575 else if (!ONIGENC_IS_CODE_NAME(enc
, c
)) {
2576 r
= ONIGERR_INVALID_CHAR_IN_GROUP_NAME
;
2580 if (r
== 0 && c
!= end_code
) {
2581 if (c
== '+' || c
== '-') {
2583 int flag
= (c
== '-' ? -1 : 1);
2586 r
= ONIGERR_INVALID_CHAR_IN_GROUP_NAME
;
2590 if (! ONIGENC_IS_CODE_DIGIT(enc
, c
)) goto err
;
2592 level
= onig_scan_unsigned_number(&p
, end
, enc
);
2593 if (level
< 0) return ONIGERR_TOO_BIG_NUMBER
;
2594 *rlevel
= (level
* flag
);
2605 r
= ONIGERR_INVALID_GROUP_NAME
;
2612 *rback_num
= onig_scan_unsigned_number(&pnum_head
, name_end
, enc
);
2613 if (*rback_num
< 0) return ONIGERR_TOO_BIG_NUMBER
;
2614 else if (*rback_num
== 0) goto err
;
2619 *rname_end
= name_end
;
2621 return (exist_level
? 1 : 0);
2624 onig_scan_env_set_error_string(env
, r
, *src
, name_end
);
2628 # endif /* USE_BACKREF_WITH_LEVEL */
2631 ref: 0 -> define name (don't allow number name)
2632 1 -> reference name (allow number name)
2635 fetch_name(OnigCodePoint start_code
, UChar
** src
, UChar
* end
,
2636 UChar
** rname_end
, ScanEnv
* env
, int* rback_num
, int ref
)
2638 int r
, is_num
, sign
;
2639 OnigCodePoint end_code
;
2640 OnigCodePoint c
= 0;
2641 OnigEncoding enc
= env
->enc
;
2648 end_code
= get_name_end_code_point(start_code
);
2656 return ONIGERR_EMPTY_GROUP_NAME
;
2661 return ONIGERR_EMPTY_GROUP_NAME
;
2663 if (ONIGENC_IS_CODE_DIGIT(enc
, c
)) {
2667 r
= ONIGERR_INVALID_GROUP_NAME
;
2671 else if (c
== '-') {
2678 r
= ONIGERR_INVALID_GROUP_NAME
;
2682 else if (!ONIGENC_IS_CODE_NAME(enc
, c
)) {
2683 r
= ONIGERR_INVALID_CHAR_IN_GROUP_NAME
;
2691 if (c
== end_code
|| c
== ')') {
2693 r
= ONIGERR_INVALID_GROUP_NAME
;
2700 if (ONIGENC_IS_CODE_DIGIT(enc
, c
)) {
2704 if (!ONIGENC_IS_CODE_WORD(enc
, c
))
2705 r
= ONIGERR_INVALID_CHAR_IN_GROUP_NAME
;
2707 r
= ONIGERR_INVALID_GROUP_NAME
;
2712 if (!ONIGENC_IS_CODE_NAME(enc
, c
)) {
2713 r
= ONIGERR_INVALID_CHAR_IN_GROUP_NAME
;
2719 if (c
!= end_code
) {
2720 r
= ONIGERR_INVALID_GROUP_NAME
;
2726 *rback_num
= onig_scan_unsigned_number(&pnum_head
, name_end
, enc
);
2727 if (*rback_num
< 0) return ONIGERR_TOO_BIG_NUMBER
;
2728 else if (*rback_num
== 0) {
2729 r
= ONIGERR_INVALID_GROUP_NAME
;
2736 *rname_end
= name_end
;
2745 if (c
== end_code
|| c
== ')')
2752 onig_scan_env_set_error_string(env
, r
, *src
, name_end
);
2758 fetch_name(OnigCodePoint start_code
, UChar
** src
, UChar
* end
,
2759 UChar
** rname_end
, ScanEnv
* env
, int* rback_num
, int ref
)
2761 int r
, is_num
, sign
;
2762 OnigCodePoint end_code
;
2763 OnigCodePoint c
= 0;
2765 OnigEncoding enc
= env
->enc
;
2772 end_code
= get_name_end_code_point(start_code
);
2774 *rname_end
= name_end
= end
;
2781 return ONIGERR_EMPTY_GROUP_NAME
;
2786 return ONIGERR_EMPTY_GROUP_NAME
;
2788 if (ONIGENC_IS_CODE_DIGIT(enc
, c
)) {
2791 else if (c
== '-') {
2797 r
= ONIGERR_INVALID_CHAR_IN_GROUP_NAME
;
2805 if (c
== end_code
|| c
== ')') break;
2806 if (! ONIGENC_IS_CODE_DIGIT(enc
, c
))
2807 r
= ONIGERR_INVALID_CHAR_IN_GROUP_NAME
;
2809 if (r
== 0 && c
!= end_code
) {
2810 r
= ONIGERR_INVALID_GROUP_NAME
;
2815 *rback_num
= onig_scan_unsigned_number(&pnum_head
, name_end
, enc
);
2816 if (*rback_num
< 0) return ONIGERR_TOO_BIG_NUMBER
;
2817 else if (*rback_num
== 0) {
2818 r
= ONIGERR_INVALID_GROUP_NAME
;
2823 *rname_end
= name_end
;
2829 onig_scan_env_set_error_string(env
, r
, *src
, name_end
);
2833 #endif /* USE_NAMED_GROUP */
2837 PRINTF_ARGS(static void onig_syntax_warn(ScanEnv
*env
, const char *fmt
, ...), 2, 3);
2841 onig_syntax_warn(ScanEnv
*env
, const char *fmt
, ...)
2844 UChar buf
[WARN_BUFSIZE
];
2845 va_start(args
, fmt
);
2846 onig_vsnprintf_with_pattern(buf
, WARN_BUFSIZE
, env
->enc
,
2847 env
->pattern
, env
->pattern_end
,
2851 if (env
->sourcefile
== NULL
)
2852 rb_warn("%s", (char *)buf
);
2854 rb_compile_warn(env
->sourcefile
, env
->sourceline
, "%s", (char *)buf
);
2856 (*onig_warn
)((char* )buf
);
2861 CC_ESC_WARN(ScanEnv
*env
, UChar
*c
)
2863 if (onig_warn
== onig_null_warn
) return ;
2865 if (IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_WARN_CC_OP_NOT_ESCAPED
) &&
2866 IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_BACKSLASH_ESCAPE_IN_CC
)) {
2867 onig_syntax_warn(env
, "character class has '%s' without escape", c
);
2872 CLOSE_BRACKET_WITHOUT_ESC_WARN(ScanEnv
* env
, UChar
* c
)
2874 if (onig_warn
== onig_null_warn
) return ;
2876 if (IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_WARN_CC_OP_NOT_ESCAPED
)) {
2877 onig_syntax_warn(env
, "regular expression has '%s' without escape", c
);
2886 CC_DUP_WARN(ScanEnv
*env
, OnigCodePoint from ARG_UNUSED
, OnigCodePoint to ARG_UNUSED
)
2888 if (onig_warn
== onig_null_warn
|| !RTEST(ruby_verbose
)) return ;
2890 if (IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_WARN_CC_DUP
) &&
2891 !(env
->warnings_flag
& ONIG_SYN_WARN_CC_DUP
)) {
2892 #ifdef WARN_ALL_CC_DUP
2893 onig_syntax_warn(env
, "character class has duplicated range: %04x-%04x", from
, to
);
2895 env
->warnings_flag
|= ONIG_SYN_WARN_CC_DUP
;
2896 onig_syntax_warn(env
, "character class has duplicated range");
2902 UNKNOWN_ESC_WARN(ScanEnv
*env
, int c
)
2904 if (onig_warn
== onig_null_warn
|| !RTEST(ruby_verbose
)) return ;
2905 onig_syntax_warn(env
, "Unknown escape \\%c is ignored", c
);
2909 find_str_position(OnigCodePoint s
[], int n
, UChar
* from
, UChar
* to
,
2910 UChar
**next
, OnigEncoding enc
)
2918 x
= ONIGENC_MBC_TO_CODE(enc
, p
, to
);
2919 q
= p
+ enclen(enc
, p
, to
);
2921 for (i
= 1; i
< n
&& q
< to
; i
++) {
2922 x
= ONIGENC_MBC_TO_CODE(enc
, q
, to
);
2923 if (x
!= s
[i
]) break;
2924 q
+= enclen(enc
, q
, to
);
2927 if (IS_NOT_NULL(next
))
2938 str_exist_check_with_esc(OnigCodePoint s
[], int n
, UChar
* from
, UChar
* to
,
2939 OnigCodePoint bad
, OnigEncoding enc
, const OnigSyntaxType
* syn
)
2950 p
+= enclen(enc
, p
, to
);
2953 x
= ONIGENC_MBC_TO_CODE(enc
, p
, to
);
2954 q
= p
+ enclen(enc
, p
, to
);
2956 for (i
= 1; i
< n
&& q
< to
; i
++) {
2957 x
= ONIGENC_MBC_TO_CODE(enc
, q
, to
);
2958 if (x
!= s
[i
]) break;
2959 q
+= enclen(enc
, q
, to
);
2961 if (i
>= n
) return 1;
2962 p
+= enclen(enc
, p
, to
);
2965 x
= ONIGENC_MBC_TO_CODE(enc
, p
, to
);
2966 if (x
== bad
) return 0;
2967 else if (x
== MC_ESC(syn
)) in_esc
= 1;
2976 fetch_token_in_cc(OnigToken
* tok
, UChar
** src
, UChar
* end
, ScanEnv
* env
)
2979 OnigCodePoint c
, c2
;
2980 const OnigSyntaxType
* syn
= env
->syntax
;
2981 OnigEncoding enc
= env
->enc
;
2992 tok
->type
= TK_CHAR
;
2998 tok
->type
= TK_CC_CLOSE
;
3000 else if (c
== '-') {
3001 tok
->type
= TK_CC_RANGE
;
3003 else if (c
== MC_ESC(syn
)) {
3004 if (! IS_SYNTAX_BV(syn
, ONIG_SYN_BACKSLASH_ESCAPE_IN_CC
))
3007 if (PEND
) return ONIGERR_END_PATTERN_AT_ESCAPE
;
3014 tok
->type
= TK_CHAR_TYPE
;
3015 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_WORD
;
3016 tok
->u
.prop
.not = 0;
3019 tok
->type
= TK_CHAR_TYPE
;
3020 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_WORD
;
3021 tok
->u
.prop
.not = 1;
3024 tok
->type
= TK_CHAR_TYPE
;
3025 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_DIGIT
;
3026 tok
->u
.prop
.not = 0;
3029 tok
->type
= TK_CHAR_TYPE
;
3030 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_DIGIT
;
3031 tok
->u
.prop
.not = 1;
3034 tok
->type
= TK_CHAR_TYPE
;
3035 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_SPACE
;
3036 tok
->u
.prop
.not = 0;
3039 tok
->type
= TK_CHAR_TYPE
;
3040 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_SPACE
;
3041 tok
->u
.prop
.not = 1;
3044 if (! IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_H_XDIGIT
)) break;
3045 tok
->type
= TK_CHAR_TYPE
;
3046 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_XDIGIT
;
3047 tok
->u
.prop
.not = 0;
3050 if (! IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_H_XDIGIT
)) break;
3051 tok
->type
= TK_CHAR_TYPE
;
3052 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_XDIGIT
;
3053 tok
->u
.prop
.not = 1;
3062 IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_P_BRACE_CHAR_PROPERTY
)) {
3064 tok
->type
= TK_CHAR_PROPERTY
;
3065 tok
->u
.prop
.not = (c
== 'P' ? 1 : 0);
3067 if (!PEND
&& IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_P_BRACE_CIRCUMFLEX_NOT
)) {
3070 tok
->u
.prop
.not = (tok
->u
.prop
.not == 0 ? 1 : 0);
3077 onig_syntax_warn(env
, "invalid Unicode Property \\%c", c
);
3085 if (PPEEK_IS('{') && IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_X_BRACE_HEX8
)) {
3087 num
= scan_unsigned_hexadecimal_number(&p
, end
, 0, 8, enc
);
3088 if (num
< 0) return ONIGERR_TOO_BIG_WIDE_CHAR_VALUE
;
3091 if (ONIGENC_IS_CODE_XDIGIT(enc
, c2
))
3092 return ONIGERR_TOO_LONG_WIDE_CHAR_VALUE
;
3095 if (p
> prev
+ enclen(enc
, prev
, end
) && !PEND
&& (PPEEK_IS('}'))) {
3097 tok
->type
= TK_CODE_POINT
;
3099 tok
->u
.code
= (OnigCodePoint
)num
;
3102 /* can't read nothing or invalid format */
3106 else if (IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_X_HEX2
)) {
3107 num
= scan_unsigned_hexadecimal_number(&p
, end
, 0, 2, enc
);
3108 if (num
< 0) return ONIGERR_TOO_BIG_NUMBER
;
3109 if (p
== prev
) { /* can't read nothing. */
3110 num
= 0; /* but, it's not error */
3112 tok
->type
= TK_RAW_BYTE
;
3122 if (IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_U_HEX4
)) {
3123 num
= scan_unsigned_hexadecimal_number(&p
, end
, 4, 4, enc
);
3124 if (num
< -1) return ONIGERR_TOO_SHORT_DIGITS
;
3125 else if (num
< 0) return ONIGERR_TOO_BIG_NUMBER
;
3126 if (p
== prev
) { /* can't read nothing. */
3127 num
= 0; /* but, it's not error */
3129 tok
->type
= TK_CODE_POINT
;
3131 tok
->u
.code
= (OnigCodePoint
)num
;
3139 if (PPEEK_IS('{') && IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_O_BRACE_OCTAL
)) {
3141 num
= scan_unsigned_octal_number(&p
, end
, 11, enc
);
3142 if (num
< 0) return ONIGERR_TOO_BIG_WIDE_CHAR_VALUE
;
3145 if (ONIGENC_IS_CODE_DIGIT(enc
, c2
) && c2
< '8')
3146 return ONIGERR_TOO_LONG_WIDE_CHAR_VALUE
;
3149 if (p
> prev
+ enclen(enc
, prev
, end
) && !PEND
&& (PPEEK_IS('}'))) {
3151 tok
->type
= TK_CODE_POINT
;
3153 tok
->u
.code
= (OnigCodePoint
)num
;
3156 /* can't read nothing or invalid format */
3163 case '1': case '2': case '3': case '4': case '5': case '6': case '7':
3164 if (IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_OCTAL3
)) {
3167 num
= scan_unsigned_octal_number(&p
, end
, 3, enc
);
3168 if (num
< 0 || 0xff < num
) return ONIGERR_TOO_BIG_NUMBER
;
3169 if (p
== prev
) { /* can't read nothing. */
3170 num
= 0; /* but, it's not error */
3172 tok
->type
= TK_RAW_BYTE
;
3180 num
= fetch_escaped_value(&p
, end
, env
, &c2
);
3181 if (num
< 0) return num
;
3182 if ((OnigCodePoint
)tok
->u
.c
!= c2
) {
3183 tok
->u
.code
= (OnigCodePoint
)c2
;
3184 tok
->type
= TK_CODE_POINT
;
3189 else if (c
== '[') {
3190 if (IS_SYNTAX_OP(syn
, ONIG_SYN_OP_POSIX_BRACKET
) && (PPEEK_IS(':'))) {
3191 OnigCodePoint send
[] = { (OnigCodePoint
)':', (OnigCodePoint
)']' };
3192 tok
->backp
= p
; /* point at '[' is read */
3194 if (str_exist_check_with_esc(send
, 2, p
, end
,
3195 (OnigCodePoint
)']', enc
, syn
)) {
3196 tok
->type
= TK_POSIX_BRACKET_OPEN
;
3205 if (IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_CCLASS_SET_OP
)) {
3206 tok
->type
= TK_CC_CC_OPEN
;
3209 CC_ESC_WARN(env
, (UChar
* )"[");
3213 else if (c
== '&') {
3214 if (IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_CCLASS_SET_OP
) &&
3215 !PEND
&& (PPEEK_IS('&'))) {
3217 tok
->type
= TK_CC_AND
;
3226 #ifdef USE_NAMED_GROUP
3228 fetch_named_backref_token(OnigCodePoint c
, OnigToken
* tok
, UChar
** src
,
3229 UChar
* end
, ScanEnv
* env
)
3232 const OnigSyntaxType
* syn
= env
->syntax
;
3241 # ifdef USE_BACKREF_WITH_LEVEL
3242 name_end
= NULL_UCHARP
; /* no need. escape gcc warning. */
3243 r
= fetch_name_with_level(c
, &p
, end
, &name_end
,
3244 env
, &back_num
, &tok
->u
.backref
.level
);
3245 if (r
== 1) tok
->u
.backref
.exist_level
= 1;
3246 else tok
->u
.backref
.exist_level
= 0;
3248 r
= fetch_name(&p
, end
, &name_end
, env
, &back_num
, 1);
3250 if (r
< 0) return r
;
3252 if (back_num
!= 0) {
3254 back_num
= BACKREF_REL_TO_ABS(back_num
, env
);
3256 return ONIGERR_INVALID_BACKREF
;
3259 if (IS_SYNTAX_BV(syn
, ONIG_SYN_STRICT_CHECK_BACKREF
)) {
3260 if (back_num
> env
->num_mem
||
3261 IS_NULL(SCANENV_MEM_NODES(env
)[back_num
]))
3262 return ONIGERR_INVALID_BACKREF
;
3264 tok
->type
= TK_BACKREF
;
3265 tok
->u
.backref
.by_name
= 0;
3266 tok
->u
.backref
.num
= 1;
3267 tok
->u
.backref
.ref1
= back_num
;
3270 num
= onig_name_to_group_numbers(env
->reg
, prev
, name_end
, &backs
);
3272 onig_scan_env_set_error_string(env
,
3273 ONIGERR_UNDEFINED_NAME_REFERENCE
, prev
, name_end
);
3274 return ONIGERR_UNDEFINED_NAME_REFERENCE
;
3276 if (IS_SYNTAX_BV(syn
, ONIG_SYN_STRICT_CHECK_BACKREF
)) {
3278 for (i
= 0; i
< num
; i
++) {
3279 if (backs
[i
] > env
->num_mem
||
3280 IS_NULL(SCANENV_MEM_NODES(env
)[backs
[i
]]))
3281 return ONIGERR_INVALID_BACKREF
;
3285 tok
->type
= TK_BACKREF
;
3286 tok
->u
.backref
.by_name
= 1;
3287 if (num
== 1 || IS_SYNTAX_BV(syn
, ONIG_SYN_USE_LEFT_MOST_NAMED_GROUP
)) {
3288 tok
->u
.backref
.num
= 1;
3289 tok
->u
.backref
.ref1
= backs
[0];
3292 tok
->u
.backref
.num
= num
;
3293 tok
->u
.backref
.refs
= backs
;
3302 fetch_token(OnigToken
* tok
, UChar
** src
, UChar
* end
, ScanEnv
* env
)
3306 OnigEncoding enc
= env
->enc
;
3307 const OnigSyntaxType
* syn
= env
->syntax
;
3318 tok
->type
= TK_STRING
;
3323 if (IS_MC_ESC_CODE(c
, syn
)) {
3324 if (PEND
) return ONIGERR_END_PATTERN_AT_ESCAPE
;
3333 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_ASTERISK_ZERO_INF
)) break;
3334 tok
->type
= TK_OP_REPEAT
;
3335 tok
->u
.repeat
.lower
= 0;
3336 tok
->u
.repeat
.upper
= REPEAT_INFINITE
;
3341 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_PLUS_ONE_INF
)) break;
3342 tok
->type
= TK_OP_REPEAT
;
3343 tok
->u
.repeat
.lower
= 1;
3344 tok
->u
.repeat
.upper
= REPEAT_INFINITE
;
3349 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_QMARK_ZERO_ONE
)) break;
3350 tok
->type
= TK_OP_REPEAT
;
3351 tok
->u
.repeat
.lower
= 0;
3352 tok
->u
.repeat
.upper
= 1;
3354 if (!PEND
&& PPEEK_IS('?') &&
3355 IS_SYNTAX_OP(syn
, ONIG_SYN_OP_QMARK_NON_GREEDY
)) {
3357 tok
->u
.repeat
.greedy
= 0;
3358 tok
->u
.repeat
.possessive
= 0;
3362 if (!PEND
&& PPEEK_IS('+') &&
3363 ((IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_PLUS_POSSESSIVE_REPEAT
) &&
3364 tok
->type
!= TK_INTERVAL
) ||
3365 (IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_PLUS_POSSESSIVE_INTERVAL
) &&
3366 tok
->type
== TK_INTERVAL
))) {
3368 tok
->u
.repeat
.greedy
= 1;
3369 tok
->u
.repeat
.possessive
= 1;
3372 tok
->u
.repeat
.greedy
= 1;
3373 tok
->u
.repeat
.possessive
= 0;
3379 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_BRACE_INTERVAL
)) break;
3380 r
= fetch_range_quantifier(&p
, end
, tok
, env
);
3381 if (r
< 0) return r
; /* error */
3382 if (r
== 0) goto greedy_check
;
3383 else if (r
== 2) { /* {n} */
3384 if (IS_SYNTAX_BV(syn
, ONIG_SYN_FIXED_INTERVAL_IS_GREEDY_ONLY
))
3385 goto possessive_check
;
3389 /* r == 1 : normal char */
3393 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_VBAR_ALT
)) break;
3398 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_LPAREN_SUBEXP
)) break;
3399 tok
->type
= TK_SUBEXP_OPEN
;
3403 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_LPAREN_SUBEXP
)) break;
3404 tok
->type
= TK_SUBEXP_CLOSE
;
3408 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_W_WORD
)) break;
3409 tok
->type
= TK_CHAR_TYPE
;
3410 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_WORD
;
3411 tok
->u
.prop
.not = 0;
3415 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_W_WORD
)) break;
3416 tok
->type
= TK_CHAR_TYPE
;
3417 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_WORD
;
3418 tok
->u
.prop
.not = 1;
3422 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_B_WORD_BOUND
)) break;
3423 tok
->type
= TK_ANCHOR
;
3424 tok
->u
.anchor
.subtype
= ANCHOR_WORD_BOUND
;
3425 tok
->u
.anchor
.ascii_range
= IS_ASCII_RANGE(env
->option
)
3426 && ! IS_WORD_BOUND_ALL_RANGE(env
->option
);
3430 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_B_WORD_BOUND
)) break;
3431 tok
->type
= TK_ANCHOR
;
3432 tok
->u
.anchor
.subtype
= ANCHOR_NOT_WORD_BOUND
;
3433 tok
->u
.anchor
.ascii_range
= IS_ASCII_RANGE(env
->option
)
3434 && ! IS_WORD_BOUND_ALL_RANGE(env
->option
);
3437 #ifdef USE_WORD_BEGIN_END
3439 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_LTGT_WORD_BEGIN_END
)) break;
3440 tok
->type
= TK_ANCHOR
;
3441 tok
->u
.anchor
.subtype
= ANCHOR_WORD_BEGIN
;
3442 tok
->u
.anchor
.ascii_range
= IS_ASCII_RANGE(env
->option
);
3446 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_LTGT_WORD_BEGIN_END
)) break;
3447 tok
->type
= TK_ANCHOR
;
3448 tok
->u
.anchor
.subtype
= ANCHOR_WORD_END
;
3449 tok
->u
.anchor
.ascii_range
= IS_ASCII_RANGE(env
->option
);
3454 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_S_WHITE_SPACE
)) break;
3455 tok
->type
= TK_CHAR_TYPE
;
3456 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_SPACE
;
3457 tok
->u
.prop
.not = 0;
3461 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_S_WHITE_SPACE
)) break;
3462 tok
->type
= TK_CHAR_TYPE
;
3463 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_SPACE
;
3464 tok
->u
.prop
.not = 1;
3468 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_D_DIGIT
)) break;
3469 tok
->type
= TK_CHAR_TYPE
;
3470 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_DIGIT
;
3471 tok
->u
.prop
.not = 0;
3475 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_D_DIGIT
)) break;
3476 tok
->type
= TK_CHAR_TYPE
;
3477 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_DIGIT
;
3478 tok
->u
.prop
.not = 1;
3482 if (! IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_H_XDIGIT
)) break;
3483 tok
->type
= TK_CHAR_TYPE
;
3484 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_XDIGIT
;
3485 tok
->u
.prop
.not = 0;
3489 if (! IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_H_XDIGIT
)) break;
3490 tok
->type
= TK_CHAR_TYPE
;
3491 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_XDIGIT
;
3492 tok
->u
.prop
.not = 1;
3496 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_AZ_BUF_ANCHOR
)) break;
3498 tok
->type
= TK_ANCHOR
;
3499 tok
->u
.anchor
.subtype
= ANCHOR_BEGIN_BUF
;
3503 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_AZ_BUF_ANCHOR
)) break;
3504 tok
->type
= TK_ANCHOR
;
3505 tok
->u
.anchor
.subtype
= ANCHOR_SEMI_END_BUF
;
3509 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_AZ_BUF_ANCHOR
)) break;
3511 tok
->type
= TK_ANCHOR
;
3512 tok
->u
.anchor
.subtype
= ANCHOR_END_BUF
;
3516 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_CAPITAL_G_BEGIN_ANCHOR
)) break;
3517 tok
->type
= TK_ANCHOR
;
3518 tok
->u
.anchor
.subtype
= ANCHOR_BEGIN_POSITION
;
3522 if (! IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_GNU_BUF_ANCHOR
)) break;
3527 if (! IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_GNU_BUF_ANCHOR
)) break;
3535 if (PPEEK_IS('{') && IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_X_BRACE_HEX8
)) {
3537 num
= scan_unsigned_hexadecimal_number(&p
, end
, 0, 8, enc
);
3538 if (num
< 0) return ONIGERR_TOO_BIG_WIDE_CHAR_VALUE
;
3540 if (ONIGENC_IS_CODE_XDIGIT(enc
, PPEEK
))
3541 return ONIGERR_TOO_LONG_WIDE_CHAR_VALUE
;
3544 if ((p
> prev
+ enclen(enc
, prev
, end
)) && !PEND
&& PPEEK_IS('}')) {
3546 tok
->type
= TK_CODE_POINT
;
3547 tok
->u
.code
= (OnigCodePoint
)num
;
3550 /* can't read nothing or invalid format */
3554 else if (IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_X_HEX2
)) {
3555 num
= scan_unsigned_hexadecimal_number(&p
, end
, 0, 2, enc
);
3556 if (num
< 0) return ONIGERR_TOO_BIG_NUMBER
;
3557 if (p
== prev
) { /* can't read nothing. */
3558 num
= 0; /* but, it's not error */
3560 tok
->type
= TK_RAW_BYTE
;
3570 if (IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_U_HEX4
)) {
3571 num
= scan_unsigned_hexadecimal_number(&p
, end
, 4, 4, enc
);
3572 if (num
< -1) return ONIGERR_TOO_SHORT_DIGITS
;
3573 else if (num
< 0) return ONIGERR_TOO_BIG_NUMBER
;
3574 if (p
== prev
) { /* can't read nothing. */
3575 num
= 0; /* but, it's not error */
3577 tok
->type
= TK_CODE_POINT
;
3579 tok
->u
.code
= (OnigCodePoint
)num
;
3587 if (PPEEK_IS('{') && IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_O_BRACE_OCTAL
)) {
3589 num
= scan_unsigned_octal_number(&p
, end
, 11, enc
);
3590 if (num
< 0) return ONIGERR_TOO_BIG_WIDE_CHAR_VALUE
;
3592 OnigCodePoint c
= PPEEK
;
3593 if (ONIGENC_IS_CODE_DIGIT(enc
, c
) && c
< '8')
3594 return ONIGERR_TOO_LONG_WIDE_CHAR_VALUE
;
3597 if ((p
> prev
+ enclen(enc
, prev
, end
)) && !PEND
&& PPEEK_IS('}')) {
3599 tok
->type
= TK_CODE_POINT
;
3600 tok
->u
.code
= (OnigCodePoint
)num
;
3603 /* can't read nothing or invalid format */
3609 case '1': case '2': case '3': case '4':
3610 case '5': case '6': case '7': case '8': case '9':
3613 num
= onig_scan_unsigned_number(&p
, end
, enc
);
3614 if (num
< 0 || num
> ONIG_MAX_BACKREF_NUM
) {
3618 if (IS_SYNTAX_OP(syn
, ONIG_SYN_OP_DECIMAL_BACKREF
) &&
3619 (num
<= env
->num_mem
|| num
<= 9)) { /* This spec. from GNU regex */
3620 if (IS_SYNTAX_BV(syn
, ONIG_SYN_STRICT_CHECK_BACKREF
)) {
3621 if (num
> env
->num_mem
|| IS_NULL(SCANENV_MEM_NODES(env
)[num
]))
3622 return ONIGERR_INVALID_BACKREF
;
3625 tok
->type
= TK_BACKREF
;
3626 tok
->u
.backref
.num
= 1;
3627 tok
->u
.backref
.ref1
= num
;
3628 tok
->u
.backref
.by_name
= 0;
3629 #ifdef USE_BACKREF_WITH_LEVEL
3630 tok
->u
.backref
.exist_level
= 0;
3636 if (c
== '8' || c
== '9') {
3645 if (IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_OCTAL3
)) {
3647 num
= scan_unsigned_octal_number(&p
, end
, (c
== '0' ? 2:3), enc
);
3648 if (num
< 0 || 0xff < num
) return ONIGERR_TOO_BIG_NUMBER
;
3649 if (p
== prev
) { /* can't read nothing. */
3650 num
= 0; /* but, it's not error */
3652 tok
->type
= TK_RAW_BYTE
;
3656 else if (c
!= '0') {
3661 #ifdef USE_NAMED_GROUP
3663 if (!PEND
&& IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_K_NAMED_BACKREF
)) {
3665 if (c
== '<' || c
== '\'') {
3666 r
= fetch_named_backref_token(c
, tok
, &p
, end
, env
);
3667 if (r
< 0) return r
;
3671 onig_syntax_warn(env
, "invalid back reference");
3677 #if defined(USE_SUBEXP_CALL) || defined(USE_NAMED_GROUP)
3679 # ifdef USE_NAMED_GROUP
3680 if (!PEND
&& IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_G_BRACE_BACKREF
)) {
3683 r
= fetch_named_backref_token(c
, tok
, &p
, end
, env
);
3684 if (r
< 0) return r
;
3690 # ifdef USE_SUBEXP_CALL
3691 if (!PEND
&& IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_G_SUBEXP_CALL
)) {
3693 if (c
== '<' || c
== '\'') {
3694 int gnum
= -1, rel
= 0;
3696 OnigCodePoint cnext
;
3701 if (PPEEK_IS(get_name_end_code_point(c
))) { /* \g<0>, \g'0' */
3707 else if (cnext
== '+') {
3713 r
= fetch_name((OnigCodePoint
)c
, &p
, end
, &name_end
, env
, &gnum
, 1);
3714 if (r
< 0) return r
;
3717 tok
->type
= TK_CALL
;
3718 tok
->u
.call
.name
= prev
;
3719 tok
->u
.call
.name_end
= name_end
;
3720 tok
->u
.call
.gnum
= gnum
;
3721 tok
->u
.call
.rel
= rel
;
3724 onig_syntax_warn(env
, "invalid subexp call");
3733 if (IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_CAPITAL_Q_QUOTE
)) {
3734 tok
->type
= TK_QUOTE_OPEN
;
3740 if (PPEEK_IS('{') &&
3741 IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_P_BRACE_CHAR_PROPERTY
)) {
3743 tok
->type
= TK_CHAR_PROPERTY
;
3744 tok
->u
.prop
.not = (c
== 'P' ? 1 : 0);
3746 if (!PEND
&& IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_P_BRACE_CIRCUMFLEX_NOT
)) {
3749 tok
->u
.prop
.not = (tok
->u
.prop
.not == 0 ? 1 : 0);
3756 onig_syntax_warn(env
, "invalid Unicode Property \\%c", c
);
3761 if (IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_CAPITAL_R_LINEBREAK
)) {
3762 tok
->type
= TK_LINEBREAK
;
3767 if (IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_CAPITAL_X_EXTENDED_GRAPHEME_CLUSTER
)) {
3768 tok
->type
= TK_EXTENDED_GRAPHEME_CLUSTER
;
3773 if (IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_CAPITAL_K_KEEP
)) {
3774 tok
->type
= TK_KEEP
;
3783 num
= fetch_escaped_value(&p
, end
, env
, &c2
);
3784 if (num
< 0) return num
;
3786 if ((OnigCodePoint
)tok
->u
.c
!= c2
) {
3787 tok
->type
= TK_CODE_POINT
;
3788 tok
->u
.code
= (OnigCodePoint
)c2
;
3791 p
= tok
->backp
+ enclen(enc
, tok
->backp
, end
);
3801 #ifdef USE_VARIABLE_META_CHARS
3802 if ((c
!= ONIG_INEFFECTIVE_META_CHAR
) &&
3803 IS_SYNTAX_OP(syn
, ONIG_SYN_OP_VARIABLE_META_CHARACTERS
)) {
3804 if (c
== MC_ANYCHAR(syn
))
3806 else if (c
== MC_ANYTIME(syn
))
3808 else if (c
== MC_ZERO_OR_ONE_TIME(syn
))
3809 goto zero_or_one_time
;
3810 else if (c
== MC_ONE_OR_MORE_TIME(syn
))
3811 goto one_or_more_time
;
3812 else if (c
== MC_ANYCHAR_ANYTIME(syn
)) {
3813 tok
->type
= TK_ANYCHAR_ANYTIME
;
3821 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_DOT_ANYCHAR
)) break;
3822 #ifdef USE_VARIABLE_META_CHARS
3825 tok
->type
= TK_ANYCHAR
;
3829 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ASTERISK_ZERO_INF
)) break;
3830 #ifdef USE_VARIABLE_META_CHARS
3833 tok
->type
= TK_OP_REPEAT
;
3834 tok
->u
.repeat
.lower
= 0;
3835 tok
->u
.repeat
.upper
= REPEAT_INFINITE
;
3840 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_PLUS_ONE_INF
)) break;
3841 #ifdef USE_VARIABLE_META_CHARS
3844 tok
->type
= TK_OP_REPEAT
;
3845 tok
->u
.repeat
.lower
= 1;
3846 tok
->u
.repeat
.upper
= REPEAT_INFINITE
;
3851 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_QMARK_ZERO_ONE
)) break;
3852 #ifdef USE_VARIABLE_META_CHARS
3855 tok
->type
= TK_OP_REPEAT
;
3856 tok
->u
.repeat
.lower
= 0;
3857 tok
->u
.repeat
.upper
= 1;
3862 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_BRACE_INTERVAL
)) break;
3863 r
= fetch_range_quantifier(&p
, end
, tok
, env
);
3864 if (r
< 0) return r
; /* error */
3865 if (r
== 0) goto greedy_check
;
3866 else if (r
== 2) { /* {n} */
3867 if (IS_SYNTAX_BV(syn
, ONIG_SYN_FIXED_INTERVAL_IS_GREEDY_ONLY
))
3868 goto possessive_check
;
3872 /* r == 1 : normal char */
3876 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_VBAR_ALT
)) break;
3881 if (PPEEK_IS('?') &&
3882 IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_QMARK_GROUP_EFFECT
)) {
3884 if (PPEEK_IS('#')) {
3887 if (PEND
) return ONIGERR_END_PATTERN_IN_GROUP
;
3889 if (c
== MC_ESC(syn
)) {
3890 if (!PEND
) PFETCH(c
);
3893 if (c
== ')') break;
3898 #ifdef USE_PERL_SUBEXP_CALL
3899 /* (?&name), (?n), (?R), (?0), (?+n), (?-n) */
3901 if ((c
== '&' || c
== 'R' || ONIGENC_IS_CODE_DIGIT(enc
, c
)) &&
3902 IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_QMARK_SUBEXP_CALL
)) {
3903 /* (?&name), (?n), (?R), (?0) */
3908 if (c
== 'R' || c
== '0') {
3909 PINC
; /* skip 'R' / '0' */
3910 if (!PPEEK_IS(')')) return ONIGERR_INVALID_GROUP_NAME
;
3911 PINC
; /* skip ')' */
3912 name_end
= name
= p
;
3917 if (c
== '&') { /* (?&name) */
3919 numref
= 0; /* don't allow number name */
3922 r
= fetch_name((OnigCodePoint
)'(', &p
, end
, &name_end
, env
, &gnum
, numref
);
3923 if (r
< 0) return r
;
3926 tok
->type
= TK_CALL
;
3927 tok
->u
.call
.name
= name
;
3928 tok
->u
.call
.name_end
= name_end
;
3929 tok
->u
.call
.gnum
= gnum
;
3930 tok
->u
.call
.rel
= 0;
3933 else if ((c
== '-' || c
== '+') &&
3934 IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_QMARK_SUBEXP_CALL
)) {
3939 OnigCodePoint cnext
;
3942 PINC
; /* skip '-' / '+' */
3944 if (ONIGENC_IS_CODE_DIGIT(enc
, cnext
)) {
3945 if (c
== '-') PUNFETCH
;
3947 r
= fetch_name((OnigCodePoint
)'(', &p
, end
, &name_end
, env
, &gnum
, 1);
3948 if (r
< 0) return r
;
3950 tok
->type
= TK_CALL
;
3951 tok
->u
.call
.name
= name
;
3952 tok
->u
.call
.name_end
= name_end
;
3953 tok
->u
.call
.gnum
= gnum
;
3954 tok
->u
.call
.rel
= 1;
3958 #endif /* USE_PERL_SUBEXP_CALL */
3959 #ifdef USE_CAPITAL_P_NAMED_GROUP
3960 if (PPEEK_IS('P') &&
3961 IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_QMARK_CAPITAL_P_NAMED_GROUP
)) {
3967 PINC
; /* skip 'P' */
3968 if (PEND
) return ONIGERR_UNDEFINED_GROUP_OPTION
;
3970 if (c
== '=') { /* (?P=name): backref */
3971 r
= fetch_named_backref_token((OnigCodePoint
)'(', tok
, &p
, end
, env
);
3972 if (r
< 0) return r
;
3975 else if (c
== '>') { /* (?P>name): subexp call */
3977 r
= fetch_name((OnigCodePoint
)'(', &p
, end
, &name_end
, env
, &gnum
, 0);
3978 if (r
< 0) return r
;
3980 tok
->type
= TK_CALL
;
3981 tok
->u
.call
.name
= name
;
3982 tok
->u
.call
.name_end
= name_end
;
3983 tok
->u
.call
.gnum
= gnum
;
3984 tok
->u
.call
.rel
= 0;
3988 #endif /* USE_CAPITAL_P_NAMED_GROUP */
3992 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_LPAREN_SUBEXP
)) break;
3993 tok
->type
= TK_SUBEXP_OPEN
;
3997 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_LPAREN_SUBEXP
)) break;
3998 tok
->type
= TK_SUBEXP_CLOSE
;
4002 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_LINE_ANCHOR
)) break;
4003 tok
->type
= TK_ANCHOR
;
4004 tok
->u
.anchor
.subtype
= (IS_SINGLELINE(env
->option
)
4005 ? ANCHOR_BEGIN_BUF
: ANCHOR_BEGIN_LINE
);
4009 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_LINE_ANCHOR
)) break;
4010 tok
->type
= TK_ANCHOR
;
4011 tok
->u
.anchor
.subtype
= (IS_SINGLELINE(env
->option
)
4012 ? ANCHOR_SEMI_END_BUF
: ANCHOR_END_LINE
);
4016 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_BRACKET_CC
)) break;
4017 tok
->type
= TK_CC_OPEN
;
4021 if (*src
> env
->pattern
) /* /].../ is allowed. */
4022 CLOSE_BRACKET_WITHOUT_ESC_WARN(env
, (UChar
* )"]");
4026 if (IS_EXTEND(env
->option
)) {
4029 if (ONIGENC_IS_CODE_NEWLINE(enc
, c
))
4037 case ' ': case '\t': case '\n': case '\r': case '\f':
4038 if (IS_EXTEND(env
->option
))
4048 #ifdef USE_VARIABLE_META_CHARS
4056 add_ctype_to_cc_by_range(CClassNode
* cc
, int ctype ARG_UNUSED
, int not,
4058 OnigCodePoint sb_out
, const OnigCodePoint mbr
[])
4063 int n
= ONIGENC_CODE_RANGE_NUM(mbr
);
4066 for (i
= 0; i
< n
; i
++) {
4067 for (j
= ONIGENC_CODE_RANGE_FROM(mbr
, i
);
4068 j
<= ONIGENC_CODE_RANGE_TO(mbr
, i
); j
++) {
4070 if (j
> ONIGENC_CODE_RANGE_FROM(mbr
, i
)) {
4071 r
= add_code_range_to_buf(&(cc
->mbuf
), env
, j
,
4072 ONIGENC_CODE_RANGE_TO(mbr
, i
));
4073 if (r
!= 0) return r
;
4079 BITSET_SET_BIT_CHKDUP(cc
->bs
, j
);
4084 for ( ; i
< n
; i
++) {
4085 r
= add_code_range_to_buf(&(cc
->mbuf
), env
,
4086 ONIGENC_CODE_RANGE_FROM(mbr
, i
),
4087 ONIGENC_CODE_RANGE_TO(mbr
, i
));
4088 if (r
!= 0) return r
;
4092 OnigCodePoint prev
= 0;
4094 for (i
= 0; i
< n
; i
++) {
4096 j
< ONIGENC_CODE_RANGE_FROM(mbr
, i
); j
++) {
4100 BITSET_SET_BIT_CHKDUP(cc
->bs
, j
);
4102 prev
= ONIGENC_CODE_RANGE_TO(mbr
, i
) + 1;
4104 for (j
= prev
; j
< sb_out
; j
++) {
4105 BITSET_SET_BIT_CHKDUP(cc
->bs
, j
);
4111 for (i
= 0; i
< n
; i
++) {
4112 if (prev
< ONIGENC_CODE_RANGE_FROM(mbr
, i
)) {
4113 r
= add_code_range_to_buf(&(cc
->mbuf
), env
, prev
,
4114 ONIGENC_CODE_RANGE_FROM(mbr
, i
) - 1);
4115 if (r
!= 0) return r
;
4117 prev
= ONIGENC_CODE_RANGE_TO(mbr
, i
) + 1;
4119 if (prev
< 0x7fffffff) {
4120 r
= add_code_range_to_buf(&(cc
->mbuf
), env
, prev
, 0x7fffffff);
4121 if (r
!= 0) return r
;
4129 add_ctype_to_cc(CClassNode
* cc
, int ctype
, int not, int ascii_range
, ScanEnv
* env
)
4133 const OnigCodePoint
*ranges
;
4134 OnigCodePoint sb_out
;
4135 OnigEncoding enc
= env
->enc
;
4137 r
= ONIGENC_GET_CTYPE_CODE_RANGE(enc
, ctype
, &sb_out
, &ranges
);
4141 initialize_cclass(&ccwork
);
4142 r
= add_ctype_to_cc_by_range(&ccwork
, ctype
, not, env
, sb_out
,
4146 r
= add_code_range_to_buf0(&(ccwork
.mbuf
), env
, 0x80, ONIG_LAST_CODE_POINT
, FALSE
);
4150 initialize_cclass(&ccascii
);
4151 if (ONIGENC_MBC_MINLEN(env
->enc
) > 1) {
4152 r
= add_code_range(&(ccascii
.mbuf
), env
, 0x00, 0x7F);
4155 bitset_set_range(env
, ccascii
.bs
, 0x00, 0x7F);
4159 r
= and_cclass(&ccwork
, &ccascii
, env
);
4161 if (IS_NOT_NULL(ccascii
.mbuf
)) bbuf_free(ccascii
.mbuf
);
4164 r
= or_cclass(cc
, &ccwork
, env
);
4166 if (IS_NOT_NULL(ccwork
.mbuf
)) bbuf_free(ccwork
.mbuf
);
4170 r
= add_ctype_to_cc_by_range(cc
, ctype
, not, env
, sb_out
, ranges
);
4174 else if (r
!= ONIG_NO_SUPPORT_CONFIG
) {
4178 maxcode
= ascii_range
? 0x80 : SINGLE_BYTE_SIZE
;
4181 case ONIGENC_CTYPE_ALPHA
:
4182 case ONIGENC_CTYPE_BLANK
:
4183 case ONIGENC_CTYPE_CNTRL
:
4184 case ONIGENC_CTYPE_DIGIT
:
4185 case ONIGENC_CTYPE_LOWER
:
4186 case ONIGENC_CTYPE_PUNCT
:
4187 case ONIGENC_CTYPE_SPACE
:
4188 case ONIGENC_CTYPE_UPPER
:
4189 case ONIGENC_CTYPE_XDIGIT
:
4190 case ONIGENC_CTYPE_ASCII
:
4191 case ONIGENC_CTYPE_ALNUM
:
4193 for (c
= 0; c
< SINGLE_BYTE_SIZE
; c
++) {
4194 if (! ONIGENC_IS_CODE_CTYPE(enc
, (OnigCodePoint
)c
, ctype
))
4195 BITSET_SET_BIT_CHKDUP(cc
->bs
, c
);
4197 ADD_ALL_MULTI_BYTE_RANGE(enc
, cc
->mbuf
);
4200 for (c
= 0; c
< SINGLE_BYTE_SIZE
; c
++) {
4201 if (ONIGENC_IS_CODE_CTYPE(enc
, (OnigCodePoint
)c
, ctype
))
4202 BITSET_SET_BIT_CHKDUP(cc
->bs
, c
);
4207 case ONIGENC_CTYPE_GRAPH
:
4208 case ONIGENC_CTYPE_PRINT
:
4210 for (c
= 0; c
< SINGLE_BYTE_SIZE
; c
++) {
4211 if (! ONIGENC_IS_CODE_CTYPE(enc
, (OnigCodePoint
)c
, ctype
)
4213 BITSET_SET_BIT_CHKDUP(cc
->bs
, c
);
4216 ADD_ALL_MULTI_BYTE_RANGE(enc
, cc
->mbuf
);
4219 for (c
= 0; c
< maxcode
; c
++) {
4220 if (ONIGENC_IS_CODE_CTYPE(enc
, (OnigCodePoint
)c
, ctype
))
4221 BITSET_SET_BIT_CHKDUP(cc
->bs
, c
);
4224 ADD_ALL_MULTI_BYTE_RANGE(enc
, cc
->mbuf
);
4228 case ONIGENC_CTYPE_WORD
:
4230 for (c
= 0; c
< maxcode
; c
++) {
4231 if (ONIGENC_IS_CODE_WORD(enc
, c
)) BITSET_SET_BIT_CHKDUP(cc
->bs
, c
);
4234 ADD_ALL_MULTI_BYTE_RANGE(enc
, cc
->mbuf
);
4237 for (c
= 0; c
< SINGLE_BYTE_SIZE
; c
++) {
4238 if ((ONIGENC_CODE_TO_MBCLEN(enc
, c
) > 0) /* check invalid code point */
4239 && (! ONIGENC_IS_CODE_WORD(enc
, c
) || c
>= maxcode
))
4240 BITSET_SET_BIT_CHKDUP(cc
->bs
, c
);
4243 ADD_ALL_MULTI_BYTE_RANGE(enc
, cc
->mbuf
);
4248 return ONIGERR_PARSER_BUG
;
4256 parse_posix_bracket(CClassNode
* cc
, CClassNode
* asc_cc
,
4257 UChar
** src
, UChar
* end
, ScanEnv
* env
)
4259 #define POSIX_BRACKET_CHECK_LIMIT_LENGTH 20
4260 #define POSIX_BRACKET_NAME_MIN_LEN 4
4262 static const PosixBracketEntryType PBS
[] = {
4263 POSIX_BRACKET_ENTRY_INIT("alnum", ONIGENC_CTYPE_ALNUM
),
4264 POSIX_BRACKET_ENTRY_INIT("alpha", ONIGENC_CTYPE_ALPHA
),
4265 POSIX_BRACKET_ENTRY_INIT("blank", ONIGENC_CTYPE_BLANK
),
4266 POSIX_BRACKET_ENTRY_INIT("cntrl", ONIGENC_CTYPE_CNTRL
),
4267 POSIX_BRACKET_ENTRY_INIT("digit", ONIGENC_CTYPE_DIGIT
),
4268 POSIX_BRACKET_ENTRY_INIT("graph", ONIGENC_CTYPE_GRAPH
),
4269 POSIX_BRACKET_ENTRY_INIT("lower", ONIGENC_CTYPE_LOWER
),
4270 POSIX_BRACKET_ENTRY_INIT("print", ONIGENC_CTYPE_PRINT
),
4271 POSIX_BRACKET_ENTRY_INIT("punct", ONIGENC_CTYPE_PUNCT
),
4272 POSIX_BRACKET_ENTRY_INIT("space", ONIGENC_CTYPE_SPACE
),
4273 POSIX_BRACKET_ENTRY_INIT("upper", ONIGENC_CTYPE_UPPER
),
4274 POSIX_BRACKET_ENTRY_INIT("xdigit", ONIGENC_CTYPE_XDIGIT
),
4275 POSIX_BRACKET_ENTRY_INIT("ascii", ONIGENC_CTYPE_ASCII
),
4276 POSIX_BRACKET_ENTRY_INIT("word", ONIGENC_CTYPE_WORD
),
4279 const PosixBracketEntryType
*pb
;
4283 OnigEncoding enc
= env
->enc
;
4286 if (PPEEK_IS('^')) {
4293 if (onigenc_strlen(enc
, p
, end
) < POSIX_BRACKET_NAME_MIN_LEN
+ 3)
4294 goto not_posix_bracket
;
4296 ascii_range
= IS_ASCII_RANGE(env
->option
) &&
4297 ! IS_POSIX_BRACKET_ALL_RANGE(env
->option
);
4298 for (pb
= PBS
; pb
< PBS
+ numberof(PBS
); pb
++) {
4299 if (onigenc_with_ascii_strncmp(enc
, p
, end
, pb
->name
, pb
->len
) == 0) {
4300 p
= (UChar
* )onigenc_step(enc
, p
, end
, pb
->len
);
4301 if (onigenc_with_ascii_strncmp(enc
, p
, end
, (UChar
* )":]", 2) != 0)
4302 return ONIGERR_INVALID_POSIX_BRACKET_TYPE
;
4304 r
= add_ctype_to_cc(cc
, pb
->ctype
, not, ascii_range
, env
);
4305 if (r
!= 0) return r
;
4307 if (IS_NOT_NULL(asc_cc
)) {
4308 if (pb
->ctype
!= ONIGENC_CTYPE_WORD
&&
4309 pb
->ctype
!= ONIGENC_CTYPE_ASCII
&&
4311 r
= add_ctype_to_cc(asc_cc
, pb
->ctype
, not, ascii_range
, env
);
4312 if (r
!= 0) return r
;
4324 while (!PEND
&& ((c
= PPEEK
) != ':') && c
!= ']') {
4326 if (++i
> POSIX_BRACKET_CHECK_LIMIT_LENGTH
) break;
4328 if (c
== ':' && ! PEND
) {
4333 return ONIGERR_INVALID_POSIX_BRACKET_TYPE
;
4337 return 1; /* 1: is not POSIX bracket, but no error. */
4341 fetch_char_property_to_ctype(UChar
** src
, UChar
* end
, ScanEnv
* env
)
4345 OnigEncoding enc
= env
->enc
;
4346 UChar
*prev
, *start
, *p
= *src
;
4348 r
= ONIGERR_INVALID_CHAR_PROPERTY_NAME
;
4355 r
= ONIGENC_PROPERTY_NAME_TO_CTYPE(enc
, start
, prev
);
4361 else if (c
== '(' || c
== ')' || c
== '{' || c
== '|') {
4366 onig_scan_env_set_error_string(env
, r
, *src
, prev
);
4370 static int cclass_case_fold(Node
** np
, CClassNode
* cc
, CClassNode
* asc_cc
, ScanEnv
* env
);
4373 parse_char_property(Node
** np
, OnigToken
* tok
, UChar
** src
, UChar
* end
,
4379 ctype
= fetch_char_property_to_ctype(src
, end
, env
);
4380 if (ctype
< 0) return ctype
;
4382 *np
= node_new_cclass();
4383 CHECK_NULL_RETURN_MEMERR(*np
);
4385 r
= add_ctype_to_cc(cc
, ctype
, 0, 0, env
);
4386 if (r
!= 0) return r
;
4387 if (tok
->u
.prop
.not != 0) NCCLASS_SET_NOT(cc
);
4389 if (IS_IGNORECASE(env
->option
)) {
4390 if (ctype
!= ONIGENC_CTYPE_ASCII
)
4391 r
= cclass_case_fold(np
, cc
, cc
, env
);
4411 next_state_class(CClassNode
* cc
, CClassNode
* asc_cc
,
4412 OnigCodePoint
* vs
, enum CCVALTYPE
* type
,
4413 enum CCSTATE
* state
, ScanEnv
* env
)
4417 if (*state
== CCS_RANGE
)
4418 return ONIGERR_CHAR_CLASS_VALUE_AT_END_OF_RANGE
;
4420 if (*state
== CCS_VALUE
&& *type
!= CCV_CLASS
) {
4421 if (*type
== CCV_SB
) {
4422 BITSET_SET_BIT_CHKDUP(cc
->bs
, (int )(*vs
));
4423 if (IS_NOT_NULL(asc_cc
))
4424 BITSET_SET_BIT(asc_cc
->bs
, (int )(*vs
));
4426 else if (*type
== CCV_CODE_POINT
) {
4427 r
= add_code_range(&(cc
->mbuf
), env
, *vs
, *vs
);
4428 if (r
< 0) return r
;
4429 if (IS_NOT_NULL(asc_cc
)) {
4430 r
= add_code_range0(&(asc_cc
->mbuf
), env
, *vs
, *vs
, 0);
4431 if (r
< 0) return r
;
4442 next_state_val(CClassNode
* cc
, CClassNode
* asc_cc
,
4443 OnigCodePoint
*from
, OnigCodePoint to
,
4444 int* from_israw
, int to_israw
,
4445 enum CCVALTYPE intype
, enum CCVALTYPE
* type
,
4446 enum CCSTATE
* state
, ScanEnv
* env
)
4452 if (*type
== CCV_SB
) {
4453 BITSET_SET_BIT_CHKDUP(cc
->bs
, (int )(*from
));
4454 if (IS_NOT_NULL(asc_cc
))
4455 BITSET_SET_BIT(asc_cc
->bs
, (int )(*from
));
4457 else if (*type
== CCV_CODE_POINT
) {
4458 r
= add_code_range(&(cc
->mbuf
), env
, *from
, *from
);
4459 if (r
< 0) return r
;
4460 if (IS_NOT_NULL(asc_cc
)) {
4461 r
= add_code_range0(&(asc_cc
->mbuf
), env
, *from
, *from
, 0);
4462 if (r
< 0) return r
;
4468 if (intype
== *type
) {
4469 if (intype
== CCV_SB
) {
4470 if (*from
> 0xff || to
> 0xff)
4471 return ONIGERR_INVALID_CODE_POINT_VALUE
;
4474 if (IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_ALLOW_EMPTY_RANGE_IN_CC
))
4477 return ONIGERR_EMPTY_RANGE_IN_CHAR_CLASS
;
4479 bitset_set_range(env
, cc
->bs
, (int )*from
, (int )to
);
4480 if (IS_NOT_NULL(asc_cc
))
4481 bitset_set_range(env
, asc_cc
->bs
, (int )*from
, (int )to
);
4484 r
= add_code_range(&(cc
->mbuf
), env
, *from
, to
);
4485 if (r
< 0) return r
;
4486 if (IS_NOT_NULL(asc_cc
)) {
4487 r
= add_code_range0(&(asc_cc
->mbuf
), env
, *from
, to
, 0);
4488 if (r
< 0) return r
;
4494 if (IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_ALLOW_EMPTY_RANGE_IN_CC
))
4497 return ONIGERR_EMPTY_RANGE_IN_CHAR_CLASS
;
4499 bitset_set_range(env
, cc
->bs
, (int )*from
, (int )(to
< 0xff ? to
: 0xff));
4500 r
= add_code_range(&(cc
->mbuf
), env
, (OnigCodePoint
)*from
, to
);
4501 if (r
< 0) return r
;
4502 if (IS_NOT_NULL(asc_cc
)) {
4503 bitset_set_range(env
, asc_cc
->bs
, (int )*from
, (int )(to
< 0xff ? to
: 0xff));
4504 r
= add_code_range0(&(asc_cc
->mbuf
), env
, (OnigCodePoint
)*from
, to
, 0);
4505 if (r
< 0) return r
;
4509 *state
= CCS_COMPLETE
;
4521 *from_israw
= to_israw
;
4528 code_exist_check(OnigCodePoint c
, UChar
* from
, UChar
* end
, int ignore_escaped
,
4533 OnigEncoding enc
= env
->enc
;
4538 if (ignore_escaped
&& in_esc
) {
4543 if (code
== c
) return 1;
4544 if (code
== MC_ESC(env
->syntax
)) in_esc
= 1;
4551 parse_char_class(Node
** np
, Node
** asc_np
, OnigToken
* tok
, UChar
** src
, UChar
* end
,
4554 int r
, neg
, len
, fetched
, and_start
;
4555 OnigCodePoint v
, vs
;
4559 CClassNode
*cc
, *prev_cc
;
4560 CClassNode
*asc_cc
, *asc_prev_cc
;
4561 CClassNode work_cc
, asc_work_cc
;
4564 enum CCVALTYPE val_type
, in_type
;
4565 int val_israw
, in_israw
;
4567 *np
= *asc_np
= NULL_NODE
;
4569 if (env
->parse_depth
> ParseDepthLimit
)
4570 return ONIGERR_PARSE_DEPTH_LIMIT_OVER
;
4571 prev_cc
= asc_prev_cc
= (CClassNode
* )NULL
;
4572 r
= fetch_token_in_cc(tok
, src
, end
, env
);
4573 if (r
== TK_CHAR
&& tok
->u
.c
== '^' && tok
->escaped
== 0) {
4575 r
= fetch_token_in_cc(tok
, src
, end
, env
);
4581 if (r
< 0) return r
;
4582 if (r
== TK_CC_CLOSE
) {
4583 if (! code_exist_check((OnigCodePoint
)']',
4584 *src
, env
->pattern_end
, 1, env
))
4585 return ONIGERR_EMPTY_CHAR_CLASS
;
4587 CC_ESC_WARN(env
, (UChar
* )"]");
4588 r
= tok
->type
= TK_CHAR
; /* allow []...] */
4591 *np
= node
= node_new_cclass();
4592 CHECK_NULL_RETURN_MEMERR(node
);
4595 if (IS_IGNORECASE(env
->option
)) {
4596 *asc_np
= asc_node
= node_new_cclass();
4597 CHECK_NULL_RETURN_MEMERR(asc_node
);
4598 asc_cc
= NCCLASS(asc_node
);
4601 asc_node
= NULL_NODE
;
4608 while (r
!= TK_CC_CLOSE
) {
4612 if ((tok
->u
.code
>= SINGLE_BYTE_SIZE
) ||
4613 (len
= ONIGENC_CODE_TO_MBCLEN(env
->enc
, tok
->u
.c
)) > 1) {
4614 in_type
= CCV_CODE_POINT
;
4624 v
= (OnigCodePoint
)tok
->u
.c
;
4630 /* tok->base != 0 : octal or hexadec. */
4631 if (! ONIGENC_IS_SINGLEBYTE(env
->enc
) && tok
->base
!= 0) {
4632 UChar buf
[ONIGENC_CODE_TO_MBC_MAXLEN
];
4633 UChar
* bufe
= buf
+ ONIGENC_CODE_TO_MBC_MAXLEN
;
4635 int i
, base
= tok
->base
;
4637 buf
[0] = (UChar
)tok
->u
.c
;
4638 for (i
= 1; i
< ONIGENC_MBC_MAXLEN(env
->enc
); i
++) {
4639 r
= fetch_token_in_cc(tok
, &p
, end
, env
);
4640 if (r
< 0) goto err
;
4641 if (r
!= TK_RAW_BYTE
|| tok
->base
!= base
) {
4645 buf
[i
] = (UChar
)tok
->u
.c
;
4648 if (i
< ONIGENC_MBC_MINLEN(env
->enc
)) {
4649 r
= ONIGERR_TOO_SHORT_MULTI_BYTE_STRING
;
4653 len
= enclen(env
->enc
, buf
, buf
+ i
);
4655 r
= ONIGERR_TOO_SHORT_MULTI_BYTE_STRING
;
4658 else if (i
> len
) { /* fetch back */
4660 for (i
= 1; i
< len
; i
++) {
4661 (void)fetch_token_in_cc(tok
, &p
, end
, env
);
4662 /* no need to check the return value (already checked above) */
4668 v
= (OnigCodePoint
)buf
[0];
4672 v
= ONIGENC_MBC_TO_CODE(env
->enc
, buf
, bufe
);
4673 in_type
= CCV_CODE_POINT
;
4677 v
= (OnigCodePoint
)tok
->u
.c
;
4689 len
= ONIGENC_CODE_TO_MBCLEN(env
->enc
, v
);
4694 in_type
= (len
== 1 ? CCV_SB
: CCV_CODE_POINT
);
4696 r
= next_state_val(cc
, asc_cc
, &vs
, v
, &val_israw
, in_israw
, in_type
, &val_type
,
4698 if (r
!= 0) goto err
;
4701 case TK_POSIX_BRACKET_OPEN
:
4702 r
= parse_posix_bracket(cc
, asc_cc
, &p
, end
, env
);
4703 if (r
< 0) goto err
;
4704 if (r
== 1) { /* is not POSIX bracket */
4705 CC_ESC_WARN(env
, (UChar
* )"[");
4707 v
= (OnigCodePoint
)tok
->u
.c
;
4715 r
= add_ctype_to_cc(cc
, tok
->u
.prop
.ctype
, tok
->u
.prop
.not,
4716 IS_ASCII_RANGE(env
->option
), env
);
4717 if (r
!= 0) return r
;
4718 if (IS_NOT_NULL(asc_cc
)) {
4719 if (tok
->u
.prop
.ctype
!= ONIGENC_CTYPE_WORD
)
4720 r
= add_ctype_to_cc(asc_cc
, tok
->u
.prop
.ctype
, tok
->u
.prop
.not,
4721 IS_ASCII_RANGE(env
->option
), env
);
4722 if (r
!= 0) return r
;
4726 r
= next_state_class(cc
, asc_cc
, &vs
, &val_type
, &state
, env
);
4727 if (r
!= 0) goto err
;
4730 case TK_CHAR_PROPERTY
:
4734 ctype
= fetch_char_property_to_ctype(&p
, end
, env
);
4735 if (ctype
< 0) return ctype
;
4736 r
= add_ctype_to_cc(cc
, ctype
, tok
->u
.prop
.not, 0, env
);
4737 if (r
!= 0) return r
;
4738 if (IS_NOT_NULL(asc_cc
)) {
4739 if (ctype
!= ONIGENC_CTYPE_ASCII
)
4740 r
= add_ctype_to_cc(asc_cc
, ctype
, tok
->u
.prop
.not, 0, env
);
4741 if (r
!= 0) return r
;
4748 if (state
== CCS_VALUE
) {
4749 r
= fetch_token_in_cc(tok
, &p
, end
, env
);
4750 if (r
< 0) goto err
;
4752 if (r
== TK_CC_CLOSE
) { /* allow [x-] */
4754 v
= (OnigCodePoint
)'-';
4758 else if (r
== TK_CC_AND
) {
4759 CC_ESC_WARN(env
, (UChar
* )"-");
4763 if (val_type
== CCV_CLASS
) {
4764 r
= ONIGERR_UNMATCHED_RANGE_SPECIFIER_IN_CHAR_CLASS
;
4770 else if (state
== CCS_START
) {
4771 /* [-xa] is allowed */
4772 v
= (OnigCodePoint
)tok
->u
.c
;
4775 r
= fetch_token_in_cc(tok
, &p
, end
, env
);
4776 if (r
< 0) goto err
;
4778 /* [--x] or [a&&-x] is warned. */
4779 if (r
== TK_CC_RANGE
|| and_start
!= 0)
4780 CC_ESC_WARN(env
, (UChar
* )"-");
4784 else if (state
== CCS_RANGE
) {
4785 CC_ESC_WARN(env
, (UChar
* )"-");
4786 goto sb_char
; /* [!--x] is allowed */
4788 else { /* CCS_COMPLETE */
4789 r
= fetch_token_in_cc(tok
, &p
, end
, env
);
4790 if (r
< 0) goto err
;
4792 if (r
== TK_CC_CLOSE
) goto range_end_val
; /* allow [a-b-] */
4793 else if (r
== TK_CC_AND
) {
4794 CC_ESC_WARN(env
, (UChar
* )"-");
4798 if (IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_ALLOW_DOUBLE_RANGE_OP_IN_CC
)) {
4799 CC_ESC_WARN(env
, (UChar
* )"-");
4800 goto range_end_val
; /* [0-9-a] is allowed as [0-9\-a] */
4802 r
= ONIGERR_UNMATCHED_RANGE_SPECIFIER_IN_CHAR_CLASS
;
4807 case TK_CC_CC_OPEN
: /* [ */
4809 Node
*anode
, *aasc_node
;
4812 r
= parse_char_class(&anode
, &aasc_node
, tok
, &p
, end
, env
);
4814 acc
= NCCLASS(anode
);
4815 r
= or_cclass(cc
, acc
, env
);
4817 if (r
== 0 && IS_NOT_NULL(aasc_node
)) {
4818 acc
= NCCLASS(aasc_node
);
4819 r
= or_cclass(asc_cc
, acc
, env
);
4821 onig_node_free(anode
);
4822 onig_node_free(aasc_node
);
4823 if (r
!= 0) goto err
;
4827 case TK_CC_AND
: /* && */
4829 if (state
== CCS_VALUE
) {
4830 r
= next_state_val(cc
, asc_cc
, &vs
, 0, &val_israw
, 0, val_type
,
4831 &val_type
, &state
, env
);
4832 if (r
!= 0) goto err
;
4834 /* initialize local variables */
4838 if (IS_NOT_NULL(prev_cc
)) {
4839 r
= and_cclass(prev_cc
, cc
, env
);
4840 if (r
!= 0) goto err
;
4841 bbuf_free(cc
->mbuf
);
4842 if (IS_NOT_NULL(asc_cc
)) {
4843 r
= and_cclass(asc_prev_cc
, asc_cc
, env
);
4844 if (r
!= 0) goto err
;
4845 bbuf_free(asc_cc
->mbuf
);
4851 if (IS_NOT_NULL(asc_cc
)) {
4852 asc_prev_cc
= asc_cc
;
4853 asc_cc
= &asc_work_cc
;
4856 initialize_cclass(cc
);
4857 if (IS_NOT_NULL(asc_cc
))
4858 initialize_cclass(asc_cc
);
4863 r
= ONIGERR_PREMATURE_END_OF_CHAR_CLASS
;
4867 r
= ONIGERR_PARSER_BUG
;
4875 r
= fetch_token_in_cc(tok
, &p
, end
, env
);
4876 if (r
< 0) goto err
;
4880 if (state
== CCS_VALUE
) {
4881 r
= next_state_val(cc
, asc_cc
, &vs
, 0, &val_israw
, 0, val_type
,
4882 &val_type
, &state
, env
);
4883 if (r
!= 0) goto err
;
4886 if (IS_NOT_NULL(prev_cc
)) {
4887 r
= and_cclass(prev_cc
, cc
, env
);
4888 if (r
!= 0) goto err
;
4889 bbuf_free(cc
->mbuf
);
4891 if (IS_NOT_NULL(asc_cc
)) {
4892 r
= and_cclass(asc_prev_cc
, asc_cc
, env
);
4893 if (r
!= 0) goto err
;
4894 bbuf_free(asc_cc
->mbuf
);
4895 asc_cc
= asc_prev_cc
;
4900 NCCLASS_SET_NOT(cc
);
4901 if (IS_NOT_NULL(asc_cc
))
4902 NCCLASS_SET_NOT(asc_cc
);
4905 NCCLASS_CLEAR_NOT(cc
);
4906 if (IS_NOT_NULL(asc_cc
))
4907 NCCLASS_CLEAR_NOT(asc_cc
);
4909 if (IS_NCCLASS_NOT(cc
) &&
4910 IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_NOT_NEWLINE_IN_NEGATIVE_CC
)) {
4913 is_empty
= (IS_NULL(cc
->mbuf
) ? 1 : 0);
4915 BITSET_IS_EMPTY(cc
->bs
, is_empty
);
4917 if (is_empty
== 0) {
4918 #define NEWLINE_CODE 0x0a
4920 if (ONIGENC_IS_CODE_NEWLINE(env
->enc
, NEWLINE_CODE
)) {
4921 if (ONIGENC_CODE_TO_MBCLEN(env
->enc
, NEWLINE_CODE
) == 1)
4922 BITSET_SET_BIT_CHKDUP(cc
->bs
, NEWLINE_CODE
);
4924 r
= add_code_range(&(cc
->mbuf
), env
, NEWLINE_CODE
, NEWLINE_CODE
);
4925 if (r
< 0) goto err
;
4935 if (cc
!= NCCLASS(*np
))
4936 bbuf_free(cc
->mbuf
);
4937 if (IS_NOT_NULL(asc_cc
) && (asc_cc
!= NCCLASS(*asc_np
)))
4938 bbuf_free(asc_cc
->mbuf
);
4942 static int parse_subexp(Node
** top
, OnigToken
* tok
, int term
,
4943 UChar
** src
, UChar
* end
, ScanEnv
* env
);
4946 parse_enclose(Node
** np
, OnigToken
* tok
, int term
, UChar
** src
, UChar
* end
,
4950 Node
*target
, *work1
= NULL
, *work2
= NULL
;
4951 OnigOptionType option
;
4953 OnigEncoding enc
= env
->enc
;
4955 #ifdef USE_NAMED_GROUP
4963 if (PEND
) return ONIGERR_END_PATTERN_WITH_UNMATCHED_PARENTHESIS
;
4965 option
= env
->option
;
4966 if (PPEEK_IS('?') &&
4967 IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_QMARK_GROUP_EFFECT
)) {
4969 if (PEND
) return ONIGERR_END_PATTERN_IN_GROUP
;
4973 case ':': /* (?:...) grouping only */
4975 r
= fetch_token(tok
, &p
, end
, env
);
4976 if (r
< 0) return r
;
4977 r
= parse_subexp(np
, tok
, term
, &p
, end
, env
);
4978 if (r
< 0) return r
;
4980 return 1; /* group */
4984 *np
= onig_node_new_anchor(ANCHOR_PREC_READ
);
4986 case '!': /* preceding read */
4987 *np
= onig_node_new_anchor(ANCHOR_PREC_READ_NOT
);
4989 case '>': /* (?>...) stop backtrack */
4990 *np
= node_new_enclose(ENCLOSE_STOP_BACKTRACK
);
4992 case '~': /* (?~...) absent operator */
4993 if (IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_QMARK_TILDE_ABSENT
)) {
4994 *np
= node_new_enclose(ENCLOSE_ABSENT
);
4997 return ONIGERR_UNDEFINED_GROUP_OPTION
;
5001 #ifdef USE_NAMED_GROUP
5003 if (IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_QMARK_LT_NAMED_GROUP
)) {
5007 return ONIGERR_UNDEFINED_GROUP_OPTION
;
5010 # ifdef USE_CAPITAL_P_NAMED_GROUP
5011 case 'P': /* (?P<name>...) */
5013 IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_QMARK_CAPITAL_P_NAMED_GROUP
)) {
5015 if (c
== '<') goto named_group1
;
5017 return ONIGERR_UNDEFINED_GROUP_OPTION
;
5022 case '<': /* look behind (?<=...), (?<!...) */
5023 if (PEND
) return ONIGERR_END_PATTERN_WITH_UNMATCHED_PARENTHESIS
;
5026 *np
= onig_node_new_anchor(ANCHOR_LOOK_BEHIND
);
5028 *np
= onig_node_new_anchor(ANCHOR_LOOK_BEHIND_NOT
);
5029 #ifdef USE_NAMED_GROUP
5030 else { /* (?<name>...) */
5031 if (IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_QMARK_LT_NAMED_GROUP
)) {
5041 # ifdef USE_CAPTURE_HISTORY
5045 r
= fetch_name((OnigCodePoint
)c
, &p
, end
, &name_end
, env
, &num
, 0);
5046 if (r
< 0) return r
;
5048 num
= scan_env_add_mem_entry(env
);
5049 if (num
< 0) return num
;
5050 if (list_capture
!= 0 && num
>= (int )BIT_STATUS_BITS_NUM
)
5051 return ONIGERR_GROUP_NUMBER_OVER_FOR_CAPTURE_HISTORY
;
5053 r
= name_add(env
->reg
, name
, name_end
, num
, env
);
5054 if (r
!= 0) return r
;
5055 *np
= node_new_enclose_memory(env
->option
, 1);
5056 CHECK_NULL_RETURN_MEMERR(*np
);
5057 NENCLOSE(*np
)->regnum
= num
;
5058 if (list_capture
!= 0)
5059 BIT_STATUS_ON_AT_SIMPLE(env
->capture_history
, num
);
5063 return ONIGERR_UNDEFINED_GROUP_OPTION
;
5068 return ONIGERR_UNDEFINED_GROUP_OPTION
;
5073 #ifdef USE_CAPTURE_HISTORY
5075 if (IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_ATMARK_CAPTURE_HISTORY
)) {
5076 # ifdef USE_NAMED_GROUP
5078 IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_QMARK_LT_NAMED_GROUP
)) {
5080 if (c
== '<' || c
== '\'') {
5082 goto named_group2
; /* (?@<name>...) */
5087 *np
= node_new_enclose_memory(env
->option
, 0);
5088 CHECK_NULL_RETURN_MEMERR(*np
);
5089 num
= scan_env_add_mem_entry(env
);
5090 if (num
< 0) return num
;
5091 if (num
>= (int )BIT_STATUS_BITS_NUM
)
5092 return ONIGERR_GROUP_NUMBER_OVER_FOR_CAPTURE_HISTORY
;
5094 NENCLOSE(*np
)->regnum
= num
;
5095 BIT_STATUS_ON_AT_SIMPLE(env
->capture_history
, num
);
5098 return ONIGERR_UNDEFINED_GROUP_OPTION
;
5101 #endif /* USE_CAPTURE_HISTORY */
5103 case '(': /* conditional expression: (?(cond)yes), (?(cond)yes|no) */
5105 IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_QMARK_LPAREN_CONDITION
)) {
5109 if (ONIGENC_IS_CODE_DIGIT(enc
, c
)) { /* (n) */
5111 r
= fetch_name((OnigCodePoint
)'(', &p
, end
, &name_end
, env
, &num
, 1);
5112 if (r
< 0) return r
;
5114 /* Relative number is not currently supported. (same as Perl) */
5116 num
= BACKREF_REL_TO_ABS(num
, env
);
5118 return ONIGERR_INVALID_BACKREF
;
5121 if (IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_STRICT_CHECK_BACKREF
)) {
5122 if (num
> env
->num_mem
||
5123 IS_NULL(SCANENV_MEM_NODES(env
)[num
]))
5124 return ONIGERR_INVALID_BACKREF
;
5127 #ifdef USE_NAMED_GROUP
5128 else if (c
== '<' || c
== '\'') { /* (<name>), ('name') */
5130 r
= fetch_named_backref_token(c
, tok
, &p
, end
, env
);
5131 if (r
< 0) return r
;
5132 if (!PPEEK_IS(')')) return ONIGERR_UNDEFINED_GROUP_OPTION
;
5135 if (IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_USE_LEFT_MOST_NAMED_GROUP
)) {
5136 num
= tok
->u
.backref
.ref1
;
5140 * Use left most named group for now. This is the same as Perl.
5141 * However this should use the same strategy as normal back-
5142 * references on Ruby syntax; search right to left. */
5143 int len
= tok
->u
.backref
.num
;
5144 num
= len
> 1 ? tok
->u
.backref
.refs
[0] : tok
->u
.backref
.ref1
;
5149 return ONIGERR_INVALID_CONDITION_PATTERN
;
5150 *np
= node_new_enclose(ENCLOSE_CONDITION
);
5151 CHECK_NULL_RETURN_MEMERR(*np
);
5152 NENCLOSE(*np
)->regnum
= num
;
5153 if (IS_NOT_NULL(name
)) NENCLOSE(*np
)->state
|= NST_NAME_REF
;
5156 return ONIGERR_UNDEFINED_GROUP_OPTION
;
5160 case '|': /* branch reset: (?|...) */
5161 if (IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_QMARK_VBAR_BRANCH_RESET
)) {
5165 return ONIGERR_UNDEFINED_GROUP_OPTION
;
5169 case '^': /* loads default options */
5170 if (!PEND
&& IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_OPTION_PERL
)) {
5172 ONOFF(option
, ONIG_OPTION_ASCII_RANGE
, 1);
5173 ONOFF(option
, ONIG_OPTION_IGNORECASE
, 1);
5174 ONOFF(option
, ONIG_OPTION_SINGLELINE
, 0);
5175 ONOFF(option
, ONIG_OPTION_MULTILINE
, 1);
5176 ONOFF(option
, ONIG_OPTION_EXTEND
, 1);
5180 else if (!PEND
&& IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_OPTION_RUBY
)) {
5182 ONOFF(option
, ONIG_OPTION_ASCII_RANGE
, 0);
5183 ONOFF(option
, ONIG_OPTION_POSIX_BRACKET_ALL_RANGE
, 0);
5184 ONOFF(option
, ONIG_OPTION_WORD_BOUND_ALL_RANGE
, 0);
5185 ONOFF(option
, ONIG_OPTION_IGNORECASE
, 1);
5186 ONOFF(option
, ONIG_OPTION_MULTILINE
, 1);
5187 ONOFF(option
, ONIG_OPTION_EXTEND
, 1);
5192 return ONIGERR_UNDEFINED_GROUP_OPTION
;
5195 #ifdef USE_POSIXLINE_OPTION
5198 case '-': case 'i': case 'm': case 's': case 'x':
5199 case 'a': case 'd': case 'l': case 'u':
5209 case '-': neg
= 1; break;
5210 case 'x': ONOFF(option
, ONIG_OPTION_EXTEND
, neg
); break;
5211 case 'i': ONOFF(option
, ONIG_OPTION_IGNORECASE
, neg
); break;
5213 if (IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_OPTION_PERL
)) {
5214 ONOFF(option
, ONIG_OPTION_MULTILINE
, neg
);
5217 return ONIGERR_UNDEFINED_GROUP_OPTION
;
5221 if (IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_OPTION_PERL
)) {
5222 ONOFF(option
, ONIG_OPTION_SINGLELINE
, (neg
== 0 ? 1 : 0));
5224 else if (IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_OPTION_RUBY
)) {
5225 ONOFF(option
, ONIG_OPTION_MULTILINE
, neg
);
5228 return ONIGERR_UNDEFINED_GROUP_OPTION
;
5230 #ifdef USE_POSIXLINE_OPTION
5232 ONOFF(option
, ONIG_OPTION_MULTILINE
|ONIG_OPTION_SINGLELINE
, neg
);
5236 case 'a': /* limits \d, \s, \w and POSIX brackets to ASCII range */
5237 if ((IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_OPTION_PERL
) ||
5238 IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_OPTION_RUBY
)) &&
5240 ONOFF(option
, ONIG_OPTION_ASCII_RANGE
, 0);
5241 ONOFF(option
, ONIG_OPTION_POSIX_BRACKET_ALL_RANGE
, 1);
5242 ONOFF(option
, ONIG_OPTION_WORD_BOUND_ALL_RANGE
, 1);
5245 return ONIGERR_UNDEFINED_GROUP_OPTION
;
5249 if ((IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_OPTION_PERL
) ||
5250 IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_OPTION_RUBY
)) &&
5252 ONOFF(option
, ONIG_OPTION_ASCII_RANGE
, 1);
5253 ONOFF(option
, ONIG_OPTION_POSIX_BRACKET_ALL_RANGE
, 1);
5254 ONOFF(option
, ONIG_OPTION_WORD_BOUND_ALL_RANGE
, 1);
5257 return ONIGERR_UNDEFINED_GROUP_OPTION
;
5261 if (IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_OPTION_PERL
) &&
5263 ONOFF(option
, ONIG_OPTION_ASCII_RANGE
, 1);
5265 else if (IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_OPTION_RUBY
) &&
5267 ONOFF(option
, ONIG_OPTION_ASCII_RANGE
, 0);
5268 ONOFF(option
, ONIG_OPTION_POSIX_BRACKET_ALL_RANGE
, 0);
5269 ONOFF(option
, ONIG_OPTION_WORD_BOUND_ALL_RANGE
, 0);
5272 return ONIGERR_UNDEFINED_GROUP_OPTION
;
5276 if (IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_OPTION_PERL
) && (neg
== 0)) {
5277 ONOFF(option
, ONIG_OPTION_ASCII_RANGE
, 1);
5280 return ONIGERR_UNDEFINED_GROUP_OPTION
;
5284 return ONIGERR_UNDEFINED_GROUP_OPTION
;
5288 *np
= node_new_option(option
);
5289 CHECK_NULL_RETURN_MEMERR(*np
);
5291 return 2; /* option only */
5293 else if (c
== ':') {
5294 OnigOptionType prev
= env
->option
;
5296 env
->option
= option
;
5297 r
= fetch_token(tok
, &p
, end
, env
);
5302 r
= parse_subexp(&target
, tok
, term
, &p
, end
, env
);
5304 if (r
< 0) return r
;
5305 *np
= node_new_option(option
);
5306 CHECK_NULL_RETURN_MEMERR(*np
);
5307 NENCLOSE(*np
)->target
= target
;
5312 if (PEND
) return ONIGERR_END_PATTERN_IN_GROUP
;
5319 return ONIGERR_UNDEFINED_GROUP_OPTION
;
5323 if (ONIG_IS_OPTION_ON(env
->option
, ONIG_OPTION_DONT_CAPTURE_GROUP
))
5326 *np
= node_new_enclose_memory(env
->option
, 0);
5327 CHECK_NULL_RETURN_MEMERR(*np
);
5328 num
= scan_env_add_mem_entry(env
);
5329 if (num
< 0) return num
;
5330 NENCLOSE(*np
)->regnum
= num
;
5333 CHECK_NULL_RETURN_MEMERR(*np
);
5334 r
= fetch_token(tok
, &p
, end
, env
);
5335 if (r
< 0) return r
;
5336 r
= parse_subexp(&target
, tok
, term
, &p
, end
, env
);
5338 onig_node_free(target
);
5342 if (NTYPE(*np
) == NT_ANCHOR
)
5343 NANCHOR(*np
)->target
= target
;
5345 NENCLOSE(*np
)->target
= target
;
5346 if (NENCLOSE(*np
)->type
== ENCLOSE_MEMORY
) {
5347 /* Don't move this to previous of parse_subexp() */
5348 r
= scan_env_set_mem_node(env
, NENCLOSE(*np
)->regnum
, *np
);
5349 if (r
!= 0) return r
;
5351 else if (NENCLOSE(*np
)->type
== ENCLOSE_CONDITION
) {
5352 if (NTYPE(target
) != NT_ALT
) {
5353 /* convert (?(cond)yes) to (?(cond)yes|empty) */
5354 work1
= node_new_empty();
5355 if (IS_NULL(work1
)) goto err
;
5356 work2
= onig_node_new_alt(work1
, NULL_NODE
);
5357 if (IS_NULL(work2
)) goto err
;
5358 work1
= onig_node_new_alt(target
, work2
);
5359 if (IS_NULL(work1
)) goto err
;
5360 NENCLOSE(*np
)->target
= work1
;
5369 onig_node_free(work1
);
5370 onig_node_free(work2
);
5371 onig_node_free(*np
);
5373 return ONIGERR_MEMORY
;
5376 static const char* const PopularQStr
[] = {
5377 "?", "*", "+", "??", "*?", "+?"
5380 static const char* const ReduceQStr
[] = {
5381 "", "", "*", "*?", "??", "+ and ??", "+? and ?"
5385 set_quantifier(Node
* qnode
, Node
* target
, int group
, ScanEnv
* env
)
5390 if (qn
->lower
== 1 && qn
->upper
== 1) {
5394 switch (NTYPE(target
)) {
5397 StrNode
* sn
= NSTR(target
);
5398 if (str_node_can_be_split(sn
, env
->enc
)) {
5399 Node
* n
= str_node_split_last_char(sn
, env
->enc
);
5400 if (IS_NOT_NULL(n
)) {
5409 { /* check redundant double repeat. */
5410 /* verbose warn (?:.?)? etc... but not warn (.?)? etc... */
5411 QtfrNode
* qnt
= NQTFR(target
);
5412 int nestq_num
= popular_quantifier_num(qn
);
5413 int targetq_num
= popular_quantifier_num(qnt
);
5415 #ifdef USE_WARNING_REDUNDANT_NESTED_REPEAT_OPERATOR
5416 if (nestq_num
>= 0 && targetq_num
>= 0 &&
5417 IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_WARN_REDUNDANT_NESTED_REPEAT
)) {
5418 switch (ReduceTypeTable
[targetq_num
][nestq_num
]) {
5423 if (onig_warn
!= onig_null_warn
) {
5424 onig_syntax_warn(env
, "regular expression has redundant nested repeat operator '%s'",
5425 PopularQStr
[targetq_num
]);
5431 if (onig_warn
!= onig_null_warn
) {
5432 onig_syntax_warn(env
, "nested repeat operator '%s' and '%s' was replaced with '%s' in regular expression",
5433 PopularQStr
[targetq_num
], PopularQStr
[nestq_num
],
5434 ReduceQStr
[ReduceTypeTable
[targetq_num
][nestq_num
]]);
5443 if (targetq_num
>= 0) {
5444 if (nestq_num
>= 0) {
5445 onig_reduce_nested_quantifier(qnode
, target
);
5448 else if (targetq_num
== 1 || targetq_num
== 2) { /* * or + */
5449 /* (?:a*){n,m}, (?:a+){n,m} => (?:a*){n,n}, (?:a+){n,n} */
5450 if (! IS_REPEAT_INFINITE(qn
->upper
) && qn
->upper
> 1 && qn
->greedy
) {
5451 qn
->upper
= (qn
->lower
== 0 ? 1 : qn
->lower
);
5462 qn
->target
= target
;
5468 #ifndef CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS
5470 clear_not_flag_cclass(CClassNode
* cc
, OnigEncoding enc
)
5475 if (IS_NCCLASS_NOT(cc
)) {
5476 bitset_invert(cc
->bs
);
5478 if (! ONIGENC_IS_SINGLEBYTE(enc
)) {
5479 r
= not_code_range_buf(enc
, cc
->mbuf
, &tbuf
);
5480 if (r
!= 0) return r
;
5482 bbuf_free(cc
->mbuf
);
5486 NCCLASS_CLEAR_NOT(cc
);
5491 #endif /* CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS */
5499 } IApplyCaseFoldArg
;
5502 i_apply_case_fold(OnigCodePoint from
, OnigCodePoint to
[],
5503 int to_len
, void* arg
)
5505 IApplyCaseFoldArg
* iarg
;
5512 iarg
= (IApplyCaseFoldArg
* )arg
;
5515 asc_cc
= iarg
->asc_cc
;
5518 if (IS_NULL(asc_cc
)) {
5521 else if (ONIGENC_IS_ASCII_CODE(from
) == ONIGENC_IS_ASCII_CODE(*to
)) {
5525 add_flag
= onig_is_code_in_cc(env
->enc
, from
, asc_cc
);
5526 if (IS_NCCLASS_NOT(asc_cc
))
5527 add_flag
= !add_flag
;
5531 int is_in
= onig_is_code_in_cc(env
->enc
, from
, cc
);
5532 #ifdef CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS
5533 if ((is_in
!= 0 && !IS_NCCLASS_NOT(cc
)) ||
5534 (is_in
== 0 && IS_NCCLASS_NOT(cc
))) {
5536 if (ONIGENC_MBC_MINLEN(env
->enc
) > 1 || *to
>= SINGLE_BYTE_SIZE
) {
5537 r
= add_code_range0(&(cc
->mbuf
), env
, *to
, *to
, 0);
5538 if (r
< 0) return r
;
5541 BITSET_SET_BIT(bs
, *to
);
5548 if (ONIGENC_MBC_MINLEN(env
->enc
) > 1 || *to
>= SINGLE_BYTE_SIZE
) {
5549 if (IS_NCCLASS_NOT(cc
)) clear_not_flag_cclass(cc
, env
->enc
);
5550 r
= add_code_range0(&(cc
->mbuf
), env
, *to
, *to
, 0);
5551 if (r
< 0) return r
;
5554 if (IS_NCCLASS_NOT(cc
)) {
5555 BITSET_CLEAR_BIT(bs
, *to
);
5558 BITSET_SET_BIT(bs
, *to
);
5563 #endif /* CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS */
5567 UChar buf
[ONIGENC_CODE_TO_MBC_MAXLEN
];
5568 Node
*snode
= NULL_NODE
;
5570 if (onig_is_code_in_cc(env
->enc
, from
, cc
)
5571 #ifdef CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS
5572 && !IS_NCCLASS_NOT(cc
)
5575 for (i
= 0; i
< to_len
; i
++) {
5576 len
= ONIGENC_CODE_TO_MBC(env
->enc
, to
[i
], buf
);
5578 snode
= onig_node_new_str(buf
, buf
+ len
);
5579 CHECK_NULL_RETURN_MEMERR(snode
);
5581 /* char-class expanded multi-char only
5582 compare with string folded at match time. */
5583 NSTRING_SET_AMBIG(snode
);
5586 r
= onig_node_str_cat(snode
, buf
, buf
+ len
);
5588 onig_node_free(snode
);
5594 *(iarg
->ptail
) = onig_node_new_alt(snode
, NULL_NODE
);
5595 CHECK_NULL_RETURN_MEMERR(*(iarg
->ptail
));
5596 iarg
->ptail
= &(NCDR((*(iarg
->ptail
))));
5604 cclass_case_fold(Node
** np
, CClassNode
* cc
, CClassNode
* asc_cc
, ScanEnv
* env
)
5607 IApplyCaseFoldArg iarg
;
5611 iarg
.asc_cc
= asc_cc
;
5612 iarg
.alt_root
= NULL_NODE
;
5613 iarg
.ptail
= &(iarg
.alt_root
);
5615 r
= ONIGENC_APPLY_ALL_CASE_FOLD(env
->enc
, env
->case_fold_flag
,
5616 i_apply_case_fold
, &iarg
);
5618 onig_node_free(iarg
.alt_root
);
5621 if (IS_NOT_NULL(iarg
.alt_root
)) {
5622 Node
* work
= onig_node_new_alt(*np
, iarg
.alt_root
);
5623 if (IS_NULL(work
)) {
5624 onig_node_free(iarg
.alt_root
);
5625 return ONIGERR_MEMORY
;
5633 node_linebreak(Node
** np
, ScanEnv
* env
)
5635 /* same as (?>\x0D\x0A|[\x0A-\x0D\x{85}\x{2028}\x{2029}]) */
5638 Node
* target1
= NULL
;
5639 Node
* target2
= NULL
;
5642 UChar buf
[ONIGENC_CODE_TO_MBC_MAXLEN
* 2];
5645 num1
= ONIGENC_CODE_TO_MBC(env
->enc
, 0x0D, buf
);
5646 if (num1
< 0) return num1
;
5647 num2
= ONIGENC_CODE_TO_MBC(env
->enc
, 0x0A, buf
+ num1
);
5648 if (num2
< 0) return num2
;
5649 left
= node_new_str_raw(buf
, buf
+ num1
+ num2
);
5650 if (IS_NULL(left
)) goto err
;
5652 /* [\x0A-\x0D] or [\x0A-\x0D\x{85}\x{2028}\x{2029}] */
5653 right
= node_new_cclass();
5654 if (IS_NULL(right
)) goto err
;
5655 cc
= NCCLASS(right
);
5656 if (ONIGENC_MBC_MINLEN(env
->enc
) > 1) {
5657 r
= add_code_range(&(cc
->mbuf
), env
, 0x0A, 0x0D);
5658 if (r
!= 0) goto err
;
5661 bitset_set_range(env
, cc
->bs
, 0x0A, 0x0D);
5664 /* TODO: move this block to enc/unicode.c */
5665 if (ONIGENC_IS_UNICODE(env
->enc
)) {
5666 /* UTF-8, UTF-16BE/LE, UTF-32BE/LE */
5667 r
= add_code_range(&(cc
->mbuf
), env
, 0x85, 0x85);
5668 if (r
!= 0) goto err
;
5669 r
= add_code_range(&(cc
->mbuf
), env
, 0x2028, 0x2029);
5670 if (r
!= 0) goto err
;
5674 target1
= onig_node_new_alt(right
, NULL_NODE
);
5675 if (IS_NULL(target1
)) goto err
;
5677 target2
= onig_node_new_alt(left
, target1
);
5678 if (IS_NULL(target2
)) goto err
;
5683 *np
= node_new_enclose(ENCLOSE_STOP_BACKTRACK
);
5684 if (IS_NULL(*np
)) goto err
;
5685 NENCLOSE(*np
)->target
= target2
;
5689 onig_node_free(left
);
5690 onig_node_free(right
);
5691 onig_node_free(target1
);
5692 onig_node_free(target2
);
5693 return ONIGERR_MEMORY
;
5697 propname2ctype(ScanEnv
* env
, const char* propname
)
5699 UChar
* name
= (UChar
* )propname
;
5700 UChar
* name_end
= name
+ strlen(propname
);
5701 int ctype
= env
->enc
->property_name_to_ctype(ONIG_ENCODING_ASCII
,
5704 onig_scan_env_set_error_string(env
, ctype
, name
, name_end
);
5710 add_property_to_cc(CClassNode
* cc
, const char* propname
, int not, ScanEnv
* env
)
5712 int ctype
= propname2ctype(env
, propname
);
5713 if (ctype
< 0) return ctype
;
5714 return add_ctype_to_cc(cc
, ctype
, not, 0, env
);
5718 * helper methods for node_extended_grapheme_cluster (/\X/)
5721 create_property_node(Node
**np
, ScanEnv
* env
, const char* propname
)
5726 *np
= node_new_cclass();
5727 if (IS_NULL(*np
)) return ONIGERR_MEMORY
;
5729 r
= add_property_to_cc(cc
, propname
, 0, env
);
5731 onig_node_free(*np
);
5736 quantify_node(Node
**np
, int lower
, int upper
)
5738 Node
* tmp
= node_new_quantifier(lower
, upper
, 0);
5739 if (IS_NULL(tmp
)) return ONIGERR_MEMORY
;
5740 NQTFR(tmp
)->target
= *np
;
5746 quantify_property_node(Node
**np
, ScanEnv
* env
, const char* propname
, char repetitions
)
5750 int upper
= REPEAT_INFINITE
;
5752 r
= create_property_node(np
, env
, propname
);
5753 if (r
!= 0) return r
;
5754 switch (repetitions
) {
5755 case '?': upper
= 1; break;
5756 case '+': lower
= 1; break;
5758 case '2': lower
= upper
= 2; break;
5759 default : return ONIGERR_PARSER_BUG
;
5761 return quantify_node(np
, lower
, upper
);
5767 /* IMPORTANT: Make sure node_array ends with NULL_NODE */
5769 create_node_from_array(int kind
, Node
**np
, Node
**node_array
)
5771 Node
* tmp
= NULL_NODE
;
5774 while (node_array
[i
] != NULL_NODE
) i
++;
5776 *np
= kind
==LIST
? node_new_list(node_array
[i
], tmp
)
5777 : onig_node_new_alt(node_array
[i
], tmp
);
5780 onig_node_free(node_array
[i
]);
5781 node_array
[i
--] = NULL_NODE
;
5783 onig_node_free(tmp
);
5784 return ONIGERR_MEMORY
;
5787 node_array
[i
] = NULL_NODE
;
5793 #define R_ERR(call) r=(call);if(r!=0)goto err
5795 /* Memory layout for common node array:
5796 * The main purpose is to be able to easily free all leftover nodes
5797 * after an error. As a side effect, we share some memory.
5799 * The layout is as shown below (each line corresponds to one call of
5800 * create_node_from_array()). Because create_node_from_array sets all
5801 * nodes of the source to NULL_NODE, we can overlap the target array
5802 * as long as we do not override the actual target location.
5804 * Target Array name Index
5806 * node_array 0 1 2 3 4 5 6 7 8 9 A B C D E F
5807 * top_alts alts[5] 0 1 2 3 4*
5808 * alts+1 list[4] 0 1 2 3*
5809 * list+1 core_alts[7] 0 1 2 3 4 5 6*
5810 * core_alts+0 H_list[4] 0 1 2 3*
5811 * H_list+1 H_alt2[4] 0 1 2 3*
5812 * h_alt2+1 H_list2[3] 0 1 2*
5813 * core_alts+4 XP_list[4] 0 1 2 3*
5814 * XP_list+1 Ex_list[4] 0 1 2 3*
5816 #define NODE_COMMON_SIZE 16
5819 node_extended_grapheme_cluster(Node
** np
, ScanEnv
* env
)
5823 Node
* top_alt
= NULL
;
5827 int any_target_position
;
5828 UChar buf
[ONIGENC_CODE_TO_MBC_MAXLEN
* 2];
5829 OnigOptionType option
;
5830 /* node_common is function-global so that we can free all nodes
5831 * in case of error. Unused slots are set to NULL_NODE at all times. */
5832 Node
*node_common
[NODE_COMMON_SIZE
];
5833 Node
**alts
= node_common
+0; /* size: 5 */
5835 for (i
=0; i
<NODE_COMMON_SIZE
; i
++)
5836 node_common
[i
] = NULL_NODE
;
5838 /* CRLF, common for both Unicode and non-Unicode */
5840 r
= ONIGENC_CODE_TO_MBC(env
->enc
, 0x0D, buf
);
5841 if (r
< 0) goto err
;
5843 r
= ONIGENC_CODE_TO_MBC(env
->enc
, 0x0A, buf
+ num1
);
5844 if (r
< 0) goto err
;
5845 alts
[0] = node_new_str_raw(buf
, buf
+ num1
+ r
);
5846 if (IS_NULL(alts
[0])) goto err
;
5848 #ifdef USE_UNICODE_PROPERTIES
5849 if (ONIGENC_IS_UNICODE(env
->enc
)) { /* UTF-8, UTF-16BE/LE, UTF-32BE/LE */
5852 if (propname2ctype(env
, "Grapheme_Cluster_Break=Extend") < 0) goto err
;
5854 * CRLF (already done)
5856 * | precore* core postcore*
5857 * | . (to catch invalid stuff, because this seems to be spec for String#grapheme_clusters) */
5859 /* [Control CR LF] (CR and LF are not in the spec, but this is a conformed fix) */
5860 alts
[1] = node_new_cclass();
5861 if (IS_NULL(alts
[1])) goto err
;
5862 cc
= NCCLASS(alts
[1]);
5863 R_ERR(add_property_to_cc(cc
, "Grapheme_Cluster_Break=Control", 0, env
));
5864 if (ONIGENC_MBC_MINLEN(env
->enc
) > 1) { /* UTF-16/UTF-32 */
5865 R_ERR(add_code_range(&(cc
->mbuf
), env
, 0x000A, 0x000A)); /* CR */
5866 R_ERR(add_code_range(&(cc
->mbuf
), env
, 0x000D, 0x000D)); /* LF */
5869 BITSET_SET_BIT(cc
->bs
, 0x0a);
5870 BITSET_SET_BIT(cc
->bs
, 0x0d);
5873 /* precore* core postcore* */
5875 Node
**list
= alts
+ 3; /* size: 4 */
5877 /* precore*; precore := Prepend */
5878 R_ERR(quantify_property_node(list
+0, env
, "Grapheme_Cluster_Break=Prepend", '*'));
5880 /* core := hangul-syllable
5883 * | [^Control CR LF] */
5885 Node
**core_alts
= list
+ 2; /* size: 7 */
5887 /* hangul-syllable :=
5888 * L* (V+ | LV V* | LVT) T*
5891 /* hangul-syllable is an alternative (would be called H_alt)
5892 * inside an alternative, but we flatten it into core_alts */
5894 /* L* (V+ | LV V* | LVT) T* */
5896 Node
**H_list
= core_alts
+ 1; /* size: 4 */
5897 R_ERR(quantify_property_node(H_list
+0, env
, "Grapheme_Cluster_Break=L", '*'));
5899 /* V+ | LV V* | LVT */
5901 Node
**H_alt2
= H_list
+ 2; /* size: 4 */
5902 R_ERR(quantify_property_node(H_alt2
+0, env
, "Grapheme_Cluster_Break=V", '+'));
5906 Node
**H_list2
= H_alt2
+ 2; /* size: 3 */
5908 R_ERR(create_property_node(H_list2
+0, env
, "Grapheme_Cluster_Break=LV"));
5909 R_ERR(quantify_property_node(H_list2
+1, env
, "Grapheme_Cluster_Break=V", '*'));
5910 R_ERR(create_node_from_array(LIST
, H_alt2
+1, H_list2
));
5913 R_ERR(create_property_node(H_alt2
+2, env
, "Grapheme_Cluster_Break=LVT"));
5914 R_ERR(create_node_from_array(ALT
, H_list
+1, H_alt2
));
5917 R_ERR(quantify_property_node(H_list
+2, env
, "Grapheme_Cluster_Break=T", '*'));
5918 R_ERR(create_node_from_array(LIST
, core_alts
+0, H_list
));
5921 R_ERR(quantify_property_node(core_alts
+1, env
, "Grapheme_Cluster_Break=L", '+'));
5922 R_ERR(quantify_property_node(core_alts
+2, env
, "Grapheme_Cluster_Break=T", '+'));
5923 /* end of hangul-syllable */
5925 /* ri-sequence := RI RI */
5926 R_ERR(quantify_property_node(core_alts
+3, env
, "Regional_Indicator", '2'));
5928 /* xpicto-sequence := \p{Extended_Pictographic} (Extend* ZWJ \p{Extended_Pictographic})* */
5930 Node
**XP_list
= core_alts
+ 5; /* size: 3 */
5931 R_ERR(create_property_node(XP_list
+0, env
, "Extended_Pictographic"));
5933 /* (Extend* ZWJ \p{Extended_Pictographic})* */
5935 Node
**Ex_list
= XP_list
+ 2; /* size: 4 */
5936 /* assert(Ex_list+4 == node_common+NODE_COMMON_SIZE); */
5937 R_ERR(quantify_property_node(Ex_list
+0, env
, "Grapheme_Cluster_Break=Extend", '*'));
5939 /* ZWJ (ZERO WIDTH JOINER) */
5940 r
= ONIGENC_CODE_TO_MBC(env
->enc
, 0x200D, buf
);
5941 if (r
< 0) goto err
;
5942 Ex_list
[1] = node_new_str_raw(buf
, buf
+ r
);
5943 if (IS_NULL(Ex_list
[1])) goto err
;
5945 R_ERR(create_property_node(Ex_list
+2, env
, "Extended_Pictographic"));
5946 R_ERR(create_node_from_array(LIST
, XP_list
+1, Ex_list
));
5948 R_ERR(quantify_node(XP_list
+1, 0, REPEAT_INFINITE
)); /* TODO: Check about node freeing */
5950 R_ERR(create_node_from_array(LIST
, core_alts
+4, XP_list
));
5953 /* [^Control CR LF] */
5954 core_alts
[5] = node_new_cclass();
5955 if (IS_NULL(core_alts
[5])) goto err
;
5956 cc
= NCCLASS(core_alts
[5]);
5957 if (ONIGENC_MBC_MINLEN(env
->enc
) > 1) { /* UTF-16/UTF-32 */
5958 BBuf
*inverted_buf
= NULL
;
5960 /* TODO: fix false warning */
5961 const int dup_not_warned
= env
->warnings_flag
| ~ONIG_SYN_WARN_CC_DUP
;
5962 env
->warnings_flag
|= ONIG_SYN_WARN_CC_DUP
;
5964 /* Start with a positive buffer and invert at the end.
5965 * Otherwise, adding single-character ranges work the wrong way. */
5966 R_ERR(add_property_to_cc(cc
, "Grapheme_Cluster_Break=Control", 0, env
));
5967 R_ERR(add_code_range(&(cc
->mbuf
), env
, 0x000A, 0x000A)); /* CR */
5968 R_ERR(add_code_range(&(cc
->mbuf
), env
, 0x000D, 0x000D)); /* LF */
5969 R_ERR(not_code_range_buf(env
->enc
, cc
->mbuf
, &inverted_buf
, env
));
5970 cc
->mbuf
= inverted_buf
; /* TODO: check what to do with buffer before inversion */
5972 env
->warnings_flag
&= dup_not_warned
; /* TODO: fix false warning */
5975 R_ERR(add_property_to_cc(cc
, "Grapheme_Cluster_Break=Control", 1, env
));
5976 BITSET_CLEAR_BIT(cc
->bs
, 0x0a);
5977 BITSET_CLEAR_BIT(cc
->bs
, 0x0d);
5980 R_ERR(create_node_from_array(ALT
, list
+1, core_alts
));
5983 /* postcore*; postcore = [Extend ZWJ SpacingMark] */
5984 R_ERR(create_property_node(list
+2, env
, "Grapheme_Cluster_Break=Extend"));
5985 cc
= NCCLASS(list
[2]);
5986 R_ERR(add_property_to_cc(cc
, "Grapheme_Cluster_Break=SpacingMark", 0, env
));
5987 R_ERR(add_code_range(&(cc
->mbuf
), env
, 0x200D, 0x200D));
5988 R_ERR(quantify_node(list
+2, 0, REPEAT_INFINITE
));
5990 R_ERR(create_node_from_array(LIST
, alts
+2, list
));
5993 any_target_position
= 3;
5996 #endif /* USE_UNICODE_PROPERTIES */
5998 any_target_position
= 1;
6001 /* PerlSyntax: (?s:.), RubySyntax: (?m:.), common for both Unicode and non-Unicode */
6002 /* Not in Unicode spec (UAX #29), but added to catch invalid stuff,
6003 * because this is Ruby spec for String#grapheme_clusters. */
6004 np1
= node_new_anychar();
6005 if (IS_NULL(np1
)) goto err
;
6007 option
= env
->option
;
6008 ONOFF(option
, ONIG_OPTION_MULTILINE
, 0);
6009 tmp
= node_new_option(option
);
6010 if (IS_NULL(tmp
)) goto err
;
6011 NENCLOSE(tmp
)->target
= np1
;
6012 alts
[any_target_position
] = tmp
;
6015 R_ERR(create_node_from_array(ALT
, &top_alt
, alts
));
6017 /* (?>): For efficiency, because there is no text piece
6018 * that is not in a grapheme cluster, and there is only one way
6019 * to split a string into grapheme clusters. */
6020 tmp
= node_new_enclose(ENCLOSE_STOP_BACKTRACK
);
6021 if (IS_NULL(tmp
)) goto err
;
6022 NENCLOSE(tmp
)->target
= top_alt
;
6025 #ifdef USE_UNICODE_PROPERTIES
6026 if (ONIGENC_IS_UNICODE(env
->enc
)) {
6027 /* Don't ignore case. */
6028 option
= env
->option
;
6029 ONOFF(option
, ONIG_OPTION_IGNORECASE
, 1);
6030 *np
= node_new_option(option
);
6031 if (IS_NULL(*np
)) goto err
;
6032 NENCLOSE(*np
)->target
= np1
;
6042 onig_node_free(np1
);
6043 for (i
=0; i
<NODE_COMMON_SIZE
; i
++)
6044 onig_node_free(node_common
[i
]);
6045 return (r
== 0) ? ONIGERR_MEMORY
: r
;
6050 countbits(unsigned int bits
)
6052 bits
= (bits
& 0x55555555) + ((bits
>> 1) & 0x55555555);
6053 bits
= (bits
& 0x33333333) + ((bits
>> 2) & 0x33333333);
6054 bits
= (bits
& 0x0f0f0f0f) + ((bits
>> 4) & 0x0f0f0f0f);
6055 bits
= (bits
& 0x00ff00ff) + ((bits
>> 8) & 0x00ff00ff);
6056 return (bits
& 0x0000ffff) + ((bits
>>16) & 0x0000ffff);
6060 is_onechar_cclass(CClassNode
* cc
, OnigCodePoint
* code
)
6062 const OnigCodePoint not_found
= ONIG_LAST_CODE_POINT
;
6063 OnigCodePoint c
= not_found
;
6065 BBuf
*bbuf
= cc
->mbuf
;
6067 if (IS_NCCLASS_NOT(cc
)) return 0;
6070 if (IS_NOT_NULL(bbuf
)) {
6071 OnigCodePoint n
, *data
;
6072 GET_CODE_POINT(n
, bbuf
->p
);
6073 data
= (OnigCodePoint
* )(bbuf
->p
) + 1;
6074 if ((n
== 1) && (data
[0] == data
[1])) {
6075 /* only one char found in the bbuf, save the code point. */
6077 if (((c
< SINGLE_BYTE_SIZE
) && BITSET_AT(cc
->bs
, c
))) {
6078 /* skip if c is included in the bitset */
6083 return 0; /* the bbuf contains multiple chars */
6088 for (i
= 0; i
< BITSET_SIZE
; i
++) {
6089 Bits b1
= cc
->bs
[i
];
6091 if (((b1
& (b1
- 1)) == 0) && (c
== not_found
)) {
6092 c
= BITS_IN_ROOM
* i
+ countbits(b1
- 1);
6094 return 0; /* the character class contains multiple chars */
6099 if (c
!= not_found
) {
6104 /* the character class contains no char. */
6110 parse_exp(Node
** np
, OnigToken
* tok
, int term
,
6111 UChar
** src
, UChar
* end
, ScanEnv
* env
)
6113 int r
, len
, group
= 0;
6118 if (tok
->type
== (enum TokenSyms
)term
)
6121 switch (tok
->type
) {
6125 *np
= node_new_empty();
6129 case TK_SUBEXP_OPEN
:
6130 r
= parse_enclose(np
, tok
, TK_SUBEXP_CLOSE
, src
, end
, env
);
6131 if (r
< 0) return r
;
6132 if (r
== 1) group
= 1;
6133 else if (r
== 2) { /* option only */
6135 OnigOptionType prev
= env
->option
;
6137 env
->option
= NENCLOSE(*np
)->option
;
6138 r
= fetch_token(tok
, src
, end
, env
);
6143 r
= parse_subexp(&target
, tok
, term
, src
, end
, env
);
6146 onig_node_free(target
);
6149 NENCLOSE(*np
)->target
= target
;
6154 case TK_SUBEXP_CLOSE
:
6155 if (! IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_ALLOW_UNMATCHED_CLOSE_SUBEXP
))
6156 return ONIGERR_UNMATCHED_CLOSE_PARENTHESIS
;
6158 if (tok
->escaped
) goto tk_raw_byte
;
6163 r
= node_linebreak(np
, env
);
6164 if (r
< 0) return r
;
6167 case TK_EXTENDED_GRAPHEME_CLUSTER
:
6168 r
= node_extended_grapheme_cluster(np
, env
);
6169 if (r
< 0) return r
;
6173 *np
= onig_node_new_anchor(ANCHOR_KEEP
);
6174 CHECK_NULL_RETURN_MEMERR(*np
);
6180 *np
= node_new_str(tok
->backp
, *src
);
6181 CHECK_NULL_RETURN_MEMERR(*np
);
6185 r
= fetch_token(tok
, src
, end
, env
);
6186 if (r
< 0) return r
;
6187 if (r
== TK_STRING
) {
6188 r
= onig_node_str_cat(*np
, tok
->backp
, *src
);
6190 #ifndef NUMBERED_CHAR_IS_NOT_CASE_AMBIG
6191 else if (r
== TK_CODE_POINT
) {
6192 r
= node_str_cat_codepoint(*np
, env
->enc
, tok
->u
.code
);
6198 if (r
< 0) return r
;
6210 *np
= node_new_str_raw_char((UChar
)tok
->u
.c
);
6211 CHECK_NULL_RETURN_MEMERR(*np
);
6214 if (len
>= ONIGENC_MBC_MINLEN(env
->enc
)) {
6215 if (len
== enclen(env
->enc
, NSTR(*np
)->s
, NSTR(*np
)->end
)) {
6216 r
= fetch_token(tok
, src
, end
, env
);
6217 NSTRING_CLEAR_RAW(*np
);
6222 r
= fetch_token(tok
, src
, end
, env
);
6223 if (r
< 0) return r
;
6224 if (r
!= TK_RAW_BYTE
) {
6225 /* Don't use this, it is wrong for little endian encodings. */
6226 #ifdef USE_PAD_TO_SHORT_BYTE_CHAR
6228 if (len
< ONIGENC_MBC_MINLEN(env
->enc
)) {
6229 rem
= ONIGENC_MBC_MINLEN(env
->enc
) - len
;
6230 (void )node_str_head_pad(NSTR(*np
), rem
, (UChar
)0);
6231 if (len
+ rem
== enclen(env
->enc
, NSTR(*np
)->s
)) {
6232 NSTRING_CLEAR_RAW(*np
);
6237 return ONIGERR_TOO_SHORT_MULTI_BYTE_STRING
;
6240 r
= node_str_cat_char(*np
, (UChar
)tok
->u
.c
);
6241 if (r
< 0) return r
;
6250 *np
= node_new_empty();
6251 CHECK_NULL_RETURN_MEMERR(*np
);
6252 r
= node_str_cat_codepoint(*np
, env
->enc
, tok
->u
.code
);
6253 if (r
!= 0) return r
;
6254 #ifdef NUMBERED_CHAR_IS_NOT_CASE_AMBIG
6255 NSTRING_SET_RAW(*np
);
6264 OnigCodePoint end_op
[2];
6265 UChar
*qstart
, *qend
, *nextp
;
6267 end_op
[0] = (OnigCodePoint
)MC_ESC(env
->syntax
);
6268 end_op
[1] = (OnigCodePoint
)'E';
6270 qend
= find_str_position(end_op
, 2, qstart
, end
, &nextp
, env
->enc
);
6271 if (IS_NULL(qend
)) {
6274 *np
= node_new_str(qstart
, qend
);
6275 CHECK_NULL_RETURN_MEMERR(*np
);
6282 switch (tok
->u
.prop
.ctype
) {
6283 case ONIGENC_CTYPE_WORD
:
6284 *np
= node_new_ctype(tok
->u
.prop
.ctype
, tok
->u
.prop
.not,
6285 IS_ASCII_RANGE(env
->option
));
6286 CHECK_NULL_RETURN_MEMERR(*np
);
6289 case ONIGENC_CTYPE_SPACE
:
6290 case ONIGENC_CTYPE_DIGIT
:
6291 case ONIGENC_CTYPE_XDIGIT
:
6295 *np
= node_new_cclass();
6296 CHECK_NULL_RETURN_MEMERR(*np
);
6298 r
= add_ctype_to_cc(cc
, tok
->u
.prop
.ctype
, 0,
6299 IS_ASCII_RANGE(env
->option
), env
);
6300 if (r
!= 0) return r
;
6301 if (tok
->u
.prop
.not != 0) NCCLASS_SET_NOT(cc
);
6306 return ONIGERR_PARSER_BUG
;
6312 case TK_CHAR_PROPERTY
:
6313 r
= parse_char_property(np
, tok
, src
, end
, env
);
6314 if (r
!= 0) return r
;
6323 r
= parse_char_class(np
, &asc_node
, tok
, src
, end
, env
);
6325 onig_node_free(asc_node
);
6330 if (is_onechar_cclass(cc
, &code
)) {
6331 onig_node_free(*np
);
6332 onig_node_free(asc_node
);
6333 *np
= node_new_empty();
6334 CHECK_NULL_RETURN_MEMERR(*np
);
6335 r
= node_str_cat_codepoint(*np
, env
->enc
, code
);
6336 if (r
!= 0) return r
;
6339 if (IS_IGNORECASE(env
->option
)) {
6340 r
= cclass_case_fold(np
, cc
, NCCLASS(asc_node
), env
);
6342 onig_node_free(asc_node
);
6346 onig_node_free(asc_node
);
6351 *np
= node_new_anychar();
6352 CHECK_NULL_RETURN_MEMERR(*np
);
6355 case TK_ANYCHAR_ANYTIME
:
6356 *np
= node_new_anychar();
6357 CHECK_NULL_RETURN_MEMERR(*np
);
6358 qn
= node_new_quantifier(0, REPEAT_INFINITE
, 0);
6359 CHECK_NULL_RETURN_MEMERR(qn
);
6360 NQTFR(qn
)->target
= *np
;
6365 len
= tok
->u
.backref
.num
;
6366 *np
= node_new_backref(len
,
6367 (len
> 1 ? tok
->u
.backref
.refs
: &(tok
->u
.backref
.ref1
)),
6368 tok
->u
.backref
.by_name
,
6369 #ifdef USE_BACKREF_WITH_LEVEL
6370 tok
->u
.backref
.exist_level
,
6371 tok
->u
.backref
.level
,
6374 CHECK_NULL_RETURN_MEMERR(*np
);
6377 #ifdef USE_SUBEXP_CALL
6380 int gnum
= tok
->u
.call
.gnum
;
6382 if (gnum
< 0 || tok
->u
.call
.rel
!= 0) {
6383 if (gnum
> 0) gnum
--;
6384 gnum
= BACKREF_REL_TO_ABS(gnum
, env
);
6386 return ONIGERR_INVALID_BACKREF
;
6388 *np
= node_new_call(tok
->u
.call
.name
, tok
->u
.call
.name_end
, gnum
);
6389 CHECK_NULL_RETURN_MEMERR(*np
);
6396 *np
= onig_node_new_anchor(tok
->u
.anchor
.subtype
);
6397 CHECK_NULL_RETURN_MEMERR(*np
);
6398 NANCHOR(*np
)->ascii_range
= tok
->u
.anchor
.ascii_range
;
6403 if (IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_CONTEXT_INDEP_REPEAT_OPS
)) {
6404 if (IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_CONTEXT_INVALID_REPEAT_OPS
))
6405 return ONIGERR_TARGET_OF_REPEAT_OPERATOR_NOT_SPECIFIED
;
6407 *np
= node_new_empty();
6415 return ONIGERR_PARSER_BUG
;
6423 r
= fetch_token(tok
, src
, end
, env
);
6424 if (r
< 0) return r
;
6427 if (r
== TK_OP_REPEAT
|| r
== TK_INTERVAL
) {
6428 if (is_invalid_quantifier_target(*targetp
))
6429 return ONIGERR_TARGET_OF_REPEAT_OPERATOR_INVALID
;
6431 qn
= node_new_quantifier(tok
->u
.repeat
.lower
, tok
->u
.repeat
.upper
,
6432 (r
== TK_INTERVAL
? 1 : 0));
6433 CHECK_NULL_RETURN_MEMERR(qn
);
6434 NQTFR(qn
)->greedy
= tok
->u
.repeat
.greedy
;
6435 r
= set_quantifier(qn
, *targetp
, group
, env
);
6441 if (tok
->u
.repeat
.possessive
!= 0) {
6443 en
= node_new_enclose(ENCLOSE_STOP_BACKTRACK
);
6446 return ONIGERR_MEMORY
;
6448 NENCLOSE(en
)->target
= qn
;
6458 else if (r
== 2) { /* split case: /abc+/ */
6461 *targetp
= node_new_list(*targetp
, NULL
);
6462 if (IS_NULL(*targetp
)) {
6464 return ONIGERR_MEMORY
;
6466 tmp
= NCDR(*targetp
) = node_new_list(qn
, NULL
);
6469 return ONIGERR_MEMORY
;
6471 targetp
= &(NCAR(tmp
));
6481 parse_branch(Node
** top
, OnigToken
* tok
, int term
,
6482 UChar
** src
, UChar
* end
, ScanEnv
* env
)
6485 Node
*node
, **headp
;
6488 r
= parse_exp(&node
, tok
, term
, src
, end
, env
);
6490 onig_node_free(node
);
6494 if (r
== TK_EOT
|| r
== term
|| r
== TK_ALT
) {
6498 *top
= node_new_list(node
, NULL
);
6499 headp
= &(NCDR(*top
));
6500 while (r
!= TK_EOT
&& r
!= term
&& r
!= TK_ALT
) {
6501 r
= parse_exp(&node
, tok
, term
, src
, end
, env
);
6503 onig_node_free(node
);
6507 if (NTYPE(node
) == NT_LIST
) {
6509 while (IS_NOT_NULL(NCDR(node
))) node
= NCDR(node
);
6510 headp
= &(NCDR(node
));
6513 *headp
= node_new_list(node
, NULL
);
6514 headp
= &(NCDR(*headp
));
6522 /* term_tok: TK_EOT or TK_SUBEXP_CLOSE */
6524 parse_subexp(Node
** top
, OnigToken
* tok
, int term
,
6525 UChar
** src
, UChar
* end
, ScanEnv
* env
)
6528 Node
*node
, **headp
;
6532 if (env
->parse_depth
> ParseDepthLimit
)
6533 return ONIGERR_PARSE_DEPTH_LIMIT_OVER
;
6534 r
= parse_branch(&node
, tok
, term
, src
, end
, env
);
6536 onig_node_free(node
);
6543 else if (r
== TK_ALT
) {
6544 *top
= onig_node_new_alt(node
, NULL
);
6545 headp
= &(NCDR(*top
));
6546 while (r
== TK_ALT
) {
6547 r
= fetch_token(tok
, src
, end
, env
);
6548 if (r
< 0) return r
;
6549 r
= parse_branch(&node
, tok
, term
, src
, end
, env
);
6551 onig_node_free(node
);
6555 *headp
= onig_node_new_alt(node
, NULL
);
6556 headp
= &(NCDR(*headp
));
6559 if (tok
->type
!= (enum TokenSyms
)term
)
6563 onig_node_free(node
);
6565 if (term
== TK_SUBEXP_CLOSE
)
6566 return ONIGERR_END_PATTERN_WITH_UNMATCHED_PARENTHESIS
;
6568 return ONIGERR_PARSER_BUG
;
6576 parse_regexp(Node
** top
, UChar
** src
, UChar
* end
, ScanEnv
* env
)
6581 r
= fetch_token(&tok
, src
, end
, env
);
6582 if (r
< 0) return r
;
6583 r
= parse_subexp(top
, &tok
, TK_EOT
, src
, end
, env
);
6584 if (r
< 0) return r
;
6586 #ifdef USE_SUBEXP_CALL
6587 if (env
->num_call
> 0) {
6588 /* Capture the pattern itself. It is used for (?R), (?0) and \g<0>. */
6591 np
= node_new_enclose_memory(env
->option
, 0);
6592 CHECK_NULL_RETURN_MEMERR(np
);
6593 NENCLOSE(np
)->regnum
= num
;
6594 NENCLOSE(np
)->target
= *top
;
6595 r
= scan_env_set_mem_node(env
, num
, np
);
6607 onig_parse_make_tree(Node
** root
, const UChar
* pattern
, const UChar
* end
,
6608 regex_t
* reg
, ScanEnv
* env
)
6613 #ifdef USE_NAMED_GROUP
6617 scan_env_clear(env
);
6618 env
->option
= reg
->options
;
6619 env
->case_fold_flag
= reg
->case_fold_flag
;
6620 env
->enc
= reg
->enc
;
6621 env
->syntax
= reg
->syntax
;
6622 env
->pattern
= (UChar
* )pattern
;
6623 env
->pattern_end
= (UChar
* )end
;
6627 p
= (UChar
* )pattern
;
6628 r
= parse_regexp(root
, &p
, (UChar
* )end
, env
);
6629 reg
->num_mem
= env
->num_mem
;
6634 onig_scan_env_set_error_string(ScanEnv
* env
, int ecode ARG_UNUSED
,
6635 UChar
* arg
, UChar
* arg_end
)
6638 env
->error_end
= arg_end
;