1 /**********************************************************************
2 regparse.c - Oniguruma (regular expression library)
3 **********************************************************************/
5 * Copyright (c) 2002-2007 K.Kosako <sndgk393 AT ybb DOT ne DOT jp>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #define WARN_BUFSIZE 256
34 #define CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS
37 const OnigSyntaxType OnigSyntaxRuby
= {
38 (( SYN_GNU_REGEX_OP
| ONIG_SYN_OP_QMARK_NON_GREEDY
|
39 ONIG_SYN_OP_ESC_OCTAL3
| ONIG_SYN_OP_ESC_X_HEX2
|
40 ONIG_SYN_OP_ESC_X_BRACE_HEX8
| ONIG_SYN_OP_ESC_CONTROL_CHARS
|
41 ONIG_SYN_OP_ESC_C_CONTROL
)
42 & ~ONIG_SYN_OP_ESC_LTGT_WORD_BEGIN_END
)
43 , ( ONIG_SYN_OP2_QMARK_GROUP_EFFECT
|
44 ONIG_SYN_OP2_OPTION_RUBY
|
45 ONIG_SYN_OP2_QMARK_LT_NAMED_GROUP
| ONIG_SYN_OP2_ESC_K_NAMED_BACKREF
|
46 ONIG_SYN_OP2_ESC_G_SUBEXP_CALL
|
47 ONIG_SYN_OP2_ESC_P_BRACE_CHAR_PROPERTY
|
48 ONIG_SYN_OP2_ESC_P_BRACE_CIRCUMFLEX_NOT
|
49 ONIG_SYN_OP2_PLUS_POSSESSIVE_REPEAT
|
50 ONIG_SYN_OP2_CCLASS_SET_OP
| ONIG_SYN_OP2_ESC_CAPITAL_C_BAR_CONTROL
|
51 ONIG_SYN_OP2_ESC_CAPITAL_M_BAR_META
| ONIG_SYN_OP2_ESC_V_VTAB
|
52 ONIG_SYN_OP2_ESC_H_XDIGIT
)
53 , ( SYN_GNU_REGEX_BV
|
54 ONIG_SYN_ALLOW_INTERVAL_LOW_ABBREV
|
55 ONIG_SYN_DIFFERENT_LEN_ALT_LOOK_BEHIND
|
56 ONIG_SYN_CAPTURE_ONLY_NAMED_GROUP
|
57 ONIG_SYN_ALLOW_MULTIPLEX_DEFINITION_NAME
|
58 ONIG_SYN_FIXED_INTERVAL_IS_GREEDY_ONLY
|
59 ONIG_SYN_WARN_CC_OP_NOT_ESCAPED
|
60 ONIG_SYN_WARN_REDUNDANT_NESTED_REPEAT
)
64 (OnigCodePoint
)'\\' /* esc */
65 , (OnigCodePoint
)ONIG_INEFFECTIVE_META_CHAR
/* anychar '.' */
66 , (OnigCodePoint
)ONIG_INEFFECTIVE_META_CHAR
/* anytime '*' */
67 , (OnigCodePoint
)ONIG_INEFFECTIVE_META_CHAR
/* zero or one time '?' */
68 , (OnigCodePoint
)ONIG_INEFFECTIVE_META_CHAR
/* one or more time '+' */
69 , (OnigCodePoint
)ONIG_INEFFECTIVE_META_CHAR
/* anychar anytime */
73 const OnigSyntaxType
* OnigDefaultSyntax
= ONIG_SYNTAX_RUBY
;
75 extern void onig_null_warn(const char* s ARG_UNUSED
) { }
77 #ifdef DEFAULT_WARN_FUNCTION
78 static OnigWarnFunc onig_warn
= (OnigWarnFunc
)DEFAULT_WARN_FUNCTION
;
80 static OnigWarnFunc onig_warn
= onig_null_warn
;
83 #ifdef DEFAULT_VERB_WARN_FUNCTION
84 static OnigWarnFunc onig_verb_warn
= (OnigWarnFunc
)DEFAULT_VERB_WARN_FUNCTION
;
86 static OnigWarnFunc onig_verb_warn
= onig_null_warn
;
89 extern void onig_set_warn_func(OnigWarnFunc f
)
94 extern void onig_set_verb_warn_func(OnigWarnFunc f
)
100 bbuf_free(BBuf
* bbuf
)
102 if (IS_NOT_NULL(bbuf
)) {
103 if (IS_NOT_NULL(bbuf
->p
)) xfree(bbuf
->p
);
109 bbuf_clone(BBuf
** rto
, BBuf
* from
)
114 *rto
= to
= (BBuf
* )xmalloc(sizeof(BBuf
));
115 CHECK_NULL_RETURN_MEMERR(to
);
116 r
= BBUF_INIT(to
, from
->alloc
);
117 if (r
!= 0) return r
;
118 to
->used
= from
->used
;
119 xmemcpy(to
->p
, from
->p
, from
->used
);
123 #define BACKREF_REL_TO_ABS(rel_no, env) \
124 ((env)->num_mem + 1 + (rel_no))
126 #define ONOFF(v,f,negative) (negative) ? ((v) &= ~(f)) : ((v) |= (f))
128 #define MBCODE_START_POS(enc) \
129 (OnigCodePoint )(ONIGENC_MBC_MINLEN(enc) > 1 ? 0 : 0x80)
131 #define SET_ALL_MULTI_BYTE_RANGE(enc, pbuf) \
132 add_code_range_to_buf(pbuf, MBCODE_START_POS(enc), ~((OnigCodePoint )0))
134 #define ADD_ALL_MULTI_BYTE_RANGE(enc, mbuf) do {\
135 if (! ONIGENC_IS_SINGLEBYTE(enc)) {\
136 r = SET_ALL_MULTI_BYTE_RANGE(enc, &(mbuf));\
142 #define BITSET_IS_EMPTY(bs,empty) do {\
145 for (i = 0; i < (int )BITSET_SIZE; i++) {\
153 bitset_set_range(BitSetRef bs
, int from
, int to
)
156 for (i
= from
; i
<= to
&& i
< SINGLE_BYTE_SIZE
; i
++) {
157 BITSET_SET_BIT(bs
, i
);
163 bitset_set_all(BitSetRef bs
)
166 for (i
= 0; i
< BITSET_SIZE
; i
++) { bs
[i
] = ~((Bits
)0); }
171 bitset_invert(BitSetRef bs
)
174 for (i
= 0; i
< (int )BITSET_SIZE
; i
++) { bs
[i
] = ~(bs
[i
]); }
178 bitset_invert_to(BitSetRef from
, BitSetRef to
)
181 for (i
= 0; i
< (int )BITSET_SIZE
; i
++) { to
[i
] = ~(from
[i
]); }
185 bitset_and(BitSetRef dest
, BitSetRef bs
)
188 for (i
= 0; i
< (int )BITSET_SIZE
; i
++) { dest
[i
] &= bs
[i
]; }
192 bitset_or(BitSetRef dest
, BitSetRef bs
)
195 for (i
= 0; i
< (int )BITSET_SIZE
; i
++) { dest
[i
] |= bs
[i
]; }
199 bitset_copy(BitSetRef dest
, BitSetRef bs
)
202 for (i
= 0; i
< (int )BITSET_SIZE
; i
++) { dest
[i
] = bs
[i
]; }
206 onig_strncmp(const UChar
* s1
, const UChar
* s2
, int n
)
218 onig_strcpy(UChar
* dest
, const UChar
* src
, const UChar
* end
)
222 xmemcpy(dest
, src
, len
);
223 dest
[len
] = (UChar
)0;
227 #ifdef USE_NAMED_GROUP
229 strdup_with_null(OnigEncoding enc
, UChar
* s
, UChar
* end
)
231 int slen
, term_len
, i
;
235 term_len
= ONIGENC_MBC_MINLEN(enc
);
237 r
= (UChar
* )xmalloc(slen
+ term_len
);
238 CHECK_NULL_RETURN(r
);
241 for (i
= 0; i
< term_len
; i
++)
242 r
[slen
+ i
] = (UChar
)0;
248 /* scan pattern methods */
251 #define PFETCH_READY UChar* pfetch_prev
252 #define PEND (p < end ? 0 : 1)
253 #define PUNFETCH p = pfetch_prev
256 p += enclen(enc, p, end); \
258 #define PFETCH(c) do { \
259 c = ((enc->max_enc_len == 1) ? *p : ONIGENC_MBC_TO_CODE(enc, p, end)); \
261 p += enclen(enc, p, end); \
264 #define PPEEK (p < end ? ONIGENC_MBC_TO_CODE(enc, p, end) : PEND_VALUE)
265 #define PPEEK_IS(c) (PPEEK == (OnigCodePoint )c)
268 strcat_capa(UChar
* dest
, UChar
* dest_end
, const UChar
* src
, const UChar
* src_end
,
274 r
= (UChar
* )xrealloc(dest
, capa
+ 1);
276 r
= (UChar
* )xmalloc(capa
+ 1);
278 CHECK_NULL_RETURN(r
);
279 onig_strcpy(r
+ (dest_end
- dest
), src
, src_end
);
283 /* dest on static area */
285 strcat_capa_from_static(UChar
* dest
, UChar
* dest_end
,
286 const UChar
* src
, const UChar
* src_end
, int capa
)
290 r
= (UChar
* )xmalloc(capa
+ 1);
291 CHECK_NULL_RETURN(r
);
292 onig_strcpy(r
, dest
, dest_end
);
293 onig_strcpy(r
+ (dest_end
- dest
), src
, src_end
);
298 #ifdef USE_ST_LIBRARY
308 str_end_cmp(st_str_end_key
* x
, st_str_end_key
* y
)
313 if ((x
->end
- x
->s
) != (y
->end
- y
->s
))
319 c
= (int )*p
- (int )*q
;
320 if (c
!= 0) return c
;
329 str_end_hash(st_str_end_key
* x
)
336 val
= val
* 997 + (int )*p
++;
339 return val
+ (val
>> 5);
342 extern hash_table_type
*
343 onig_st_init_strend_table_with_size(int size
)
345 static const struct st_hash_type hashType
= {
350 return (hash_table_type
* )
351 onig_st_init_table_with_size(&hashType
, size
);
355 onig_st_lookup_strend(hash_table_type
* table
, const UChar
* str_key
,
356 const UChar
* end_key
, hash_data_type
*value
)
360 key
.s
= (UChar
* )str_key
;
361 key
.end
= (UChar
* )end_key
;
363 return onig_st_lookup(table
, (st_data_t
)(&key
), value
);
367 onig_st_insert_strend(hash_table_type
* table
, const UChar
* str_key
,
368 const UChar
* end_key
, hash_data_type value
)
373 key
= (st_str_end_key
* )xmalloc(sizeof(st_str_end_key
));
374 key
->s
= (UChar
* )str_key
;
375 key
->end
= (UChar
* )end_key
;
376 result
= onig_st_insert(table
, (st_data_t
)key
, value
);
383 #endif /* USE_ST_LIBRARY */
386 #ifdef USE_NAMED_GROUP
388 #define INIT_NAME_BACKREFS_ALLOC_NUM 8
392 int name_len
; /* byte length */
393 int back_num
; /* number of backrefs */
399 #ifdef USE_ST_LIBRARY
401 typedef st_table NameTable
;
402 typedef st_data_t HashDataType
; /* 1.6 st.h doesn't define st_data_t type */
404 #define NAMEBUF_SIZE 24
405 #define NAMEBUF_SIZE_1 25
409 i_print_name_entry(UChar
* key
, NameEntry
* e
, void* arg
)
412 FILE* fp
= (FILE* )arg
;
414 fprintf(fp
, "%s: ", e
->name
);
415 if (e
->back_num
== 0)
417 else if (e
->back_num
== 1)
418 fprintf(fp
, "%d", e
->back_ref1
);
420 for (i
= 0; i
< e
->back_num
; i
++) {
421 if (i
> 0) fprintf(fp
, ", ");
422 fprintf(fp
, "%d", e
->back_refs
[i
]);
430 onig_print_names(FILE* fp
, regex_t
* reg
)
432 NameTable
* t
= (NameTable
* )reg
->name_table
;
434 if (IS_NOT_NULL(t
)) {
435 fprintf(fp
, "name table\n");
436 onig_st_foreach(t
, i_print_name_entry
, (HashDataType
)fp
);
441 #endif /* ONIG_DEBUG */
444 i_free_name_entry(UChar
* key
, NameEntry
* e
, void* arg ARG_UNUSED
)
447 if (IS_NOT_NULL(e
->back_refs
)) xfree(e
->back_refs
);
454 names_clear(regex_t
* reg
)
456 NameTable
* t
= (NameTable
* )reg
->name_table
;
458 if (IS_NOT_NULL(t
)) {
459 onig_st_foreach(t
, i_free_name_entry
, 0);
465 onig_names_free(regex_t
* reg
)
470 r
= names_clear(reg
);
473 t
= (NameTable
* )reg
->name_table
;
474 if (IS_NOT_NULL(t
)) onig_st_free_table(t
);
475 reg
->name_table
= (void* )NULL
;
480 name_find(regex_t
* reg
, const UChar
* name
, const UChar
* name_end
)
483 NameTable
* t
= (NameTable
* )reg
->name_table
;
485 e
= (NameEntry
* )NULL
;
486 if (IS_NOT_NULL(t
)) {
487 onig_st_lookup_strend(t
, name
, name_end
, (HashDataType
* )((void* )(&e
)));
493 int (*func
)(const UChar
*, const UChar
*,int,int*,regex_t
*,void*);
501 i_names(UChar
* key ARG_UNUSED
, NameEntry
* e
, INamesArg
* arg
)
503 int r
= (*(arg
->func
))(e
->name
,
504 e
->name
+ e
->name_len
,
506 (e
->back_num
> 1 ? e
->back_refs
: &(e
->back_ref1
)),
516 onig_foreach_name(regex_t
* reg
,
517 int (*func
)(const UChar
*, const UChar
*,int,int*,regex_t
*,void*), void* arg
)
520 NameTable
* t
= (NameTable
* )reg
->name_table
;
523 if (IS_NOT_NULL(t
)) {
527 narg
.enc
= reg
->enc
; /* should be pattern encoding. */
528 onig_st_foreach(t
, i_names
, (HashDataType
)&narg
);
534 i_renumber_name(UChar
* key ARG_UNUSED
, NameEntry
* e
, GroupNumRemap
* map
)
538 if (e
->back_num
> 1) {
539 for (i
= 0; i
< e
->back_num
; i
++) {
540 e
->back_refs
[i
] = map
[e
->back_refs
[i
]].new_val
;
543 else if (e
->back_num
== 1) {
544 e
->back_ref1
= map
[e
->back_ref1
].new_val
;
551 onig_renumber_name_table(regex_t
* reg
, GroupNumRemap
* map
)
553 NameTable
* t
= (NameTable
* )reg
->name_table
;
555 if (IS_NOT_NULL(t
)) {
556 onig_st_foreach(t
, i_renumber_name
, (HashDataType
)map
);
563 onig_number_of_names(regex_t
* reg
)
565 NameTable
* t
= (NameTable
* )reg
->name_table
;
568 return t
->num_entries
;
573 #else /* USE_ST_LIBRARY */
575 #define INIT_NAMES_ALLOC_NUM 8
585 onig_print_names(FILE* fp
, regex_t
* reg
)
589 NameTable
* t
= (NameTable
* )reg
->name_table
;
591 if (IS_NOT_NULL(t
) && t
->num
> 0) {
592 fprintf(fp
, "name table\n");
593 for (i
= 0; i
< t
->num
; i
++) {
595 fprintf(fp
, "%s: ", e
->name
);
596 if (e
->back_num
== 0) {
599 else if (e
->back_num
== 1) {
600 fprintf(fp
, "%d", e
->back_ref1
);
603 for (j
= 0; j
< e
->back_num
; j
++) {
604 if (j
> 0) fprintf(fp
, ", ");
605 fprintf(fp
, "%d", e
->back_refs
[j
]);
617 names_clear(regex_t
* reg
)
621 NameTable
* t
= (NameTable
* )reg
->name_table
;
623 if (IS_NOT_NULL(t
)) {
624 for (i
= 0; i
< t
->num
; i
++) {
626 if (IS_NOT_NULL(e
->name
)) {
632 if (IS_NOT_NULL(e
->back_refs
)) xfree(e
->back_refs
);
633 e
->back_refs
= (int* )NULL
;
636 if (IS_NOT_NULL(t
->e
)) {
646 onig_names_free(regex_t
* reg
)
651 r
= names_clear(reg
);
654 t
= (NameTable
* )reg
->name_table
;
655 if (IS_NOT_NULL(t
)) xfree(t
);
656 reg
->name_table
= NULL
;
661 name_find(regex_t
* reg
, UChar
* name
, UChar
* name_end
)
665 NameTable
* t
= (NameTable
* )reg
->name_table
;
667 if (IS_NOT_NULL(t
)) {
668 len
= name_end
- name
;
669 for (i
= 0; i
< t
->num
; i
++) {
671 if (len
== e
->name_len
&& onig_strncmp(name
, e
->name
, len
) == 0)
675 return (NameEntry
* )NULL
;
679 onig_foreach_name(regex_t
* reg
,
680 int (*func
)(const UChar
*, const UChar
*,int,int*,regex_t
*,void*), void* arg
)
684 NameTable
* t
= (NameTable
* )reg
->name_table
;
686 if (IS_NOT_NULL(t
)) {
687 for (i
= 0; i
< t
->num
; i
++) {
689 r
= (*func
)(e
->name
, e
->name
+ e
->name_len
, e
->back_num
,
690 (e
->back_num
> 1 ? e
->back_refs
: &(e
->back_ref1
)),
692 if (r
!= 0) return r
;
699 onig_number_of_names(regex_t
* reg
)
701 NameTable
* t
= (NameTable
* )reg
->name_table
;
709 #endif /* else USE_ST_LIBRARY */
712 name_add(regex_t
* reg
, UChar
* name
, UChar
* name_end
, int backref
, ScanEnv
* env
)
716 NameTable
* t
= (NameTable
* )reg
->name_table
;
718 if (name_end
- name
<= 0)
719 return ONIGERR_EMPTY_GROUP_NAME
;
721 e
= name_find(reg
, name
, name_end
);
723 #ifdef USE_ST_LIBRARY
725 t
= onig_st_init_strend_table_with_size(5);
726 reg
->name_table
= (void* )t
;
728 e
= (NameEntry
* )xmalloc(sizeof(NameEntry
));
729 CHECK_NULL_RETURN_MEMERR(e
);
731 e
->name
= strdup_with_null(reg
->enc
, name
, name_end
);
732 if (IS_NULL(e
->name
)) {
734 return ONIGERR_MEMORY
;
736 onig_st_insert_strend(t
, e
->name
, (e
->name
+ (name_end
- name
)),
739 e
->name_len
= name_end
- name
;
742 e
->back_refs
= (int* )NULL
;
747 alloc
= INIT_NAMES_ALLOC_NUM
;
748 t
= (NameTable
* )xmalloc(sizeof(NameTable
));
749 CHECK_NULL_RETURN_MEMERR(t
);
754 t
->e
= (NameEntry
* )xmalloc(sizeof(NameEntry
) * alloc
);
757 return ONIGERR_MEMORY
;
763 else if (t
->num
== t
->alloc
) {
766 alloc
= t
->alloc
* 2;
767 t
->e
= (NameEntry
* )xrealloc(t
->e
, sizeof(NameEntry
) * alloc
);
768 CHECK_NULL_RETURN_MEMERR(t
->e
);
772 for (i
= t
->num
; i
< t
->alloc
; i
++) {
774 t
->e
[i
].name_len
= 0;
775 t
->e
[i
].back_num
= 0;
776 t
->e
[i
].back_alloc
= 0;
777 t
->e
[i
].back_refs
= (int* )NULL
;
782 e
->name
= strdup_with_null(reg
->enc
, name
, name_end
);
783 e
->name_len
= name_end
- name
;
787 if (e
->back_num
>= 1 &&
788 ! IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_ALLOW_MULTIPLEX_DEFINITION_NAME
)) {
789 onig_scan_env_set_error_string(env
, ONIGERR_MULTIPLEX_DEFINED_NAME
,
791 return ONIGERR_MULTIPLEX_DEFINED_NAME
;
795 if (e
->back_num
== 1) {
796 e
->back_ref1
= backref
;
799 if (e
->back_num
== 2) {
800 alloc
= INIT_NAME_BACKREFS_ALLOC_NUM
;
801 e
->back_refs
= (int* )xmalloc(sizeof(int) * alloc
);
802 CHECK_NULL_RETURN_MEMERR(e
->back_refs
);
803 e
->back_alloc
= alloc
;
804 e
->back_refs
[0] = e
->back_ref1
;
805 e
->back_refs
[1] = backref
;
808 if (e
->back_num
> e
->back_alloc
) {
809 alloc
= e
->back_alloc
* 2;
810 e
->back_refs
= (int* )xrealloc(e
->back_refs
, sizeof(int) * alloc
);
811 CHECK_NULL_RETURN_MEMERR(e
->back_refs
);
812 e
->back_alloc
= alloc
;
814 e
->back_refs
[e
->back_num
- 1] = backref
;
822 onig_name_to_group_numbers(regex_t
* reg
, const UChar
* name
,
823 const UChar
* name_end
, int** nums
)
825 NameEntry
* e
= name_find(reg
, name
, name_end
);
827 if (IS_NULL(e
)) return ONIGERR_UNDEFINED_NAME_REFERENCE
;
829 switch (e
->back_num
) {
834 *nums
= &(e
->back_ref1
);
837 *nums
= e
->back_refs
;
844 onig_name_to_backref_number(regex_t
* reg
, const UChar
* name
,
845 const UChar
* name_end
, OnigRegion
*region
)
849 n
= onig_name_to_group_numbers(reg
, name
, name_end
, &nums
);
853 return ONIGERR_PARSER_BUG
;
857 if (IS_NOT_NULL(region
)) {
858 for (i
= n
- 1; i
>= 0; i
--) {
859 if (region
->beg
[nums
[i
]] != ONIG_REGION_NOTPOS
)
867 #else /* USE_NAMED_GROUP */
870 onig_name_to_group_numbers(regex_t
* reg
, const UChar
* name
,
871 const UChar
* name_end
, int** nums
)
873 return ONIG_NO_SUPPORT_CONFIG
;
877 onig_name_to_backref_number(regex_t
* reg
, const UChar
* name
,
878 const UChar
* name_end
, OnigRegion
* region
)
880 return ONIG_NO_SUPPORT_CONFIG
;
884 onig_foreach_name(regex_t
* reg
,
885 int (*func
)(const UChar
*, const UChar
*,int,int*,regex_t
*,void*), void* arg
)
887 return ONIG_NO_SUPPORT_CONFIG
;
891 onig_number_of_names(regex_t
* reg
)
895 #endif /* else USE_NAMED_GROUP */
898 onig_noname_group_capture_is_active(regex_t
* reg
)
900 if (ONIG_IS_OPTION_ON(reg
->options
, ONIG_OPTION_DONT_CAPTURE_GROUP
))
903 #ifdef USE_NAMED_GROUP
904 if (onig_number_of_names(reg
) > 0 &&
905 IS_SYNTAX_BV(reg
->syntax
, ONIG_SYN_CAPTURE_ONLY_NAMED_GROUP
) &&
906 !ONIG_IS_OPTION_ON(reg
->options
, ONIG_OPTION_CAPTURE_GROUP
)) {
915 #define INIT_SCANENV_MEMNODES_ALLOC_SIZE 16
918 scan_env_clear(ScanEnv
* env
)
922 BIT_STATUS_CLEAR(env
->capture_history
);
923 BIT_STATUS_CLEAR(env
->bt_mem_start
);
924 BIT_STATUS_CLEAR(env
->bt_mem_end
);
925 BIT_STATUS_CLEAR(env
->backrefed_mem
);
926 env
->error
= (UChar
* )NULL
;
927 env
->error_end
= (UChar
* )NULL
;
930 #ifdef USE_NAMED_GROUP
934 env
->mem_nodes_dynamic
= (Node
** )NULL
;
936 for (i
= 0; i
< SCANENV_MEMNODES_SIZE
; i
++)
937 env
->mem_nodes_static
[i
] = NULL_NODE
;
939 #ifdef USE_COMBINATION_EXPLOSION_CHECK
940 env
->num_comb_exp_check
= 0;
941 env
->comb_exp_max_regnum
= 0;
942 env
->curr_max_regnum
= 0;
943 env
->has_recursion
= 0;
948 scan_env_add_mem_entry(ScanEnv
* env
)
953 need
= env
->num_mem
+ 1;
954 if (need
>= SCANENV_MEMNODES_SIZE
) {
955 if (env
->mem_alloc
<= need
) {
956 if (IS_NULL(env
->mem_nodes_dynamic
)) {
957 alloc
= INIT_SCANENV_MEMNODES_ALLOC_SIZE
;
958 p
= (Node
** )xmalloc(sizeof(Node
*) * alloc
);
959 xmemcpy(p
, env
->mem_nodes_static
,
960 sizeof(Node
*) * SCANENV_MEMNODES_SIZE
);
963 alloc
= env
->mem_alloc
* 2;
964 p
= (Node
** )xrealloc(env
->mem_nodes_dynamic
, sizeof(Node
*) * alloc
);
966 CHECK_NULL_RETURN_MEMERR(p
);
968 for (i
= env
->num_mem
+ 1; i
< alloc
; i
++)
971 env
->mem_nodes_dynamic
= p
;
972 env
->mem_alloc
= alloc
;
981 scan_env_set_mem_node(ScanEnv
* env
, int num
, Node
* node
)
983 if (env
->num_mem
>= num
)
984 SCANENV_MEM_NODES(env
)[num
] = node
;
986 return ONIGERR_PARSER_BUG
;
991 #ifdef USE_PARSE_TREE_NODE_RECYCLE
992 typedef struct _FreeNode
{
993 struct _FreeNode
* next
;
996 static FreeNode
* FreeNodeList
= (FreeNode
* )NULL
;
1000 onig_node_free(Node
* node
)
1003 if (IS_NULL(node
)) return ;
1005 switch (NTYPE(node
)) {
1007 if (NSTR(node
)->capa
!= 0 &&
1008 IS_NOT_NULL(NSTR(node
)->s
) && NSTR(node
)->s
!= NSTR(node
)->buf
) {
1009 xfree(NSTR(node
)->s
);
1015 onig_node_free(NCAR(node
));
1017 Node
* next_node
= NCDR(node
);
1019 #ifdef USE_PARSE_TREE_NODE_RECYCLE
1021 FreeNode
* n
= (FreeNode
* )node
;
1023 THREAD_ATOMIC_START
;
1024 n
->next
= FreeNodeList
;
1038 CClassNode
* cc
= NCCLASS(node
);
1040 if (IS_NCCLASS_SHARE(cc
)) return ;
1042 bbuf_free(cc
->mbuf
);
1047 if (NQTFR(node
)->target
)
1048 onig_node_free(NQTFR(node
)->target
);
1052 if (NENCLOSE(node
)->target
)
1053 onig_node_free(NENCLOSE(node
)->target
);
1057 if (IS_NOT_NULL(NBREF(node
)->back_dynamic
))
1058 xfree(NBREF(node
)->back_dynamic
);
1062 if (NANCHOR(node
)->target
)
1063 onig_node_free(NANCHOR(node
)->target
);
1067 #ifdef USE_PARSE_TREE_NODE_RECYCLE
1069 FreeNode
* n
= (FreeNode
* )node
;
1071 THREAD_ATOMIC_START
;
1072 n
->next
= FreeNodeList
;
1081 #ifdef USE_PARSE_TREE_NODE_RECYCLE
1083 onig_free_node_list(void)
1087 /* THREAD_ATOMIC_START; */
1088 while (IS_NOT_NULL(FreeNodeList
)) {
1090 FreeNodeList
= FreeNodeList
->next
;
1093 /* THREAD_ATOMIC_END; */
1103 #ifdef USE_PARSE_TREE_NODE_RECYCLE
1104 THREAD_ATOMIC_START
;
1105 if (IS_NOT_NULL(FreeNodeList
)) {
1106 node
= (Node
* )FreeNodeList
;
1107 FreeNodeList
= FreeNodeList
->next
;
1114 node
= (Node
* )xmalloc(sizeof(Node
));
1115 /* xmemset(node, 0, sizeof(Node)); */
1121 initialize_cclass(CClassNode
* cc
)
1123 BITSET_CLEAR(cc
->bs
);
1124 /* cc->base.flags = 0; */
1130 node_new_cclass(void)
1132 Node
* node
= node_new();
1133 CHECK_NULL_RETURN(node
);
1135 SET_NTYPE(node
, NT_CCLASS
);
1136 initialize_cclass(NCCLASS(node
));
1141 node_new_cclass_by_codepoint_range(int not, OnigCodePoint sb_out
,
1142 const OnigCodePoint ranges
[])
1148 Node
* node
= node_new_cclass();
1149 CHECK_NULL_RETURN(node
);
1152 if (not != 0) NCCLASS_SET_NOT(cc
);
1154 BITSET_CLEAR(cc
->bs
);
1155 if (sb_out
> 0 && IS_NOT_NULL(ranges
)) {
1156 n
= ONIGENC_CODE_RANGE_NUM(ranges
);
1157 for (i
= 0; i
< n
; i
++) {
1158 for (j
= ONIGENC_CODE_RANGE_FROM(ranges
, i
);
1159 j
<= (OnigCodePoint
)ONIGENC_CODE_RANGE_TO(ranges
, i
); j
++) {
1160 if (j
>= sb_out
) goto sb_end
;
1162 BITSET_SET_BIT(cc
->bs
, j
);
1168 if (IS_NULL(ranges
)) {
1175 n
= ONIGENC_CODE_RANGE_NUM(ranges
);
1176 if (n
== 0) goto is_null
;
1178 bbuf
= (BBuf
* )xmalloc(sizeof(BBuf
));
1179 CHECK_NULL_RETURN(bbuf
);
1180 bbuf
->alloc
= n
+ 1;
1182 bbuf
->p
= (UChar
* )((void* )ranges
);
1191 node_new_ctype(int type
, int not)
1193 Node
* node
= node_new();
1194 CHECK_NULL_RETURN(node
);
1196 SET_NTYPE(node
, NT_CTYPE
);
1197 NCTYPE(node
)->ctype
= type
;
1198 NCTYPE(node
)->not = not;
1203 node_new_anychar(void)
1205 Node
* node
= node_new();
1206 CHECK_NULL_RETURN(node
);
1208 SET_NTYPE(node
, NT_CANY
);
1213 node_new_list(Node
* left
, Node
* right
)
1215 Node
* node
= node_new();
1216 CHECK_NULL_RETURN(node
);
1218 SET_NTYPE(node
, NT_LIST
);
1225 onig_node_new_list(Node
* left
, Node
* right
)
1227 return node_new_list(left
, right
);
1231 onig_node_list_add(Node
* list
, Node
* x
)
1235 n
= onig_node_new_list(x
, NULL
);
1236 if (IS_NULL(n
)) return NULL_NODE
;
1238 if (IS_NOT_NULL(list
)) {
1239 while (IS_NOT_NULL(NCDR(list
)))
1249 onig_node_new_alt(Node
* left
, Node
* right
)
1251 Node
* node
= node_new();
1252 CHECK_NULL_RETURN(node
);
1254 SET_NTYPE(node
, NT_ALT
);
1261 onig_node_new_anchor(int type
)
1263 Node
* node
= node_new();
1264 CHECK_NULL_RETURN(node
);
1266 SET_NTYPE(node
, NT_ANCHOR
);
1267 NANCHOR(node
)->type
= type
;
1268 NANCHOR(node
)->target
= NULL
;
1269 NANCHOR(node
)->char_len
= -1;
1274 node_new_backref(int back_num
, int* backrefs
, int by_name
,
1275 #ifdef USE_BACKREF_WITH_LEVEL
1276 int exist_level
, int nest_level
,
1281 Node
* node
= node_new();
1283 CHECK_NULL_RETURN(node
);
1285 SET_NTYPE(node
, NT_BREF
);
1286 NBREF(node
)->state
= 0;
1287 NBREF(node
)->back_num
= back_num
;
1288 NBREF(node
)->back_dynamic
= (int* )NULL
;
1290 NBREF(node
)->state
|= NST_NAME_REF
;
1292 #ifdef USE_BACKREF_WITH_LEVEL
1293 if (exist_level
!= 0) {
1294 NBREF(node
)->state
|= NST_NEST_LEVEL
;
1295 NBREF(node
)->nest_level
= nest_level
;
1299 for (i
= 0; i
< back_num
; i
++) {
1300 if (backrefs
[i
] <= env
->num_mem
&&
1301 IS_NULL(SCANENV_MEM_NODES(env
)[backrefs
[i
]])) {
1302 NBREF(node
)->state
|= NST_RECURSION
; /* /...(\1).../ */
1307 if (back_num
<= NODE_BACKREFS_SIZE
) {
1308 for (i
= 0; i
< back_num
; i
++)
1309 NBREF(node
)->back_static
[i
] = backrefs
[i
];
1312 int* p
= (int* )xmalloc(sizeof(int) * back_num
);
1314 onig_node_free(node
);
1317 NBREF(node
)->back_dynamic
= p
;
1318 for (i
= 0; i
< back_num
; i
++)
1324 #ifdef USE_SUBEXP_CALL
1326 node_new_call(UChar
* name
, UChar
* name_end
, int gnum
)
1328 Node
* node
= node_new();
1329 CHECK_NULL_RETURN(node
);
1331 SET_NTYPE(node
, NT_CALL
);
1332 NCALL(node
)->state
= 0;
1333 NCALL(node
)->target
= NULL_NODE
;
1334 NCALL(node
)->name
= name
;
1335 NCALL(node
)->name_end
= name_end
;
1336 NCALL(node
)->group_num
= gnum
; /* call by number if gnum != 0 */
1342 node_new_quantifier(int lower
, int upper
, int by_number
)
1344 Node
* node
= node_new();
1345 CHECK_NULL_RETURN(node
);
1347 SET_NTYPE(node
, NT_QTFR
);
1348 NQTFR(node
)->state
= 0;
1349 NQTFR(node
)->target
= NULL
;
1350 NQTFR(node
)->lower
= lower
;
1351 NQTFR(node
)->upper
= upper
;
1352 NQTFR(node
)->greedy
= 1;
1353 NQTFR(node
)->target_empty_info
= NQ_TARGET_ISNOT_EMPTY
;
1354 NQTFR(node
)->head_exact
= NULL_NODE
;
1355 NQTFR(node
)->next_head_exact
= NULL_NODE
;
1356 NQTFR(node
)->is_refered
= 0;
1358 NQTFR(node
)->state
|= NST_BY_NUMBER
;
1360 #ifdef USE_COMBINATION_EXPLOSION_CHECK
1361 NQTFR(node
)->comb_exp_check_num
= 0;
1368 node_new_enclose(int type
)
1370 Node
* node
= node_new();
1371 CHECK_NULL_RETURN(node
);
1373 SET_NTYPE(node
, NT_ENCLOSE
);
1374 NENCLOSE(node
)->type
= type
;
1375 NENCLOSE(node
)->state
= 0;
1376 NENCLOSE(node
)->regnum
= 0;
1377 NENCLOSE(node
)->option
= 0;
1378 NENCLOSE(node
)->target
= NULL
;
1379 NENCLOSE(node
)->call_addr
= -1;
1380 NENCLOSE(node
)->opt_count
= 0;
1385 onig_node_new_enclose(int type
)
1387 return node_new_enclose(type
);
1391 node_new_enclose_memory(OnigOptionType option
, int is_named
)
1393 Node
* node
= node_new_enclose(ENCLOSE_MEMORY
);
1394 CHECK_NULL_RETURN(node
);
1396 SET_ENCLOSE_STATUS(node
, NST_NAMED_GROUP
);
1398 #ifdef USE_SUBEXP_CALL
1399 NENCLOSE(node
)->option
= option
;
1405 node_new_option(OnigOptionType option
)
1407 Node
* node
= node_new_enclose(ENCLOSE_OPTION
);
1408 CHECK_NULL_RETURN(node
);
1409 NENCLOSE(node
)->option
= option
;
1414 onig_node_str_cat(Node
* node
, const UChar
* s
, const UChar
* end
)
1416 int addlen
= end
- s
;
1419 int len
= NSTR(node
)->end
- NSTR(node
)->s
;
1421 if (NSTR(node
)->capa
> 0 || (len
+ addlen
> NODE_STR_BUF_SIZE
- 1)) {
1423 int capa
= len
+ addlen
+ NODE_STR_MARGIN
;
1425 if (capa
<= NSTR(node
)->capa
) {
1426 onig_strcpy(NSTR(node
)->s
+ len
, s
, end
);
1429 if (NSTR(node
)->s
== NSTR(node
)->buf
)
1430 p
= strcat_capa_from_static(NSTR(node
)->s
, NSTR(node
)->end
,
1433 p
= strcat_capa(NSTR(node
)->s
, NSTR(node
)->end
, s
, end
, capa
);
1435 CHECK_NULL_RETURN_MEMERR(p
);
1437 NSTR(node
)->capa
= capa
;
1441 onig_strcpy(NSTR(node
)->s
+ len
, s
, end
);
1443 NSTR(node
)->end
= NSTR(node
)->s
+ len
+ addlen
;
1450 onig_node_str_set(Node
* node
, const UChar
* s
, const UChar
* end
)
1452 onig_node_str_clear(node
);
1453 return onig_node_str_cat(node
, s
, end
);
1457 node_str_cat_char(Node
* node
, UChar c
)
1462 return onig_node_str_cat(node
, s
, s
+ 1);
1466 onig_node_conv_to_str_node(Node
* node
, int flag
)
1468 SET_NTYPE(node
, NT_STR
);
1469 NSTR(node
)->flag
= flag
;
1470 NSTR(node
)->capa
= 0;
1471 NSTR(node
)->s
= NSTR(node
)->buf
;
1472 NSTR(node
)->end
= NSTR(node
)->buf
;
1476 onig_node_str_clear(Node
* node
)
1478 if (NSTR(node
)->capa
!= 0 &&
1479 IS_NOT_NULL(NSTR(node
)->s
) && NSTR(node
)->s
!= NSTR(node
)->buf
) {
1480 xfree(NSTR(node
)->s
);
1483 NSTR(node
)->capa
= 0;
1484 NSTR(node
)->flag
= 0;
1485 NSTR(node
)->s
= NSTR(node
)->buf
;
1486 NSTR(node
)->end
= NSTR(node
)->buf
;
1490 node_new_str(const UChar
* s
, const UChar
* end
)
1492 Node
* node
= node_new();
1493 CHECK_NULL_RETURN(node
);
1495 SET_NTYPE(node
, NT_STR
);
1496 NSTR(node
)->capa
= 0;
1497 NSTR(node
)->flag
= 0;
1498 NSTR(node
)->s
= NSTR(node
)->buf
;
1499 NSTR(node
)->end
= NSTR(node
)->buf
;
1500 if (onig_node_str_cat(node
, s
, end
)) {
1501 onig_node_free(node
);
1508 onig_node_new_str(const UChar
* s
, const UChar
* end
)
1510 return node_new_str(s
, end
);
1514 node_new_str_raw(UChar
* s
, UChar
* end
)
1516 Node
* node
= node_new_str(s
, end
);
1517 NSTRING_SET_RAW(node
);
1522 node_new_empty(void)
1524 return node_new_str(NULL
, NULL
);
1528 node_new_str_raw_char(UChar c
)
1533 return node_new_str_raw(p
, p
+ 1);
1537 str_node_split_last_char(StrNode
* sn
, OnigEncoding enc
)
1540 Node
* n
= NULL_NODE
;
1542 if (sn
->end
> sn
->s
) {
1543 p
= onigenc_get_prev_char_head(enc
, sn
->s
, sn
->end
);
1544 if (p
&& p
> sn
->s
) { /* can be splitted. */
1545 n
= node_new_str(p
, sn
->end
);
1546 if ((sn
->flag
& NSTR_RAW
) != 0)
1548 sn
->end
= (UChar
* )p
;
1555 str_node_can_be_split(StrNode
* sn
, OnigEncoding enc
)
1557 if (sn
->end
> sn
->s
) {
1558 return ((enclen(enc
, sn
->s
, sn
->end
) < sn
->end
- sn
->s
) ? 1 : 0);
1563 #ifdef USE_PAD_TO_SHORT_BYTE_CHAR
1565 node_str_head_pad(StrNode
* sn
, int num
, UChar val
)
1567 UChar buf
[NODE_STR_BUF_SIZE
];
1570 len
= sn
->end
- sn
->s
;
1571 onig_strcpy(buf
, sn
->s
, sn
->end
);
1572 onig_strcpy(&(sn
->s
[num
]), buf
, buf
+ len
);
1575 for (i
= 0; i
< num
; i
++) {
1582 onig_scan_unsigned_number(UChar
** src
, const UChar
* end
, OnigEncoding enc
)
1584 unsigned int num
, val
;
1592 if (ONIGENC_IS_CODE_DIGIT(enc
, c
)) {
1593 val
= (unsigned int )DIGITVAL(c
);
1594 if ((INT_MAX_LIMIT
- val
) / 10UL < num
)
1595 return -1; /* overflow */
1597 num
= num
* 10 + val
;
1609 scan_unsigned_hexadecimal_number(UChar
** src
, UChar
* end
, int maxlen
,
1613 unsigned int num
, val
;
1618 while (!PEND
&& maxlen
-- != 0) {
1620 if (ONIGENC_IS_CODE_XDIGIT(enc
, c
)) {
1621 val
= (unsigned int )XDIGITVAL(enc
,c
);
1622 if ((INT_MAX_LIMIT
- val
) / 16UL < num
)
1623 return -1; /* overflow */
1625 num
= (num
<< 4) + XDIGITVAL(enc
,c
);
1637 scan_unsigned_octal_number(UChar
** src
, UChar
* end
, int maxlen
,
1641 unsigned int num
, val
;
1646 while (!PEND
&& maxlen
-- != 0) {
1648 if (ONIGENC_IS_CODE_DIGIT(enc
, c
) && c
< '8') {
1650 if ((INT_MAX_LIMIT
- val
) / 8UL < num
)
1651 return -1; /* overflow */
1653 num
= (num
<< 3) + val
;
1665 #define BBUF_WRITE_CODE_POINT(bbuf,pos,code) \
1666 BBUF_WRITE(bbuf, pos, &(code), SIZE_CODE_POINT)
1669 [n][from-1][to-1][from-2][to-2] ... [from-n][to-n]
1670 (all data size is OnigCodePoint)
1673 new_code_range(BBuf
** pbuf
)
1675 #define INIT_MULTI_BYTE_RANGE_SIZE (SIZE_CODE_POINT * 5)
1680 bbuf
= *pbuf
= (BBuf
* )xmalloc(sizeof(BBuf
));
1681 CHECK_NULL_RETURN_MEMERR(*pbuf
);
1682 r
= BBUF_INIT(*pbuf
, INIT_MULTI_BYTE_RANGE_SIZE
);
1686 BBUF_WRITE_CODE_POINT(bbuf
, 0, n
);
1691 add_code_range_to_buf(BBuf
** pbuf
, OnigCodePoint from
, OnigCodePoint to
)
1694 int low
, high
, bound
, x
;
1695 OnigCodePoint n
, *data
;
1699 n
= from
; from
= to
; to
= n
;
1702 if (IS_NULL(*pbuf
)) {
1703 r
= new_code_range(pbuf
);
1710 GET_CODE_POINT(n
, bbuf
->p
);
1712 data
= (OnigCodePoint
* )(bbuf
->p
);
1715 for (low
= 0, bound
= n
; low
< bound
; ) {
1716 x
= (low
+ bound
) >> 1;
1717 if (from
> data
[x
*2 + 1])
1723 for (high
= low
, bound
= n
; high
< bound
; ) {
1724 x
= (high
+ bound
) >> 1;
1725 if (to
>= data
[x
*2] - 1)
1731 inc_n
= low
+ 1 - high
;
1732 if (n
+ inc_n
> ONIG_MAX_MULTI_BYTE_RANGES_NUM
)
1733 return ONIGERR_TOO_MANY_MULTI_BYTE_RANGES
;
1736 if (from
> data
[low
*2])
1738 if (to
< data
[(high
- 1)*2 + 1])
1739 to
= data
[(high
- 1)*2 + 1];
1742 if (inc_n
!= 0 && (OnigCodePoint
)high
< n
) {
1743 int from_pos
= SIZE_CODE_POINT
* (1 + high
* 2);
1744 int to_pos
= SIZE_CODE_POINT
* (1 + (low
+ 1) * 2);
1745 int size
= (n
- high
) * 2 * SIZE_CODE_POINT
;
1748 BBUF_MOVE_RIGHT(bbuf
, from_pos
, to_pos
, size
);
1751 BBUF_MOVE_LEFT_REDUCE(bbuf
, from_pos
, to_pos
);
1755 pos
= SIZE_CODE_POINT
* (1 + low
* 2);
1756 BBUF_ENSURE_SIZE(bbuf
, pos
+ SIZE_CODE_POINT
* 2);
1757 BBUF_WRITE_CODE_POINT(bbuf
, pos
, from
);
1758 BBUF_WRITE_CODE_POINT(bbuf
, pos
+ SIZE_CODE_POINT
, to
);
1760 BBUF_WRITE_CODE_POINT(bbuf
, 0, n
);
1766 add_code_range(BBuf
** pbuf
, ScanEnv
* env
, OnigCodePoint from
, OnigCodePoint to
)
1769 if (IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_ALLOW_EMPTY_RANGE_IN_CC
))
1772 return ONIGERR_EMPTY_RANGE_IN_CHAR_CLASS
;
1775 return add_code_range_to_buf(pbuf
, from
, to
);
1779 not_code_range_buf(OnigEncoding enc
, BBuf
* bbuf
, BBuf
** pbuf
)
1782 OnigCodePoint pre
, from
, *data
, to
= 0;
1784 *pbuf
= (BBuf
* )NULL
;
1785 if (IS_NULL(bbuf
)) {
1787 return SET_ALL_MULTI_BYTE_RANGE(enc
, pbuf
);
1790 data
= (OnigCodePoint
* )(bbuf
->p
);
1791 GET_CODE_POINT(n
, data
);
1793 if (n
<= 0) goto set_all
;
1796 pre
= MBCODE_START_POS(enc
);
1797 for (i
= 0; i
< n
; i
++) {
1800 if (pre
<= from
- 1) {
1801 r
= add_code_range_to_buf(pbuf
, pre
, from
- 1);
1802 if (r
!= 0) return r
;
1804 if (to
== ~((OnigCodePoint
)0)) break;
1807 if (to
< ~((OnigCodePoint
)0)) {
1808 r
= add_code_range_to_buf(pbuf
, to
+ 1, ~((OnigCodePoint
)0));
1813 #define SWAP_BBUF_NOT(bbuf1, not1, bbuf2, not2) do {\
1816 tnot = not1; not1 = not2; not2 = tnot; \
1817 tbuf = bbuf1; bbuf1 = bbuf2; bbuf2 = tbuf; \
1821 or_code_range_buf(OnigEncoding enc
, BBuf
* bbuf1
, int not1
,
1822 BBuf
* bbuf2
, int not2
, BBuf
** pbuf
)
1825 OnigCodePoint i
, n1
, *data1
;
1826 OnigCodePoint from
, to
;
1828 *pbuf
= (BBuf
* )NULL
;
1829 if (IS_NULL(bbuf1
) && IS_NULL(bbuf2
)) {
1830 if (not1
!= 0 || not2
!= 0)
1831 return SET_ALL_MULTI_BYTE_RANGE(enc
, pbuf
);
1837 SWAP_BBUF_NOT(bbuf1
, not1
, bbuf2
, not2
);
1839 if (IS_NULL(bbuf1
)) {
1841 return SET_ALL_MULTI_BYTE_RANGE(enc
, pbuf
);
1845 return bbuf_clone(pbuf
, bbuf2
);
1848 return not_code_range_buf(enc
, bbuf2
, pbuf
);
1854 SWAP_BBUF_NOT(bbuf1
, not1
, bbuf2
, not2
);
1856 data1
= (OnigCodePoint
* )(bbuf1
->p
);
1857 GET_CODE_POINT(n1
, data1
);
1860 if (not2
== 0 && not1
== 0) { /* 1 OR 2 */
1861 r
= bbuf_clone(pbuf
, bbuf2
);
1863 else if (not1
== 0) { /* 1 OR (not 2) */
1864 r
= not_code_range_buf(enc
, bbuf2
, pbuf
);
1866 if (r
!= 0) return r
;
1868 for (i
= 0; i
< n1
; i
++) {
1871 r
= add_code_range_to_buf(pbuf
, from
, to
);
1872 if (r
!= 0) return r
;
1878 and_code_range1(BBuf
** pbuf
, OnigCodePoint from1
, OnigCodePoint to1
,
1879 OnigCodePoint
* data
, int n
)
1882 OnigCodePoint from2
, to2
;
1884 for (i
= 0; i
< n
; i
++) {
1887 if (from2
< from1
) {
1888 if (to2
< from1
) continue;
1893 else if (from2
<= to1
) {
1895 if (from1
<= from2
- 1) {
1896 r
= add_code_range_to_buf(pbuf
, from1
, from2
-1);
1897 if (r
!= 0) return r
;
1908 if (from1
> to1
) break;
1911 r
= add_code_range_to_buf(pbuf
, from1
, to1
);
1912 if (r
!= 0) return r
;
1918 and_code_range_buf(BBuf
* bbuf1
, int not1
, BBuf
* bbuf2
, int not2
, BBuf
** pbuf
)
1921 OnigCodePoint i
, j
, n1
, n2
, *data1
, *data2
;
1922 OnigCodePoint from
, to
, from1
, to1
, from2
, to2
;
1924 *pbuf
= (BBuf
* )NULL
;
1925 if (IS_NULL(bbuf1
)) {
1926 if (not1
!= 0 && IS_NOT_NULL(bbuf2
)) /* not1 != 0 -> not2 == 0 */
1927 return bbuf_clone(pbuf
, bbuf2
);
1930 else if (IS_NULL(bbuf2
)) {
1932 return bbuf_clone(pbuf
, bbuf1
);
1937 SWAP_BBUF_NOT(bbuf1
, not1
, bbuf2
, not2
);
1939 data1
= (OnigCodePoint
* )(bbuf1
->p
);
1940 data2
= (OnigCodePoint
* )(bbuf2
->p
);
1941 GET_CODE_POINT(n1
, data1
);
1942 GET_CODE_POINT(n2
, data2
);
1946 if (not2
== 0 && not1
== 0) { /* 1 AND 2 */
1947 for (i
= 0; i
< n1
; i
++) {
1950 for (j
= 0; j
< n2
; j
++) {
1953 if (from2
> to1
) break;
1954 if (to2
< from1
) continue;
1955 from
= MAX(from1
, from2
);
1957 r
= add_code_range_to_buf(pbuf
, from
, to
);
1958 if (r
!= 0) return r
;
1962 else if (not1
== 0) { /* 1 AND (not 2) */
1963 for (i
= 0; i
< n1
; i
++) {
1966 r
= and_code_range1(pbuf
, from1
, to1
, data2
, n2
);
1967 if (r
!= 0) return r
;
1975 and_cclass(CClassNode
* dest
, CClassNode
* cc
, OnigEncoding enc
)
1978 BBuf
*buf1
, *buf2
, *pbuf
;
1979 BitSetRef bsr1
, bsr2
;
1982 not1
= IS_NCCLASS_NOT(dest
);
1985 not2
= IS_NCCLASS_NOT(cc
);
1990 bitset_invert_to(bsr1
, bs1
);
1994 bitset_invert_to(bsr2
, bs2
);
1997 bitset_and(bsr1
, bsr2
);
1998 if (bsr1
!= dest
->bs
) {
1999 bitset_copy(dest
->bs
, bsr1
);
2003 bitset_invert(dest
->bs
);
2006 if (! ONIGENC_IS_SINGLEBYTE(enc
)) {
2007 if (not1
!= 0 && not2
!= 0) {
2008 r
= or_code_range_buf(enc
, buf1
, 0, buf2
, 0, &pbuf
);
2011 r
= and_code_range_buf(buf1
, not1
, buf2
, not2
, &pbuf
);
2012 if (r
== 0 && not1
!= 0) {
2014 r
= not_code_range_buf(enc
, pbuf
, &tbuf
);
2023 if (r
!= 0) return r
;
2033 or_cclass(CClassNode
* dest
, CClassNode
* cc
, OnigEncoding enc
)
2036 BBuf
*buf1
, *buf2
, *pbuf
;
2037 BitSetRef bsr1
, bsr2
;
2040 not1
= IS_NCCLASS_NOT(dest
);
2043 not2
= IS_NCCLASS_NOT(cc
);
2048 bitset_invert_to(bsr1
, bs1
);
2052 bitset_invert_to(bsr2
, bs2
);
2055 bitset_or(bsr1
, bsr2
);
2056 if (bsr1
!= dest
->bs
) {
2057 bitset_copy(dest
->bs
, bsr1
);
2061 bitset_invert(dest
->bs
);
2064 if (! ONIGENC_IS_SINGLEBYTE(enc
)) {
2065 if (not1
!= 0 && not2
!= 0) {
2066 r
= and_code_range_buf(buf1
, 0, buf2
, 0, &pbuf
);
2069 r
= or_code_range_buf(enc
, buf1
, not1
, buf2
, not2
, &pbuf
);
2070 if (r
== 0 && not1
!= 0) {
2072 r
= not_code_range_buf(enc
, pbuf
, &tbuf
);
2081 if (r
!= 0) return r
;
2092 conv_backslash_value(int c
, ScanEnv
* env
)
2094 if (IS_SYNTAX_OP(env
->syntax
, ONIG_SYN_OP_ESC_CONTROL_CHARS
)) {
2096 case 'n': return '\n';
2097 case 't': return '\t';
2098 case 'r': return '\r';
2099 case 'f': return '\f';
2100 case 'a': return '\007';
2101 case 'b': return '\010';
2102 case 'e': return '\033';
2104 if (IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_ESC_V_VTAB
))
2116 is_invalid_quantifier_target(Node
* node
)
2118 switch (NTYPE(node
)) {
2124 /* allow enclosed elements */
2125 /* return is_invalid_quantifier_target(NENCLOSE(node)->target); */
2130 if (! is_invalid_quantifier_target(NCAR(node
))) return 0;
2131 } while (IS_NOT_NULL(node
= NCDR(node
)));
2137 if (is_invalid_quantifier_target(NCAR(node
))) return 1;
2138 } while (IS_NOT_NULL(node
= NCDR(node
)));
2147 /* ?:0, *:1, +:2, ??:3, *?:4, +?:5 */
2149 popular_quantifier_num(QtfrNode
* q
)
2152 if (q
->lower
== 0) {
2153 if (q
->upper
== 1) return 0;
2154 else if (IS_REPEAT_INFINITE(q
->upper
)) return 1;
2156 else if (q
->lower
== 1) {
2157 if (IS_REPEAT_INFINITE(q
->upper
)) return 2;
2161 if (q
->lower
== 0) {
2162 if (q
->upper
== 1) return 3;
2163 else if (IS_REPEAT_INFINITE(q
->upper
)) return 4;
2165 else if (q
->lower
== 1) {
2166 if (IS_REPEAT_INFINITE(q
->upper
)) return 5;
2174 RQ_ASIS
= 0, /* as is */
2175 RQ_DEL
= 1, /* delete parent */
2177 RQ_AQ
, /* to '*?' */
2178 RQ_QQ
, /* to '??' */
2179 RQ_P_QQ
, /* to '+)??' */
2180 RQ_PQ_Q
/* to '+?)?' */
2183 static enum ReduceType
const ReduceTypeTable
[6][6] = {
2184 {RQ_DEL
, RQ_A
, RQ_A
, RQ_QQ
, RQ_AQ
, RQ_ASIS
}, /* '?' */
2185 {RQ_DEL
, RQ_DEL
, RQ_DEL
, RQ_P_QQ
, RQ_P_QQ
, RQ_DEL
}, /* '*' */
2186 {RQ_A
, RQ_A
, RQ_DEL
, RQ_ASIS
, RQ_P_QQ
, RQ_DEL
}, /* '+' */
2187 {RQ_DEL
, RQ_AQ
, RQ_AQ
, RQ_DEL
, RQ_AQ
, RQ_AQ
}, /* '??' */
2188 {RQ_DEL
, RQ_DEL
, RQ_DEL
, RQ_DEL
, RQ_DEL
, RQ_DEL
}, /* '*?' */
2189 {RQ_ASIS
, RQ_PQ_Q
, RQ_DEL
, RQ_AQ
, RQ_AQ
, RQ_DEL
} /* '+?' */
2193 onig_reduce_nested_quantifier(Node
* pnode
, Node
* cnode
)
2200 pnum
= popular_quantifier_num(p
);
2201 cnum
= popular_quantifier_num(c
);
2202 if (pnum
< 0 || cnum
< 0) return ;
2204 switch(ReduceTypeTable
[cnum
][pnum
]) {
2209 p
->target
= c
->target
;
2210 p
->lower
= 0; p
->upper
= REPEAT_INFINITE
; p
->greedy
= 1;
2213 p
->target
= c
->target
;
2214 p
->lower
= 0; p
->upper
= REPEAT_INFINITE
; p
->greedy
= 0;
2217 p
->target
= c
->target
;
2218 p
->lower
= 0; p
->upper
= 1; p
->greedy
= 0;
2222 p
->lower
= 0; p
->upper
= 1; p
->greedy
= 0;
2223 c
->lower
= 1; c
->upper
= REPEAT_INFINITE
; c
->greedy
= 1;
2228 p
->lower
= 0; p
->upper
= 1; p
->greedy
= 1;
2229 c
->lower
= 1; c
->upper
= REPEAT_INFINITE
; c
->greedy
= 0;
2238 c
->target
= NULL_NODE
;
2239 onig_node_free(cnode
);
2244 TK_EOT
= 0, /* end of token */
2256 TK_ANYCHAR_ANYTIME
, /* SQL '%' == .* */
2262 TK_CHAR_PROPERTY
, /* \p{...}, \P{...} */
2266 TK_POSIX_BRACKET_OPEN
,
2268 TK_CC_CC_OPEN
/* [ */
2272 enum TokenSyms type
;
2274 int base
; /* is number: 8, 16 (used in [....]) */
2293 #ifdef USE_BACKREF_WITH_LEVEL
2295 int level
; /* \k<name+n> */
2312 fetch_range_quantifier(UChar
** src
, UChar
* end
, OnigToken
* tok
, ScanEnv
* env
)
2314 int low
, up
, syn_allow
, non_low
= 0;
2317 OnigEncoding enc
= env
->enc
;
2321 syn_allow
= IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_ALLOW_INVALID_INTERVAL
);
2325 return 1; /* "....{" : OK! */
2327 return ONIGERR_END_PATTERN_AT_LEFT_BRACE
; /* "....{" syntax error */
2332 if (c
== ')' || c
== '(' || c
== '|') {
2333 return ONIGERR_END_PATTERN_AT_LEFT_BRACE
;
2337 low
= onig_scan_unsigned_number(&p
, end
, env
->enc
);
2338 if (low
< 0) return ONIGERR_TOO_BIG_NUMBER_FOR_REPEAT_RANGE
;
2339 if (low
> ONIG_MAX_REPEAT_NUM
)
2340 return ONIGERR_TOO_BIG_NUMBER_FOR_REPEAT_RANGE
;
2342 if (p
== *src
) { /* can't read low */
2343 if (IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_ALLOW_INTERVAL_LOW_ABBREV
)) {
2344 /* allow {,n} as {0,n} */
2352 if (PEND
) goto invalid
;
2356 up
= onig_scan_unsigned_number(&p
, end
, env
->enc
);
2357 if (up
< 0) return ONIGERR_TOO_BIG_NUMBER_FOR_REPEAT_RANGE
;
2358 if (up
> ONIG_MAX_REPEAT_NUM
)
2359 return ONIGERR_TOO_BIG_NUMBER_FOR_REPEAT_RANGE
;
2364 up
= REPEAT_INFINITE
; /* {n,} : {n,infinite} */
2372 up
= low
; /* {n} : exact n times */
2376 if (PEND
) goto invalid
;
2378 if (IS_SYNTAX_OP(env
->syntax
, ONIG_SYN_OP_ESC_BRACE_INTERVAL
)) {
2379 if (c
!= MC_ESC(env
->syntax
)) goto invalid
;
2382 if (c
!= '}') goto invalid
;
2384 if (!IS_REPEAT_INFINITE(up
) && low
> up
) {
2385 return ONIGERR_UPPER_SMALLER_THAN_LOWER_IN_REPEAT_RANGE
;
2388 tok
->type
= TK_INTERVAL
;
2389 tok
->u
.repeat
.lower
= low
;
2390 tok
->u
.repeat
.upper
= up
;
2392 return r
; /* 0: normal {n,m}, 2: fixed {n} */
2398 return ONIGERR_INVALID_REPEAT_RANGE_PATTERN
;
2401 /* \M-, \C-, \c, or \... */
2403 fetch_escaped_value(UChar
** src
, UChar
* end
, ScanEnv
* env
)
2407 OnigEncoding enc
= env
->enc
;
2411 if (PEND
) return ONIGERR_END_PATTERN_AT_ESCAPE
;
2416 if (IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_ESC_CAPITAL_M_BAR_META
)) {
2417 if (PEND
) return ONIGERR_END_PATTERN_AT_META
;
2419 if (c
!= '-') return ONIGERR_META_CODE_SYNTAX
;
2420 if (PEND
) return ONIGERR_END_PATTERN_AT_META
;
2422 if (c
== MC_ESC(env
->syntax
)) {
2423 v
= fetch_escaped_value(&p
, end
, env
);
2424 if (v
< 0) return v
;
2425 c
= (OnigCodePoint
)v
;
2427 c
= ((c
& 0xff) | 0x80);
2434 if (IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_ESC_CAPITAL_C_BAR_CONTROL
)) {
2435 if (PEND
) return ONIGERR_END_PATTERN_AT_CONTROL
;
2437 if (c
!= '-') return ONIGERR_CONTROL_CODE_SYNTAX
;
2444 if (IS_SYNTAX_OP(env
->syntax
, ONIG_SYN_OP_ESC_C_CONTROL
)) {
2446 if (PEND
) return ONIGERR_END_PATTERN_AT_CONTROL
;
2452 if (c
== MC_ESC(env
->syntax
)) {
2453 v
= fetch_escaped_value(&p
, end
, env
);
2454 if (v
< 0) return v
;
2455 c
= (OnigCodePoint
)v
;
2466 c
= conv_backslash_value(c
, env
);
2475 static int fetch_token(OnigToken
* tok
, UChar
** src
, UChar
* end
, ScanEnv
* env
);
2477 static OnigCodePoint
2478 get_name_end_code_point(OnigCodePoint start
)
2481 case '<': return (OnigCodePoint
)'>'; break;
2482 case '\'': return (OnigCodePoint
)'\''; break;
2487 return (OnigCodePoint
)0;
2490 #ifdef USE_NAMED_GROUP
2491 #ifdef USE_BACKREF_WITH_LEVEL
2493 \k<name+n>, \k<name-n>
2494 \k<num+n>, \k<num-n>
2495 \k<-num+n>, \k<-num-n>
2498 fetch_name_with_level(OnigCodePoint start_code
, UChar
** src
, UChar
* end
,
2499 UChar
** rname_end
, ScanEnv
* env
,
2500 int* rback_num
, int* rlevel
)
2502 int r
, sign
, is_num
, exist_level
;
2503 OnigCodePoint end_code
;
2504 OnigCodePoint c
= 0;
2505 OnigEncoding enc
= env
->enc
;
2512 is_num
= exist_level
= 0;
2516 end_code
= get_name_end_code_point(start_code
);
2521 return ONIGERR_EMPTY_GROUP_NAME
;
2526 return ONIGERR_EMPTY_GROUP_NAME
;
2528 if (ONIGENC_IS_CODE_DIGIT(enc
, c
)) {
2531 else if (c
== '-') {
2536 else if (!ONIGENC_IS_CODE_WORD(enc
, c
)) {
2537 r
= ONIGERR_INVALID_CHAR_IN_GROUP_NAME
;
2544 if (c
== end_code
|| c
== ')' || c
== '+' || c
== '-') {
2545 if (is_num
== 2) r
= ONIGERR_INVALID_GROUP_NAME
;
2550 if (ONIGENC_IS_CODE_DIGIT(enc
, c
)) {
2554 r
= ONIGERR_INVALID_GROUP_NAME
;
2558 else if (!ONIGENC_IS_CODE_WORD(enc
, c
)) {
2559 r
= ONIGERR_INVALID_CHAR_IN_GROUP_NAME
;
2563 if (r
== 0 && c
!= end_code
) {
2564 if (c
== '+' || c
== '-') {
2566 int flag
= (c
== '-' ? -1 : 1);
2569 if (! ONIGENC_IS_CODE_DIGIT(enc
, c
)) goto err
;
2571 level
= onig_scan_unsigned_number(&p
, end
, enc
);
2572 if (level
< 0) return ONIGERR_TOO_BIG_NUMBER
;
2573 *rlevel
= (level
* flag
);
2582 r
= ONIGERR_INVALID_GROUP_NAME
;
2589 *rback_num
= onig_scan_unsigned_number(&pnum_head
, name_end
, enc
);
2590 if (*rback_num
< 0) return ONIGERR_TOO_BIG_NUMBER
;
2591 else if (*rback_num
== 0) goto err
;
2596 *rname_end
= name_end
;
2598 return (exist_level
? 1 : 0);
2601 onig_scan_env_set_error_string(env
, r
, *src
, name_end
);
2605 #endif /* USE_BACKREF_WITH_LEVEL */
2608 def: 0 -> define name (don't allow number name)
2609 1 -> reference name (allow number name)
2612 fetch_name(OnigCodePoint start_code
, UChar
** src
, UChar
* end
,
2613 UChar
** rname_end
, ScanEnv
* env
, int* rback_num
, int ref
)
2615 int r
, is_num
, sign
;
2616 OnigCodePoint end_code
;
2617 OnigCodePoint c
= 0;
2618 OnigEncoding enc
= env
->enc
;
2626 end_code
= get_name_end_code_point(start_code
);
2634 return ONIGERR_EMPTY_GROUP_NAME
;
2639 return ONIGERR_EMPTY_GROUP_NAME
;
2641 if (ONIGENC_IS_CODE_DIGIT(enc
, c
)) {
2645 r
= ONIGERR_INVALID_GROUP_NAME
;
2649 else if (c
== '-') {
2656 r
= ONIGERR_INVALID_GROUP_NAME
;
2660 else if (!ONIGENC_IS_CODE_WORD(enc
, c
)) {
2661 r
= ONIGERR_INVALID_CHAR_IN_GROUP_NAME
;
2669 if (c
== end_code
|| c
== ')') {
2670 if (is_num
== 2) r
= ONIGERR_INVALID_GROUP_NAME
;
2675 if (ONIGENC_IS_CODE_DIGIT(enc
, c
)) {
2679 if (!ONIGENC_IS_CODE_WORD(enc
, c
))
2680 r
= ONIGERR_INVALID_CHAR_IN_GROUP_NAME
;
2682 r
= ONIGERR_INVALID_GROUP_NAME
;
2688 if (!ONIGENC_IS_CODE_WORD(enc
, c
)) {
2689 r
= ONIGERR_INVALID_CHAR_IN_GROUP_NAME
;
2694 if (c
!= end_code
) {
2695 r
= ONIGERR_INVALID_GROUP_NAME
;
2700 *rback_num
= onig_scan_unsigned_number(&pnum_head
, name_end
, enc
);
2701 if (*rback_num
< 0) return ONIGERR_TOO_BIG_NUMBER
;
2702 else if (*rback_num
== 0) {
2703 r
= ONIGERR_INVALID_GROUP_NAME
;
2710 *rname_end
= name_end
;
2718 if (c
== end_code
|| c
== ')')
2725 onig_scan_env_set_error_string(env
, r
, *src
, name_end
);
2731 fetch_name(OnigCodePoint start_code
, UChar
** src
, UChar
* end
,
2732 UChar
** rname_end
, ScanEnv
* env
, int* rback_num
, int ref
)
2734 int r
, is_num
, sign
;
2735 OnigCodePoint end_code
;
2736 OnigCodePoint c
= 0;
2738 OnigEncoding enc
= env
->enc
;
2745 end_code
= get_name_end_code_point(start_code
);
2747 *rname_end
= name_end
= end
;
2754 return ONIGERR_EMPTY_GROUP_NAME
;
2759 return ONIGERR_EMPTY_GROUP_NAME
;
2761 if (ONIGENC_IS_CODE_DIGIT(enc
, c
)) {
2764 else if (c
== '-') {
2770 r
= ONIGERR_INVALID_CHAR_IN_GROUP_NAME
;
2778 if (c
== end_code
|| c
== ')') break;
2779 if (! ONIGENC_IS_CODE_DIGIT(enc
, c
))
2780 r
= ONIGERR_INVALID_CHAR_IN_GROUP_NAME
;
2782 if (r
== 0 && c
!= end_code
) {
2783 r
= ONIGERR_INVALID_GROUP_NAME
;
2788 *rback_num
= onig_scan_unsigned_number(&pnum_head
, name_end
, enc
);
2789 if (*rback_num
< 0) return ONIGERR_TOO_BIG_NUMBER
;
2790 else if (*rback_num
== 0) {
2791 r
= ONIGERR_INVALID_GROUP_NAME
;
2796 *rname_end
= name_end
;
2802 onig_scan_env_set_error_string(env
, r
, *src
, name_end
);
2806 #endif /* USE_NAMED_GROUP */
2809 CC_ESC_WARN(ScanEnv
* env
, UChar
*c
)
2811 if (onig_warn
== onig_null_warn
) return ;
2813 if (IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_WARN_CC_OP_NOT_ESCAPED
) &&
2814 IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_BACKSLASH_ESCAPE_IN_CC
)) {
2815 UChar buf
[WARN_BUFSIZE
];
2816 onig_snprintf_with_pattern(buf
, WARN_BUFSIZE
, env
->enc
,
2817 env
->pattern
, env
->pattern_end
,
2818 (UChar
* )"character class has '%s' without escape", c
);
2819 (*onig_warn
)((char* )buf
);
2824 CLOSE_BRACKET_WITHOUT_ESC_WARN(ScanEnv
* env
, UChar
* c
)
2826 if (onig_warn
== onig_null_warn
) return ;
2828 if (IS_SYNTAX_BV((env
)->syntax
, ONIG_SYN_WARN_CC_OP_NOT_ESCAPED
)) {
2829 UChar buf
[WARN_BUFSIZE
];
2830 onig_snprintf_with_pattern(buf
, WARN_BUFSIZE
, (env
)->enc
,
2831 (env
)->pattern
, (env
)->pattern_end
,
2832 (UChar
* )"regular expression has '%s' without escape", c
);
2833 (*onig_warn
)((char* )buf
);
2838 find_str_position(OnigCodePoint s
[], int n
, UChar
* from
, UChar
* to
,
2839 UChar
**next
, OnigEncoding enc
)
2847 x
= ONIGENC_MBC_TO_CODE(enc
, p
, to
);
2848 q
= p
+ enclen(enc
, p
, to
);
2850 for (i
= 1; i
< n
&& q
< to
; i
++) {
2851 x
= ONIGENC_MBC_TO_CODE(enc
, q
, to
);
2852 if (x
!= s
[i
]) break;
2853 q
+= enclen(enc
, q
, to
);
2856 if (IS_NOT_NULL(next
))
2867 str_exist_check_with_esc(OnigCodePoint s
[], int n
, UChar
* from
, UChar
* to
,
2868 OnigCodePoint bad
, OnigEncoding enc
, const OnigSyntaxType
* syn
)
2879 p
+= enclen(enc
, p
, to
);
2882 x
= ONIGENC_MBC_TO_CODE(enc
, p
, to
);
2883 q
= p
+ enclen(enc
, p
, to
);
2885 for (i
= 1; i
< n
&& q
< to
; i
++) {
2886 x
= ONIGENC_MBC_TO_CODE(enc
, q
, to
);
2887 if (x
!= s
[i
]) break;
2888 q
+= enclen(enc
, q
, to
);
2890 if (i
>= n
) return 1;
2891 p
+= enclen(enc
, p
, to
);
2894 x
= ONIGENC_MBC_TO_CODE(enc
, p
, to
);
2895 if (x
== bad
) return 0;
2896 else if (x
== MC_ESC(syn
)) in_esc
= 1;
2905 fetch_token_in_cc(OnigToken
* tok
, UChar
** src
, UChar
* end
, ScanEnv
* env
)
2908 OnigCodePoint c
, c2
;
2909 const OnigSyntaxType
* syn
= env
->syntax
;
2910 OnigEncoding enc
= env
->enc
;
2921 tok
->type
= TK_CHAR
;
2927 tok
->type
= TK_CC_CLOSE
;
2929 else if (c
== '-') {
2930 tok
->type
= TK_CC_RANGE
;
2932 else if (c
== MC_ESC(syn
)) {
2933 if (! IS_SYNTAX_BV(syn
, ONIG_SYN_BACKSLASH_ESCAPE_IN_CC
))
2936 if (PEND
) return ONIGERR_END_PATTERN_AT_ESCAPE
;
2943 tok
->type
= TK_CHAR_TYPE
;
2944 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_WORD
;
2945 tok
->u
.prop
.not = 0;
2948 tok
->type
= TK_CHAR_TYPE
;
2949 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_WORD
;
2950 tok
->u
.prop
.not = 1;
2953 tok
->type
= TK_CHAR_TYPE
;
2954 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_DIGIT
;
2955 tok
->u
.prop
.not = 0;
2958 tok
->type
= TK_CHAR_TYPE
;
2959 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_DIGIT
;
2960 tok
->u
.prop
.not = 1;
2963 tok
->type
= TK_CHAR_TYPE
;
2964 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_SPACE
;
2965 tok
->u
.prop
.not = 0;
2968 tok
->type
= TK_CHAR_TYPE
;
2969 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_SPACE
;
2970 tok
->u
.prop
.not = 1;
2973 if (! IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_H_XDIGIT
)) break;
2974 tok
->type
= TK_CHAR_TYPE
;
2975 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_XDIGIT
;
2976 tok
->u
.prop
.not = 0;
2979 if (! IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_H_XDIGIT
)) break;
2980 tok
->type
= TK_CHAR_TYPE
;
2981 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_XDIGIT
;
2982 tok
->u
.prop
.not = 1;
2989 IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_P_BRACE_CHAR_PROPERTY
)) {
2991 tok
->type
= TK_CHAR_PROPERTY
;
2992 tok
->u
.prop
.not = (c
== 'P' ? 1 : 0);
2994 if (IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_P_BRACE_CIRCUMFLEX_NOT
)) {
2997 tok
->u
.prop
.not = (tok
->u
.prop
.not == 0 ? 1 : 0);
3009 if (PPEEK_IS('{') && IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_X_BRACE_HEX8
)) {
3011 num
= scan_unsigned_hexadecimal_number(&p
, end
, 8, enc
);
3012 if (num
< 0) return ONIGERR_TOO_BIG_WIDE_CHAR_VALUE
;
3015 if (ONIGENC_IS_CODE_XDIGIT(enc
, c2
))
3016 return ONIGERR_TOO_LONG_WIDE_CHAR_VALUE
;
3019 if (p
> prev
+ enclen(enc
, prev
, end
) && !PEND
&& (PPEEK_IS('}'))) {
3021 tok
->type
= TK_CODE_POINT
;
3023 tok
->u
.code
= (OnigCodePoint
)num
;
3026 /* can't read nothing or invalid format */
3030 else if (IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_X_HEX2
)) {
3031 num
= scan_unsigned_hexadecimal_number(&p
, end
, 2, enc
);
3032 if (num
< 0) return ONIGERR_TOO_BIG_NUMBER
;
3033 if (p
== prev
) { /* can't read nothing. */
3034 num
= 0; /* but, it's not error */
3036 tok
->type
= TK_RAW_BYTE
;
3046 if (IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_U_HEX4
)) {
3047 num
= scan_unsigned_hexadecimal_number(&p
, end
, 4, enc
);
3048 if (num
< 0) return ONIGERR_TOO_BIG_NUMBER
;
3049 if (p
== prev
) { /* can't read nothing. */
3050 num
= 0; /* but, it's not error */
3052 tok
->type
= TK_CODE_POINT
;
3054 tok
->u
.code
= (OnigCodePoint
)num
;
3059 case '1': case '2': case '3': case '4': case '5': case '6': case '7':
3060 if (IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_OCTAL3
)) {
3063 num
= scan_unsigned_octal_number(&p
, end
, 3, enc
);
3064 if (num
< 0) return ONIGERR_TOO_BIG_NUMBER
;
3065 if (p
== prev
) { /* can't read nothing. */
3066 num
= 0; /* but, it's not error */
3068 tok
->type
= TK_RAW_BYTE
;
3076 num
= fetch_escaped_value(&p
, end
, env
);
3077 if (num
< 0) return num
;
3078 if (tok
->u
.c
!= num
) {
3079 tok
->u
.code
= (OnigCodePoint
)num
;
3080 tok
->type
= TK_CODE_POINT
;
3085 else if (c
== '[') {
3086 if (IS_SYNTAX_OP(syn
, ONIG_SYN_OP_POSIX_BRACKET
) && (PPEEK_IS(':'))) {
3087 OnigCodePoint send
[] = { (OnigCodePoint
)':', (OnigCodePoint
)']' };
3088 tok
->backp
= p
; /* point at '[' is readed */
3090 if (str_exist_check_with_esc(send
, 2, p
, end
,
3091 (OnigCodePoint
)']', enc
, syn
)) {
3092 tok
->type
= TK_POSIX_BRACKET_OPEN
;
3101 if (IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_CCLASS_SET_OP
)) {
3102 tok
->type
= TK_CC_CC_OPEN
;
3105 CC_ESC_WARN(env
, (UChar
* )"[");
3109 else if (c
== '&') {
3110 if (IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_CCLASS_SET_OP
) &&
3111 !PEND
&& (PPEEK_IS('&'))) {
3113 tok
->type
= TK_CC_AND
;
3123 fetch_token(OnigToken
* tok
, UChar
** src
, UChar
* end
, ScanEnv
* env
)
3127 OnigEncoding enc
= env
->enc
;
3128 const OnigSyntaxType
* syn
= env
->syntax
;
3139 tok
->type
= TK_STRING
;
3144 if (IS_MC_ESC_CODE(c
, syn
)) {
3145 if (PEND
) return ONIGERR_END_PATTERN_AT_ESCAPE
;
3154 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_ASTERISK_ZERO_INF
)) break;
3155 tok
->type
= TK_OP_REPEAT
;
3156 tok
->u
.repeat
.lower
= 0;
3157 tok
->u
.repeat
.upper
= REPEAT_INFINITE
;
3162 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_PLUS_ONE_INF
)) break;
3163 tok
->type
= TK_OP_REPEAT
;
3164 tok
->u
.repeat
.lower
= 1;
3165 tok
->u
.repeat
.upper
= REPEAT_INFINITE
;
3170 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_QMARK_ZERO_ONE
)) break;
3171 tok
->type
= TK_OP_REPEAT
;
3172 tok
->u
.repeat
.lower
= 0;
3173 tok
->u
.repeat
.upper
= 1;
3175 if (!PEND
&& PPEEK_IS('?') &&
3176 IS_SYNTAX_OP(syn
, ONIG_SYN_OP_QMARK_NON_GREEDY
)) {
3178 tok
->u
.repeat
.greedy
= 0;
3179 tok
->u
.repeat
.possessive
= 0;
3183 if (!PEND
&& PPEEK_IS('+') &&
3184 ((IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_PLUS_POSSESSIVE_REPEAT
) &&
3185 tok
->type
!= TK_INTERVAL
) ||
3186 (IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_PLUS_POSSESSIVE_INTERVAL
) &&
3187 tok
->type
== TK_INTERVAL
))) {
3189 tok
->u
.repeat
.greedy
= 1;
3190 tok
->u
.repeat
.possessive
= 1;
3193 tok
->u
.repeat
.greedy
= 1;
3194 tok
->u
.repeat
.possessive
= 0;
3200 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_BRACE_INTERVAL
)) break;
3201 r
= fetch_range_quantifier(&p
, end
, tok
, env
);
3202 if (r
< 0) return r
; /* error */
3203 if (r
== 0) goto greedy_check
;
3204 else if (r
== 2) { /* {n} */
3205 if (IS_SYNTAX_BV(syn
, ONIG_SYN_FIXED_INTERVAL_IS_GREEDY_ONLY
))
3206 goto possessive_check
;
3210 /* r == 1 : normal char */
3214 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_VBAR_ALT
)) break;
3219 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_LPAREN_SUBEXP
)) break;
3220 tok
->type
= TK_SUBEXP_OPEN
;
3224 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_LPAREN_SUBEXP
)) break;
3225 tok
->type
= TK_SUBEXP_CLOSE
;
3229 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_W_WORD
)) break;
3230 tok
->type
= TK_CHAR_TYPE
;
3231 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_WORD
;
3232 tok
->u
.prop
.not = 0;
3236 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_W_WORD
)) break;
3237 tok
->type
= TK_CHAR_TYPE
;
3238 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_WORD
;
3239 tok
->u
.prop
.not = 1;
3243 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_B_WORD_BOUND
)) break;
3244 tok
->type
= TK_ANCHOR
;
3245 tok
->u
.anchor
= ANCHOR_WORD_BOUND
;
3249 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_B_WORD_BOUND
)) break;
3250 tok
->type
= TK_ANCHOR
;
3251 tok
->u
.anchor
= ANCHOR_NOT_WORD_BOUND
;
3254 #ifdef USE_WORD_BEGIN_END
3256 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_LTGT_WORD_BEGIN_END
)) break;
3257 tok
->type
= TK_ANCHOR
;
3258 tok
->u
.anchor
= ANCHOR_WORD_BEGIN
;
3262 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_LTGT_WORD_BEGIN_END
)) break;
3263 tok
->type
= TK_ANCHOR
;
3264 tok
->u
.anchor
= ANCHOR_WORD_END
;
3269 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_S_WHITE_SPACE
)) break;
3270 tok
->type
= TK_CHAR_TYPE
;
3271 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_SPACE
;
3272 tok
->u
.prop
.not = 0;
3276 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_S_WHITE_SPACE
)) break;
3277 tok
->type
= TK_CHAR_TYPE
;
3278 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_SPACE
;
3279 tok
->u
.prop
.not = 1;
3283 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_D_DIGIT
)) break;
3284 tok
->type
= TK_CHAR_TYPE
;
3285 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_DIGIT
;
3286 tok
->u
.prop
.not = 0;
3290 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_D_DIGIT
)) break;
3291 tok
->type
= TK_CHAR_TYPE
;
3292 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_DIGIT
;
3293 tok
->u
.prop
.not = 1;
3297 if (! IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_H_XDIGIT
)) break;
3298 tok
->type
= TK_CHAR_TYPE
;
3299 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_XDIGIT
;
3300 tok
->u
.prop
.not = 0;
3304 if (! IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_H_XDIGIT
)) break;
3305 tok
->type
= TK_CHAR_TYPE
;
3306 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_XDIGIT
;
3307 tok
->u
.prop
.not = 1;
3311 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_AZ_BUF_ANCHOR
)) break;
3313 tok
->type
= TK_ANCHOR
;
3314 tok
->u
.subtype
= ANCHOR_BEGIN_BUF
;
3318 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_AZ_BUF_ANCHOR
)) break;
3319 tok
->type
= TK_ANCHOR
;
3320 tok
->u
.subtype
= ANCHOR_SEMI_END_BUF
;
3324 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_AZ_BUF_ANCHOR
)) break;
3326 tok
->type
= TK_ANCHOR
;
3327 tok
->u
.subtype
= ANCHOR_END_BUF
;
3331 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_CAPITAL_G_BEGIN_ANCHOR
)) break;
3332 tok
->type
= TK_ANCHOR
;
3333 tok
->u
.subtype
= ANCHOR_BEGIN_POSITION
;
3337 if (! IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_GNU_BUF_ANCHOR
)) break;
3342 if (! IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_GNU_BUF_ANCHOR
)) break;
3350 if (PPEEK_IS('{') && IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_X_BRACE_HEX8
)) {
3352 num
= scan_unsigned_hexadecimal_number(&p
, end
, 8, enc
);
3353 if (num
< 0) return ONIGERR_TOO_BIG_WIDE_CHAR_VALUE
;
3355 if (ONIGENC_IS_CODE_XDIGIT(enc
, PPEEK
))
3356 return ONIGERR_TOO_LONG_WIDE_CHAR_VALUE
;
3359 if ((p
> prev
+ enclen(enc
, prev
, end
)) && !PEND
&& PPEEK_IS('}')) {
3361 tok
->type
= TK_CODE_POINT
;
3362 tok
->u
.code
= (OnigCodePoint
)num
;
3365 /* can't read nothing or invalid format */
3369 else if (IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_X_HEX2
)) {
3370 num
= scan_unsigned_hexadecimal_number(&p
, end
, 2, enc
);
3371 if (num
< 0) return ONIGERR_TOO_BIG_NUMBER
;
3372 if (p
== prev
) { /* can't read nothing. */
3373 num
= 0; /* but, it's not error */
3375 tok
->type
= TK_RAW_BYTE
;
3385 if (IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_U_HEX4
)) {
3386 num
= scan_unsigned_hexadecimal_number(&p
, end
, 4, enc
);
3387 if (num
< 0) return ONIGERR_TOO_BIG_NUMBER
;
3388 if (p
== prev
) { /* can't read nothing. */
3389 num
= 0; /* but, it's not error */
3391 tok
->type
= TK_CODE_POINT
;
3393 tok
->u
.code
= (OnigCodePoint
)num
;
3397 case '1': case '2': case '3': case '4':
3398 case '5': case '6': case '7': case '8': case '9':
3401 num
= onig_scan_unsigned_number(&p
, end
, enc
);
3402 if (num
< 0 || num
> ONIG_MAX_BACKREF_NUM
) {
3406 if (IS_SYNTAX_OP(syn
, ONIG_SYN_OP_DECIMAL_BACKREF
) &&
3407 (num
<= env
->num_mem
|| num
<= 9)) { /* This spec. from GNU regex */
3408 if (IS_SYNTAX_BV(syn
, ONIG_SYN_STRICT_CHECK_BACKREF
)) {
3409 if (num
> env
->num_mem
|| IS_NULL(SCANENV_MEM_NODES(env
)[num
]))
3410 return ONIGERR_INVALID_BACKREF
;
3413 tok
->type
= TK_BACKREF
;
3414 tok
->u
.backref
.num
= 1;
3415 tok
->u
.backref
.ref1
= num
;
3416 tok
->u
.backref
.by_name
= 0;
3417 #ifdef USE_BACKREF_WITH_LEVEL
3418 tok
->u
.backref
.exist_level
= 0;
3424 if (c
== '8' || c
== '9') {
3433 if (IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_OCTAL3
)) {
3435 num
= scan_unsigned_octal_number(&p
, end
, (c
== '0' ? 2:3), enc
);
3436 if (num
< 0) return ONIGERR_TOO_BIG_NUMBER
;
3437 if (p
== prev
) { /* can't read nothing. */
3438 num
= 0; /* but, it's not error */
3440 tok
->type
= TK_RAW_BYTE
;
3444 else if (c
!= '0') {
3449 #ifdef USE_NAMED_GROUP
3451 if (IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_K_NAMED_BACKREF
)) {
3453 if (c
== '<' || c
== '\'') {
3460 #ifdef USE_BACKREF_WITH_LEVEL
3461 name_end
= NULL_UCHARP
; /* no need. escape gcc warning. */
3462 r
= fetch_name_with_level((OnigCodePoint
)c
, &p
, end
, &name_end
,
3463 env
, &back_num
, &tok
->u
.backref
.level
);
3464 if (r
== 1) tok
->u
.backref
.exist_level
= 1;
3465 else tok
->u
.backref
.exist_level
= 0;
3467 r
= fetch_name(&p
, end
, &name_end
, env
, &back_num
, 1);
3469 if (r
< 0) return r
;
3471 if (back_num
!= 0) {
3473 back_num
= BACKREF_REL_TO_ABS(back_num
, env
);
3475 return ONIGERR_INVALID_BACKREF
;
3478 if (IS_SYNTAX_BV(syn
, ONIG_SYN_STRICT_CHECK_BACKREF
)) {
3479 if (back_num
> env
->num_mem
||
3480 IS_NULL(SCANENV_MEM_NODES(env
)[back_num
]))
3481 return ONIGERR_INVALID_BACKREF
;
3483 tok
->type
= TK_BACKREF
;
3484 tok
->u
.backref
.by_name
= 0;
3485 tok
->u
.backref
.num
= 1;
3486 tok
->u
.backref
.ref1
= back_num
;
3489 num
= onig_name_to_group_numbers(env
->reg
, prev
, name_end
, &backs
);
3491 onig_scan_env_set_error_string(env
,
3492 ONIGERR_UNDEFINED_NAME_REFERENCE
, prev
, name_end
);
3493 return ONIGERR_UNDEFINED_NAME_REFERENCE
;
3495 if (IS_SYNTAX_BV(syn
, ONIG_SYN_STRICT_CHECK_BACKREF
)) {
3497 for (i
= 0; i
< num
; i
++) {
3498 if (backs
[i
] > env
->num_mem
||
3499 IS_NULL(SCANENV_MEM_NODES(env
)[backs
[i
]]))
3500 return ONIGERR_INVALID_BACKREF
;
3504 tok
->type
= TK_BACKREF
;
3505 tok
->u
.backref
.by_name
= 1;
3507 tok
->u
.backref
.num
= 1;
3508 tok
->u
.backref
.ref1
= backs
[0];
3511 tok
->u
.backref
.num
= num
;
3512 tok
->u
.backref
.refs
= backs
;
3522 #ifdef USE_SUBEXP_CALL
3524 if (IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_G_SUBEXP_CALL
)) {
3526 if (c
== '<' || c
== '\'') {
3531 r
= fetch_name((OnigCodePoint
)c
, &p
, end
, &name_end
, env
, &gnum
, 1);
3532 if (r
< 0) return r
;
3534 tok
->type
= TK_CALL
;
3535 tok
->u
.call
.name
= prev
;
3536 tok
->u
.call
.name_end
= name_end
;
3537 tok
->u
.call
.gnum
= gnum
;
3546 if (IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_CAPITAL_Q_QUOTE
)) {
3547 tok
->type
= TK_QUOTE_OPEN
;
3553 if (PPEEK_IS('{') &&
3554 IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_P_BRACE_CHAR_PROPERTY
)) {
3556 tok
->type
= TK_CHAR_PROPERTY
;
3557 tok
->u
.prop
.not = (c
== 'P' ? 1 : 0);
3559 if (IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_P_BRACE_CIRCUMFLEX_NOT
)) {
3562 tok
->u
.prop
.not = (tok
->u
.prop
.not == 0 ? 1 : 0);
3572 num
= fetch_escaped_value(&p
, end
, env
);
3573 if (num
< 0) return num
;
3575 if (tok
->u
.c
!= num
) {
3576 tok
->type
= TK_CODE_POINT
;
3577 tok
->u
.code
= (OnigCodePoint
)num
;
3580 p
= tok
->backp
+ enclen(enc
, tok
->backp
, end
);
3589 #ifdef USE_VARIABLE_META_CHARS
3590 if ((c
!= ONIG_INEFFECTIVE_META_CHAR
) &&
3591 IS_SYNTAX_OP(syn
, ONIG_SYN_OP_VARIABLE_META_CHARACTERS
)) {
3592 if (c
== MC_ANYCHAR(syn
))
3594 else if (c
== MC_ANYTIME(syn
))
3596 else if (c
== MC_ZERO_OR_ONE_TIME(syn
))
3597 goto zero_or_one_time
;
3598 else if (c
== MC_ONE_OR_MORE_TIME(syn
))
3599 goto one_or_more_time
;
3600 else if (c
== MC_ANYCHAR_ANYTIME(syn
)) {
3601 tok
->type
= TK_ANYCHAR_ANYTIME
;
3609 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_DOT_ANYCHAR
)) break;
3610 #ifdef USE_VARIABLE_META_CHARS
3613 tok
->type
= TK_ANYCHAR
;
3617 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ASTERISK_ZERO_INF
)) break;
3618 #ifdef USE_VARIABLE_META_CHARS
3621 tok
->type
= TK_OP_REPEAT
;
3622 tok
->u
.repeat
.lower
= 0;
3623 tok
->u
.repeat
.upper
= REPEAT_INFINITE
;
3628 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_PLUS_ONE_INF
)) break;
3629 #ifdef USE_VARIABLE_META_CHARS
3632 tok
->type
= TK_OP_REPEAT
;
3633 tok
->u
.repeat
.lower
= 1;
3634 tok
->u
.repeat
.upper
= REPEAT_INFINITE
;
3639 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_QMARK_ZERO_ONE
)) break;
3640 #ifdef USE_VARIABLE_META_CHARS
3643 tok
->type
= TK_OP_REPEAT
;
3644 tok
->u
.repeat
.lower
= 0;
3645 tok
->u
.repeat
.upper
= 1;
3650 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_BRACE_INTERVAL
)) break;
3651 r
= fetch_range_quantifier(&p
, end
, tok
, env
);
3652 if (r
< 0) return r
; /* error */
3653 if (r
== 0) goto greedy_check
;
3654 else if (r
== 2) { /* {n} */
3655 if (IS_SYNTAX_BV(syn
, ONIG_SYN_FIXED_INTERVAL_IS_GREEDY_ONLY
))
3656 goto possessive_check
;
3660 /* r == 1 : normal char */
3664 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_VBAR_ALT
)) break;
3669 if (PPEEK_IS('?') &&
3670 IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_QMARK_GROUP_EFFECT
)) {
3672 if (PPEEK_IS('#')) {
3675 if (PEND
) return ONIGERR_END_PATTERN_IN_GROUP
;
3677 if (c
== MC_ESC(syn
)) {
3678 if (!PEND
) PFETCH(c
);
3681 if (c
== ')') break;
3689 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_LPAREN_SUBEXP
)) break;
3690 tok
->type
= TK_SUBEXP_OPEN
;
3694 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_LPAREN_SUBEXP
)) break;
3695 tok
->type
= TK_SUBEXP_CLOSE
;
3699 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_LINE_ANCHOR
)) break;
3700 tok
->type
= TK_ANCHOR
;
3701 tok
->u
.subtype
= (IS_SINGLELINE(env
->option
)
3702 ? ANCHOR_BEGIN_BUF
: ANCHOR_BEGIN_LINE
);
3706 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_LINE_ANCHOR
)) break;
3707 tok
->type
= TK_ANCHOR
;
3708 tok
->u
.subtype
= (IS_SINGLELINE(env
->option
)
3709 ? ANCHOR_SEMI_END_BUF
: ANCHOR_END_LINE
);
3713 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_BRACKET_CC
)) break;
3714 tok
->type
= TK_CC_OPEN
;
3718 if (*src
> env
->pattern
) /* /].../ is allowed. */
3719 CLOSE_BRACKET_WITHOUT_ESC_WARN(env
, (UChar
* )"]");
3723 if (IS_EXTEND(env
->option
)) {
3726 if (ONIGENC_IS_CODE_NEWLINE(enc
, c
))
3734 case ' ': case '\t': case '\n': case '\r': case '\f':
3735 if (IS_EXTEND(env
->option
))
3745 #ifdef USE_VARIABLE_META_CHARS
3753 add_ctype_to_cc_by_range(CClassNode
* cc
, int ctype ARG_UNUSED
, int not,
3754 OnigEncoding enc ARG_UNUSED
,
3755 OnigCodePoint sb_out
, const OnigCodePoint mbr
[])
3760 int n
= ONIGENC_CODE_RANGE_NUM(mbr
);
3763 for (i
= 0; i
< n
; i
++) {
3764 for (j
= ONIGENC_CODE_RANGE_FROM(mbr
, i
);
3765 j
<= ONIGENC_CODE_RANGE_TO(mbr
, i
); j
++) {
3767 if (j
== ONIGENC_CODE_RANGE_TO(mbr
, i
)) i
++;
3768 else if (j
> ONIGENC_CODE_RANGE_FROM(mbr
, i
)) {
3769 r
= add_code_range_to_buf(&(cc
->mbuf
), j
,
3770 ONIGENC_CODE_RANGE_TO(mbr
, i
));
3771 if (r
!= 0) return r
;
3777 BITSET_SET_BIT(cc
->bs
, j
);
3782 for ( ; i
< n
; i
++) {
3783 r
= add_code_range_to_buf(&(cc
->mbuf
),
3784 ONIGENC_CODE_RANGE_FROM(mbr
, i
),
3785 ONIGENC_CODE_RANGE_TO(mbr
, i
));
3786 if (r
!= 0) return r
;
3790 OnigCodePoint prev
= 0;
3792 for (i
= 0; i
< n
; i
++) {
3794 j
< ONIGENC_CODE_RANGE_FROM(mbr
, i
); j
++) {
3798 BITSET_SET_BIT(cc
->bs
, j
);
3800 prev
= ONIGENC_CODE_RANGE_TO(mbr
, i
) + 1;
3802 for (j
= prev
; j
< sb_out
; j
++) {
3803 BITSET_SET_BIT(cc
->bs
, j
);
3809 for (i
= 0; i
< n
; i
++) {
3810 if (prev
< ONIGENC_CODE_RANGE_FROM(mbr
, i
)) {
3811 r
= add_code_range_to_buf(&(cc
->mbuf
), prev
,
3812 ONIGENC_CODE_RANGE_FROM(mbr
, i
) - 1);
3813 if (r
!= 0) return r
;
3815 prev
= ONIGENC_CODE_RANGE_TO(mbr
, i
) + 1;
3817 if (prev
< 0x7fffffff) {
3818 r
= add_code_range_to_buf(&(cc
->mbuf
), prev
, 0x7fffffff);
3819 if (r
!= 0) return r
;
3827 add_ctype_to_cc(CClassNode
* cc
, int ctype
, int not, ScanEnv
* env
)
3830 const OnigCodePoint
*ranges
;
3831 OnigCodePoint sb_out
;
3832 OnigEncoding enc
= env
->enc
;
3834 r
= ONIGENC_GET_CTYPE_CODE_RANGE(enc
, ctype
, &sb_out
, &ranges
);
3836 return add_ctype_to_cc_by_range(cc
, ctype
, not, env
->enc
, sb_out
, ranges
);
3838 else if (r
!= ONIG_NO_SUPPORT_CONFIG
) {
3844 case ONIGENC_CTYPE_ALPHA
:
3845 case ONIGENC_CTYPE_BLANK
:
3846 case ONIGENC_CTYPE_CNTRL
:
3847 case ONIGENC_CTYPE_DIGIT
:
3848 case ONIGENC_CTYPE_LOWER
:
3849 case ONIGENC_CTYPE_PUNCT
:
3850 case ONIGENC_CTYPE_SPACE
:
3851 case ONIGENC_CTYPE_UPPER
:
3852 case ONIGENC_CTYPE_XDIGIT
:
3853 case ONIGENC_CTYPE_ASCII
:
3854 case ONIGENC_CTYPE_ALNUM
:
3856 for (c
= 0; c
< SINGLE_BYTE_SIZE
; c
++) {
3857 if (! ONIGENC_IS_CODE_CTYPE(enc
, (OnigCodePoint
)c
, ctype
))
3858 BITSET_SET_BIT(cc
->bs
, c
);
3860 ADD_ALL_MULTI_BYTE_RANGE(enc
, cc
->mbuf
);
3863 for (c
= 0; c
< SINGLE_BYTE_SIZE
; c
++) {
3864 if (ONIGENC_IS_CODE_CTYPE(enc
, (OnigCodePoint
)c
, ctype
))
3865 BITSET_SET_BIT(cc
->bs
, c
);
3870 case ONIGENC_CTYPE_GRAPH
:
3871 case ONIGENC_CTYPE_PRINT
:
3873 for (c
= 0; c
< SINGLE_BYTE_SIZE
; c
++) {
3874 if (! ONIGENC_IS_CODE_CTYPE(enc
, (OnigCodePoint
)c
, ctype
))
3875 BITSET_SET_BIT(cc
->bs
, c
);
3879 for (c
= 0; c
< SINGLE_BYTE_SIZE
; c
++) {
3880 if (ONIGENC_IS_CODE_CTYPE(enc
, (OnigCodePoint
)c
, ctype
))
3881 BITSET_SET_BIT(cc
->bs
, c
);
3883 ADD_ALL_MULTI_BYTE_RANGE(enc
, cc
->mbuf
);
3887 case ONIGENC_CTYPE_WORD
:
3889 for (c
= 0; c
< SINGLE_BYTE_SIZE
; c
++) {
3890 if (IS_CODE_SB_WORD(enc
, c
)) BITSET_SET_BIT(cc
->bs
, c
);
3892 ADD_ALL_MULTI_BYTE_RANGE(enc
, cc
->mbuf
);
3895 for (c
= 0; c
< SINGLE_BYTE_SIZE
; c
++) {
3896 if ((ONIGENC_CODE_TO_MBCLEN(enc
, c
) > 0) /* check invalid code point */
3897 && ! ONIGENC_IS_CODE_WORD(enc
, c
))
3898 BITSET_SET_BIT(cc
->bs
, c
);
3904 return ONIGERR_PARSER_BUG
;
3912 parse_posix_bracket(CClassNode
* cc
, UChar
** src
, UChar
* end
, ScanEnv
* env
)
3914 #define POSIX_BRACKET_CHECK_LIMIT_LENGTH 20
3915 #define POSIX_BRACKET_NAME_MIN_LEN 4
3917 static const PosixBracketEntryType PBS
[] = {
3918 { (UChar
* )"alnum", ONIGENC_CTYPE_ALNUM
, 5 },
3919 { (UChar
* )"alpha", ONIGENC_CTYPE_ALPHA
, 5 },
3920 { (UChar
* )"blank", ONIGENC_CTYPE_BLANK
, 5 },
3921 { (UChar
* )"cntrl", ONIGENC_CTYPE_CNTRL
, 5 },
3922 { (UChar
* )"digit", ONIGENC_CTYPE_DIGIT
, 5 },
3923 { (UChar
* )"graph", ONIGENC_CTYPE_GRAPH
, 5 },
3924 { (UChar
* )"lower", ONIGENC_CTYPE_LOWER
, 5 },
3925 { (UChar
* )"print", ONIGENC_CTYPE_PRINT
, 5 },
3926 { (UChar
* )"punct", ONIGENC_CTYPE_PUNCT
, 5 },
3927 { (UChar
* )"space", ONIGENC_CTYPE_SPACE
, 5 },
3928 { (UChar
* )"upper", ONIGENC_CTYPE_UPPER
, 5 },
3929 { (UChar
* )"xdigit", ONIGENC_CTYPE_XDIGIT
, 6 },
3930 { (UChar
* )"ascii", ONIGENC_CTYPE_ASCII
, 5 },
3931 { (UChar
* )"word", ONIGENC_CTYPE_WORD
, 4 },
3932 { (UChar
* )NULL
, -1, 0 }
3935 const PosixBracketEntryType
*pb
;
3938 OnigEncoding enc
= env
->enc
;
3942 if (PPEEK_IS('^')) {
3949 if (onigenc_strlen(enc
, p
, end
) < POSIX_BRACKET_NAME_MIN_LEN
+ 3)
3950 goto not_posix_bracket
;
3952 for (pb
= PBS
; IS_NOT_NULL(pb
->name
); pb
++) {
3953 if (onigenc_with_ascii_strncmp(enc
, p
, end
, pb
->name
, pb
->len
) == 0) {
3954 p
= (UChar
* )onigenc_step(enc
, p
, end
, pb
->len
);
3955 if (onigenc_with_ascii_strncmp(enc
, p
, end
, (UChar
* )":]", 2) != 0)
3956 return ONIGERR_INVALID_POSIX_BRACKET_TYPE
;
3958 r
= add_ctype_to_cc(cc
, pb
->ctype
, not, env
);
3959 if (r
!= 0) return r
;
3970 while (!PEND
&& ((c
= PPEEK
) != ':') && c
!= ']') {
3972 if (++i
> POSIX_BRACKET_CHECK_LIMIT_LENGTH
) break;
3974 if (c
== ':' && ! PEND
) {
3979 return ONIGERR_INVALID_POSIX_BRACKET_TYPE
;
3983 return 1; /* 1: is not POSIX bracket, but no error. */
3987 fetch_char_property_to_ctype(UChar
** src
, UChar
* end
, ScanEnv
* env
)
3991 OnigEncoding enc
= env
->enc
;
3992 UChar
*prev
, *start
, *p
= *src
;
4002 r
= ONIGENC_PROPERTY_NAME_TO_CTYPE(enc
, start
, prev
);
4008 else if (c
== '(' || c
== ')' || c
== '{' || c
== '|') {
4009 r
= ONIGERR_INVALID_CHAR_PROPERTY_NAME
;
4014 onig_scan_env_set_error_string(env
, r
, *src
, prev
);
4019 parse_char_property(Node
** np
, OnigToken
* tok
, UChar
** src
, UChar
* end
,
4025 ctype
= fetch_char_property_to_ctype(src
, end
, env
);
4026 if (ctype
< 0) return ctype
;
4028 *np
= node_new_cclass();
4029 CHECK_NULL_RETURN_MEMERR(*np
);
4031 r
= add_ctype_to_cc(cc
, ctype
, 0, env
);
4032 if (r
!= 0) return r
;
4033 if (tok
->u
.prop
.not != 0) NCCLASS_SET_NOT(cc
);
4053 next_state_class(CClassNode
* cc
, OnigCodePoint
* vs
, enum CCVALTYPE
* type
,
4054 enum CCSTATE
* state
, ScanEnv
* env
)
4058 if (*state
== CCS_RANGE
)
4059 return ONIGERR_CHAR_CLASS_VALUE_AT_END_OF_RANGE
;
4061 if (*state
== CCS_VALUE
&& *type
!= CCV_CLASS
) {
4062 if (*type
== CCV_SB
)
4063 BITSET_SET_BIT(cc
->bs
, (int )(*vs
));
4064 else if (*type
== CCV_CODE_POINT
) {
4065 r
= add_code_range(&(cc
->mbuf
), env
, *vs
, *vs
);
4066 if (r
< 0) return r
;
4076 next_state_val(CClassNode
* cc
, OnigCodePoint
*vs
, OnigCodePoint v
,
4077 int* vs_israw
, int v_israw
,
4078 enum CCVALTYPE intype
, enum CCVALTYPE
* type
,
4079 enum CCSTATE
* state
, ScanEnv
* env
)
4085 if (*type
== CCV_SB
)
4086 BITSET_SET_BIT(cc
->bs
, (int )(*vs
));
4087 else if (*type
== CCV_CODE_POINT
) {
4088 r
= add_code_range(&(cc
->mbuf
), env
, *vs
, *vs
);
4089 if (r
< 0) return r
;
4094 if (intype
== *type
) {
4095 if (intype
== CCV_SB
) {
4096 if (*vs
> 0xff || v
> 0xff)
4097 return ONIGERR_INVALID_CODE_POINT_VALUE
;
4100 if (IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_ALLOW_EMPTY_RANGE_IN_CC
))
4103 return ONIGERR_EMPTY_RANGE_IN_CHAR_CLASS
;
4105 bitset_set_range(cc
->bs
, (int )*vs
, (int )v
);
4108 r
= add_code_range(&(cc
->mbuf
), env
, *vs
, v
);
4109 if (r
< 0) return r
;
4114 if (intype
== CCV_CODE_POINT
&& *type
== CCV_SB
) {
4117 if (IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_ALLOW_EMPTY_RANGE_IN_CC
))
4120 return ONIGERR_EMPTY_RANGE_IN_CHAR_CLASS
;
4122 bitset_set_range(cc
->bs
, (int )*vs
, (int )(v
< 0xff ? v
: 0xff));
4123 r
= add_code_range(&(cc
->mbuf
), env
, (OnigCodePoint
)*vs
, v
);
4124 if (r
< 0) return r
;
4128 return ONIGERR_MISMATCH_CODE_LENGTH_IN_CLASS_RANGE
;
4132 *state
= CCS_COMPLETE
;
4144 *vs_israw
= v_israw
;
4151 code_exist_check(OnigCodePoint c
, UChar
* from
, UChar
* end
, int ignore_escaped
,
4156 OnigEncoding enc
= env
->enc
;
4162 if (ignore_escaped
&& in_esc
) {
4167 if (code
== c
) return 1;
4168 if (code
== MC_ESC(env
->syntax
)) in_esc
= 1;
4175 parse_char_class(Node
** np
, OnigToken
* tok
, UChar
** src
, UChar
* end
,
4178 int r
, neg
, len
, fetched
, and_start
;
4179 OnigCodePoint v
, vs
;
4182 CClassNode
*cc
, *prev_cc
;
4186 enum CCVALTYPE val_type
, in_type
;
4187 int val_israw
, in_israw
;
4189 prev_cc
= (CClassNode
* )NULL
;
4191 r
= fetch_token_in_cc(tok
, src
, end
, env
);
4192 if (r
== TK_CHAR
&& tok
->u
.c
== '^' && tok
->escaped
== 0) {
4194 r
= fetch_token_in_cc(tok
, src
, end
, env
);
4200 if (r
< 0) return r
;
4201 if (r
== TK_CC_CLOSE
) {
4202 if (! code_exist_check((OnigCodePoint
)']',
4203 *src
, env
->pattern_end
, 1, env
))
4204 return ONIGERR_EMPTY_CHAR_CLASS
;
4206 CC_ESC_WARN(env
, (UChar
* )"]");
4207 r
= tok
->type
= TK_CHAR
; /* allow []...] */
4210 *np
= node
= node_new_cclass();
4211 CHECK_NULL_RETURN_MEMERR(node
);
4217 while (r
!= TK_CC_CLOSE
) {
4221 len
= ONIGENC_CODE_TO_MBCLEN(env
->enc
, tok
->u
.c
);
4223 in_type
= CCV_CODE_POINT
;
4233 v
= (OnigCodePoint
)tok
->u
.c
;
4239 /* tok->base != 0 : octal or hexadec. */
4240 if (! ONIGENC_IS_SINGLEBYTE(env
->enc
) && tok
->base
!= 0) {
4241 UChar buf
[ONIGENC_CODE_TO_MBC_MAXLEN
];
4242 UChar
* bufe
= buf
+ ONIGENC_CODE_TO_MBC_MAXLEN
;
4244 int i
, base
= tok
->base
;
4247 for (i
= 1; i
< ONIGENC_MBC_MAXLEN(env
->enc
); i
++) {
4248 r
= fetch_token_in_cc(tok
, &p
, end
, env
);
4249 if (r
< 0) goto err
;
4250 if (r
!= TK_RAW_BYTE
|| tok
->base
!= base
) {
4257 if (i
< ONIGENC_MBC_MINLEN(env
->enc
)) {
4258 r
= ONIGERR_TOO_SHORT_MULTI_BYTE_STRING
;
4262 len
= enclen(env
->enc
, buf
, buf
+i
);
4264 r
= ONIGERR_TOO_SHORT_MULTI_BYTE_STRING
;
4267 else if (i
> len
) { /* fetch back */
4269 for (i
= 1; i
< len
; i
++) {
4270 r
= fetch_token_in_cc(tok
, &p
, end
, env
);
4276 v
= (OnigCodePoint
)buf
[0];
4280 v
= ONIGENC_MBC_TO_CODE(env
->enc
, buf
, bufe
);
4281 in_type
= CCV_CODE_POINT
;
4285 v
= (OnigCodePoint
)tok
->u
.c
;
4297 len
= ONIGENC_CODE_TO_MBCLEN(env
->enc
, v
);
4302 in_type
= (len
== 1 ? CCV_SB
: CCV_CODE_POINT
);
4304 r
= next_state_val(cc
, &vs
, v
, &val_israw
, in_israw
, in_type
, &val_type
,
4306 if (r
!= 0) goto err
;
4309 case TK_POSIX_BRACKET_OPEN
:
4310 r
= parse_posix_bracket(cc
, &p
, end
, env
);
4311 if (r
< 0) goto err
;
4312 if (r
== 1) { /* is not POSIX bracket */
4313 CC_ESC_WARN(env
, (UChar
* )"[");
4315 v
= (OnigCodePoint
)tok
->u
.c
;
4323 r
= add_ctype_to_cc(cc
, tok
->u
.prop
.ctype
, tok
->u
.prop
.not, env
);
4324 if (r
!= 0) return r
;
4327 r
= next_state_class(cc
, &vs
, &val_type
, &state
, env
);
4328 if (r
!= 0) goto err
;
4331 case TK_CHAR_PROPERTY
:
4335 ctype
= fetch_char_property_to_ctype(&p
, end
, env
);
4336 if (ctype
< 0) return ctype
;
4337 r
= add_ctype_to_cc(cc
, ctype
, tok
->u
.prop
.not, env
);
4338 if (r
!= 0) return r
;
4344 if (state
== CCS_VALUE
) {
4345 r
= fetch_token_in_cc(tok
, &p
, end
, env
);
4346 if (r
< 0) goto err
;
4348 if (r
== TK_CC_CLOSE
) { /* allow [x-] */
4350 v
= (OnigCodePoint
)'-';
4354 else if (r
== TK_CC_AND
) {
4355 CC_ESC_WARN(env
, (UChar
* )"-");
4360 else if (state
== CCS_START
) {
4361 /* [-xa] is allowed */
4362 v
= (OnigCodePoint
)tok
->u
.c
;
4365 r
= fetch_token_in_cc(tok
, &p
, end
, env
);
4366 if (r
< 0) goto err
;
4368 /* [--x] or [a&&-x] is warned. */
4369 if (r
== TK_CC_RANGE
|| and_start
!= 0)
4370 CC_ESC_WARN(env
, (UChar
* )"-");
4374 else if (state
== CCS_RANGE
) {
4375 CC_ESC_WARN(env
, (UChar
* )"-");
4376 goto sb_char
; /* [!--x] is allowed */
4378 else { /* CCS_COMPLETE */
4379 r
= fetch_token_in_cc(tok
, &p
, end
, env
);
4380 if (r
< 0) goto err
;
4382 if (r
== TK_CC_CLOSE
) goto range_end_val
; /* allow [a-b-] */
4383 else if (r
== TK_CC_AND
) {
4384 CC_ESC_WARN(env
, (UChar
* )"-");
4388 if (IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_ALLOW_DOUBLE_RANGE_OP_IN_CC
)) {
4389 CC_ESC_WARN(env
, (UChar
* )"-");
4390 goto sb_char
; /* [0-9-a] is allowed as [0-9\-a] */
4392 r
= ONIGERR_UNMATCHED_RANGE_SPECIFIER_IN_CHAR_CLASS
;
4397 case TK_CC_CC_OPEN
: /* [ */
4402 r
= parse_char_class(&anode
, tok
, &p
, end
, env
);
4403 if (r
!= 0) goto cc_open_err
;
4404 acc
= NCCLASS(anode
);
4405 r
= or_cclass(cc
, acc
, env
->enc
);
4407 onig_node_free(anode
);
4409 if (r
!= 0) goto err
;
4413 case TK_CC_AND
: /* && */
4415 if (state
== CCS_VALUE
) {
4416 r
= next_state_val(cc
, &vs
, 0, &val_israw
, 0, val_type
,
4417 &val_type
, &state
, env
);
4418 if (r
!= 0) goto err
;
4420 /* initialize local variables */
4424 if (IS_NOT_NULL(prev_cc
)) {
4425 r
= and_cclass(prev_cc
, cc
, env
->enc
);
4426 if (r
!= 0) goto err
;
4427 bbuf_free(cc
->mbuf
);
4433 initialize_cclass(cc
);
4438 r
= ONIGERR_PREMATURE_END_OF_CHAR_CLASS
;
4442 r
= ONIGERR_PARSER_BUG
;
4450 r
= fetch_token_in_cc(tok
, &p
, end
, env
);
4451 if (r
< 0) goto err
;
4455 if (state
== CCS_VALUE
) {
4456 r
= next_state_val(cc
, &vs
, 0, &val_israw
, 0, val_type
,
4457 &val_type
, &state
, env
);
4458 if (r
!= 0) goto err
;
4461 if (IS_NOT_NULL(prev_cc
)) {
4462 r
= and_cclass(prev_cc
, cc
, env
->enc
);
4463 if (r
!= 0) goto err
;
4464 bbuf_free(cc
->mbuf
);
4469 NCCLASS_SET_NOT(cc
);
4471 NCCLASS_CLEAR_NOT(cc
);
4472 if (IS_NCCLASS_NOT(cc
) &&
4473 IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_NOT_NEWLINE_IN_NEGATIVE_CC
)) {
4476 is_empty
= (IS_NULL(cc
->mbuf
) ? 1 : 0);
4478 BITSET_IS_EMPTY(cc
->bs
, is_empty
);
4480 if (is_empty
== 0) {
4481 #define NEWLINE_CODE 0x0a
4483 if (ONIGENC_IS_CODE_NEWLINE(env
->enc
, NEWLINE_CODE
)) {
4484 if (ONIGENC_CODE_TO_MBCLEN(env
->enc
, NEWLINE_CODE
) == 1)
4485 BITSET_SET_BIT(cc
->bs
, NEWLINE_CODE
);
4487 add_code_range(&(cc
->mbuf
), env
, NEWLINE_CODE
, NEWLINE_CODE
);
4495 if (cc
!= NCCLASS(*np
))
4496 bbuf_free(cc
->mbuf
);
4497 onig_node_free(*np
);
4501 static int parse_subexp(Node
** top
, OnigToken
* tok
, int term
,
4502 UChar
** src
, UChar
* end
, ScanEnv
* env
);
4505 parse_enclose(Node
** np
, OnigToken
* tok
, int term
, UChar
** src
, UChar
* end
,
4510 OnigOptionType option
;
4512 OnigEncoding enc
= env
->enc
;
4514 #ifdef USE_NAMED_GROUP
4522 if (PEND
) return ONIGERR_END_PATTERN_WITH_UNMATCHED_PARENTHESIS
;
4524 option
= env
->option
;
4525 if (PPEEK_IS('?') &&
4526 IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_QMARK_GROUP_EFFECT
)) {
4528 if (PEND
) return ONIGERR_END_PATTERN_IN_GROUP
;
4532 case ':': /* (?:...) grouping only */
4534 r
= fetch_token(tok
, &p
, end
, env
);
4535 if (r
< 0) return r
;
4536 r
= parse_subexp(np
, tok
, term
, &p
, end
, env
);
4537 if (r
< 0) return r
;
4539 return 1; /* group */
4543 *np
= onig_node_new_anchor(ANCHOR_PREC_READ
);
4545 case '!': /* preceding read */
4546 *np
= onig_node_new_anchor(ANCHOR_PREC_READ_NOT
);
4548 case '>': /* (?>...) stop backtrack */
4549 *np
= node_new_enclose(ENCLOSE_STOP_BACKTRACK
);
4552 #ifdef USE_NAMED_GROUP
4554 if (IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_QMARK_LT_NAMED_GROUP
)) {
4558 return ONIGERR_UNDEFINED_GROUP_OPTION
;
4562 case '<': /* look behind (?<=...), (?<!...) */
4565 *np
= onig_node_new_anchor(ANCHOR_LOOK_BEHIND
);
4567 *np
= onig_node_new_anchor(ANCHOR_LOOK_BEHIND_NOT
);
4568 #ifdef USE_NAMED_GROUP
4570 if (IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_QMARK_LT_NAMED_GROUP
)) {
4582 r
= fetch_name((OnigCodePoint
)c
, &p
, end
, &name_end
, env
, &num
, 0);
4583 if (r
< 0) return r
;
4585 num
= scan_env_add_mem_entry(env
);
4586 if (num
< 0) return num
;
4587 if (list_capture
!= 0 && num
>= (int )BIT_STATUS_BITS_NUM
)
4588 return ONIGERR_GROUP_NUMBER_OVER_FOR_CAPTURE_HISTORY
;
4590 r
= name_add(env
->reg
, name
, name_end
, num
, env
);
4591 if (r
!= 0) return r
;
4592 *np
= node_new_enclose_memory(env
->option
, 1);
4593 CHECK_NULL_RETURN_MEMERR(*np
);
4594 NENCLOSE(*np
)->regnum
= num
;
4595 if (list_capture
!= 0)
4596 BIT_STATUS_ON_AT_SIMPLE(env
->capture_history
, num
);
4600 return ONIGERR_UNDEFINED_GROUP_OPTION
;
4605 return ONIGERR_UNDEFINED_GROUP_OPTION
;
4611 if (IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_ATMARK_CAPTURE_HISTORY
)) {
4612 #ifdef USE_NAMED_GROUP
4613 if (IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_QMARK_LT_NAMED_GROUP
)) {
4615 if (c
== '<' || c
== '\'') {
4617 goto named_group2
; /* (?@<name>...) */
4622 *np
= node_new_enclose_memory(env
->option
, 0);
4623 CHECK_NULL_RETURN_MEMERR(*np
);
4624 num
= scan_env_add_mem_entry(env
);
4626 onig_node_free(*np
);
4629 else if (num
>= (int )BIT_STATUS_BITS_NUM
) {
4630 onig_node_free(*np
);
4631 return ONIGERR_GROUP_NUMBER_OVER_FOR_CAPTURE_HISTORY
;
4633 NENCLOSE(*np
)->regnum
= num
;
4634 BIT_STATUS_ON_AT_SIMPLE(env
->capture_history
, num
);
4637 return ONIGERR_UNDEFINED_GROUP_OPTION
;
4641 #ifdef USE_POSIXLINE_OPTION
4644 case '-': case 'i': case 'm': case 's': case 'x':
4654 case '-': neg
= 1; break;
4655 case 'x': ONOFF(option
, ONIG_OPTION_EXTEND
, neg
); break;
4656 case 'i': ONOFF(option
, ONIG_OPTION_IGNORECASE
, neg
); break;
4658 if (IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_OPTION_PERL
)) {
4659 ONOFF(option
, ONIG_OPTION_MULTILINE
, neg
);
4662 return ONIGERR_UNDEFINED_GROUP_OPTION
;
4666 if (IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_OPTION_PERL
)) {
4667 ONOFF(option
, ONIG_OPTION_SINGLELINE
, (neg
== 0 ? 1 : 0));
4669 else if (IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_OPTION_RUBY
)) {
4670 ONOFF(option
, ONIG_OPTION_MULTILINE
, neg
);
4673 return ONIGERR_UNDEFINED_GROUP_OPTION
;
4675 #ifdef USE_POSIXLINE_OPTION
4677 ONOFF(option
, ONIG_OPTION_MULTILINE
|ONIG_OPTION_SINGLELINE
, neg
);
4681 return ONIGERR_UNDEFINED_GROUP_OPTION
;
4685 *np
= node_new_option(option
);
4686 CHECK_NULL_RETURN_MEMERR(*np
);
4688 return 2; /* option only */
4690 else if (c
== ':') {
4691 OnigOptionType prev
= env
->option
;
4693 env
->option
= option
;
4694 r
= fetch_token(tok
, &p
, end
, env
);
4695 if (r
< 0) return r
;
4696 r
= parse_subexp(&target
, tok
, term
, &p
, end
, env
);
4698 if (r
< 0) return r
;
4699 *np
= node_new_option(option
);
4700 CHECK_NULL_RETURN_MEMERR(*np
);
4701 NENCLOSE(*np
)->target
= target
;
4706 if (PEND
) return ONIGERR_END_PATTERN_IN_GROUP
;
4713 return ONIGERR_UNDEFINED_GROUP_OPTION
;
4717 if (ONIG_IS_OPTION_ON(env
->option
, ONIG_OPTION_DONT_CAPTURE_GROUP
))
4720 *np
= node_new_enclose_memory(env
->option
, 0);
4721 CHECK_NULL_RETURN_MEMERR(*np
);
4722 num
= scan_env_add_mem_entry(env
);
4723 if (num
< 0) return num
;
4724 NENCLOSE(*np
)->regnum
= num
;
4727 CHECK_NULL_RETURN_MEMERR(*np
);
4728 r
= fetch_token(tok
, &p
, end
, env
);
4729 if (r
< 0) return r
;
4730 r
= parse_subexp(&target
, tok
, term
, &p
, end
, env
);
4731 if (r
< 0) return r
;
4733 if (NTYPE(*np
) == NT_ANCHOR
)
4734 NANCHOR(*np
)->target
= target
;
4736 NENCLOSE(*np
)->target
= target
;
4737 if (NENCLOSE(*np
)->type
== ENCLOSE_MEMORY
) {
4738 /* Don't move this to previous of parse_subexp() */
4739 r
= scan_env_set_mem_node(env
, NENCLOSE(*np
)->regnum
, *np
);
4740 if (r
!= 0) return r
;
4748 static const char* const PopularQStr
[] = {
4749 "?", "*", "+", "??", "*?", "+?"
4752 static const char* const ReduceQStr
[] = {
4753 "", "", "*", "*?", "??", "+ and ??", "+? and ?"
4757 set_quantifier(Node
* qnode
, Node
* target
, int group
, ScanEnv
* env
)
4762 if (qn
->lower
== 1 && qn
->upper
== 1) {
4766 switch (NTYPE(target
)) {
4769 StrNode
* sn
= NSTR(target
);
4770 if (str_node_can_be_split(sn
, env
->enc
)) {
4771 Node
* n
= str_node_split_last_char(sn
, env
->enc
);
4772 if (IS_NOT_NULL(n
)) {
4781 { /* check redundant double repeat. */
4782 /* verbose warn (?:.?)? etc... but not warn (.?)? etc... */
4783 QtfrNode
* qnt
= NQTFR(target
);
4784 int nestq_num
= popular_quantifier_num(qn
);
4785 int targetq_num
= popular_quantifier_num(qnt
);
4787 #ifdef USE_WARNING_REDUNDANT_NESTED_REPEAT_OPERATOR
4788 if (!IS_QUANTIFIER_BY_NUMBER(qn
) && !IS_QUANTIFIER_BY_NUMBER(qnt
) &&
4789 IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_WARN_REDUNDANT_NESTED_REPEAT
)) {
4790 UChar buf
[WARN_BUFSIZE
];
4792 switch(ReduceTypeTable
[targetq_num
][nestq_num
]) {
4797 if (onig_verb_warn
!= onig_null_warn
) {
4798 onig_snprintf_with_pattern(buf
, WARN_BUFSIZE
, env
->enc
,
4799 env
->pattern
, env
->pattern_end
,
4800 (UChar
* )"redundant nested repeat operator");
4801 (*onig_verb_warn
)((char* )buf
);
4807 if (onig_verb_warn
!= onig_null_warn
) {
4808 onig_snprintf_with_pattern(buf
, WARN_BUFSIZE
, env
->enc
,
4809 env
->pattern
, env
->pattern_end
,
4810 (UChar
* )"nested repeat operator %s and %s was replaced with '%s'",
4811 PopularQStr
[targetq_num
], PopularQStr
[nestq_num
],
4812 ReduceQStr
[ReduceTypeTable
[targetq_num
][nestq_num
]]);
4813 (*onig_verb_warn
)((char* )buf
);
4822 if (targetq_num
>= 0) {
4823 if (nestq_num
>= 0) {
4824 onig_reduce_nested_quantifier(qnode
, target
);
4827 else if (targetq_num
== 1 || targetq_num
== 2) { /* * or + */
4828 /* (?:a*){n,m}, (?:a+){n,m} => (?:a*){n,n}, (?:a+){n,n} */
4829 if (! IS_REPEAT_INFINITE(qn
->upper
) && qn
->upper
> 1 && qn
->greedy
) {
4830 qn
->upper
= (qn
->lower
== 0 ? 1 : qn
->lower
);
4841 qn
->target
= target
;
4847 #ifdef USE_SHARED_CCLASS_TABLE
4849 #define THRESHOLD_RANGE_NUM_FOR_SHARE_CCLASS 8
4851 /* for ctype node hash table */
4859 static int type_cclass_cmp(type_cclass_key
* x
, type_cclass_key
* y
)
4861 if (x
->type
!= y
->type
) return 1;
4862 if (x
->enc
!= y
->enc
) return 1;
4863 if (x
->not != y
->not) return 1;
4867 static int type_cclass_hash(type_cclass_key
* key
)
4874 p
= (UChar
* )&(key
->enc
);
4875 for (i
= 0; i
< (int )sizeof(key
->enc
); i
++) {
4876 val
= val
* 997 + (int )*p
++;
4879 p
= (UChar
* )(&key
->type
);
4880 for (i
= 0; i
< (int )sizeof(key
->type
); i
++) {
4881 val
= val
* 997 + (int )*p
++;
4885 return val
+ (val
>> 5);
4888 static const struct st_hash_type type_type_cclass_hash
= {
4893 static st_table
* OnigTypeCClassTable
;
4897 i_free_shared_class(type_cclass_key
* key
, Node
* node
, void* arg ARG_UNUSED
)
4899 if (IS_NOT_NULL(node
)) {
4900 CClassNode
* cc
= NCCLASS(node
);
4901 if (IS_NOT_NULL(cc
->mbuf
)) xfree(cc
->mbuf
);
4905 if (IS_NOT_NULL(key
)) xfree(key
);
4910 onig_free_shared_cclass_table(void)
4912 THREAD_ATOMIC_START
;
4913 if (IS_NOT_NULL(OnigTypeCClassTable
)) {
4914 onig_st_foreach(OnigTypeCClassTable
, i_free_shared_class
, 0);
4915 onig_st_free_table(OnigTypeCClassTable
);
4916 OnigTypeCClassTable
= NULL
;
4923 #endif /* USE_SHARED_CCLASS_TABLE */
4926 #ifndef CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS
4928 clear_not_flag_cclass(CClassNode
* cc
, OnigEncoding enc
)
4933 if (IS_NCCLASS_NOT(cc
)) {
4934 bitset_invert(cc
->bs
);
4936 if (! ONIGENC_IS_SINGLEBYTE(enc
)) {
4937 r
= not_code_range_buf(enc
, cc
->mbuf
, &tbuf
);
4938 if (r
!= 0) return r
;
4940 bbuf_free(cc
->mbuf
);
4944 NCCLASS_CLEAR_NOT(cc
);
4949 #endif /* CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS */
4956 } IApplyCaseFoldArg
;
4959 i_apply_case_fold(OnigCodePoint from
, OnigCodePoint to
[],
4960 int to_len
, void* arg
)
4962 IApplyCaseFoldArg
* iarg
;
4967 iarg
= (IApplyCaseFoldArg
* )arg
;
4973 int is_in
= onig_is_code_in_cc(env
->enc
, from
, cc
);
4974 #ifdef CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS
4975 if ((is_in
!= 0 && !IS_NCCLASS_NOT(cc
)) ||
4976 (is_in
== 0 && IS_NCCLASS_NOT(cc
))) {
4977 if (ONIGENC_MBC_MINLEN(env
->enc
) > 1 || *to
>= SINGLE_BYTE_SIZE
) {
4978 add_code_range(&(cc
->mbuf
), env
, *to
, *to
);
4981 BITSET_SET_BIT(bs
, *to
);
4986 if (ONIGENC_MBC_MINLEN(env
->enc
) > 1 || *to
>= SINGLE_BYTE_SIZE
) {
4987 if (IS_NCCLASS_NOT(cc
)) clear_not_flag_cclass(cc
, env
->enc
);
4988 add_code_range(&(cc
->mbuf
), env
, *to
, *to
);
4991 if (IS_NCCLASS_NOT(cc
)) {
4992 BITSET_CLEAR_BIT(bs
, *to
);
4995 BITSET_SET_BIT(bs
, *to
);
4998 #endif /* CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS */
5002 UChar buf
[ONIGENC_CODE_TO_MBC_MAXLEN
];
5003 Node
*snode
= NULL_NODE
;
5005 if (onig_is_code_in_cc(env
->enc
, from
, cc
)
5006 #ifdef CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS
5007 && !IS_NCCLASS_NOT(cc
)
5010 for (i
= 0; i
< to_len
; i
++) {
5011 len
= ONIGENC_CODE_TO_MBC(env
->enc
, to
[i
], buf
);
5013 snode
= onig_node_new_str(buf
, buf
+ len
);
5014 CHECK_NULL_RETURN_MEMERR(snode
);
5016 /* char-class expanded multi-char only
5017 compare with string folded at match time. */
5018 NSTRING_SET_AMBIG(snode
);
5021 r
= onig_node_str_cat(snode
, buf
, buf
+ len
);
5023 onig_node_free(snode
);
5029 *(iarg
->ptail
) = onig_node_new_alt(snode
, NULL_NODE
);
5030 CHECK_NULL_RETURN_MEMERR(*(iarg
->ptail
));
5031 iarg
->ptail
= &(NCDR((*(iarg
->ptail
))));
5039 parse_exp(Node
** np
, OnigToken
* tok
, int term
,
5040 UChar
** src
, UChar
* end
, ScanEnv
* env
)
5042 int r
, len
, group
= 0;
5047 if (tok
->type
== (enum TokenSyms
)term
)
5050 switch (tok
->type
) {
5054 *np
= node_new_empty();
5058 case TK_SUBEXP_OPEN
:
5059 r
= parse_enclose(np
, tok
, TK_SUBEXP_CLOSE
, src
, end
, env
);
5060 if (r
< 0) return r
;
5061 if (r
== 1) group
= 1;
5062 else if (r
== 2) { /* option only */
5064 OnigOptionType prev
= env
->option
;
5066 env
->option
= NENCLOSE(*np
)->option
;
5067 r
= fetch_token(tok
, src
, end
, env
);
5068 if (r
< 0) return r
;
5069 r
= parse_subexp(&target
, tok
, term
, src
, end
, env
);
5071 if (r
< 0) return r
;
5072 NENCLOSE(*np
)->target
= target
;
5077 case TK_SUBEXP_CLOSE
:
5078 if (! IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_ALLOW_UNMATCHED_CLOSE_SUBEXP
))
5079 return ONIGERR_UNMATCHED_CLOSE_PARENTHESIS
;
5081 if (tok
->escaped
) goto tk_raw_byte
;
5088 *np
= node_new_str(tok
->backp
, *src
);
5089 CHECK_NULL_RETURN_MEMERR(*np
);
5092 r
= fetch_token(tok
, src
, end
, env
);
5093 if (r
< 0) return r
;
5094 if (r
!= TK_STRING
) break;
5096 r
= onig_node_str_cat(*np
, tok
->backp
, *src
);
5097 if (r
< 0) return r
;
5109 *np
= node_new_str_raw_char((UChar
)tok
->u
.c
);
5110 CHECK_NULL_RETURN_MEMERR(*np
);
5113 if (len
>= ONIGENC_MBC_MINLEN(env
->enc
)) {
5114 if (len
== enclen(env
->enc
, NSTR(*np
)->s
, NSTR(*np
)->end
)) {
5115 r
= fetch_token(tok
, src
, end
, env
);
5116 NSTRING_CLEAR_RAW(*np
);
5121 r
= fetch_token(tok
, src
, end
, env
);
5122 if (r
< 0) return r
;
5123 if (r
!= TK_RAW_BYTE
) {
5124 /* Don't use this, it is wrong for little endian encodings. */
5125 #ifdef USE_PAD_TO_SHORT_BYTE_CHAR
5127 if (len
< ONIGENC_MBC_MINLEN(env
->enc
)) {
5128 rem
= ONIGENC_MBC_MINLEN(env
->enc
) - len
;
5129 (void )node_str_head_pad(NSTR(*np
), rem
, (UChar
)0);
5130 if (len
+ rem
== enclen(env
->enc
, NSTR(*np
)->s
)) {
5131 NSTRING_CLEAR_RAW(*np
);
5136 return ONIGERR_TOO_SHORT_MULTI_BYTE_STRING
;
5139 r
= node_str_cat_char(*np
, (UChar
)tok
->u
.c
);
5140 if (r
< 0) return r
;
5149 UChar buf
[ONIGENC_CODE_TO_MBC_MAXLEN
];
5150 int num
= ONIGENC_CODE_TO_MBC(env
->enc
, tok
->u
.code
, buf
);
5151 if (num
< 0) return num
;
5152 #ifdef NUMBERED_CHAR_IS_NOT_CASE_AMBIG
5153 *np
= node_new_str_raw(buf
, buf
+ num
);
5155 *np
= node_new_str(buf
, buf
+ num
);
5157 CHECK_NULL_RETURN_MEMERR(*np
);
5163 OnigCodePoint end_op
[2];
5164 UChar
*qstart
, *qend
, *nextp
;
5166 end_op
[0] = (OnigCodePoint
)MC_ESC(env
->syntax
);
5167 end_op
[1] = (OnigCodePoint
)'E';
5169 qend
= find_str_position(end_op
, 2, qstart
, end
, &nextp
, env
->enc
);
5170 if (IS_NULL(qend
)) {
5173 *np
= node_new_str(qstart
, qend
);
5174 CHECK_NULL_RETURN_MEMERR(*np
);
5181 switch (tok
->u
.prop
.ctype
) {
5182 case ONIGENC_CTYPE_WORD
:
5183 *np
= node_new_ctype(tok
->u
.prop
.ctype
, tok
->u
.prop
.not);
5184 CHECK_NULL_RETURN_MEMERR(*np
);
5187 case ONIGENC_CTYPE_SPACE
:
5188 case ONIGENC_CTYPE_DIGIT
:
5189 case ONIGENC_CTYPE_XDIGIT
:
5193 #ifdef USE_SHARED_CCLASS_TABLE
5194 const OnigCodePoint
*mbr
;
5195 OnigCodePoint sb_out
;
5197 r
= ONIGENC_GET_CTYPE_CODE_RANGE(env
->enc
, tok
->u
.prop
.ctype
,
5200 ONIGENC_CODE_RANGE_NUM(mbr
)
5201 >= THRESHOLD_RANGE_NUM_FOR_SHARE_CCLASS
) {
5202 type_cclass_key key
;
5203 type_cclass_key
* new_key
;
5206 key
.not = tok
->u
.prop
.not;
5207 key
.type
= tok
->u
.prop
.ctype
;
5209 THREAD_ATOMIC_START
;
5211 if (IS_NULL(OnigTypeCClassTable
)) {
5213 = onig_st_init_table_with_size(&type_type_cclass_hash
, 10);
5214 if (IS_NULL(OnigTypeCClassTable
)) {
5216 return ONIGERR_MEMORY
;
5220 if (onig_st_lookup(OnigTypeCClassTable
, (st_data_t
)&key
,
5227 *np
= node_new_cclass_by_codepoint_range(tok
->u
.prop
.not,
5231 return ONIGERR_MEMORY
;
5235 NCCLASS_SET_SHARE(cc
);
5236 new_key
= (type_cclass_key
* )xmalloc(sizeof(type_cclass_key
));
5237 xmemcpy(new_key
, &key
, sizeof(type_cclass_key
));
5238 onig_st_add_direct(OnigTypeCClassTable
, (st_data_t
)new_key
,
5245 *np
= node_new_cclass();
5246 CHECK_NULL_RETURN_MEMERR(*np
);
5248 add_ctype_to_cc(cc
, tok
->u
.prop
.ctype
, 0, env
);
5249 if (tok
->u
.prop
.not != 0) NCCLASS_SET_NOT(cc
);
5250 #ifdef USE_SHARED_CCLASS_TABLE
5257 return ONIGERR_PARSER_BUG
;
5263 case TK_CHAR_PROPERTY
:
5264 r
= parse_char_property(np
, tok
, src
, end
, env
);
5265 if (r
!= 0) return r
;
5272 r
= parse_char_class(np
, tok
, src
, end
, env
);
5273 if (r
!= 0) return r
;
5276 if (IS_IGNORECASE(env
->option
)) {
5277 IApplyCaseFoldArg iarg
;
5281 iarg
.alt_root
= NULL_NODE
;
5282 iarg
.ptail
= &(iarg
.alt_root
);
5284 r
= ONIGENC_APPLY_ALL_CASE_FOLD(env
->enc
, env
->case_fold_flag
,
5285 i_apply_case_fold
, &iarg
);
5287 onig_node_free(iarg
.alt_root
);
5290 if (IS_NOT_NULL(iarg
.alt_root
)) {
5291 Node
* work
= onig_node_new_alt(*np
, iarg
.alt_root
);
5292 if (IS_NULL(work
)) {
5293 onig_node_free(iarg
.alt_root
);
5294 return ONIGERR_MEMORY
;
5303 *np
= node_new_anychar();
5304 CHECK_NULL_RETURN_MEMERR(*np
);
5307 case TK_ANYCHAR_ANYTIME
:
5308 *np
= node_new_anychar();
5309 CHECK_NULL_RETURN_MEMERR(*np
);
5310 qn
= node_new_quantifier(0, REPEAT_INFINITE
, 0);
5311 CHECK_NULL_RETURN_MEMERR(qn
);
5312 NQTFR(qn
)->target
= *np
;
5317 len
= tok
->u
.backref
.num
;
5318 *np
= node_new_backref(len
,
5319 (len
> 1 ? tok
->u
.backref
.refs
: &(tok
->u
.backref
.ref1
)),
5320 tok
->u
.backref
.by_name
,
5321 #ifdef USE_BACKREF_WITH_LEVEL
5322 tok
->u
.backref
.exist_level
,
5323 tok
->u
.backref
.level
,
5326 CHECK_NULL_RETURN_MEMERR(*np
);
5329 #ifdef USE_SUBEXP_CALL
5332 int gnum
= tok
->u
.call
.gnum
;
5335 gnum
= BACKREF_REL_TO_ABS(gnum
, env
);
5337 return ONIGERR_INVALID_BACKREF
;
5339 *np
= node_new_call(tok
->u
.call
.name
, tok
->u
.call
.name_end
, gnum
);
5340 CHECK_NULL_RETURN_MEMERR(*np
);
5347 *np
= onig_node_new_anchor(tok
->u
.anchor
);
5352 if (IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_CONTEXT_INDEP_REPEAT_OPS
)) {
5353 if (IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_CONTEXT_INVALID_REPEAT_OPS
))
5354 return ONIGERR_TARGET_OF_REPEAT_OPERATOR_NOT_SPECIFIED
;
5356 *np
= node_new_empty();
5364 return ONIGERR_PARSER_BUG
;
5372 r
= fetch_token(tok
, src
, end
, env
);
5373 if (r
< 0) return r
;
5376 if (r
== TK_OP_REPEAT
|| r
== TK_INTERVAL
) {
5377 if (is_invalid_quantifier_target(*targetp
))
5378 return ONIGERR_TARGET_OF_REPEAT_OPERATOR_INVALID
;
5380 qn
= node_new_quantifier(tok
->u
.repeat
.lower
, tok
->u
.repeat
.upper
,
5381 (r
== TK_INTERVAL
? 1 : 0));
5382 CHECK_NULL_RETURN_MEMERR(qn
);
5383 NQTFR(qn
)->greedy
= tok
->u
.repeat
.greedy
;
5384 r
= set_quantifier(qn
, *targetp
, group
, env
);
5385 if (r
< 0) return r
;
5387 if (tok
->u
.repeat
.possessive
!= 0) {
5389 en
= node_new_enclose(ENCLOSE_STOP_BACKTRACK
);
5390 CHECK_NULL_RETURN_MEMERR(en
);
5391 NENCLOSE(en
)->target
= qn
;
5401 else if (r
== 2) { /* split case: /abc+/ */
5404 *targetp
= node_new_list(*targetp
, NULL
);
5405 CHECK_NULL_RETURN_MEMERR(*targetp
);
5406 tmp
= NCDR(*targetp
) = node_new_list(qn
, NULL
);
5407 CHECK_NULL_RETURN_MEMERR(tmp
);
5408 targetp
= &(NCAR(tmp
));
5418 parse_branch(Node
** top
, OnigToken
* tok
, int term
,
5419 UChar
** src
, UChar
* end
, ScanEnv
* env
)
5422 Node
*node
, **headp
;
5425 r
= parse_exp(&node
, tok
, term
, src
, end
, env
);
5426 if (r
< 0) return r
;
5428 if (r
== TK_EOT
|| r
== term
|| r
== TK_ALT
) {
5432 *top
= node_new_list(node
, NULL
);
5433 headp
= &(NCDR(*top
));
5434 while (r
!= TK_EOT
&& r
!= term
&& r
!= TK_ALT
) {
5435 r
= parse_exp(&node
, tok
, term
, src
, end
, env
);
5436 if (r
< 0) return r
;
5438 if (NTYPE(node
) == NT_LIST
) {
5440 while (IS_NOT_NULL(NCDR(node
))) node
= NCDR(node
);
5441 headp
= &(NCDR(node
));
5444 *headp
= node_new_list(node
, NULL
);
5445 headp
= &(NCDR(*headp
));
5453 /* term_tok: TK_EOT or TK_SUBEXP_CLOSE */
5455 parse_subexp(Node
** top
, OnigToken
* tok
, int term
,
5456 UChar
** src
, UChar
* end
, ScanEnv
* env
)
5459 Node
*node
, **headp
;
5462 r
= parse_branch(&node
, tok
, term
, src
, end
, env
);
5464 onig_node_free(node
);
5471 else if (r
== TK_ALT
) {
5472 *top
= onig_node_new_alt(node
, NULL
);
5473 headp
= &(NCDR(*top
));
5474 while (r
== TK_ALT
) {
5475 r
= fetch_token(tok
, src
, end
, env
);
5476 if (r
< 0) return r
;
5477 r
= parse_branch(&node
, tok
, term
, src
, end
, env
);
5478 if (r
< 0) return r
;
5480 *headp
= onig_node_new_alt(node
, NULL
);
5481 headp
= &(NCDR(*headp
));
5484 if (tok
->type
!= (enum TokenSyms
)term
)
5489 if (term
== TK_SUBEXP_CLOSE
)
5490 return ONIGERR_END_PATTERN_WITH_UNMATCHED_PARENTHESIS
;
5492 return ONIGERR_PARSER_BUG
;
5499 parse_regexp(Node
** top
, UChar
** src
, UChar
* end
, ScanEnv
* env
)
5504 r
= fetch_token(&tok
, src
, end
, env
);
5505 if (r
< 0) return r
;
5506 r
= parse_subexp(top
, &tok
, TK_EOT
, src
, end
, env
);
5507 if (r
< 0) return r
;
5512 onig_parse_make_tree(Node
** root
, const UChar
* pattern
, const UChar
* end
,
5513 regex_t
* reg
, ScanEnv
* env
)
5518 #ifdef USE_NAMED_GROUP
5522 scan_env_clear(env
);
5523 env
->option
= reg
->options
;
5524 env
->case_fold_flag
= reg
->case_fold_flag
;
5525 env
->enc
= reg
->enc
;
5526 env
->syntax
= reg
->syntax
;
5527 env
->pattern
= (UChar
* )pattern
;
5528 env
->pattern_end
= (UChar
* )end
;
5532 p
= (UChar
* )pattern
;
5533 r
= parse_regexp(root
, &p
, (UChar
* )end
, env
);
5534 reg
->num_mem
= env
->num_mem
;
5539 onig_scan_env_set_error_string(ScanEnv
* env
, int ecode ARG_UNUSED
,
5540 UChar
* arg
, UChar
* arg_end
)
5543 env
->error_end
= arg_end
;