32#if defined(__GNUC__) && __GNUC__ >= 12
33#pragma GCC diagnostic ignored "-Warray-bounds"
34#pragma GCC diagnostic ignored "-Wstringop-overflow"
35#pragma GCC diagnostic ignored "-Wrestrict"
40#include "internal/sanitizers.h"
42#define WARN_BUFSIZE 256
44#define CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS
48 (( SYN_GNU_REGEX_OP | ONIG_SYN_OP_QMARK_NON_GREEDY |
49 ONIG_SYN_OP_ESC_OCTAL3 | ONIG_SYN_OP_ESC_X_HEX2 |
50 ONIG_SYN_OP_ESC_X_BRACE_HEX8 | ONIG_SYN_OP_ESC_CONTROL_CHARS |
51 ONIG_SYN_OP_ESC_C_CONTROL )
52 & ~ONIG_SYN_OP_ESC_LTGT_WORD_BEGIN_END )
53 , ( ONIG_SYN_OP2_QMARK_GROUP_EFFECT |
54 ONIG_SYN_OP2_OPTION_RUBY |
55 ONIG_SYN_OP2_QMARK_LT_NAMED_GROUP | ONIG_SYN_OP2_ESC_K_NAMED_BACKREF |
56 ONIG_SYN_OP2_ESC_G_SUBEXP_CALL |
57 ONIG_SYN_OP2_ESC_P_BRACE_CHAR_PROPERTY |
58 ONIG_SYN_OP2_ESC_P_BRACE_CIRCUMFLEX_NOT |
59 ONIG_SYN_OP2_PLUS_POSSESSIVE_REPEAT |
60 ONIG_SYN_OP2_CCLASS_SET_OP | ONIG_SYN_OP2_ESC_CAPITAL_C_BAR_CONTROL |
61 ONIG_SYN_OP2_ESC_CAPITAL_M_BAR_META | ONIG_SYN_OP2_ESC_V_VTAB |
62 ONIG_SYN_OP2_ESC_H_XDIGIT |
64 ONIG_SYN_OP2_ESC_U_HEX4 |
66 ONIG_SYN_OP2_ESC_CAPITAL_X_EXTENDED_GRAPHEME_CLUSTER |
67 ONIG_SYN_OP2_QMARK_LPAREN_CONDITION |
68 ONIG_SYN_OP2_ESC_CAPITAL_R_LINEBREAK |
69 ONIG_SYN_OP2_ESC_CAPITAL_K_KEEP |
70 ONIG_SYN_OP2_QMARK_TILDE_ABSENT )
71 , ( SYN_GNU_REGEX_BV |
72 ONIG_SYN_ALLOW_INTERVAL_LOW_ABBREV |
73 ONIG_SYN_DIFFERENT_LEN_ALT_LOOK_BEHIND |
74 ONIG_SYN_CAPTURE_ONLY_NAMED_GROUP |
75 ONIG_SYN_ALLOW_MULTIPLEX_DEFINITION_NAME |
76 ONIG_SYN_FIXED_INTERVAL_IS_GREEDY_ONLY |
77 ONIG_SYN_WARN_CC_OP_NOT_ESCAPED |
78 ONIG_SYN_WARN_CC_DUP |
79 ONIG_SYN_WARN_REDUNDANT_NESTED_REPEAT )
80 , ( ONIG_OPTION_ASCII_RANGE | ONIG_OPTION_POSIX_BRACKET_ALL_RANGE |
81 ONIG_OPTION_WORD_BOUND_ALL_RANGE )
85 , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR
86 , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR
87 , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR
88 , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR
89 , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR
95extern void onig_null_warn(
const char* s ARG_UNUSED) { }
97#ifdef DEFAULT_WARN_FUNCTION
98static OnigWarnFunc onig_warn = (OnigWarnFunc )DEFAULT_WARN_FUNCTION;
100static OnigWarnFunc onig_warn = onig_null_warn;
103#ifdef DEFAULT_VERB_WARN_FUNCTION
104static OnigWarnFunc onig_verb_warn = (OnigWarnFunc )DEFAULT_VERB_WARN_FUNCTION;
106static OnigWarnFunc onig_verb_warn = onig_null_warn;
109extern void onig_set_warn_func(OnigWarnFunc f)
114extern void onig_set_verb_warn_func(OnigWarnFunc f)
119static void CC_DUP_WARN(
ScanEnv *env, OnigCodePoint from, OnigCodePoint to);
122static unsigned int ParseDepthLimit = DEFAULT_PARSE_DEPTH_LIMIT;
125onig_get_parse_depth_limit(
void)
127 return ParseDepthLimit;
131onig_set_parse_depth_limit(
unsigned int depth)
134 ParseDepthLimit = DEFAULT_PARSE_DEPTH_LIMIT;
136 ParseDepthLimit = depth;
144 if (IS_NOT_NULL(bbuf)) {
157 CHECK_NULL_RETURN_MEMERR(to);
158 r = BBUF_INIT(to, from->alloc);
159 if (r != 0)
return r;
160 to->used = from->used;
161 xmemcpy(to->p, from->p, from->used);
165#define BACKREF_REL_TO_ABS(rel_no, env) \
166 ((env)->num_mem + 1 + (rel_no))
168#define ONOFF(v,f,negative) (negative) ? ((v) &= ~(f)) : ((v) |= (f))
170#define MBCODE_START_POS(enc) \
171 (OnigCodePoint )(ONIGENC_MBC_MINLEN(enc) > 1 ? 0 : 0x80)
173#define SET_ALL_MULTI_BYTE_RANGE(enc, pbuf) \
174 add_code_range_to_buf(pbuf, env, MBCODE_START_POS(enc), ONIG_LAST_CODE_POINT)
176#define ADD_ALL_MULTI_BYTE_RANGE(enc, mbuf) do {\
177 if (! ONIGENC_IS_SINGLEBYTE(enc)) {\
178 r = SET_ALL_MULTI_BYTE_RANGE(enc, &(mbuf));\
184#define BITSET_SET_BIT_CHKDUP(bs, pos) do { \
185 if (BITSET_AT(bs, pos)) CC_DUP_WARN(env, pos, pos); \
186 BS_ROOM(bs, pos) |= BS_BIT(pos); \
189#define BITSET_IS_EMPTY(bs,empty) do {\
192 for (i = 0; i < BITSET_SIZE; i++) {\
200bitset_set_range(
ScanEnv *env, BitSetRef bs,
int from,
int to)
203 for (i = from; i <= to && i < SINGLE_BYTE_SIZE; i++) {
204 BITSET_SET_BIT_CHKDUP(bs, i);
210bitset_set_all(BitSetRef bs)
213 for (i = 0; i < BITSET_SIZE; i++) { bs[i] = ~((Bits )0); }
218bitset_invert(BitSetRef bs)
221 for (i = 0; i < BITSET_SIZE; i++) { bs[i] = ~(bs[i]); }
225bitset_invert_to(BitSetRef from, BitSetRef to)
228 for (i = 0; i < BITSET_SIZE; i++) { to[i] = ~(from[i]); }
232bitset_and(BitSetRef dest, BitSetRef bs)
235 for (i = 0; i < BITSET_SIZE; i++) { dest[i] &= bs[i]; }
239bitset_or(BitSetRef dest, BitSetRef bs)
242 for (i = 0; i < BITSET_SIZE; i++) { dest[i] |= bs[i]; }
246bitset_copy(BitSetRef dest, BitSetRef bs)
249 for (i = 0; i < BITSET_SIZE; i++) { dest[i] = bs[i]; }
252#if defined(USE_NAMED_GROUP) && !defined(USE_ST_LIBRARY)
254onig_strncmp(
const UChar* s1,
const UChar* s2,
size_t n)
267onig_strcpy(UChar* dest,
const UChar* src,
const UChar* end)
269 ptrdiff_t
len = end - src;
271 xmemcpy(dest, src,
len);
272 dest[
len] = (UChar )0;
276#ifdef USE_NAMED_GROUP
278strdup_with_null(
OnigEncoding enc,
const UChar* s,
const UChar* end)
285 term_len = ONIGENC_MBC_MINLEN(enc);
287 r = (UChar* )
xmalloc(slen + term_len);
288 CHECK_NULL_RETURN(r);
291 for (i = 0; i < term_len; i++)
292 r[slen + i] = (UChar )0;
303# define PFETCH_READY UChar* pfetch_prev = NULL; (void)pfetch_prev
305# define PFETCH_READY UChar* pfetch_prev
307#define PEND (p < end ? 0 : 1)
308#define PUNFETCH p = pfetch_prev
311 p += enclen(enc, p, end); \
313#define PFETCH(c) do { \
314 c = ((enc->max_enc_len == 1) ? *p : ONIGENC_MBC_TO_CODE(enc, p, end)); \
316 p += enclen(enc, p, end); \
320 p += enclen(enc, p, end); \
322#define PFETCH_S(c) do { \
323 c = ((enc->max_enc_len == 1) ? *p : ONIGENC_MBC_TO_CODE(enc, p, end)); \
324 p += enclen(enc, p, end); \
327#define PPEEK (p < end ? ONIGENC_MBC_TO_CODE(enc, p, end) : PEND_VALUE)
328#define PPEEK_IS(c) (PPEEK == (OnigCodePoint )c)
331strcat_capa(UChar* dest, UChar* dest_end,
const UChar* src,
const UChar* src_end,
341 CHECK_NULL_RETURN(r);
342 onig_strcpy(r + (dest_end - dest), src, src_end);
348strcat_capa_from_static(UChar* dest, UChar* dest_end,
349 const UChar* src,
const UChar* src_end,
size_t capa)
354 CHECK_NULL_RETURN(r);
355 onig_strcpy(r, dest, dest_end);
356 onig_strcpy(r + (dest_end - dest), src, src_end);
375str_end_cmp(st_data_t xp, st_data_t yp)
383 if ((x->end - x->s) != (y->end - y->s))
389 c = (int )*p - (
int )*q;
390 if (c != 0)
return c;
398NO_SANITIZE(
"unsigned-integer-overflow",
static st_index_t str_end_hash(st_data_t xp));
401str_end_hash(st_data_t xp)
409 val = val * 997 + (int )*p++;
412 return val + (val >> 5);
415extern hash_table_type*
416onig_st_init_strend_table_with_size(st_index_t size)
423 return (hash_table_type* )
424 onig_st_init_table_with_size(&hashType, size);
428onig_st_lookup_strend(hash_table_type* table,
const UChar* str_key,
429 const UChar* end_key, hash_data_type *value)
433 key.s = (UChar* )str_key;
434 key.end = (UChar* )end_key;
436 return onig_st_lookup(table, (st_data_t )(&key), value);
440onig_st_insert_strend(hash_table_type* table,
const UChar* str_key,
441 const UChar* end_key, hash_data_type value)
447 key->s = (UChar* )str_key;
448 key->end = (UChar* )end_key;
449 result = onig_st_insert(table, (st_data_t )key, value);
459#ifdef USE_NAMED_GROUP
461# define INIT_NAME_BACKREFS_ALLOC_NUM 8
472# ifdef USE_ST_LIBRARY
475typedef st_data_t HashDataType;
479i_print_name_entry(HashDataType key_, HashDataType e_, HashDataType arg_)
482 void* arg = (
void *)arg_;
486 fprintf(fp,
"%s: ", e->name);
487 if (e->back_num == 0)
489 else if (e->back_num == 1)
490 fprintf(fp,
"%d", e->back_ref1);
492 for (i = 0; i < e->back_num; i++) {
493 if (i > 0) fprintf(fp,
", ");
494 fprintf(fp,
"%d", e->back_refs[i]);
506 if (IS_NOT_NULL(t)) {
507 fprintf(fp,
"name table\n");
508 onig_st_foreach(t, i_print_name_entry, (HashDataType )fp);
516i_free_name_entry(HashDataType key_, HashDataType e_, HashDataType arg_ ARG_UNUSED)
518 UChar* key = (UChar *)key_;
532 if (IS_NOT_NULL(t)) {
533 onig_st_foreach(t, i_free_name_entry, 0);
544 r = names_clear(reg);
548 if (IS_NOT_NULL(t)) onig_st_free_table(t);
549 reg->name_table = (
void* )NULL;
554copy_named_captures_iter(
const OnigUChar *name,
const OnigUChar *name_end,
555 int back_num,
int *back_refs,
OnigRegex regex,
void *arg)
559 if (IS_NULL(entry_copy))
return -1;
561 entry_copy->name_len = name_end - name;
562 entry_copy->back_num = back_num;
563 entry_copy->back_alloc = back_num;
564 entry_copy->back_ref1 = back_refs[0];
565 entry_copy->back_refs =
xmalloc(back_num * (
sizeof(
int*)));
566 if (IS_NULL(entry_copy->back_refs)) {
570 memcpy(entry_copy->back_refs, back_refs, back_num *
sizeof(back_refs[0]));
572 UChar *new_name = strdup_with_null(regex->enc, name, name_end);
573 if (IS_NULL(new_name)) {
574 xfree(entry_copy->back_refs);
578 entry_copy->name = new_name;
580 if (onig_st_insert_strend(copy_table, new_name, (new_name + entry_copy->name_len), (hash_data_type)entry_copy)) {
581 xfree(entry_copy->name);
582 xfree(entry_copy->back_refs);
594 NameTable * t = onig_st_init_strend_table_with_size(onig_number_of_names(oreg));
595 CHECK_NULL_RETURN_MEMERR(t);
596 if (onig_foreach_name(oreg, copy_named_captures_iter, (
void*)t)) {
597 onig_st_free_table(t);
598 return ONIGERR_MEMORY;
606name_find(
regex_t* reg,
const UChar* name,
const UChar* name_end)
612 if (IS_NOT_NULL(t)) {
613 onig_st_lookup_strend(t, name, name_end, (HashDataType* )((
void* )(&e)));
619 int (*func)(
const UChar*,
const UChar*,int,
int*,
regex_t*,
void*);
627i_names(HashDataType key_ ARG_UNUSED, HashDataType e_, HashDataType arg_)
631 int r = (*(arg->func))(e->name,
632 e->name + e->name_len,
634 (e->back_num > 1 ? e->back_refs : &(e->back_ref1)),
645 int (*func)(
const UChar*,
const UChar*,
int,
int*,
regex_t*,
void*),
void* arg)
651 if (IS_NOT_NULL(t)) {
656 onig_st_foreach(t, i_names, (HashDataType )&narg);
662i_renumber_name(HashDataType key_ ARG_UNUSED, HashDataType e_, HashDataType map_)
668 if (e->back_num > 1) {
669 for (i = 0; i < e->back_num; i++) {
670 e->back_refs[i] = map[e->back_refs[i]].new_val;
673 else if (e->back_num == 1) {
674 e->back_ref1 = map[e->back_ref1].new_val;
685 if (IS_NOT_NULL(t)) {
686 onig_st_foreach(t, i_renumber_name, (HashDataType )map);
693onig_number_of_names(
const regex_t* reg)
698 return (
int )t->num_entries;
705# define INIT_NAMES_ALLOC_NUM 8
721 if (IS_NOT_NULL(t) && t->num > 0) {
722 fprintf(fp,
"name table\n");
723 for (i = 0; i < t->num; i++) {
725 fprintf(fp,
"%s: ", e->name);
726 if (e->back_num == 0) {
729 else if (e->back_num == 1) {
730 fprintf(fp,
"%d", e->back_ref1);
733 for (j = 0; j < e->back_num; j++) {
734 if (j > 0) fprintf(fp,
", ");
735 fprintf(fp,
"%d", e->back_refs[j]);
753 if (IS_NOT_NULL(t)) {
754 for (i = 0; i < t->num; i++) {
756 if (IS_NOT_NULL(e->name)) {
763 e->back_refs = (
int* )NULL;
780 r = names_clear(reg);
785 reg->name_table = NULL;
795 int i, num = ot->num;
797 CHECK_NULL_RETURN_MEMERR(t);
799 t->e =
xmalloc(t->alloc *
sizeof(t->e[0]));
802 return ONIGERR_MEMORY;
806 for (i = 0; i < num; t->num = ++i) {
812 e->name = strdup_with_null(enc, oe->name, oe->name + e->name_len);
813 if (IS_NULL(e->name)) {
814 onig_names_free(reg);
815 return ONIGERR_MEMORY;
817 e->back_refs =
xmalloc(e->back_alloc *
sizeof(e->back_refs[0]));
818 if (IS_NULL(e->back_refs)) {
820 onig_names_free(reg);
821 return ONIGERR_MEMORY;
823 memcpy(e->back_refs, oe->back_refs, e->back_num *
sizeof(e->back_refs[0]));
824 e->back_ref1 = e->back_refs[0];
831name_find(
regex_t* reg,
const UChar* name,
const UChar* name_end)
838 if (IS_NOT_NULL(t)) {
839 len = name_end - name;
840 for (i = 0; i < t->num; i++) {
842 if (
len == e->name_len && onig_strncmp(name, e->name,
len) == 0)
851 int (*func)(
const UChar*,
const UChar*,
int,
int*,
regex_t*,
void*),
void* arg)
857 if (IS_NOT_NULL(t)) {
858 for (i = 0; i < t->num; i++) {
860 r = (*func)(e->name, e->name + e->name_len, e->back_num,
861 (e->back_num > 1 ? e->back_refs : &(e->back_ref1)),
863 if (r != 0)
return r;
876 if (IS_NOT_NULL(t)) {
877 for (i = 0; i < t->num; i++) {
880 if (e->back_num > 1) {
881 for (j = 0; j < e->back_num; j++) {
882 e->back_refs[j] = map[e->back_refs[j]].new_val;
885 else if (e->back_num == 1) {
886 e->back_ref1 = map[e->back_ref1].new_val;
894onig_number_of_names(
const regex_t* reg)
907name_add(
regex_t* reg, UChar* name, UChar* name_end,
int backref,
ScanEnv* env)
913 if (name_end - name <= 0)
914 return ONIGERR_EMPTY_GROUP_NAME;
916 e = name_find(reg, name, name_end);
918# ifdef USE_ST_LIBRARY
920 t = onig_st_init_strend_table_with_size(5);
921 reg->name_table = (
void* )t;
924 CHECK_NULL_RETURN_MEMERR(e);
926 e->name = strdup_with_null(reg->enc, name, name_end);
927 if (IS_NULL(e->name)) {
929 return ONIGERR_MEMORY;
931 onig_st_insert_strend(t, e->name, (e->name + (name_end - name)),
934 e->name_len = name_end - name;
937 e->back_refs = (
int* )NULL;
942 alloc = INIT_NAMES_ALLOC_NUM;
944 CHECK_NULL_RETURN_MEMERR(t);
952 return ONIGERR_MEMORY;
958 else if (t->num == t->alloc) {
962 alloc = t->alloc * 2;
964 CHECK_NULL_RETURN_MEMERR(p);
969 for (i = t->num; i < t->alloc; i++) {
971 t->e[i].name_len = 0;
972 t->e[i].back_num = 0;
973 t->e[i].back_alloc = 0;
974 t->e[i].back_refs = (
int* )NULL;
979 e->name = strdup_with_null(reg->enc, name, name_end);
980 if (IS_NULL(e->name))
return ONIGERR_MEMORY;
981 e->name_len = name_end - name;
985 if (e->back_num >= 1 &&
986 ! IS_SYNTAX_BV(env->syntax, ONIG_SYN_ALLOW_MULTIPLEX_DEFINITION_NAME)) {
987 onig_scan_env_set_error_string(env, ONIGERR_MULTIPLEX_DEFINED_NAME,
989 return ONIGERR_MULTIPLEX_DEFINED_NAME;
993 if (e->back_num == 1) {
994 e->back_ref1 = backref;
997 if (e->back_num == 2) {
998 alloc = INIT_NAME_BACKREFS_ALLOC_NUM;
999 e->back_refs = (
int* )
xmalloc(
sizeof(
int) * alloc);
1000 CHECK_NULL_RETURN_MEMERR(e->back_refs);
1001 e->back_alloc = alloc;
1002 e->back_refs[0] = e->back_ref1;
1003 e->back_refs[1] = backref;
1006 if (e->back_num > e->back_alloc) {
1008 alloc = e->back_alloc * 2;
1009 p = (
int* )
xrealloc(e->back_refs,
sizeof(
int) * alloc);
1010 CHECK_NULL_RETURN_MEMERR(p);
1012 e->back_alloc = alloc;
1014 e->back_refs[e->back_num - 1] = backref;
1022onig_name_to_group_numbers(
regex_t* reg,
const UChar* name,
1023 const UChar* name_end,
int** nums)
1025 NameEntry* e = name_find(reg, name, name_end);
1027 if (IS_NULL(e))
return ONIGERR_UNDEFINED_NAME_REFERENCE;
1029 switch (e->back_num) {
1034 *nums = &(e->back_ref1);
1037 *nums = e->back_refs;
1044onig_name_to_backref_number(
regex_t* reg,
const UChar* name,
1045 const UChar* name_end,
const OnigRegion *region)
1049 n = onig_name_to_group_numbers(reg, name, name_end, &nums);
1053 return ONIGERR_PARSER_BUG;
1057 if (IS_NOT_NULL(region)) {
1058 for (i = n - 1; i >= 0; i--) {
1059 if (region->beg[nums[i]] != ONIG_REGION_NOTPOS)
1070onig_name_to_group_numbers(
regex_t* reg,
const UChar* name,
1071 const UChar* name_end,
int** nums)
1073 return ONIG_NO_SUPPORT_CONFIG;
1077onig_name_to_backref_number(
regex_t* reg,
const UChar* name,
1078 const UChar* name_end,
const OnigRegion* region)
1080 return ONIG_NO_SUPPORT_CONFIG;
1084onig_foreach_name(
regex_t* reg,
1085 int (*func)(
const UChar*,
const UChar*,
int,
int*,
regex_t*,
void*),
void* arg)
1087 return ONIG_NO_SUPPORT_CONFIG;
1091onig_number_of_names(
const regex_t* reg)
1104onig_noname_group_capture_is_active(
const regex_t* reg)
1106 if (ONIG_IS_OPTION_ON(reg->options, ONIG_OPTION_DONT_CAPTURE_GROUP))
1109#ifdef USE_NAMED_GROUP
1110 if (onig_number_of_names(reg) > 0 &&
1111 IS_SYNTAX_BV(reg->syntax, ONIG_SYN_CAPTURE_ONLY_NAMED_GROUP) &&
1112 !ONIG_IS_OPTION_ON(reg->options, ONIG_OPTION_CAPTURE_GROUP)) {
1121#define INIT_SCANENV_MEMNODES_ALLOC_SIZE 16
1128 BIT_STATUS_CLEAR(env->capture_history);
1129 BIT_STATUS_CLEAR(env->bt_mem_start);
1130 BIT_STATUS_CLEAR(env->bt_mem_end);
1131 BIT_STATUS_CLEAR(env->backrefed_mem);
1132 env->error = (UChar* )NULL;
1133 env->error_end = (UChar* )NULL;
1136#ifdef USE_NAMED_GROUP
1140 env->mem_nodes_dynamic = (
Node** )NULL;
1142 for (i = 0; i < SCANENV_MEMNODES_SIZE; i++)
1143 env->mem_nodes_static[i] = NULL_NODE;
1145#ifdef USE_COMBINATION_EXPLOSION_CHECK
1146 env->num_comb_exp_check = 0;
1147 env->comb_exp_max_regnum = 0;
1148 env->curr_max_regnum = 0;
1149 env->has_recursion = 0;
1151 env->parse_depth = 0;
1152 env->warnings_flag = 0;
1156scan_env_add_mem_entry(
ScanEnv* env)
1161 need = env->num_mem + 1;
1162 if (need > ONIG_MAX_CAPTURE_GROUP_NUM)
1163 return ONIGERR_TOO_MANY_CAPTURE_GROUPS;
1164 if (need >= SCANENV_MEMNODES_SIZE) {
1165 if (env->mem_alloc <= need) {
1166 if (IS_NULL(env->mem_nodes_dynamic)) {
1167 alloc = INIT_SCANENV_MEMNODES_ALLOC_SIZE;
1169 CHECK_NULL_RETURN_MEMERR(p);
1170 xmemcpy(p, env->mem_nodes_static,
1171 sizeof(
Node*) * SCANENV_MEMNODES_SIZE);
1174 alloc = env->mem_alloc * 2;
1176 CHECK_NULL_RETURN_MEMERR(p);
1179 for (i = env->num_mem + 1; i < alloc; i++)
1182 env->mem_nodes_dynamic = p;
1183 env->mem_alloc = alloc;
1188 return env->num_mem;
1192scan_env_set_mem_node(
ScanEnv* env,
int num,
Node* node)
1194 if (env->num_mem >= num)
1195 SCANENV_MEM_NODES(env)[num] = node;
1197 return ONIGERR_PARSER_BUG;
1203onig_node_free(
Node* node)
1206 if (IS_NULL(node)) return ;
1208 switch (NTYPE(node)) {
1210 if (NSTR(node)->
capa != 0 &&
1211 IS_NOT_NULL(NSTR(node)->s) && NSTR(node)->s != NSTR(node)->buf) {
1212 xfree(NSTR(node)->s);
1218 onig_node_free(NCAR(node));
1220 Node* next_node = NCDR(node);
1232 bbuf_free(cc->mbuf);
1237 onig_node_free(NQTFR(node)->target);
1241 onig_node_free(NENCLOSE(node)->target);
1245 xfree(NBREF(node)->back_dynamic);
1249 onig_node_free(NANCHOR(node)->target);
1269 BITSET_CLEAR(cc->bs);
1276node_new_cclass(
void)
1278 Node* node = node_new();
1279 CHECK_NULL_RETURN(node);
1281 SET_NTYPE(node, NT_CCLASS);
1282 initialize_cclass(NCCLASS(node));
1287node_new_ctype(
int type,
int not,
int ascii_range)
1289 Node* node = node_new();
1290 CHECK_NULL_RETURN(node);
1292 SET_NTYPE(node, NT_CTYPE);
1293 NCTYPE(node)->ctype =
type;
1294 NCTYPE(node)->not = not;
1295 NCTYPE(node)->ascii_range = ascii_range;
1300node_new_anychar(
void)
1302 Node* node = node_new();
1303 CHECK_NULL_RETURN(node);
1305 SET_NTYPE(node, NT_CANY);
1310node_new_list(
Node* left,
Node* right)
1312 Node* node = node_new();
1313 CHECK_NULL_RETURN(node);
1315 SET_NTYPE(node, NT_LIST);
1322onig_node_new_list(
Node* left,
Node* right)
1324 return node_new_list(left, right);
1328onig_node_list_add(
Node* list,
Node* x)
1332 n = onig_node_new_list(x, NULL);
1333 if (IS_NULL(n))
return NULL_NODE;
1335 if (IS_NOT_NULL(list)) {
1336 while (IS_NOT_NULL(NCDR(list)))
1346onig_node_new_alt(
Node* left,
Node* right)
1348 Node* node = node_new();
1349 CHECK_NULL_RETURN(node);
1351 SET_NTYPE(node, NT_ALT);
1358onig_node_new_anchor(
int type)
1360 Node* node = node_new();
1361 CHECK_NULL_RETURN(node);
1363 SET_NTYPE(node, NT_ANCHOR);
1364 NANCHOR(node)->type =
type;
1365 NANCHOR(node)->target = NULL;
1366 NANCHOR(node)->char_len = -1;
1367 NANCHOR(node)->ascii_range = 0;
1372node_new_backref(
int back_num,
int* backrefs,
int by_name,
1373#ifdef USE_BACKREF_WITH_LEVEL
1374 int exist_level,
int nest_level,
1379 Node* node = node_new();
1381 CHECK_NULL_RETURN(node);
1383 SET_NTYPE(node, NT_BREF);
1384 NBREF(node)->state = 0;
1385 NBREF(node)->back_num = back_num;
1386 NBREF(node)->back_dynamic = (
int* )NULL;
1388 NBREF(node)->state |= NST_NAME_REF;
1390#ifdef USE_BACKREF_WITH_LEVEL
1391 if (exist_level != 0) {
1392 NBREF(node)->state |= NST_NEST_LEVEL;
1393 NBREF(node)->nest_level = nest_level;
1397 for (i = 0; i < back_num; i++) {
1398 if (backrefs[i] <= env->num_mem &&
1399 IS_NULL(SCANENV_MEM_NODES(env)[backrefs[i]])) {
1400 NBREF(node)->state |= NST_RECURSION;
1405 if (back_num <= NODE_BACKREFS_SIZE) {
1406 for (i = 0; i < back_num; i++)
1407 NBREF(node)->back_static[i] = backrefs[i];
1410 int* p = (
int* )
xmalloc(
sizeof(
int) * back_num);
1412 onig_node_free(node);
1415 NBREF(node)->back_dynamic = p;
1416 for (i = 0; i < back_num; i++)
1422#ifdef USE_SUBEXP_CALL
1424node_new_call(UChar* name, UChar* name_end,
int gnum)
1426 Node* node = node_new();
1427 CHECK_NULL_RETURN(node);
1429 SET_NTYPE(node, NT_CALL);
1430 NCALL(node)->state = 0;
1431 NCALL(node)->target = NULL_NODE;
1432 NCALL(node)->name = name;
1433 NCALL(node)->name_end = name_end;
1434 NCALL(node)->group_num = gnum;
1440node_new_quantifier(
int lower,
int upper,
int by_number)
1442 Node* node = node_new();
1443 CHECK_NULL_RETURN(node);
1445 SET_NTYPE(node, NT_QTFR);
1446 NQTFR(node)->state = 0;
1447 NQTFR(node)->target = NULL;
1448 NQTFR(node)->lower = lower;
1449 NQTFR(node)->upper = upper;
1450 NQTFR(node)->greedy = 1;
1451 NQTFR(node)->target_empty_info = NQ_TARGET_ISNOT_EMPTY;
1452 NQTFR(node)->head_exact = NULL_NODE;
1453 NQTFR(node)->next_head_exact = NULL_NODE;
1454 NQTFR(node)->is_referred = 0;
1456 NQTFR(node)->state |= NST_BY_NUMBER;
1458#ifdef USE_COMBINATION_EXPLOSION_CHECK
1459 NQTFR(node)->comb_exp_check_num = 0;
1466node_new_enclose(
int type)
1468 Node* node = node_new();
1469 CHECK_NULL_RETURN(node);
1471 SET_NTYPE(node, NT_ENCLOSE);
1472 NENCLOSE(node)->type =
type;
1473 NENCLOSE(node)->state = 0;
1474 NENCLOSE(node)->regnum = 0;
1475 NENCLOSE(node)->option = 0;
1476 NENCLOSE(node)->target = NULL;
1477 NENCLOSE(node)->call_addr = -1;
1478 NENCLOSE(node)->opt_count = 0;
1483onig_node_new_enclose(
int type)
1485 return node_new_enclose(
type);
1489node_new_enclose_memory(OnigOptionType option,
int is_named)
1491 Node* node = node_new_enclose(ENCLOSE_MEMORY);
1492 CHECK_NULL_RETURN(node);
1494 SET_ENCLOSE_STATUS(node, NST_NAMED_GROUP);
1496#ifdef USE_SUBEXP_CALL
1497 NENCLOSE(node)->option = option;
1503node_new_option(OnigOptionType option)
1505 Node* node = node_new_enclose(ENCLOSE_OPTION);
1506 CHECK_NULL_RETURN(node);
1507 NENCLOSE(node)->option = option;
1512onig_node_str_cat(
Node* node,
const UChar* s,
const UChar* end)
1514 ptrdiff_t addlen = end - s;
1517 ptrdiff_t
len = NSTR(node)->end - NSTR(node)->s;
1519 if (NSTR(node)->
capa > 0 || (
len + addlen > NODE_STR_BUF_SIZE - 1)) {
1521 ptrdiff_t
capa =
len + addlen + NODE_STR_MARGIN;
1524 onig_strcpy(NSTR(node)->s +
len, s, end);
1527 if (NSTR(node)->s == NSTR(node)->buf)
1528 p = strcat_capa_from_static(NSTR(node)->s, NSTR(node)->end,
1531 p = strcat_capa(NSTR(node)->s, NSTR(node)->end, s, end,
capa);
1533 CHECK_NULL_RETURN_MEMERR(p);
1535 NSTR(node)->capa = (int )
capa;
1539 onig_strcpy(NSTR(node)->s +
len, s, end);
1541 NSTR(node)->end = NSTR(node)->s +
len + addlen;
1548onig_node_str_set(
Node* node,
const UChar* s,
const UChar* end)
1550 onig_node_str_clear(node);
1551 return onig_node_str_cat(node, s, end);
1555node_str_cat_char(
Node* node, UChar c)
1560 return onig_node_str_cat(node, s, s + 1);
1566 UChar buf[ONIGENC_CODE_TO_MBC_MAXLEN];
1567 int num = ONIGENC_CODE_TO_MBC(enc, c, buf);
1568 if (num < 0)
return num;
1569 return onig_node_str_cat(node, buf, buf + num);
1574onig_node_conv_to_str_node(
Node* node,
int flag)
1576 SET_NTYPE(node, NT_STR);
1577 NSTR(node)->flag = flag;
1578 NSTR(node)->capa = 0;
1579 NSTR(node)->s = NSTR(node)->buf;
1580 NSTR(node)->end = NSTR(node)->buf;
1585onig_node_str_clear(
Node* node)
1587 if (NSTR(node)->
capa != 0 &&
1588 IS_NOT_NULL(NSTR(node)->s) && NSTR(node)->s != NSTR(node)->buf) {
1589 xfree(NSTR(node)->s);
1592 NSTR(node)->capa = 0;
1593 NSTR(node)->flag = 0;
1594 NSTR(node)->s = NSTR(node)->buf;
1595 NSTR(node)->end = NSTR(node)->buf;
1599node_new_str(
const UChar* s,
const UChar* end)
1601 Node* node = node_new();
1602 CHECK_NULL_RETURN(node);
1604 SET_NTYPE(node, NT_STR);
1605 NSTR(node)->capa = 0;
1606 NSTR(node)->flag = 0;
1607 NSTR(node)->s = NSTR(node)->buf;
1608 NSTR(node)->end = NSTR(node)->buf;
1609 if (onig_node_str_cat(node, s, end)) {
1610 onig_node_free(node);
1617onig_node_new_str(
const UChar* s,
const UChar* end)
1619 return node_new_str(s, end);
1623node_new_str_raw(UChar* s, UChar* end)
1625 Node* node = node_new_str(s, end);
1626 if (IS_NOT_NULL(node))
1627 NSTRING_SET_RAW(node);
1634 return node_new_str(NULL, NULL);
1638node_new_str_raw_char(UChar c)
1643 return node_new_str_raw(p, p + 1);
1650 Node* n = NULL_NODE;
1652 if (sn->end > sn->s) {
1653 p = onigenc_get_prev_char_head(enc, sn->s, sn->end, sn->end);
1654 if (p && p > sn->s) {
1655 n = node_new_str(p, sn->end);
1656 if (IS_NOT_NULL(n) && (sn->flag & NSTR_RAW) != 0)
1658 sn->end = (UChar* )p;
1667 if (sn->end > sn->s) {
1668 return ((enclen(enc, sn->s, sn->end) < sn->end - sn->s) ? 1 : 0);
1673#ifdef USE_PAD_TO_SHORT_BYTE_CHAR
1675node_str_head_pad(
StrNode* sn,
int num, UChar val)
1677 UChar buf[NODE_STR_BUF_SIZE];
1680 len = sn->end - sn->s;
1681 onig_strcpy(buf, sn->s, sn->end);
1682 onig_strcpy(&(sn->s[num]), buf, buf +
len);
1685 for (i = 0; i < num; i++) {
1692onig_scan_unsigned_number(UChar** src,
const UChar* end,
OnigEncoding enc)
1694 unsigned int num, val;
1702 if (ONIGENC_IS_CODE_DIGIT(enc, c)) {
1703 val = (
unsigned int )DIGITVAL(c);
1704 if ((INT_MAX_LIMIT - val) / 10UL < num)
1707 num = num * 10 + val;
1719scan_unsigned_hexadecimal_number(UChar** src, UChar* end,
int minlen,
1723 unsigned int num, val;
1728 restlen = maxlen - minlen;
1730 while (!PEND && maxlen-- != 0) {
1732 if (ONIGENC_IS_CODE_XDIGIT(enc, c)) {
1733 val = (
unsigned int )XDIGITVAL(enc,c);
1734 if ((INT_MAX_LIMIT - val) / 16UL < num)
1737 num = (num << 4) + XDIGITVAL(enc,c);
1745 if (maxlen > restlen)
1752scan_unsigned_octal_number(UChar** src, UChar* end,
int maxlen,
1756 unsigned int num, val;
1761 while (!PEND && maxlen-- != 0) {
1763 if (ONIGENC_IS_CODE_DIGIT(enc, c) && c <
'8') {
1765 if ((INT_MAX_LIMIT - val) / 8UL < num)
1768 num = (num << 3) + val;
1780#define BBUF_WRITE_CODE_POINT(bbuf,pos,code) \
1781 BBUF_WRITE(bbuf, pos, &(code), SIZE_CODE_POINT)
1788new_code_range(
BBuf** pbuf)
1790#define INIT_MULTI_BYTE_RANGE_SIZE (SIZE_CODE_POINT * 5)
1796 CHECK_NULL_RETURN_MEMERR(*pbuf);
1797 r = BBUF_INIT(*pbuf, INIT_MULTI_BYTE_RANGE_SIZE);
1801 BBUF_WRITE_CODE_POINT(bbuf, 0, n);
1806add_code_range_to_buf0(
BBuf** pbuf,
ScanEnv* env, OnigCodePoint from, OnigCodePoint to,
1810 OnigCodePoint low, high, bound, x;
1811 OnigCodePoint n, *data;
1815 n = from; from = to; to = n;
1818 if (IS_NULL(*pbuf)) {
1819 r = new_code_range(pbuf);
1826 GET_CODE_POINT(n, bbuf->p);
1828 data = (OnigCodePoint* )(bbuf->p);
1831 bound = (from == 0) ? 0 : n;
1832 for (low = 0; low < bound; ) {
1833 x = (low + bound) >> 1;
1834 if (from - 1 > data[x*2 + 1])
1840 high = (to == ONIG_LAST_CODE_POINT) ? n : low;
1841 for (bound = n; high < bound; ) {
1842 x = (high + bound) >> 1;
1843 if (to + 1 >= data[x*2])
1852 inc_n = low + 1 - high;
1853 if (n + inc_n > ONIG_MAX_MULTI_BYTE_RANGES_NUM)
1854 return ONIGERR_TOO_MANY_MULTI_BYTE_RANGES;
1857 if (checkdup && from <= data[low*2+1]
1858 && (data[low*2] <= from || data[low*2+1] <= to))
1859 CC_DUP_WARN(env, from, to);
1860 if (from > data[low*2])
1862 if (to < data[(high - 1)*2 + 1])
1863 to = data[(high - 1)*2 + 1];
1867 int from_pos = SIZE_CODE_POINT * (1 + high * 2);
1868 int to_pos = SIZE_CODE_POINT * (1 + (low + 1) * 2);
1872 int size = (n - high) * 2 * SIZE_CODE_POINT;
1873 BBUF_MOVE_RIGHT(bbuf, from_pos, to_pos, size);
1877 BBUF_MOVE_LEFT_REDUCE(bbuf, from_pos, to_pos);
1881 pos = SIZE_CODE_POINT * (1 + low * 2);
1882 BBUF_ENSURE_SIZE(bbuf, pos + SIZE_CODE_POINT * 2);
1883 BBUF_WRITE_CODE_POINT(bbuf, pos, from);
1884 BBUF_WRITE_CODE_POINT(bbuf, pos + SIZE_CODE_POINT, to);
1886 BBUF_WRITE_CODE_POINT(bbuf, 0, n);
1892add_code_range_to_buf(
BBuf** pbuf,
ScanEnv* env, OnigCodePoint from, OnigCodePoint to)
1894 return add_code_range_to_buf0(pbuf, env, from, to, 1);
1898add_code_range0(
BBuf** pbuf,
ScanEnv* env, OnigCodePoint from, OnigCodePoint to,
int checkdup)
1901 if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_ALLOW_EMPTY_RANGE_IN_CC))
1904 return ONIGERR_EMPTY_RANGE_IN_CHAR_CLASS;
1907 return add_code_range_to_buf0(pbuf, env, from, to, checkdup);
1911add_code_range(
BBuf** pbuf,
ScanEnv* env, OnigCodePoint from, OnigCodePoint to)
1913 return add_code_range0(pbuf, env, from, to, 1);
1920 OnigCodePoint pre, from, *data, to = 0;
1922 *pbuf = (
BBuf* )NULL;
1923 if (IS_NULL(bbuf)) {
1925 return SET_ALL_MULTI_BYTE_RANGE(enc, pbuf);
1928 data = (OnigCodePoint* )(bbuf->p);
1929 GET_CODE_POINT(n, data);
1931 if (n <= 0)
goto set_all;
1934 pre = MBCODE_START_POS(enc);
1935 for (i = 0; i < n; i++) {
1938 if (pre <= from - 1) {
1939 r = add_code_range_to_buf(pbuf, env, pre, from - 1);
1940 if (r != 0)
return r;
1942 if (to == ONIG_LAST_CODE_POINT)
break;
1945 if (to < ONIG_LAST_CODE_POINT) {
1946 r = add_code_range_to_buf(pbuf, env, to + 1, ONIG_LAST_CODE_POINT);
1951#define SWAP_BBUF_NOT(bbuf1, not1, bbuf2, not2) do {\
1954 tnot = not1; not1 = not2; not2 = tnot; \
1955 tbuf = bbuf1; bbuf1 = bbuf2; bbuf2 = tbuf; \
1963 OnigCodePoint i, n1, *data1;
1964 OnigCodePoint from, to;
1966 *pbuf = (
BBuf* )NULL;
1967 if (IS_NULL(bbuf1) && IS_NULL(bbuf2)) {
1968 if (not1 != 0 || not2 != 0)
1969 return SET_ALL_MULTI_BYTE_RANGE(enc, pbuf);
1975 SWAP_BBUF_NOT(bbuf1, not1, bbuf2, not2);
1977 if (IS_NULL(bbuf1)) {
1979 return SET_ALL_MULTI_BYTE_RANGE(enc, pbuf);
1983 return bbuf_clone(pbuf, bbuf2);
1986 return not_code_range_buf(enc, bbuf2, pbuf, env);
1992 SWAP_BBUF_NOT(bbuf1, not1, bbuf2, not2);
1994 data1 = (OnigCodePoint* )(bbuf1->p);
1995 GET_CODE_POINT(n1, data1);
1998 if (not2 == 0 && not1 == 0) {
1999 r = bbuf_clone(pbuf, bbuf2);
2001 else if (not1 == 0) {
2002 r = not_code_range_buf(enc, bbuf2, pbuf, env);
2004 if (r != 0)
return r;
2006 for (i = 0; i < n1; i++) {
2009 r = add_code_range_to_buf(pbuf, env, from, to);
2010 if (r != 0)
return r;
2016and_code_range1(
BBuf** pbuf,
ScanEnv* env, OnigCodePoint from1, OnigCodePoint to1,
2017 OnigCodePoint* data,
int n)
2020 OnigCodePoint from2, to2;
2022 for (i = 0; i < n; i++) {
2025 if (from2 < from1) {
2026 if (to2 < from1)
continue;
2031 else if (from2 <= to1) {
2033 if (from1 <= from2 - 1) {
2034 r = add_code_range_to_buf(pbuf, env, from1, from2-1);
2035 if (r != 0)
return r;
2046 if (from1 > to1)
break;
2049 r = add_code_range_to_buf(pbuf, env, from1, to1);
2050 if (r != 0)
return r;
2056and_code_range_buf(
BBuf* bbuf1,
int not1,
BBuf* bbuf2,
int not2,
BBuf** pbuf,
ScanEnv* env)
2059 OnigCodePoint i, j, n1, n2, *data1, *data2;
2060 OnigCodePoint from, to, from1, to1, from2, to2;
2062 *pbuf = (
BBuf* )NULL;
2063 if (IS_NULL(bbuf1)) {
2064 if (not1 != 0 && IS_NOT_NULL(bbuf2))
2065 return bbuf_clone(pbuf, bbuf2);
2068 else if (IS_NULL(bbuf2)) {
2070 return bbuf_clone(pbuf, bbuf1);
2075 SWAP_BBUF_NOT(bbuf1, not1, bbuf2, not2);
2077 data1 = (OnigCodePoint* )(bbuf1->p);
2078 data2 = (OnigCodePoint* )(bbuf2->p);
2079 GET_CODE_POINT(n1, data1);
2080 GET_CODE_POINT(n2, data2);
2084 if (not2 == 0 && not1 == 0) {
2085 for (i = 0; i < n1; i++) {
2088 for (j = 0; j < n2; j++) {
2091 if (from2 > to1)
break;
2092 if (to2 < from1)
continue;
2093 from = MAX(from1, from2);
2095 r = add_code_range_to_buf(pbuf, env, from, to);
2096 if (r != 0)
return r;
2100 else if (not1 == 0) {
2101 for (i = 0; i < n1; i++) {
2104 r = and_code_range1(pbuf, env, from1, to1, data2, n2);
2105 if (r != 0)
return r;
2117 BBuf *buf1, *buf2, *pbuf = 0;
2118 BitSetRef bsr1, bsr2;
2121 not1 = IS_NCCLASS_NOT(dest);
2124 not2 = IS_NCCLASS_NOT(cc);
2129 bitset_invert_to(bsr1, bs1);
2133 bitset_invert_to(bsr2, bs2);
2136 bitset_and(bsr1, bsr2);
2137 if (bsr1 != dest->bs) {
2138 bitset_copy(dest->bs, bsr1);
2142 bitset_invert(dest->bs);
2145 if (! ONIGENC_IS_SINGLEBYTE(enc)) {
2146 if (not1 != 0 && not2 != 0) {
2147 r = or_code_range_buf(enc, buf1, 0, buf2, 0, &pbuf, env);
2150 r = and_code_range_buf(buf1, not1, buf2, not2, &pbuf, env);
2151 if (r == 0 && not1 != 0) {
2153 r = not_code_range_buf(enc, pbuf, &tbuf, env);
2175 BBuf *buf1, *buf2, *pbuf = 0;
2176 BitSetRef bsr1, bsr2;
2179 not1 = IS_NCCLASS_NOT(dest);
2182 not2 = IS_NCCLASS_NOT(cc);
2187 bitset_invert_to(bsr1, bs1);
2191 bitset_invert_to(bsr2, bs2);
2194 bitset_or(bsr1, bsr2);
2195 if (bsr1 != dest->bs) {
2196 bitset_copy(dest->bs, bsr1);
2200 bitset_invert(dest->bs);
2203 if (! ONIGENC_IS_SINGLEBYTE(enc)) {
2204 if (not1 != 0 && not2 != 0) {
2205 r = and_code_range_buf(buf1, 0, buf2, 0, &pbuf, env);
2208 r = or_code_range_buf(enc, buf1, not1, buf2, not2, &pbuf, env);
2209 if (r == 0 && not1 != 0) {
2211 r = not_code_range_buf(enc, pbuf, &tbuf, env);
2229static void UNKNOWN_ESC_WARN(
ScanEnv *env,
int c);
2232conv_backslash_value(OnigCodePoint c,
ScanEnv* env)
2234 if (IS_SYNTAX_OP(env->syntax, ONIG_SYN_OP_ESC_CONTROL_CHARS)) {
2236 case 'n':
return '\n';
2237 case 't':
return '\t';
2238 case 'r':
return '\r';
2239 case 'f':
return '\f';
2240 case 'a':
return '\007';
2241 case 'b':
return '\010';
2242 case 'e':
return '\033';
2244 if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_ESC_V_VTAB))
2249 if ((
'a' <= c && c <=
'z') || (
'A' <= c && c <=
'Z'))
2250 UNKNOWN_ESC_WARN(env, c);
2257#ifdef USE_NO_INVALID_QUANTIFIER
2258# define is_invalid_quantifier_target(node) 0
2261is_invalid_quantifier_target(
Node* node)
2263 switch (NTYPE(node)) {
2275 if (! is_invalid_quantifier_target(NCAR(node)))
return 0;
2276 }
while (IS_NOT_NULL(node = NCDR(node)));
2282 if (is_invalid_quantifier_target(NCAR(node)))
return 1;
2283 }
while (IS_NOT_NULL(node = NCDR(node)));
2298 if (q->lower == 0) {
2299 if (q->upper == 1)
return 0;
2300 else if (IS_REPEAT_INFINITE(q->upper))
return 1;
2302 else if (q->lower == 1) {
2303 if (IS_REPEAT_INFINITE(q->upper))
return 2;
2307 if (q->lower == 0) {
2308 if (q->upper == 1)
return 3;
2309 else if (IS_REPEAT_INFINITE(q->upper))
return 4;
2311 else if (q->lower == 1) {
2312 if (IS_REPEAT_INFINITE(q->upper))
return 5;
2328static enum ReduceType
const ReduceTypeTable[6][6] = {
2330 {RQ_DEL, RQ_A, RQ_A, RQ_QQ, RQ_AQ, RQ_ASIS},
2331 {RQ_DEL, RQ_DEL, RQ_DEL, RQ_P_QQ, RQ_P_QQ, RQ_DEL},
2332 {RQ_A, RQ_A, RQ_DEL, RQ_ASIS, RQ_P_QQ, RQ_DEL},
2333 {RQ_DEL, RQ_AQ, RQ_AQ, RQ_DEL, RQ_AQ, RQ_AQ},
2334 {RQ_DEL, RQ_DEL, RQ_DEL, RQ_DEL, RQ_DEL, RQ_DEL},
2335 {RQ_ASIS, RQ_ASIS, RQ_ASIS, RQ_AQ, RQ_AQ, RQ_DEL}
2339onig_reduce_nested_quantifier(
Node* pnode,
Node* cnode)
2346 pnum = popular_quantifier_num(p);
2347 cnum = popular_quantifier_num(c);
2348 if (pnum < 0 || cnum < 0) return ;
2350 switch (ReduceTypeTable[cnum][pnum]) {
2355 p->target = c->target;
2356 p->lower = 0; p->upper = REPEAT_INFINITE; p->greedy = 1;
2359 p->target = c->target;
2360 p->lower = 0; p->upper = REPEAT_INFINITE; p->greedy = 0;
2363 p->target = c->target;
2364 p->lower = 0; p->upper = 1; p->greedy = 0;
2368 p->lower = 0; p->upper = 1; p->greedy = 0;
2369 c->lower = 1; c->upper = REPEAT_INFINITE; c->greedy = 1;
2378 c->target = NULL_NODE;
2379 onig_node_free(cnode);
2404 TK_EXTENDED_GRAPHEME_CLUSTER,
2409 TK_POSIX_BRACKET_OPEN,
2415 enum TokenSyms
type;
2438#ifdef USE_BACKREF_WITH_LEVEL
2458fetch_range_quantifier(UChar** src, UChar* end,
OnigToken* tok,
ScanEnv* env)
2460 int low, up, syn_allow, non_low = 0;
2467 syn_allow = IS_SYNTAX_BV(env->syntax, ONIG_SYN_ALLOW_INVALID_INTERVAL);
2473 return ONIGERR_END_PATTERN_AT_LEFT_BRACE;
2478 if (c ==
')' || c ==
'(' || c ==
'|') {
2479 return ONIGERR_END_PATTERN_AT_LEFT_BRACE;
2483 low = onig_scan_unsigned_number(&p, end, env->enc);
2484 if (low < 0)
return ONIGERR_TOO_BIG_NUMBER_FOR_REPEAT_RANGE;
2485 if (low > ONIG_MAX_REPEAT_NUM)
2486 return ONIGERR_TOO_BIG_NUMBER_FOR_REPEAT_RANGE;
2489 if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_ALLOW_INTERVAL_LOW_ABBREV)) {
2498 if (PEND)
goto invalid;
2502 up = onig_scan_unsigned_number(&p, end, env->enc);
2503 if (up < 0)
return ONIGERR_TOO_BIG_NUMBER_FOR_REPEAT_RANGE;
2504 if (up > ONIG_MAX_REPEAT_NUM)
2505 return ONIGERR_TOO_BIG_NUMBER_FOR_REPEAT_RANGE;
2510 up = REPEAT_INFINITE;
2522 if (PEND)
goto invalid;
2524 if (IS_SYNTAX_OP(env->syntax, ONIG_SYN_OP_ESC_BRACE_INTERVAL)) {
2525 if (c != MC_ESC(env->syntax))
goto invalid;
2526 if (PEND)
goto invalid;
2529 if (c !=
'}')
goto invalid;
2531 if (!IS_REPEAT_INFINITE(up) && low > up) {
2532 return ONIGERR_UPPER_SMALLER_THAN_LOWER_IN_REPEAT_RANGE;
2535 tok->type = TK_INTERVAL;
2536 tok->u.repeat.lower = low;
2537 tok->u.repeat.upper = up;
2545 return ONIGERR_INVALID_REPEAT_RANGE_PATTERN;
2550fetch_escaped_value(UChar** src, UChar* end,
ScanEnv* env, OnigCodePoint* val)
2557 if (PEND)
return ONIGERR_END_PATTERN_AT_ESCAPE;
2562 if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_ESC_CAPITAL_M_BAR_META)) {
2563 if (PEND)
return ONIGERR_END_PATTERN_AT_META;
2565 if (c !=
'-')
return ONIGERR_META_CODE_SYNTAX;
2566 if (PEND)
return ONIGERR_END_PATTERN_AT_META;
2568 if (c == MC_ESC(env->syntax)) {
2569 v = fetch_escaped_value(&p, end, env, &c);
2570 if (v < 0)
return v;
2572 c = ((c & 0xff) | 0x80);
2579 if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_ESC_CAPITAL_C_BAR_CONTROL)) {
2580 if (PEND)
return ONIGERR_END_PATTERN_AT_CONTROL;
2582 if (c !=
'-')
return ONIGERR_CONTROL_CODE_SYNTAX;
2589 if (IS_SYNTAX_OP(env->syntax, ONIG_SYN_OP_ESC_C_CONTROL)) {
2591 if (PEND)
return ONIGERR_END_PATTERN_AT_CONTROL;
2597 if (c == MC_ESC(env->syntax)) {
2598 v = fetch_escaped_value(&p, end, env, &c);
2599 if (v < 0)
return v;
2610 c = conv_backslash_value(c, env);
2620static int fetch_token(
OnigToken* tok, UChar** src, UChar* end,
ScanEnv* env);
2623get_name_end_code_point(OnigCodePoint start)
2626 case '<':
return (OnigCodePoint )
'>';
break;
2627 case '\'':
return (OnigCodePoint )
'\'';
break;
2628 case '(':
return (OnigCodePoint )
')';
break;
2629 case '{':
return (OnigCodePoint )
'}';
break;
2634 return (OnigCodePoint )0;
2637#ifdef USE_NAMED_GROUP
2639# define ONIGENC_IS_CODE_NAME(enc, c) TRUE
2641# define ONIGENC_IS_CODE_NAME(enc, c) ONIGENC_IS_CODE_WORD(enc, c)
2644# ifdef USE_BACKREF_WITH_LEVEL
2651fetch_name_with_level(OnigCodePoint start_code, UChar** src, UChar* end,
2652 UChar** rname_end,
ScanEnv* env,
2653 int* rback_num,
int* rlevel)
2655 int r, sign, is_num, exist_level;
2656 OnigCodePoint end_code;
2657 OnigCodePoint c = 0;
2665 is_num = exist_level = 0;
2669 end_code = get_name_end_code_point(start_code);
2674 return ONIGERR_EMPTY_GROUP_NAME;
2679 return ONIGERR_EMPTY_GROUP_NAME;
2681 if (ONIGENC_IS_CODE_DIGIT(enc, c)) {
2684 else if (c ==
'-') {
2689 else if (!ONIGENC_IS_CODE_NAME(enc, c)) {
2690 r = ONIGERR_INVALID_CHAR_IN_GROUP_NAME;
2697 if (c == end_code || c ==
')' || c ==
'+' || c ==
'-') {
2698 if (is_num == 2) r = ONIGERR_INVALID_GROUP_NAME;
2703 if (ONIGENC_IS_CODE_DIGIT(enc, c)) {
2707 r = ONIGERR_INVALID_GROUP_NAME;
2711 else if (!ONIGENC_IS_CODE_NAME(enc, c)) {
2712 r = ONIGERR_INVALID_CHAR_IN_GROUP_NAME;
2716 if (r == 0 && c != end_code) {
2717 if (c ==
'+' || c ==
'-') {
2719 int flag = (c ==
'-' ? -1 : 1);
2722 r = ONIGERR_INVALID_CHAR_IN_GROUP_NAME;
2726 if (! ONIGENC_IS_CODE_DIGIT(enc, c))
goto err;
2728 level = onig_scan_unsigned_number(&p, end, enc);
2729 if (level < 0)
return ONIGERR_TOO_BIG_NUMBER;
2730 *rlevel = (level * flag);
2741 r = ONIGERR_INVALID_GROUP_NAME;
2748 *rback_num = onig_scan_unsigned_number(&pnum_head, name_end, enc);
2749 if (*rback_num < 0)
return ONIGERR_TOO_BIG_NUMBER;
2750 else if (*rback_num == 0)
goto err;
2755 *rname_end = name_end;
2757 return (exist_level ? 1 : 0);
2760 onig_scan_env_set_error_string(env, r, *src, name_end);
2771fetch_name(OnigCodePoint start_code, UChar** src, UChar* end,
2772 UChar** rname_end,
ScanEnv* env,
int* rback_num,
int ref)
2774 int r, is_num, sign;
2775 OnigCodePoint end_code;
2776 OnigCodePoint c = 0;
2784 end_code = get_name_end_code_point(start_code);
2792 return ONIGERR_EMPTY_GROUP_NAME;
2797 return ONIGERR_EMPTY_GROUP_NAME;
2799 if (ONIGENC_IS_CODE_DIGIT(enc, c)) {
2803 r = ONIGERR_INVALID_GROUP_NAME;
2807 else if (c ==
'-') {
2814 r = ONIGERR_INVALID_GROUP_NAME;
2818 else if (!ONIGENC_IS_CODE_NAME(enc, c)) {
2819 r = ONIGERR_INVALID_CHAR_IN_GROUP_NAME;
2827 if (c == end_code || c ==
')') {
2829 r = ONIGERR_INVALID_GROUP_NAME;
2836 if (ONIGENC_IS_CODE_DIGIT(enc, c)) {
2840 if (!ONIGENC_IS_CODE_WORD(enc, c))
2841 r = ONIGERR_INVALID_CHAR_IN_GROUP_NAME;
2843 r = ONIGERR_INVALID_GROUP_NAME;
2848 if (!ONIGENC_IS_CODE_NAME(enc, c)) {
2849 r = ONIGERR_INVALID_CHAR_IN_GROUP_NAME;
2855 if (c != end_code) {
2856 r = ONIGERR_INVALID_GROUP_NAME;
2862 *rback_num = onig_scan_unsigned_number(&pnum_head, name_end, enc);
2863 if (*rback_num < 0)
return ONIGERR_TOO_BIG_NUMBER;
2864 else if (*rback_num == 0) {
2865 r = ONIGERR_INVALID_GROUP_NAME;
2872 *rname_end = name_end;
2881 if (c == end_code || c ==
')')
2888 onig_scan_env_set_error_string(env, r, *src, name_end);
2894fetch_name(OnigCodePoint start_code, UChar** src, UChar* end,
2895 UChar** rname_end,
ScanEnv* env,
int* rback_num,
int ref)
2897 int r, is_num, sign;
2898 OnigCodePoint end_code;
2899 OnigCodePoint c = 0;
2908 end_code = get_name_end_code_point(start_code);
2910 *rname_end = name_end = end;
2917 return ONIGERR_EMPTY_GROUP_NAME;
2922 return ONIGERR_EMPTY_GROUP_NAME;
2924 if (ONIGENC_IS_CODE_DIGIT(enc, c)) {
2927 else if (c ==
'-') {
2933 r = ONIGERR_INVALID_CHAR_IN_GROUP_NAME;
2941 if (c == end_code || c ==
')')
break;
2942 if (! ONIGENC_IS_CODE_DIGIT(enc, c))
2943 r = ONIGERR_INVALID_CHAR_IN_GROUP_NAME;
2945 if (r == 0 && c != end_code) {
2946 r = ONIGERR_INVALID_GROUP_NAME;
2951 *rback_num = onig_scan_unsigned_number(&pnum_head, name_end, enc);
2952 if (*rback_num < 0)
return ONIGERR_TOO_BIG_NUMBER;
2953 else if (*rback_num == 0) {
2954 r = ONIGERR_INVALID_GROUP_NAME;
2959 *rname_end = name_end;
2965 onig_scan_env_set_error_string(env, r, *src, name_end);
2973PRINTF_ARGS(
static void onig_syntax_warn(
ScanEnv *env,
const char *fmt, ...), 2, 3);
2977onig_syntax_warn(
ScanEnv *env,
const char *fmt, ...)
2980 UChar buf[WARN_BUFSIZE];
2981 va_start(args, fmt);
2982 onig_vsnprintf_with_pattern(buf, WARN_BUFSIZE, env->enc,
2983 env->pattern, env->pattern_end,
2987 if (env->sourcefile == NULL)
2990 rb_compile_warn(env->sourcefile, env->sourceline,
"%s", (
char *)buf);
2992 (*onig_warn)((
char* )buf);
2997CC_ESC_WARN(
ScanEnv *env, UChar *c)
2999 if (onig_warn == onig_null_warn) return ;
3001 if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_WARN_CC_OP_NOT_ESCAPED) &&
3002 IS_SYNTAX_BV(env->syntax, ONIG_SYN_BACKSLASH_ESCAPE_IN_CC)) {
3003 onig_syntax_warn(env,
"character class has '%s' without escape", c);
3008CLOSE_BRACKET_WITHOUT_ESC_WARN(
ScanEnv* env, UChar* c)
3010 if (onig_warn == onig_null_warn) return ;
3012 if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_WARN_CC_OP_NOT_ESCAPED)) {
3013 onig_syntax_warn(env,
"regular expression has '%s' without escape", c);
3022CC_DUP_WARN(
ScanEnv *env, OnigCodePoint from ARG_UNUSED, OnigCodePoint to ARG_UNUSED)
3026 if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_WARN_CC_DUP) &&
3027 !(env->warnings_flag & ONIG_SYN_WARN_CC_DUP)) {
3028#ifdef WARN_ALL_CC_DUP
3029 onig_syntax_warn(env,
"character class has duplicated range: %04x-%04x", from, to);
3031 env->warnings_flag |= ONIG_SYN_WARN_CC_DUP;
3032 onig_syntax_warn(env,
"character class has duplicated range");
3038UNKNOWN_ESC_WARN(
ScanEnv *env,
int c)
3041 onig_syntax_warn(env,
"Unknown escape \\%c is ignored", c);
3045find_str_position(OnigCodePoint s[],
int n, UChar* from, UChar* to,
3054 x = ONIGENC_MBC_TO_CODE(enc, p, to);
3055 q = p + enclen(enc, p, to);
3057 for (i = 1; i < n && q < to; i++) {
3058 x = ONIGENC_MBC_TO_CODE(enc, q, to);
3059 if (x != s[i])
break;
3060 q += enclen(enc, q, to);
3063 if (IS_NOT_NULL(next))
3074str_exist_check_with_esc(OnigCodePoint s[],
int n, UChar* from, UChar* to,
3086 p += enclen(enc, p, to);
3089 x = ONIGENC_MBC_TO_CODE(enc, p, to);
3090 q = p + enclen(enc, p, to);
3092 for (i = 1; i < n && q < to; i++) {
3093 x = ONIGENC_MBC_TO_CODE(enc, q, to);
3094 if (x != s[i])
break;
3095 q += enclen(enc, q, to);
3097 if (i >= n)
return 1;
3098 p += enclen(enc, p, to);
3101 x = ONIGENC_MBC_TO_CODE(enc, p, to);
3102 if (x == bad)
return 0;
3103 else if (x == MC_ESC(syn)) in_esc = 1;
3115 OnigCodePoint c, c2;
3128 tok->type = TK_CHAR;
3134 tok->type = TK_CC_CLOSE;
3136 else if (c ==
'-') {
3137 tok->type = TK_CC_RANGE;
3139 else if (c == MC_ESC(syn)) {
3140 if (! IS_SYNTAX_BV(syn, ONIG_SYN_BACKSLASH_ESCAPE_IN_CC))
3143 if (PEND)
return ONIGERR_END_PATTERN_AT_ESCAPE;
3150 tok->type = TK_CHAR_TYPE;
3151 tok->u.prop.ctype = ONIGENC_CTYPE_WORD;
3152 tok->u.prop.not = 0;
3155 tok->type = TK_CHAR_TYPE;
3156 tok->u.prop.ctype = ONIGENC_CTYPE_WORD;
3157 tok->u.prop.not = 1;
3160 tok->type = TK_CHAR_TYPE;
3161 tok->u.prop.ctype = ONIGENC_CTYPE_DIGIT;
3162 tok->u.prop.not = 0;
3165 tok->type = TK_CHAR_TYPE;
3166 tok->u.prop.ctype = ONIGENC_CTYPE_DIGIT;
3167 tok->u.prop.not = 1;
3170 tok->type = TK_CHAR_TYPE;
3171 tok->u.prop.ctype = ONIGENC_CTYPE_SPACE;
3172 tok->u.prop.not = 0;
3175 tok->type = TK_CHAR_TYPE;
3176 tok->u.prop.ctype = ONIGENC_CTYPE_SPACE;
3177 tok->u.prop.not = 1;
3180 if (! IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_H_XDIGIT))
break;
3181 tok->type = TK_CHAR_TYPE;
3182 tok->u.prop.ctype = ONIGENC_CTYPE_XDIGIT;
3183 tok->u.prop.not = 0;
3186 if (! IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_H_XDIGIT))
break;
3187 tok->type = TK_CHAR_TYPE;
3188 tok->u.prop.ctype = ONIGENC_CTYPE_XDIGIT;
3189 tok->u.prop.not = 1;
3198 IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_P_BRACE_CHAR_PROPERTY)) {
3200 tok->type = TK_CHAR_PROPERTY;
3201 tok->u.prop.not = (c ==
'P' ? 1 : 0);
3203 if (!PEND && IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_P_BRACE_CIRCUMFLEX_NOT)) {
3206 tok->u.prop.not = (tok->u.prop.not == 0 ? 1 : 0);
3213 onig_syntax_warn(env,
"invalid Unicode Property \\%c", c);
3221 if (PPEEK_IS(
'{') && IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_X_BRACE_HEX8)) {
3223 num = scan_unsigned_hexadecimal_number(&p, end, 0, 8, enc);
3224 if (num < 0)
return ONIGERR_TOO_BIG_WIDE_CHAR_VALUE;
3227 if (ONIGENC_IS_CODE_XDIGIT(enc, c2))
3228 return ONIGERR_TOO_LONG_WIDE_CHAR_VALUE;
3231 if (p > prev + enclen(enc, prev, end) && !PEND && (PPEEK_IS(
'}'))) {
3233 tok->type = TK_CODE_POINT;
3235 tok->u.code = (OnigCodePoint )num;
3242 else if (IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_X_HEX2)) {
3243 num = scan_unsigned_hexadecimal_number(&p, end, 0, 2, enc);
3244 if (num < 0)
return ONIGERR_TOO_BIG_NUMBER;
3248 tok->type = TK_RAW_BYTE;
3258 if (IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_U_HEX4)) {
3259 num = scan_unsigned_hexadecimal_number(&p, end, 4, 4, enc);
3260 if (num < -1)
return ONIGERR_TOO_SHORT_DIGITS;
3261 else if (num < 0)
return ONIGERR_TOO_BIG_NUMBER;
3265 tok->type = TK_CODE_POINT;
3267 tok->u.code = (OnigCodePoint )num;
3275 if (PPEEK_IS(
'{') && IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_O_BRACE_OCTAL)) {
3277 num = scan_unsigned_octal_number(&p, end, 11, enc);
3278 if (num < 0)
return ONIGERR_TOO_BIG_WIDE_CHAR_VALUE;
3281 if (ONIGENC_IS_CODE_DIGIT(enc, c2) && c2 <
'8')
3282 return ONIGERR_TOO_LONG_WIDE_CHAR_VALUE;
3285 if (p > prev + enclen(enc, prev, end) && !PEND && (PPEEK_IS(
'}'))) {
3287 tok->type = TK_CODE_POINT;
3289 tok->u.code = (OnigCodePoint )num;
3299 case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
3300 if (IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_OCTAL3)) {
3303 num = scan_unsigned_octal_number(&p, end, 3, enc);
3304 if (num < 0 || 0xff < num)
return ONIGERR_TOO_BIG_NUMBER;
3308 tok->type = TK_RAW_BYTE;
3316 num = fetch_escaped_value(&p, end, env, &c2);
3317 if (num < 0)
return num;
3318 if ((OnigCodePoint )tok->u.c != c2) {
3319 tok->u.code = (OnigCodePoint )c2;
3320 tok->type = TK_CODE_POINT;
3325 else if (c ==
'[') {
3326 if (IS_SYNTAX_OP(syn, ONIG_SYN_OP_POSIX_BRACKET) && (PPEEK_IS(
':'))) {
3327 OnigCodePoint send[] = { (OnigCodePoint )
':', (OnigCodePoint )
']' };
3330 if (str_exist_check_with_esc(send, 2, p, end,
3331 (OnigCodePoint )
']', enc, syn)) {
3332 tok->type = TK_POSIX_BRACKET_OPEN;
3341 if (IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_CCLASS_SET_OP)) {
3342 tok->type = TK_CC_CC_OPEN;
3345 CC_ESC_WARN(env, (UChar* )
"[");
3349 else if (c ==
'&') {
3350 if (IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_CCLASS_SET_OP) &&
3351 !PEND && (PPEEK_IS(
'&'))) {
3353 tok->type = TK_CC_AND;
3362#ifdef USE_NAMED_GROUP
3364fetch_named_backref_token(OnigCodePoint c,
OnigToken* tok, UChar** src,
3377# ifdef USE_BACKREF_WITH_LEVEL
3378 name_end = NULL_UCHARP;
3379 r = fetch_name_with_level(c, &p, end, &name_end,
3380 env, &back_num, &tok->u.backref.level);
3381 if (r == 1) tok->u.backref.exist_level = 1;
3382 else tok->u.backref.exist_level = 0;
3384 r = fetch_name(&p, end, &name_end, env, &back_num, 1);
3386 if (r < 0)
return r;
3388 if (back_num != 0) {
3390 back_num = BACKREF_REL_TO_ABS(back_num, env);
3392 return ONIGERR_INVALID_BACKREF;
3395 if (IS_SYNTAX_BV(syn, ONIG_SYN_STRICT_CHECK_BACKREF)) {
3396 if (back_num > env->num_mem ||
3397 IS_NULL(SCANENV_MEM_NODES(env)[back_num]))
3398 return ONIGERR_INVALID_BACKREF;
3400 tok->type = TK_BACKREF;
3401 tok->u.backref.by_name = 0;
3402 tok->u.backref.num = 1;
3403 tok->u.backref.ref1 = back_num;
3406 num = onig_name_to_group_numbers(env->reg, prev, name_end, &backs);
3408 onig_scan_env_set_error_string(env,
3409 ONIGERR_UNDEFINED_NAME_REFERENCE, prev, name_end);
3410 return ONIGERR_UNDEFINED_NAME_REFERENCE;
3412 if (IS_SYNTAX_BV(syn, ONIG_SYN_STRICT_CHECK_BACKREF)) {
3414 for (i = 0; i < num; i++) {
3415 if (backs[i] > env->num_mem ||
3416 IS_NULL(SCANENV_MEM_NODES(env)[backs[i]]))
3417 return ONIGERR_INVALID_BACKREF;
3421 tok->type = TK_BACKREF;
3422 tok->u.backref.by_name = 1;
3423 if (num == 1 || IS_SYNTAX_BV(syn, ONIG_SYN_USE_LEFT_MOST_NAMED_GROUP)) {
3424 tok->u.backref.num = 1;
3425 tok->u.backref.ref1 = backs[0];
3428 tok->u.backref.num = num;
3429 tok->u.backref.refs = backs;
3454 tok->type = TK_STRING;
3459 if (IS_MC_ESC_CODE(c, syn)) {
3460 if (PEND)
return ONIGERR_END_PATTERN_AT_ESCAPE;
3469 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_ASTERISK_ZERO_INF))
break;
3470 tok->type = TK_OP_REPEAT;
3471 tok->u.repeat.lower = 0;
3472 tok->u.repeat.upper = REPEAT_INFINITE;
3477 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_PLUS_ONE_INF))
break;
3478 tok->type = TK_OP_REPEAT;
3479 tok->u.repeat.lower = 1;
3480 tok->u.repeat.upper = REPEAT_INFINITE;
3485 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_QMARK_ZERO_ONE))
break;
3486 tok->type = TK_OP_REPEAT;
3487 tok->u.repeat.lower = 0;
3488 tok->u.repeat.upper = 1;
3490 if (!PEND && PPEEK_IS(
'?') &&
3491 IS_SYNTAX_OP(syn, ONIG_SYN_OP_QMARK_NON_GREEDY)) {
3493 tok->u.repeat.greedy = 0;
3494 tok->u.repeat.possessive = 0;
3498 if (!PEND && PPEEK_IS(
'+') &&
3499 ((IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_PLUS_POSSESSIVE_REPEAT) &&
3500 tok->type != TK_INTERVAL) ||
3501 (IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_PLUS_POSSESSIVE_INTERVAL) &&
3502 tok->type == TK_INTERVAL))) {
3504 tok->u.repeat.greedy = 1;
3505 tok->u.repeat.possessive = 1;
3508 tok->u.repeat.greedy = 1;
3509 tok->u.repeat.possessive = 0;
3515 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_BRACE_INTERVAL))
break;
3516 r = fetch_range_quantifier(&p, end, tok, env);
3517 if (r < 0)
return r;
3518 if (r == 0)
goto greedy_check;
3520 if (IS_SYNTAX_BV(syn, ONIG_SYN_FIXED_INTERVAL_IS_GREEDY_ONLY))
3521 goto possessive_check;
3529 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_VBAR_ALT))
break;
3534 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_LPAREN_SUBEXP))
break;
3535 tok->type = TK_SUBEXP_OPEN;
3539 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_LPAREN_SUBEXP))
break;
3540 tok->type = TK_SUBEXP_CLOSE;
3544 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_W_WORD))
break;
3545 tok->type = TK_CHAR_TYPE;
3546 tok->u.prop.ctype = ONIGENC_CTYPE_WORD;
3547 tok->u.prop.not = 0;
3551 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_W_WORD))
break;
3552 tok->type = TK_CHAR_TYPE;
3553 tok->u.prop.ctype = ONIGENC_CTYPE_WORD;
3554 tok->u.prop.not = 1;
3558 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_B_WORD_BOUND))
break;
3559 tok->type = TK_ANCHOR;
3560 tok->u.anchor.subtype = ANCHOR_WORD_BOUND;
3561 tok->u.anchor.ascii_range = IS_ASCII_RANGE(env->option)
3562 && ! IS_WORD_BOUND_ALL_RANGE(env->option);
3566 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_B_WORD_BOUND))
break;
3567 tok->type = TK_ANCHOR;
3568 tok->u.anchor.subtype = ANCHOR_NOT_WORD_BOUND;
3569 tok->u.anchor.ascii_range = IS_ASCII_RANGE(env->option)
3570 && ! IS_WORD_BOUND_ALL_RANGE(env->option);
3573#ifdef USE_WORD_BEGIN_END
3575 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_LTGT_WORD_BEGIN_END))
break;
3576 tok->type = TK_ANCHOR;
3577 tok->u.anchor.subtype = ANCHOR_WORD_BEGIN;
3578 tok->u.anchor.ascii_range = IS_ASCII_RANGE(env->option);
3582 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_LTGT_WORD_BEGIN_END))
break;
3583 tok->type = TK_ANCHOR;
3584 tok->u.anchor.subtype = ANCHOR_WORD_END;
3585 tok->u.anchor.ascii_range = IS_ASCII_RANGE(env->option);
3590 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_S_WHITE_SPACE))
break;
3591 tok->type = TK_CHAR_TYPE;
3592 tok->u.prop.ctype = ONIGENC_CTYPE_SPACE;
3593 tok->u.prop.not = 0;
3597 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_S_WHITE_SPACE))
break;
3598 tok->type = TK_CHAR_TYPE;
3599 tok->u.prop.ctype = ONIGENC_CTYPE_SPACE;
3600 tok->u.prop.not = 1;
3604 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_D_DIGIT))
break;
3605 tok->type = TK_CHAR_TYPE;
3606 tok->u.prop.ctype = ONIGENC_CTYPE_DIGIT;
3607 tok->u.prop.not = 0;
3611 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_D_DIGIT))
break;
3612 tok->type = TK_CHAR_TYPE;
3613 tok->u.prop.ctype = ONIGENC_CTYPE_DIGIT;
3614 tok->u.prop.not = 1;
3618 if (! IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_H_XDIGIT))
break;
3619 tok->type = TK_CHAR_TYPE;
3620 tok->u.prop.ctype = ONIGENC_CTYPE_XDIGIT;
3621 tok->u.prop.not = 0;
3625 if (! IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_H_XDIGIT))
break;
3626 tok->type = TK_CHAR_TYPE;
3627 tok->u.prop.ctype = ONIGENC_CTYPE_XDIGIT;
3628 tok->u.prop.not = 1;
3632 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_AZ_BUF_ANCHOR))
break;
3634 tok->type = TK_ANCHOR;
3635 tok->u.anchor.subtype = ANCHOR_BEGIN_BUF;
3639 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_AZ_BUF_ANCHOR))
break;
3640 tok->type = TK_ANCHOR;
3641 tok->u.anchor.subtype = ANCHOR_SEMI_END_BUF;
3645 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_AZ_BUF_ANCHOR))
break;
3647 tok->type = TK_ANCHOR;
3648 tok->u.anchor.subtype = ANCHOR_END_BUF;
3652 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_CAPITAL_G_BEGIN_ANCHOR))
break;
3653 tok->type = TK_ANCHOR;
3654 tok->u.anchor.subtype = ANCHOR_BEGIN_POSITION;
3658 if (! IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_GNU_BUF_ANCHOR))
break;
3663 if (! IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_GNU_BUF_ANCHOR))
break;
3671 if (PPEEK_IS(
'{') && IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_X_BRACE_HEX8)) {
3673 num = scan_unsigned_hexadecimal_number(&p, end, 0, 8, enc);
3674 if (num < 0)
return ONIGERR_TOO_BIG_WIDE_CHAR_VALUE;
3676 if (ONIGENC_IS_CODE_XDIGIT(enc, PPEEK))
3677 return ONIGERR_TOO_LONG_WIDE_CHAR_VALUE;
3680 if ((p > prev + enclen(enc, prev, end)) && !PEND && PPEEK_IS(
'}')) {
3682 tok->type = TK_CODE_POINT;
3683 tok->u.code = (OnigCodePoint )num;
3690 else if (IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_X_HEX2)) {
3691 num = scan_unsigned_hexadecimal_number(&p, end, 0, 2, enc);
3692 if (num < 0)
return ONIGERR_TOO_BIG_NUMBER;
3696 tok->type = TK_RAW_BYTE;
3706 if (IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_U_HEX4)) {
3707 num = scan_unsigned_hexadecimal_number(&p, end, 4, 4, enc);
3708 if (num < -1)
return ONIGERR_TOO_SHORT_DIGITS;
3709 else if (num < 0)
return ONIGERR_TOO_BIG_NUMBER;
3713 tok->type = TK_CODE_POINT;
3715 tok->u.code = (OnigCodePoint )num;
3723 if (PPEEK_IS(
'{') && IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_O_BRACE_OCTAL)) {
3725 num = scan_unsigned_octal_number(&p, end, 11, enc);
3726 if (num < 0)
return ONIGERR_TOO_BIG_WIDE_CHAR_VALUE;
3728 OnigCodePoint c = PPEEK;
3729 if (ONIGENC_IS_CODE_DIGIT(enc, c) && c <
'8')
3730 return ONIGERR_TOO_LONG_WIDE_CHAR_VALUE;
3733 if ((p > prev + enclen(enc, prev, end)) && !PEND && PPEEK_IS(
'}')) {
3735 tok->type = TK_CODE_POINT;
3736 tok->u.code = (OnigCodePoint )num;
3745 case '1':
case '2':
case '3':
case '4':
3746 case '5':
case '6':
case '7':
case '8':
case '9':
3749 num = onig_scan_unsigned_number(&p, end, enc);
3750 if (num < 0 || num > ONIG_MAX_BACKREF_NUM) {
3754 if (IS_SYNTAX_OP(syn, ONIG_SYN_OP_DECIMAL_BACKREF) &&
3755 (num <= env->num_mem || num <= 9)) {
3756 if (IS_SYNTAX_BV(syn, ONIG_SYN_STRICT_CHECK_BACKREF)) {
3757 if (num > env->num_mem || IS_NULL(SCANENV_MEM_NODES(env)[num]))
3758 return ONIGERR_INVALID_BACKREF;
3761 tok->type = TK_BACKREF;
3762 tok->u.backref.num = 1;
3763 tok->u.backref.ref1 = num;
3764 tok->u.backref.by_name = 0;
3765#ifdef USE_BACKREF_WITH_LEVEL
3766 tok->u.backref.exist_level = 0;
3772 if (c ==
'8' || c ==
'9') {
3781 if (IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_OCTAL3)) {
3783 num = scan_unsigned_octal_number(&p, end, (c ==
'0' ? 2:3), enc);
3784 if (num < 0 || 0xff < num)
return ONIGERR_TOO_BIG_NUMBER;
3788 tok->type = TK_RAW_BYTE;
3792 else if (c !=
'0') {
3797#ifdef USE_NAMED_GROUP
3799 if (!PEND && IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_K_NAMED_BACKREF)) {
3801 if (c ==
'<' || c ==
'\'') {
3802 r = fetch_named_backref_token(c, tok, &p, end, env);
3803 if (r < 0)
return r;
3807 onig_syntax_warn(env,
"invalid back reference");
3813#if defined(USE_SUBEXP_CALL) || defined(USE_NAMED_GROUP)
3815# ifdef USE_NAMED_GROUP
3816 if (!PEND && IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_G_BRACE_BACKREF)) {
3819 r = fetch_named_backref_token(c, tok, &p, end, env);
3820 if (r < 0)
return r;
3826# ifdef USE_SUBEXP_CALL
3827 if (!PEND && IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_G_SUBEXP_CALL)) {
3829 if (c ==
'<' || c ==
'\'') {
3830 int gnum = -1, rel = 0;
3832 OnigCodePoint cnext;
3837 if (PPEEK_IS(get_name_end_code_point(c))) {
3843 else if (cnext ==
'+') {
3849 r = fetch_name((OnigCodePoint )c, &p, end, &name_end, env, &gnum, 1);
3850 if (r < 0)
return r;
3853 tok->type = TK_CALL;
3854 tok->u.call.name = prev;
3855 tok->u.call.name_end = name_end;
3856 tok->u.call.gnum = gnum;
3857 tok->u.call.rel = rel;
3860 onig_syntax_warn(env,
"invalid subexp call");
3869 if (IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_CAPITAL_Q_QUOTE)) {
3870 tok->type = TK_QUOTE_OPEN;
3876 if (PPEEK_IS(
'{') &&
3877 IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_P_BRACE_CHAR_PROPERTY)) {
3879 tok->type = TK_CHAR_PROPERTY;
3880 tok->u.prop.not = (c ==
'P' ? 1 : 0);
3882 if (!PEND && IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_P_BRACE_CIRCUMFLEX_NOT)) {
3885 tok->u.prop.not = (tok->u.prop.not == 0 ? 1 : 0);
3892 onig_syntax_warn(env,
"invalid Unicode Property \\%c", c);
3897 if (IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_CAPITAL_R_LINEBREAK)) {
3898 tok->type = TK_LINEBREAK;
3903 if (IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_CAPITAL_X_EXTENDED_GRAPHEME_CLUSTER)) {
3904 tok->type = TK_EXTENDED_GRAPHEME_CLUSTER;
3909 if (IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_CAPITAL_K_KEEP)) {
3910 tok->type = TK_KEEP;
3919 num = fetch_escaped_value(&p, end, env, &c2);
3920 if (num < 0)
return num;
3922 if ((OnigCodePoint )tok->u.c != c2) {
3923 tok->type = TK_CODE_POINT;
3924 tok->u.code = (OnigCodePoint )c2;
3927 p = tok->backp + enclen(enc, tok->backp, end);
3937#ifdef USE_VARIABLE_META_CHARS
3938 if ((c != ONIG_INEFFECTIVE_META_CHAR) &&
3939 IS_SYNTAX_OP(syn, ONIG_SYN_OP_VARIABLE_META_CHARACTERS)) {
3940 if (c == MC_ANYCHAR(syn))
3942 else if (c == MC_ANYTIME(syn))
3944 else if (c == MC_ZERO_OR_ONE_TIME(syn))
3945 goto zero_or_one_time;
3946 else if (c == MC_ONE_OR_MORE_TIME(syn))
3947 goto one_or_more_time;
3948 else if (c == MC_ANYCHAR_ANYTIME(syn)) {
3949 tok->type = TK_ANYCHAR_ANYTIME;
3957 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_DOT_ANYCHAR))
break;
3958#ifdef USE_VARIABLE_META_CHARS
3961 tok->type = TK_ANYCHAR;
3965 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_ASTERISK_ZERO_INF))
break;
3966#ifdef USE_VARIABLE_META_CHARS
3969 tok->type = TK_OP_REPEAT;
3970 tok->u.repeat.lower = 0;
3971 tok->u.repeat.upper = REPEAT_INFINITE;
3976 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_PLUS_ONE_INF))
break;
3977#ifdef USE_VARIABLE_META_CHARS
3980 tok->type = TK_OP_REPEAT;
3981 tok->u.repeat.lower = 1;
3982 tok->u.repeat.upper = REPEAT_INFINITE;
3987 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_QMARK_ZERO_ONE))
break;
3988#ifdef USE_VARIABLE_META_CHARS
3991 tok->type = TK_OP_REPEAT;
3992 tok->u.repeat.lower = 0;
3993 tok->u.repeat.upper = 1;
3998 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_BRACE_INTERVAL))
break;
3999 r = fetch_range_quantifier(&p, end, tok, env);
4000 if (r < 0)
return r;
4001 if (r == 0)
goto greedy_check;
4003 if (IS_SYNTAX_BV(syn, ONIG_SYN_FIXED_INTERVAL_IS_GREEDY_ONLY))
4004 goto possessive_check;
4012 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_VBAR_ALT))
break;
4017 if (PPEEK_IS(
'?') &&
4018 IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_QMARK_GROUP_EFFECT)) {
4020 if (PPEEK_IS(
'#')) {
4023 if (PEND)
return ONIGERR_END_PATTERN_IN_GROUP;
4025 if (c == MC_ESC(syn)) {
4026 if (!PEND) PFETCH(c);
4029 if (c ==
')')
break;
4034#ifdef USE_PERL_SUBEXP_CALL
4037 if ((c ==
'&' || c ==
'R' || ONIGENC_IS_CODE_DIGIT(enc, c)) &&
4038 IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_QMARK_SUBEXP_CALL)) {
4044 if (c ==
'R' || c ==
'0') {
4046 if (!PPEEK_IS(
')'))
return ONIGERR_INVALID_GROUP_NAME;
4048 name_end = name = p;
4058 r = fetch_name((OnigCodePoint )
'(', &p, end, &name_end, env, &gnum, numref);
4059 if (r < 0)
return r;
4062 tok->type = TK_CALL;
4063 tok->u.call.name = name;
4064 tok->u.call.name_end = name_end;
4065 tok->u.call.gnum = gnum;
4066 tok->u.call.rel = 0;
4069 else if ((c ==
'-' || c ==
'+') &&
4070 IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_QMARK_SUBEXP_CALL)) {
4075 OnigCodePoint cnext;
4080 if (ONIGENC_IS_CODE_DIGIT(enc, cnext)) {
4081 if (c ==
'-') PUNFETCH;
4083 r = fetch_name((OnigCodePoint )
'(', &p, end, &name_end, env, &gnum, 1);
4084 if (r < 0)
return r;
4086 tok->type = TK_CALL;
4087 tok->u.call.name = name;
4088 tok->u.call.name_end = name_end;
4089 tok->u.call.gnum = gnum;
4090 tok->u.call.rel = 1;
4095#ifdef USE_CAPITAL_P_NAMED_GROUP
4096 if (PPEEK_IS(
'P') &&
4097 IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_QMARK_CAPITAL_P_NAMED_GROUP)) {
4104 if (PEND)
return ONIGERR_UNDEFINED_GROUP_OPTION;
4107 r = fetch_named_backref_token((OnigCodePoint )
'(', tok, &p, end, env);
4108 if (r < 0)
return r;
4111 else if (c ==
'>') {
4113 r = fetch_name((OnigCodePoint )
'(', &p, end, &name_end, env, &gnum, 0);
4114 if (r < 0)
return r;
4116 tok->type = TK_CALL;
4117 tok->u.call.name = name;
4118 tok->u.call.name_end = name_end;
4119 tok->u.call.gnum = gnum;
4120 tok->u.call.rel = 0;
4128 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_LPAREN_SUBEXP))
break;
4129 tok->type = TK_SUBEXP_OPEN;
4133 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_LPAREN_SUBEXP))
break;
4134 tok->type = TK_SUBEXP_CLOSE;
4138 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_LINE_ANCHOR))
break;
4139 tok->type = TK_ANCHOR;
4140 tok->u.anchor.subtype = (IS_SINGLELINE(env->option)
4141 ? ANCHOR_BEGIN_BUF : ANCHOR_BEGIN_LINE);
4145 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_LINE_ANCHOR))
break;
4146 tok->type = TK_ANCHOR;
4147 tok->u.anchor.subtype = (IS_SINGLELINE(env->option)
4148 ? ANCHOR_SEMI_END_BUF : ANCHOR_END_LINE);
4152 if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_BRACKET_CC))
break;
4153 tok->type = TK_CC_OPEN;
4157 if (*src > env->pattern)
4158 CLOSE_BRACKET_WITHOUT_ESC_WARN(env, (UChar* )
"]");
4162 if (IS_EXTEND(env->option)) {
4165 if (ONIGENC_IS_CODE_NEWLINE(enc, c))
4173 case ' ':
case '\t':
case '\n':
case '\r':
case '\f':
4174 if (IS_EXTEND(env->option))
4184#ifdef USE_VARIABLE_META_CHARS
4192add_ctype_to_cc_by_range(
CClassNode* cc,
int ctype ARG_UNUSED,
int not,
4194 OnigCodePoint sb_out,
const OnigCodePoint mbr[])
4199 int n = ONIGENC_CODE_RANGE_NUM(mbr);
4202 for (i = 0; i < n; i++) {
4203 for (j = ONIGENC_CODE_RANGE_FROM(mbr, i);
4204 j <= ONIGENC_CODE_RANGE_TO(mbr, i); j++) {
4206 if (j > ONIGENC_CODE_RANGE_FROM(mbr, i)) {
4207 r = add_code_range_to_buf(&(cc->mbuf), env, j,
4208 ONIGENC_CODE_RANGE_TO(mbr, i));
4209 if (r != 0)
return r;
4215 BITSET_SET_BIT_CHKDUP(cc->bs, j);
4220 for ( ; i < n; i++) {
4221 r = add_code_range_to_buf(&(cc->mbuf), env,
4222 ONIGENC_CODE_RANGE_FROM(mbr, i),
4223 ONIGENC_CODE_RANGE_TO(mbr, i));
4224 if (r != 0)
return r;
4228 OnigCodePoint prev = 0;
4230 for (i = 0; i < n; i++) {
4232 j < ONIGENC_CODE_RANGE_FROM(mbr, i); j++) {
4236 BITSET_SET_BIT_CHKDUP(cc->bs, j);
4238 prev = ONIGENC_CODE_RANGE_TO(mbr, i) + 1;
4240 for (j = prev; j < sb_out; j++) {
4241 BITSET_SET_BIT_CHKDUP(cc->bs, j);
4247 for (i = 0; i < n; i++) {
4248 if (prev < ONIGENC_CODE_RANGE_FROM(mbr, i)) {
4249 r = add_code_range_to_buf(&(cc->mbuf), env, prev,
4250 ONIGENC_CODE_RANGE_FROM(mbr, i) - 1);
4251 if (r != 0)
return r;
4253 prev = ONIGENC_CODE_RANGE_TO(mbr, i) + 1;
4255 if (prev < 0x7fffffff) {
4256 r = add_code_range_to_buf(&(cc->mbuf), env, prev, 0x7fffffff);
4257 if (r != 0)
return r;
4265add_ctype_to_cc(
CClassNode* cc,
int ctype,
int not,
int ascii_range,
ScanEnv* env)
4269 const OnigCodePoint *ranges;
4270 OnigCodePoint sb_out;
4273 r = ONIGENC_GET_CTYPE_CODE_RANGE(enc, ctype, &sb_out, &ranges);
4277 initialize_cclass(&ccwork);
4278 r = add_ctype_to_cc_by_range(&ccwork, ctype, not, env, sb_out,
4282 r = add_code_range_to_buf0(&(ccwork.mbuf), env, 0x80, ONIG_LAST_CODE_POINT, FALSE);
4286 initialize_cclass(&ccascii);
4287 if (ONIGENC_MBC_MINLEN(env->enc) > 1) {
4288 r = add_code_range(&(ccascii.mbuf), env, 0x00, 0x7F);
4291 bitset_set_range(env, ccascii.bs, 0x00, 0x7F);
4295 r = and_cclass(&ccwork, &ccascii, env);
4297 if (IS_NOT_NULL(ccascii.mbuf)) bbuf_free(ccascii.mbuf);
4300 r = or_cclass(cc, &ccwork, env);
4302 if (IS_NOT_NULL(ccwork.mbuf)) bbuf_free(ccwork.mbuf);
4306 r = add_ctype_to_cc_by_range(cc, ctype, not, env, sb_out, ranges);
4310 else if (r != ONIG_NO_SUPPORT_CONFIG) {
4314 maxcode = ascii_range ? 0x80 : SINGLE_BYTE_SIZE;
4317 case ONIGENC_CTYPE_ALPHA:
4318 case ONIGENC_CTYPE_BLANK:
4319 case ONIGENC_CTYPE_CNTRL:
4320 case ONIGENC_CTYPE_DIGIT:
4321 case ONIGENC_CTYPE_LOWER:
4322 case ONIGENC_CTYPE_PUNCT:
4323 case ONIGENC_CTYPE_SPACE:
4324 case ONIGENC_CTYPE_UPPER:
4325 case ONIGENC_CTYPE_XDIGIT:
4326 case ONIGENC_CTYPE_ASCII:
4327 case ONIGENC_CTYPE_ALNUM:
4329 for (c = 0; c < SINGLE_BYTE_SIZE; c++) {
4330 if (! ONIGENC_IS_CODE_CTYPE(enc, (OnigCodePoint )c, ctype))
4331 BITSET_SET_BIT_CHKDUP(cc->bs, c);
4333 ADD_ALL_MULTI_BYTE_RANGE(enc, cc->mbuf);
4336 for (c = 0; c < SINGLE_BYTE_SIZE; c++) {
4337 if (ONIGENC_IS_CODE_CTYPE(enc, (OnigCodePoint )c, ctype))
4338 BITSET_SET_BIT_CHKDUP(cc->bs, c);
4343 case ONIGENC_CTYPE_GRAPH:
4344 case ONIGENC_CTYPE_PRINT:
4346 for (c = 0; c < SINGLE_BYTE_SIZE; c++) {
4347 if (! ONIGENC_IS_CODE_CTYPE(enc, (OnigCodePoint )c, ctype)
4349 BITSET_SET_BIT_CHKDUP(cc->bs, c);
4352 ADD_ALL_MULTI_BYTE_RANGE(enc, cc->mbuf);
4355 for (c = 0; c < maxcode; c++) {
4356 if (ONIGENC_IS_CODE_CTYPE(enc, (OnigCodePoint )c, ctype))
4357 BITSET_SET_BIT_CHKDUP(cc->bs, c);
4360 ADD_ALL_MULTI_BYTE_RANGE(enc, cc->mbuf);
4364 case ONIGENC_CTYPE_WORD:
4366 for (c = 0; c < maxcode; c++) {
4367 if (ONIGENC_IS_CODE_WORD(enc, c)) BITSET_SET_BIT_CHKDUP(cc->bs, c);
4370 ADD_ALL_MULTI_BYTE_RANGE(enc, cc->mbuf);
4373 for (c = 0; c < SINGLE_BYTE_SIZE; c++) {
4374 if ((ONIGENC_CODE_TO_MBCLEN(enc, c) > 0)
4375 && (! ONIGENC_IS_CODE_WORD(enc, c) || c >= maxcode))
4376 BITSET_SET_BIT_CHKDUP(cc->bs, c);
4379 ADD_ALL_MULTI_BYTE_RANGE(enc, cc->mbuf);
4384 return ONIGERR_PARSER_BUG;
4393 UChar** src, UChar* end,
ScanEnv* env)
4395#define POSIX_BRACKET_CHECK_LIMIT_LENGTH 20
4396#define POSIX_BRACKET_NAME_MIN_LEN 4
4399 POSIX_BRACKET_ENTRY_INIT(
"alnum", ONIGENC_CTYPE_ALNUM),
4400 POSIX_BRACKET_ENTRY_INIT(
"alpha", ONIGENC_CTYPE_ALPHA),
4401 POSIX_BRACKET_ENTRY_INIT(
"blank", ONIGENC_CTYPE_BLANK),
4402 POSIX_BRACKET_ENTRY_INIT(
"cntrl", ONIGENC_CTYPE_CNTRL),
4403 POSIX_BRACKET_ENTRY_INIT(
"digit", ONIGENC_CTYPE_DIGIT),
4404 POSIX_BRACKET_ENTRY_INIT(
"graph", ONIGENC_CTYPE_GRAPH),
4405 POSIX_BRACKET_ENTRY_INIT(
"lower", ONIGENC_CTYPE_LOWER),
4406 POSIX_BRACKET_ENTRY_INIT(
"print", ONIGENC_CTYPE_PRINT),
4407 POSIX_BRACKET_ENTRY_INIT(
"punct", ONIGENC_CTYPE_PUNCT),
4408 POSIX_BRACKET_ENTRY_INIT(
"space", ONIGENC_CTYPE_SPACE),
4409 POSIX_BRACKET_ENTRY_INIT(
"upper", ONIGENC_CTYPE_UPPER),
4410 POSIX_BRACKET_ENTRY_INIT(
"xdigit", ONIGENC_CTYPE_XDIGIT),
4411 POSIX_BRACKET_ENTRY_INIT(
"ascii", ONIGENC_CTYPE_ASCII),
4412 POSIX_BRACKET_ENTRY_INIT(
"word", ONIGENC_CTYPE_WORD),
4422 if (PPEEK_IS(
'^')) {
4429 if (onigenc_strlen(enc, p, end) < POSIX_BRACKET_NAME_MIN_LEN + 3)
4430 goto not_posix_bracket;
4432 ascii_range = IS_ASCII_RANGE(env->option) &&
4433 ! IS_POSIX_BRACKET_ALL_RANGE(env->option);
4434 for (pb = PBS; pb < PBS + numberof(PBS); pb++) {
4435 if (onigenc_with_ascii_strncmp(enc, p, end, pb->name, pb->len) == 0) {
4436 p = (UChar* )onigenc_step(enc, p, end, pb->len);
4437 if (onigenc_with_ascii_strncmp(enc, p, end, (UChar* )
":]", 2) != 0)
4438 return ONIGERR_INVALID_POSIX_BRACKET_TYPE;
4440 r = add_ctype_to_cc(cc, pb->ctype, not, ascii_range, env);
4441 if (r != 0)
return r;
4443 if (IS_NOT_NULL(asc_cc)) {
4444 if (pb->ctype != ONIGENC_CTYPE_WORD &&
4445 pb->ctype != ONIGENC_CTYPE_ASCII &&
4447 r = add_ctype_to_cc(asc_cc, pb->ctype, not, ascii_range, env);
4448 if (r != 0)
return r;
4460 while (!PEND && ((c = PPEEK) !=
':') && c !=
']') {
4462 if (++i > POSIX_BRACKET_CHECK_LIMIT_LENGTH)
break;
4464 if (c ==
':' && ! PEND) {
4469 return ONIGERR_INVALID_POSIX_BRACKET_TYPE;
4477fetch_char_property_to_ctype(UChar** src, UChar* end,
ScanEnv* env)
4482 UChar *prev, *start, *p = *src;
4484 r = ONIGERR_INVALID_CHAR_PROPERTY_NAME;
4491 r = ONIGENC_PROPERTY_NAME_TO_CTYPE(enc, start, prev);
4497 else if (c ==
'(' || c ==
')' || c ==
'{' || c ==
'|') {
4502 onig_scan_env_set_error_string(env, r, *src, prev);
4509parse_char_property(
Node** np,
OnigToken* tok, UChar** src, UChar* end,
4515 ctype = fetch_char_property_to_ctype(src, end, env);
4516 if (ctype < 0)
return ctype;
4518 *np = node_new_cclass();
4519 CHECK_NULL_RETURN_MEMERR(*np);
4521 r = add_ctype_to_cc(cc, ctype, 0, 0, env);
4522 if (r != 0)
return r;
4523 if (tok->u.prop.not != 0) NCCLASS_SET_NOT(cc);
4525 if (IS_IGNORECASE(env->option)) {
4526 if (ctype != ONIGENC_CTYPE_ASCII)
4527 r = cclass_case_fold(np, cc, cc, env);
4548 OnigCodePoint* vs,
enum CCVALTYPE*
type,
4549 enum CCSTATE* state,
ScanEnv* env)
4553 if (*state == CCS_RANGE)
4554 return ONIGERR_CHAR_CLASS_VALUE_AT_END_OF_RANGE;
4556 if (*state == CCS_VALUE && *
type != CCV_CLASS) {
4557 if (*
type == CCV_SB) {
4558 BITSET_SET_BIT_CHKDUP(cc->bs, (
int )(*vs));
4559 if (IS_NOT_NULL(asc_cc))
4560 BITSET_SET_BIT(asc_cc->bs, (
int )(*vs));
4562 else if (*
type == CCV_CODE_POINT) {
4563 r = add_code_range(&(cc->mbuf), env, *vs, *vs);
4564 if (r < 0)
return r;
4565 if (IS_NOT_NULL(asc_cc)) {
4566 r = add_code_range0(&(asc_cc->mbuf), env, *vs, *vs, 0);
4567 if (r < 0)
return r;
4579 OnigCodePoint *from, OnigCodePoint to,
4580 int* from_israw,
int to_israw,
4581 enum CCVALTYPE intype,
enum CCVALTYPE*
type,
4582 enum CCSTATE* state,
ScanEnv* env)
4588 if (*
type == CCV_SB) {
4589 BITSET_SET_BIT_CHKDUP(cc->bs, (
int )(*from));
4590 if (IS_NOT_NULL(asc_cc))
4591 BITSET_SET_BIT(asc_cc->bs, (
int )(*from));
4593 else if (*
type == CCV_CODE_POINT) {
4594 r = add_code_range(&(cc->mbuf), env, *from, *from);
4595 if (r < 0)
return r;
4596 if (IS_NOT_NULL(asc_cc)) {
4597 r = add_code_range0(&(asc_cc->mbuf), env, *from, *from, 0);
4598 if (r < 0)
return r;
4604 if (intype == *
type) {
4605 if (intype == CCV_SB) {
4606 if (*from > 0xff || to > 0xff)
4607 return ONIGERR_INVALID_CODE_POINT_VALUE;
4610 if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_ALLOW_EMPTY_RANGE_IN_CC))
4613 return ONIGERR_EMPTY_RANGE_IN_CHAR_CLASS;
4615 bitset_set_range(env, cc->bs, (
int )*from, (
int )to);
4616 if (IS_NOT_NULL(asc_cc))
4617 bitset_set_range(env, asc_cc->bs, (
int )*from, (
int )to);
4620 r = add_code_range(&(cc->mbuf), env, *from, to);
4621 if (r < 0)
return r;
4622 if (IS_NOT_NULL(asc_cc)) {
4623 r = add_code_range0(&(asc_cc->mbuf), env, *from, to, 0);
4624 if (r < 0)
return r;
4630 if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_ALLOW_EMPTY_RANGE_IN_CC))
4633 return ONIGERR_EMPTY_RANGE_IN_CHAR_CLASS;
4635 bitset_set_range(env, cc->bs, (
int )*from, (
int )(to < 0xff ? to : 0xff));
4636 r = add_code_range(&(cc->mbuf), env, (OnigCodePoint )*from, to);
4637 if (r < 0)
return r;
4638 if (IS_NOT_NULL(asc_cc)) {
4639 bitset_set_range(env, asc_cc->bs, (
int )*from, (
int )(to < 0xff ? to : 0xff));
4640 r = add_code_range0(&(asc_cc->mbuf), env, (OnigCodePoint )*from, to, 0);
4641 if (r < 0)
return r;
4645 *state = CCS_COMPLETE;
4657 *from_israw = to_israw;
4664code_exist_check(OnigCodePoint c, UChar* from, UChar* end,
int ignore_escaped,
4674 if (ignore_escaped && in_esc) {
4679 if (code == c)
return 1;
4680 if (code == MC_ESC(env->syntax)) in_esc = 1;
4687parse_char_class(
Node** np,
Node** asc_np,
OnigToken* tok, UChar** src, UChar* end,
4690 int r, neg,
len, fetched, and_start;
4691 OnigCodePoint v, vs;
4700 enum CCVALTYPE val_type, in_type;
4701 int val_israw, in_israw;
4703 *np = *asc_np = NULL_NODE;
4705 if (env->parse_depth > ParseDepthLimit)
4706 return ONIGERR_PARSE_DEPTH_LIMIT_OVER;
4708 r = fetch_token_in_cc(tok, src, end, env);
4709 if (r == TK_CHAR && tok->u.c ==
'^' && tok->escaped == 0) {
4711 r = fetch_token_in_cc(tok, src, end, env);
4717 if (r < 0)
return r;
4718 if (r == TK_CC_CLOSE) {
4719 if (! code_exist_check((OnigCodePoint )
']',
4720 *src, env->pattern_end, 1, env))
4721 return ONIGERR_EMPTY_CHAR_CLASS;
4723 CC_ESC_WARN(env, (UChar* )
"]");
4724 r = tok->type = TK_CHAR;
4727 *np = node = node_new_cclass();
4728 CHECK_NULL_RETURN_MEMERR(node);
4731 if (IS_IGNORECASE(env->option)) {
4732 *asc_np = asc_node = node_new_cclass();
4733 CHECK_NULL_RETURN_MEMERR(asc_node);
4734 asc_cc = NCCLASS(asc_node);
4737 asc_node = NULL_NODE;
4744 while (r != TK_CC_CLOSE) {
4748 if ((tok->u.code >= SINGLE_BYTE_SIZE) ||
4749 (
len = ONIGENC_CODE_TO_MBCLEN(env->enc, tok->u.c)) > 1) {
4750 in_type = CCV_CODE_POINT;
4760 v = (OnigCodePoint )tok->u.c;
4767 if (! ONIGENC_IS_SINGLEBYTE(env->enc) && tok->base != 0) {
4768 UChar buf[ONIGENC_CODE_TO_MBC_MAXLEN];
4769 UChar* bufe = buf + ONIGENC_CODE_TO_MBC_MAXLEN;
4771 int i, base = tok->base;
4773 buf[0] = (UChar )tok->u.c;
4774 for (i = 1; i < ONIGENC_MBC_MAXLEN(env->enc); i++) {
4775 r = fetch_token_in_cc(tok, &p, end, env);
4776 if (r < 0)
goto err;
4777 if (r != TK_RAW_BYTE || tok->base != base) {
4781 buf[i] = (UChar )tok->u.c;
4784 if (i < ONIGENC_MBC_MINLEN(env->enc)) {
4785 r = ONIGERR_TOO_SHORT_MULTI_BYTE_STRING;
4789 len = enclen(env->enc, buf, buf + i);
4791 r = ONIGERR_TOO_SHORT_MULTI_BYTE_STRING;
4796 for (i = 1; i <
len; i++) {
4797 (void)fetch_token_in_cc(tok, &p, end, env);
4804 v = (OnigCodePoint )buf[0];
4808 v = ONIGENC_MBC_TO_CODE(env->enc, buf, bufe);
4809 in_type = CCV_CODE_POINT;
4813 v = (OnigCodePoint )tok->u.c;
4825 len = ONIGENC_CODE_TO_MBCLEN(env->enc, v);
4830 in_type = (
len == 1 ? CCV_SB : CCV_CODE_POINT);
4832 r = next_state_val(cc, asc_cc, &vs, v, &val_israw, in_israw, in_type, &val_type,
4834 if (r != 0)
goto err;
4837 case TK_POSIX_BRACKET_OPEN:
4838 r = parse_posix_bracket(cc, asc_cc, &p, end, env);
4839 if (r < 0)
goto err;
4841 CC_ESC_WARN(env, (UChar* )
"[");
4843 v = (OnigCodePoint )tok->u.c;
4851 r = add_ctype_to_cc(cc, tok->u.prop.ctype, tok->u.prop.not,
4852 IS_ASCII_RANGE(env->option), env);
4853 if (r != 0)
return r;
4854 if (IS_NOT_NULL(asc_cc)) {
4855 if (tok->u.prop.ctype != ONIGENC_CTYPE_WORD)
4856 r = add_ctype_to_cc(asc_cc, tok->u.prop.ctype, tok->u.prop.not,
4857 IS_ASCII_RANGE(env->option), env);
4858 if (r != 0)
return r;
4862 r = next_state_class(cc, asc_cc, &vs, &val_type, &state, env);
4863 if (r != 0)
goto err;
4866 case TK_CHAR_PROPERTY:
4870 ctype = fetch_char_property_to_ctype(&p, end, env);
4871 if (ctype < 0)
return ctype;
4872 r = add_ctype_to_cc(cc, ctype, tok->u.prop.not, 0, env);
4873 if (r != 0)
return r;
4874 if (IS_NOT_NULL(asc_cc)) {
4875 if (ctype != ONIGENC_CTYPE_ASCII)
4876 r = add_ctype_to_cc(asc_cc, ctype, tok->u.prop.not, 0, env);
4877 if (r != 0)
return r;
4884 if (state == CCS_VALUE) {
4885 r = fetch_token_in_cc(tok, &p, end, env);
4886 if (r < 0)
goto err;
4888 if (r == TK_CC_CLOSE) {
4890 v = (OnigCodePoint )
'-';
4894 else if (r == TK_CC_AND) {
4895 CC_ESC_WARN(env, (UChar* )
"-");
4899 if (val_type == CCV_CLASS) {
4900 r = ONIGERR_UNMATCHED_RANGE_SPECIFIER_IN_CHAR_CLASS;
4906 else if (state == CCS_START) {
4908 v = (OnigCodePoint )tok->u.c;
4911 r = fetch_token_in_cc(tok, &p, end, env);
4912 if (r < 0)
goto err;
4915 if (r == TK_CC_RANGE || and_start != 0)
4916 CC_ESC_WARN(env, (UChar* )
"-");
4920 else if (state == CCS_RANGE) {
4921 CC_ESC_WARN(env, (UChar* )
"-");
4925 r = fetch_token_in_cc(tok, &p, end, env);
4926 if (r < 0)
goto err;
4928 if (r == TK_CC_CLOSE)
goto range_end_val;
4929 else if (r == TK_CC_AND) {
4930 CC_ESC_WARN(env, (UChar* )
"-");
4934 if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_ALLOW_DOUBLE_RANGE_OP_IN_CC)) {
4935 CC_ESC_WARN(env, (UChar* )
"-");
4938 r = ONIGERR_UNMATCHED_RANGE_SPECIFIER_IN_CHAR_CLASS;
4945 Node *anode, *aasc_node;
4948 r = parse_char_class(&anode, &aasc_node, tok, &p, end, env);
4950 acc = NCCLASS(anode);
4951 r = or_cclass(cc, acc, env);
4953 if (r == 0 && IS_NOT_NULL(aasc_node)) {
4954 acc = NCCLASS(aasc_node);
4955 r = or_cclass(asc_cc, acc, env);
4957 onig_node_free(anode);
4958 onig_node_free(aasc_node);
4959 if (r != 0)
goto err;
4965 if (state == CCS_VALUE) {
4966 r = next_state_val(cc, asc_cc, &vs, 0, &val_israw, 0, val_type,
4967 &val_type, &state, env);
4968 if (r != 0)
goto err;
4974 if (IS_NOT_NULL(prev_cc)) {
4975 r = and_cclass(prev_cc, cc, env);
4976 if (r != 0)
goto err;
4977 bbuf_free(cc->mbuf);
4978 if (IS_NOT_NULL(asc_cc)) {
4979 r = and_cclass(asc_prev_cc, asc_cc, env);
4980 if (r != 0)
goto err;
4981 bbuf_free(asc_cc->mbuf);
4987 if (IS_NOT_NULL(asc_cc)) {
4988 asc_prev_cc = asc_cc;
4989 asc_cc = &asc_work_cc;
4992 initialize_cclass(cc);
4993 if (IS_NOT_NULL(asc_cc))
4994 initialize_cclass(asc_cc);
4999 r = ONIGERR_PREMATURE_END_OF_CHAR_CLASS;
5003 r = ONIGERR_PARSER_BUG;
5011 r = fetch_token_in_cc(tok, &p, end, env);
5012 if (r < 0)
goto err;
5016 if (state == CCS_VALUE) {
5017 r = next_state_val(cc, asc_cc, &vs, 0, &val_israw, 0, val_type,
5018 &val_type, &state, env);
5019 if (r != 0)
goto err;
5022 if (IS_NOT_NULL(prev_cc)) {
5023 r = and_cclass(prev_cc, cc, env);
5024 if (r != 0)
goto err;
5025 bbuf_free(cc->mbuf);
5027 if (IS_NOT_NULL(asc_cc)) {
5028 r = and_cclass(asc_prev_cc, asc_cc, env);
5029 if (r != 0)
goto err;
5030 bbuf_free(asc_cc->mbuf);
5031 asc_cc = asc_prev_cc;
5036 NCCLASS_SET_NOT(cc);
5037 if (IS_NOT_NULL(asc_cc))
5038 NCCLASS_SET_NOT(asc_cc);
5041 NCCLASS_CLEAR_NOT(cc);
5042 if (IS_NOT_NULL(asc_cc))
5043 NCCLASS_CLEAR_NOT(asc_cc);
5045 if (IS_NCCLASS_NOT(cc) &&
5046 IS_SYNTAX_BV(env->syntax, ONIG_SYN_NOT_NEWLINE_IN_NEGATIVE_CC)) {
5049 is_empty = (IS_NULL(cc->mbuf) ? 1 : 0);
5051 BITSET_IS_EMPTY(cc->bs, is_empty);
5053 if (is_empty == 0) {
5054#define NEWLINE_CODE 0x0a
5056 if (ONIGENC_IS_CODE_NEWLINE(env->enc, NEWLINE_CODE)) {
5057 if (ONIGENC_CODE_TO_MBCLEN(env->enc, NEWLINE_CODE) == 1)
5058 BITSET_SET_BIT_CHKDUP(cc->bs, NEWLINE_CODE);
5060 r = add_code_range(&(cc->mbuf), env, NEWLINE_CODE, NEWLINE_CODE);
5061 if (r < 0)
goto err;
5071 if (cc != NCCLASS(*np))
5072 bbuf_free(cc->mbuf);
5073 if (IS_NOT_NULL(asc_cc) && (asc_cc != NCCLASS(*asc_np)))
5074 bbuf_free(asc_cc->mbuf);
5078static int parse_subexp(
Node** top,
OnigToken* tok,
int term,
5079 UChar** src, UChar* end,
ScanEnv* env);
5082parse_enclose(
Node** np,
OnigToken* tok,
int term, UChar** src, UChar* end,
5086 Node *target, *work1 = NULL, *work2 = NULL;
5087 OnigOptionType option;
5091#ifdef USE_NAMED_GROUP
5099 if (PEND)
return ONIGERR_END_PATTERN_WITH_UNMATCHED_PARENTHESIS;
5101 option = env->option;
5102 if (PPEEK_IS(
'?') &&
5103 IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_QMARK_GROUP_EFFECT)) {
5105 if (PEND)
return ONIGERR_END_PATTERN_IN_GROUP;
5111 r = fetch_token(tok, &p, end, env);
5112 if (r < 0)
return r;
5113 r = parse_subexp(np, tok, term, &p, end, env);
5114 if (r < 0)
return r;
5120 *np = onig_node_new_anchor(ANCHOR_PREC_READ);
5123 *np = onig_node_new_anchor(ANCHOR_PREC_READ_NOT);
5126 *np = node_new_enclose(ENCLOSE_STOP_BACKTRACK);
5129 if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_QMARK_TILDE_ABSENT)) {
5130 *np = node_new_enclose(ENCLOSE_ABSENT);
5133 return ONIGERR_UNDEFINED_GROUP_OPTION;
5137#ifdef USE_NAMED_GROUP
5139 if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_QMARK_LT_NAMED_GROUP)) {
5143 return ONIGERR_UNDEFINED_GROUP_OPTION;
5146# ifdef USE_CAPITAL_P_NAMED_GROUP
5149 IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_QMARK_CAPITAL_P_NAMED_GROUP)) {
5151 if (c ==
'<')
goto named_group1;
5153 return ONIGERR_UNDEFINED_GROUP_OPTION;
5159 if (PEND)
return ONIGERR_END_PATTERN_WITH_UNMATCHED_PARENTHESIS;
5162 *np = onig_node_new_anchor(ANCHOR_LOOK_BEHIND);
5164 *np = onig_node_new_anchor(ANCHOR_LOOK_BEHIND_NOT);
5165#ifdef USE_NAMED_GROUP
5167 if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_QMARK_LT_NAMED_GROUP)) {
5177# ifdef USE_CAPTURE_HISTORY
5181 r = fetch_name((OnigCodePoint )c, &p, end, &name_end, env, &num, 0);
5182 if (r < 0)
return r;
5184 num = scan_env_add_mem_entry(env);
5185 if (num < 0)
return num;
5186 if (list_capture != 0 && num >= (
int )BIT_STATUS_BITS_NUM)
5187 return ONIGERR_GROUP_NUMBER_OVER_FOR_CAPTURE_HISTORY;
5189 r = name_add(env->reg, name, name_end, num, env);
5190 if (r != 0)
return r;
5191 *np = node_new_enclose_memory(env->option, 1);
5192 CHECK_NULL_RETURN_MEMERR(*np);
5193 NENCLOSE(*np)->regnum = num;
5194 if (list_capture != 0)
5195 BIT_STATUS_ON_AT_SIMPLE(env->capture_history, num);
5199 return ONIGERR_UNDEFINED_GROUP_OPTION;
5204 return ONIGERR_UNDEFINED_GROUP_OPTION;
5209#ifdef USE_CAPTURE_HISTORY
5211 if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_ATMARK_CAPTURE_HISTORY)) {
5212# ifdef USE_NAMED_GROUP
5214 IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_QMARK_LT_NAMED_GROUP)) {
5216 if (c ==
'<' || c ==
'\'') {
5223 *np = node_new_enclose_memory(env->option, 0);
5224 CHECK_NULL_RETURN_MEMERR(*np);
5225 num = scan_env_add_mem_entry(env);
5226 if (num < 0)
return num;
5227 if (num >= (
int )BIT_STATUS_BITS_NUM)
5228 return ONIGERR_GROUP_NUMBER_OVER_FOR_CAPTURE_HISTORY;
5230 NENCLOSE(*np)->regnum = num;
5231 BIT_STATUS_ON_AT_SIMPLE(env->capture_history, num);
5234 return ONIGERR_UNDEFINED_GROUP_OPTION;
5241 IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_QMARK_LPAREN_CONDITION)) {
5245 if (ONIGENC_IS_CODE_DIGIT(enc, c)) {
5247 r = fetch_name((OnigCodePoint )
'(', &p, end, &name_end, env, &num, 1);
5248 if (r < 0)
return r;
5252 num = BACKREF_REL_TO_ABS(num, env);
5254 return ONIGERR_INVALID_BACKREF;
5257 if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_STRICT_CHECK_BACKREF)) {
5258 if (num > env->num_mem ||
5259 IS_NULL(SCANENV_MEM_NODES(env)[num]))
5260 return ONIGERR_INVALID_BACKREF;
5263#ifdef USE_NAMED_GROUP
5264 else if (c ==
'<' || c ==
'\'') {
5266 r = fetch_named_backref_token(c, tok, &p, end, env);
5267 if (r < 0)
return r;
5268 if (!PPEEK_IS(
')'))
return ONIGERR_UNDEFINED_GROUP_OPTION;
5271 if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_USE_LEFT_MOST_NAMED_GROUP)) {
5272 num = tok->u.backref.ref1;
5279 int len = tok->u.backref.num;
5280 num =
len > 1 ? tok->u.backref.refs[0] : tok->u.backref.ref1;
5285 return ONIGERR_INVALID_CONDITION_PATTERN;
5286 *np = node_new_enclose(ENCLOSE_CONDITION);
5287 CHECK_NULL_RETURN_MEMERR(*np);
5288 NENCLOSE(*np)->regnum = num;
5289 if (IS_NOT_NULL(name)) NENCLOSE(*np)->state |= NST_NAME_REF;
5292 return ONIGERR_UNDEFINED_GROUP_OPTION;
5297 if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_QMARK_VBAR_BRANCH_RESET)) {
5301 return ONIGERR_UNDEFINED_GROUP_OPTION;
5306 if (!PEND && IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_OPTION_PERL)) {
5308 ONOFF(option, ONIG_OPTION_ASCII_RANGE, 1);
5309 ONOFF(option, ONIG_OPTION_IGNORECASE, 1);
5310 ONOFF(option, ONIG_OPTION_SINGLELINE, 0);
5311 ONOFF(option, ONIG_OPTION_MULTILINE, 1);
5312 ONOFF(option, ONIG_OPTION_EXTEND, 1);
5316 else if (!PEND && IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_OPTION_RUBY)) {
5318 ONOFF(option, ONIG_OPTION_ASCII_RANGE, 0);
5319 ONOFF(option, ONIG_OPTION_POSIX_BRACKET_ALL_RANGE, 0);
5320 ONOFF(option, ONIG_OPTION_WORD_BOUND_ALL_RANGE, 0);
5321 ONOFF(option, ONIG_OPTION_IGNORECASE, 1);
5322 ONOFF(option, ONIG_OPTION_MULTILINE, 1);
5323 ONOFF(option, ONIG_OPTION_EXTEND, 1);
5328 return ONIGERR_UNDEFINED_GROUP_OPTION;
5331#ifdef USE_POSIXLINE_OPTION
5334 case '-':
case 'i':
case 'm':
case 's':
case 'x':
5335 case 'a':
case 'd':
case 'l':
case 'u':
5345 case '-': neg = 1;
break;
5346 case 'x': ONOFF(option, ONIG_OPTION_EXTEND, neg);
break;
5347 case 'i': ONOFF(option, ONIG_OPTION_IGNORECASE, neg);
break;
5349 if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_OPTION_PERL)) {
5350 ONOFF(option, ONIG_OPTION_MULTILINE, neg);
5353 return ONIGERR_UNDEFINED_GROUP_OPTION;
5357 if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_OPTION_PERL)) {
5358 ONOFF(option, ONIG_OPTION_SINGLELINE, (neg == 0 ? 1 : 0));
5360 else if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_OPTION_RUBY)) {
5361 ONOFF(option, ONIG_OPTION_MULTILINE, neg);
5364 return ONIGERR_UNDEFINED_GROUP_OPTION;
5366#ifdef USE_POSIXLINE_OPTION
5368 ONOFF(option, ONIG_OPTION_MULTILINE|ONIG_OPTION_SINGLELINE, neg);
5373 if ((IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_OPTION_PERL) ||
5374 IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_OPTION_RUBY)) &&
5376 ONOFF(option, ONIG_OPTION_ASCII_RANGE, 0);
5377 ONOFF(option, ONIG_OPTION_POSIX_BRACKET_ALL_RANGE, 1);
5378 ONOFF(option, ONIG_OPTION_WORD_BOUND_ALL_RANGE, 1);
5381 return ONIGERR_UNDEFINED_GROUP_OPTION;
5385 if ((IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_OPTION_PERL) ||
5386 IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_OPTION_RUBY)) &&
5388 ONOFF(option, ONIG_OPTION_ASCII_RANGE, 1);
5389 ONOFF(option, ONIG_OPTION_POSIX_BRACKET_ALL_RANGE, 1);
5390 ONOFF(option, ONIG_OPTION_WORD_BOUND_ALL_RANGE, 1);
5393 return ONIGERR_UNDEFINED_GROUP_OPTION;
5397 if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_OPTION_PERL) &&
5399 ONOFF(option, ONIG_OPTION_ASCII_RANGE, 1);
5401 else if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_OPTION_RUBY) &&
5403 ONOFF(option, ONIG_OPTION_ASCII_RANGE, 0);
5404 ONOFF(option, ONIG_OPTION_POSIX_BRACKET_ALL_RANGE, 0);
5405 ONOFF(option, ONIG_OPTION_WORD_BOUND_ALL_RANGE, 0);
5408 return ONIGERR_UNDEFINED_GROUP_OPTION;
5412 if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_OPTION_PERL) && (neg == 0)) {
5413 ONOFF(option, ONIG_OPTION_ASCII_RANGE, 1);
5416 return ONIGERR_UNDEFINED_GROUP_OPTION;
5420 return ONIGERR_UNDEFINED_GROUP_OPTION;
5424 *np = node_new_option(option);
5425 CHECK_NULL_RETURN_MEMERR(*np);
5429 else if (c ==
':') {
5430 OnigOptionType prev = env->option;
5432 env->option = option;
5433 r = fetch_token(tok, &p, end, env);
5438 r = parse_subexp(&target, tok, term, &p, end, env);
5440 if (r < 0)
return r;
5441 *np = node_new_option(option);
5442 CHECK_NULL_RETURN_MEMERR(*np);
5443 NENCLOSE(*np)->target = target;
5448 if (PEND)
return ONIGERR_END_PATTERN_IN_GROUP;
5455 return ONIGERR_UNDEFINED_GROUP_OPTION;
5459 if (ONIG_IS_OPTION_ON(env->option, ONIG_OPTION_DONT_CAPTURE_GROUP))
5462 *np = node_new_enclose_memory(env->option, 0);
5463 CHECK_NULL_RETURN_MEMERR(*np);
5464 num = scan_env_add_mem_entry(env);
5465 if (num < 0)
return num;
5466 NENCLOSE(*np)->regnum = num;
5469 CHECK_NULL_RETURN_MEMERR(*np);
5470 r = fetch_token(tok, &p, end, env);
5471 if (r < 0)
return r;
5472 r = parse_subexp(&target, tok, term, &p, end, env);
5474 onig_node_free(target);
5478 if (NTYPE(*np) == NT_ANCHOR)
5479 NANCHOR(*np)->target = target;
5481 NENCLOSE(*np)->target = target;
5482 if (NENCLOSE(*np)->type == ENCLOSE_MEMORY) {
5484 r = scan_env_set_mem_node(env, NENCLOSE(*np)->regnum, *np);
5485 if (r != 0)
return r;
5487 else if (NENCLOSE(*np)->type == ENCLOSE_CONDITION) {
5488 if (NTYPE(target) != NT_ALT) {
5490 work1 = node_new_empty();
5491 if (IS_NULL(work1))
goto err;
5492 work2 = onig_node_new_alt(work1, NULL_NODE);
5493 if (IS_NULL(work2))
goto err;
5494 work1 = onig_node_new_alt(target, work2);
5495 if (IS_NULL(work1))
goto err;
5496 NENCLOSE(*np)->target = work1;
5505 onig_node_free(work1);
5506 onig_node_free(work2);
5507 onig_node_free(*np);
5509 return ONIGERR_MEMORY;
5512static const char*
const PopularQStr[] = {
5513 "?",
"*",
"+",
"??",
"*?",
"+?"
5516static const char*
const ReduceQStr[] = {
5517 "",
"",
"*",
"*?",
"??",
"+ and ??",
"+? and ?"
5526 if (qn->lower == 1 && qn->upper == 1) {
5530 switch (NTYPE(target)) {
5534 if (str_node_can_be_split(sn, env->enc)) {
5535 Node* n = str_node_split_last_char(sn, env->enc);
5536 if (IS_NOT_NULL(n)) {
5548 int nestq_num = popular_quantifier_num(qn);
5549 int targetq_num = popular_quantifier_num(qnt);
5551#ifdef USE_WARNING_REDUNDANT_NESTED_REPEAT_OPERATOR
5552 if (nestq_num >= 0 && targetq_num >= 0 &&
5553 IS_SYNTAX_BV(env->syntax, ONIG_SYN_WARN_REDUNDANT_NESTED_REPEAT)) {
5554 switch (ReduceTypeTable[targetq_num][nestq_num]) {
5559 if (onig_warn != onig_null_warn) {
5560 onig_syntax_warn(env,
"regular expression has redundant nested repeat operator '%s'",
5561 PopularQStr[targetq_num]);
5567 if (onig_warn != onig_null_warn) {
5568 onig_syntax_warn(env,
"nested repeat operator '%s' and '%s' was replaced with '%s' in regular expression",
5569 PopularQStr[targetq_num], PopularQStr[nestq_num],
5570 ReduceQStr[ReduceTypeTable[targetq_num][nestq_num]]);
5579 if (targetq_num >= 0) {
5580 if (nestq_num >= 0) {
5581 onig_reduce_nested_quantifier(qnode, target);
5584 else if (targetq_num == 1 || targetq_num == 2) {
5586 if (! IS_REPEAT_INFINITE(qn->upper) && qn->upper > 1 && qn->greedy) {
5587 qn->upper = (qn->lower == 0 ? 1 : qn->lower);
5598 qn->target = target;
5604#ifndef CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS
5611 if (IS_NCCLASS_NOT(cc)) {
5612 bitset_invert(cc->bs);
5614 if (! ONIGENC_IS_SINGLEBYTE(enc)) {
5615 r = not_code_range_buf(enc, cc->mbuf, &tbuf);
5616 if (r != 0)
return r;
5618 bbuf_free(cc->mbuf);
5622 NCCLASS_CLEAR_NOT(cc);
5638i_apply_case_fold(OnigCodePoint from, OnigCodePoint to[],
5639 int to_len,
void* arg)
5651 asc_cc = iarg->asc_cc;
5654 if (IS_NULL(asc_cc)) {
5657 else if (ONIGENC_IS_ASCII_CODE(from) == ONIGENC_IS_ASCII_CODE(*to)) {
5661 add_flag = onig_is_code_in_cc(env->enc, from, asc_cc);
5662 if (IS_NCCLASS_NOT(asc_cc))
5663 add_flag = !add_flag;
5667 int is_in = onig_is_code_in_cc(env->enc, from, cc);
5668#ifdef CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS
5669 if ((is_in != 0 && !IS_NCCLASS_NOT(cc)) ||
5670 (is_in == 0 && IS_NCCLASS_NOT(cc))) {
5672 if (ONIGENC_MBC_MINLEN(env->enc) > 1 || *to >= SINGLE_BYTE_SIZE) {
5673 r = add_code_range0(&(cc->mbuf), env, *to, *to, 0);
5674 if (r < 0)
return r;
5677 BITSET_SET_BIT(bs, *to);
5684 if (ONIGENC_MBC_MINLEN(env->enc) > 1 || *to >= SINGLE_BYTE_SIZE) {
5685 if (IS_NCCLASS_NOT(cc)) clear_not_flag_cclass(cc, env->enc);
5686 r = add_code_range0(&(cc->mbuf), env, *to, *to, 0);
5687 if (r < 0)
return r;
5690 if (IS_NCCLASS_NOT(cc)) {
5691 BITSET_CLEAR_BIT(bs, *to);
5694 BITSET_SET_BIT(bs, *to);
5703 UChar buf[ONIGENC_CODE_TO_MBC_MAXLEN];
5704 Node *snode = NULL_NODE;
5706 if (onig_is_code_in_cc(env->enc, from, cc)
5707#ifdef CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS
5708 && !IS_NCCLASS_NOT(cc)
5711 for (i = 0; i < to_len; i++) {
5712 len = ONIGENC_CODE_TO_MBC(env->enc, to[i], buf);
5714 snode = onig_node_new_str(buf, buf +
len);
5715 CHECK_NULL_RETURN_MEMERR(snode);
5719 NSTRING_SET_AMBIG(snode);
5722 r = onig_node_str_cat(snode, buf, buf +
len);
5724 onig_node_free(snode);
5730 *(iarg->ptail) = onig_node_new_alt(snode, NULL_NODE);
5731 CHECK_NULL_RETURN_MEMERR(*(iarg->ptail));
5732 iarg->ptail = &(NCDR((*(iarg->ptail))));
5747 iarg.asc_cc = asc_cc;
5748 iarg.alt_root = NULL_NODE;
5749 iarg.ptail = &(iarg.alt_root);
5751 r = ONIGENC_APPLY_ALL_CASE_FOLD(env->enc, env->case_fold_flag,
5752 i_apply_case_fold, &iarg);
5754 onig_node_free(iarg.alt_root);
5757 if (IS_NOT_NULL(iarg.alt_root)) {
5758 Node* work = onig_node_new_alt(*np, iarg.alt_root);
5759 if (IS_NULL(work)) {
5760 onig_node_free(iarg.alt_root);
5761 return ONIGERR_MEMORY;
5774 Node* target1 = NULL;
5775 Node* target2 = NULL;
5778 UChar buf[ONIGENC_CODE_TO_MBC_MAXLEN * 2];
5781 num1 = ONIGENC_CODE_TO_MBC(env->enc, 0x0D, buf);
5782 if (num1 < 0)
return num1;
5783 num2 = ONIGENC_CODE_TO_MBC(env->enc, 0x0A, buf + num1);
5784 if (num2 < 0)
return num2;
5785 left = node_new_str_raw(buf, buf + num1 + num2);
5786 if (IS_NULL(left))
goto err;
5789 right = node_new_cclass();
5790 if (IS_NULL(right))
goto err;
5791 cc = NCCLASS(right);
5792 if (ONIGENC_MBC_MINLEN(env->enc) > 1) {
5793 r = add_code_range(&(cc->mbuf), env, 0x0A, 0x0D);
5794 if (r != 0)
goto err;
5797 bitset_set_range(env, cc->bs, 0x0A, 0x0D);
5801 if (ONIGENC_IS_UNICODE(env->enc)) {
5803 r = add_code_range(&(cc->mbuf), env, 0x85, 0x85);
5804 if (r != 0)
goto err;
5805 r = add_code_range(&(cc->mbuf), env, 0x2028, 0x2029);
5806 if (r != 0)
goto err;
5810 target1 = onig_node_new_alt(right, NULL_NODE);
5811 if (IS_NULL(target1))
goto err;
5813 target2 = onig_node_new_alt(left, target1);
5814 if (IS_NULL(target2))
goto err;
5819 *np = node_new_enclose(ENCLOSE_STOP_BACKTRACK);
5820 if (IS_NULL(*np))
goto err;
5821 NENCLOSE(*np)->target = target2;
5825 onig_node_free(left);
5826 onig_node_free(right);
5827 onig_node_free(target1);
5828 onig_node_free(target2);
5829 return ONIGERR_MEMORY;
5833propname2ctype(
ScanEnv* env,
const char* propname)
5835 UChar* name = (UChar* )propname;
5836 UChar* name_end = name + strlen(propname);
5837 int ctype = env->enc->property_name_to_ctype(ONIG_ENCODING_ASCII,
5840 onig_scan_env_set_error_string(env, ctype, name, name_end);
5848 int ctype = propname2ctype(env, propname);
5849 if (ctype < 0)
return ctype;
5850 return add_ctype_to_cc(cc, ctype, not, 0, env);
5857create_property_node(
Node **np,
ScanEnv* env,
const char* propname)
5862 *np = node_new_cclass();
5863 if (IS_NULL(*np))
return ONIGERR_MEMORY;
5865 r = add_property_to_cc(cc, propname, 0, env);
5867 onig_node_free(*np);
5872quantify_node(
Node **np,
int lower,
int upper)
5874 Node* tmp = node_new_quantifier(lower, upper, 0);
5875 if (IS_NULL(tmp))
return ONIGERR_MEMORY;
5876 NQTFR(tmp)->target = *np;
5882quantify_property_node(
Node **np,
ScanEnv* env,
const char* propname,
char repetitions)
5886 int upper = REPEAT_INFINITE;
5888 r = create_property_node(np, env, propname);
5889 if (r != 0)
return r;
5890 switch (repetitions) {
5891 case '?': upper = 1;
break;
5892 case '+': lower = 1;
break;
5894 case '2': lower = upper = 2;
break;
5895 default :
return ONIGERR_PARSER_BUG;
5897 return quantify_node(np, lower, upper);
5905create_node_from_array(
int kind,
Node **np,
Node **node_array)
5907 Node* tmp = NULL_NODE;
5910 while (node_array[i] != NULL_NODE) i++;
5912 *np = kind==LIST ? node_new_list(node_array[i], tmp)
5913 : onig_node_new_alt(node_array[i], tmp);
5916 onig_node_free(node_array[i]);
5917 node_array[i--] = NULL_NODE;
5919 onig_node_free(tmp);
5920 return ONIGERR_MEMORY;
5923 node_array[i] = NULL_NODE;
5929#define R_ERR(call) r=(call);if(r!=0)goto err
5952#define NODE_COMMON_SIZE 16
5955node_extended_grapheme_cluster(
Node** np,
ScanEnv* env)
5959 Node* top_alt = NULL;
5963 int any_target_position;
5964 UChar buf[ONIGENC_CODE_TO_MBC_MAXLEN * 2];
5965 OnigOptionType option;
5968 Node *node_common[NODE_COMMON_SIZE];
5969 Node **alts = node_common+0;
5971 for (i=0; i<NODE_COMMON_SIZE; i++)
5972 node_common[i] = NULL_NODE;
5976 r = ONIGENC_CODE_TO_MBC(env->enc, 0x0D, buf);
5977 if (r < 0)
goto err;
5979 r = ONIGENC_CODE_TO_MBC(env->enc, 0x0A, buf + num1);
5980 if (r < 0)
goto err;
5981 alts[0] = node_new_str_raw(buf, buf + num1 + r);
5982 if (IS_NULL(alts[0]))
goto err;
5984#ifdef USE_UNICODE_PROPERTIES
5985 if (ONIGENC_IS_UNICODE(env->enc)) {
5988 if (propname2ctype(env,
"Grapheme_Cluster_Break=Extend") < 0)
goto err;
5996 alts[1] = node_new_cclass();
5997 if (IS_NULL(alts[1]))
goto err;
5998 cc = NCCLASS(alts[1]);
5999 R_ERR(add_property_to_cc(cc,
"Grapheme_Cluster_Break=Control", 0, env));
6000 if (ONIGENC_MBC_MINLEN(env->enc) > 1) {
6001 R_ERR(add_code_range(&(cc->mbuf), env, 0x000A, 0x000A));
6002 R_ERR(add_code_range(&(cc->mbuf), env, 0x000D, 0x000D));
6005 BITSET_SET_BIT(cc->bs, 0x0a);
6006 BITSET_SET_BIT(cc->bs, 0x0d);
6011 Node **list = alts + 3;
6014 R_ERR(quantify_property_node(list+0, env,
"Grapheme_Cluster_Break=Prepend",
'*'));
6021 Node **core_alts = list + 2;
6032 Node **H_list = core_alts + 1;
6033 R_ERR(quantify_property_node(H_list+0, env,
"Grapheme_Cluster_Break=L",
'*'));
6037 Node **H_alt2 = H_list + 2;
6038 R_ERR(quantify_property_node(H_alt2+0, env,
"Grapheme_Cluster_Break=V",
'+'));
6042 Node **H_list2 = H_alt2 + 2;
6044 R_ERR(create_property_node(H_list2+0, env,
"Grapheme_Cluster_Break=LV"));
6045 R_ERR(quantify_property_node(H_list2+1, env,
"Grapheme_Cluster_Break=V",
'*'));
6046 R_ERR(create_node_from_array(LIST, H_alt2+1, H_list2));
6049 R_ERR(create_property_node(H_alt2+2, env,
"Grapheme_Cluster_Break=LVT"));
6050 R_ERR(create_node_from_array(ALT, H_list+1, H_alt2));
6053 R_ERR(quantify_property_node(H_list+2, env,
"Grapheme_Cluster_Break=T",
'*'));
6054 R_ERR(create_node_from_array(LIST, core_alts+0, H_list));
6057 R_ERR(quantify_property_node(core_alts+1, env,
"Grapheme_Cluster_Break=L",
'+'));
6058 R_ERR(quantify_property_node(core_alts+2, env,
"Grapheme_Cluster_Break=T",
'+'));
6062 R_ERR(quantify_property_node(core_alts+3, env,
"Regional_Indicator",
'2'));
6066 Node **XP_list = core_alts + 5;
6067 R_ERR(create_property_node(XP_list+0, env,
"Extended_Pictographic"));
6071 Node **Ex_list = XP_list + 2;
6073 R_ERR(quantify_property_node(Ex_list+0, env,
"Grapheme_Cluster_Break=Extend",
'*'));
6076 r = ONIGENC_CODE_TO_MBC(env->enc, 0x200D, buf);
6077 if (r < 0)
goto err;
6078 Ex_list[1] = node_new_str_raw(buf, buf + r);
6079 if (IS_NULL(Ex_list[1]))
goto err;
6081 R_ERR(create_property_node(Ex_list+2, env,
"Extended_Pictographic"));
6082 R_ERR(create_node_from_array(LIST, XP_list+1, Ex_list));
6084 R_ERR(quantify_node(XP_list+1, 0, REPEAT_INFINITE));
6086 R_ERR(create_node_from_array(LIST, core_alts+4, XP_list));
6090 core_alts[5] = node_new_cclass();
6091 if (IS_NULL(core_alts[5]))
goto err;
6092 cc = NCCLASS(core_alts[5]);
6093 if (ONIGENC_MBC_MINLEN(env->enc) > 1) {
6094 BBuf *inverted_buf = NULL;
6097 const int dup_not_warned = env->warnings_flag | ~ONIG_SYN_WARN_CC_DUP;
6098 env->warnings_flag |= ONIG_SYN_WARN_CC_DUP;
6102 R_ERR(add_property_to_cc(cc,
"Grapheme_Cluster_Break=Control", 0, env));
6103 R_ERR(add_code_range(&(cc->mbuf), env, 0x000A, 0x000A));
6104 R_ERR(add_code_range(&(cc->mbuf), env, 0x000D, 0x000D));
6105 R_ERR(not_code_range_buf(env->enc, cc->mbuf, &inverted_buf, env));
6106 bbuf_free(cc->mbuf);
6107 cc->mbuf = inverted_buf;
6109 env->warnings_flag &= dup_not_warned;
6112 R_ERR(add_property_to_cc(cc,
"Grapheme_Cluster_Break=Control", 1, env));
6113 BITSET_CLEAR_BIT(cc->bs, 0x0a);
6114 BITSET_CLEAR_BIT(cc->bs, 0x0d);
6117 R_ERR(create_node_from_array(ALT, list+1, core_alts));
6121 R_ERR(create_property_node(list+2, env,
"Grapheme_Cluster_Break=Extend"));
6122 cc = NCCLASS(list[2]);
6123 R_ERR(add_property_to_cc(cc,
"Grapheme_Cluster_Break=SpacingMark", 0, env));
6124 R_ERR(add_code_range(&(cc->mbuf), env, 0x200D, 0x200D));
6125 R_ERR(quantify_node(list+2, 0, REPEAT_INFINITE));
6127 R_ERR(create_node_from_array(LIST, alts+2, list));
6130 any_target_position = 3;
6135 any_target_position = 1;
6141 np1 = node_new_anychar();
6142 if (IS_NULL(np1))
goto err;
6144 option = env->option;
6145 ONOFF(option, ONIG_OPTION_MULTILINE, 0);
6146 tmp = node_new_option(option);
6147 if (IS_NULL(tmp))
goto err;
6148 NENCLOSE(tmp)->target = np1;
6149 alts[any_target_position] = tmp;
6152 R_ERR(create_node_from_array(ALT, &top_alt, alts));
6157 tmp = node_new_enclose(ENCLOSE_STOP_BACKTRACK);
6158 if (IS_NULL(tmp))
goto err;
6159 NENCLOSE(tmp)->target = top_alt;
6162#ifdef USE_UNICODE_PROPERTIES
6163 if (ONIGENC_IS_UNICODE(env->enc)) {
6165 option = env->option;
6166 ONOFF(option, ONIG_OPTION_IGNORECASE, 1);
6167 *np = node_new_option(option);
6168 if (IS_NULL(*np))
goto err;
6169 NENCLOSE(*np)->target = np1;
6179 onig_node_free(np1);
6180 for (i=0; i<NODE_COMMON_SIZE; i++)
6181 onig_node_free(node_common[i]);
6182 return (r == 0) ? ONIGERR_MEMORY : r;
6187countbits(
unsigned int bits)
6189 bits = (bits & 0x55555555) + ((bits >> 1) & 0x55555555);
6190 bits = (bits & 0x33333333) + ((bits >> 2) & 0x33333333);
6191 bits = (bits & 0x0f0f0f0f) + ((bits >> 4) & 0x0f0f0f0f);
6192 bits = (bits & 0x00ff00ff) + ((bits >> 8) & 0x00ff00ff);
6193 return (bits & 0x0000ffff) + ((bits >>16) & 0x0000ffff);
6197is_onechar_cclass(
CClassNode* cc, OnigCodePoint* code)
6199 const OnigCodePoint not_found = ONIG_LAST_CODE_POINT;
6200 OnigCodePoint c = not_found;
6202 BBuf *bbuf = cc->mbuf;
6204 if (IS_NCCLASS_NOT(cc))
return 0;
6207 if (IS_NOT_NULL(bbuf)) {
6208 OnigCodePoint n, *data;
6209 GET_CODE_POINT(n, bbuf->p);
6210 data = (OnigCodePoint* )(bbuf->p) + 1;
6211 if ((n == 1) && (data[0] == data[1])) {
6214 if (((c < SINGLE_BYTE_SIZE) && BITSET_AT(cc->bs, c))) {
6225 for (i = 0; i < BITSET_SIZE; i++) {
6226 Bits b1 = cc->bs[i];
6228 if (((b1 & (b1 - 1)) == 0) && (c == not_found)) {
6229 c = BITS_IN_ROOM * i + countbits(b1 - 1);
6236 if (c != not_found) {
6248 UChar** src, UChar* end,
ScanEnv* env)
6250 int r,
len, group = 0;
6255 if (tok->type == (
enum TokenSyms )term)
6258 switch (tok->type) {
6262 *np = node_new_empty();
6266 case TK_SUBEXP_OPEN:
6267 r = parse_enclose(np, tok, TK_SUBEXP_CLOSE, src, end, env);
6268 if (r < 0)
return r;
6269 if (r == 1) group = 1;
6272 OnigOptionType prev = env->option;
6274 env->option = NENCLOSE(*np)->option;
6275 r = fetch_token(tok, src, end, env);
6280 r = parse_subexp(&target, tok, term, src, end, env);
6283 onig_node_free(target);
6286 NENCLOSE(*np)->target = target;
6291 case TK_SUBEXP_CLOSE:
6292 if (! IS_SYNTAX_BV(env->syntax, ONIG_SYN_ALLOW_UNMATCHED_CLOSE_SUBEXP))
6293 return ONIGERR_UNMATCHED_CLOSE_PARENTHESIS;
6295 if (tok->escaped)
goto tk_raw_byte;
6300 r = node_linebreak(np, env);
6301 if (r < 0)
return r;
6304 case TK_EXTENDED_GRAPHEME_CLUSTER:
6305 r = node_extended_grapheme_cluster(np, env);
6306 if (r < 0)
return r;
6310 *np = onig_node_new_anchor(ANCHOR_KEEP);
6311 CHECK_NULL_RETURN_MEMERR(*np);
6317 *np = node_new_str(tok->backp, *src);
6318 CHECK_NULL_RETURN_MEMERR(*np);
6322 r = fetch_token(tok, src, end, env);
6323 if (r < 0)
return r;
6324 if (r == TK_STRING) {
6325 r = onig_node_str_cat(*np, tok->backp, *src);
6327#ifndef NUMBERED_CHAR_IS_NOT_CASE_AMBIG
6328 else if (r == TK_CODE_POINT) {
6329 r = node_str_cat_codepoint(*np, env->enc, tok->u.code);
6335 if (r < 0)
return r;
6347 *np = node_new_str_raw_char((UChar )tok->u.c);
6348 CHECK_NULL_RETURN_MEMERR(*np);
6351 if (
len >= ONIGENC_MBC_MINLEN(env->enc)) {
6352 if (
len == enclen(env->enc, NSTR(*np)->s, NSTR(*np)->end)) {
6353 r = fetch_token(tok, src, end, env);
6354 NSTRING_CLEAR_RAW(*np);
6359 r = fetch_token(tok, src, end, env);
6360 if (r < 0)
return r;
6361 if (r != TK_RAW_BYTE) {
6363#ifdef USE_PAD_TO_SHORT_BYTE_CHAR
6365 if (
len < ONIGENC_MBC_MINLEN(env->enc)) {
6366 rem = ONIGENC_MBC_MINLEN(env->enc) -
len;
6367 (void )node_str_head_pad(NSTR(*np), rem, (UChar )0);
6368 if (
len + rem == enclen(env->enc, NSTR(*np)->s)) {
6369 NSTRING_CLEAR_RAW(*np);
6374 return ONIGERR_TOO_SHORT_MULTI_BYTE_STRING;
6377 r = node_str_cat_char(*np, (UChar )tok->u.c);
6378 if (r < 0)
return r;
6387 *np = node_new_empty();
6388 CHECK_NULL_RETURN_MEMERR(*np);
6389 r = node_str_cat_codepoint(*np, env->enc, tok->u.code);
6390 if (r != 0)
return r;
6391#ifdef NUMBERED_CHAR_IS_NOT_CASE_AMBIG
6392 NSTRING_SET_RAW(*np);
6401 OnigCodePoint end_op[2];
6402 UChar *qstart, *qend, *nextp;
6404 end_op[0] = (OnigCodePoint )MC_ESC(env->syntax);
6405 end_op[1] = (OnigCodePoint )
'E';
6407 qend = find_str_position(end_op, 2, qstart, end, &nextp, env->enc);
6408 if (IS_NULL(qend)) {
6411 *np = node_new_str(qstart, qend);
6412 CHECK_NULL_RETURN_MEMERR(*np);
6419 switch (tok->u.prop.ctype) {
6420 case ONIGENC_CTYPE_WORD:
6421 *np = node_new_ctype(tok->u.prop.ctype, tok->u.prop.not,
6422 IS_ASCII_RANGE(env->option));
6423 CHECK_NULL_RETURN_MEMERR(*np);
6426 case ONIGENC_CTYPE_SPACE:
6427 case ONIGENC_CTYPE_DIGIT:
6428 case ONIGENC_CTYPE_XDIGIT:
6432 *np = node_new_cclass();
6433 CHECK_NULL_RETURN_MEMERR(*np);
6435 r = add_ctype_to_cc(cc, tok->u.prop.ctype, 0,
6436 IS_ASCII_RANGE(env->option), env);
6437 if (r != 0)
return r;
6438 if (tok->u.prop.not != 0) NCCLASS_SET_NOT(cc);
6443 return ONIGERR_PARSER_BUG;
6449 case TK_CHAR_PROPERTY:
6450 r = parse_char_property(np, tok, src, end, env);
6451 if (r != 0)
return r;
6460 r = parse_char_class(np, &asc_node, tok, src, end, env);
6462 onig_node_free(asc_node);
6467 if (is_onechar_cclass(cc, &code)) {
6468 onig_node_free(*np);
6469 onig_node_free(asc_node);
6470 *np = node_new_empty();
6471 CHECK_NULL_RETURN_MEMERR(*np);
6472 r = node_str_cat_codepoint(*np, env->enc, code);
6473 if (r != 0)
return r;
6476 if (IS_IGNORECASE(env->option)) {
6477 r = cclass_case_fold(np, cc, NCCLASS(asc_node), env);
6479 onig_node_free(asc_node);
6483 onig_node_free(asc_node);
6488 *np = node_new_anychar();
6489 CHECK_NULL_RETURN_MEMERR(*np);
6492 case TK_ANYCHAR_ANYTIME:
6493 *np = node_new_anychar();
6494 CHECK_NULL_RETURN_MEMERR(*np);
6495 qn = node_new_quantifier(0, REPEAT_INFINITE, 0);
6496 CHECK_NULL_RETURN_MEMERR(qn);
6497 NQTFR(qn)->target = *np;
6502 len = tok->u.backref.num;
6503 *np = node_new_backref(
len,
6504 (
len > 1 ? tok->u.backref.refs : &(tok->u.backref.ref1)),
6505 tok->u.backref.by_name,
6506#ifdef USE_BACKREF_WITH_LEVEL
6507 tok->u.backref.exist_level,
6508 tok->u.backref.level,
6511 CHECK_NULL_RETURN_MEMERR(*np);
6514#ifdef USE_SUBEXP_CALL
6517 int gnum = tok->u.call.gnum;
6519 if (gnum < 0 || tok->u.call.rel != 0) {
6520 if (gnum > 0) gnum--;
6521 gnum = BACKREF_REL_TO_ABS(gnum, env);
6523 return ONIGERR_INVALID_BACKREF;
6525 *np = node_new_call(tok->u.call.name, tok->u.call.name_end, gnum);
6526 CHECK_NULL_RETURN_MEMERR(*np);
6533 *np = onig_node_new_anchor(tok->u.anchor.subtype);
6534 CHECK_NULL_RETURN_MEMERR(*np);
6535 NANCHOR(*np)->ascii_range = tok->u.anchor.ascii_range;
6540 if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_CONTEXT_INDEP_REPEAT_OPS)) {
6541 if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_CONTEXT_INVALID_REPEAT_OPS))
6542 return ONIGERR_TARGET_OF_REPEAT_OPERATOR_NOT_SPECIFIED;
6544 *np = node_new_empty();
6552 return ONIGERR_PARSER_BUG;
6560 r = fetch_token(tok, src, end, env);
6561 if (r < 0)
return r;
6564 if (r == TK_OP_REPEAT || r == TK_INTERVAL) {
6565 if (is_invalid_quantifier_target(*targetp))
6566 return ONIGERR_TARGET_OF_REPEAT_OPERATOR_INVALID;
6568 qn = node_new_quantifier(tok->u.repeat.lower, tok->u.repeat.upper,
6569 (r == TK_INTERVAL ? 1 : 0));
6570 CHECK_NULL_RETURN_MEMERR(qn);
6571 NQTFR(qn)->greedy = tok->u.repeat.greedy;
6572 r = set_quantifier(qn, *targetp, group, env);
6578 if (tok->u.repeat.possessive != 0) {
6580 en = node_new_enclose(ENCLOSE_STOP_BACKTRACK);
6583 return ONIGERR_MEMORY;
6585 NENCLOSE(en)->target = qn;
6598 *targetp = node_new_list(*targetp, NULL);
6599 if (IS_NULL(*targetp)) {
6601 return ONIGERR_MEMORY;
6603 tmp = NCDR(*targetp) = node_new_list(qn, NULL);
6606 return ONIGERR_MEMORY;
6608 targetp = &(NCAR(tmp));
6619 UChar** src, UChar* end,
ScanEnv* env)
6622 Node *node, **headp;
6625 r = parse_exp(&node, tok, term, src, end, env);
6627 onig_node_free(node);
6631 if (r == TK_EOT || r == term || r == TK_ALT) {
6635 *top = node_new_list(node, NULL);
6636 headp = &(NCDR(*top));
6637 while (r != TK_EOT && r != term && r != TK_ALT) {
6638 r = parse_exp(&node, tok, term, src, end, env);
6640 onig_node_free(node);
6644 if (NTYPE(node) == NT_LIST) {
6646 while (IS_NOT_NULL(NCDR(node))) node = NCDR(node);
6647 headp = &(NCDR(node));
6650 *headp = node_new_list(node, NULL);
6651 headp = &(NCDR(*headp));
6662 UChar** src, UChar* end,
ScanEnv* env)
6665 Node *node, **headp;
6669 if (env->parse_depth > ParseDepthLimit)
6670 return ONIGERR_PARSE_DEPTH_LIMIT_OVER;
6671 r = parse_branch(&node, tok, term, src, end, env);
6673 onig_node_free(node);
6680 else if (r == TK_ALT) {
6681 *top = onig_node_new_alt(node, NULL);
6682 headp = &(NCDR(*top));
6683 while (r == TK_ALT) {
6684 r = fetch_token(tok, src, end, env);
6686 onig_node_free(node);
6689 r = parse_branch(&node, tok, term, src, end, env);
6691 onig_node_free(node);
6695 *headp = onig_node_new_alt(node, NULL);
6696 headp = &(NCDR(*headp));
6699 if (tok->type != (
enum TokenSyms )term)
6703 onig_node_free(node);
6705 if (term == TK_SUBEXP_CLOSE)
6706 return ONIGERR_END_PATTERN_WITH_UNMATCHED_PARENTHESIS;
6708 return ONIGERR_PARSER_BUG;
6716parse_regexp(
Node** top, UChar** src, UChar* end,
ScanEnv* env)
6721 r = fetch_token(&tok, src, end, env);
6722 if (r < 0)
return r;
6723 r = parse_subexp(top, &tok, TK_EOT, src, end, env);
6724 if (r < 0)
return r;
6726#ifdef USE_SUBEXP_CALL
6727 if (env->num_call > 0) {
6731 np = node_new_enclose_memory(env->option, 0);
6732 CHECK_NULL_RETURN_MEMERR(np);
6733 NENCLOSE(np)->regnum = num;
6734 NENCLOSE(np)->target = *top;
6735 r = scan_env_set_mem_node(env, num, np);
6747onig_parse_make_tree(
Node** root,
const UChar* pattern,
const UChar* end,
6753#ifdef USE_NAMED_GROUP
6757 scan_env_clear(env);
6758 env->option = reg->options;
6759 env->case_fold_flag = reg->case_fold_flag;
6760 env->enc = reg->enc;
6761 env->syntax = reg->syntax;
6762 env->pattern = (UChar* )pattern;
6763 env->pattern_end = (UChar* )end;
6767 p = (UChar* )pattern;
6768 r = parse_regexp(root, &p, (UChar* )end, env);
6769 reg->num_mem = env->num_mem;
6774onig_scan_env_set_error_string(
ScanEnv* env,
int ecode ARG_UNUSED,
6775 UChar* arg, UChar* arg_end)
6778 env->error_end = arg_end;
#define xfree
Old name of ruby_xfree.
#define xrealloc
Old name of ruby_xrealloc.
#define xmalloc
Old name of ruby_xmalloc.
#define ruby_verbose
This variable controls whether the interpreter is in debug mode.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
int capa
Designed capacity of the buffer.
int len
Length of the buffer.
VALUE type(ANYARGS)
ANYARGS-ed function type.
#define RTEST
This is an old name of RB_TEST.