1 /**********************************************************************
2 regparse.c - Oniguruma (regular expression library)
3 **********************************************************************/
5 * Copyright (c) 2002-2019 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
33 #ifdef DEBUG_NODE_FREE
37 #define INIT_TAG_NAMES_ALLOC_NUM 5
39 #define WARN_BUFSIZE 256
41 #define CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS
43 #define IS_ALLOWED_CODE_IN_CALLOUT_NAME(c) \
44 ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '_' /* || c == '!' */)
45 #define IS_ALLOWED_CODE_IN_CALLOUT_TAG_NAME(c) \
46 ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '_')
49 OnigSyntaxType OnigSyntaxOniguruma
= {
50 (( SYN_GNU_REGEX_OP
| ONIG_SYN_OP_QMARK_NON_GREEDY
|
51 ONIG_SYN_OP_ESC_OCTAL3
| ONIG_SYN_OP_ESC_X_HEX2
|
52 ONIG_SYN_OP_ESC_X_BRACE_HEX8
| ONIG_SYN_OP_ESC_O_BRACE_OCTAL
|
53 ONIG_SYN_OP_ESC_CONTROL_CHARS
|
54 ONIG_SYN_OP_ESC_C_CONTROL
)
55 & ~ONIG_SYN_OP_ESC_LTGT_WORD_BEGIN_END
)
56 , ( ONIG_SYN_OP2_QMARK_GROUP_EFFECT
|
57 ONIG_SYN_OP2_OPTION_ONIGURUMA
|
58 ONIG_SYN_OP2_QMARK_LT_NAMED_GROUP
| ONIG_SYN_OP2_ESC_K_NAMED_BACKREF
|
59 ONIG_SYN_OP2_QMARK_LPAREN_IF_ELSE
|
60 ONIG_SYN_OP2_QMARK_TILDE_ABSENT_GROUP
|
61 ONIG_SYN_OP2_QMARK_BRACE_CALLOUT_CONTENTS
|
62 ONIG_SYN_OP2_ASTERISK_CALLOUT_NAME
|
63 ONIG_SYN_OP2_ESC_X_Y_TEXT_SEGMENT
|
64 ONIG_SYN_OP2_ESC_CAPITAL_R_GENERAL_NEWLINE
|
65 ONIG_SYN_OP2_ESC_CAPITAL_N_O_SUPER_DOT
|
66 ONIG_SYN_OP2_ESC_CAPITAL_K_KEEP
|
67 ONIG_SYN_OP2_ESC_G_SUBEXP_CALL
|
68 ONIG_SYN_OP2_ESC_P_BRACE_CHAR_PROPERTY
|
69 ONIG_SYN_OP2_ESC_P_BRACE_CIRCUMFLEX_NOT
|
70 ONIG_SYN_OP2_PLUS_POSSESSIVE_REPEAT
|
71 ONIG_SYN_OP2_CCLASS_SET_OP
| ONIG_SYN_OP2_ESC_CAPITAL_C_BAR_CONTROL
|
72 ONIG_SYN_OP2_ESC_CAPITAL_M_BAR_META
| ONIG_SYN_OP2_ESC_V_VTAB
|
73 ONIG_SYN_OP2_ESC_H_XDIGIT
| ONIG_SYN_OP2_ESC_U_HEX4
)
74 , ( SYN_GNU_REGEX_BV
|
75 ONIG_SYN_ALLOW_INTERVAL_LOW_ABBREV
|
76 ONIG_SYN_DIFFERENT_LEN_ALT_LOOK_BEHIND
|
77 ONIG_SYN_CAPTURE_ONLY_NAMED_GROUP
|
78 ONIG_SYN_ALLOW_MULTIPLEX_DEFINITION_NAME
|
79 ONIG_SYN_FIXED_INTERVAL_IS_GREEDY_ONLY
|
80 ONIG_SYN_ALLOW_INVALID_CODE_END_OF_RANGE_IN_CC
|
81 ONIG_SYN_WARN_CC_OP_NOT_ESCAPED
|
82 ONIG_SYN_WARN_REDUNDANT_NESTED_REPEAT
)
86 (OnigCodePoint
)'\\' /* esc */
87 , (OnigCodePoint
)ONIG_INEFFECTIVE_META_CHAR
/* anychar '.' */
88 , (OnigCodePoint
)ONIG_INEFFECTIVE_META_CHAR
/* anytime '*' */
89 , (OnigCodePoint
)ONIG_INEFFECTIVE_META_CHAR
/* zero or one time '?' */
90 , (OnigCodePoint
)ONIG_INEFFECTIVE_META_CHAR
/* one or more time '+' */
91 , (OnigCodePoint
)ONIG_INEFFECTIVE_META_CHAR
/* anychar anytime */
95 OnigSyntaxType OnigSyntaxRuby
= {
96 (( SYN_GNU_REGEX_OP
| ONIG_SYN_OP_QMARK_NON_GREEDY
|
97 ONIG_SYN_OP_ESC_OCTAL3
| ONIG_SYN_OP_ESC_X_HEX2
|
98 ONIG_SYN_OP_ESC_X_BRACE_HEX8
| ONIG_SYN_OP_ESC_O_BRACE_OCTAL
|
99 ONIG_SYN_OP_ESC_CONTROL_CHARS
|
100 ONIG_SYN_OP_ESC_C_CONTROL
)
101 & ~ONIG_SYN_OP_ESC_LTGT_WORD_BEGIN_END
)
102 , ( ONIG_SYN_OP2_QMARK_GROUP_EFFECT
|
103 ONIG_SYN_OP2_OPTION_RUBY
|
104 ONIG_SYN_OP2_QMARK_LT_NAMED_GROUP
| ONIG_SYN_OP2_ESC_K_NAMED_BACKREF
|
105 ONIG_SYN_OP2_QMARK_LPAREN_IF_ELSE
|
106 ONIG_SYN_OP2_QMARK_TILDE_ABSENT_GROUP
|
107 ONIG_SYN_OP2_ESC_X_Y_TEXT_SEGMENT
|
108 ONIG_SYN_OP2_ESC_CAPITAL_R_GENERAL_NEWLINE
|
109 ONIG_SYN_OP2_ESC_CAPITAL_K_KEEP
|
110 ONIG_SYN_OP2_ESC_G_SUBEXP_CALL
|
111 ONIG_SYN_OP2_ESC_P_BRACE_CHAR_PROPERTY
|
112 ONIG_SYN_OP2_ESC_P_BRACE_CIRCUMFLEX_NOT
|
113 ONIG_SYN_OP2_PLUS_POSSESSIVE_REPEAT
|
114 ONIG_SYN_OP2_CCLASS_SET_OP
| ONIG_SYN_OP2_ESC_CAPITAL_C_BAR_CONTROL
|
115 ONIG_SYN_OP2_ESC_CAPITAL_M_BAR_META
| ONIG_SYN_OP2_ESC_V_VTAB
|
116 ONIG_SYN_OP2_ESC_H_XDIGIT
| ONIG_SYN_OP2_ESC_U_HEX4
)
117 , ( SYN_GNU_REGEX_BV
|
118 ONIG_SYN_ALLOW_INTERVAL_LOW_ABBREV
|
119 ONIG_SYN_DIFFERENT_LEN_ALT_LOOK_BEHIND
|
120 ONIG_SYN_CAPTURE_ONLY_NAMED_GROUP
|
121 ONIG_SYN_ALLOW_MULTIPLEX_DEFINITION_NAME
|
122 ONIG_SYN_FIXED_INTERVAL_IS_GREEDY_ONLY
|
123 ONIG_SYN_WARN_CC_OP_NOT_ESCAPED
|
124 ONIG_SYN_WARN_REDUNDANT_NESTED_REPEAT
)
128 (OnigCodePoint
)'\\' /* esc */
129 , (OnigCodePoint
)ONIG_INEFFECTIVE_META_CHAR
/* anychar '.' */
130 , (OnigCodePoint
)ONIG_INEFFECTIVE_META_CHAR
/* anytime '*' */
131 , (OnigCodePoint
)ONIG_INEFFECTIVE_META_CHAR
/* zero or one time '?' */
132 , (OnigCodePoint
)ONIG_INEFFECTIVE_META_CHAR
/* one or more time '+' */
133 , (OnigCodePoint
)ONIG_INEFFECTIVE_META_CHAR
/* anychar anytime */
137 OnigSyntaxType
* OnigDefaultSyntax
= ONIG_SYNTAX_ONIGURUMA
;
139 extern void onig_null_warn(const char* s ARG_UNUSED
) { }
141 #ifdef DEFAULT_WARN_FUNCTION
142 static OnigWarnFunc onig_warn
= (OnigWarnFunc
)DEFAULT_WARN_FUNCTION
;
144 static OnigWarnFunc onig_warn
= onig_null_warn
;
147 #ifdef DEFAULT_VERB_WARN_FUNCTION
148 static OnigWarnFunc onig_verb_warn
= (OnigWarnFunc
)DEFAULT_VERB_WARN_FUNCTION
;
150 static OnigWarnFunc onig_verb_warn
= onig_null_warn
;
153 extern void onig_set_warn_func(OnigWarnFunc f
)
158 extern void onig_set_verb_warn_func(OnigWarnFunc f
)
164 onig_warning(const char* s
)
166 if (onig_warn
== onig_null_warn
) return ;
171 #define DEFAULT_MAX_CAPTURE_NUM 32767
173 static int MaxCaptureNum
= DEFAULT_MAX_CAPTURE_NUM
;
176 onig_set_capture_num_limit(int num
)
178 if (num
< 0) return -1;
184 static unsigned int ParseDepthLimit
= DEFAULT_PARSE_DEPTH_LIMIT
;
187 onig_get_parse_depth_limit(void)
189 return ParseDepthLimit
;
193 onig_set_parse_depth_limit(unsigned int depth
)
196 ParseDepthLimit
= DEFAULT_PARSE_DEPTH_LIMIT
;
198 ParseDepthLimit
= depth
;
203 bbuf_init(BBuf
* buf
, int size
)
210 buf
->p
= (UChar
* )xmalloc(size
);
211 if (IS_NULL(buf
->p
)) return(ONIGERR_MEMORY
);
220 bbuf_free(BBuf
* bbuf
)
222 if (IS_NOT_NULL(bbuf
)) {
223 if (IS_NOT_NULL(bbuf
->p
)) xfree(bbuf
->p
);
229 bbuf_clone(BBuf
** rto
, BBuf
* from
)
234 *rto
= to
= (BBuf
* )xmalloc(sizeof(BBuf
));
235 CHECK_NULL_RETURN_MEMERR(to
);
236 r
= BB_INIT(to
, from
->alloc
);
242 to
->used
= from
->used
;
243 xmemcpy(to
->p
, from
->p
, from
->used
);
247 static int backref_rel_to_abs(int rel_no
, ScanEnv
* env
)
250 return env
->num_mem
+ rel_no
;
253 return env
->num_mem
+ 1 + rel_no
;
257 #define OPTION_ON(v,f) ((v) |= (f))
258 #define OPTION_OFF(v,f) ((v) &= ~(f))
260 #define OPTION_NEGATE(v,f,negative) (negative) ? ((v) &= ~(f)) : ((v) |= (f))
262 #define MBCODE_START_POS(enc) \
263 (OnigCodePoint )(ONIGENC_MBC_MINLEN(enc) > 1 ? 0 : 0x80)
265 #define SET_ALL_MULTI_BYTE_RANGE(enc, pbuf) \
266 add_code_range_to_buf(pbuf, MBCODE_START_POS(enc), ~((OnigCodePoint )0))
268 #define ADD_ALL_MULTI_BYTE_RANGE(enc, mbuf) do {\
269 if (! ONIGENC_IS_SINGLEBYTE(enc)) {\
270 r = SET_ALL_MULTI_BYTE_RANGE(enc, &(mbuf));\
271 if (r != 0) return r;\
276 #define BITSET_IS_EMPTY(bs,empty) do {\
279 for (i = 0; i < (int )BITSET_SIZE; i++) {\
287 bitset_set_range(BitSetRef bs
, int from
, int to
)
290 for (i
= from
; i
<= to
&& i
< SINGLE_BYTE_SIZE
; i
++) {
291 BITSET_SET_BIT(bs
, i
);
297 bitset_set_all(BitSetRef bs
)
300 for (i
= 0; i
< BITSET_SIZE
; i
++) { bs
[i
] = ~((Bits
)0); }
305 bitset_invert(BitSetRef bs
)
308 for (i
= 0; i
< (int )BITSET_SIZE
; i
++) { bs
[i
] = ~(bs
[i
]); }
312 bitset_invert_to(BitSetRef from
, BitSetRef to
)
315 for (i
= 0; i
< (int )BITSET_SIZE
; i
++) { to
[i
] = ~(from
[i
]); }
319 bitset_and(BitSetRef dest
, BitSetRef bs
)
322 for (i
= 0; i
< (int )BITSET_SIZE
; i
++) { dest
[i
] &= bs
[i
]; }
326 bitset_or(BitSetRef dest
, BitSetRef bs
)
329 for (i
= 0; i
< (int )BITSET_SIZE
; i
++) { dest
[i
] |= bs
[i
]; }
333 bitset_copy(BitSetRef dest
, BitSetRef bs
)
336 for (i
= 0; i
< (int )BITSET_SIZE
; i
++) { dest
[i
] = bs
[i
]; }
340 onig_strncmp(const UChar
* s1
, const UChar
* s2
, int n
)
352 onig_strcpy(UChar
* dest
, const UChar
* src
, const UChar
* end
)
354 int len
= (int )(end
- src
);
356 xmemcpy(dest
, src
, len
);
357 dest
[len
] = (UChar
)0;
362 save_entry(ScanEnv
* env
, enum SaveType type
, int* id
)
364 int nid
= env
->save_num
;
367 if (IS_NULL(env
->saves
)) {
369 env
->saves
= (SaveItem
* )xmalloc(sizeof(SaveItem
) * n
);
370 CHECK_NULL_RETURN_MEMERR(env
->saves
);
371 env
->save_alloc_num
= n
;
373 else if (env
->save_alloc_num
<= nid
) {
374 int n
= env
->save_alloc_num
* 2;
375 SaveItem
* p
= (SaveItem
* )xrealloc(env
->saves
, sizeof(SaveItem
) * n
, sizeof(SaveItem
)*env
->save_alloc_num
);
376 CHECK_NULL_RETURN_MEMERR(p
);
378 env
->save_alloc_num
= n
;
381 env
->saves
[nid
].type
= type
;
389 /* scan pattern methods */
392 #define PFETCH_READY UChar* pfetch_prev
393 #define PEND (p < end ? 0 : 1)
394 #define PUNFETCH p = pfetch_prev
397 p += ONIGENC_MBC_ENC_LEN(enc, p); \
399 #define PFETCH(c) do { \
400 c = ONIGENC_MBC_TO_CODE(enc, p, end); \
402 p += ONIGENC_MBC_ENC_LEN(enc, p); \
405 #define PINC_S do { \
406 p += ONIGENC_MBC_ENC_LEN(enc, p); \
408 #define PFETCH_S(c) do { \
409 c = ONIGENC_MBC_TO_CODE(enc, p, end); \
410 p += ONIGENC_MBC_ENC_LEN(enc, p); \
413 #define PPEEK (p < end ? ONIGENC_MBC_TO_CODE(enc, p, end) : PEND_VALUE)
414 #define PPEEK_IS(c) (PPEEK == (OnigCodePoint )c)
417 strcat_capa(UChar
* dest
, UChar
* dest_end
, const UChar
* src
, const UChar
* src_end
,
418 int capa
, int oldCapa
)
423 r
= (UChar
* )xrealloc(dest
, capa
+ 1, oldCapa
);
425 r
= (UChar
* )xmalloc(capa
+ 1);
427 CHECK_NULL_RETURN(r
);
428 onig_strcpy(r
+ (dest_end
- dest
), src
, src_end
);
432 /* dest on static area */
434 strcat_capa_from_static(UChar
* dest
, UChar
* dest_end
,
435 const UChar
* src
, const UChar
* src_end
, int capa
)
439 r
= (UChar
* )xmalloc(capa
+ 1);
440 CHECK_NULL_RETURN(r
);
441 onig_strcpy(r
, dest
, dest_end
);
442 onig_strcpy(r
+ (dest_end
- dest
), src
, src_end
);
447 #ifdef USE_ST_LIBRARY
455 str_end_cmp(st_str_end_key
* x
, st_str_end_key
* y
)
460 if ((x
->end
- x
->s
) != (y
->end
- y
->s
))
466 c
= (int )*p
- (int )*q
;
467 if (c
!= 0) return c
;
476 str_end_hash(st_str_end_key
* x
)
483 val
= val
* 997 + (int )*p
++;
486 return val
+ (val
>> 5);
489 extern hash_table_type
*
490 onig_st_init_strend_table_with_size(int size
)
492 static struct st_hash_type hashType
= {
497 return (hash_table_type
* )
498 onig_st_init_table_with_size(&hashType
, size
);
502 onig_st_lookup_strend(hash_table_type
* table
, const UChar
* str_key
,
503 const UChar
* end_key
, hash_data_type
*value
)
507 key
.s
= (UChar
* )str_key
;
508 key
.end
= (UChar
* )end_key
;
510 return onig_st_lookup(table
, (st_data_t
)(&key
), value
);
514 onig_st_insert_strend(hash_table_type
* table
, const UChar
* str_key
,
515 const UChar
* end_key
, hash_data_type value
)
520 key
= (st_str_end_key
* )xmalloc(sizeof(st_str_end_key
));
521 CHECK_NULL_RETURN_MEMERR(key
);
523 key
->s
= (UChar
* )str_key
;
524 key
->end
= (UChar
* )end_key
;
525 result
= onig_st_insert(table
, (st_data_t
)key
, value
);
537 int type
; /* callout type: single or not */
540 } st_callout_name_key
;
543 callout_name_table_cmp(st_callout_name_key
* x
, st_callout_name_key
* y
)
548 if (x
->enc
!= y
->enc
) return 1;
549 if (x
->type
!= y
->type
) return 1;
550 if ((x
->end
- x
->s
) != (y
->end
- y
->s
))
556 c
= (int )*p
- (int )*q
;
557 if (c
!= 0) return c
;
566 callout_name_table_hash(st_callout_name_key
* x
)
573 val
= val
* 997 + (int )*p
++;
576 /* use intptr_t for escape warning in Windows */
577 return val
+ (val
>> 5) + ((intptr_t )x
->enc
& 0xffff) + x
->type
;
580 extern hash_table_type
*
581 onig_st_init_callout_name_table_with_size(int size
)
583 static struct st_hash_type hashType
= {
584 callout_name_table_cmp
,
585 callout_name_table_hash
,
588 return (hash_table_type
* )
589 onig_st_init_table_with_size(&hashType
, size
);
593 onig_st_lookup_callout_name_table(hash_table_type
* table
,
596 const UChar
* str_key
,
597 const UChar
* end_key
,
598 hash_data_type
*value
)
600 st_callout_name_key key
;
604 key
.s
= (UChar
* )str_key
;
605 key
.end
= (UChar
* )end_key
;
607 return onig_st_lookup(table
, (st_data_t
)(&key
), value
);
611 st_insert_callout_name_table(hash_table_type
* table
,
612 OnigEncoding enc
, int type
,
613 UChar
* str_key
, UChar
* end_key
,
614 hash_data_type value
)
616 st_callout_name_key
* key
;
619 key
= (st_callout_name_key
* )xmalloc(sizeof(st_callout_name_key
));
620 CHECK_NULL_RETURN_MEMERR(key
);
622 /* key->s: don't duplicate, because str_key is duped in callout_name_entry() */
627 result
= onig_st_insert(table
, (st_data_t
)key
, value
);
635 #endif /* USE_ST_LIBRARY */
638 #define INIT_NAME_BACKREFS_ALLOC_NUM 8
642 int name_len
; /* byte length */
643 int back_num
; /* number of backrefs */
649 #ifdef USE_ST_LIBRARY
651 #define INIT_NAMES_ALLOC_NUM 5
653 typedef st_table NameTable
;
654 typedef st_data_t HashDataType
; /* 1.6 st.h doesn't define st_data_t type */
656 #define NAMEBUF_SIZE 24
657 #define NAMEBUF_SIZE_1 25
661 i_print_name_entry(UChar
* key
, NameEntry
* e
, void* arg
)
664 FILE* fp
= (FILE* )arg
;
666 fprintf(fp
, "%s: ", e
->name
);
667 if (e
->back_num
== 0)
669 else if (e
->back_num
== 1)
670 fprintf(fp
, "%d", e
->back_ref1
);
672 for (i
= 0; i
< e
->back_num
; i
++) {
673 if (i
> 0) fprintf(fp
, ", ");
674 fprintf(fp
, "%d", e
->back_refs
[i
]);
682 onig_print_names(FILE* fp
, regex_t
* reg
)
684 NameTable
* t
= (NameTable
* )reg
->name_table
;
686 if (IS_NOT_NULL(t
)) {
687 fprintf(fp
, "name table\n");
688 onig_st_foreach(t
, i_print_name_entry
, (HashDataType
)fp
);
693 #endif /* ONIG_DEBUG */
696 i_free_name_entry(UChar
* key
, NameEntry
* e
, void* arg ARG_UNUSED
)
699 if (IS_NOT_NULL(e
->back_refs
)) xfree(e
->back_refs
);
706 names_clear(regex_t
* reg
)
708 NameTable
* t
= (NameTable
* )reg
->name_table
;
710 if (IS_NOT_NULL(t
)) {
711 onig_st_foreach(t
, i_free_name_entry
, 0);
717 onig_names_free(regex_t
* reg
)
722 r
= names_clear(reg
);
723 if (r
!= 0) return r
;
725 t
= (NameTable
* )reg
->name_table
;
726 if (IS_NOT_NULL(t
)) onig_st_free_table(t
);
727 reg
->name_table
= (void* )NULL
;
732 name_find(regex_t
* reg
, const UChar
* name
, const UChar
* name_end
)
735 NameTable
* t
= (NameTable
* )reg
->name_table
;
737 e
= (NameEntry
* )NULL
;
738 if (IS_NOT_NULL(t
)) {
739 onig_st_lookup_strend(t
, name
, name_end
, (HashDataType
* )((void* )(&e
)));
745 int (*func
)(const UChar
*, const UChar
*,int,int*,regex_t
*,void*);
753 i_names(UChar
* key ARG_UNUSED
, NameEntry
* e
, INamesArg
* arg
)
755 int r
= (*(arg
->func
))(e
->name
,
756 e
->name
+ e
->name_len
,
758 (e
->back_num
> 1 ? e
->back_refs
: &(e
->back_ref1
)),
768 onig_foreach_name(regex_t
* reg
,
769 int (*func
)(const UChar
*, const UChar
*,int,int*,regex_t
*,void*), void* arg
)
772 NameTable
* t
= (NameTable
* )reg
->name_table
;
775 if (IS_NOT_NULL(t
)) {
779 narg
.enc
= reg
->enc
; /* should be pattern encoding. */
780 onig_st_foreach(t
, i_names
, (HashDataType
)&narg
);
786 i_renumber_name(UChar
* key ARG_UNUSED
, NameEntry
* e
, GroupNumRemap
* map
)
790 if (e
->back_num
> 1) {
791 for (i
= 0; i
< e
->back_num
; i
++) {
792 e
->back_refs
[i
] = map
[e
->back_refs
[i
]].new_val
;
795 else if (e
->back_num
== 1) {
796 e
->back_ref1
= map
[e
->back_ref1
].new_val
;
803 onig_renumber_name_table(regex_t
* reg
, GroupNumRemap
* map
)
805 NameTable
* t
= (NameTable
* )reg
->name_table
;
807 if (IS_NOT_NULL(t
)) {
808 onig_st_foreach(t
, i_renumber_name
, (HashDataType
)map
);
815 onig_number_of_names(regex_t
* reg
)
817 NameTable
* t
= (NameTable
* )reg
->name_table
;
820 return t
->num_entries
;
825 #else /* USE_ST_LIBRARY */
827 #define INIT_NAMES_ALLOC_NUM 8
837 onig_print_names(FILE* fp
, regex_t
* reg
)
841 NameTable
* t
= (NameTable
* )reg
->name_table
;
843 if (IS_NOT_NULL(t
) && t
->num
> 0) {
844 fprintf(fp
, "name table\n");
845 for (i
= 0; i
< t
->num
; i
++) {
847 fprintf(fp
, "%s: ", e
->name
);
848 if (e
->back_num
== 0) {
851 else if (e
->back_num
== 1) {
852 fprintf(fp
, "%d", e
->back_ref1
);
855 for (j
= 0; j
< e
->back_num
; j
++) {
856 if (j
> 0) fprintf(fp
, ", ");
857 fprintf(fp
, "%d", e
->back_refs
[j
]);
869 names_clear(regex_t
* reg
)
873 NameTable
* t
= (NameTable
* )reg
->name_table
;
875 if (IS_NOT_NULL(t
)) {
876 for (i
= 0; i
< t
->num
; i
++) {
878 if (IS_NOT_NULL(e
->name
)) {
884 if (IS_NOT_NULL(e
->back_refs
)) xfree(e
->back_refs
);
885 e
->back_refs
= (int* )NULL
;
888 if (IS_NOT_NULL(t
->e
)) {
898 onig_names_free(regex_t
* reg
)
903 r
= names_clear(reg
);
904 if (r
!= 0) return r
;
906 t
= (NameTable
* )reg
->name_table
;
907 if (IS_NOT_NULL(t
)) xfree(t
);
908 reg
->name_table
= NULL
;
913 name_find(regex_t
* reg
, UChar
* name
, UChar
* name_end
)
917 NameTable
* t
= (NameTable
* )reg
->name_table
;
919 if (IS_NOT_NULL(t
)) {
920 len
= name_end
- name
;
921 for (i
= 0; i
< t
->num
; i
++) {
923 if (len
== e
->name_len
&& onig_strncmp(name
, e
->name
, len
) == 0)
927 return (NameEntry
* )NULL
;
931 onig_foreach_name(regex_t
* reg
,
932 int (*func
)(const UChar
*, const UChar
*,int,int*,regex_t
*,void*), void* arg
)
936 NameTable
* t
= (NameTable
* )reg
->name_table
;
938 if (IS_NOT_NULL(t
)) {
939 for (i
= 0; i
< t
->num
; i
++) {
941 r
= (*func
)(e
->name
, e
->name
+ e
->name_len
, e
->back_num
,
942 (e
->back_num
> 1 ? e
->back_refs
: &(e
->back_ref1
)),
944 if (r
!= 0) return r
;
951 onig_number_of_names(regex_t
* reg
)
953 NameTable
* t
= (NameTable
* )reg
->name_table
;
961 #endif /* else USE_ST_LIBRARY */
964 name_add(regex_t
* reg
, UChar
* name
, UChar
* name_end
, int backref
, ScanEnv
* env
)
969 NameTable
* t
= (NameTable
* )reg
->name_table
;
971 if (name_end
- name
<= 0)
972 return ONIGERR_EMPTY_GROUP_NAME
;
974 e
= name_find(reg
, name
, name_end
);
976 #ifdef USE_ST_LIBRARY
978 t
= onig_st_init_strend_table_with_size(INIT_NAMES_ALLOC_NUM
);
979 CHECK_NULL_RETURN_MEMERR(t
);
980 reg
->name_table
= (void* )t
;
982 e
= (NameEntry
* )xmalloc(sizeof(NameEntry
));
983 CHECK_NULL_RETURN_MEMERR(e
);
985 e
->name
= onigenc_strdup(reg
->enc
, name
, name_end
);
986 if (IS_NULL(e
->name
)) {
987 xfree(e
); return ONIGERR_MEMORY
;
989 r
= onig_st_insert_strend(t
, e
->name
, (e
->name
+ (name_end
- name
)),
993 e
->name_len
= (int )(name_end
- name
);
996 e
->back_refs
= (int* )NULL
;
1001 alloc
= INIT_NAMES_ALLOC_NUM
;
1002 t
= (NameTable
* )xmalloc(sizeof(NameTable
));
1003 CHECK_NULL_RETURN_MEMERR(t
);
1008 t
->e
= (NameEntry
* )xmalloc(sizeof(NameEntry
) * alloc
);
1009 if (IS_NULL(t
->e
)) {
1011 return ONIGERR_MEMORY
;
1014 reg
->name_table
= t
;
1017 else if (t
->num
== t
->alloc
) {
1020 alloc
= t
->alloc
* 2;
1021 t
->e
= (NameEntry
* )xrealloc(t
->e
, sizeof(NameEntry
) * alloc
, sizeof(NameEntry
) * t
->alloc
);
1022 CHECK_NULL_RETURN_MEMERR(t
->e
);
1026 for (i
= t
->num
; i
< t
->alloc
; i
++) {
1027 t
->e
[i
].name
= NULL
;
1028 t
->e
[i
].name_len
= 0;
1029 t
->e
[i
].back_num
= 0;
1030 t
->e
[i
].back_alloc
= 0;
1031 t
->e
[i
].back_refs
= (int* )NULL
;
1034 e
= &(t
->e
[t
->num
]);
1036 e
->name
= onigenc_strdup(reg
->enc
, name
, name_end
);
1037 if (IS_NULL(e
->name
)) return ONIGERR_MEMORY
;
1038 e
->name_len
= name_end
- name
;
1042 if (e
->back_num
>= 1 &&
1043 ! IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_ALLOW_MULTIPLEX_DEFINITION_NAME
)) {
1044 onig_scan_env_set_error_string(env
, ONIGERR_MULTIPLEX_DEFINED_NAME
,
1046 return ONIGERR_MULTIPLEX_DEFINED_NAME
;
1050 if (e
->back_num
== 1) {
1051 e
->back_ref1
= backref
;
1054 if (e
->back_num
== 2) {
1055 alloc
= INIT_NAME_BACKREFS_ALLOC_NUM
;
1056 e
->back_refs
= (int* )xmalloc(sizeof(int) * alloc
);
1057 CHECK_NULL_RETURN_MEMERR(e
->back_refs
);
1058 e
->back_alloc
= alloc
;
1059 e
->back_refs
[0] = e
->back_ref1
;
1060 e
->back_refs
[1] = backref
;
1063 if (e
->back_num
> e
->back_alloc
) {
1064 alloc
= e
->back_alloc
* 2;
1065 e
->back_refs
= (int* )xrealloc(e
->back_refs
, sizeof(int) * alloc
, sizeof(int) * e
->back_alloc
);
1066 CHECK_NULL_RETURN_MEMERR(e
->back_refs
);
1067 e
->back_alloc
= alloc
;
1069 e
->back_refs
[e
->back_num
- 1] = backref
;
1077 onig_name_to_group_numbers(regex_t
* reg
, const UChar
* name
,
1078 const UChar
* name_end
, int** nums
)
1080 NameEntry
* e
= name_find(reg
, name
, name_end
);
1082 if (IS_NULL(e
)) return ONIGERR_UNDEFINED_NAME_REFERENCE
;
1084 switch (e
->back_num
) {
1088 *nums
= &(e
->back_ref1
);
1091 *nums
= e
->back_refs
;
1098 name_to_group_numbers(ScanEnv
* env
, const UChar
* name
, const UChar
* name_end
,
1105 e
= name_find(reg
, name
, name_end
);
1108 onig_scan_env_set_error_string(env
, ONIGERR_UNDEFINED_NAME_REFERENCE
,
1109 (UChar
* )name
, (UChar
* )name_end
);
1110 return ONIGERR_UNDEFINED_NAME_REFERENCE
;
1113 switch (e
->back_num
) {
1117 *nums
= &(e
->back_ref1
);
1120 *nums
= e
->back_refs
;
1127 onig_name_to_backref_number(regex_t
* reg
, const UChar
* name
,
1128 const UChar
* name_end
, OnigRegion
*region
)
1132 n
= onig_name_to_group_numbers(reg
, name
, name_end
, &nums
);
1136 return ONIGERR_PARSER_BUG
;
1140 if (IS_NOT_NULL(region
)) {
1141 for (i
= n
- 1; i
>= 0; i
--) {
1142 if (region
->beg
[nums
[i
]] != ONIG_REGION_NOTPOS
)
1151 onig_noname_group_capture_is_active(regex_t
* reg
)
1153 if (ONIG_IS_OPTION_ON(reg
->options
, ONIG_OPTION_DONT_CAPTURE_GROUP
))
1156 if (onig_number_of_names(reg
) > 0 &&
1157 IS_SYNTAX_BV(reg
->syntax
, ONIG_SYN_CAPTURE_ONLY_NAMED_GROUP
) &&
1158 !ONIG_IS_OPTION_ON(reg
->options
, ONIG_OPTION_CAPTURE_GROUP
)) {
1168 OnigCalloutType type
;
1170 OnigCalloutFunc start_func
;
1171 OnigCalloutFunc end_func
;
1174 unsigned int arg_types
[ONIG_CALLOUT_MAX_ARGS_NUM
];
1175 OnigValue opt_defaults
[ONIG_CALLOUT_MAX_ARGS_NUM
];
1176 UChar
* name
; /* reference to GlobalCalloutNameTable entry: e->name */
1177 } CalloutNameListEntry
;
1182 CalloutNameListEntry
* v
;
1183 } CalloutNameListType
;
1185 static CalloutNameListType
* GlobalCalloutNameList
;
1188 make_callout_func_list(CalloutNameListType
** rs
, int init_size
)
1190 CalloutNameListType
* s
;
1191 CalloutNameListEntry
* v
;
1195 s
= xmalloc(sizeof(*s
));
1196 if (IS_NULL(s
)) return ONIGERR_MEMORY
;
1198 v
= (CalloutNameListEntry
* )xmalloc(sizeof(CalloutNameListEntry
) * init_size
);
1201 return ONIGERR_MEMORY
;
1205 s
->alloc
= init_size
;
1213 free_callout_func_list(CalloutNameListType
* s
)
1215 if (IS_NOT_NULL(s
)) {
1216 if (IS_NOT_NULL(s
->v
)) {
1219 for (i
= 0; i
< s
->n
; i
++) {
1220 CalloutNameListEntry
* e
= s
->v
+ i
;
1221 for (j
= e
->arg_num
- e
->opt_arg_num
; j
< e
->arg_num
; j
++) {
1222 if (e
->arg_types
[j
] == ONIG_TYPE_STRING
) {
1223 UChar
* p
= e
->opt_defaults
[j
].s
.start
;
1224 if (IS_NOT_NULL(p
)) xfree(p
);
1235 callout_func_list_add(CalloutNameListType
* s
, int* rid
)
1237 if (s
->n
>= s
->alloc
) {
1238 int new_size
= s
->alloc
* 2;
1239 CalloutNameListEntry
* nv
= (CalloutNameListEntry
* )
1240 xrealloc(s
->v
, sizeof(CalloutNameListEntry
) * new_size
, sizeof(CalloutNameListEntry
)*s
->alloc
);
1241 if (IS_NULL(nv
)) return ONIGERR_MEMORY
;
1243 s
->alloc
= new_size
;
1249 xmemset(&(s
->v
[s
->n
]), 0, sizeof(*(s
->v
)));
1257 int name_len
; /* byte length */
1261 #ifdef USE_ST_LIBRARY
1262 typedef st_table CalloutNameTable
;
1265 CalloutNameEntry
* e
;
1271 static CalloutNameTable
* GlobalCalloutNameTable
;
1272 static int CalloutNameIDCounter
;
1274 #ifdef USE_ST_LIBRARY
1277 i_free_callout_name_entry(st_callout_name_key
* key
, CalloutNameEntry
* e
,
1278 void* arg ARG_UNUSED
)
1281 /*xfree(key->s); */ /* is same as e->name */
1288 callout_name_table_clear(CalloutNameTable
* t
)
1290 if (IS_NOT_NULL(t
)) {
1291 onig_st_foreach(t
, i_free_callout_name_entry
, 0);
1297 global_callout_name_table_free(void)
1299 if (IS_NOT_NULL(GlobalCalloutNameTable
)) {
1300 int r
= callout_name_table_clear(GlobalCalloutNameTable
);
1301 if (r
!= 0) return r
;
1303 onig_st_free_table(GlobalCalloutNameTable
);
1304 GlobalCalloutNameTable
= 0;
1305 CalloutNameIDCounter
= 0;
1311 static CalloutNameEntry
*
1312 callout_name_find(OnigEncoding enc
, int is_not_single
,
1313 const UChar
* name
, const UChar
* name_end
)
1316 CalloutNameEntry
* e
;
1317 CalloutNameTable
* t
= GlobalCalloutNameTable
;
1319 e
= (CalloutNameEntry
* )NULL
;
1320 if (IS_NOT_NULL(t
)) {
1321 r
= onig_st_lookup_callout_name_table(t
, enc
, is_not_single
, name
, name_end
,
1322 (HashDataType
* )((void* )(&e
)));
1323 if (r
== 0) { /* not found */
1324 if (enc
!= ONIG_ENCODING_ASCII
&&
1325 ONIGENC_IS_ASCII_COMPATIBLE_ENCODING(enc
)) {
1326 enc
= ONIG_ENCODING_ASCII
;
1327 onig_st_lookup_callout_name_table(t
, enc
, is_not_single
, name
, name_end
,
1328 (HashDataType
* )((void* )(&e
)));
1338 callout_name_table_clear(CalloutNameTable
* t
)
1341 CalloutNameEntry
* e
;
1343 if (IS_NOT_NULL(t
)) {
1344 for (i
= 0; i
< t
->num
; i
++) {
1346 if (IS_NOT_NULL(e
->name
)) {
1354 if (IS_NOT_NULL(t
->e
)) {
1364 global_callout_name_table_free(void)
1366 if (IS_NOT_NULL(GlobalCalloutNameTable
)) {
1367 int r
= callout_name_table_clear(GlobalCalloutNameTable
);
1368 if (r
!= 0) return r
;
1370 xfree(GlobalCalloutNameTable
);
1371 GlobalCalloutNameTable
= 0;
1372 CalloutNameIDCounter
= 0;
1377 static CalloutNameEntry
*
1378 callout_name_find(UChar
* name
, UChar
* name_end
)
1381 CalloutNameEntry
* e
;
1382 CalloutNameTable
* t
= Calloutnames
;
1384 if (IS_NOT_NULL(t
)) {
1385 len
= name_end
- name
;
1386 for (i
= 0; i
< t
->num
; i
++) {
1388 if (len
== e
->name_len
&& onig_strncmp(name
, e
->name
, len
) == 0)
1392 return (CalloutNameEntry
* )NULL
;
1397 /* name string must be single byte char string. */
1399 callout_name_entry(CalloutNameEntry
** rentry
, OnigEncoding enc
,
1400 int is_not_single
, UChar
* name
, UChar
* name_end
)
1403 CalloutNameEntry
* e
;
1404 CalloutNameTable
* t
= GlobalCalloutNameTable
;
1407 if (name_end
- name
<= 0)
1408 return ONIGERR_INVALID_CALLOUT_NAME
;
1410 e
= callout_name_find(enc
, is_not_single
, name
, name_end
);
1412 #ifdef USE_ST_LIBRARY
1414 t
= onig_st_init_callout_name_table_with_size(INIT_NAMES_ALLOC_NUM
);
1415 CHECK_NULL_RETURN_MEMERR(t
);
1416 GlobalCalloutNameTable
= t
;
1418 e
= (CalloutNameEntry
* )xmalloc(sizeof(CalloutNameEntry
));
1419 CHECK_NULL_RETURN_MEMERR(e
);
1421 e
->name
= onigenc_strdup(enc
, name
, name_end
);
1422 if (IS_NULL(e
->name
)) {
1423 xfree(e
); return ONIGERR_MEMORY
;
1426 r
= st_insert_callout_name_table(t
, enc
, is_not_single
,
1427 e
->name
, (e
->name
+ (name_end
- name
)),
1429 if (r
< 0) return r
;
1436 alloc
= INIT_NAMES_ALLOC_NUM
;
1437 t
= (CalloutNameTable
* )xmalloc(sizeof(CalloutNameTable
));
1438 CHECK_NULL_RETURN_MEMERR(t
);
1443 t
->e
= (CalloutNameEntry
* )xmalloc(sizeof(CalloutNameEntry
) * alloc
);
1444 if (IS_NULL(t
->e
)) {
1446 return ONIGERR_MEMORY
;
1449 GlobalCalloutNameTable
= t
;
1452 else if (t
->num
== t
->alloc
) {
1455 alloc
= t
->alloc
* 2;
1456 t
->e
= (CalloutNameEntry
* )xrealloc(t
->e
, sizeof(CalloutNameEntry
) * alloc
, sizeof(CalloutNameEntry
)*t
->alloc
);
1457 CHECK_NULL_RETURN_MEMERR(t
->e
);
1461 for (i
= t
->num
; i
< t
->alloc
; i
++) {
1462 t
->e
[i
].name
= NULL
;
1463 t
->e
[i
].name_len
= 0;
1467 e
= &(t
->e
[t
->num
]);
1469 e
->name
= onigenc_strdup(enc
, name
, name_end
);
1470 if (IS_NULL(e
->name
)) return ONIGERR_MEMORY
;
1473 CalloutNameIDCounter
++;
1474 e
->id
= CalloutNameIDCounter
;
1475 e
->name_len
= (int )(name_end
- name
);
1483 is_allowed_callout_name(OnigEncoding enc
, UChar
* name
, UChar
* name_end
)
1488 if (name
>= name_end
) return 0;
1491 while (p
< name_end
) {
1492 c
= ONIGENC_MBC_TO_CODE(enc
, p
, name_end
);
1493 if (! IS_ALLOWED_CODE_IN_CALLOUT_NAME(c
))
1497 if (c
>= '0' && c
<= '9') return 0;
1500 p
+= ONIGENC_MBC_ENC_LEN(enc
, p
);
1507 is_allowed_callout_tag_name(OnigEncoding enc
, UChar
* name
, UChar
* name_end
)
1512 if (name
>= name_end
) return 0;
1515 while (p
< name_end
) {
1516 c
= ONIGENC_MBC_TO_CODE(enc
, p
, name_end
);
1517 if (! IS_ALLOWED_CODE_IN_CALLOUT_TAG_NAME(c
))
1521 if (c
>= '0' && c
<= '9') return 0;
1524 p
+= ONIGENC_MBC_ENC_LEN(enc
, p
);
1531 onig_set_callout_of_name(OnigEncoding enc
, OnigCalloutType callout_type
,
1532 UChar
* name
, UChar
* name_end
, int in
,
1533 OnigCalloutFunc start_func
,
1534 OnigCalloutFunc end_func
,
1535 int arg_num
, unsigned int arg_types
[],
1536 int opt_arg_num
, OnigValue opt_defaults
[])
1543 CalloutNameEntry
* e
;
1544 CalloutNameListEntry
* fe
;
1546 if (callout_type
!= ONIG_CALLOUT_TYPE_SINGLE
)
1547 return ONIGERR_INVALID_ARGUMENT
;
1549 if (arg_num
< 0 || arg_num
> ONIG_CALLOUT_MAX_ARGS_NUM
)
1550 return ONIGERR_INVALID_CALLOUT_ARG
;
1552 if (opt_arg_num
< 0 || opt_arg_num
> arg_num
)
1553 return ONIGERR_INVALID_CALLOUT_ARG
;
1555 if (start_func
== 0 && end_func
== 0)
1556 return ONIGERR_INVALID_CALLOUT_ARG
;
1558 if ((in
& ONIG_CALLOUT_IN_PROGRESS
) == 0 && (in
& ONIG_CALLOUT_IN_RETRACTION
) == 0)
1559 return ONIGERR_INVALID_CALLOUT_ARG
;
1561 for (i
= 0; i
< arg_num
; i
++) {
1562 unsigned int t
= arg_types
[i
];
1563 if (t
== ONIG_TYPE_VOID
)
1564 return ONIGERR_INVALID_CALLOUT_ARG
;
1566 if (i
>= arg_num
- opt_arg_num
) {
1567 if (t
!= ONIG_TYPE_LONG
&& t
!= ONIG_TYPE_CHAR
&& t
!= ONIG_TYPE_STRING
&&
1569 return ONIGERR_INVALID_CALLOUT_ARG
;
1572 if (t
!= ONIG_TYPE_LONG
) {
1573 t
= t
& ~ONIG_TYPE_LONG
;
1574 if (t
!= ONIG_TYPE_CHAR
&& t
!= ONIG_TYPE_STRING
&& t
!= ONIG_TYPE_TAG
)
1575 return ONIGERR_INVALID_CALLOUT_ARG
;
1581 if (! is_allowed_callout_name(enc
, name
, name_end
)) {
1582 return ONIGERR_INVALID_CALLOUT_NAME
;
1585 is_not_single
= (callout_type
!= ONIG_CALLOUT_TYPE_SINGLE
);
1586 id
= callout_name_entry(&e
, enc
, is_not_single
, name
, name_end
);
1587 if (id
< 0) return id
;
1590 if (IS_NULL(GlobalCalloutNameList
)) {
1591 r
= make_callout_func_list(&GlobalCalloutNameList
, 10);
1592 if (r
!= ONIG_NORMAL
) return r
;
1595 while (id
>= GlobalCalloutNameList
->n
) {
1597 r
= callout_func_list_add(GlobalCalloutNameList
, &rid
);
1598 if (r
!= ONIG_NORMAL
) return r
;
1601 fe
= GlobalCalloutNameList
->v
+ id
;
1602 fe
->type
= callout_type
;
1604 fe
->start_func
= start_func
;
1605 fe
->end_func
= end_func
;
1606 fe
->arg_num
= arg_num
;
1607 fe
->opt_arg_num
= opt_arg_num
;
1610 for (i
= 0; i
< arg_num
; i
++) {
1611 fe
->arg_types
[i
] = arg_types
[i
];
1613 for (i
= arg_num
- opt_arg_num
, j
= 0; i
< arg_num
; i
++, j
++) {
1614 if(IS_NULL(opt_defaults
))return ONIGERR_INVALID_ARGUMENT
;
1615 if (fe
->arg_types
[i
] == ONIG_TYPE_STRING
) {
1619 if (IS_NULL(opt_defaults
)) return ONIGERR_INVALID_ARGUMENT
;
1621 val
= opt_defaults
+ j
;
1622 ds
= onigenc_strdup(enc
, val
->s
.start
, val
->s
.end
);
1623 CHECK_NULL_RETURN_MEMERR(ds
);
1625 fe
->opt_defaults
[i
].s
.start
= ds
;
1626 fe
->opt_defaults
[i
].s
.end
= ds
+ (val
->s
.end
- val
->s
.start
);
1629 fe
->opt_defaults
[i
] = opt_defaults
[j
];
1638 get_callout_name_id_by_name(OnigEncoding enc
, int is_not_single
,
1639 UChar
* name
, UChar
* name_end
, int* rid
)
1642 CalloutNameEntry
* e
;
1644 if (! is_allowed_callout_name(enc
, name
, name_end
)) {
1645 return ONIGERR_INVALID_CALLOUT_NAME
;
1648 e
= callout_name_find(enc
, is_not_single
, name
, name_end
);
1650 return ONIGERR_UNDEFINED_CALLOUT_NAME
;
1659 extern OnigCalloutFunc
1660 onig_get_callout_start_func(regex_t
* reg
, int callout_num
)
1662 /* If used for callouts of contents, return 0. */
1663 CalloutListEntry
* e
;
1665 e
= onig_reg_callout_list_at(reg
, callout_num
);
1666 CHECK_NULL_RETURN(e
);
1667 return e
->start_func
;
1671 onig_get_callout_tag_start(regex_t
* reg
, int callout_num
)
1673 CalloutListEntry
* e
= onig_reg_callout_list_at(reg
, callout_num
);
1674 CHECK_NULL_RETURN(e
);
1675 return e
->tag_start
;
1679 onig_get_callout_tag_end(regex_t
* reg
, int callout_num
)
1681 CalloutListEntry
* e
= onig_reg_callout_list_at(reg
, callout_num
);
1682 CHECK_NULL_RETURN(e
);
1687 extern OnigCalloutType
1688 onig_get_callout_type_by_name_id(int name_id
)
1690 if (name_id
< 0 || name_id
>= GlobalCalloutNameList
->n
)
1693 return GlobalCalloutNameList
->v
[name_id
].type
;
1696 extern OnigCalloutFunc
1697 onig_get_callout_start_func_by_name_id(int name_id
)
1699 if (name_id
< 0 || name_id
>= GlobalCalloutNameList
->n
)
1702 return GlobalCalloutNameList
->v
[name_id
].start_func
;
1705 extern OnigCalloutFunc
1706 onig_get_callout_end_func_by_name_id(int name_id
)
1708 if (name_id
< 0 || name_id
>= GlobalCalloutNameList
->n
)
1711 return GlobalCalloutNameList
->v
[name_id
].end_func
;
1715 onig_get_callout_in_by_name_id(int name_id
)
1717 if (name_id
< 0 || name_id
>= GlobalCalloutNameList
->n
)
1720 return GlobalCalloutNameList
->v
[name_id
].in
;
1724 get_callout_arg_num_by_name_id(int name_id
)
1726 return GlobalCalloutNameList
->v
[name_id
].arg_num
;
1730 get_callout_opt_arg_num_by_name_id(int name_id
)
1732 return GlobalCalloutNameList
->v
[name_id
].opt_arg_num
;
1736 get_callout_arg_type_by_name_id(int name_id
, int index
)
1738 return GlobalCalloutNameList
->v
[name_id
].arg_types
[index
];
1742 get_callout_opt_default_by_name_id(int name_id
, int index
)
1744 return GlobalCalloutNameList
->v
[name_id
].opt_defaults
[index
];
1748 onig_get_callout_name_by_name_id(int name_id
)
1750 if (name_id
< 0 || name_id
>= GlobalCalloutNameList
->n
)
1753 return GlobalCalloutNameList
->v
[name_id
].name
;
1757 onig_global_callout_names_free(void)
1759 free_callout_func_list(GlobalCalloutNameList
);
1760 GlobalCalloutNameList
= 0;
1762 global_callout_name_table_free();
1767 typedef st_table CalloutTagTable
;
1768 typedef intptr_t CalloutTagVal
;
1770 #define CALLOUT_TAG_LIST_FLAG_TAG_EXIST (1<<0)
1773 i_callout_callout_list_set(UChar
* key
, CalloutTagVal e
, void* arg
)
1776 RegexExt
* ext
= (RegexExt
* )arg
;
1779 ext
->callout_list
[num
].flag
|= CALLOUT_TAG_LIST_FLAG_TAG_EXIST
;
1784 setup_ext_callout_list_values(regex_t
* reg
)
1790 if (IS_NOT_NULL(ext
->tag_table
)) {
1791 onig_st_foreach((CalloutTagTable
*)ext
->tag_table
, i_callout_callout_list_set
,
1795 for (i
= 0; i
< ext
->callout_num
; i
++) {
1796 CalloutListEntry
* e
= ext
->callout_list
+ i
;
1797 if (e
->of
== ONIG_CALLOUT_OF_NAME
) {
1798 for (j
= 0; j
< e
->u
.arg
.num
; j
++) {
1799 if (e
->u
.arg
.types
[j
] == ONIG_TYPE_TAG
) {
1803 start
= e
->u
.arg
.vals
[j
].s
.start
;
1804 end
= e
->u
.arg
.vals
[j
].s
.end
;
1805 num
= onig_get_callout_num_by_tag(reg
, start
, end
);
1806 if (num
< 0) return num
;
1807 e
->u
.arg
.vals
[j
].tag
= num
;
1817 onig_callout_tag_is_exist_at_callout_num(regex_t
* reg
, int callout_num
)
1819 RegexExt
* ext
= reg
->extp
;
1821 if (IS_NULL(ext
) || IS_NULL(ext
->callout_list
)) return 0;
1822 if (callout_num
> ext
->callout_num
) return 0;
1824 return (ext
->callout_list
[callout_num
].flag
&
1825 CALLOUT_TAG_LIST_FLAG_TAG_EXIST
) != 0;
1829 i_free_callout_tag_entry(UChar
* key
, CalloutTagVal e
, void* arg ARG_UNUSED
)
1836 callout_tag_table_clear(CalloutTagTable
* t
)
1838 if (IS_NOT_NULL(t
)) {
1839 onig_st_foreach(t
, i_free_callout_tag_entry
, 0);
1845 onig_callout_tag_table_free(void* table
)
1847 CalloutTagTable
* t
= (CalloutTagTable
* )table
;
1849 if (IS_NOT_NULL(t
)) {
1850 int r
= callout_tag_table_clear(t
);
1851 if (r
!= 0) return r
;
1853 onig_st_free_table(t
);
1860 onig_get_callout_num_by_tag(regex_t
* reg
,
1861 const UChar
* tag
, const UChar
* tag_end
)
1868 if (IS_NULL(ext
) || IS_NULL(ext
->tag_table
))
1869 return ONIGERR_INVALID_CALLOUT_TAG_NAME
;
1871 r
= onig_st_lookup_strend(ext
->tag_table
, tag
, tag_end
,
1872 (HashDataType
* )((void* )(&e
)));
1873 if (r
== 0) return ONIGERR_INVALID_CALLOUT_TAG_NAME
;
1877 static CalloutTagVal
1878 callout_tag_find(CalloutTagTable
* t
, const UChar
* name
, const UChar
* name_end
)
1883 if (IS_NOT_NULL(t
)) {
1884 onig_st_lookup_strend(t
, name
, name_end
, (HashDataType
* )((void* )(&e
)));
1890 callout_tag_table_new(CalloutTagTable
** rt
)
1895 t
= onig_st_init_strend_table_with_size(INIT_TAG_NAMES_ALLOC_NUM
);
1896 CHECK_NULL_RETURN_MEMERR(t
);
1903 callout_tag_entry_raw(ScanEnv
* env
, CalloutTagTable
* t
, UChar
* name
,
1904 UChar
* name_end
, CalloutTagVal entry_val
)
1909 if (name_end
- name
<= 0)
1910 return ONIGERR_INVALID_CALLOUT_TAG_NAME
;
1912 val
= callout_tag_find(t
, name
, name_end
);
1914 onig_scan_env_set_error_string(env
, ONIGERR_MULTIPLEX_DEFINED_NAME
,
1916 return ONIGERR_MULTIPLEX_DEFINED_NAME
;
1919 r
= onig_st_insert_strend(t
, name
, name_end
, (HashDataType
)entry_val
);
1920 if (r
< 0) return r
;
1926 ext_ensure_tag_table(regex_t
* reg
)
1932 ext
= onig_get_regex_ext(reg
);
1933 CHECK_NULL_RETURN_MEMERR(ext
);
1935 if (IS_NULL(ext
->tag_table
)) {
1936 r
= callout_tag_table_new(&t
);
1937 if (r
!= ONIG_NORMAL
) return r
;
1946 callout_tag_entry(ScanEnv
* env
, regex_t
* reg
, UChar
* name
, UChar
* name_end
,
1947 CalloutTagVal entry_val
)
1951 CalloutListEntry
* e
;
1953 r
= ext_ensure_tag_table(reg
);
1954 if (r
!= ONIG_NORMAL
) return r
;
1956 ext
= onig_get_regex_ext(reg
);
1957 CHECK_NULL_RETURN_MEMERR(ext
);
1958 r
= callout_tag_entry_raw(env
, ext
->tag_table
, name
, name_end
, entry_val
);
1960 e
= onig_reg_callout_list_at(reg
, (int )entry_val
);
1961 CHECK_NULL_RETURN_MEMERR(e
);
1962 e
->tag_start
= name
;
1963 e
->tag_end
= name_end
;
1968 #endif /* USE_CALLOUT */
1971 #define INIT_SCANENV_MEMENV_ALLOC_SIZE 16
1974 scan_env_clear(ScanEnv
* env
)
1976 MEM_STATUS_CLEAR(env
->capture_history
);
1977 MEM_STATUS_CLEAR(env
->bt_mem_start
);
1978 MEM_STATUS_CLEAR(env
->bt_mem_end
);
1979 MEM_STATUS_CLEAR(env
->backrefed_mem
);
1980 env
->error
= (UChar
* )NULL
;
1981 env
->error_end
= (UChar
* )NULL
;
1985 env
->unset_addr_list
= NULL
;
1986 env
->has_call_zero
= 0;
1992 env
->mem_env_dynamic
= (MemEnv
* )NULL
;
1994 xmemset(env
->mem_env_static
, 0, sizeof(env
->mem_env_static
));
1996 env
->parse_depth
= 0;
1999 env
->save_alloc_num
= 0;
2004 scan_env_add_mem_entry(ScanEnv
* env
)
2009 need
= env
->num_mem
+ 1;
2010 if (need
> MaxCaptureNum
&& MaxCaptureNum
!= 0)
2011 return ONIGERR_TOO_MANY_CAPTURES
;
2013 if (need
>= SCANENV_MEMENV_SIZE
) {
2014 if (env
->mem_alloc
<= need
) {
2015 if (IS_NULL(env
->mem_env_dynamic
)) {
2016 alloc
= INIT_SCANENV_MEMENV_ALLOC_SIZE
;
2017 p
= (MemEnv
* )xmalloc(sizeof(MemEnv
) * alloc
);
2018 CHECK_NULL_RETURN_MEMERR(p
);
2019 xmemcpy(p
, env
->mem_env_static
, sizeof(env
->mem_env_static
));
2022 alloc
= env
->mem_alloc
* 2;
2023 p
= (MemEnv
* )xrealloc(env
->mem_env_dynamic
, sizeof(MemEnv
) * alloc
, sizeof(MemEnv
)*env
->mem_alloc
);
2024 CHECK_NULL_RETURN_MEMERR(p
);
2027 for (i
= env
->num_mem
+ 1; i
< alloc
; i
++) {
2028 p
[i
].node
= NULL_NODE
;
2035 env
->mem_env_dynamic
= p
;
2036 env
->mem_alloc
= alloc
;
2041 return env
->num_mem
;
2045 scan_env_set_mem_node(ScanEnv
* env
, int num
, Node
* node
)
2047 if (env
->num_mem
>= num
)
2048 SCANENV_MEMENV(env
)[num
].node
= node
;
2050 return ONIGERR_PARSER_BUG
;
2055 onig_node_free(Node
* node
)
2058 if (IS_NULL(node
)) return ;
2060 #ifdef DEBUG_NODE_FREE
2061 fprintf(stderr
, "onig_node_free: %p\n", node
);
2064 switch (NODE_TYPE(node
)) {
2066 if (STR_(node
)->capacity
!= 0 &&
2067 IS_NOT_NULL(STR_(node
)->s
) && STR_(node
)->s
!= STR_(node
)->buf
) {
2068 xfree(STR_(node
)->s
);
2074 onig_node_free(NODE_CAR(node
));
2076 Node
* next_node
= NODE_CDR(node
);
2086 CClassNode
* cc
= CCLASS_(node
);
2089 bbuf_free(cc
->mbuf
);
2094 if (IS_NOT_NULL(BACKREF_(node
)->back_dynamic
))
2095 xfree(BACKREF_(node
)->back_dynamic
);
2099 if (NODE_BODY(node
))
2100 onig_node_free(NODE_BODY(node
));
2103 BagNode
* en
= BAG_(node
);
2104 if (en
->type
== BAG_IF_ELSE
) {
2105 onig_node_free(en
->te
.Then
);
2106 onig_node_free(en
->te
.Else
);
2113 if (NODE_BODY(node
))
2114 onig_node_free(NODE_BODY(node
));
2127 cons_node_free_alone(Node
* node
)
2131 onig_node_free(node
);
2139 node
= (Node
* )xmalloc(sizeof(Node
));
2140 CHECK_NULL_RETURN(node
);
2141 xmemset(node
, 0, sizeof(*node
));
2143 #ifdef DEBUG_NODE_FREE
2144 fprintf(stderr
, "node_new: %p\n", node
);
2151 initialize_cclass(CClassNode
* cc
)
2153 BITSET_CLEAR(cc
->bs
);
2159 node_new_cclass(void)
2161 Node
* node
= node_new();
2162 CHECK_NULL_RETURN(node
);
2164 NODE_SET_TYPE(node
, NODE_CCLASS
);
2165 initialize_cclass(CCLASS_(node
));
2170 node_new_ctype(int type
, int not, OnigOptionType options
)
2172 Node
* node
= node_new();
2173 CHECK_NULL_RETURN(node
);
2175 NODE_SET_TYPE(node
, NODE_CTYPE
);
2176 CTYPE_(node
)->ctype
= type
;
2177 CTYPE_(node
)->not = not;
2178 CTYPE_(node
)->options
= options
;
2179 CTYPE_(node
)->ascii_mode
= IS_ASCII_MODE_CTYPE_OPTION(type
, options
);
2184 node_new_anychar(void)
2186 Node
* node
= node_new_ctype(CTYPE_ANYCHAR
, 0, ONIG_OPTION_NONE
);
2191 node_new_anychar_with_fixed_option(OnigOptionType option
)
2196 node
= node_new_anychar();
2197 CHECK_NULL_RETURN(node
);
2200 ct
->options
= option
;
2201 NODE_STATUS_ADD(node
, FIXED_OPTION
);
2206 node_new_no_newline(Node
** node
, ScanEnv
* env
)
2210 n
= node_new_anychar_with_fixed_option(ONIG_OPTION_NONE
);
2211 CHECK_NULL_RETURN_MEMERR(n
);
2217 node_new_true_anychar(Node
** node
, ScanEnv
* env
)
2221 n
= node_new_anychar_with_fixed_option(ONIG_OPTION_MULTILINE
);
2222 CHECK_NULL_RETURN_MEMERR(n
);
2228 node_new_list(Node
* left
, Node
* right
)
2230 Node
* node
= node_new();
2231 CHECK_NULL_RETURN(node
);
2233 NODE_SET_TYPE(node
, NODE_LIST
);
2234 NODE_CAR(node
) = left
;
2235 NODE_CDR(node
) = right
;
2240 onig_node_new_list(Node
* left
, Node
* right
)
2242 return node_new_list(left
, right
);
2246 onig_node_list_add(Node
* list
, Node
* x
)
2250 n
= onig_node_new_list(x
, NULL
);
2251 if (IS_NULL(n
)) return NULL_NODE
;
2253 if (IS_NOT_NULL(list
)) {
2254 while (IS_NOT_NULL(NODE_CDR(list
)))
2255 list
= NODE_CDR(list
);
2264 onig_node_new_alt(Node
* left
, Node
* right
)
2266 Node
* node
= node_new();
2267 CHECK_NULL_RETURN(node
);
2269 NODE_SET_TYPE(node
, NODE_ALT
);
2270 NODE_CAR(node
) = left
;
2271 NODE_CDR(node
) = right
;
2276 make_list_or_alt(NodeType type
, int n
, Node
* ns
[])
2280 if (n
<= 0) return NULL_NODE
;
2284 CHECK_NULL_RETURN(r
);
2285 NODE_SET_TYPE(r
, type
);
2286 NODE_CAR(r
) = ns
[0];
2287 NODE_CDR(r
) = NULL_NODE
;
2293 CHECK_NULL_RETURN(r
);
2295 right
= make_list_or_alt(type
, n
- 1, ns
+ 1);
2296 if (IS_NULL(right
)) {
2301 NODE_SET_TYPE(r
, type
);
2302 NODE_CAR(r
) = ns
[0];
2303 NODE_CDR(r
) = right
;
2310 make_list(int n
, Node
* ns
[])
2312 return make_list_or_alt(NODE_LIST
, n
, ns
);
2316 make_alt(int n
, Node
* ns
[])
2318 return make_list_or_alt(NODE_ALT
, n
, ns
);
2322 onig_node_new_anchor(int type
, int ascii_mode
)
2324 Node
* node
= node_new();
2325 CHECK_NULL_RETURN(node
);
2327 NODE_SET_TYPE(node
, NODE_ANCHOR
);
2328 ANCHOR_(node
)->type
= type
;
2329 ANCHOR_(node
)->char_len
= -1;
2330 ANCHOR_(node
)->ascii_mode
= ascii_mode
;
2335 node_new_backref(int back_num
, int* backrefs
, int by_name
,
2336 #ifdef USE_BACKREF_WITH_LEVEL
2337 int exist_level
, int nest_level
,
2342 Node
* node
= node_new();
2344 CHECK_NULL_RETURN(node
);
2346 NODE_SET_TYPE(node
, NODE_BACKREF
);
2347 BACKREF_(node
)->back_num
= back_num
;
2348 BACKREF_(node
)->back_dynamic
= (int* )NULL
;
2350 NODE_STATUS_ADD(node
, BY_NAME
);
2352 #ifdef USE_BACKREF_WITH_LEVEL
2353 if (exist_level
!= 0) {
2354 NODE_STATUS_ADD(node
, NEST_LEVEL
);
2355 BACKREF_(node
)->nest_level
= nest_level
;
2359 for (i
= 0; i
< back_num
; i
++) {
2360 if (backrefs
[i
] <= env
->num_mem
&&
2361 IS_NULL(SCANENV_MEMENV(env
)[backrefs
[i
]].node
)) {
2362 NODE_STATUS_ADD(node
, RECURSION
); /* /...(\1).../ */
2367 if (back_num
<= NODE_BACKREFS_SIZE
) {
2368 for (i
= 0; i
< back_num
; i
++)
2369 BACKREF_(node
)->back_static
[i
] = backrefs
[i
];
2372 int* p
= (int* )xmalloc(sizeof(int) * back_num
);
2374 onig_node_free(node
);
2377 BACKREF_(node
)->back_dynamic
= p
;
2378 for (i
= 0; i
< back_num
; i
++)
2385 node_new_backref_checker(int back_num
, int* backrefs
, int by_name
,
2386 #ifdef USE_BACKREF_WITH_LEVEL
2387 int exist_level
, int nest_level
,
2393 node
= node_new_backref(back_num
, backrefs
, by_name
,
2394 #ifdef USE_BACKREF_WITH_LEVEL
2395 exist_level
, nest_level
,
2398 CHECK_NULL_RETURN(node
);
2400 NODE_STATUS_ADD(node
, CHECKER
);
2406 node_new_call(UChar
* name
, UChar
* name_end
, int gnum
, int by_number
)
2408 Node
* node
= node_new();
2409 CHECK_NULL_RETURN(node
);
2411 NODE_SET_TYPE(node
, NODE_CALL
);
2412 CALL_(node
)->by_number
= by_number
;
2413 CALL_(node
)->name
= name
;
2414 CALL_(node
)->name_end
= name_end
;
2415 CALL_(node
)->group_num
= gnum
;
2416 CALL_(node
)->entry_count
= 1;
2422 node_new_quantifier(int lower
, int upper
, int by_number
)
2424 Node
* node
= node_new();
2425 CHECK_NULL_RETURN(node
);
2427 NODE_SET_TYPE(node
, NODE_QUANT
);
2428 QUANT_(node
)->lower
= lower
;
2429 QUANT_(node
)->upper
= upper
;
2430 QUANT_(node
)->greedy
= 1;
2431 QUANT_(node
)->emptiness
= BODY_IS_NOT_EMPTY
;
2432 QUANT_(node
)->head_exact
= NULL_NODE
;
2433 QUANT_(node
)->next_head_exact
= NULL_NODE
;
2434 QUANT_(node
)->is_refered
= 0;
2436 NODE_STATUS_ADD(node
, BY_NUMBER
);
2442 node_new_bag(enum BagType type
)
2444 Node
* node
= node_new();
2445 CHECK_NULL_RETURN(node
);
2447 NODE_SET_TYPE(node
, NODE_BAG
);
2448 BAG_(node
)->type
= type
;
2452 BAG_(node
)->m
.regnum
= 0;
2453 BAG_(node
)->m
.called_addr
= -1;
2454 BAG_(node
)->m
.entry_count
= 1;
2455 BAG_(node
)->m
.called_state
= 0;
2459 BAG_(node
)->o
.options
= 0;
2462 case BAG_STOP_BACKTRACK
:
2466 BAG_(node
)->te
.Then
= 0;
2467 BAG_(node
)->te
.Else
= 0;
2471 BAG_(node
)->opt_count
= 0;
2476 onig_node_new_bag(enum BagType type
)
2478 return node_new_bag(type
);
2482 node_new_bag_if_else(Node
* cond
, Node
* Then
, Node
* Else
)
2485 n
= node_new_bag(BAG_IF_ELSE
);
2486 CHECK_NULL_RETURN(n
);
2488 NODE_BODY(n
) = cond
;
2489 BAG_(n
)->te
.Then
= Then
;
2490 BAG_(n
)->te
.Else
= Else
;
2495 node_new_memory(int is_named
)
2497 Node
* node
= node_new_bag(BAG_MEMORY
);
2498 CHECK_NULL_RETURN(node
);
2500 NODE_STATUS_ADD(node
, NAMED_GROUP
);
2506 node_new_option(OnigOptionType option
)
2508 Node
* node
= node_new_bag(BAG_OPTION
);
2509 CHECK_NULL_RETURN(node
);
2510 BAG_(node
)->o
.options
= option
;
2515 node_new_group(Node
* content
)
2520 CHECK_NULL_RETURN(node
);
2521 NODE_SET_TYPE(node
, NODE_LIST
);
2522 NODE_CAR(node
) = content
;
2523 NODE_CDR(node
) = NULL_NODE
;
2529 node_drop_group(Node
* group
)
2533 content
= NODE_CAR(group
);
2534 NODE_CAR(group
) = NULL_NODE
;
2535 onig_node_free(group
);
2540 node_new_fail(Node
** node
, ScanEnv
* env
)
2543 CHECK_NULL_RETURN_MEMERR(*node
);
2545 NODE_SET_TYPE(*node
, NODE_GIMMICK
);
2546 GIMMICK_(*node
)->type
= GIMMICK_FAIL
;
2551 node_new_save_gimmick(Node
** node
, enum SaveType save_type
, ScanEnv
* env
)
2556 r
= save_entry(env
, save_type
, &id
);
2557 if (r
!= ONIG_NORMAL
) return r
;
2560 CHECK_NULL_RETURN_MEMERR(*node
);
2562 NODE_SET_TYPE(*node
, NODE_GIMMICK
);
2563 GIMMICK_(*node
)->id
= id
;
2564 GIMMICK_(*node
)->type
= GIMMICK_SAVE
;
2565 GIMMICK_(*node
)->detail_type
= (int )save_type
;
2571 node_new_update_var_gimmick(Node
** node
, enum UpdateVarType update_var_type
,
2572 int id
, ScanEnv
* env
)
2575 CHECK_NULL_RETURN_MEMERR(*node
);
2577 NODE_SET_TYPE(*node
, NODE_GIMMICK
);
2578 GIMMICK_(*node
)->id
= id
;
2579 GIMMICK_(*node
)->type
= GIMMICK_UPDATE_VAR
;
2580 GIMMICK_(*node
)->detail_type
= (int )update_var_type
;
2586 node_new_keep(Node
** node
, ScanEnv
* env
)
2590 r
= node_new_save_gimmick(node
, SAVE_KEEP
, env
);
2591 if (r
!= 0) return r
;
2600 onig_free_reg_callout_list(int n
, CalloutListEntry
* list
)
2605 if (IS_NULL(list
)) return ;
2607 for (i
= 0; i
< n
; i
++) {
2608 if (list
[i
].of
== ONIG_CALLOUT_OF_NAME
) {
2609 for (j
= 0; j
< list
[i
].u
.arg
.passed_num
; j
++) {
2610 if (list
[i
].u
.arg
.types
[j
] == ONIG_TYPE_STRING
) {
2611 if (IS_NOT_NULL(list
[i
].u
.arg
.vals
[j
].s
.start
))
2612 xfree(list
[i
].u
.arg
.vals
[j
].s
.start
);
2616 else { /* ONIG_CALLOUT_OF_CONTENTS */
2617 if (IS_NOT_NULL(list
[i
].u
.content
.start
)) {
2618 xfree((void* )list
[i
].u
.content
.start
);
2626 extern CalloutListEntry
*
2627 onig_reg_callout_list_at(regex_t
* reg
, int num
)
2629 RegexExt
* ext
= reg
->extp
;
2630 CHECK_NULL_RETURN(ext
);
2632 if (num
<= 0 || num
> ext
->callout_num
)
2636 return ext
->callout_list
+ num
;
2640 reg_callout_list_entry(ScanEnv
* env
, int* rnum
)
2642 #define INIT_CALLOUT_LIST_NUM 3
2645 CalloutListEntry
* list
;
2646 CalloutListEntry
* e
;
2649 ext
= onig_get_regex_ext(env
->reg
);
2650 CHECK_NULL_RETURN_MEMERR(ext
);
2652 if (IS_NULL(ext
->callout_list
)) {
2653 list
= (CalloutListEntry
* )xmalloc(sizeof(*list
) * INIT_CALLOUT_LIST_NUM
);
2654 CHECK_NULL_RETURN_MEMERR(list
);
2656 ext
->callout_list
= list
;
2657 ext
->callout_list_alloc
= INIT_CALLOUT_LIST_NUM
;
2658 ext
->callout_num
= 0;
2661 num
= ext
->callout_num
+ 1;
2662 if (num
> ext
->callout_list_alloc
) {
2663 int alloc
= ext
->callout_list_alloc
* 2;
2664 list
= (CalloutListEntry
* )xrealloc(ext
->callout_list
,
2665 sizeof(CalloutListEntry
) * alloc
,
2666 sizeof(CalloutListEntry
) * ext
->callout_list_alloc
);
2667 CHECK_NULL_RETURN_MEMERR(list
);
2669 ext
->callout_list
= list
;
2670 ext
->callout_list_alloc
= alloc
;
2673 e
= ext
->callout_list
+ (num
- 1);
2677 e
->in
= ONIG_CALLOUT_OF_CONTENTS
;
2684 e
->u
.arg
.passed_num
= 0;
2686 ext
->callout_num
= num
;
2692 node_new_callout(Node
** node
, OnigCalloutOf callout_of
, int num
, int id
,
2696 CHECK_NULL_RETURN_MEMERR(*node
);
2698 NODE_SET_TYPE(*node
, NODE_GIMMICK
);
2699 GIMMICK_(*node
)->id
= id
;
2700 GIMMICK_(*node
)->num
= num
;
2701 GIMMICK_(*node
)->type
= GIMMICK_CALLOUT
;
2702 GIMMICK_(*node
)->detail_type
= (int )callout_of
;
2709 make_text_segment(Node
** node
, ScanEnv
* env
)
2716 /* \X == (?>\O(?:\Y\O)*) */
2721 ns
[0] = onig_node_new_anchor(ANCR_NO_TEXT_SEGMENT_BOUNDARY
, 0);
2722 if (IS_NULL(ns
[0])) goto err
;
2724 r
= node_new_true_anychar(&ns
[1], env
);
2725 if (r
!= 0) goto err1
;
2727 x
= make_list(2, ns
);
2728 if (IS_NULL(x
)) goto err
;
2732 x
= node_new_quantifier(0, INFINITE_REPEAT
, 1);
2733 if (IS_NULL(x
)) goto err
;
2735 NODE_BODY(x
) = ns
[0];
2739 r
= node_new_true_anychar(&ns
[0], env
);
2740 if (r
!= 0) goto err1
;
2742 x
= make_list(2, ns
);
2743 if (IS_NULL(x
)) goto err
;
2748 x
= node_new_bag(BAG_STOP_BACKTRACK
);
2749 if (IS_NULL(x
)) goto err
;
2751 NODE_BODY(x
) = ns
[0];
2759 for (i
= 0; i
< 2; i
++) onig_node_free(ns
[i
]);
2764 make_absent_engine(Node
** node
, int pre_save_right_id
, Node
* absent
,
2765 Node
* step_one
, int lower
, int upper
, int possessive
,
2766 int is_range_cutter
, ScanEnv
* env
)
2774 for (i
= 0; i
< 4; i
++) ns
[i
] = NULL_NODE
;
2777 ns
[3] = step_one
; /* for err */
2778 r
= node_new_save_gimmick(&ns
[0], SAVE_S
, env
);
2779 if (r
!= 0) goto err
;
2781 id
= GIMMICK_(ns
[0])->id
;
2782 r
= node_new_update_var_gimmick(&ns
[2], UPDATE_VAR_RIGHT_RANGE_FROM_S_STACK
,
2784 if (r
!= 0) goto err
;
2786 r
= node_new_fail(&ns
[3], env
);
2787 if (r
!= 0) goto err
;
2789 x
= make_list(4, ns
);
2790 if (IS_NULL(x
)) goto err0
;
2794 ns
[2] = ns
[3] = NULL_NODE
;
2796 x
= make_alt(2, ns
);
2797 if (IS_NULL(x
)) goto err0
;
2801 x
= node_new_quantifier(lower
, upper
, 0);
2802 if (IS_NULL(x
)) goto err0
;
2804 NODE_BODY(x
) = ns
[0];
2807 if (possessive
!= 0) {
2808 x
= node_new_bag(BAG_STOP_BACKTRACK
);
2809 if (IS_NULL(x
)) goto err0
;
2811 NODE_BODY(x
) = ns
[0];
2815 r
= node_new_update_var_gimmick(&ns
[1], UPDATE_VAR_RIGHT_RANGE_FROM_STACK
,
2816 pre_save_right_id
, env
);
2817 if (r
!= 0) goto err
;
2819 r
= node_new_fail(&ns
[2], env
);
2820 if (r
!= 0) goto err
;
2822 x
= make_list(2, ns
+ 1);
2823 if (IS_NULL(x
)) goto err0
;
2825 ns
[1] = x
; ns
[2] = NULL_NODE
;
2827 x
= make_alt(2, ns
);
2828 if (IS_NULL(x
)) goto err0
;
2830 if (is_range_cutter
!= 0)
2831 NODE_STATUS_ADD(x
, SUPER
);
2839 for (i
= 0; i
< 4; i
++) onig_node_free(ns
[i
]);
2844 make_absent_tail(Node
** node1
, Node
** node2
, int pre_save_right_id
,
2853 *node1
= *node2
= NULL_NODE
;
2854 save
= ns
[0] = ns
[1] = NULL_NODE
;
2856 r
= node_new_save_gimmick(&save
, SAVE_RIGHT_RANGE
, env
);
2857 if (r
!= 0) goto err
;
2859 id
= GIMMICK_(save
)->id
;
2860 r
= node_new_update_var_gimmick(&ns
[0], UPDATE_VAR_RIGHT_RANGE_FROM_STACK
,
2862 if (r
!= 0) goto err
;
2864 r
= node_new_fail(&ns
[1], env
);
2865 if (r
!= 0) goto err
;
2867 x
= make_list(2, ns
);
2868 if (IS_NULL(x
)) goto err0
;
2870 ns
[0] = NULL_NODE
; ns
[1] = x
;
2872 r
= node_new_update_var_gimmick(&ns
[0], UPDATE_VAR_RIGHT_RANGE_FROM_STACK
,
2873 pre_save_right_id
, env
);
2874 if (r
!= 0) goto err
;
2876 x
= make_alt(2, ns
);
2877 if (IS_NULL(x
)) goto err0
;
2886 onig_node_free(save
);
2887 onig_node_free(ns
[0]);
2888 onig_node_free(ns
[1]);
2893 make_range_clear(Node
** node
, ScanEnv
* env
)
2902 save
= ns
[0] = ns
[1] = NULL_NODE
;
2904 r
= node_new_save_gimmick(&save
, SAVE_RIGHT_RANGE
, env
);
2905 if (r
!= 0) goto err
;
2907 id
= GIMMICK_(save
)->id
;
2908 r
= node_new_update_var_gimmick(&ns
[0], UPDATE_VAR_RIGHT_RANGE_FROM_STACK
,
2910 if (r
!= 0) goto err
;
2912 r
= node_new_fail(&ns
[1], env
);
2913 if (r
!= 0) goto err
;
2915 x
= make_list(2, ns
);
2916 if (IS_NULL(x
)) goto err0
;
2918 ns
[0] = NULL_NODE
; ns
[1] = x
;
2920 r
= node_new_update_var_gimmick(&ns
[0], UPDATE_VAR_RIGHT_RANGE_INIT
, 0, env
);
2921 if (r
!= 0) goto err
;
2923 x
= make_alt(2, ns
);
2924 if (IS_NULL(x
)) goto err0
;
2926 NODE_STATUS_ADD(x
, SUPER
);
2931 x
= make_list(2, ns
);
2932 if (IS_NULL(x
)) goto err0
;
2940 onig_node_free(save
);
2941 onig_node_free(ns
[0]);
2942 onig_node_free(ns
[1]);
2947 is_simple_one_char_repeat(Node
* node
, Node
** rquant
, Node
** rbody
,
2948 int* is_possessive
, ScanEnv
* env
)
2953 *rquant
= *rbody
= 0;
2956 if (NODE_TYPE(node
) == NODE_QUANT
) {
2960 if (NODE_TYPE(node
) == NODE_BAG
) {
2961 BagNode
* en
= BAG_(node
);
2962 if (en
->type
== BAG_STOP_BACKTRACK
) {
2964 quant
= NODE_BAG_BODY(en
);
2965 if (NODE_TYPE(quant
) != NODE_QUANT
)
2975 if (QUANT_(quant
)->greedy
== 0)
2978 body
= NODE_BODY(quant
);
2979 switch (NODE_TYPE(body
)) {
2983 StrNode
* sn
= STR_(body
);
2987 while (s
< sn
->end
) {
2988 s
+= enclen(env
->enc
, s
);
3003 if (node
!= quant
) {
3004 NODE_BODY(node
) = 0;
3005 onig_node_free(node
);
3007 NODE_BODY(quant
) = NULL_NODE
;
3014 make_absent_tree_for_simple_one_char_repeat(Node
** node
, Node
* absent
, Node
* quant
,
3015 Node
* body
, int possessive
, ScanEnv
* env
)
3026 ns
[0] = ns
[1] = NULL_NODE
;
3027 ns
[2] = body
, ns
[3] = absent
;
3029 lower
= QUANT_(quant
)->lower
;
3030 upper
= QUANT_(quant
)->upper
;
3031 onig_node_free(quant
);
3033 r
= node_new_save_gimmick(&ns
[0], SAVE_RIGHT_RANGE
, env
);
3034 if (r
!= 0) goto err
;
3036 id1
= GIMMICK_(ns
[0])->id
;
3038 r
= make_absent_engine(&ns
[1], id1
, absent
, body
, lower
, upper
, possessive
,
3040 if (r
!= 0) goto err
;
3042 ns
[2] = ns
[3] = NULL_NODE
;
3044 r
= node_new_update_var_gimmick(&ns
[2], UPDATE_VAR_RIGHT_RANGE_FROM_STACK
,
3046 if (r
!= 0) goto err
;
3048 x
= make_list(3, ns
);
3049 if (IS_NULL(x
)) goto err0
;
3057 for (i
= 0; i
< 4; i
++) onig_node_free(ns
[i
]);
3062 make_absent_tree(Node
** node
, Node
* absent
, Node
* expr
, int is_range_cutter
,
3073 for (i
= 0; i
< 7; i
++) ns
[i
] = NULL_NODE
;
3074 ns
[4] = expr
; ns
[5] = absent
;
3076 if (is_range_cutter
== 0) {
3080 if (expr
== NULL_NODE
) {
3081 /* default expr \O* */
3082 quant
= node_new_quantifier(0, INFINITE_REPEAT
, 0);
3083 if (IS_NULL(quant
)) goto err0
;
3085 r
= node_new_true_anychar(&body
, env
);
3087 onig_node_free(quant
);
3094 if (is_simple_one_char_repeat(expr
, &quant
, &body
, &possessive
, env
)) {
3096 r
= make_absent_tree_for_simple_one_char_repeat(node
, absent
, quant
,
3097 body
, possessive
, env
);
3100 onig_node_free(quant
);
3101 onig_node_free(body
);
3110 r
= node_new_save_gimmick(&ns
[0], SAVE_RIGHT_RANGE
, env
);
3111 if (r
!= 0) goto err
;
3113 id1
= GIMMICK_(ns
[0])->id
;
3115 r
= node_new_save_gimmick(&ns
[1], SAVE_S
, env
);
3116 if (r
!= 0) goto err
;
3118 id2
= GIMMICK_(ns
[1])->id
;
3120 r
= node_new_true_anychar(&ns
[3], env
);
3121 if (r
!= 0) goto err
;
3124 r
= make_absent_engine(&ns
[2], id1
, absent
, ns
[3], 0, INFINITE_REPEAT
,
3125 possessive
, is_range_cutter
, env
);
3126 if (r
!= 0) goto err
;
3131 r
= node_new_update_var_gimmick(&ns
[3], UPDATE_VAR_S_FROM_STACK
, id2
, env
);
3132 if (r
!= 0) goto err
;
3134 if (is_range_cutter
!= 0) {
3135 x
= make_list(4, ns
);
3136 if (IS_NULL(x
)) goto err0
;
3139 r
= make_absent_tail(&ns
[5], &ns
[6], id1
, env
);
3140 if (r
!= 0) goto err
;
3142 x
= make_list(7, ns
);
3143 if (IS_NULL(x
)) goto err0
;
3152 for (i
= 0; i
< 7; i
++) onig_node_free(ns
[i
]);
3157 onig_node_str_cat(Node
* node
, const UChar
* s
, const UChar
* end
)
3159 int addlen
= (int )(end
- s
);
3162 int len
= (int )(STR_(node
)->end
- STR_(node
)->s
);
3164 if (STR_(node
)->capacity
> 0 || (len
+ addlen
> NODE_STRING_BUF_SIZE
- 1)) {
3166 int capa
= len
+ addlen
+ NODE_STRING_MARGIN
;
3168 if (capa
<= STR_(node
)->capacity
) {
3169 onig_strcpy(STR_(node
)->s
+ len
, s
, end
);
3172 if (STR_(node
)->s
== STR_(node
)->buf
)
3173 p
= strcat_capa_from_static(STR_(node
)->s
, STR_(node
)->end
,
3176 p
= strcat_capa(STR_(node
)->s
, STR_(node
)->end
, s
, end
, capa
, STR_(node
)->capacity
);
3178 CHECK_NULL_RETURN_MEMERR(p
);
3180 STR_(node
)->capacity
= capa
;
3184 onig_strcpy(STR_(node
)->s
+ len
, s
, end
);
3186 STR_(node
)->end
= STR_(node
)->s
+ len
+ addlen
;
3193 onig_node_str_set(Node
* node
, const UChar
* s
, const UChar
* end
)
3195 onig_node_str_clear(node
);
3196 return onig_node_str_cat(node
, s
, end
);
3200 node_str_cat_char(Node
* node
, UChar c
)
3205 return onig_node_str_cat(node
, s
, s
+ 1);
3209 onig_node_conv_to_str_node(Node
* node
, int flag
)
3211 NODE_SET_TYPE(node
, NODE_STRING
);
3212 STR_(node
)->flag
= flag
;
3213 STR_(node
)->capacity
= 0;
3214 STR_(node
)->s
= STR_(node
)->buf
;
3215 STR_(node
)->end
= STR_(node
)->buf
;
3219 onig_node_str_clear(Node
* node
)
3221 if (STR_(node
)->capacity
!= 0 &&
3222 IS_NOT_NULL(STR_(node
)->s
) && STR_(node
)->s
!= STR_(node
)->buf
) {
3223 xfree(STR_(node
)->s
);
3226 STR_(node
)->capacity
= 0;
3227 STR_(node
)->flag
= 0;
3228 STR_(node
)->s
= STR_(node
)->buf
;
3229 STR_(node
)->end
= STR_(node
)->buf
;
3233 node_new_str(const UChar
* s
, const UChar
* end
)
3235 Node
* node
= node_new();
3236 CHECK_NULL_RETURN(node
);
3238 NODE_SET_TYPE(node
, NODE_STRING
);
3239 STR_(node
)->capacity
= 0;
3240 STR_(node
)->flag
= 0;
3241 STR_(node
)->s
= STR_(node
)->buf
;
3242 STR_(node
)->end
= STR_(node
)->buf
;
3243 if (onig_node_str_cat(node
, s
, end
)) {
3244 onig_node_free(node
);
3251 onig_node_new_str(const UChar
* s
, const UChar
* end
)
3253 return node_new_str(s
, end
);
3257 node_new_str_raw(UChar
* s
, UChar
* end
)
3259 Node
* node
= node_new_str(s
, end
);
3260 CHECK_NULL_RETURN(node
);
3261 NODE_STRING_SET_RAW(node
);
3266 node_new_empty(void)
3268 return node_new_str(NULL
, NULL
);
3272 node_new_str_raw_char(UChar c
)
3279 node
= node_new_str_raw(p
, p
+ 1);
3281 /* clear buf tail */
3282 for (i
= 1; i
< NODE_STRING_BUF_SIZE
; i
++)
3283 STR_(node
)->buf
[i
] = '\0';
3289 str_node_split_last_char(Node
* node
, OnigEncoding enc
)
3297 if (sn
->end
> sn
->s
) {
3298 p
= onigenc_get_prev_char_head(enc
, sn
->s
, sn
->end
);
3299 if (p
&& p
> sn
->s
) { /* can be split. */
3300 rn
= node_new_str(p
, sn
->end
);
3301 CHECK_NULL_RETURN(rn
);
3302 if (NODE_STRING_IS_RAW(node
))
3303 NODE_STRING_SET_RAW(rn
);
3305 sn
->end
= (UChar
* )p
;
3312 str_node_can_be_split(Node
* node
, OnigEncoding enc
)
3314 StrNode
* sn
= STR_(node
);
3315 if (sn
->end
> sn
->s
) {
3316 return ((enclen(enc
, sn
->s
) < sn
->end
- sn
->s
) ? 1 : 0);
3322 onig_scan_unsigned_number(UChar
** src
, const UChar
* end
, OnigEncoding enc
)
3324 unsigned int num
, val
;
3332 if (IS_CODE_DIGIT_ASCII(enc
, c
)) {
3333 val
= (unsigned int )DIGITVAL(c
);
3334 if ((INT_MAX_LIMIT
- val
) / 10UL < num
)
3335 return -1; /* overflow */
3337 num
= num
* 10 + val
;
3349 scan_unsigned_hexadecimal_number(UChar
** src
, UChar
* end
, int minlen
,
3350 int maxlen
, OnigEncoding enc
)
3353 unsigned int num
, val
;
3360 while (! PEND
&& n
< maxlen
) {
3362 if (IS_CODE_XDIGIT_ASCII(enc
, c
)) {
3364 val
= (unsigned int )XDIGITVAL(enc
,c
);
3365 if ((INT_MAX_LIMIT
- val
) / 16UL < num
)
3366 return ONIGERR_TOO_BIG_NUMBER
; /* overflow */
3368 num
= (num
<< 4) + XDIGITVAL(enc
,c
);
3377 return ONIGERR_INVALID_CODE_POINT_VALUE
;
3384 scan_unsigned_octal_number(UChar
** src
, UChar
* end
, int maxlen
,
3388 unsigned int num
, val
;
3393 while (! PEND
&& maxlen
-- != 0) {
3395 if (IS_CODE_DIGIT_ASCII(enc
, c
) && c
< '8') {
3397 if ((INT_MAX_LIMIT
- val
) / 8UL < num
)
3398 return -1; /* overflow */
3400 num
= (num
<< 3) + val
;
3412 #define BB_WRITE_CODE_POINT(bbuf,pos,code) \
3413 BB_WRITE(bbuf, pos, &(code), SIZE_CODE_POINT)
3416 [n][from-1][to-1][from-2][to-2] ... [from-n][to-n]
3417 (all data size is OnigCodePoint)
3420 new_code_range(BBuf
** pbuf
)
3422 #define INIT_MULTI_BYTE_RANGE_SIZE (SIZE_CODE_POINT * 5)
3427 bbuf
= *pbuf
= (BBuf
* )xmalloc(sizeof(BBuf
));
3428 CHECK_NULL_RETURN_MEMERR(bbuf
);
3429 r
= BB_INIT(bbuf
, INIT_MULTI_BYTE_RANGE_SIZE
);
3437 BB_WRITE_CODE_POINT(bbuf
, 0, n
);
3442 add_code_range_to_buf(BBuf
** pbuf
, OnigCodePoint from
, OnigCodePoint to
)
3445 int low
, high
, bound
, x
;
3446 OnigCodePoint n
, *data
;
3450 n
= from
; from
= to
; to
= n
;
3453 if (IS_NULL(*pbuf
)) {
3454 r
= new_code_range(pbuf
);
3455 if (r
!= 0) return r
;
3461 GET_CODE_POINT(n
, bbuf
->p
);
3463 data
= (OnigCodePoint
* )(bbuf
->p
);
3466 for (low
= 0, bound
= n
; low
< bound
; ) {
3467 x
= (low
+ bound
) >> 1;
3468 if (from
> data
[x
*2 + 1])
3474 high
= (to
== ~((OnigCodePoint
)0)) ? n
: low
;
3475 for (bound
= n
; high
< bound
; ) {
3476 x
= (high
+ bound
) >> 1;
3477 if (to
+ 1 >= data
[x
*2])
3483 inc_n
= low
+ 1 - high
;
3484 if (n
+ inc_n
> ONIG_MAX_MULTI_BYTE_RANGES_NUM
)
3485 return ONIGERR_TOO_MANY_MULTI_BYTE_RANGES
;
3488 if (from
> data
[low
*2])
3490 if (to
< data
[(high
- 1)*2 + 1])
3491 to
= data
[(high
- 1)*2 + 1];
3494 if (inc_n
!= 0 && (OnigCodePoint
)high
< n
) {
3495 int from_pos
= SIZE_CODE_POINT
* (1 + high
* 2);
3496 int to_pos
= SIZE_CODE_POINT
* (1 + (low
+ 1) * 2);
3497 int size
= (n
- high
) * 2 * SIZE_CODE_POINT
;
3500 BB_MOVE_RIGHT(bbuf
, from_pos
, to_pos
, size
);
3503 BB_MOVE_LEFT_REDUCE(bbuf
, from_pos
, to_pos
);
3507 pos
= SIZE_CODE_POINT
* (1 + low
* 2);
3508 BB_ENSURE_SIZE(bbuf
, pos
+ SIZE_CODE_POINT
* 2);
3509 BB_WRITE_CODE_POINT(bbuf
, pos
, from
);
3510 BB_WRITE_CODE_POINT(bbuf
, pos
+ SIZE_CODE_POINT
, to
);
3512 BB_WRITE_CODE_POINT(bbuf
, 0, n
);
3518 add_code_range(BBuf
** pbuf
, ScanEnv
* env
, OnigCodePoint from
, OnigCodePoint to
)
3521 if (IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_ALLOW_EMPTY_RANGE_IN_CC
))
3524 return ONIGERR_EMPTY_RANGE_IN_CHAR_CLASS
;
3527 return add_code_range_to_buf(pbuf
, from
, to
);
3531 not_code_range_buf(OnigEncoding enc
, BBuf
* bbuf
, BBuf
** pbuf
)
3534 OnigCodePoint pre
, from
, *data
, to
= 0;
3536 *pbuf
= (BBuf
* )NULL
;
3537 if (IS_NULL(bbuf
)) {
3539 return SET_ALL_MULTI_BYTE_RANGE(enc
, pbuf
);
3542 data
= (OnigCodePoint
* )(bbuf
->p
);
3543 GET_CODE_POINT(n
, data
);
3545 if (n
<= 0) goto set_all
;
3548 pre
= MBCODE_START_POS(enc
);
3549 for (i
= 0; i
< n
; i
++) {
3552 if (pre
<= from
- 1) {
3553 r
= add_code_range_to_buf(pbuf
, pre
, from
- 1);
3554 if (r
!= 0) return r
;
3556 if (to
== ~((OnigCodePoint
)0)) break;
3559 if (to
< ~((OnigCodePoint
)0)) {
3560 r
= add_code_range_to_buf(pbuf
, to
+ 1, ~((OnigCodePoint
)0));
3565 #define SWAP_BB_NOT(bbuf1, not1, bbuf2, not2) do {\
3568 tnot = not1; not1 = not2; not2 = tnot; \
3569 tbuf = bbuf1; bbuf1 = bbuf2; bbuf2 = tbuf; \
3573 or_code_range_buf(OnigEncoding enc
, BBuf
* bbuf1
, int not1
,
3574 BBuf
* bbuf2
, int not2
, BBuf
** pbuf
)
3577 OnigCodePoint i
, n1
, *data1
;
3578 OnigCodePoint from
, to
;
3580 *pbuf
= (BBuf
* )NULL
;
3581 if (IS_NULL(bbuf1
) && IS_NULL(bbuf2
)) {
3582 if (not1
!= 0 || not2
!= 0)
3583 return SET_ALL_MULTI_BYTE_RANGE(enc
, pbuf
);
3589 SWAP_BB_NOT(bbuf1
, not1
, bbuf2
, not2
);
3591 if (IS_NULL(bbuf1
)) {
3593 return SET_ALL_MULTI_BYTE_RANGE(enc
, pbuf
);
3597 return bbuf_clone(pbuf
, bbuf2
);
3600 return not_code_range_buf(enc
, bbuf2
, pbuf
);
3606 SWAP_BB_NOT(bbuf1
, not1
, bbuf2
, not2
);
3608 data1
= (OnigCodePoint
* )(bbuf1
->p
);
3609 GET_CODE_POINT(n1
, data1
);
3612 if (not2
== 0 && not1
== 0) { /* 1 OR 2 */
3613 r
= bbuf_clone(pbuf
, bbuf2
);
3615 else if (not1
== 0) { /* 1 OR (not 2) */
3616 r
= not_code_range_buf(enc
, bbuf2
, pbuf
);
3618 if (r
!= 0) return r
;
3620 for (i
= 0; i
< n1
; i
++) {
3623 r
= add_code_range_to_buf(pbuf
, from
, to
);
3624 if (r
!= 0) return r
;
3630 and_code_range1(BBuf
** pbuf
, OnigCodePoint from1
, OnigCodePoint to1
,
3631 OnigCodePoint
* data
, int n
)
3634 OnigCodePoint from2
, to2
;
3636 for (i
= 0; i
< n
; i
++) {
3639 if (from2
< from1
) {
3640 if (to2
< from1
) continue;
3645 else if (from2
<= to1
) {
3647 if (from1
<= from2
- 1) {
3648 r
= add_code_range_to_buf(pbuf
, from1
, from2
-1);
3649 if (r
!= 0) return r
;
3660 if (from1
> to1
) break;
3663 r
= add_code_range_to_buf(pbuf
, from1
, to1
);
3664 if (r
!= 0) return r
;
3670 and_code_range_buf(BBuf
* bbuf1
, int not1
, BBuf
* bbuf2
, int not2
, BBuf
** pbuf
)
3673 OnigCodePoint i
, j
, n1
, n2
, *data1
, *data2
;
3674 OnigCodePoint from
, to
, from1
, to1
, from2
, to2
;
3676 *pbuf
= (BBuf
* )NULL
;
3677 if (IS_NULL(bbuf1
)) {
3678 if (not1
!= 0 && IS_NOT_NULL(bbuf2
)) /* not1 != 0 -> not2 == 0 */
3679 return bbuf_clone(pbuf
, bbuf2
);
3682 else if (IS_NULL(bbuf2
)) {
3684 return bbuf_clone(pbuf
, bbuf1
);
3689 SWAP_BB_NOT(bbuf1
, not1
, bbuf2
, not2
);
3691 data1
= (OnigCodePoint
* )(bbuf1
->p
);
3692 data2
= (OnigCodePoint
* )(bbuf2
->p
);
3693 GET_CODE_POINT(n1
, data1
);
3694 GET_CODE_POINT(n2
, data2
);
3698 if (not2
== 0 && not1
== 0) { /* 1 AND 2 */
3699 for (i
= 0; i
< n1
; i
++) {
3702 for (j
= 0; j
< n2
; j
++) {
3705 if (from2
> to1
) break;
3706 if (to2
< from1
) continue;
3707 from
= MAX(from1
, from2
);
3709 r
= add_code_range_to_buf(pbuf
, from
, to
);
3710 if (r
!= 0) return r
;
3714 else if (not1
== 0) { /* 1 AND (not 2) */
3715 for (i
= 0; i
< n1
; i
++) {
3718 r
= and_code_range1(pbuf
, from1
, to1
, data2
, n2
);
3719 if (r
!= 0) return r
;
3727 and_cclass(CClassNode
* dest
, CClassNode
* cc
, OnigEncoding enc
)
3730 BBuf
*buf1
, *buf2
, *pbuf
;
3731 BitSetRef bsr1
, bsr2
;
3734 not1
= IS_NCCLASS_NOT(dest
);
3737 not2
= IS_NCCLASS_NOT(cc
);
3742 bitset_invert_to(bsr1
, bs1
);
3746 bitset_invert_to(bsr2
, bs2
);
3749 bitset_and(bsr1
, bsr2
);
3750 if (bsr1
!= dest
->bs
) {
3751 bitset_copy(dest
->bs
, bsr1
);
3754 bitset_invert(dest
->bs
);
3757 if (! ONIGENC_IS_SINGLEBYTE(enc
)) {
3758 if (not1
!= 0 && not2
!= 0) {
3759 r
= or_code_range_buf(enc
, buf1
, 0, buf2
, 0, &pbuf
);
3762 r
= and_code_range_buf(buf1
, not1
, buf2
, not2
, &pbuf
);
3763 if (r
== 0 && not1
!= 0) {
3765 r
= not_code_range_buf(enc
, pbuf
, &tbuf
);
3774 if (r
!= 0) return r
;
3784 or_cclass(CClassNode
* dest
, CClassNode
* cc
, OnigEncoding enc
)
3787 BBuf
*buf1
, *buf2
, *pbuf
;
3788 BitSetRef bsr1
, bsr2
;
3791 not1
= IS_NCCLASS_NOT(dest
);
3794 not2
= IS_NCCLASS_NOT(cc
);
3799 bitset_invert_to(bsr1
, bs1
);
3803 bitset_invert_to(bsr2
, bs2
);
3806 bitset_or(bsr1
, bsr2
);
3807 if (bsr1
!= dest
->bs
) {
3808 bitset_copy(dest
->bs
, bsr1
);
3811 bitset_invert(dest
->bs
);
3814 if (! ONIGENC_IS_SINGLEBYTE(enc
)) {
3815 if (not1
!= 0 && not2
!= 0) {
3816 r
= and_code_range_buf(buf1
, 0, buf2
, 0, &pbuf
);
3819 r
= or_code_range_buf(enc
, buf1
, not1
, buf2
, not2
, &pbuf
);
3820 if (r
== 0 && not1
!= 0) {
3822 r
= not_code_range_buf(enc
, pbuf
, &tbuf
);
3831 if (r
!= 0) return r
;
3841 static OnigCodePoint
3842 conv_backslash_value(OnigCodePoint c
, ScanEnv
* env
)
3844 if (IS_SYNTAX_OP(env
->syntax
, ONIG_SYN_OP_ESC_CONTROL_CHARS
)) {
3846 case 'n': return '\n';
3847 case 't': return '\t';
3848 case 'r': return '\r';
3849 case 'f': return '\f';
3850 case 'a': return '\007';
3851 case 'b': return '\010';
3852 case 'e': return '\033';
3854 if (IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_ESC_V_VTAB
))
3866 is_invalid_quantifier_target(Node
* node
)
3868 switch (NODE_TYPE(node
)) {
3875 /* allow enclosed elements */
3876 /* return is_invalid_quantifier_target(NODE_BODY(node)); */
3881 if (! is_invalid_quantifier_target(NODE_CAR(node
))) return 0;
3882 } while (IS_NOT_NULL(node
= NODE_CDR(node
)));
3888 if (is_invalid_quantifier_target(NODE_CAR(node
))) return 1;
3889 } while (IS_NOT_NULL(node
= NODE_CDR(node
)));
3898 /* ?:0, *:1, +:2, ??:3, *?:4, +?:5 */
3900 quantifier_type_num(QuantNode
* q
)
3903 if (q
->lower
== 0) {
3904 if (q
->upper
== 1) return 0;
3905 else if (IS_INFINITE_REPEAT(q
->upper
)) return 1;
3907 else if (q
->lower
== 1) {
3908 if (IS_INFINITE_REPEAT(q
->upper
)) return 2;
3912 if (q
->lower
== 0) {
3913 if (q
->upper
== 1) return 3;
3914 else if (IS_INFINITE_REPEAT(q
->upper
)) return 4;
3916 else if (q
->lower
== 1) {
3917 if (IS_INFINITE_REPEAT(q
->upper
)) return 5;
3925 RQ_ASIS
= 0, /* as is */
3926 RQ_DEL
= 1, /* delete parent */
3928 RQ_AQ
, /* to '*?' */
3929 RQ_QQ
, /* to '??' */
3930 RQ_P_QQ
, /* to '+)??' */
3931 RQ_PQ_Q
/* to '+?)?' */
3934 static enum ReduceType ReduceTypeTable
[6][6] = {
3935 {RQ_DEL
, RQ_A
, RQ_A
, RQ_QQ
, RQ_AQ
, RQ_ASIS
}, /* '?' */
3936 {RQ_DEL
, RQ_DEL
, RQ_DEL
, RQ_P_QQ
, RQ_P_QQ
, RQ_DEL
}, /* '*' */
3937 {RQ_A
, RQ_A
, RQ_DEL
, RQ_ASIS
, RQ_P_QQ
, RQ_DEL
}, /* '+' */
3938 {RQ_DEL
, RQ_AQ
, RQ_AQ
, RQ_DEL
, RQ_AQ
, RQ_AQ
}, /* '??' */
3939 {RQ_DEL
, RQ_DEL
, RQ_DEL
, RQ_DEL
, RQ_DEL
, RQ_DEL
}, /* '*?' */
3940 {RQ_ASIS
, RQ_PQ_Q
, RQ_DEL
, RQ_AQ
, RQ_AQ
, RQ_DEL
} /* '+?' */
3944 onig_reduce_nested_quantifier(Node
* pnode
, Node
* cnode
)
3951 pnum
= quantifier_type_num(p
);
3952 cnum
= quantifier_type_num(c
);
3953 if (pnum
< 0 || cnum
< 0) {
3954 if ((p
->lower
== p
->upper
) && ! IS_INFINITE_REPEAT(p
->upper
)) {
3955 if ((c
->lower
== c
->upper
) && ! IS_INFINITE_REPEAT(c
->upper
)) {
3956 int n
= onig_positive_int_multiply(p
->lower
, c
->lower
);
3958 p
->lower
= p
->upper
= n
;
3959 NODE_BODY(pnode
) = NODE_BODY(cnode
);
3968 switch(ReduceTypeTable
[cnum
][pnum
]) {
3973 NODE_BODY(pnode
) = NODE_BODY(cnode
);
3974 p
->lower
= 0; p
->upper
= INFINITE_REPEAT
; p
->greedy
= 1;
3977 NODE_BODY(pnode
) = NODE_BODY(cnode
);
3978 p
->lower
= 0; p
->upper
= INFINITE_REPEAT
; p
->greedy
= 0;
3981 NODE_BODY(pnode
) = NODE_BODY(cnode
);
3982 p
->lower
= 0; p
->upper
= 1; p
->greedy
= 0;
3985 NODE_BODY(pnode
) = cnode
;
3986 p
->lower
= 0; p
->upper
= 1; p
->greedy
= 0;
3987 c
->lower
= 1; c
->upper
= INFINITE_REPEAT
; c
->greedy
= 1;
3991 NODE_BODY(pnode
) = cnode
;
3992 p
->lower
= 0; p
->upper
= 1; p
->greedy
= 1;
3993 c
->lower
= 1; c
->upper
= INFINITE_REPEAT
; c
->greedy
= 0;
3997 NODE_BODY(pnode
) = cnode
;
4003 NODE_BODY(cnode
) = NULL_NODE
;
4004 onig_node_free(cnode
);
4008 node_new_general_newline(Node
** node
, ScanEnv
* env
)
4012 UChar buf
[ONIGENC_CODE_TO_MBC_MAXLEN
* 2];
4018 dlen
= ONIGENC_CODE_TO_MBC(env
->enc
, 0x0d, buf
);
4019 if (dlen
< 0) return dlen
;
4020 alen
= ONIGENC_CODE_TO_MBC(env
->enc
, 0x0a, buf
+ dlen
);
4021 if (alen
< 0) return alen
;
4023 crnl
= node_new_str_raw(buf
, buf
+ dlen
+ alen
);
4024 CHECK_NULL_RETURN_MEMERR(crnl
);
4026 ncc
= node_new_cclass();
4027 if (IS_NULL(ncc
)) goto err2
;
4031 bitset_set_range(cc
->bs
, 0x0a, 0x0d);
4034 r
= add_code_range(&(cc
->mbuf
), env
, 0x0a, 0x0d);
4037 onig_node_free(ncc
);
4039 onig_node_free(crnl
);
4040 return ONIGERR_MEMORY
;
4044 if (ONIGENC_IS_UNICODE_ENCODING(env
->enc
)) {
4045 r
= add_code_range(&(cc
->mbuf
), env
, 0x85, 0x85);
4046 if (r
!= 0) goto err1
;
4047 r
= add_code_range(&(cc
->mbuf
), env
, 0x2028, 0x2029);
4048 if (r
!= 0) goto err1
;
4051 x
= node_new_bag_if_else(crnl
, 0, ncc
);
4052 if (IS_NULL(x
)) goto err1
;
4059 TK_EOT
= 0, /* end of token */
4071 TK_ANYCHAR_ANYTIME
, /* SQL '%' == .* */
4077 TK_CHAR_PROPERTY
, /* \p{...}, \P{...} */
4079 TK_GENERAL_NEWLINE
, /* \R */
4080 TK_NO_NEWLINE
, /* \N */
4081 TK_TRUE_ANYCHAR
, /* \O */
4082 TK_TEXT_SEGMENT
, /* \X */
4087 TK_POSIX_BRACKET_OPEN
,
4089 TK_CC_CC_OPEN
/* [ */
4093 enum TokenSyms type
;
4095 int base
; /* is number: 8, 16 (used in [....]) */
4114 #ifdef USE_BACKREF_WITH_LEVEL
4116 int level
; /* \k<name+n> */
4134 fetch_interval_quantifier(UChar
** src
, UChar
* end
, PToken
* tok
, ScanEnv
* env
)
4136 int low
, up
, syn_allow
, non_low
= 0;
4139 OnigEncoding enc
= env
->enc
;
4143 syn_allow
= IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_ALLOW_INVALID_INTERVAL
);
4147 return 1; /* "....{" : OK! */
4149 return ONIGERR_END_PATTERN_AT_LEFT_BRACE
; /* "....{" syntax error */
4154 if (c
== ')' || c
== '(' || c
== '|') {
4155 return ONIGERR_END_PATTERN_AT_LEFT_BRACE
;
4159 low
= onig_scan_unsigned_number(&p
, end
, env
->enc
);
4160 if (low
< 0) return ONIGERR_TOO_BIG_NUMBER_FOR_REPEAT_RANGE
;
4161 if (low
> ONIG_MAX_REPEAT_NUM
)
4162 return ONIGERR_TOO_BIG_NUMBER_FOR_REPEAT_RANGE
;
4164 if (p
== *src
) { /* can't read low */
4165 if (IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_ALLOW_INTERVAL_LOW_ABBREV
)) {
4166 /* allow {,n} as {0,n} */
4174 if (PEND
) goto invalid
;
4178 up
= onig_scan_unsigned_number(&p
, end
, env
->enc
);
4179 if (up
< 0) return ONIGERR_TOO_BIG_NUMBER_FOR_REPEAT_RANGE
;
4180 if (up
> ONIG_MAX_REPEAT_NUM
)
4181 return ONIGERR_TOO_BIG_NUMBER_FOR_REPEAT_RANGE
;
4186 up
= INFINITE_REPEAT
; /* {n,} : {n,infinite} */
4194 up
= low
; /* {n} : exact n times */
4198 if (PEND
) goto invalid
;
4200 if (IS_SYNTAX_OP(env
->syntax
, ONIG_SYN_OP_ESC_BRACE_INTERVAL
)) {
4201 if (c
!= MC_ESC(env
->syntax
)) goto invalid
;
4204 if (c
!= '}') goto invalid
;
4206 if (!IS_INFINITE_REPEAT(up
) && low
> up
) {
4207 /* {n,m}+ supported case */
4208 if (IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_PLUS_POSSESSIVE_INTERVAL
))
4209 return ONIGERR_UPPER_SMALLER_THAN_LOWER_IN_REPEAT_RANGE
;
4211 tok
->u
.repeat
.possessive
= 1;
4214 tmp
= low
; low
= up
; up
= tmp
;
4218 tok
->u
.repeat
.possessive
= 0;
4220 tok
->type
= TK_INTERVAL
;
4221 tok
->u
.repeat
.lower
= low
;
4222 tok
->u
.repeat
.upper
= up
;
4224 return r
; /* 0: normal {n,m}, 2: fixed {n} */
4228 /* *src = p; */ /* !!! Don't do this line !!! */
4232 return ONIGERR_INVALID_REPEAT_RANGE_PATTERN
;
4235 /* \M-, \C-, \c, or \... */
4237 fetch_escaped_value(UChar
** src
, UChar
* end
, ScanEnv
* env
, OnigCodePoint
* val
)
4241 OnigEncoding enc
= env
->enc
;
4244 if (PEND
) return ONIGERR_END_PATTERN_AT_ESCAPE
;
4249 if (IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_ESC_CAPITAL_M_BAR_META
)) {
4250 if (PEND
) return ONIGERR_END_PATTERN_AT_META
;
4252 if (c
!= '-') return ONIGERR_META_CODE_SYNTAX
;
4253 if (PEND
) return ONIGERR_END_PATTERN_AT_META
;
4255 if (c
== MC_ESC(env
->syntax
)) {
4256 v
= fetch_escaped_value(&p
, end
, env
, &c
);
4257 if (v
< 0) return v
;
4259 c
= ((c
& 0xff) | 0x80);
4266 if (IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_ESC_CAPITAL_C_BAR_CONTROL
)) {
4267 if (PEND
) return ONIGERR_END_PATTERN_AT_CONTROL
;
4269 if (c
!= '-') return ONIGERR_CONTROL_CODE_SYNTAX
;
4276 if (IS_SYNTAX_OP(env
->syntax
, ONIG_SYN_OP_ESC_C_CONTROL
)) {
4278 if (PEND
) return ONIGERR_END_PATTERN_AT_CONTROL
;
4284 if (c
== MC_ESC(env
->syntax
)) {
4285 v
= fetch_escaped_value(&p
, end
, env
, &c
);
4286 if (v
< 0) return v
;
4297 c
= conv_backslash_value(c
, env
);
4307 static int fetch_token(PToken
* tok
, UChar
** src
, UChar
* end
, ScanEnv
* env
);
4309 static OnigCodePoint
4310 get_name_end_code_point(OnigCodePoint start
)
4313 case '<': return (OnigCodePoint
)'>'; break;
4314 case '\'': return (OnigCodePoint
)'\''; break;
4315 case '(': return (OnigCodePoint
)')'; break;
4320 return (OnigCodePoint
)0;
4329 #ifdef USE_BACKREF_WITH_LEVEL
4331 \k<name+n>, \k<name-n>
4332 \k<num+n>, \k<num-n>
4333 \k<-num+n>, \k<-num-n>
4334 \k<+num+n>, \k<+num-n>
4337 fetch_name_with_level(OnigCodePoint start_code
, UChar
** src
, UChar
* end
,
4338 UChar
** rname_end
, ScanEnv
* env
,
4339 int* rback_num
, int* rlevel
, enum REF_NUM
* num_type
)
4341 int r
, sign
, exist_level
;
4343 OnigCodePoint end_code
;
4344 OnigCodePoint c
= 0;
4345 OnigEncoding enc
= env
->enc
;
4353 *num_type
= IS_NOT_NUM
;
4357 end_code
= get_name_end_code_point(start_code
);
4363 return ONIGERR_EMPTY_GROUP_NAME
;
4368 return ONIGERR_EMPTY_GROUP_NAME
;
4370 if (IS_CODE_DIGIT_ASCII(enc
, c
)) {
4371 *num_type
= IS_ABS_NUM
;
4374 else if (c
== '-') {
4375 *num_type
= IS_REL_NUM
;
4379 else if (c
== '+') {
4380 *num_type
= IS_REL_NUM
;
4384 else if (!ONIGENC_IS_CODE_WORD(enc
, c
)) {
4385 r
= ONIGERR_INVALID_CHAR_IN_GROUP_NAME
;
4392 if (c
== end_code
|| c
== ')' || c
== '+' || c
== '-') {
4393 if (*num_type
!= IS_NOT_NUM
&& digit_count
== 0)
4394 r
= ONIGERR_INVALID_GROUP_NAME
;
4398 if (*num_type
!= IS_NOT_NUM
) {
4399 if (IS_CODE_DIGIT_ASCII(enc
, c
)) {
4403 r
= ONIGERR_INVALID_GROUP_NAME
;
4404 *num_type
= IS_NOT_NUM
;
4407 else if (!ONIGENC_IS_CODE_WORD(enc
, c
)) {
4408 r
= ONIGERR_INVALID_CHAR_IN_GROUP_NAME
;
4412 if (r
== 0 && c
!= end_code
) {
4413 if (c
== '+' || c
== '-') {
4415 int flag
= (c
== '-' ? -1 : 1);
4418 r
= ONIGERR_INVALID_CHAR_IN_GROUP_NAME
;
4422 if (! IS_CODE_DIGIT_ASCII(enc
, c
)) goto err
;
4424 level
= onig_scan_unsigned_number(&p
, end
, enc
);
4425 if (level
< 0) return ONIGERR_TOO_BIG_NUMBER
;
4426 *rlevel
= (level
* flag
);
4439 r
= ONIGERR_INVALID_GROUP_NAME
;
4444 if (*num_type
!= IS_NOT_NUM
) {
4445 *rback_num
= onig_scan_unsigned_number(&pnum_head
, name_end
, enc
);
4446 if (*rback_num
< 0) return ONIGERR_TOO_BIG_NUMBER
;
4447 else if (*rback_num
== 0) {
4448 if (*num_type
== IS_REL_NUM
)
4455 *rname_end
= name_end
;
4457 return (exist_level
? 1 : 0);
4460 onig_scan_env_set_error_string(env
, r
, *src
, name_end
);
4464 #endif /* USE_BACKREF_WITH_LEVEL */
4467 ref: 0 -> define name (don't allow number name)
4468 1 -> reference name (allow number name)
4471 fetch_name(OnigCodePoint start_code
, UChar
** src
, UChar
* end
,
4472 UChar
** rname_end
, ScanEnv
* env
, int* rback_num
,
4473 enum REF_NUM
* num_type
, int ref
)
4477 OnigCodePoint end_code
;
4478 OnigCodePoint c
= 0;
4479 OnigEncoding enc
= env
->enc
;
4486 end_code
= get_name_end_code_point(start_code
);
4492 *num_type
= IS_NOT_NUM
;
4495 return ONIGERR_EMPTY_GROUP_NAME
;
4500 return ONIGERR_EMPTY_GROUP_NAME
;
4502 if (IS_CODE_DIGIT_ASCII(enc
, c
)) {
4504 *num_type
= IS_ABS_NUM
;
4506 r
= ONIGERR_INVALID_GROUP_NAME
;
4510 else if (c
== '-') {
4512 *num_type
= IS_REL_NUM
;
4517 r
= ONIGERR_INVALID_GROUP_NAME
;
4520 else if (c
== '+') {
4522 *num_type
= IS_REL_NUM
;
4527 r
= ONIGERR_INVALID_GROUP_NAME
;
4530 else if (!ONIGENC_IS_CODE_WORD(enc
, c
)) {
4531 r
= ONIGERR_INVALID_CHAR_IN_GROUP_NAME
;
4539 if (c
== end_code
|| c
== ')') {
4540 if (*num_type
!= IS_NOT_NUM
&& digit_count
== 0)
4541 r
= ONIGERR_INVALID_GROUP_NAME
;
4545 if (*num_type
!= IS_NOT_NUM
) {
4546 if (IS_CODE_DIGIT_ASCII(enc
, c
)) {
4550 if (!ONIGENC_IS_CODE_WORD(enc
, c
))
4551 r
= ONIGERR_INVALID_CHAR_IN_GROUP_NAME
;
4553 r
= ONIGERR_INVALID_GROUP_NAME
;
4555 *num_type
= IS_NOT_NUM
;
4559 if (!ONIGENC_IS_CODE_WORD(enc
, c
)) {
4560 r
= ONIGERR_INVALID_CHAR_IN_GROUP_NAME
;
4565 if (c
!= end_code
) {
4566 r
= ONIGERR_INVALID_GROUP_NAME
;
4570 if (*num_type
!= IS_NOT_NUM
) {
4571 *rback_num
= onig_scan_unsigned_number(&pnum_head
, name_end
, enc
);
4572 if (*rback_num
< 0) return ONIGERR_TOO_BIG_NUMBER
;
4573 else if (*rback_num
== 0) {
4574 if (*num_type
== IS_REL_NUM
) {
4575 r
= ONIGERR_INVALID_GROUP_NAME
;
4583 *rname_end
= name_end
;
4591 if (c
== end_code
|| c
== ')')
4598 onig_scan_env_set_error_string(env
, r
, *src
, name_end
);
4604 CC_ESC_WARN(ScanEnv
* env
, UChar
*c
)
4606 if (onig_warn
== onig_null_warn
) return ;
4608 if (IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_WARN_CC_OP_NOT_ESCAPED
) &&
4609 IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_BACKSLASH_ESCAPE_IN_CC
)) {
4610 UChar buf
[WARN_BUFSIZE
];
4611 onig_snprintf_with_pattern(buf
, WARN_BUFSIZE
, env
->enc
,
4612 env
->pattern
, env
->pattern_end
,
4613 (UChar
* )"character class has '%s' without escape",
4615 (*onig_warn
)((char* )buf
);
4620 CLOSE_BRACKET_WITHOUT_ESC_WARN(ScanEnv
* env
, UChar
* c
)
4622 if (onig_warn
== onig_null_warn
) return ;
4624 if (IS_SYNTAX_BV((env
)->syntax
, ONIG_SYN_WARN_CC_OP_NOT_ESCAPED
)) {
4625 UChar buf
[WARN_BUFSIZE
];
4626 onig_snprintf_with_pattern(buf
, WARN_BUFSIZE
, (env
)->enc
,
4627 (env
)->pattern
, (env
)->pattern_end
,
4628 (UChar
* )"regular expression has '%s' without escape", c
);
4629 (*onig_warn
)((char* )buf
);
4634 find_str_position(OnigCodePoint s
[], int n
, UChar
* from
, UChar
* to
,
4635 UChar
**next
, OnigEncoding enc
)
4643 x
= ONIGENC_MBC_TO_CODE(enc
, p
, to
);
4644 q
= p
+ enclen(enc
, p
);
4646 for (i
= 1; i
< n
&& q
< to
; i
++) {
4647 x
= ONIGENC_MBC_TO_CODE(enc
, q
, to
);
4648 if (x
!= s
[i
]) break;
4649 q
+= enclen(enc
, q
);
4652 if (IS_NOT_NULL(next
))
4663 str_exist_check_with_esc(OnigCodePoint s
[], int n
, UChar
* from
, UChar
* to
,
4664 OnigCodePoint bad
, OnigEncoding enc
, OnigSyntaxType
* syn
)
4675 p
+= enclen(enc
, p
);
4678 x
= ONIGENC_MBC_TO_CODE(enc
, p
, to
);
4679 q
= p
+ enclen(enc
, p
);
4681 for (i
= 1; i
< n
&& q
< to
; i
++) {
4682 x
= ONIGENC_MBC_TO_CODE(enc
, q
, to
);
4683 if (x
!= s
[i
]) break;
4684 q
+= enclen(enc
, q
);
4686 if (i
>= n
) return 1;
4687 p
+= enclen(enc
, p
);
4690 x
= ONIGENC_MBC_TO_CODE(enc
, p
, to
);
4691 if (x
== bad
) return 0;
4692 else if (x
== MC_ESC(syn
)) in_esc
= 1;
4701 fetch_token_in_cc(PToken
* tok
, UChar
** src
, UChar
* end
, ScanEnv
* env
)
4704 OnigCodePoint c
, c2
;
4705 OnigSyntaxType
* syn
= env
->syntax
;
4706 OnigEncoding enc
= env
->enc
;
4717 tok
->type
= TK_CHAR
;
4723 tok
->type
= TK_CC_CLOSE
;
4725 else if (c
== '-') {
4726 tok
->type
= TK_CC_RANGE
;
4728 else if (c
== MC_ESC(syn
)) {
4729 if (! IS_SYNTAX_BV(syn
, ONIG_SYN_BACKSLASH_ESCAPE_IN_CC
))
4732 if (PEND
) return ONIGERR_END_PATTERN_AT_ESCAPE
;
4739 tok
->type
= TK_CHAR_TYPE
;
4740 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_WORD
;
4741 tok
->u
.prop
.not = 0;
4744 tok
->type
= TK_CHAR_TYPE
;
4745 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_WORD
;
4746 tok
->u
.prop
.not = 1;
4749 tok
->type
= TK_CHAR_TYPE
;
4750 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_DIGIT
;
4751 tok
->u
.prop
.not = 0;
4754 tok
->type
= TK_CHAR_TYPE
;
4755 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_DIGIT
;
4756 tok
->u
.prop
.not = 1;
4759 tok
->type
= TK_CHAR_TYPE
;
4760 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_SPACE
;
4761 tok
->u
.prop
.not = 0;
4764 tok
->type
= TK_CHAR_TYPE
;
4765 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_SPACE
;
4766 tok
->u
.prop
.not = 1;
4769 if (! IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_H_XDIGIT
)) break;
4770 tok
->type
= TK_CHAR_TYPE
;
4771 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_XDIGIT
;
4772 tok
->u
.prop
.not = 0;
4775 if (! IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_H_XDIGIT
)) break;
4776 tok
->type
= TK_CHAR_TYPE
;
4777 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_XDIGIT
;
4778 tok
->u
.prop
.not = 1;
4787 IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_P_BRACE_CHAR_PROPERTY
)) {
4789 tok
->type
= TK_CHAR_PROPERTY
;
4790 tok
->u
.prop
.not = c
== 'P';
4792 if (!PEND
&& IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_P_BRACE_CIRCUMFLEX_NOT
)) {
4795 tok
->u
.prop
.not = tok
->u
.prop
.not == 0;
4807 if (PPEEK_IS('{') && IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_O_BRACE_OCTAL
)) {
4809 num
= scan_unsigned_octal_number(&p
, end
, 11, enc
);
4810 if (num
< 0) return ONIGERR_TOO_BIG_WIDE_CHAR_VALUE
;
4813 if (IS_CODE_DIGIT_ASCII(enc
, c2
))
4814 return ONIGERR_TOO_LONG_WIDE_CHAR_VALUE
;
4817 if (p
> prev
+ enclen(enc
, prev
) && !PEND
&& (PPEEK_IS('}'))) {
4819 tok
->type
= TK_CODE_POINT
;
4821 tok
->u
.code
= (OnigCodePoint
)num
;
4824 /* can't read nothing or invalid format */
4834 if (PPEEK_IS('{') && IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_X_BRACE_HEX8
)) {
4836 num
= scan_unsigned_hexadecimal_number(&p
, end
, 0, 8, enc
);
4838 if (num
== ONIGERR_TOO_BIG_NUMBER
)
4839 return ONIGERR_TOO_BIG_WIDE_CHAR_VALUE
;
4845 if (IS_CODE_XDIGIT_ASCII(enc
, c2
))
4846 return ONIGERR_TOO_LONG_WIDE_CHAR_VALUE
;
4849 if (p
> prev
+ enclen(enc
, prev
) && !PEND
&& (PPEEK_IS('}'))) {
4851 tok
->type
= TK_CODE_POINT
;
4853 tok
->u
.code
= (OnigCodePoint
)num
;
4856 /* can't read nothing or invalid format */
4860 else if (IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_X_HEX2
)) {
4861 num
= scan_unsigned_hexadecimal_number(&p
, end
, 0, 2, enc
);
4862 if (num
< 0) return num
;
4863 if (p
== prev
) { /* can't read nothing. */
4864 num
= 0; /* but, it's not error */
4866 tok
->type
= TK_RAW_BYTE
;
4876 if (IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_U_HEX4
)) {
4877 num
= scan_unsigned_hexadecimal_number(&p
, end
, 4, 4, enc
);
4878 if (num
< 0) return num
;
4879 if (p
== prev
) { /* can't read nothing. */
4880 num
= 0; /* but, it's not error */
4882 tok
->type
= TK_CODE_POINT
;
4884 tok
->u
.code
= (OnigCodePoint
)num
;
4889 case '1': case '2': case '3': case '4': case '5': case '6': case '7':
4890 if (IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_OCTAL3
)) {
4893 num
= scan_unsigned_octal_number(&p
, end
, 3, enc
);
4894 if (num
< 0 || num
>= 256) return ONIGERR_TOO_BIG_NUMBER
;
4895 if (p
== prev
) { /* can't read nothing. */
4896 num
= 0; /* but, it's not error */
4898 tok
->type
= TK_RAW_BYTE
;
4906 num
= fetch_escaped_value(&p
, end
, env
, &c2
);
4907 if (num
< 0) return num
;
4908 if (tok
->u
.c
!= c2
) {
4910 tok
->type
= TK_CODE_POINT
;
4915 else if (c
== '[') {
4916 if (IS_SYNTAX_OP(syn
, ONIG_SYN_OP_POSIX_BRACKET
) && (PPEEK_IS(':'))) {
4917 OnigCodePoint send
[] = { (OnigCodePoint
)':', (OnigCodePoint
)']' };
4918 tok
->backp
= p
; /* point at '[' is read */
4920 if (str_exist_check_with_esc(send
, 2, p
, end
,
4921 (OnigCodePoint
)']', enc
, syn
)) {
4922 tok
->type
= TK_POSIX_BRACKET_OPEN
;
4931 if (IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_CCLASS_SET_OP
)) {
4932 tok
->type
= TK_CC_CC_OPEN
;
4935 CC_ESC_WARN(env
, (UChar
* )"[");
4939 else if (c
== '&') {
4940 if (IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_CCLASS_SET_OP
) &&
4941 !PEND
&& (PPEEK_IS('&'))) {
4943 tok
->type
= TK_CC_AND
;
4953 fetch_token(PToken
* tok
, UChar
** src
, UChar
* end
, ScanEnv
* env
)
4957 OnigEncoding enc
= env
->enc
;
4958 OnigSyntaxType
* syn
= env
->syntax
;
4969 tok
->type
= TK_STRING
;
4974 if (IS_MC_ESC_CODE(c
, syn
)) {
4975 if (PEND
) return ONIGERR_END_PATTERN_AT_ESCAPE
;
4984 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_ASTERISK_ZERO_INF
)) break;
4985 tok
->type
= TK_REPEAT
;
4986 tok
->u
.repeat
.lower
= 0;
4987 tok
->u
.repeat
.upper
= INFINITE_REPEAT
;
4992 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_PLUS_ONE_INF
)) break;
4993 tok
->type
= TK_REPEAT
;
4994 tok
->u
.repeat
.lower
= 1;
4995 tok
->u
.repeat
.upper
= INFINITE_REPEAT
;
5000 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_QMARK_ZERO_ONE
)) break;
5001 tok
->type
= TK_REPEAT
;
5002 tok
->u
.repeat
.lower
= 0;
5003 tok
->u
.repeat
.upper
= 1;
5005 tok
->u
.repeat
.possessive
= 0;
5007 if (!PEND
&& PPEEK_IS('?') &&
5008 IS_SYNTAX_OP(syn
, ONIG_SYN_OP_QMARK_NON_GREEDY
) &&
5009 tok
->u
.repeat
.possessive
== 0) {
5011 tok
->u
.repeat
.greedy
= 0;
5012 tok
->u
.repeat
.possessive
= 0;
5016 tok
->u
.repeat
.greedy
= 1;
5017 if (!PEND
&& PPEEK_IS('+') &&
5018 ((IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_PLUS_POSSESSIVE_REPEAT
) &&
5019 tok
->type
!= TK_INTERVAL
) ||
5020 (IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_PLUS_POSSESSIVE_INTERVAL
) &&
5021 tok
->type
== TK_INTERVAL
)) &&
5022 tok
->u
.repeat
.possessive
== 0) {
5024 tok
->u
.repeat
.possessive
= 1;
5030 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_BRACE_INTERVAL
)) break;
5031 r
= fetch_interval_quantifier(&p
, end
, tok
, env
);
5032 if (r
< 0) return r
; /* error */
5033 if (r
== 0) goto greedy_check2
;
5034 else if (r
== 2) { /* {n} */
5035 if (IS_SYNTAX_BV(syn
, ONIG_SYN_FIXED_INTERVAL_IS_GREEDY_ONLY
))
5036 goto possessive_check
;
5040 /* r == 1 : normal char */
5044 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_VBAR_ALT
)) break;
5049 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_LPAREN_SUBEXP
)) break;
5050 tok
->type
= TK_SUBEXP_OPEN
;
5054 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_LPAREN_SUBEXP
)) break;
5055 tok
->type
= TK_SUBEXP_CLOSE
;
5059 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_W_WORD
)) break;
5060 tok
->type
= TK_CHAR_TYPE
;
5061 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_WORD
;
5062 tok
->u
.prop
.not = 0;
5066 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_W_WORD
)) break;
5067 tok
->type
= TK_CHAR_TYPE
;
5068 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_WORD
;
5069 tok
->u
.prop
.not = 1;
5073 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_B_WORD_BOUND
)) break;
5074 tok
->type
= TK_ANCHOR
;
5075 tok
->u
.anchor
= ANCR_WORD_BOUNDARY
;
5079 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_B_WORD_BOUND
)) break;
5080 tok
->type
= TK_ANCHOR
;
5081 tok
->u
.anchor
= ANCR_NO_WORD_BOUNDARY
;
5085 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP2_ESC_X_Y_TEXT_SEGMENT
)) break;
5086 tok
->type
= TK_ANCHOR
;
5087 tok
->u
.anchor
= ANCR_TEXT_SEGMENT_BOUNDARY
;
5091 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP2_ESC_X_Y_TEXT_SEGMENT
)) break;
5092 tok
->type
= TK_ANCHOR
;
5093 tok
->u
.anchor
= ANCR_NO_TEXT_SEGMENT_BOUNDARY
;
5096 #ifdef USE_WORD_BEGIN_END
5098 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_LTGT_WORD_BEGIN_END
)) break;
5099 tok
->type
= TK_ANCHOR
;
5100 tok
->u
.anchor
= ANCR_WORD_BEGIN
;
5104 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_LTGT_WORD_BEGIN_END
)) break;
5105 tok
->type
= TK_ANCHOR
;
5106 tok
->u
.anchor
= ANCR_WORD_END
;
5111 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_S_WHITE_SPACE
)) break;
5112 tok
->type
= TK_CHAR_TYPE
;
5113 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_SPACE
;
5114 tok
->u
.prop
.not = 0;
5118 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_S_WHITE_SPACE
)) break;
5119 tok
->type
= TK_CHAR_TYPE
;
5120 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_SPACE
;
5121 tok
->u
.prop
.not = 1;
5125 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_D_DIGIT
)) break;
5126 tok
->type
= TK_CHAR_TYPE
;
5127 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_DIGIT
;
5128 tok
->u
.prop
.not = 0;
5132 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_D_DIGIT
)) break;
5133 tok
->type
= TK_CHAR_TYPE
;
5134 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_DIGIT
;
5135 tok
->u
.prop
.not = 1;
5139 if (! IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_H_XDIGIT
)) break;
5140 tok
->type
= TK_CHAR_TYPE
;
5141 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_XDIGIT
;
5142 tok
->u
.prop
.not = 0;
5146 if (! IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_H_XDIGIT
)) break;
5147 tok
->type
= TK_CHAR_TYPE
;
5148 tok
->u
.prop
.ctype
= ONIGENC_CTYPE_XDIGIT
;
5149 tok
->u
.prop
.not = 1;
5153 if (! IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_CAPITAL_K_KEEP
)) break;
5154 tok
->type
= TK_KEEP
;
5158 if (! IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_CAPITAL_R_GENERAL_NEWLINE
)) break;
5159 tok
->type
= TK_GENERAL_NEWLINE
;
5163 if (! IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_CAPITAL_N_O_SUPER_DOT
)) break;
5164 tok
->type
= TK_NO_NEWLINE
;
5168 if (! IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_CAPITAL_N_O_SUPER_DOT
)) break;
5169 tok
->type
= TK_TRUE_ANYCHAR
;
5173 if (! IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_X_Y_TEXT_SEGMENT
)) break;
5174 tok
->type
= TK_TEXT_SEGMENT
;
5178 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_AZ_BUF_ANCHOR
)) break;
5180 tok
->type
= TK_ANCHOR
;
5181 tok
->u
.subtype
= ANCR_BEGIN_BUF
;
5185 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_AZ_BUF_ANCHOR
)) break;
5186 tok
->type
= TK_ANCHOR
;
5187 tok
->u
.subtype
= ANCR_SEMI_END_BUF
;
5191 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_AZ_BUF_ANCHOR
)) break;
5193 tok
->type
= TK_ANCHOR
;
5194 tok
->u
.subtype
= ANCR_END_BUF
;
5198 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_CAPITAL_G_BEGIN_ANCHOR
)) break;
5199 tok
->type
= TK_ANCHOR
;
5200 tok
->u
.subtype
= ANCR_BEGIN_POSITION
;
5204 if (! IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_GNU_BUF_ANCHOR
)) break;
5209 if (! IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_GNU_BUF_ANCHOR
)) break;
5217 if (PPEEK_IS('{') && IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_O_BRACE_OCTAL
)) {
5219 num
= scan_unsigned_octal_number(&p
, end
, 11, enc
);
5220 if (num
< 0) return ONIGERR_TOO_BIG_WIDE_CHAR_VALUE
;
5222 if (IS_CODE_DIGIT_ASCII(enc
, PPEEK
))
5223 return ONIGERR_TOO_LONG_WIDE_CHAR_VALUE
;
5226 if ((p
> prev
+ enclen(enc
, prev
)) && !PEND
&& PPEEK_IS('}')) {
5228 tok
->type
= TK_CODE_POINT
;
5229 tok
->u
.code
= (OnigCodePoint
)num
;
5232 /* can't read nothing or invalid format */
5242 if (PPEEK_IS('{') && IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_X_BRACE_HEX8
)) {
5244 num
= scan_unsigned_hexadecimal_number(&p
, end
, 0, 8, enc
);
5246 if (num
== ONIGERR_TOO_BIG_NUMBER
)
5247 return ONIGERR_TOO_BIG_WIDE_CHAR_VALUE
;
5252 if (IS_CODE_XDIGIT_ASCII(enc
, PPEEK
))
5253 return ONIGERR_TOO_LONG_WIDE_CHAR_VALUE
;
5256 if ((p
> prev
+ enclen(enc
, prev
)) && !PEND
&& PPEEK_IS('}')) {
5258 tok
->type
= TK_CODE_POINT
;
5259 tok
->u
.code
= (OnigCodePoint
)num
;
5262 /* can't read nothing or invalid format */
5266 else if (IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_X_HEX2
)) {
5267 num
= scan_unsigned_hexadecimal_number(&p
, end
, 0, 2, enc
);
5268 if (num
< 0) return num
;
5269 if (p
== prev
) { /* can't read nothing. */
5270 num
= 0; /* but, it's not error */
5272 tok
->type
= TK_RAW_BYTE
;
5282 if (IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_U_HEX4
)) {
5283 num
= scan_unsigned_hexadecimal_number(&p
, end
, 4, 4, enc
);
5284 if (num
< 0) return num
;
5285 if (p
== prev
) { /* can't read nothing. */
5286 num
= 0; /* but, it's not error */
5288 tok
->type
= TK_CODE_POINT
;
5290 tok
->u
.code
= (OnigCodePoint
)num
;
5294 case '1': case '2': case '3': case '4':
5295 case '5': case '6': case '7': case '8': case '9':
5298 num
= onig_scan_unsigned_number(&p
, end
, enc
);
5299 if (num
< 0 || num
> ONIG_MAX_BACKREF_NUM
) {
5303 if (IS_SYNTAX_OP(syn
, ONIG_SYN_OP_DECIMAL_BACKREF
) &&
5304 (num
<= env
->num_mem
|| num
<= 9)) { /* This spec. from GNU regex */
5305 if (IS_SYNTAX_BV(syn
, ONIG_SYN_STRICT_CHECK_BACKREF
)) {
5306 if (num
> env
->num_mem
|| IS_NULL(SCANENV_MEMENV(env
)[num
].node
))
5307 return ONIGERR_INVALID_BACKREF
;
5310 tok
->type
= TK_BACKREF
;
5311 tok
->u
.backref
.num
= 1;
5312 tok
->u
.backref
.ref1
= num
;
5313 tok
->u
.backref
.by_name
= 0;
5314 #ifdef USE_BACKREF_WITH_LEVEL
5315 tok
->u
.backref
.exist_level
= 0;
5321 if (c
== '8' || c
== '9') {
5330 if (IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ESC_OCTAL3
)) {
5332 num
= scan_unsigned_octal_number(&p
, end
, (c
== '0' ? 2:3), enc
);
5333 if (num
< 0 || num
>= 256) return ONIGERR_TOO_BIG_NUMBER
;
5334 if (p
== prev
) { /* can't read nothing. */
5335 num
= 0; /* but, it's not error */
5337 tok
->type
= TK_RAW_BYTE
;
5341 else if (c
!= '0') {
5347 if (!PEND
&& IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_K_NAMED_BACKREF
)) {
5349 if (c
== '<' || c
== '\'') {
5353 enum REF_NUM num_type
;
5357 #ifdef USE_BACKREF_WITH_LEVEL
5358 name_end
= NULL_UCHARP
; /* no need. escape gcc warning. */
5359 r
= fetch_name_with_level((OnigCodePoint
)c
, &p
, end
, &name_end
,
5360 env
, &back_num
, &tok
->u
.backref
.level
, &num_type
);
5361 if (r
== 1) tok
->u
.backref
.exist_level
= 1;
5362 else tok
->u
.backref
.exist_level
= 0;
5364 r
= fetch_name(c
, &p
, end
, &name_end
, env
, &back_num
, &num_type
, 1);
5366 if (r
< 0) return r
;
5368 if (num_type
!= IS_NOT_NUM
) {
5369 if (num_type
== IS_REL_NUM
) {
5370 back_num
= backref_rel_to_abs(back_num
, env
);
5373 return ONIGERR_INVALID_BACKREF
;
5375 if (IS_SYNTAX_BV(syn
, ONIG_SYN_STRICT_CHECK_BACKREF
)) {
5376 if (back_num
> env
->num_mem
||
5377 IS_NULL(SCANENV_MEMENV(env
)[back_num
].node
))
5378 return ONIGERR_INVALID_BACKREF
;
5380 tok
->type
= TK_BACKREF
;
5381 tok
->u
.backref
.by_name
= 0;
5382 tok
->u
.backref
.num
= 1;
5383 tok
->u
.backref
.ref1
= back_num
;
5386 num
= name_to_group_numbers(env
, prev
, name_end
, &backs
);
5388 return ONIGERR_UNDEFINED_NAME_REFERENCE
;
5390 if (IS_SYNTAX_BV(syn
, ONIG_SYN_STRICT_CHECK_BACKREF
)) {
5392 for (i
= 0; i
< num
; i
++) {
5393 if (backs
[i
] > env
->num_mem
||
5394 IS_NULL(SCANENV_MEMENV(env
)[backs
[i
]].node
))
5395 return ONIGERR_INVALID_BACKREF
;
5399 tok
->type
= TK_BACKREF
;
5400 tok
->u
.backref
.by_name
= 1;
5402 tok
->u
.backref
.num
= 1;
5403 tok
->u
.backref
.ref1
= backs
[0];
5406 tok
->u
.backref
.num
= num
;
5407 tok
->u
.backref
.refs
= backs
;
5418 if (!PEND
&& IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_G_SUBEXP_CALL
)) {
5420 if (c
== '<' || c
== '\'') {
5423 enum REF_NUM num_type
;
5426 r
= fetch_name((OnigCodePoint
)c
, &p
, end
, &name_end
, env
,
5427 &gnum
, &num_type
, 1);
5428 if (r
< 0) return r
;
5430 if (num_type
!= IS_NOT_NUM
) {
5431 if (num_type
== IS_REL_NUM
) {
5432 gnum
= backref_rel_to_abs(gnum
, env
);
5434 onig_scan_env_set_error_string(env
, ONIGERR_UNDEFINED_NAME_REFERENCE
,
5436 return ONIGERR_UNDEFINED_GROUP_REFERENCE
;
5439 tok
->u
.call
.by_number
= 1;
5440 tok
->u
.call
.gnum
= gnum
;
5443 tok
->u
.call
.by_number
= 0;
5444 tok
->u
.call
.gnum
= 0;
5447 tok
->type
= TK_CALL
;
5448 tok
->u
.call
.name
= prev
;
5449 tok
->u
.call
.name_end
= name_end
;
5458 if (IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_CAPITAL_Q_QUOTE
)) {
5459 tok
->type
= TK_QUOTE_OPEN
;
5465 if (!PEND
&& PPEEK_IS('{') &&
5466 IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_P_BRACE_CHAR_PROPERTY
)) {
5468 tok
->type
= TK_CHAR_PROPERTY
;
5469 tok
->u
.prop
.not = c
== 'P';
5472 IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_ESC_P_BRACE_CIRCUMFLEX_NOT
)) {
5475 tok
->u
.prop
.not = tok
->u
.prop
.not == 0;
5488 num
= fetch_escaped_value(&p
, end
, env
, &c2
);
5489 if (num
< 0) return num
;
5491 if (tok
->u
.c
!= c2
) {
5492 tok
->type
= TK_CODE_POINT
;
5496 p
= tok
->backp
+ enclen(enc
, tok
->backp
);
5506 #ifdef USE_VARIABLE_META_CHARS
5507 if ((c
!= ONIG_INEFFECTIVE_META_CHAR
) &&
5508 IS_SYNTAX_OP(syn
, ONIG_SYN_OP_VARIABLE_META_CHARACTERS
)) {
5509 if (c
== MC_ANYCHAR(syn
))
5511 else if (c
== MC_ANYTIME(syn
))
5513 else if (c
== MC_ZERO_OR_ONE_TIME(syn
))
5514 goto zero_or_one_time
;
5515 else if (c
== MC_ONE_OR_MORE_TIME(syn
))
5516 goto one_or_more_time
;
5517 else if (c
== MC_ANYCHAR_ANYTIME(syn
)) {
5518 tok
->type
= TK_ANYCHAR_ANYTIME
;
5526 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_DOT_ANYCHAR
)) break;
5527 #ifdef USE_VARIABLE_META_CHARS
5530 tok
->type
= TK_ANYCHAR
;
5534 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_ASTERISK_ZERO_INF
)) break;
5535 #ifdef USE_VARIABLE_META_CHARS
5538 tok
->type
= TK_REPEAT
;
5539 tok
->u
.repeat
.lower
= 0;
5540 tok
->u
.repeat
.upper
= INFINITE_REPEAT
;
5545 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_PLUS_ONE_INF
)) break;
5546 #ifdef USE_VARIABLE_META_CHARS
5549 tok
->type
= TK_REPEAT
;
5550 tok
->u
.repeat
.lower
= 1;
5551 tok
->u
.repeat
.upper
= INFINITE_REPEAT
;
5556 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_QMARK_ZERO_ONE
)) break;
5557 #ifdef USE_VARIABLE_META_CHARS
5560 tok
->type
= TK_REPEAT
;
5561 tok
->u
.repeat
.lower
= 0;
5562 tok
->u
.repeat
.upper
= 1;
5567 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_BRACE_INTERVAL
)) break;
5568 r
= fetch_interval_quantifier(&p
, end
, tok
, env
);
5569 if (r
< 0) return r
; /* error */
5570 if (r
== 0) goto greedy_check2
;
5571 else if (r
== 2) { /* {n} */
5572 if (IS_SYNTAX_BV(syn
, ONIG_SYN_FIXED_INTERVAL_IS_GREEDY_ONLY
))
5573 goto possessive_check
;
5577 /* r == 1 : normal char */
5581 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_VBAR_ALT
)) break;
5586 if (!PEND
&& PPEEK_IS('?') &&
5587 IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_QMARK_GROUP_EFFECT
)) {
5594 if (PEND
) return ONIGERR_END_PATTERN_IN_GROUP
;
5596 if (c
== MC_ESC(syn
)) {
5597 if (! PEND
) PFETCH(c
);
5600 if (c
== ')') break;
5605 else if (IS_SYNTAX_OP2(syn
, ONIG_SYN_OP2_QMARK_PERL_SUBEXP_CALL
)) {
5609 enum REF_NUM num_type
;
5616 r
= fetch_name((OnigCodePoint
)'(', &p
, end
, &name_end
, env
, &gnum
,
5618 if (r
< 0) return r
;
5620 tok
->type
= TK_CALL
;
5621 tok
->u
.call
.by_number
= 0;
5622 tok
->u
.call
.gnum
= 0;
5623 tok
->u
.call
.name
= name
;
5624 tok
->u
.call
.name_end
= name_end
;
5629 tok
->type
= TK_CALL
;
5630 tok
->u
.call
.by_number
= 1;
5631 tok
->u
.call
.gnum
= 0;
5632 tok
->u
.call
.name
= p
;
5634 if (! PPEEK_IS(')')) return ONIGERR_UNDEFINED_GROUP_OPTION
;
5635 tok
->u
.call
.name_end
= p
;
5640 goto lparen_qmark_num
;
5643 if (! ONIGENC_IS_CODE_DIGIT(enc
, c
)) goto lparen_qmark_end
;
5648 r
= fetch_name((OnigCodePoint
)'(', &p
, end
, &name_end
, env
,
5649 &gnum
, &num_type
, 1);
5650 if (r
< 0) return r
;
5652 if (num_type
== IS_NOT_NUM
) {
5653 return ONIGERR_INVALID_GROUP_NAME
;
5656 if (num_type
== IS_REL_NUM
) {
5657 gnum
= backref_rel_to_abs(gnum
, env
);
5659 onig_scan_env_set_error_string(env
,
5660 ONIGERR_UNDEFINED_NAME_REFERENCE
, name
, name_end
);
5661 return ONIGERR_UNDEFINED_GROUP_REFERENCE
;
5664 tok
->u
.call
.by_number
= 1;
5665 tok
->u
.call
.gnum
= gnum
;
5668 tok
->type
= TK_CALL
;
5669 tok
->u
.call
.name
= name
;
5670 tok
->u
.call
.name_end
= name_end
;
5680 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_LPAREN_SUBEXP
)) break;
5681 tok
->type
= TK_SUBEXP_OPEN
;
5685 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_LPAREN_SUBEXP
)) break;
5686 tok
->type
= TK_SUBEXP_CLOSE
;
5690 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_LINE_ANCHOR
)) break;
5691 tok
->type
= TK_ANCHOR
;
5692 tok
->u
.subtype
= (IS_SINGLELINE(env
->options
)
5693 ? ANCR_BEGIN_BUF
: ANCR_BEGIN_LINE
);
5697 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_LINE_ANCHOR
)) break;
5698 tok
->type
= TK_ANCHOR
;
5699 tok
->u
.subtype
= (IS_SINGLELINE(env
->options
)
5700 ? ANCR_SEMI_END_BUF
: ANCR_END_LINE
);
5704 if (! IS_SYNTAX_OP(syn
, ONIG_SYN_OP_BRACKET_CC
)) break;
5705 tok
->type
= TK_CC_OPEN
;
5709 if (*src
> env
->pattern
) /* /].../ is allowed. */
5710 CLOSE_BRACKET_WITHOUT_ESC_WARN(env
, (UChar
* )"]");
5714 if (IS_EXTEND(env
->options
)) {
5717 if (ONIGENC_IS_CODE_NEWLINE(enc
, c
))
5725 case ' ': case '\t': case '\n': case '\r': case '\f':
5726 if (IS_EXTEND(env
->options
))
5736 #ifdef USE_VARIABLE_META_CHARS
5744 add_ctype_to_cc_by_range(CClassNode
* cc
, int ctype ARG_UNUSED
, int not,
5745 OnigEncoding enc ARG_UNUSED
, OnigCodePoint sb_out
,
5746 const OnigCodePoint mbr
[])
5751 int n
= ONIGENC_CODE_RANGE_NUM(mbr
);
5754 for (i
= 0; i
< n
; i
++) {
5755 for (j
= ONIGENC_CODE_RANGE_FROM(mbr
, i
);
5756 j
<= ONIGENC_CODE_RANGE_TO(mbr
, i
); j
++) {
5758 if (j
> ONIGENC_CODE_RANGE_FROM(mbr
, i
)) {
5759 r
= add_code_range_to_buf(&(cc
->mbuf
), j
,
5760 ONIGENC_CODE_RANGE_TO(mbr
, i
));
5761 if (r
!= 0) return r
;
5767 BITSET_SET_BIT(cc
->bs
, j
);
5772 for ( ; i
< n
; i
++) {
5773 r
= add_code_range_to_buf(&(cc
->mbuf
),
5774 ONIGENC_CODE_RANGE_FROM(mbr
, i
),
5775 ONIGENC_CODE_RANGE_TO(mbr
, i
));
5776 if (r
!= 0) return r
;
5780 OnigCodePoint prev
= 0;
5782 for (i
= 0; i
< n
; i
++) {
5783 for (j
= prev
; j
< ONIGENC_CODE_RANGE_FROM(mbr
, i
); j
++) {
5787 BITSET_SET_BIT(cc
->bs
, j
);
5789 prev
= ONIGENC_CODE_RANGE_TO(mbr
, i
) + 1;
5791 for (j
= prev
; j
< sb_out
; j
++) {
5792 BITSET_SET_BIT(cc
->bs
, j
);
5798 for (i
= 0; i
< n
; i
++) {
5799 if (prev
< ONIGENC_CODE_RANGE_FROM(mbr
, i
)) {
5800 r
= add_code_range_to_buf(&(cc
->mbuf
), prev
,
5801 ONIGENC_CODE_RANGE_FROM(mbr
, i
) - 1);
5802 if (r
!= 0) return r
;
5804 prev
= ONIGENC_CODE_RANGE_TO(mbr
, i
) + 1;
5805 if (prev
== 0) goto end
;
5808 r
= add_code_range_to_buf(&(cc
->mbuf
), prev
, MAX_CODE_POINT
);
5809 if (r
!= 0) return r
;
5817 add_ctype_to_cc_by_range_limit(CClassNode
* cc
, int ctype ARG_UNUSED
, int not,
5818 OnigEncoding enc ARG_UNUSED
,
5819 OnigCodePoint sb_out
,
5820 const OnigCodePoint mbr
[], OnigCodePoint limit
)
5827 int n
= ONIGENC_CODE_RANGE_NUM(mbr
);
5830 for (i
= 0; i
< n
; i
++) {
5831 for (j
= ONIGENC_CODE_RANGE_FROM(mbr
, i
);
5832 j
<= ONIGENC_CODE_RANGE_TO(mbr
, i
); j
++) {
5833 if (j
> limit
) goto end
;
5835 if (j
> ONIGENC_CODE_RANGE_FROM(mbr
, i
)) {
5836 to
= ONIGENC_CODE_RANGE_TO(mbr
, i
);
5837 if (to
> limit
) to
= limit
;
5838 r
= add_code_range_to_buf(&(cc
->mbuf
), j
, to
);
5839 if (r
!= 0) return r
;
5845 BITSET_SET_BIT(cc
->bs
, j
);
5850 for ( ; i
< n
; i
++) {
5851 from
= ONIGENC_CODE_RANGE_FROM(mbr
, i
);
5852 to
= ONIGENC_CODE_RANGE_TO(mbr
, i
);
5853 if (from
> limit
) break;
5854 if (to
> limit
) to
= limit
;
5855 r
= add_code_range_to_buf(&(cc
->mbuf
), from
, to
);
5856 if (r
!= 0) return r
;
5860 OnigCodePoint prev
= 0;
5862 for (i
= 0; i
< n
; i
++) {
5863 from
= ONIGENC_CODE_RANGE_FROM(mbr
, i
);
5865 for (j
= prev
; j
< sb_out
; j
++) {
5866 BITSET_SET_BIT(cc
->bs
, j
);
5870 for (j
= prev
; j
< from
; j
++) {
5871 if (j
>= sb_out
) goto sb_end2
;
5872 BITSET_SET_BIT(cc
->bs
, j
);
5874 prev
= ONIGENC_CODE_RANGE_TO(mbr
, i
);
5875 if (prev
> limit
) prev
= limit
;
5877 if (prev
== 0) goto end
;
5879 for (j
= prev
; j
< sb_out
; j
++) {
5880 BITSET_SET_BIT(cc
->bs
, j
);
5886 for (i
= 0; i
< n
; i
++) {
5887 from
= ONIGENC_CODE_RANGE_FROM(mbr
, i
);
5888 if (from
> limit
) goto last
;
5891 r
= add_code_range_to_buf(&(cc
->mbuf
), prev
, from
- 1);
5892 if (r
!= 0) return r
;
5894 prev
= ONIGENC_CODE_RANGE_TO(mbr
, i
);
5895 if (prev
> limit
) prev
= limit
;
5897 if (prev
== 0) goto end
;
5901 r
= add_code_range_to_buf(&(cc
->mbuf
), prev
, MAX_CODE_POINT
);
5902 if (r
!= 0) return r
;
5910 add_ctype_to_cc(CClassNode
* cc
, int ctype
, int not, ScanEnv
* env
)
5912 #define ASCII_LIMIT 127
5916 const OnigCodePoint
*ranges
;
5917 OnigCodePoint limit
;
5918 OnigCodePoint sb_out
;
5919 OnigEncoding enc
= env
->enc
;
5921 ascii_mode
= IS_ASCII_MODE_CTYPE_OPTION(ctype
, env
->options
);
5923 r
= ONIGENC_GET_CTYPE_CODE_RANGE(enc
, ctype
, &sb_out
, &ranges
);
5925 if (ascii_mode
== 0)
5926 r
= add_ctype_to_cc_by_range(cc
, ctype
, not, env
->enc
, sb_out
, ranges
);
5928 r
= add_ctype_to_cc_by_range_limit(cc
, ctype
, not, env
->enc
, sb_out
,
5929 ranges
, ASCII_LIMIT
);
5932 else if (r
!= ONIG_NO_SUPPORT_CONFIG
) {
5937 limit
= ascii_mode
? ASCII_LIMIT
: SINGLE_BYTE_SIZE
;
5940 case ONIGENC_CTYPE_ALPHA
:
5941 case ONIGENC_CTYPE_BLANK
:
5942 case ONIGENC_CTYPE_CNTRL
:
5943 case ONIGENC_CTYPE_DIGIT
:
5944 case ONIGENC_CTYPE_LOWER
:
5945 case ONIGENC_CTYPE_PUNCT
:
5946 case ONIGENC_CTYPE_SPACE
:
5947 case ONIGENC_CTYPE_UPPER
:
5948 case ONIGENC_CTYPE_XDIGIT
:
5949 case ONIGENC_CTYPE_ASCII
:
5950 case ONIGENC_CTYPE_ALNUM
:
5952 for (c
= 0; c
< (int )limit
; c
++) {
5953 if (! ONIGENC_IS_CODE_CTYPE(enc
, (OnigCodePoint
)c
, ctype
))
5954 BITSET_SET_BIT(cc
->bs
, c
);
5956 for (c
= limit
; c
< SINGLE_BYTE_SIZE
; c
++) {
5957 BITSET_SET_BIT(cc
->bs
, c
);
5960 ADD_ALL_MULTI_BYTE_RANGE(enc
, cc
->mbuf
);
5963 for (c
= 0; c
< (int )limit
; c
++) {
5964 if (ONIGENC_IS_CODE_CTYPE(enc
, (OnigCodePoint
)c
, ctype
))
5965 BITSET_SET_BIT(cc
->bs
, c
);
5970 case ONIGENC_CTYPE_GRAPH
:
5971 case ONIGENC_CTYPE_PRINT
:
5972 case ONIGENC_CTYPE_WORD
:
5974 for (c
= 0; c
< (int )limit
; c
++) {
5975 if (ONIGENC_CODE_TO_MBCLEN(enc
, c
) > 0 /* check invalid code point */
5976 && ! ONIGENC_IS_CODE_CTYPE(enc
, (OnigCodePoint
)c
, ctype
))
5977 BITSET_SET_BIT(cc
->bs
, c
);
5979 for (c
= limit
; c
< SINGLE_BYTE_SIZE
; c
++) {
5980 if (ONIGENC_CODE_TO_MBCLEN(enc
, c
) > 0)
5981 BITSET_SET_BIT(cc
->bs
, c
);
5985 for (c
= 0; c
< (int )limit
; c
++) {
5986 if (ONIGENC_IS_CODE_CTYPE(enc
, (OnigCodePoint
)c
, ctype
))
5987 BITSET_SET_BIT(cc
->bs
, c
);
5989 if (ascii_mode
== 0)
5990 ADD_ALL_MULTI_BYTE_RANGE(enc
, cc
->mbuf
);
5995 return ONIGERR_PARSER_BUG
;
6003 parse_posix_bracket(CClassNode
* cc
, UChar
** src
, UChar
* end
, ScanEnv
* env
)
6005 #define POSIX_BRACKET_CHECK_LIMIT_LENGTH 20
6006 #define POSIX_BRACKET_NAME_MIN_LEN 4
6008 static PosixBracketEntryType PBS
[] = {
6009 { (UChar
* )"alnum", ONIGENC_CTYPE_ALNUM
, 5 },
6010 { (UChar
* )"alpha", ONIGENC_CTYPE_ALPHA
, 5 },
6011 { (UChar
* )"blank", ONIGENC_CTYPE_BLANK
, 5 },
6012 { (UChar
* )"cntrl", ONIGENC_CTYPE_CNTRL
, 5 },
6013 { (UChar
* )"digit", ONIGENC_CTYPE_DIGIT
, 5 },
6014 { (UChar
* )"graph", ONIGENC_CTYPE_GRAPH
, 5 },
6015 { (UChar
* )"lower", ONIGENC_CTYPE_LOWER
, 5 },
6016 { (UChar
* )"print", ONIGENC_CTYPE_PRINT
, 5 },
6017 { (UChar
* )"punct", ONIGENC_CTYPE_PUNCT
, 5 },
6018 { (UChar
* )"space", ONIGENC_CTYPE_SPACE
, 5 },
6019 { (UChar
* )"upper", ONIGENC_CTYPE_UPPER
, 5 },
6020 { (UChar
* )"xdigit", ONIGENC_CTYPE_XDIGIT
, 6 },
6021 { (UChar
* )"ascii", ONIGENC_CTYPE_ASCII
, 5 },
6022 { (UChar
* )"word", ONIGENC_CTYPE_WORD
, 4 },
6023 { (UChar
* )NULL
, -1, 0 }
6026 PosixBracketEntryType
*pb
;
6029 OnigEncoding enc
= env
->enc
;
6032 if (PPEEK_IS('^')) {
6039 if (onigenc_strlen(enc
, p
, end
) < POSIX_BRACKET_NAME_MIN_LEN
+ 3)
6040 goto not_posix_bracket
;
6042 for (pb
= PBS
; IS_NOT_NULL(pb
->name
); pb
++) {
6043 if (onigenc_with_ascii_strncmp(enc
, p
, end
, pb
->name
, pb
->len
) == 0) {
6044 p
= (UChar
* )onigenc_step(enc
, p
, end
, pb
->len
);
6045 if (onigenc_with_ascii_strncmp(enc
, p
, end
, (UChar
* )":]", 2) != 0)
6046 return ONIGERR_INVALID_POSIX_BRACKET_TYPE
;
6048 r
= add_ctype_to_cc(cc
, pb
->ctype
, not, env
);
6049 if (r
!= 0) return r
;
6060 while (!PEND
&& ((c
= PPEEK
) != ':') && c
!= ']') {
6062 if (++i
> POSIX_BRACKET_CHECK_LIMIT_LENGTH
) break;
6064 if (c
== ':' && ! PEND
) {
6069 return ONIGERR_INVALID_POSIX_BRACKET_TYPE
;
6073 return 1; /* 1: is not POSIX bracket, but no error. */
6077 fetch_char_property_to_ctype(UChar
** src
, UChar
* end
, ScanEnv
* env
)
6081 OnigEncoding enc
= env
->enc
;
6082 UChar
*prev
, *start
, *p
= *src
;
6091 r
= ONIGENC_PROPERTY_NAME_TO_CTYPE(enc
, start
, prev
);
6097 else if (c
== '(' || c
== ')' || c
== '{' || c
== '|') {
6098 r
= ONIGERR_INVALID_CHAR_PROPERTY_NAME
;
6103 onig_scan_env_set_error_string(env
, r
, *src
, prev
);
6108 parse_char_property(Node
** np
, PToken
* tok
, UChar
** src
, UChar
* end
, ScanEnv
* env
)
6113 ctype
= fetch_char_property_to_ctype(src
, end
, env
);
6114 if (ctype
< 0) return ctype
;
6116 *np
= node_new_cclass();
6117 CHECK_NULL_RETURN_MEMERR(*np
);
6119 r
= add_ctype_to_cc(cc
, ctype
, 0, env
);
6120 if (r
!= 0) return r
;
6121 if (tok
->u
.prop
.not != 0) NCCLASS_SET_NOT(cc
);
6141 next_state_class(CClassNode
* cc
, OnigCodePoint
* vs
, enum CCVALTYPE
* type
,
6142 enum CCSTATE
* state
, ScanEnv
* env
)
6146 if (*state
== CCS_RANGE
)
6147 return ONIGERR_CHAR_CLASS_VALUE_AT_END_OF_RANGE
;
6149 if (*state
== CCS_VALUE
&& *type
!= CCV_CLASS
) {
6150 if (*type
== CCV_SB
)
6151 BITSET_SET_BIT(cc
->bs
, (int )(*vs
));
6152 else if (*type
== CCV_CODE_POINT
) {
6153 r
= add_code_range(&(cc
->mbuf
), env
, *vs
, *vs
);
6154 if (r
< 0) return r
;
6164 next_state_val(CClassNode
* cc
, OnigCodePoint
*from
, OnigCodePoint to
,
6165 int* from_israw
, int to_israw
,
6166 enum CCVALTYPE intype
, enum CCVALTYPE
* type
,
6167 enum CCSTATE
* state
, ScanEnv
* env
)
6173 if (*type
== CCV_SB
) {
6175 return ONIGERR_INVALID_CODE_POINT_VALUE
;
6177 BITSET_SET_BIT(cc
->bs
, (int )(*from
));
6179 else if (*type
== CCV_CODE_POINT
) {
6180 r
= add_code_range(&(cc
->mbuf
), env
, *from
, *from
);
6181 if (r
< 0) return r
;
6186 if (intype
== *type
) {
6187 if (intype
== CCV_SB
) {
6188 if (*from
> 0xff || to
> 0xff)
6189 return ONIGERR_INVALID_CODE_POINT_VALUE
;
6192 if (IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_ALLOW_EMPTY_RANGE_IN_CC
))
6195 return ONIGERR_EMPTY_RANGE_IN_CHAR_CLASS
;
6197 bitset_set_range(cc
->bs
, (int )*from
, (int )to
);
6200 r
= add_code_range(&(cc
->mbuf
), env
, *from
, to
);
6201 if (r
< 0) return r
;
6206 if (IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_ALLOW_EMPTY_RANGE_IN_CC
))
6209 return ONIGERR_EMPTY_RANGE_IN_CHAR_CLASS
;
6211 bitset_set_range(cc
->bs
, (int )*from
, (int )(to
< 0xff ? to
: 0xff));
6212 r
= add_code_range(&(cc
->mbuf
), env
, (OnigCodePoint
)*from
, to
);
6213 if (r
< 0) return r
;
6216 *state
= CCS_COMPLETE
;
6228 *from_israw
= to_israw
;
6235 code_exist_check(OnigCodePoint c
, UChar
* from
, UChar
* end
, int ignore_escaped
,
6240 OnigEncoding enc
= env
->enc
;
6245 if (ignore_escaped
&& in_esc
) {
6250 if (code
== c
) return 1;
6251 if (code
== MC_ESC(env
->syntax
)) in_esc
= 1;
6258 parse_char_class(Node
** np
, PToken
* tok
, UChar
** src
, UChar
* end
, ScanEnv
* env
)
6260 int r
, neg
, len
, fetched
, and_start
;
6261 OnigCodePoint v
, vs
;
6264 CClassNode
*cc
, *prev_cc
;
6268 enum CCVALTYPE val_type
, in_type
;
6269 int val_israw
, in_israw
;
6273 if (env
->parse_depth
> ParseDepthLimit
)
6274 return ONIGERR_PARSE_DEPTH_LIMIT_OVER
;
6276 prev_cc
= (CClassNode
* )NULL
;
6277 r
= fetch_token_in_cc(tok
, src
, end
, env
);
6278 if (r
== TK_CHAR
&& tok
->u
.c
== '^' && tok
->escaped
== 0) {
6280 r
= fetch_token_in_cc(tok
, src
, end
, env
);
6286 if (r
< 0) return r
;
6287 if (r
== TK_CC_CLOSE
) {
6288 if (! code_exist_check((OnigCodePoint
)']',
6289 *src
, env
->pattern_end
, 1, env
))
6290 return ONIGERR_EMPTY_CHAR_CLASS
;
6292 CC_ESC_WARN(env
, (UChar
* )"]");
6293 r
= tok
->type
= TK_CHAR
; /* allow []...] */
6296 *np
= node
= node_new_cclass();
6297 CHECK_NULL_RETURN_MEMERR(node
);
6303 while (r
!= TK_CC_CLOSE
) {
6308 len
= ONIGENC_CODE_TO_MBCLEN(env
->enc
, tok
->u
.c
);
6310 in_type
= CCV_CODE_POINT
;
6320 v
= (OnigCodePoint
)tok
->u
.c
;
6326 /* tok->base != 0 : octal or hexadec. */
6327 if (! ONIGENC_IS_SINGLEBYTE(env
->enc
) && tok
->base
!= 0) {
6329 UChar buf
[ONIGENC_CODE_TO_MBC_MAXLEN
];
6330 UChar
* bufe
= buf
+ ONIGENC_CODE_TO_MBC_MAXLEN
;
6332 int base
= tok
->base
;
6335 for (i
= 1; i
< ONIGENC_MBC_MAXLEN(env
->enc
); i
++) {
6336 r
= fetch_token_in_cc(tok
, &p
, end
, env
);
6337 if (r
< 0) goto err
;
6338 if (r
!= TK_RAW_BYTE
|| tok
->base
!= base
) {
6345 if (i
< ONIGENC_MBC_MINLEN(env
->enc
)) {
6346 r
= ONIGERR_TOO_SHORT_MULTI_BYTE_STRING
;
6350 /* clear buf tail */
6351 for (j
= i
; j
< ONIGENC_CODE_TO_MBC_MAXLEN
; j
++) buf
[j
] = '\0';
6353 len
= enclen(env
->enc
, buf
);
6355 r
= ONIGERR_TOO_SHORT_MULTI_BYTE_STRING
;
6358 else if (i
> len
) { /* fetch back */
6360 for (i
= 1; i
< len
; i
++) {
6361 r
= fetch_token_in_cc(tok
, &p
, end
, env
);
6367 v
= (OnigCodePoint
)buf
[0];
6371 v
= ONIGENC_MBC_TO_CODE(env
->enc
, buf
, bufe
);
6372 in_type
= CCV_CODE_POINT
;
6376 v
= (OnigCodePoint
)tok
->u
.c
;
6388 len
= ONIGENC_CODE_TO_MBCLEN(env
->enc
, v
);
6390 if (state
!= CCS_RANGE
||
6391 ! IS_SYNTAX_BV(env
->syntax
,
6392 ONIG_SYN_ALLOW_INVALID_CODE_END_OF_RANGE_IN_CC
) ||
6393 v
< 0x100 || ONIGENC_MBC_MAXLEN(env
->enc
) == 1) {
6398 in_type
= (len
== 1 ? CCV_SB
: CCV_CODE_POINT
);
6400 r
= next_state_val(cc
, &vs
, v
, &val_israw
, in_israw
, in_type
, &val_type
,
6402 if (r
!= 0) goto err
;
6405 case TK_POSIX_BRACKET_OPEN
:
6406 r
= parse_posix_bracket(cc
, &p
, end
, env
);
6407 if (r
< 0) goto err
;
6408 if (r
== 1) { /* is not POSIX bracket */
6409 CC_ESC_WARN(env
, (UChar
* )"[");
6411 v
= (OnigCodePoint
)tok
->u
.c
;
6419 r
= add_ctype_to_cc(cc
, tok
->u
.prop
.ctype
, tok
->u
.prop
.not, env
);
6420 if (r
!= 0) goto err
;
6423 r
= next_state_class(cc
, &vs
, &val_type
, &state
, env
);
6424 if (r
!= 0) goto err
;
6427 case TK_CHAR_PROPERTY
:
6429 int ctype
= fetch_char_property_to_ctype(&p
, end
, env
);
6434 r
= add_ctype_to_cc(cc
, ctype
, tok
->u
.prop
.not, env
);
6435 if (r
!= 0) goto err
;
6441 if (state
== CCS_VALUE
) {
6442 r
= fetch_token_in_cc(tok
, &p
, end
, env
);
6443 if (r
< 0) goto err
;
6445 if (r
== TK_CC_CLOSE
) { /* allow [x-] */
6447 v
= (OnigCodePoint
)'-';
6451 else if (r
== TK_CC_AND
) {
6452 CC_ESC_WARN(env
, (UChar
* )"-");
6456 if (val_type
== CCV_CLASS
) {
6457 r
= ONIGERR_UNMATCHED_RANGE_SPECIFIER_IN_CHAR_CLASS
;
6463 else if (state
== CCS_START
) {
6464 /* [-xa] is allowed */
6465 v
= (OnigCodePoint
)tok
->u
.c
;
6468 r
= fetch_token_in_cc(tok
, &p
, end
, env
);
6469 if (r
< 0) goto err
;
6471 /* [--x] or [a&&-x] is warned. */
6472 if (r
== TK_CC_RANGE
|| and_start
!= 0)
6473 CC_ESC_WARN(env
, (UChar
* )"-");
6477 else if (state
== CCS_RANGE
) {
6478 CC_ESC_WARN(env
, (UChar
* )"-");
6479 goto any_char_in
; /* [!--x] is allowed */
6481 else { /* CCS_COMPLETE */
6482 r
= fetch_token_in_cc(tok
, &p
, end
, env
);
6483 if (r
< 0) goto err
;
6485 if (r
== TK_CC_CLOSE
) goto range_end_val
; /* allow [a-b-] */
6486 else if (r
== TK_CC_AND
) {
6487 CC_ESC_WARN(env
, (UChar
* )"-");
6491 if (IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_ALLOW_DOUBLE_RANGE_OP_IN_CC
)) {
6492 CC_ESC_WARN(env
, (UChar
* )"-");
6493 goto range_end_val
; /* [0-9-a] is allowed as [0-9\-a] */
6495 r
= ONIGERR_UNMATCHED_RANGE_SPECIFIER_IN_CHAR_CLASS
;
6500 case TK_CC_CC_OPEN
: /* [ */
6505 r
= parse_char_class(&anode
, tok
, &p
, end
, env
);
6507 onig_node_free(anode
);
6510 acc
= CCLASS_(anode
);
6511 r
= or_cclass(cc
, acc
, env
->enc
);
6512 onig_node_free(anode
);
6515 if (r
!= 0) goto err
;
6519 case TK_CC_AND
: /* && */
6521 if (state
== CCS_VALUE
) {
6522 r
= next_state_val(cc
, &vs
, 0, &val_israw
, 0, val_type
,
6523 &val_type
, &state
, env
);
6524 if (r
!= 0) goto err
;
6526 /* initialize local variables */
6530 if (IS_NOT_NULL(prev_cc
)) {
6531 r
= and_cclass(prev_cc
, cc
, env
->enc
);
6532 if (r
!= 0) goto err
;
6533 bbuf_free(cc
->mbuf
);
6539 initialize_cclass(cc
);
6544 r
= ONIGERR_PREMATURE_END_OF_CHAR_CLASS
;
6548 r
= ONIGERR_PARSER_BUG
;
6556 r
= fetch_token_in_cc(tok
, &p
, end
, env
);
6557 if (r
< 0) goto err
;
6561 if (state
== CCS_VALUE
) {
6562 r
= next_state_val(cc
, &vs
, 0, &val_israw
, 0, val_type
,
6563 &val_type
, &state
, env
);
6564 if (r
!= 0) goto err
;
6567 if (IS_NOT_NULL(prev_cc
)) {
6568 r
= and_cclass(prev_cc
, cc
, env
->enc
);
6569 if (r
!= 0) goto err
;
6570 bbuf_free(cc
->mbuf
);
6575 NCCLASS_SET_NOT(cc
);
6577 NCCLASS_CLEAR_NOT(cc
);
6578 if (IS_NCCLASS_NOT(cc
) &&
6579 IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_NOT_NEWLINE_IN_NEGATIVE_CC
)) {
6580 int is_empty
= (IS_NULL(cc
->mbuf
) ? 1 : 0);
6582 BITSET_IS_EMPTY(cc
->bs
, is_empty
);
6584 if (is_empty
== 0) {
6585 #define NEWLINE_CODE 0x0a
6587 if (ONIGENC_IS_CODE_NEWLINE(env
->enc
, NEWLINE_CODE
)) {
6588 if (ONIGENC_CODE_TO_MBCLEN(env
->enc
, NEWLINE_CODE
) == 1)
6589 BITSET_SET_BIT(cc
->bs
, NEWLINE_CODE
);
6591 add_code_range(&(cc
->mbuf
), env
, NEWLINE_CODE
, NEWLINE_CODE
);
6600 if (cc
!= CCLASS_(*np
))
6601 bbuf_free(cc
->mbuf
);
6605 static int parse_subexp(Node
** top
, PToken
* tok
, int term
,
6606 UChar
** src
, UChar
* end
, ScanEnv
* env
, int group_head
);
6610 /* (?{...}[tag][+-]) (?{{...}}[tag][+-]) */
6612 parse_callout_of_contents(Node
** np
, int cterm
, UChar
** src
, UChar
* end
, ScanEnv
* env
)
6625 CalloutListEntry
* e
;
6627 OnigEncoding enc
= env
->enc
;
6630 if (PEND
) return ONIGERR_INVALID_CALLOUT_PATTERN
;
6633 while (PPEEK_IS('{')) {
6636 if (PEND
) return ONIGERR_INVALID_CALLOUT_PATTERN
;
6639 in
= ONIG_CALLOUT_IN_PROGRESS
;
6642 if (PEND
) return ONIGERR_INVALID_CALLOUT_PATTERN
;
6649 if (PEND
) return ONIGERR_INVALID_CALLOUT_PATTERN
;
6658 if (PEND
) return ONIGERR_END_PATTERN_IN_GROUP
;
6662 if (PEND
) return ONIGERR_END_PATTERN_IN_GROUP
;
6663 tag_end
= tag_start
= p
;
6665 if (PEND
) return ONIGERR_END_PATTERN_IN_GROUP
;
6668 if (c
== ']') break;
6670 if (! is_allowed_callout_tag_name(enc
, tag_start
, tag_end
))
6671 return ONIGERR_INVALID_CALLOUT_TAG_NAME
;
6673 if (PEND
) return ONIGERR_END_PATTERN_IN_GROUP
;
6677 tag_start
= tag_end
= 0;
6681 in
|= ONIG_CALLOUT_IN_RETRACTION
;
6682 if (PEND
) return ONIGERR_END_PATTERN_IN_GROUP
;
6685 else if (c
== '<') {
6686 in
= ONIG_CALLOUT_IN_RETRACTION
;
6687 if (PEND
) return ONIGERR_END_PATTERN_IN_GROUP
;
6690 else if (c
== '>') { /* no needs (default) */
6691 if (PEND
) return ONIGERR_END_PATTERN_IN_GROUP
;
6696 return ONIGERR_INVALID_CALLOUT_PATTERN
;
6698 r
= reg_callout_list_entry(env
, &num
);
6699 if (r
!= 0) return r
;
6701 ext
= onig_get_regex_ext(env
->reg
);
6702 CHECK_NULL_RETURN_MEMERR(ext
);
6703 if (IS_NULL(ext
->pattern
)) {
6704 r
= onig_ext_set_pattern(env
->reg
, env
->pattern
, env
->pattern_end
);
6705 if (r
!= ONIG_NORMAL
) return r
;
6708 if (tag_start
!= tag_end
) {
6709 r
= callout_tag_entry(env
, env
->reg
, tag_start
, tag_end
, num
);
6710 if (r
!= ONIG_NORMAL
) return r
;
6713 contents
= onigenc_strdup(enc
, code_start
, code_end
);
6714 CHECK_NULL_RETURN_MEMERR(contents
);
6716 r
= node_new_callout(np
, ONIG_CALLOUT_OF_CONTENTS
, num
, ONIG_NON_NAME_ID
, env
);
6722 e
= onig_reg_callout_list_at(env
->reg
, num
);
6725 return ONIGERR_MEMORY
;
6728 e
->of
= ONIG_CALLOUT_OF_CONTENTS
;
6730 e
->name_id
= ONIG_NON_NAME_ID
;
6731 e
->u
.content
.start
= contents
;
6732 e
->u
.content
.end
= contents
+ (code_end
- code_start
);
6739 parse_long(OnigEncoding enc
, UChar
* s
, UChar
* end
, int sign_on
, long max
, long* rl
)
6747 if (s
>= end
) return ONIGERR_INVALID_CALLOUT_ARG
;
6753 c
= ONIGENC_MBC_TO_CODE(enc
, p
, end
);
6754 p
+= ONIGENC_MBC_ENC_LEN(enc
, p
);
6755 if (c
>= '0' && c
<= '9') {
6756 d
= (long )(c
- '0');
6757 if (v
> (max
- d
) / 10)
6758 return ONIGERR_INVALID_CALLOUT_ARG
;
6762 else if (sign_on
!= 0 && (c
== '-' || c
== '+')) {
6763 if (c
== '-') flag
= -1;
6766 return ONIGERR_INVALID_CALLOUT_ARG
;
6776 parse_callout_args(int skip_mode
, int cterm
, UChar
** src
, UChar
* end
,
6777 unsigned int types
[], OnigValue vals
[], ScanEnv
* env
)
6779 #define MAX_CALLOUT_ARG_BYTE_LENGTH 128
6790 UChar buf
[MAX_CALLOUT_ARG_BYTE_LENGTH
];
6791 OnigEncoding enc
= env
->enc
;
6794 if (PEND
) return ONIGERR_INVALID_CALLOUT_PATTERN
;
6797 while (n
< ONIG_CALLOUT_MAX_ARGS_NUM
) {
6805 if (PEND
) return ONIGERR_INVALID_CALLOUT_PATTERN
;
6811 if (c
== '\\' || c
== cterm
|| c
== ',') {
6825 else if (c
== cterm
|| c
== ',')
6831 if (skip_mode
== 0) {
6833 if (bufend
+ clen
> buf
+ MAX_CALLOUT_ARG_BYTE_LENGTH
)
6834 return ONIGERR_INVALID_CALLOUT_ARG
; /* too long argument */
6836 xmemcpy(bufend
, e
, clen
);
6845 if (skip_mode
== 0) {
6846 if ((types
[n
] & ONIG_TYPE_LONG
) != 0) {
6850 r
= parse_long(enc
, buf
, bufend
, 1, LONG_MAX
, &rl
);
6851 if (r
== ONIG_NORMAL
) {
6854 types
[n
] = ONIG_TYPE_LONG
;
6859 types
[n
] = (types
[n
] & ~ONIG_TYPE_LONG
);
6860 if (types
[n
] == ONIG_TYPE_VOID
)
6861 return ONIGERR_INVALID_CALLOUT_ARG
;
6866 case ONIG_TYPE_LONG
:
6869 case ONIG_TYPE_CHAR
:
6870 if (cn
!= 1) return ONIGERR_INVALID_CALLOUT_ARG
;
6871 vals
[n
].c
= ONIGENC_MBC_TO_CODE(enc
, buf
, bufend
);
6874 case ONIG_TYPE_STRING
:
6876 UChar
* rs
= onigenc_strdup(enc
, buf
, bufend
);
6877 CHECK_NULL_RETURN_MEMERR(rs
);
6878 vals
[n
].s
.start
= rs
;
6879 vals
[n
].s
.end
= rs
+ (e
- s
);
6884 if (eesc
!= 0 || ! is_allowed_callout_tag_name(enc
, s
, e
))
6885 return ONIGERR_INVALID_CALLOUT_TAG_NAME
;
6887 vals
[n
].s
.start
= s
;
6891 case ONIG_TYPE_VOID
:
6892 case ONIG_TYPE_POINTER
:
6893 return ONIGERR_PARSER_BUG
;
6901 if (c
== cterm
) break;
6904 if (c
!= cterm
) return ONIGERR_INVALID_CALLOUT_PATTERN
;
6910 /* (*name[TAG]) (*name[TAG]{a,b,..}) */
6912 parse_callout_of_name(Node
** np
, int cterm
, UChar
** src
, UChar
* end
, ScanEnv
* env
)
6929 CalloutListEntry
* e
;
6931 unsigned int types
[ONIG_CALLOUT_MAX_ARGS_NUM
];
6932 OnigValue vals
[ONIG_CALLOUT_MAX_ARGS_NUM
];
6933 OnigEncoding enc
= env
->enc
;
6937 if (PEND
) return ONIGERR_INVALID_CALLOUT_PATTERN
;
6942 if (PEND
) return ONIGERR_END_PATTERN_IN_GROUP
;
6945 if (c
== cterm
|| c
== '[' || c
== '{') break;
6948 if (! is_allowed_callout_name(enc
, name_start
, name_end
))
6949 return ONIGERR_INVALID_CALLOUT_NAME
;
6952 if (PEND
) return ONIGERR_END_PATTERN_IN_GROUP
;
6953 tag_end
= tag_start
= p
;
6955 if (PEND
) return ONIGERR_END_PATTERN_IN_GROUP
;
6958 if (c
== ']') break;
6960 if (! is_allowed_callout_tag_name(enc
, tag_start
, tag_end
))
6961 return ONIGERR_INVALID_CALLOUT_TAG_NAME
;
6963 if (PEND
) return ONIGERR_END_PATTERN_IN_GROUP
;
6967 tag_start
= tag_end
= 0;
6973 if (PEND
) return ONIGERR_END_PATTERN_IN_GROUP
;
6975 /* read for single check only */
6977 arg_num
= parse_callout_args(1, '}', &p
, end
, 0, 0, env
);
6978 if (arg_num
< 0) return arg_num
;
6980 is_not_single
= PPEEK_IS(cterm
) ? 0 : 1;
6982 r
= get_callout_name_id_by_name(enc
, is_not_single
, name_start
, name_end
,
6984 if (r
!= ONIG_NORMAL
) return r
;
6986 max_arg_num
= get_callout_arg_num_by_name_id(name_id
);
6987 for (i
= 0; i
< max_arg_num
; i
++) {
6988 types
[i
] = get_callout_arg_type_by_name_id(name_id
, i
);
6991 arg_num
= parse_callout_args(0, '}', &p
, end
, types
, vals
, env
);
6992 if (arg_num
< 0) return arg_num
;
6994 if (PEND
) return ONIGERR_END_PATTERN_IN_GROUP
;
7001 r
= get_callout_name_id_by_name(enc
, is_not_single
, name_start
, name_end
,
7003 if (r
!= ONIG_NORMAL
) return r
;
7005 max_arg_num
= get_callout_arg_num_by_name_id(name_id
);
7006 for (i
= 0; i
< max_arg_num
; i
++) {
7007 types
[i
] = get_callout_arg_type_by_name_id(name_id
, i
);
7011 in
= onig_get_callout_in_by_name_id(name_id
);
7012 opt_arg_num
= get_callout_opt_arg_num_by_name_id(name_id
);
7013 if (arg_num
> max_arg_num
|| arg_num
< (max_arg_num
- opt_arg_num
))
7014 return ONIGERR_INVALID_CALLOUT_ARG
;
7017 return ONIGERR_INVALID_CALLOUT_PATTERN
;
7019 r
= reg_callout_list_entry(env
, &num
);
7020 if (r
!= 0) return r
;
7022 ext
= onig_get_regex_ext(env
->reg
);
7023 CHECK_NULL_RETURN_MEMERR(ext
);
7024 if (IS_NULL(ext
->pattern
)) {
7025 r
= onig_ext_set_pattern(env
->reg
, env
->pattern
, env
->pattern_end
);
7026 if (r
!= ONIG_NORMAL
) return r
;
7029 if (tag_start
!= tag_end
) {
7030 r
= callout_tag_entry(env
, env
->reg
, tag_start
, tag_end
, num
);
7031 if (r
!= ONIG_NORMAL
) return r
;
7034 r
= node_new_callout(&node
, ONIG_CALLOUT_OF_NAME
, num
, name_id
, env
);
7035 if (r
!= ONIG_NORMAL
) return r
;
7037 e
= onig_reg_callout_list_at(env
->reg
, num
);
7038 CHECK_NULL_RETURN_MEMERR(e
);
7040 e
->of
= ONIG_CALLOUT_OF_NAME
;
7042 e
->name_id
= name_id
;
7043 e
->type
= onig_get_callout_type_by_name_id(name_id
);
7044 e
->start_func
= onig_get_callout_start_func_by_name_id(name_id
);
7045 e
->end_func
= onig_get_callout_end_func_by_name_id(name_id
);
7046 e
->u
.arg
.num
= max_arg_num
;
7047 e
->u
.arg
.passed_num
= arg_num
;
7048 for (i
= 0; i
< max_arg_num
; i
++) {
7049 e
->u
.arg
.types
[i
] = types
[i
];
7051 e
->u
.arg
.vals
[i
] = vals
[i
];
7053 e
->u
.arg
.vals
[i
] = get_callout_opt_default_by_name_id(name_id
, i
);
7063 parse_bag(Node
** np
, PToken
* tok
, int term
, UChar
** src
, UChar
* end
,
7068 OnigOptionType option
;
7071 OnigEncoding enc
= env
->enc
;
7077 if (PEND
) return ONIGERR_END_PATTERN_WITH_UNMATCHED_PARENTHESIS
;
7079 option
= env
->options
;
7081 if (c
== '?' && IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_QMARK_GROUP_EFFECT
)) {
7083 if (PEND
) return ONIGERR_END_PATTERN_IN_GROUP
;
7087 case ':': /* (?:...) grouping only */
7089 r
= fetch_token(tok
, &p
, end
, env
);
7090 if (r
< 0) return r
;
7091 r
= parse_subexp(np
, tok
, term
, &p
, end
, env
, 0);
7092 if (r
< 0) return r
;
7094 return 1; /* group */
7098 *np
= onig_node_new_anchor(ANCR_PREC_READ
, 0);
7100 case '!': /* preceding read */
7101 *np
= onig_node_new_anchor(ANCR_PREC_READ_NOT
, 0);
7103 case '>': /* (?>...) stop backtrack */
7104 *np
= node_new_bag(BAG_STOP_BACKTRACK
);
7108 if (IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_QMARK_LT_NAMED_GROUP
)) {
7112 return ONIGERR_UNDEFINED_GROUP_OPTION
;
7115 case '<': /* look behind (?<=...), (?<!...) */
7116 if (PEND
) return ONIGERR_END_PATTERN_WITH_UNMATCHED_PARENTHESIS
;
7119 *np
= onig_node_new_anchor(ANCR_LOOK_BEHIND
, 0);
7121 *np
= onig_node_new_anchor(ANCR_LOOK_BEHIND_NOT
, 0);
7123 if (IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_QMARK_LT_NAMED_GROUP
)) {
7126 enum REF_NUM num_type
;
7136 r
= fetch_name((OnigCodePoint
)c
, &p
, end
, &name_end
, env
, &num
,
7138 if (r
< 0) return r
;
7140 num
= scan_env_add_mem_entry(env
);
7141 if (num
< 0) return num
;
7142 if (list_capture
!= 0 && num
>= (int )MEM_STATUS_BITS_NUM
)
7143 return ONIGERR_GROUP_NUMBER_OVER_FOR_CAPTURE_HISTORY
;
7145 r
= name_add(env
->reg
, name
, name_end
, num
, env
);
7146 if (r
!= 0) return r
;
7147 *np
= node_new_memory(1);
7148 CHECK_NULL_RETURN_MEMERR(*np
);
7149 BAG_(*np
)->m
.regnum
= num
;
7150 if (list_capture
!= 0)
7151 MEM_STATUS_ON_SIMPLE(env
->capture_history
, num
);
7155 return ONIGERR_UNDEFINED_GROUP_OPTION
;
7161 if (IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_QMARK_TILDE_ABSENT_GROUP
)) {
7165 int is_range_cutter
;
7167 if (PEND
) return ONIGERR_END_PATTERN_IN_GROUP
;
7169 if (PPEEK_IS('|')) { /* (?~|generator|absent) */
7171 if (PEND
) return ONIGERR_END_PATTERN_IN_GROUP
;
7174 if (PPEEK_IS(')')) { /* (?~|) : range clear */
7176 r
= make_range_clear(np
, env
);
7177 if (r
!= 0) return r
;
7184 r
= fetch_token(tok
, &p
, end
, env
);
7185 if (r
< 0) return r
;
7186 r
= parse_subexp(&absent
, tok
, term
, &p
, end
, env
, 1);
7188 onig_node_free(absent
);
7193 is_range_cutter
= 0;
7194 if (head_bar
!= 0) {
7196 if (NODE_TYPE(top
) != NODE_ALT
|| IS_NULL(NODE_CDR(top
))) {
7198 is_range_cutter
= 1;
7199 /* return ONIGERR_INVALID_ABSENT_GROUP_GENERATOR_PATTERN; */
7202 absent
= NODE_CAR(top
);
7203 expr
= NODE_CDR(top
);
7204 NODE_CAR(top
) = NULL_NODE
;
7205 NODE_CDR(top
) = NULL_NODE
;
7206 onig_node_free(top
);
7207 if (IS_NULL(NODE_CDR(expr
))) {
7209 expr
= NODE_CAR(top
);
7210 NODE_CAR(top
) = NULL_NODE
;
7211 onig_node_free(top
);
7216 r
= make_absent_tree(np
, absent
, expr
, is_range_cutter
, env
);
7223 return ONIGERR_UNDEFINED_GROUP_OPTION
;
7229 if (! IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_QMARK_BRACE_CALLOUT_CONTENTS
))
7230 return ONIGERR_UNDEFINED_GROUP_OPTION
;
7232 r
= parse_callout_of_contents(np
, ')', &p
, end
, env
);
7233 if (r
!= 0) return r
;
7241 if (IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_QMARK_LPAREN_IF_ELSE
)) {
7244 int condition_is_checker
;
7246 if (PEND
) return ONIGERR_END_PATTERN_IN_GROUP
;
7248 if (PEND
) return ONIGERR_END_PATTERN_IN_GROUP
;
7250 if (IS_CODE_DIGIT_ASCII(enc
, c
)
7251 || c
== '-' || c
== '+' || c
== '<' || c
== '\'') {
7256 enum REF_NUM num_type
;
7259 is_enclosed
= (c
== '<' || c
== '\'') ? 1 : 0;
7264 #ifdef USE_BACKREF_WITH_LEVEL
7265 name_end
= NULL_UCHARP
; /* no need. escape gcc warning. */
7266 r
= fetch_name_with_level(
7267 (OnigCodePoint
)(is_enclosed
!= 0 ? c
: '('),
7269 env
, &back_num
, &level
, &num_type
);
7270 if (r
== 1) exist_level
= 1;
7272 r
= fetch_name((OnigCodePoint
)(is_enclosed
!= 0 ? c
: '('),
7273 &p
, end
, &name_end
, env
, &back_num
, &num_type
, 1);
7276 if (is_enclosed
== 0) {
7283 condition_is_checker
= 1;
7284 if (num_type
!= IS_NOT_NUM
) {
7285 if (num_type
== IS_REL_NUM
) {
7286 back_num
= backref_rel_to_abs(back_num
, env
);
7289 return ONIGERR_INVALID_BACKREF
;
7291 if (IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_STRICT_CHECK_BACKREF
)) {
7292 if (back_num
> env
->num_mem
||
7293 IS_NULL(SCANENV_MEMENV(env
)[back_num
].node
))
7294 return ONIGERR_INVALID_BACKREF
;
7297 condition
= node_new_backref_checker(1, &back_num
, 0,
7298 #ifdef USE_BACKREF_WITH_LEVEL
7307 num
= name_to_group_numbers(env
, prev
, name_end
, &backs
);
7309 return ONIGERR_UNDEFINED_NAME_REFERENCE
;
7311 if (IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_STRICT_CHECK_BACKREF
)) {
7313 for (i
= 0; i
< num
; i
++) {
7314 if (backs
[i
] > env
->num_mem
||
7315 IS_NULL(SCANENV_MEMENV(env
)[backs
[i
]].node
))
7316 return ONIGERR_INVALID_BACKREF
;
7320 condition
= node_new_backref_checker(num
, backs
, 1,
7321 #ifdef USE_BACKREF_WITH_LEVEL
7327 if (is_enclosed
!= 0) {
7328 if (PEND
) goto err_if_else
;
7330 if (c
!= ')') goto err_if_else
;
7334 else if (c
== '?') {
7335 if (IS_SYNTAX_OP2(env
->syntax
,
7336 ONIG_SYN_OP2_QMARK_BRACE_CALLOUT_CONTENTS
)) {
7337 if (! PEND
&& PPEEK_IS('{')) {
7338 /* condition part is callouts of contents: (?(?{...})THEN|ELSE) */
7339 condition_is_checker
= 0;
7341 r
= parse_callout_of_contents(&condition
, ')', &p
, end
, env
);
7342 if (r
!= 0) return r
;
7348 else if (c
== '*' &&
7349 IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_ASTERISK_CALLOUT_NAME
)) {
7350 condition_is_checker
= 0;
7351 r
= parse_callout_of_name(&condition
, ')', &p
, end
, env
);
7352 if (r
!= 0) return r
;
7359 condition_is_checker
= 0;
7360 r
= fetch_token(tok
, &p
, end
, env
);
7361 if (r
< 0) return r
;
7362 r
= parse_subexp(&condition
, tok
, term
, &p
, end
, env
, 0);
7364 onig_node_free(condition
);
7372 CHECK_NULL_RETURN_MEMERR(condition
);
7376 onig_node_free(condition
);
7377 return ONIGERR_END_PATTERN_IN_GROUP
;
7380 if (PPEEK_IS(')')) { /* case: empty body: make backref checker */
7381 if (condition_is_checker
== 0) {
7382 onig_node_free(condition
);
7383 return ONIGERR_INVALID_IF_ELSE_SYNTAX
;
7388 else { /* if-else */
7393 if (PPEEK_IS('|')) {
7400 r
= fetch_token(tok
, &p
, end
, env
);
7402 onig_node_free(condition
);
7405 r
= parse_subexp(&target
, tok
, term
, &p
, end
, env
, 1);
7407 onig_node_free(condition
);
7408 onig_node_free(target
);
7412 if (then_is_empty
!= 0) {
7416 if (NODE_TYPE(target
) == NODE_ALT
) {
7417 Then
= NODE_CAR(target
);
7418 if (NODE_CDR(NODE_CDR(target
)) == NULL_NODE
) {
7419 Else
= NODE_CAR(NODE_CDR(target
));
7420 cons_node_free_alone(NODE_CDR(target
));
7423 Else
= NODE_CDR(target
);
7425 cons_node_free_alone(target
);
7433 *np
= node_new_bag_if_else(condition
, Then
, Else
);
7435 onig_node_free(condition
);
7436 onig_node_free(Then
);
7437 onig_node_free(Else
);
7438 return ONIGERR_MEMORY
;
7444 return ONIGERR_UNDEFINED_GROUP_OPTION
;
7448 #ifdef USE_CAPTURE_HISTORY
7450 if (IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_ATMARK_CAPTURE_HISTORY
)) {
7451 if (IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_QMARK_LT_NAMED_GROUP
)) {
7453 if (c
== '<' || c
== '\'') {
7455 goto named_group2
; /* (?@<name>...) */
7460 *np
= node_new_memory(0);
7461 CHECK_NULL_RETURN_MEMERR(*np
);
7462 num
= scan_env_add_mem_entry(env
);
7466 else if (num
>= (int )MEM_STATUS_BITS_NUM
) {
7467 return ONIGERR_GROUP_NUMBER_OVER_FOR_CAPTURE_HISTORY
;
7469 BAG_(*np
)->m
.regnum
= num
;
7470 MEM_STATUS_ON_SIMPLE(env
->capture_history
, num
);
7473 return ONIGERR_UNDEFINED_GROUP_OPTION
;
7478 #ifdef USE_POSIXLINE_OPTION
7481 case '-': case 'i': case 'm': case 's': case 'x':
7482 case 'W': case 'D': case 'S': case 'P':
7493 case '-': neg
= 1; break;
7494 case 'x': OPTION_NEGATE(option
, ONIG_OPTION_EXTEND
, neg
); break;
7495 case 'i': OPTION_NEGATE(option
, ONIG_OPTION_IGNORECASE
, neg
); break;
7497 if (IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_OPTION_PERL
)) {
7498 OPTION_NEGATE(option
, ONIG_OPTION_MULTILINE
, neg
);
7501 return ONIGERR_UNDEFINED_GROUP_OPTION
;
7505 if (IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_OPTION_PERL
)) {
7506 OPTION_NEGATE(option
, ONIG_OPTION_SINGLELINE
, (neg
== 0 ? 1 : 0));
7508 else if (IS_SYNTAX_OP2(env
->syntax
,
7509 ONIG_SYN_OP2_OPTION_ONIGURUMA
|ONIG_SYN_OP2_OPTION_RUBY
)) {
7510 OPTION_NEGATE(option
, ONIG_OPTION_MULTILINE
, neg
);
7513 return ONIGERR_UNDEFINED_GROUP_OPTION
;
7515 #ifdef USE_POSIXLINE_OPTION
7517 OPTION_NEGATE(option
, ONIG_OPTION_MULTILINE
|ONIG_OPTION_SINGLELINE
, neg
);
7520 case 'W': OPTION_NEGATE(option
, ONIG_OPTION_WORD_IS_ASCII
, neg
); break;
7521 case 'D': OPTION_NEGATE(option
, ONIG_OPTION_DIGIT_IS_ASCII
, neg
); break;
7522 case 'S': OPTION_NEGATE(option
, ONIG_OPTION_SPACE_IS_ASCII
, neg
); break;
7523 case 'P': OPTION_NEGATE(option
, ONIG_OPTION_POSIX_IS_ASCII
, neg
); break;
7525 case 'y': /* y{g}, y{w} */
7527 if (! IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_OPTION_ONIGURUMA
))
7528 return ONIGERR_UNDEFINED_GROUP_OPTION
;
7530 if (neg
!= 0) return ONIGERR_UNDEFINED_GROUP_OPTION
;
7532 if (PEND
) return ONIGERR_END_PATTERN_IN_GROUP
;
7533 if (! PPEEK_IS('{')) return ONIGERR_UNDEFINED_GROUP_OPTION
;
7535 if (PEND
) return ONIGERR_END_PATTERN_IN_GROUP
;
7539 if (! ONIGENC_IS_UNICODE_ENCODING(enc
))
7540 return ONIGERR_UNDEFINED_GROUP_OPTION
;
7542 OPTION_NEGATE(option
, ONIG_OPTION_TEXT_SEGMENT_EXTENDED_GRAPHEME_CLUSTER
, 0);
7543 OPTION_NEGATE(option
, ONIG_OPTION_TEXT_SEGMENT_WORD
, 1);
7545 #ifdef USE_UNICODE_WORD_BREAK
7547 if (! ONIGENC_IS_UNICODE_ENCODING(enc
))
7548 return ONIGERR_UNDEFINED_GROUP_OPTION
;
7550 OPTION_NEGATE(option
, ONIG_OPTION_TEXT_SEGMENT_WORD
, 0);
7551 OPTION_NEGATE(option
, ONIG_OPTION_TEXT_SEGMENT_EXTENDED_GRAPHEME_CLUSTER
, 1);
7555 return ONIGERR_UNDEFINED_GROUP_OPTION
;
7558 if (PEND
) return ONIGERR_END_PATTERN_IN_GROUP
;
7561 return ONIGERR_UNDEFINED_GROUP_OPTION
;
7566 return ONIGERR_UNDEFINED_GROUP_OPTION
;
7570 *np
= node_new_option(option
);
7571 CHECK_NULL_RETURN_MEMERR(*np
);
7573 return 2; /* option only */
7575 else if (c
== ':') {
7576 OnigOptionType prev
= env
->options
;
7578 env
->options
= option
;
7579 r
= fetch_token(tok
, &p
, end
, env
);
7580 if (r
< 0) return r
;
7581 r
= parse_subexp(&target
, tok
, term
, &p
, end
, env
, 0);
7582 env
->options
= prev
;
7584 onig_node_free(target
);
7587 *np
= node_new_option(option
);
7588 CHECK_NULL_RETURN_MEMERR(*np
);
7589 NODE_BODY(*np
) = target
;
7594 if (PEND
) return ONIGERR_END_PATTERN_IN_GROUP
;
7601 return ONIGERR_UNDEFINED_GROUP_OPTION
;
7605 else if (c
== '*' &&
7606 IS_SYNTAX_OP2(env
->syntax
, ONIG_SYN_OP2_ASTERISK_CALLOUT_NAME
)) {
7608 r
= parse_callout_of_name(np
, ')', &p
, end
, env
);
7609 if (r
!= 0) return r
;
7615 if (ONIG_IS_OPTION_ON(env
->options
, ONIG_OPTION_DONT_CAPTURE_GROUP
))
7618 *np
= node_new_memory(0);
7619 CHECK_NULL_RETURN_MEMERR(*np
);
7620 num
= scan_env_add_mem_entry(env
);
7621 if (num
< 0) return num
;
7622 BAG_(*np
)->m
.regnum
= num
;
7625 CHECK_NULL_RETURN_MEMERR(*np
);
7626 r
= fetch_token(tok
, &p
, end
, env
);
7627 if (r
< 0) return r
;
7628 r
= parse_subexp(&target
, tok
, term
, &p
, end
, env
, 0);
7630 onig_node_free(target
);
7634 NODE_BODY(*np
) = target
;
7636 if (NODE_TYPE(*np
) == NODE_BAG
) {
7637 if (BAG_(*np
)->type
== BAG_MEMORY
) {
7638 /* Don't move this to previous of parse_subexp() */
7639 r
= scan_env_set_mem_node(env
, BAG_(*np
)->m
.regnum
, *np
);
7640 if (r
!= 0) return r
;
7649 static const char* PopularQStr
[] = {
7650 "?", "*", "+", "??", "*?", "+?"
7653 static const char* ReduceQStr
[] = {
7654 "", "", "*", "*?", "??", "+ and ??", "+? and ?"
7658 set_quantifier(Node
* qnode
, Node
* target
, int group
, ScanEnv
* env
)
7663 if (qn
->lower
== 1 && qn
->upper
== 1)
7666 switch (NODE_TYPE(target
)) {
7669 if (str_node_can_be_split(target
, env
->enc
)) {
7670 Node
* n
= str_node_split_last_char(target
, env
->enc
);
7671 if (IS_NOT_NULL(n
)) {
7672 NODE_BODY(qnode
) = n
;
7680 { /* check redundant double repeat. */
7681 /* verbose warn (?:.?)? etc... but not warn (.?)? etc... */
7682 QuantNode
* qnt
= QUANT_(target
);
7683 int nestq_num
= quantifier_type_num(qn
);
7684 int targetq_num
= quantifier_type_num(qnt
);
7686 #ifdef USE_WARNING_REDUNDANT_NESTED_REPEAT_OPERATOR
7687 if (targetq_num
>= 0 && nestq_num
>= 0 &&
7688 IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_WARN_REDUNDANT_NESTED_REPEAT
)) {
7689 UChar buf
[WARN_BUFSIZE
];
7691 switch(ReduceTypeTable
[targetq_num
][nestq_num
]) {
7696 if (onig_verb_warn
!= onig_null_warn
) {
7697 onig_snprintf_with_pattern(buf
, WARN_BUFSIZE
, env
->enc
,
7698 env
->pattern
, env
->pattern_end
,
7699 (UChar
* )"redundant nested repeat operator");
7700 (*onig_verb_warn
)((char* )buf
);
7706 if (onig_verb_warn
!= onig_null_warn
) {
7707 onig_snprintf_with_pattern(buf
, WARN_BUFSIZE
, env
->enc
,
7708 env
->pattern
, env
->pattern_end
,
7709 (UChar
* )"nested repeat operator %s and %s was replaced with '%s'",
7710 PopularQStr
[targetq_num
], PopularQStr
[nestq_num
],
7711 ReduceQStr
[ReduceTypeTable
[targetq_num
][nestq_num
]]);
7712 (*onig_verb_warn
)((char* )buf
);
7721 if (targetq_num
>= 0 && nestq_num
< 0) {
7722 if (targetq_num
== 1 || targetq_num
== 2) { /* * or + */
7723 /* (?:a*){n,m}, (?:a+){n,m} => (?:a*){n,n}, (?:a+){n,n} */
7724 if (! IS_INFINITE_REPEAT(qn
->upper
) && qn
->upper
> 1 && qn
->greedy
) {
7725 qn
->upper
= (qn
->lower
== 0 ? 1 : qn
->lower
);
7730 NODE_BODY(qnode
) = target
;
7731 onig_reduce_nested_quantifier(qnode
, target
);
7741 NODE_BODY(qnode
) = target
;
7747 #ifndef CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS
7749 clear_not_flag_cclass(CClassNode
* cc
, OnigEncoding enc
)
7754 if (IS_NCCLASS_NOT(cc
)) {
7755 bitset_invert(cc
->bs
);
7757 if (! ONIGENC_IS_SINGLEBYTE(enc
)) {
7758 r
= not_code_range_buf(enc
, cc
->mbuf
, &tbuf
);
7759 if (r
!= 0) return r
;
7761 bbuf_free(cc
->mbuf
);
7765 NCCLASS_CLEAR_NOT(cc
);
7770 #endif /* CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS */
7777 } IApplyCaseFoldArg
;
7780 i_apply_case_fold(OnigCodePoint from
, OnigCodePoint to
[], int to_len
, void* arg
)
7782 IApplyCaseFoldArg
* iarg
;
7787 iarg
= (IApplyCaseFoldArg
* )arg
;
7793 int is_in
= onig_is_code_in_cc(env
->enc
, from
, cc
);
7794 #ifdef CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS
7795 if ((is_in
!= 0 && !IS_NCCLASS_NOT(cc
)) ||
7796 (is_in
== 0 && IS_NCCLASS_NOT(cc
))) {
7797 if (ONIGENC_MBC_MINLEN(env
->enc
) > 1 || *to
>= SINGLE_BYTE_SIZE
) {
7798 add_code_range(&(cc
->mbuf
), env
, *to
, *to
);
7801 BITSET_SET_BIT(bs
, *to
);
7806 if (ONIGENC_MBC_MINLEN(env
->enc
) > 1 || *to
>= SINGLE_BYTE_SIZE
) {
7807 if (IS_NCCLASS_NOT(cc
)) clear_not_flag_cclass(cc
, env
->enc
);
7808 add_code_range(&(cc
->mbuf
), env
, *to
, *to
);
7811 if (IS_NCCLASS_NOT(cc
)) {
7812 BITSET_CLEAR_BIT(bs
, *to
);
7815 BITSET_SET_BIT(bs
, *to
);
7818 #endif /* CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS */
7822 UChar buf
[ONIGENC_CODE_TO_MBC_MAXLEN
];
7823 Node
*snode
= NULL_NODE
;
7825 if (onig_is_code_in_cc(env
->enc
, from
, cc
)
7826 #ifdef CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS
7827 && !IS_NCCLASS_NOT(cc
)
7830 for (i
= 0; i
< to_len
; i
++) {
7831 len
= ONIGENC_CODE_TO_MBC(env
->enc
, to
[i
], buf
);
7833 snode
= onig_node_new_str(buf
, buf
+ len
);
7834 CHECK_NULL_RETURN_MEMERR(snode
);
7836 /* char-class expanded multi-char only
7837 compare with string folded at match time. */
7838 NODE_STRING_SET_AMBIG(snode
);
7841 r
= onig_node_str_cat(snode
, buf
, buf
+ len
);
7843 onig_node_free(snode
);
7849 *(iarg
->ptail
) = onig_node_new_alt(snode
, NULL_NODE
);
7850 CHECK_NULL_RETURN_MEMERR(*(iarg
->ptail
));
7851 iarg
->ptail
= &(NODE_CDR((*(iarg
->ptail
))));
7859 parse_exp(Node
** np
, PToken
* tok
, int term
, UChar
** src
, UChar
* end
,
7860 ScanEnv
* env
, int group_head
)
7865 unsigned int parse_depth
;
7869 if (tok
->type
== (enum TokenSyms
)term
)
7872 parse_depth
= env
->parse_depth
;
7874 switch (tok
->type
) {
7878 *np
= node_new_empty();
7879 CHECK_NULL_RETURN_MEMERR(*np
);
7883 case TK_SUBEXP_OPEN
:
7884 r
= parse_bag(np
, tok
, TK_SUBEXP_CLOSE
, src
, end
, env
);
7885 if (r
< 0) return r
;
7886 if (r
== 1) { /* group */
7887 if (group_head
== 0)
7891 *np
= node_new_group(target
);
7893 onig_node_free(target
);
7894 return ONIGERR_MEMORY
;
7899 else if (r
== 2) { /* option only */
7901 OnigOptionType prev
= env
->options
;
7903 env
->options
= BAG_(*np
)->o
.options
;
7904 r
= fetch_token(tok
, src
, end
, env
);
7905 if (r
< 0) return r
;
7906 r
= parse_subexp(&target
, tok
, term
, src
, end
, env
, 0);
7907 env
->options
= prev
;
7909 onig_node_free(target
);
7912 NODE_BODY(*np
) = target
;
7917 case TK_SUBEXP_CLOSE
:
7918 if (! IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_ALLOW_UNMATCHED_CLOSE_SUBEXP
))
7919 return ONIGERR_UNMATCHED_CLOSE_PARENTHESIS
;
7921 if (tok
->escaped
) goto tk_raw_byte
;
7928 *np
= node_new_str(tok
->backp
, *src
);
7929 CHECK_NULL_RETURN_MEMERR(*np
);
7932 r
= fetch_token(tok
, src
, end
, env
);
7933 if (r
< 0) return r
;
7934 if (r
!= TK_STRING
) break;
7936 r
= onig_node_str_cat(*np
, tok
->backp
, *src
);
7937 if (r
< 0) return r
;
7949 *np
= node_new_str_raw_char((UChar
)tok
->u
.c
);
7950 CHECK_NULL_RETURN_MEMERR(*np
);
7953 if (len
>= ONIGENC_MBC_MINLEN(env
->enc
)) {
7954 if (len
== enclen(env
->enc
, STR_(*np
)->s
)) {
7955 r
= fetch_token(tok
, src
, end
, env
);
7956 goto tk_raw_byte_end
;
7960 r
= fetch_token(tok
, src
, end
, env
);
7961 if (r
< 0) return r
;
7962 if (r
!= TK_RAW_BYTE
)
7963 return ONIGERR_TOO_SHORT_MULTI_BYTE_STRING
;
7965 r
= node_str_cat_char(*np
, (UChar
)tok
->u
.c
);
7966 if (r
< 0) return r
;
7972 if (! ONIGENC_IS_VALID_MBC_STRING(env
->enc
, STR_(*np
)->s
, STR_(*np
)->end
))
7973 return ONIGERR_INVALID_WIDE_CHAR_VALUE
;
7975 NODE_STRING_CLEAR_RAW(*np
);
7982 UChar buf
[ONIGENC_CODE_TO_MBC_MAXLEN
];
7983 len
= ONIGENC_CODE_TO_MBC(env
->enc
, tok
->u
.code
, buf
);
7984 if (len
< 0) return len
;
7985 #ifdef NUMBERED_CHAR_IS_NOT_CASE_AMBIG
7986 *np
= node_new_str_raw(buf
, buf
+ len
);
7988 *np
= node_new_str(buf
, buf
+ len
);
7990 CHECK_NULL_RETURN_MEMERR(*np
);
7996 OnigCodePoint end_op
[2];
7997 UChar
*qstart
, *qend
, *nextp
;
7999 end_op
[0] = (OnigCodePoint
)MC_ESC(env
->syntax
);
8000 end_op
[1] = (OnigCodePoint
)'E';
8002 qend
= find_str_position(end_op
, 2, qstart
, end
, &nextp
, env
->enc
);
8003 if (IS_NULL(qend
)) {
8006 *np
= node_new_str(qstart
, qend
);
8007 CHECK_NULL_RETURN_MEMERR(*np
);
8014 switch (tok
->u
.prop
.ctype
) {
8015 case ONIGENC_CTYPE_WORD
:
8016 *np
= node_new_ctype(tok
->u
.prop
.ctype
, tok
->u
.prop
.not, env
->options
);
8017 CHECK_NULL_RETURN_MEMERR(*np
);
8020 case ONIGENC_CTYPE_SPACE
:
8021 case ONIGENC_CTYPE_DIGIT
:
8022 case ONIGENC_CTYPE_XDIGIT
:
8026 *np
= node_new_cclass();
8027 CHECK_NULL_RETURN_MEMERR(*np
);
8029 add_ctype_to_cc(cc
, tok
->u
.prop
.ctype
, 0, env
);
8030 if (tok
->u
.prop
.not != 0) NCCLASS_SET_NOT(cc
);
8035 return ONIGERR_PARSER_BUG
;
8041 case TK_CHAR_PROPERTY
:
8042 r
= parse_char_property(np
, tok
, src
, end
, env
);
8043 if (r
!= 0) return r
;
8050 r
= parse_char_class(np
, tok
, src
, end
, env
);
8051 if (r
!= 0) return r
;
8054 if (IS_IGNORECASE(env
->options
)) {
8055 IApplyCaseFoldArg iarg
;
8059 iarg
.alt_root
= NULL_NODE
;
8060 iarg
.ptail
= &(iarg
.alt_root
);
8062 r
= ONIGENC_APPLY_ALL_CASE_FOLD(env
->enc
, env
->case_fold_flag
,
8063 i_apply_case_fold
, &iarg
);
8065 onig_node_free(iarg
.alt_root
);
8068 if (IS_NOT_NULL(iarg
.alt_root
)) {
8069 Node
* work
= onig_node_new_alt(*np
, iarg
.alt_root
);
8070 if (IS_NULL(work
)) {
8071 onig_node_free(iarg
.alt_root
);
8072 return ONIGERR_MEMORY
;
8081 *np
= node_new_anychar();
8082 CHECK_NULL_RETURN_MEMERR(*np
);
8085 case TK_ANYCHAR_ANYTIME
:
8086 *np
= node_new_anychar();
8087 CHECK_NULL_RETURN_MEMERR(*np
);
8088 qn
= node_new_quantifier(0, INFINITE_REPEAT
, 0);
8089 CHECK_NULL_RETURN_MEMERR(qn
);
8090 NODE_BODY(qn
) = *np
;
8095 len
= tok
->u
.backref
.num
;
8096 *np
= node_new_backref(len
,
8097 (len
> 1 ? tok
->u
.backref
.refs
: &(tok
->u
.backref
.ref1
)),
8098 tok
->u
.backref
.by_name
,
8099 #ifdef USE_BACKREF_WITH_LEVEL
8100 tok
->u
.backref
.exist_level
,
8101 tok
->u
.backref
.level
,
8104 CHECK_NULL_RETURN_MEMERR(*np
);
8110 int gnum
= tok
->u
.call
.gnum
;
8112 *np
= node_new_call(tok
->u
.call
.name
, tok
->u
.call
.name_end
,
8113 gnum
, tok
->u
.call
.by_number
);
8114 CHECK_NULL_RETURN_MEMERR(*np
);
8116 if (tok
->u
.call
.by_number
!= 0 && gnum
== 0) {
8117 env
->has_call_zero
= 1;
8126 IS_WORD_ASCII(env
->options
) && IS_WORD_ANCHOR_TYPE(tok
->u
.anchor
) ? 1 : 0;
8127 *np
= onig_node_new_anchor(tok
->u
.anchor
, ascii_mode
);
8128 CHECK_NULL_RETURN_MEMERR(*np
);
8134 if (IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_CONTEXT_INDEP_REPEAT_OPS
)) {
8135 if (IS_SYNTAX_BV(env
->syntax
, ONIG_SYN_CONTEXT_INVALID_REPEAT_OPS
))
8136 return ONIGERR_TARGET_OF_REPEAT_OPERATOR_NOT_SPECIFIED
;
8138 *np
= node_new_empty();
8139 CHECK_NULL_RETURN_MEMERR(*np
);
8148 r
= node_new_keep(np
, env
);
8149 if (r
< 0) return r
;
8152 case TK_GENERAL_NEWLINE
:
8153 r
= node_new_general_newline(np
, env
);
8154 if (r
< 0) return r
;
8158 r
= node_new_no_newline(np
, env
);
8159 if (r
< 0) return r
;
8162 case TK_TRUE_ANYCHAR
:
8163 r
= node_new_true_anychar(np
, env
);
8164 if (r
< 0) return r
;
8167 case TK_TEXT_SEGMENT
:
8168 r
= make_text_segment(np
, env
);
8169 if (r
< 0) return r
;
8173 return ONIGERR_PARSER_BUG
;
8181 r
= fetch_token(tok
, src
, end
, env
);
8182 if (r
< 0) return r
;
8185 if (r
== TK_REPEAT
|| r
== TK_INTERVAL
) {
8188 if (is_invalid_quantifier_target(*tp
))
8189 return ONIGERR_TARGET_OF_REPEAT_OPERATOR_INVALID
;
8192 if (parse_depth
> ParseDepthLimit
)
8193 return ONIGERR_PARSE_DEPTH_LIMIT_OVER
;
8195 qn
= node_new_quantifier(tok
->u
.repeat
.lower
, tok
->u
.repeat
.upper
,
8197 CHECK_NULL_RETURN_MEMERR(qn
);
8198 QUANT_(qn
)->greedy
= tok
->u
.repeat
.greedy
;
8200 target
= node_drop_group(*tp
);
8206 r
= set_quantifier(qn
, target
, group
, env
);
8212 if (tok
->u
.repeat
.possessive
!= 0) {
8214 en
= node_new_bag(BAG_STOP_BACKTRACK
);
8217 return ONIGERR_MEMORY
;
8226 else if (r
== 1) { /* x{1,1} ==> x */
8230 else if (r
== 2) { /* split case: /abc+/ */
8233 *tp
= node_new_list(*tp
, NULL
);
8236 return ONIGERR_MEMORY
;
8238 tmp
= NODE_CDR(*tp
) = node_new_list(qn
, NULL
);
8241 return ONIGERR_MEMORY
;
8243 tp
= &(NODE_CAR(tmp
));
8254 parse_branch(Node
** top
, PToken
* tok
, int term
, UChar
** src
, UChar
* end
,
8255 ScanEnv
* env
, int group_head
)
8258 Node
*node
, **headp
;
8261 r
= parse_exp(&node
, tok
, term
, src
, end
, env
, group_head
);
8263 onig_node_free(node
);
8267 if (r
== TK_EOT
|| r
== term
|| r
== TK_ALT
) {
8271 *top
= node_new_list(node
, NULL
);
8272 if (IS_NULL(*top
)) {
8273 onig_node_free(node
);
8274 return ONIGERR_MEMORY
;
8277 headp
= &(NODE_CDR(*top
));
8278 while (r
!= TK_EOT
&& r
!= term
&& r
!= TK_ALT
) {
8279 r
= parse_exp(&node
, tok
, term
, src
, end
, env
, 0);
8281 onig_node_free(node
);
8285 if (NODE_TYPE(node
) == NODE_LIST
) {
8287 while (IS_NOT_NULL(NODE_CDR(node
))) node
= NODE_CDR(node
);
8288 headp
= &(NODE_CDR(node
));
8291 *headp
= node_new_list(node
, NULL
);
8292 headp
= &(NODE_CDR(*headp
));
8300 /* term_tok: TK_EOT or TK_SUBEXP_CLOSE */
8302 parse_subexp(Node
** top
, PToken
* tok
, int term
, UChar
** src
, UChar
* end
,
8303 ScanEnv
* env
, int group_head
)
8306 Node
*node
, **headp
;
8310 if (env
->parse_depth
> ParseDepthLimit
)
8311 return ONIGERR_PARSE_DEPTH_LIMIT_OVER
;
8313 r
= parse_branch(&node
, tok
, term
, src
, end
, env
, group_head
);
8315 onig_node_free(node
);
8322 else if (r
== TK_ALT
) {
8323 *top
= onig_node_new_alt(node
, NULL
);
8324 if (IS_NULL(*top
)) {
8325 onig_node_free(node
);
8326 return ONIGERR_MEMORY
;
8329 headp
= &(NODE_CDR(*top
));
8330 while (r
== TK_ALT
) {
8331 r
= fetch_token(tok
, src
, end
, env
);
8332 if (r
< 0) return r
;
8333 r
= parse_branch(&node
, tok
, term
, src
, end
, env
, 0);
8335 onig_node_free(node
);
8338 *headp
= onig_node_new_alt(node
, NULL
);
8339 if (IS_NULL(*headp
)) {
8340 onig_node_free(node
);
8341 onig_node_free(*top
);
8342 return ONIGERR_MEMORY
;
8345 headp
= &(NODE_CDR(*headp
));
8348 if (tok
->type
!= (enum TokenSyms
)term
)
8352 onig_node_free(node
);
8354 if (term
== TK_SUBEXP_CLOSE
)
8355 return ONIGERR_END_PATTERN_WITH_UNMATCHED_PARENTHESIS
;
8357 return ONIGERR_PARSER_BUG
;
8365 parse_regexp(Node
** top
, UChar
** src
, UChar
* end
, ScanEnv
* env
)
8370 r
= fetch_token(&tok
, src
, end
, env
);
8371 if (r
< 0) return r
;
8372 r
= parse_subexp(top
, &tok
, TK_EOT
, src
, end
, env
, 0);
8373 if (r
< 0) return r
;
8380 make_call_zero_body(Node
* node
, ScanEnv
* env
, Node
** rnode
)
8384 Node
* x
= node_new_memory(0 /* 0: is not named */);
8385 CHECK_NULL_RETURN_MEMERR(x
);
8387 NODE_BODY(x
) = node
;
8388 BAG_(x
)->m
.regnum
= 0;
8389 r
= scan_env_set_mem_node(env
, 0, x
);
8401 onig_parse_tree(Node
** root
, const UChar
* pattern
, const UChar
* end
,
8402 regex_t
* reg
, ScanEnv
* env
)
8412 scan_env_clear(env
);
8413 env
->options
= reg
->options
;
8414 env
->case_fold_flag
= reg
->case_fold_flag
;
8415 env
->enc
= reg
->enc
;
8416 env
->syntax
= reg
->syntax
;
8417 env
->pattern
= (UChar
* )pattern
;
8418 env
->pattern_end
= (UChar
* )end
;
8423 if (! ONIGENC_IS_VALID_MBC_STRING(env
->enc
, pattern
, end
))
8424 return ONIGERR_INVALID_WIDE_CHAR_VALUE
;
8426 p
= (UChar
* )pattern
;
8427 r
= parse_regexp(root
, &p
, (UChar
* )end
, env
);
8430 if (r
!= 0) return r
;
8432 if (env
->has_call_zero
!= 0) {
8434 r
= make_call_zero_body(*root
, env
, &zero_node
);
8435 if (r
!= 0) return r
;
8441 reg
->num_mem
= env
->num_mem
;
8445 if (IS_NOT_NULL(ext
) && ext
->callout_num
> 0) {
8446 r
= setup_ext_callout_list_values(reg
);
8454 onig_scan_env_set_error_string(ScanEnv
* env
, int ecode ARG_UNUSED
,
8455 UChar
* arg
, UChar
* arg_end
)
8458 env
->error_end
= arg_end
;