summaryrefslogtreecommitdiff
path: root/regparse.c
diff options
context:
space:
mode:
authornaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-12-10 17:47:04 +0000
committernaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-12-10 17:47:04 +0000
commit2873edeafb6f6df1fc99bb9b1167591b99dd378c (patch)
treedeb58ca3dc6d6cd71c1740e62aa7e47bea5ed37e /regparse.c
parent42a677c895f82bcd611db2773fbe68b0558b142d (diff)
Merge Onigmo 6.0.0
* https://github.com/k-takata/Onigmo/blob/Onigmo-6.0.0/HISTORY * fix for ruby 2.4: https://github.com/k-takata/Onigmo/pull/78 * suppress warning: https://github.com/k-takata/Onigmo/pull/79 * include/ruby/oniguruma.h: include onigmo.h. * template/encdb.h.tmpl: ignore duplicated definition of EUC-CN in enc/euc_kr.c. It is defined in enc/gb2313.c with CRuby macro. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57045 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'regparse.c')
-rw-r--r--regparse.c1099
1 files changed, 491 insertions, 608 deletions
diff --git a/regparse.c b/regparse.c
index fba0a34..204aa46 100644
--- a/regparse.c
+++ b/regparse.c
@@ -3,7 +3,7 @@
**********************************************************************/
/*-
* Copyright (c) 2002-2008 K.Kosako <sndgk393 AT ybb DOT ne DOT jp>
- * Copyright (c) 2011-2014 K.Takata <kentkt AT csc DOT jp>
+ * Copyright (c) 2011-2016 K.Takata <kentkt AT csc DOT jp>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -29,6 +29,7 @@
*/
#include "regparse.h"
+#include <stdarg.h>
#define WARN_BUFSIZE 256
@@ -51,6 +52,9 @@ const OnigSyntaxType OnigSyntaxRuby = {
ONIG_SYN_OP2_CCLASS_SET_OP | ONIG_SYN_OP2_ESC_CAPITAL_C_BAR_CONTROL |
ONIG_SYN_OP2_ESC_CAPITAL_M_BAR_META | ONIG_SYN_OP2_ESC_V_VTAB |
ONIG_SYN_OP2_ESC_H_XDIGIT |
+#ifndef RUBY
+ ONIG_SYN_OP2_ESC_U_HEX4 |
+#endif
ONIG_SYN_OP2_ESC_CAPITAL_X_EXTENDED_GRAPHEME_CLUSTER |
ONIG_SYN_OP2_QMARK_LPAREN_CONDITION |
ONIG_SYN_OP2_ESC_CAPITAL_R_LINEBREAK |
@@ -105,6 +109,26 @@ extern void onig_set_verb_warn_func(OnigWarnFunc f)
static void CC_DUP_WARN(ScanEnv *env);
+
+static unsigned int ParseDepthLimit = DEFAULT_PARSE_DEPTH_LIMIT;
+
+extern unsigned int
+onig_get_parse_depth_limit(void)
+{
+ return ParseDepthLimit;
+}
+
+extern int
+onig_set_parse_depth_limit(unsigned int depth)
+{
+ if (depth == 0)
+ ParseDepthLimit = DEFAULT_PARSE_DEPTH_LIMIT;
+ else
+ ParseDepthLimit = depth;
+ return 0;
+}
+
+
static void
bbuf_free(BBuf* bbuf)
{
@@ -216,6 +240,7 @@ bitset_copy(BitSetRef dest, BitSetRef bs)
for (i = 0; i < BITSET_SIZE; i++) { dest[i] = bs[i]; }
}
+#if defined(USE_NAMED_GROUP) && !defined(USE_ST_LIBRARY)
extern int
onig_strncmp(const UChar* s1, const UChar* s2, int n)
{
@@ -227,6 +252,7 @@ onig_strncmp(const UChar* s1, const UChar* s2, int n)
}
return 0;
}
+#endif
extern void
onig_strcpy(UChar* dest, const UChar* src, const UChar* end)
@@ -265,9 +291,9 @@ strdup_with_null(OnigEncoding enc, UChar* s, UChar* end)
#ifdef __GNUC__
/* get rid of Wunused-but-set-variable and Wuninitialized */
-#define PFETCH_READY UChar* pfetch_prev = NULL; (void)pfetch_prev
+# define PFETCH_READY UChar* pfetch_prev = NULL; (void)pfetch_prev
#else
-#define PFETCH_READY UChar* pfetch_prev
+# define PFETCH_READY UChar* pfetch_prev
#endif
#define PEND (p < end ? 0 : 1)
#define PUNFETCH p = pfetch_prev
@@ -325,7 +351,11 @@ strcat_capa_from_static(UChar* dest, UChar* dest_end,
#ifdef USE_ST_LIBRARY
-#include "ruby/st.h"
+# ifdef RUBY
+# include "ruby/st.h"
+# else
+# include "st.h"
+# endif
typedef struct {
const UChar* s;
@@ -417,7 +447,7 @@ onig_st_insert_strend(hash_table_type* table, const UChar* str_key,
#ifdef USE_NAMED_GROUP
-#define INIT_NAME_BACKREFS_ALLOC_NUM 8
+# define INIT_NAME_BACKREFS_ALLOC_NUM 8
typedef struct {
UChar* name;
@@ -428,12 +458,12 @@ typedef struct {
int* back_refs;
} NameEntry;
-#ifdef USE_ST_LIBRARY
+# ifdef USE_ST_LIBRARY
typedef st_table NameTable;
typedef st_data_t HashDataType; /* 1.6 st.h doesn't define st_data_t type */
-#ifdef ONIG_DEBUG
+# ifdef ONIG_DEBUG
static int
i_print_name_entry(UChar* key, NameEntry* e, void* arg)
{
@@ -467,7 +497,7 @@ onig_print_names(FILE* fp, regex_t* reg)
}
return 0;
}
-#endif /* ONIG_DEBUG */
+# endif /* ONIG_DEBUG */
static int
i_free_name_entry(UChar* key, NameEntry* e, void* arg ARG_UNUSED)
@@ -530,8 +560,8 @@ static int
i_names(UChar* key ARG_UNUSED, NameEntry* e, INamesArg* arg)
{
int r = (*(arg->func))(e->name,
- e->name + e->name_len,
- e->back_num,
+ e->name + e->name_len,
+ e->back_num,
(e->back_num > 1 ? e->back_refs : &(e->back_ref1)),
arg->reg, arg->arg);
if (r != 0) {
@@ -589,7 +619,7 @@ onig_renumber_name_table(regex_t* reg, GroupNumRemap* map)
extern int
-onig_number_of_names(regex_t* reg)
+onig_number_of_names(const regex_t* reg)
{
NameTable* t = (NameTable* )reg->name_table;
@@ -599,9 +629,9 @@ onig_number_of_names(regex_t* reg)
return 0;
}
-#else /* USE_ST_LIBRARY */
+# else /* USE_ST_LIBRARY */
-#define INIT_NAMES_ALLOC_NUM 8
+# define INIT_NAMES_ALLOC_NUM 8
typedef struct {
NameEntry* e;
@@ -609,7 +639,7 @@ typedef struct {
int alloc;
} NameTable;
-#ifdef ONIG_DEBUG
+# ifdef ONIG_DEBUG
extern int
onig_print_names(FILE* fp, regex_t* reg)
{
@@ -640,7 +670,7 @@ onig_print_names(FILE* fp, regex_t* reg)
}
return 0;
}
-#endif
+# endif
static int
names_clear(regex_t* reg)
@@ -725,7 +755,7 @@ onig_foreach_name(regex_t* reg,
}
extern int
-onig_number_of_names(regex_t* reg)
+onig_number_of_names(const regex_t* reg)
{
NameTable* t = (NameTable* )reg->name_table;
@@ -735,7 +765,7 @@ onig_number_of_names(regex_t* reg)
return 0;
}
-#endif /* else USE_ST_LIBRARY */
+# endif /* else USE_ST_LIBRARY */
static int
name_add(regex_t* reg, UChar* name, UChar* name_end, int backref, ScanEnv* env)
@@ -749,7 +779,7 @@ name_add(regex_t* reg, UChar* name, UChar* name_end, int backref, ScanEnv* env)
e = name_find(reg, name, name_end);
if (IS_NULL(e)) {
-#ifdef USE_ST_LIBRARY
+# ifdef USE_ST_LIBRARY
if (IS_NULL(t)) {
t = onig_st_init_strend_table_with_size(5);
reg->name_table = (void* )t;
@@ -770,7 +800,7 @@ name_add(regex_t* reg, UChar* name, UChar* name_end, int backref, ScanEnv* env)
e->back_alloc = 0;
e->back_refs = (int* )NULL;
-#else
+# else
if (IS_NULL(t)) {
alloc = INIT_NAMES_ALLOC_NUM;
@@ -813,7 +843,7 @@ name_add(regex_t* reg, UChar* name, UChar* name_end, int backref, ScanEnv* env)
e->name = strdup_with_null(reg->enc, name, name_end);
if (IS_NULL(e->name)) return ONIGERR_MEMORY;
e->name_len = name_end - name;
-#endif
+# endif
}
if (e->back_num >= 1 &&
@@ -876,7 +906,7 @@ onig_name_to_group_numbers(regex_t* reg, const UChar* name,
extern int
onig_name_to_backref_number(regex_t* reg, const UChar* name,
- const UChar* name_end, OnigRegion *region)
+ const UChar* name_end, const OnigRegion *region)
{
int i, n, *nums;
@@ -909,7 +939,7 @@ onig_name_to_group_numbers(regex_t* reg, const UChar* name,
extern int
onig_name_to_backref_number(regex_t* reg, const UChar* name,
- const UChar* name_end, OnigRegion* region)
+ const UChar* name_end, const OnigRegion* region)
{
return ONIG_NO_SUPPORT_CONFIG;
}
@@ -922,14 +952,14 @@ onig_foreach_name(regex_t* reg,
}
extern int
-onig_number_of_names(regex_t* reg)
+onig_number_of_names(const regex_t* reg)
{
return 0;
}
#endif /* else USE_NAMED_GROUP */
extern int
-onig_noname_group_capture_is_active(regex_t* reg)
+onig_noname_group_capture_is_active(const regex_t* reg)
{
if (ONIG_IS_OPTION_ON(reg->options, ONIG_OPTION_DONT_CAPTURE_GROUP))
return 0;
@@ -976,6 +1006,7 @@ scan_env_clear(ScanEnv* env)
env->curr_max_regnum = 0;
env->has_recursion = 0;
#endif
+ env->parse_depth = 0;
env->warnings_flag = 0;
}
@@ -1025,14 +1056,6 @@ scan_env_set_mem_node(ScanEnv* env, int num, Node* node)
}
-#ifdef USE_PARSE_TREE_NODE_RECYCLE
-typedef struct _FreeNode {
- struct _FreeNode* next;
-} FreeNode;
-
-static FreeNode* FreeNodeList = (FreeNode* )NULL;
-#endif
-
extern void
onig_node_free(Node* node)
{
@@ -1053,18 +1076,7 @@ onig_node_free(Node* node)
{
Node* next_node = NCDR(node);
-#ifdef USE_PARSE_TREE_NODE_RECYCLE
- {
- FreeNode* n = (FreeNode* )node;
-
- THREAD_ATOMIC_START;
- n->next = FreeNodeList;
- FreeNodeList = n;
- THREAD_ATOMIC_END;
- }
-#else
xfree(node);
-#endif
node = next_node;
goto start;
}
@@ -1074,9 +1086,8 @@ onig_node_free(Node* node)
{
CClassNode* cc = NCCLASS(node);
- if (IS_NCCLASS_SHARE(cc)) return ;
if (cc->mbuf)
- bbuf_free(cc->mbuf);
+ bbuf_free(cc->mbuf);
}
break;
@@ -1101,77 +1112,18 @@ onig_node_free(Node* node)
break;
}
-#ifdef USE_PARSE_TREE_NODE_RECYCLE
- {
- FreeNode* n = (FreeNode* )node;
-
- THREAD_ATOMIC_START;
- n->next = FreeNodeList;
- FreeNodeList = n;
- THREAD_ATOMIC_END;
- }
-#else
xfree(node);
-#endif
}
-#ifdef USE_PARSE_TREE_NODE_RECYCLE
-extern int
-onig_free_node_list(void)
-{
- FreeNode* n;
-
- /* THREAD_ATOMIC_START; */
- while (IS_NOT_NULL(FreeNodeList)) {
- n = FreeNodeList;
- FreeNodeList = FreeNodeList->next;
- xfree(n);
- }
- /* THREAD_ATOMIC_END; */
- return 0;
-}
-#endif
-
static Node*
node_new(void)
{
Node* node;
-#ifdef USE_PARSE_TREE_NODE_RECYCLE
- THREAD_ATOMIC_START;
- if (IS_NOT_NULL(FreeNodeList)) {
- node = (Node* )FreeNodeList;
- FreeNodeList = FreeNodeList->next;
- THREAD_ATOMIC_END;
- return node;
- }
- THREAD_ATOMIC_END;
-#endif
-
- node = (Node* )xmalloc(sizeof(Node));
- /* xmemset(node, 0, sizeof(Node)); */
- return node;
-}
-
-#if defined(USE_MULTI_THREAD_SYSTEM) && \
- defined(USE_SHARED_CCLASS_TABLE) && \
- defined(USE_PARSE_TREE_NODE_RECYCLE)
-static Node*
-node_new_locked(void)
-{
- Node* node;
-
- if (IS_NOT_NULL(FreeNodeList)) {
- node = (Node* )FreeNodeList;
- FreeNodeList = FreeNodeList->next;
- return node;
- }
-
node = (Node* )xmalloc(sizeof(Node));
/* xmemset(node, 0, sizeof(Node)); */
return node;
}
-#endif
static void
initialize_cclass(CClassNode* cc)
@@ -1193,75 +1145,6 @@ node_new_cclass(void)
return node;
}
-#if defined(USE_MULTI_THREAD_SYSTEM) && \
- defined(USE_SHARED_CCLASS_TABLE) && \
- defined(USE_PARSE_TREE_NODE_RECYCLE)
-static Node*
-node_new_cclass_locked(void)
-{
- Node* node = node_new_locked();
- CHECK_NULL_RETURN(node);
-
- SET_NTYPE(node, NT_CCLASS);
- initialize_cclass(NCCLASS(node));
- return node;
-}
-#else
-#define node_new_cclass_locked() node_new_cclass()
-#endif
-
-#ifdef USE_SHARED_CCLASS_TABLE
-static Node*
-node_new_cclass_by_codepoint_range(int not, OnigCodePoint sb_out,
- const OnigCodePoint ranges[])
-{
- int n, i;
- CClassNode* cc;
- OnigCodePoint j;
-
- Node* node = node_new_cclass_locked();
- CHECK_NULL_RETURN(node);
-
- cc = NCCLASS(node);
- if (not != 0) NCCLASS_SET_NOT(cc);
-
- BITSET_CLEAR(cc->bs);
- if (sb_out > 0 && IS_NOT_NULL(ranges)) {
- n = ONIGENC_CODE_RANGE_NUM(ranges);
- for (i = 0; i < n; i++) {
- for (j = ONIGENC_CODE_RANGE_FROM(ranges, i);
- j <= (OnigCodePoint )ONIGENC_CODE_RANGE_TO(ranges, i); j++) {
- if (j >= sb_out) goto sb_end;
-
- BITSET_SET_BIT(cc->bs, j);
- }
- }
- }
-
- sb_end:
- if (IS_NULL(ranges)) {
- is_null:
- cc->mbuf = NULL;
- }
- else {
- BBuf* bbuf;
-
- n = ONIGENC_CODE_RANGE_NUM(ranges);
- if (n == 0) goto is_null;
-
- bbuf = (BBuf* )xmalloc(sizeof(BBuf));
- CHECK_NULL_RETURN(bbuf);
- bbuf->alloc = n + 1;
- bbuf->used = n + 1;
- bbuf->p = (UChar* )((void* )ranges);
-
- cc->mbuf = bbuf;
- }
-
- return node;
-}
-#endif /* USE_SHARED_CCLASS_TABLE */
-
static Node*
node_new_ctype(int type, int not, int ascii_range)
{
@@ -1548,6 +1431,7 @@ node_str_cat_codepoint(Node* node, OnigEncoding enc, OnigCodePoint c)
return onig_node_str_cat(node, buf, buf + num);
}
+#if 0
extern void
onig_node_conv_to_str_node(Node* node, int flag)
{
@@ -1557,6 +1441,7 @@ onig_node_conv_to_str_node(Node* node, int flag)
NSTR(node)->s = NSTR(node)->buf;
NSTR(node)->end = NSTR(node)->buf;
}
+#endif
extern void
onig_node_str_clear(Node* node)
@@ -1715,6 +1600,7 @@ scan_unsigned_hexadecimal_number(UChar** src, UChar* end, int minlen,
}
else {
PUNFETCH;
+ maxlen++;
break;
}
}
@@ -1886,7 +1772,7 @@ add_code_range0(BBuf** pbuf, ScanEnv* env, OnigCodePoint from, OnigCodePoint to,
static int
add_code_range(BBuf** pbuf, ScanEnv* env, OnigCodePoint from, OnigCodePoint to)
{
- return add_code_range0(pbuf, env, from, to, 1);
+ return add_code_range0(pbuf, env, from, to, 1);
}
static int
@@ -1990,7 +1876,7 @@ or_code_range_buf(OnigEncoding enc, BBuf* bbuf1, int not1,
static int
and_code_range1(BBuf** pbuf, ScanEnv* env, OnigCodePoint from1, OnigCodePoint to1,
- OnigCodePoint* data, int n)
+ OnigCodePoint* data, int n)
{
int i, r;
OnigCodePoint from2, to2;
@@ -2204,8 +2090,8 @@ or_cclass(CClassNode* dest, CClassNode* cc, ScanEnv* env)
static void UNKNOWN_ESC_WARN(ScanEnv *env, int c);
-static int
-conv_backslash_value(int c, ScanEnv* env)
+static OnigCodePoint
+conv_backslash_value(OnigCodePoint c, ScanEnv* env)
{
if (IS_SYNTAX_OP(env->syntax, ONIG_SYN_OP_ESC_CONTROL_CHARS)) {
switch (c) {
@@ -2231,7 +2117,7 @@ conv_backslash_value(int c, ScanEnv* env)
}
#ifdef USE_NO_INVALID_QUANTIFIER
-#define is_invalid_quantifier_target(node) 0
+# define is_invalid_quantifier_target(node) 0
#else
static int
is_invalid_quantifier_target(Node* node)
@@ -2303,6 +2189,7 @@ enum ReduceType {
};
static enum ReduceType const ReduceTypeTable[6][6] = {
+/* '?', '*', '+', '??', '*?', '+?' p / c */
{RQ_DEL, RQ_A, RQ_A, RQ_QQ, RQ_AQ, RQ_ASIS}, /* '?' */
{RQ_DEL, RQ_DEL, RQ_DEL, RQ_P_QQ, RQ_P_QQ, RQ_DEL}, /* '*' */
{RQ_A, RQ_A, RQ_DEL, RQ_ASIS, RQ_P_QQ, RQ_DEL}, /* '+' */
@@ -2505,6 +2392,7 @@ fetch_range_quantifier(UChar** src, UChar* end, OnigToken* tok, ScanEnv* env)
PFETCH(c);
if (IS_SYNTAX_OP(env->syntax, ONIG_SYN_OP_ESC_BRACE_INTERVAL)) {
if (c != MC_ESC(env->syntax)) goto invalid;
+ if (PEND) goto invalid;
PFETCH(c);
}
if (c != '}') goto invalid;
@@ -2528,7 +2416,7 @@ fetch_range_quantifier(UChar** src, UChar* end, OnigToken* tok, ScanEnv* env)
/* \M-, \C-, \c, or \... */
static int
-fetch_escaped_value(UChar** src, UChar* end, ScanEnv* env)
+fetch_escaped_value(UChar** src, UChar* end, ScanEnv* env, OnigCodePoint* val)
{
int v;
OnigCodePoint c;
@@ -2547,9 +2435,8 @@ fetch_escaped_value(UChar** src, UChar* end, ScanEnv* env)
if (PEND) return ONIGERR_END_PATTERN_AT_META;
PFETCH_S(c);
if (c == MC_ESC(env->syntax)) {
- v = fetch_escaped_value(&p, end, env);
- if (v < 0) return v;
- c = (OnigCodePoint )v;
+ v = fetch_escaped_value(&p, end, env, &c);
+ if (v < 0) return v;
}
c = ((c & 0xff) | 0x80);
}
@@ -2573,15 +2460,14 @@ fetch_escaped_value(UChar** src, UChar* end, ScanEnv* env)
if (PEND) return ONIGERR_END_PATTERN_AT_CONTROL;
PFETCH_S(c);
if (c == '?') {
- c = 0177;
+ c = 0177;
}
else {
- if (c == MC_ESC(env->syntax)) {
- v = fetch_escaped_value(&p, end, env);
- if (v < 0) return v;
- c = (OnigCodePoint )v;
- }
- c &= 0x9f;
+ if (c == MC_ESC(env->syntax)) {
+ v = fetch_escaped_value(&p, end, env, &c);
+ if (v < 0) return v;
+ }
+ c &= 0x9f;
}
break;
}
@@ -2596,7 +2482,8 @@ fetch_escaped_value(UChar** src, UChar* end, ScanEnv* env)
}
*src = p;
- return c;
+ *val = c;
+ return 0;
}
static int fetch_token(OnigToken* tok, UChar** src, UChar* end, ScanEnv* env);
@@ -2617,8 +2504,13 @@ get_name_end_code_point(OnigCodePoint start)
}
#ifdef USE_NAMED_GROUP
-#define ONIGENC_IS_CODE_NAME(enc, c) TRUE
-#ifdef USE_BACKREF_WITH_LEVEL
+# ifdef RUBY
+# define ONIGENC_IS_CODE_NAME(enc, c) TRUE
+# else
+# define ONIGENC_IS_CODE_NAME(enc, c) ONIGENC_IS_CODE_WORD(enc, c)
+# endif
+
+# ifdef USE_BACKREF_WITH_LEVEL
/*
\k<name+n>, \k<name-n>
\k<num+n>, \k<num-n>
@@ -2678,11 +2570,11 @@ fetch_name_with_level(OnigCodePoint start_code, UChar** src, UChar* end,
if (is_num != 0) {
if (ONIGENC_IS_CODE_DIGIT(enc, c)) {
- is_num = 1;
+ is_num = 1;
}
else {
- r = ONIGERR_INVALID_GROUP_NAME;
- is_num = 0;
+ r = ONIGERR_INVALID_GROUP_NAME;
+ is_num = 0;
}
}
else if (!ONIGENC_IS_CODE_NAME(enc, c)) {
@@ -2695,6 +2587,10 @@ fetch_name_with_level(OnigCodePoint start_code, UChar** src, UChar* end,
int level;
int flag = (c == '-' ? -1 : 1);
+ if (PEND) {
+ r = ONIGERR_INVALID_CHAR_IN_GROUP_NAME;
+ goto end;
+ }
PFETCH(c);
if (! ONIGENC_IS_CODE_DIGIT(enc, c)) goto err;
PUNFETCH;
@@ -2703,9 +2599,11 @@ fetch_name_with_level(OnigCodePoint start_code, UChar** src, UChar* end,
*rlevel = (level * flag);
exist_level = 1;
- PFETCH(c);
- if (c == end_code)
- goto end;
+ if (!PEND) {
+ PFETCH(c);
+ if (c == end_code)
+ goto end;
+ }
}
err:
@@ -2732,7 +2630,7 @@ fetch_name_with_level(OnigCodePoint start_code, UChar** src, UChar* end,
return r;
}
}
-#endif /* USE_BACKREF_WITH_LEVEL */
+# endif /* USE_BACKREF_WITH_LEVEL */
/*
ref: 0 -> define name (don't allow number name)
@@ -2769,17 +2667,17 @@ fetch_name(OnigCodePoint start_code, UChar** src, UChar* end,
if (ONIGENC_IS_CODE_DIGIT(enc, c)) {
if (ref == 1)
- is_num = 1;
+ is_num = 1;
else {
- r = ONIGERR_INVALID_GROUP_NAME;
- is_num = 0;
+ r = ONIGERR_INVALID_GROUP_NAME;
+ is_num = 0;
}
}
else if (c == '-') {
if (ref == 1) {
- is_num = 2;
- sign = -1;
- pnum_head = p;
+ is_num = 2;
+ sign = -1;
+ pnum_head = p;
}
else {
r = ONIGERR_INVALID_GROUP_NAME;
@@ -2796,30 +2694,30 @@ fetch_name(OnigCodePoint start_code, UChar** src, UChar* end,
name_end = p;
PFETCH_S(c);
if (c == end_code || c == ')') {
- if (is_num == 2) {
- r = ONIGERR_INVALID_GROUP_NAME;
- goto teardown;
- }
- break;
+ if (is_num == 2) {
+ r = ONIGERR_INVALID_GROUP_NAME;
+ goto teardown;
+ }
+ break;
}
if (is_num != 0) {
- if (ONIGENC_IS_CODE_DIGIT(enc, c)) {
- is_num = 1;
- }
- else {
- if (!ONIGENC_IS_CODE_WORD(enc, c))
- r = ONIGERR_INVALID_CHAR_IN_GROUP_NAME;
- else
- r = ONIGERR_INVALID_GROUP_NAME;
- goto teardown;
- }
+ if (ONIGENC_IS_CODE_DIGIT(enc, c)) {
+ is_num = 1;
+ }
+ else {
+ if (!ONIGENC_IS_CODE_WORD(enc, c))
+ r = ONIGERR_INVALID_CHAR_IN_GROUP_NAME;
+ else
+ r = ONIGERR_INVALID_GROUP_NAME;
+ goto teardown;
+ }
}
else {
- if (!ONIGENC_IS_CODE_NAME(enc, c)) {
- r = ONIGERR_INVALID_CHAR_IN_GROUP_NAME;
- goto teardown;
- }
+ if (!ONIGENC_IS_CODE_NAME(enc, c)) {
+ r = ONIGERR_INVALID_CHAR_IN_GROUP_NAME;
+ goto teardown;
+ }
}
}
@@ -2833,8 +2731,8 @@ fetch_name(OnigCodePoint start_code, UChar** src, UChar* end,
*rback_num = onig_scan_unsigned_number(&pnum_head, name_end, enc);
if (*rback_num < 0) return ONIGERR_TOO_BIG_NUMBER;
else if (*rback_num == 0) {
- r = ONIGERR_INVALID_GROUP_NAME;
- goto err;
+ r = ONIGERR_INVALID_GROUP_NAME;
+ goto err;
}
*rback_num *= sign;
@@ -2845,12 +2743,12 @@ fetch_name(OnigCodePoint start_code, UChar** src, UChar* end,
return 0;
}
else {
- teardown:
+teardown:
while (!PEND) {
name_end = p;
PFETCH_S(c);
if (c == end_code || c == ')')
- break;
+ break;
}
if (PEND)
name_end = end;
@@ -2939,8 +2837,6 @@ fetch_name(OnigCodePoint start_code, UChar** src, UChar* end,
}
#endif /* USE_NAMED_GROUP */
-void onig_vsnprintf_with_pattern(UChar buf[], int bufsize, OnigEncoding enc,
- UChar* pat, UChar* pat_end, const UChar *fmt, va_list args);
static void
onig_syntax_warn(ScanEnv *env, const char *fmt, ...)
@@ -2952,10 +2848,14 @@ onig_syntax_warn(ScanEnv *env, const char *fmt, ...)
env->pattern, env->pattern_end,
(const UChar *)fmt, args);
va_end(args);
+#ifdef RUBY
if (env->sourcefile == NULL)
rb_warn("%s", (char *)buf);
else
rb_compile_warn(env->sourcefile, env->sourceline, "%s", (char *)buf);
+#else
+ (*onig_warn)((char* )buf);
+#endif
}
static void
@@ -2979,6 +2879,10 @@ CLOSE_BRACKET_WITHOUT_ESC_WARN(ScanEnv* env, UChar* c)
}
}
+#ifndef RTEST
+# define RTEST(v) 1
+#endif
+
static void
CC_DUP_WARN(ScanEnv *env)
{
@@ -3148,6 +3052,8 @@ fetch_token_in_cc(OnigToken* tok, UChar** src, UChar* end, ScanEnv* env)
case 'p':
case 'P':
+ if (PEND) break;
+
c2 = PPEEK;
if (c2 == '{' &&
IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_P_BRACE_CHAR_PROPERTY)) {
@@ -3155,7 +3061,7 @@ fetch_token_in_cc(OnigToken* tok, UChar** src, UChar* end, ScanEnv* env)
tok->type = TK_CHAR_PROPERTY;
tok->u.prop.not = (c == 'P' ? 1 : 0);
- if (IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_P_BRACE_CIRCUMFLEX_NOT)) {
+ if (!PEND && IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_P_BRACE_CIRCUMFLEX_NOT)) {
PFETCH(c2);
if (c2 == '^') {
tok->u.prop.not = (tok->u.prop.not == 0 ? 1 : 0);
@@ -3178,10 +3084,10 @@ fetch_token_in_cc(OnigToken* tok, UChar** src, UChar* end, ScanEnv* env)
num = scan_unsigned_hexadecimal_number(&p, end, 0, 8, enc);
if (num < 0) return ONIGERR_TOO_BIG_WIDE_CHAR_VALUE;
if (!PEND) {
- c2 = PPEEK;
- if (ONIGENC_IS_CODE_XDIGIT(enc, c2))
- return ONIGERR_TOO_LONG_WIDE_CHAR_VALUE;
- }
+ c2 = PPEEK;
+ if (ONIGENC_IS_CODE_XDIGIT(enc, c2))
+ return ONIGERR_TOO_LONG_WIDE_CHAR_VALUE;
+ }
if (p > prev + enclen(enc, prev, end) && !PEND && (PPEEK_IS('}'))) {
PINC;
@@ -3223,6 +3129,33 @@ fetch_token_in_cc(OnigToken* tok, UChar** src, UChar* end, ScanEnv* env)
}
break;
+ case 'o':
+ if (PEND) break;
+
+ prev = p;
+ if (PPEEK_IS('{') && IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_O_BRACE_OCTAL)) {
+ PINC;
+ num = scan_unsigned_octal_number(&p, end, 11, enc);
+ if (num < 0) return ONIGERR_TOO_BIG_WIDE_CHAR_VALUE;
+ if (!PEND) {
+ c2 = PPEEK;
+ if (ONIGENC_IS_CODE_DIGIT(enc, c2) && c2 < '8')
+ return ONIGERR_TOO_LONG_WIDE_CHAR_VALUE;
+ }
+
+ if (p > prev + enclen(enc, prev, end) && !PEND && (PPEEK_IS('}'))) {
+ PINC;
+ tok->type = TK_CODE_POINT;
+ tok->base = 8;
+ tok->u.code = (OnigCodePoint )num;
+ }
+ else {
+ /* can't read nothing or invalid format */
+ p = prev;
+ }
+ }
+ break;
+
case '0':
case '1': case '2': case '3': case '4': case '5': case '6': case '7':
if (IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_OCTAL3)) {
@@ -3241,10 +3174,10 @@ fetch_token_in_cc(OnigToken* tok, UChar** src, UChar* end, ScanEnv* env)
default:
PUNFETCH;
- num = fetch_escaped_value(&p, end, env);
+ num = fetch_escaped_value(&p, end, env, &c2);
if (num < 0) return num;
- if (tok->u.c != num) {
- tok->u.code = (OnigCodePoint )num;
+ if ((OnigCodePoint)tok->u.c != c2) {
+ tok->u.code = (OnigCodePoint )c2;
tok->type = TK_CODE_POINT;
}
break;
@@ -3302,15 +3235,15 @@ fetch_named_backref_token(OnigCodePoint c, OnigToken* tok, UChar** src,
prev = p;
-#ifdef USE_BACKREF_WITH_LEVEL
+# ifdef USE_BACKREF_WITH_LEVEL
name_end = NULL_UCHARP; /* no need. escape gcc warning. */
r = fetch_name_with_level(c, &p, end, &name_end,
env, &back_num, &tok->u.backref.level);
if (r == 1) tok->u.backref.exist_level = 1;
else tok->u.backref.exist_level = 0;
-#else
+# else
r = fetch_name(&p, end, &name_end, env, &back_num, 1);
-#endif
+# endif
if (r < 0) return r;
if (back_num != 0) {
@@ -3348,7 +3281,7 @@ fetch_named_backref_token(OnigCodePoint c, OnigToken* tok, UChar** src,
tok->type = TK_BACKREF;
tok->u.backref.by_name = 1;
- if (num == 1) {
+ if (num == 1 || IS_SYNTAX_BV(syn, ONIG_SYN_USE_LEFT_MOST_NAMED_GROUP)) {
tok->u.backref.num = 1;
tok->u.backref.ref1 = backs[0];
}
@@ -3601,9 +3534,9 @@ fetch_token(OnigToken* tok, UChar** src, UChar* end, ScanEnv* env)
num = scan_unsigned_hexadecimal_number(&p, end, 0, 8, enc);
if (num < 0) return ONIGERR_TOO_BIG_WIDE_CHAR_VALUE;
if (!PEND) {
- if (ONIGENC_IS_CODE_XDIGIT(enc, PPEEK))
- return ONIGERR_TOO_LONG_WIDE_CHAR_VALUE;
- }
+ if (ONIGENC_IS_CODE_XDIGIT(enc, PPEEK))
+ return ONIGERR_TOO_LONG_WIDE_CHAR_VALUE;
+ }
if ((p > prev + enclen(enc, prev, end)) && !PEND && PPEEK_IS('}')) {
PINC;
@@ -3644,13 +3577,39 @@ fetch_token(OnigToken* tok, UChar** src, UChar* end, ScanEnv* env)
}
break;
+ case 'o':
+ if (PEND) break;
+
+ prev = p;
+ if (PPEEK_IS('{') && IS_SYNTAX_OP(syn, ONIG_SYN_OP_ESC_O_BRACE_OCTAL)) {
+ PINC;
+ num = scan_unsigned_octal_number(&p, end, 11, enc);
+ if (num < 0) return ONIGERR_TOO_BIG_WIDE_CHAR_VALUE;
+ if (!PEND) {
+ OnigCodePoint c = PPEEK;
+ if (ONIGENC_IS_CODE_DIGIT(enc, c) && c < '8')
+ return ONIGERR_TOO_LONG_WIDE_CHAR_VALUE;
+ }
+
+ if ((p > prev + enclen(enc, prev, end)) && !PEND && PPEEK_IS('}')) {
+ PINC;
+ tok->type = TK_CODE_POINT;
+ tok->u.code = (OnigCodePoint )num;
+ }
+ else {
+ /* can't read nothing or invalid format */
+ p = prev;
+ }
+ }
+ break;
+
case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
PUNFETCH;
prev = p;
num = onig_scan_unsigned_number(&p, end, enc);
if (num < 0 || num > ONIG_MAX_BACKREF_NUM) {
- goto skip_backref;
+ goto skip_backref;
}
if (IS_SYNTAX_OP(syn, ONIG_SYN_OP_DECIMAL_BACKREF) &&
@@ -3698,7 +3657,7 @@ fetch_token(OnigToken* tok, UChar** src, UChar* end, ScanEnv* env)
#ifdef USE_NAMED_GROUP
case 'k':
- if (IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_K_NAMED_BACKREF)) {
+ if (!PEND && IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_K_NAMED_BACKREF)) {
PFETCH(c);
if (c == '<' || c == '\'') {
r = fetch_named_backref_token(c, tok, &p, end, env);
@@ -3714,8 +3673,8 @@ fetch_token(OnigToken* tok, UChar** src, UChar* end, ScanEnv* env)
#if defined(USE_SUBEXP_CALL) || defined(USE_NAMED_GROUP)
case 'g':
-#ifdef USE_NAMED_GROUP
- if (IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_G_BRACE_BACKREF)) {
+# ifdef USE_NAMED_GROUP
+ if (!PEND && IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_G_BRACE_BACKREF)) {
PFETCH(c);
if (c == '{') {
r = fetch_named_backref_token(c, tok, &p, end, env);
@@ -3724,9 +3683,9 @@ fetch_token(OnigToken* tok, UChar** src, UChar* end, ScanEnv* env)
else
PUNFETCH;
}
-#endif
-#ifdef USE_SUBEXP_CALL
- if (IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_G_SUBEXP_CALL)) {
+# endif
+# ifdef USE_SUBEXP_CALL
+ if (!PEND && IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_G_SUBEXP_CALL)) {
PFETCH(c);
if (c == '<' || c == '\'') {
int gnum = -1, rel = 0;
@@ -3763,7 +3722,7 @@ fetch_token(OnigToken* tok, UChar** src, UChar* end, ScanEnv* env)
PUNFETCH;
}
}
-#endif
+# endif
break;
#endif
@@ -3781,7 +3740,7 @@ fetch_token(OnigToken* tok, UChar** src, UChar* end, ScanEnv* env)
tok->type = TK_CHAR_PROPERTY;
tok->u.prop.not = (c == 'P' ? 1 : 0);
- if (IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_P_BRACE_CIRCUMFLEX_NOT)) {
+ if (!PEND && IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_ESC_P_BRACE_CIRCUMFLEX_NOT)) {
PFETCH(c);
if (c == '^') {
tok->u.prop.not = (tok->u.prop.not == 0 ? 1 : 0);
@@ -3814,16 +3773,20 @@ fetch_token(OnigToken* tok, UChar** src, UChar* end, ScanEnv* env)
break;
default:
- PUNFETCH;
- num = fetch_escaped_value(&p, end, env);
- if (num < 0) return num;
- /* set_raw: */
- if (tok->u.c != num) {
- tok->type = TK_CODE_POINT;
- tok->u.code = (OnigCodePoint )num;
- }
- else { /* string */
- p = tok->backp + enclen(enc, tok->backp, end);
+ {
+ OnigCodePoint c2;
+
+ PUNFETCH;
+ num = fetch_escaped_value(&p, end, env, &c2);
+ if (num < 0) return num;
+ /* set_raw: */
+ if ((OnigCodePoint)tok->u.c != c2) {
+ tok->type = TK_CODE_POINT;
+ tok->u.code = (OnigCodePoint )c2;
+ }
+ else { /* string */
+ p = tok->backp + enclen(enc, tok->backp, end);
+ }
}
break;
}
@@ -3913,22 +3876,22 @@ fetch_token(OnigToken* tok, UChar** src, UChar* end, ScanEnv* env)
case '(':
if (PPEEK_IS('?') &&
- IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_QMARK_GROUP_EFFECT)) {
- PINC;
- if (PPEEK_IS('#')) {
- PFETCH(c);
- while (1) {
- if (PEND) return ONIGERR_END_PATTERN_IN_GROUP;
- PFETCH(c);
- if (c == MC_ESC(syn)) {
- if (!PEND) PFETCH(c);
- }
- else {
- if (c == ')') break;
- }
- }
- goto start;
- }
+ IS_SYNTAX_OP2(syn, ONIG_SYN_OP2_QMARK_GROUP_EFFECT)) {
+ PINC;
+ if (PPEEK_IS('#')) {
+ PFETCH(c);
+ while (1) {
+ if (PEND) return ONIGERR_END_PATTERN_IN_GROUP;
+ PFETCH(c);
+ if (c == MC_ESC(syn)) {
+ if (!PEND) PFETCH(c);
+ }
+ else {
+ if (c == ')') break;
+ }
+ }
+ goto start;
+ }
#ifdef USE_PERL_SUBEXP_CALL
/* (?&name), (?n), (?R), (?0), (?+n), (?-n) */
c = PPEEK;
@@ -3999,6 +3962,7 @@ fetch_token(OnigToken* tok, UChar** src, UChar* end, ScanEnv* env)
PFETCH_READY;
PINC; /* skip 'P' */
+ if (PEND) return ONIGERR_UNDEFINED_GROUP_OPTION;
PFETCH(c);
if (c == '=') { /* (?P=name): backref */
r = fetch_named_backref_token((OnigCodePoint )'(', tok, &p, end, env);
@@ -4017,10 +3981,9 @@ fetch_token(OnigToken* tok, UChar** src, UChar* end, ScanEnv* env)
tok->u.call.rel = 0;
break;
}
- PUNFETCH;
}
#endif /* USE_CAPITAL_P_NAMED_GROUP */
- PUNFETCH;
+ PUNFETCH;
}
if (! IS_SYNTAX_OP(syn, ONIG_SYN_OP_LPAREN_SUBEXP)) break;
@@ -4098,8 +4061,8 @@ add_ctype_to_cc_by_range(CClassNode* cc, int ctype ARG_UNUSED, int not,
if (not == 0) {
for (i = 0; i < n; i++) {
- for (j = ONIGENC_CODE_RANGE_FROM(mbr, i);
- j <= ONIGENC_CODE_RANGE_TO(mbr, i); j++) {
+ for (j = ONIGENC_CODE_RANGE_FROM(mbr, i);
+ j <= ONIGENC_CODE_RANGE_TO(mbr, i); j++) {
if (j >= sb_out) {
if (j > ONIGENC_CODE_RANGE_FROM(mbr, i)) {
r = add_code_range_to_buf(&(cc->mbuf), env, j,
@@ -4110,7 +4073,7 @@ add_ctype_to_cc_by_range(CClassNode* cc, int ctype ARG_UNUSED, int not,
goto sb_end;
}
- BITSET_SET_BIT_CHKDUP(cc->bs, j);
+ BITSET_SET_BIT_CHKDUP(cc->bs, j);
}
}
@@ -4183,12 +4146,15 @@ add_ctype_to_cc(CClassNode* cc, int ctype, int not, int ascii_range, ScanEnv* en
CClassNode ccascii;
initialize_cclass(&ccascii);
if (ONIGENC_MBC_MINLEN(env->enc) > 1) {
- add_code_range(&(ccascii.mbuf), env, 0x00, 0x7F);
+ r = add_code_range(&(ccascii.mbuf), env, 0x00, 0x7F);
}
else {
bitset_set_range(env, ccascii.bs, 0x00, 0x7F);
+ r = 0;
+ }
+ if (r == 0) {
+ r = and_cclass(&ccwork, &ccascii, env);
}
- r = and_cclass(&ccwork, &ccascii, env);
if (IS_NOT_NULL(ccascii.mbuf)) bbuf_free(ccascii.mbuf);
}
if (r == 0) {
@@ -4244,7 +4210,7 @@ add_ctype_to_cc(CClassNode* cc, int ctype, int not, int ascii_range, ScanEnv* en
BITSET_SET_BIT_CHKDUP(cc->bs, c);
}
if (ascii_range)
- ADD_ALL_MULTI_BYTE_RANGE(enc, cc->mbuf);
+ ADD_ALL_MULTI_BYTE_RANGE(enc, cc->mbuf);
}
else {
for (c = 0; c < maxcode; c++) {
@@ -4252,7 +4218,7 @@ add_ctype_to_cc(CClassNode* cc, int ctype, int not, int ascii_range, ScanEnv* en
BITSET_SET_BIT_CHKDUP(cc->bs, c);
}
if (! ascii_range)
- ADD_ALL_MULTI_BYTE_RANGE(enc, cc->mbuf);
+ ADD_ALL_MULTI_BYTE_RANGE(enc, cc->mbuf);
}
break;
@@ -4262,16 +4228,16 @@ add_ctype_to_cc(CClassNode* cc, int ctype, int not, int ascii_range, ScanEnv* en
if (ONIGENC_IS_CODE_WORD(enc, c)) BITSET_SET_BIT_CHKDUP(cc->bs, c);
}
if (! ascii_range)
- ADD_ALL_MULTI_BYTE_RANGE(enc, cc->mbuf);
+ ADD_ALL_MULTI_BYTE_RANGE(enc, cc->mbuf);
}
else {
for (c = 0; c < SINGLE_BYTE_SIZE; c++) {
- if ((ONIGENC_CODE_TO_MBCLEN(enc, c) > 0) /* check invalid code point */
+ if ((ONIGENC_CODE_TO_MBCLEN(enc, c) > 0) /* check invalid code point */
&& (! ONIGENC_IS_CODE_WORD(enc, c) || c >= maxcode))
BITSET_SET_BIT_CHKDUP(cc->bs, c);
}
if (ascii_range)
- ADD_ALL_MULTI_BYTE_RANGE(enc, cc->mbuf);
+ ADD_ALL_MULTI_BYTE_RANGE(enc, cc->mbuf);
}
break;
@@ -4330,7 +4296,7 @@ parse_posix_bracket(CClassNode* cc, CClassNode* asc_cc,
if (onigenc_with_ascii_strncmp(enc, p, end, pb->name, pb->len) == 0) {
p = (UChar* )onigenc_step(enc, p, end, pb->len);
if (onigenc_with_ascii_strncmp(enc, p, end, (UChar* )":]", 2) != 0)
- return ONIGERR_INVALID_POSIX_BRACKET_TYPE;
+ return ONIGERR_INVALID_POSIX_BRACKET_TYPE;
r = add_ctype_to_cc(cc, pb->ctype, not, ascii_range, env);
if (r != 0) return r;
@@ -4361,7 +4327,7 @@ parse_posix_bracket(CClassNode* cc, CClassNode* asc_cc,
if (! PEND) {
PFETCH_S(c);
if (c == ']')
- return ONIGERR_INVALID_POSIX_BRACKET_TYPE;
+ return ONIGERR_INVALID_POSIX_BRACKET_TYPE;
}
}
@@ -4441,7 +4407,7 @@ enum CCVALTYPE {
static int
next_state_class(CClassNode* cc, CClassNode* asc_cc,
- OnigCodePoint* vs, enum CCVALTYPE* type,
+ OnigCodePoint* vs, enum CCVALTYPE* type,
enum CCSTATE* state, ScanEnv* env)
{
int r;
@@ -4499,8 +4465,8 @@ next_state_val(CClassNode* cc, CClassNode* asc_cc,
case CCS_RANGE:
if (intype == *type) {
if (intype == CCV_SB) {
- if (*vs > 0xff || v > 0xff)
- return ONIGERR_INVALID_CODE_POINT_VALUE;
+ if (*vs > 0xff || v > 0xff)
+ return ONIGERR_INVALID_CODE_POINT_VALUE;
if (*vs > v) {
if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_ALLOW_EMPTY_RANGE_IN_CC))
@@ -4604,6 +4570,9 @@ parse_char_class(Node** np, Node** asc_np, OnigToken* tok, UChar** src, UChar* e
enum CCVALTYPE val_type, in_type;
int val_israw, in_israw;
+ env->parse_depth++;
+ if (env->parse_depth > ParseDepthLimit)
+ return ONIGERR_PARSE_DEPTH_LIMIT_OVER;
prev_cc = asc_prev_cc = (CClassNode* )NULL;
*np = *asc_np = NULL_NODE;
r = fetch_token_in_cc(tok, src, end, env);
@@ -4687,7 +4656,7 @@ parse_char_class(Node** np, Node** asc_np, OnigToken* tok, UChar** src, UChar* e
goto err;
}
- len = enclen(env->enc, buf, buf+i);
+ len = enclen(env->enc, buf, buf + i);
if (i < len) {
r = ONIGERR_TOO_SHORT_MULTI_BYTE_STRING;
goto err;
@@ -4695,7 +4664,8 @@ parse_char_class(Node** np, Node** asc_np, OnigToken* tok, UChar** src, UChar* e
else if (i > len) { /* fetch back */
p = psave;
for (i = 1; i < len; i++) {
- r = fetch_token_in_cc(tok, &p, end, env);
+ (void)fetch_token_in_cc(tok, &p, end, env);
+ /* no need to check the retun value (already checked above) */
}
fetched = 0;
}
@@ -4948,16 +4918,17 @@ parse_char_class(Node** np, Node** asc_np, OnigToken* tok, UChar** src, UChar* e
#define NEWLINE_CODE 0x0a
if (ONIGENC_IS_CODE_NEWLINE(env->enc, NEWLINE_CODE)) {
- if (ONIGENC_CODE_TO_MBCLEN(env->enc, NEWLINE_CODE) == 1)
- BITSET_SET_BIT_CHKDUP(cc->bs, NEWLINE_CODE);
- else {
- r = add_code_range(&(cc->mbuf), env, NEWLINE_CODE, NEWLINE_CODE);
- if (r < 0) goto err;
- }
+ if (ONIGENC_CODE_TO_MBCLEN(env->enc, NEWLINE_CODE) == 1)
+ BITSET_SET_BIT_CHKDUP(cc->bs, NEWLINE_CODE);
+ else {
+ r = add_code_range(&(cc->mbuf), env, NEWLINE_CODE, NEWLINE_CODE);
+ if (r < 0) goto err;
+ }
}
}
}
*src = p;
+ env->parse_depth--;
return 0;
err:
@@ -5028,18 +4999,20 @@ parse_enclose(Node** np, OnigToken* tok, int term, UChar** src, UChar* end,
return ONIGERR_UNDEFINED_GROUP_OPTION;
break;
-#ifdef USE_CAPITAL_P_NAMED_GROUP
+# ifdef USE_CAPITAL_P_NAMED_GROUP
case 'P': /* (?P<name>...) */
- if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_QMARK_CAPITAL_P_NAMED_GROUP)) {
+ if (!PEND &&
+ IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_QMARK_CAPITAL_P_NAMED_GROUP)) {
PFETCH(c);
if (c == '<') goto named_group1;
}
return ONIGERR_UNDEFINED_GROUP_OPTION;
break;
-#endif
+# endif
#endif
case '<': /* look behind (?<=...), (?<!...) */
+ if (PEND) return ONIGERR_END_PATTERN_WITH_UNMATCHED_PARENTHESIS;
PFETCH(c);
if (c == '=')
*np = onig_node_new_anchor(ANCHOR_LOOK_BEHIND);
@@ -5090,7 +5063,8 @@ parse_enclose(Node** np, OnigToken* tok, int term, UChar** src, UChar* end,
case '@':
if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_ATMARK_CAPTURE_HISTORY)) {
#ifdef USE_NAMED_GROUP
- if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_QMARK_LT_NAMED_GROUP)) {
+ if (!PEND &&
+ IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_QMARK_LT_NAMED_GROUP)) {
PFETCH(c);
if (c == '<' || c == '\'') {
list_capture = 1;
@@ -5115,7 +5089,8 @@ parse_enclose(Node** np, OnigToken* tok, int term, UChar** src, UChar* end,
break;
case '(': /* conditional expression: (?(cond)yes), (?(cond)yes|no) */
- if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_QMARK_LPAREN_CONDITION)) {
+ if (!PEND &&
+ IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_QMARK_LPAREN_CONDITION)) {
UChar *name = NULL;
UChar *name_end;
PFETCH(c);
@@ -5133,36 +5108,29 @@ parse_enclose(Node** np, OnigToken* tok, int term, UChar** src, UChar* end,
#endif
if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_STRICT_CHECK_BACKREF)) {
if (num > env->num_mem ||
- IS_NULL(SCANENV_MEM_NODES(env)[num]))
+ IS_NULL(SCANENV_MEM_NODES(env)[num]))
return ONIGERR_INVALID_BACKREF;
}
}
#ifdef USE_NAMED_GROUP
else if (c == '<' || c == '\'') { /* (<name>), ('name') */
- int nums;
- int *backs;
-
name = p;
- r = fetch_name((OnigCodePoint )c, &p, end, &name_end, env, &num, 0);
+ r = fetch_named_backref_token(c, tok, &p, end, env);
if (r < 0) return r;
- PFETCH(c);
- if (c != ')') return ONIGERR_UNDEFINED_GROUP_OPTION;
+ if (!PPEEK_IS(')')) return ONIGERR_UNDEFINED_GROUP_OPTION;
+ PINC;
- nums = onig_name_to_group_numbers(env->reg, name, name_end, &backs);
- if (nums <= 0) {
- onig_scan_env_set_error_string(env,
- ONIGERR_UNDEFINED_NAME_REFERENCE, name, name_end);
- return ONIGERR_UNDEFINED_NAME_REFERENCE;
+ if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_USE_LEFT_MOST_NAMED_GROUP)) {
+ num = tok->u.backref.ref1;
}
- if (IS_SYNTAX_BV(env->syntax, ONIG_SYN_STRICT_CHECK_BACKREF)) {
- int i;
- for (i = 0; i < nums; i++) {
- if (backs[i] > env->num_mem ||
- IS_NULL(SCANENV_MEM_NODES(env)[backs[i]]))
- return ONIGERR_INVALID_BACKREF;
- }
+ else {
+ /* FIXME:
+ * Use left most named group for now. This is the same as Perl.
+ * However this should use the same strategy as normal back-
+ * references on Ruby syntax; search right to left. */
+ int len = tok->u.backref.num;
+ num = len > 1 ? tok->u.backref.refs[0] : tok->u.backref.ref1;
}
- num = backs[0]; /* XXX: use left most named group as Perl */
}
#endif
else
@@ -5187,7 +5155,7 @@ parse_enclose(Node** np, OnigToken* tok, int term, UChar** src, UChar* end,
#endif
case '^': /* loads default options */
- if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_OPTION_PERL)) {
+ if (!PEND && IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_OPTION_PERL)) {
/* d-imsx */
ONOFF(option, ONIG_OPTION_ASCII_RANGE, 1);
ONOFF(option, ONIG_OPTION_IGNORECASE, 1);
@@ -5197,7 +5165,7 @@ parse_enclose(Node** np, OnigToken* tok, int term, UChar** src, UChar* end,
PFETCH(c);
}
#if 0
- else if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_OPTION_RUBY)) {
+ else if (!PEND && IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_OPTION_RUBY)) {
/* d-imx */
ONOFF(option, ONIG_OPTION_ASCII_RANGE, 0);
ONOFF(option, ONIG_OPTION_POSIX_BRACKET_ALL_RANGE, 0);
@@ -5255,8 +5223,8 @@ parse_enclose(Node** np, OnigToken* tok, int term, UChar** src, UChar* end,
case 'a': /* limits \d, \s, \w and POSIX brackets to ASCII range */
if ((IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_OPTION_PERL) ||
- IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_OPTION_RUBY)) &&
- (neg == 0)) {
+ IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_OPTION_RUBY)) &&
+ (neg == 0)) {
ONOFF(option, ONIG_OPTION_ASCII_RANGE, 0);
ONOFF(option, ONIG_OPTION_POSIX_BRACKET_ALL_RANGE, 1);
ONOFF(option, ONIG_OPTION_WORD_BOUND_ALL_RANGE, 1);
@@ -5267,8 +5235,8 @@ parse_enclose(Node** np, OnigToken* tok, int term, UChar** src, UChar* end,
case 'u':
if ((IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_OPTION_PERL) ||
- IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_OPTION_RUBY)) &&
- (neg == 0)) {
+ IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_OPTION_RUBY)) &&
+ (neg == 0)) {
ONOFF(option, ONIG_OPTION_ASCII_RANGE, 1);
ONOFF(option, ONIG_OPTION_POSIX_BRACKET_ALL_RANGE, 1);
ONOFF(option, ONIG_OPTION_WORD_BOUND_ALL_RANGE, 1);
@@ -5279,11 +5247,11 @@ parse_enclose(Node** np, OnigToken* tok, int term, UChar** src, UChar* end,
case 'd':
if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_OPTION_PERL) &&
- (neg == 0)) {
+ (neg == 0)) {
ONOFF(option, ONIG_OPTION_ASCII_RANGE, 1);
}
else if (IS_SYNTAX_OP2(env->syntax, ONIG_SYN_OP2_OPTION_RUBY) &&
- (neg == 0)) {
+ (neg == 0)) {
ONOFF(option, ONIG_OPTION_ASCII_RANGE, 0);
ONOFF(option, ONIG_OPTION_POSIX_BRACKET_ALL_RANGE, 0);
ONOFF(option, ONIG_OPTION_WORD_BOUND_ALL_RANGE, 0);
@@ -5313,9 +5281,12 @@ parse_enclose(Node** np, OnigToken* tok, int term, UChar** src, UChar* end,
else if (c == ':') {
OnigOptionType prev = env->option;
- env->option = option;
+ env->option = option;
r = fetch_token(tok, &p, end, env);
- if (r < 0) return r;
+ if (r < 0) {
+ env->option = prev;
+ return r;
+ }
r = parse_subexp(&target, tok, term, &p, end, env);
env->option = prev;
if (r < 0) return r;
@@ -5430,29 +5401,29 @@ set_quantifier(Node* qnode, Node* target, int group, ScanEnv* env)
int targetq_num = popular_quantifier_num(qnt);
#ifdef USE_WARNING_REDUNDANT_NESTED_REPEAT_OPERATOR
- if (!IS_QUANTIFIER_BY_NUMBER(qn) && !IS_QUANTIFIER_BY_NUMBER(qnt) &&
+ if (nestq_num >= 0 && targetq_num >= 0 &&
IS_SYNTAX_BV(env->syntax, ONIG_SYN_WARN_REDUNDANT_NESTED_REPEAT)) {
- switch (ReduceTypeTable[targetq_num][nestq_num]) {
- case RQ_ASIS:
- break;
-
- case RQ_DEL:
- if (onig_warn != onig_null_warn) {
- onig_syntax_warn(env, "regular expression has redundant nested repeat operator '%s'",
- PopularQStr[targetq_num]);
- }
- goto warn_exit;
- break;
-
- default:
- if (onig_warn != onig_null_warn) {
- onig_syntax_warn(env, "nested repeat operator '%s' and '%s' was replaced with '%s' in regular expression",
- PopularQStr[targetq_num], PopularQStr[nestq_num],
- ReduceQStr[ReduceTypeTable[targetq_num][nestq_num]]);
- }
- goto warn_exit;
- break;
- }
+ switch (ReduceTypeTable[targetq_num][nestq_num]) {
+ case RQ_ASIS:
+ break;
+
+ case RQ_DEL:
+ if (onig_warn != onig_null_warn) {
+ onig_syntax_warn(env, "regular expression has redundant nested repeat operator '%s'",
+ PopularQStr[targetq_num]);
+ }
+ goto warn_exit;
+ break;
+
+ default:
+ if (onig_warn != onig_null_warn) {
+ onig_syntax_warn(env, "nested repeat operator '%s' and '%s' was replaced with '%s' in regular expression",
+ PopularQStr[targetq_num], PopularQStr[nestq_num],
+ ReduceQStr[ReduceTypeTable[targetq_num][nestq_num]]);
+ }
+ goto warn_exit;
+ break;
+ }
}
warn_exit:
@@ -5482,85 +5453,6 @@ set_quantifier(Node* qnode, Node* target, int group, ScanEnv* env)
}
-#ifdef USE_SHARED_CCLASS_TABLE
-
-#define THRESHOLD_RANGE_NUM_FOR_SHARE_CCLASS 8
-
-/* for ctype node hash table */
-
-typedef struct {
- OnigEncoding enc;
- int not;
- int type;
-} type_cclass_key;
-
-static int type_cclass_cmp(type_cclass_key* x, type_cclass_key* y)
-{
- if (x->type != y->type) return 1;
- if (x->enc != y->enc) return 1;
- if (x->not != y->not) return 1;
- return 0;
-}
-
-static st_index_t type_cclass_hash(type_cclass_key* key)
-{
- int i, val;
- UChar *p;
-
- val = 0;
-
- p = (UChar* )&(key->enc);
- for (i = 0; i < (int )sizeof(key->enc); i++) {
- val = val * 997 + (int )*p++;
- }
-
- p = (UChar* )(&key->type);
- for (i = 0; i < (int )sizeof(key->type); i++) {
- val = val * 997 + (int )*p++;
- }
-
- val += key->not;
- return val + (val >> 5);
-}
-
-static const struct st_hash_type type_type_cclass_hash = {
- type_cclass_cmp,
- type_cclass_hash,
-};
-
-static st_table* OnigTypeCClassTable;
-
-
-static int
-i_free_shared_class(type_cclass_key* key, Node* node, void* arg ARG_UNUSED)
-{
- if (IS_NOT_NULL(node)) {
- CClassNode* cc = NCCLASS(node);
- if (IS_NOT_NULL(cc->mbuf)) xfree(cc->mbuf);
- xfree(node);
- }
-
- if (IS_NOT_NULL(key)) xfree(key);
- return ST_DELETE;
-}
-
-extern int
-onig_free_shared_cclass_table(void)
-{
- /* THREAD_ATOMIC_START; */
- if (IS_NOT_NULL(OnigTypeCClassTable)) {
- onig_st_foreach(OnigTypeCClassTable, i_free_shared_class, 0);
- onig_st_free_table(OnigTypeCClassTable);
- OnigTypeCClassTable = NULL;
- }
- /* THREAD_ATOMIC_END; */
-
- return 0;
-}
-
-#endif /* USE_SHARED_CCLASS_TABLE */
-
-
#ifndef CASE_FOLD_IS_APPLIED_INSIDE_NEGATIVE_CCLASS
static int
clear_not_flag_cclass(CClassNode* cc, OnigEncoding enc)
@@ -5603,7 +5495,7 @@ i_apply_case_fold(OnigCodePoint from, OnigCodePoint to[],
CClassNode* cc;
CClassNode* asc_cc;
BitSetRef bs;
- int add_flag;
+ int add_flag, r;
iarg = (IApplyCaseFoldArg* )arg;
env = iarg->env;
@@ -5630,7 +5522,8 @@ i_apply_case_fold(OnigCodePoint from, OnigCodePoint to[],
(is_in == 0 && IS_NCCLASS_NOT(cc))) {
if (add_flag) {
if (ONIGENC_MBC_MINLEN(env->enc) > 1 || *to >= SINGLE_BYTE_SIZE) {
- add_code_range0(&(cc->mbuf), env, *to, *to, 0);
+ r = add_code_range0(&(cc->mbuf), env, *to, *to, 0);
+ if (r < 0) return r;
}
else {
BITSET_SET_BIT(bs, *to);
@@ -5642,7 +5535,8 @@ i_apply_case_fold(OnigCodePoint from, OnigCodePoint to[],
if (add_flag) {
if (ONIGENC_MBC_MINLEN(env->enc) > 1 || *to >= SINGLE_BYTE_SIZE) {
if (IS_NCCLASS_NOT(cc)) clear_not_flag_cclass(cc, env->enc);
- add_code_range0(&(cc->mbuf), env, *to, *to, 0);
+ r = add_code_range0(&(cc->mbuf), env, *to, *to, 0);
+ if (r < 0) return r;
}
else {
if (IS_NCCLASS_NOT(cc)) {
@@ -5732,7 +5626,7 @@ node_linebreak(Node** np, ScanEnv* env)
Node* target1 = NULL;
Node* target2 = NULL;
CClassNode* cc;
- int num1, num2;
+ int num1, num2, r;
UChar buf[ONIGENC_CODE_TO_MBC_MAXLEN * 2];
/* \x0D\x0A */
@@ -5748,7 +5642,8 @@ node_linebreak(Node** np, ScanEnv* env)
if (IS_NULL(right)) goto err;
cc = NCCLASS(right);
if (ONIGENC_MBC_MINLEN(env->enc) > 1) {
- add_code_range(&(cc->mbuf), env, 0x0A, 0x0D);
+ r = add_code_range(&(cc->mbuf), env, 0x0A, 0x0D);
+ if (r != 0) goto err;
}
else {
bitset_set_range(env, cc->bs, 0x0A, 0x0D);
@@ -5757,8 +5652,10 @@ node_linebreak(Node** np, ScanEnv* env)
/* TODO: move this block to enc/unicode.c */
if (ONIGENC_IS_UNICODE(env->enc)) {
/* UTF-8, UTF-16BE/LE, UTF-32BE/LE */
- add_code_range(&(cc->mbuf), env, 0x85, 0x85);
- add_code_range(&(cc->mbuf), env, 0x2028, 0x2029);
+ r = add_code_range(&(cc->mbuf), env, 0x85, 0x85);
+ if (r != 0) goto err;
+ r = add_code_range(&(cc->mbuf), env, 0x2028, 0x2029);
+ if (r != 0) goto err;
}
/* ...|... */
@@ -5787,7 +5684,7 @@ node_linebreak(Node** np, ScanEnv* env)
static int
propname2ctype(ScanEnv* env, const char* propname)
{
- UChar* name = (UChar*)propname;
+ UChar* name = (UChar* )propname;
int ctype = env->enc->property_name_to_ctype(ONIG_ENCODING_ASCII,
name, name + strlen(propname));
return ctype;
@@ -5796,21 +5693,23 @@ propname2ctype(ScanEnv* env, const char* propname)
static int
node_extended_grapheme_cluster(Node** np, ScanEnv* env)
{
+ Node* tmp = NULL;
Node* np1 = NULL;
Node* list = NULL;
Node* list2 = NULL;
Node* alt = NULL;
Node* alt2 = NULL;
+ BBuf *pbuf1 = NULL;
int r = 0;
+ int num1;
+ UChar buf[ONIGENC_CODE_TO_MBC_MAXLEN * 2];
+ OnigOptionType option;
#ifdef USE_UNICODE_PROPERTIES
if (ONIGENC_IS_UNICODE(env->enc)) {
/* UTF-8, UTF-16BE/LE, UTF-32BE/LE */
- Node* tmp = NULL;
- int num1, num2;
- UChar buf[ONIGENC_CODE_TO_MBC_MAXLEN * 2];
CClassNode* cc;
- OnigOptionType option;
+ OnigCodePoint sb_out = (ONIGENC_MBC_MINLEN(env->enc) > 1) ? 0x00 : 0x80;
int extend = propname2ctype(env, "Grapheme_Cluster_Break=Extend");
/* Prepend*
@@ -5845,8 +5744,26 @@ node_extended_grapheme_cluster(Node** np, ScanEnv* env)
cc = NCCLASS(np1);
r = add_ctype_to_cc(cc, propname2ctype(env, "Grapheme_Cluster_Break=Control"), 1, 0, env);
if (r != 0) goto err;
- BITSET_CLEAR_BIT(cc->bs, 0x0a);
- BITSET_CLEAR_BIT(cc->bs, 0x0d);
+ if (ONIGENC_MBC_MINLEN(env->enc) > 1) {
+ BBuf *pbuf2 = NULL;
+ r = add_code_range(&pbuf1, env, 0x0a, 0x0a);
+ if (r != 0) goto err;
+ r = add_code_range(&pbuf1, env, 0x0d, 0x0d);
+ if (r != 0) goto err;
+ r = and_code_range_buf(cc->mbuf, 0, pbuf1, 1, &pbuf2, env);
+ if (r != 0) {
+ bbuf_free(pbuf2);
+ goto err;
+ }
+ bbuf_free(pbuf1);
+ pbuf1 = NULL;
+ bbuf_free(cc->mbuf);
+ cc->mbuf = pbuf2;
+ }
+ else {
+ BITSET_CLEAR_BIT(cc->bs, 0x0a);
+ BITSET_CLEAR_BIT(cc->bs, 0x0d);
+ }
tmp = onig_node_new_alt(np1, NULL_NODE);
if (IS_NULL(tmp)) goto err;
@@ -6134,32 +6051,26 @@ node_extended_grapheme_cluster(Node** np, ScanEnv* env)
np1 = node_new_cclass();
if (IS_NULL(np1)) goto err;
cc = NCCLASS(np1);
- r = add_code_range(&(cc->mbuf), env, 0x1F308, 0x1F308);
- if (r != 0) goto err;
- r = add_code_range(&(cc->mbuf), env, 0x1F33E, 0x1F33E);
- if (r != 0) goto err;
- r = add_code_range(&(cc->mbuf), env, 0x1F373, 0x1F373);
- if (r != 0) goto err;
- r = add_code_range(&(cc->mbuf), env, 0x1F393, 0x1F393);
- if (r != 0) goto err;
- r = add_code_range(&(cc->mbuf), env, 0x1F3A4, 0x1F3A4);
- if (r != 0) goto err;
- r = add_code_range(&(cc->mbuf), env, 0x1F3A8, 0x1F3A8);
- if (r != 0) goto err;
- r = add_code_range(&(cc->mbuf), env, 0x1F3EB, 0x1F3EB);
- if (r != 0) goto err;
- r = add_code_range(&(cc->mbuf), env, 0x1F3ED, 0x1F3ED);
- if (r != 0) goto err;
- r = add_code_range(&(cc->mbuf), env, 0x1F4BB, 0x1F4BC);
- if (r != 0) goto err;
- r = add_code_range(&(cc->mbuf), env, 0x1F527, 0x1F527);
- if (r != 0) goto err;
- r = add_code_range(&(cc->mbuf), env, 0x1F52C, 0x1F52C);
- if (r != 0) goto err;
- r = add_code_range(&(cc->mbuf), env, 0x1F680, 0x1F680);
- if (r != 0) goto err;
- r = add_code_range(&(cc->mbuf), env, 0x1F692, 0x1F692);
- if (r != 0) goto err;
+ {
+ static const OnigCodePoint ranges[] = {
+ 13,
+ 0x1F308, 0x1F308,
+ 0x1F33E, 0x1F33E,
+ 0x1F373, 0x1F373,
+ 0x1F393, 0x1F393,
+ 0x1F3A4, 0x1F3A4,
+ 0x1F3A8, 0x1F3A8,
+ 0x1F3EB, 0x1F3EB,
+ 0x1F3ED, 0x1F3ED,
+ 0x1F4BB, 0x1F4BC,
+ 0x1F527, 0x1F527,
+ 0x1F52C, 0x1F52C,
+ 0x1F680, 0x1F680,
+ 0x1F692, 0x1F692,
+ };
+ r = add_ctype_to_cc_by_range(cc, -1, 0, env, sb_out, ranges);
+ if (r != 0) goto err;
+ }
r = add_ctype_to_cc(cc, propname2ctype(env, "Grapheme_Cluster_Break=Glue_After_Zwj"), 0, 0, env);
if (r != 0) goto err;
@@ -6176,11 +6087,10 @@ node_extended_grapheme_cluster(Node** np, ScanEnv* env)
/* Emoji variation sequence
* http://unicode.org/Public/emoji/4.0/emoji-zwj-sequences.txt
*/
- np1 = node_new_cclass();
+ r = ONIGENC_CODE_TO_MBC(env->enc, 0xfe0f, buf);
+ if (r < 0) goto err;
+ np1 = node_new_str_raw(buf, buf + r);
if (IS_NULL(np1)) goto err;
- cc = NCCLASS(np1);
- r = add_code_range(&(cc->mbuf), env, 0xfe0f, 0xfe0f);
- if (r != 0) goto err;
tmp = node_new_quantifier(0, 1, 0);
if (IS_NULL(tmp)) goto err;
@@ -6195,14 +6105,17 @@ node_extended_grapheme_cluster(Node** np, ScanEnv* env)
np1 = node_new_cclass();
if (IS_NULL(np1)) goto err;
cc = NCCLASS(np1);
- r = add_code_range(&(cc->mbuf), env, 0x2640, 0x2640);
- if (r != 0) goto err;
- r = add_code_range(&(cc->mbuf), env, 0x2642, 0x2642);
- if (r != 0) goto err;
- r = add_code_range(&(cc->mbuf), env, 0x2695, 0x2696);
- if (r != 0) goto err;
- r = add_code_range(&(cc->mbuf), env, 0x2708, 0x2708);
- if (r != 0) goto err;
+ {
+ static const OnigCodePoint ranges[] = {
+ 4,
+ 0x2640, 0x2640,
+ 0x2642, 0x2642,
+ 0x2695, 0x2696,
+ 0x2708, 0x2708,
+ };
+ r = add_ctype_to_cc_by_range(cc, -1, 0, env, sb_out, ranges);
+ if (r != 0) goto err;
+ }
tmp = node_new_list(np1, list2);
if (IS_NULL(tmp)) goto err;
@@ -6220,11 +6133,10 @@ node_extended_grapheme_cluster(Node** np, ScanEnv* env)
alt2 = NULL;
/* ZWJ */
- np1 = node_new_cclass();
+ r = ONIGENC_CODE_TO_MBC(env->enc, 0x200D, buf);
+ if (r < 0) goto err;
+ np1 = node_new_str_raw(buf, buf + r);
if (IS_NULL(np1)) goto err;
- cc = NCCLASS(np1);
- r = add_code_range(&(cc->mbuf), env, 0x200D, 0x200D);
- if (r != 0) goto err;
tmp = node_new_list(np1, list2);
if (IS_NULL(tmp)) goto err;
@@ -6280,21 +6192,21 @@ node_extended_grapheme_cluster(Node** np, ScanEnv* env)
np1 = node_new_cclass();
if (IS_NULL(np1)) goto err;
cc = NCCLASS(np1);
- r = add_code_range(&(cc->mbuf), env, 0x1F3C2, 0x1F3C2);
- if (r != 0) goto err;
- r = add_code_range(&(cc->mbuf), env, 0x1F3C7, 0x1F3C7);
- if (r != 0) goto err;
- r = add_code_range(&(cc->mbuf), env, 0x1F3CC, 0x1F3CC);
- if (r != 0) goto err;
- r = add_code_range(&(cc->mbuf), env, 0x1F3F3, 0x1F3F3);
- if (r != 0) goto err;
- r = add_code_range(&(cc->mbuf), env, 0x1F441, 0x1F441);
- if (r != 0) goto err;
- r = add_code_range(&(cc->mbuf), env, 0x1F46F, 0x1F46F);
- if (r != 0) goto err;
- r = add_code_range(&(cc->mbuf), env, 0x1F574, 0x1F574);
- if (r != 0) goto err;
- r = add_code_range(&(cc->mbuf), env, 0x1F6CC, 0x1F6CC);
+ {
+ static const OnigCodePoint ranges[] = {
+ 8,
+ 0x1F3C2, 0x1F3C2,
+ 0x1F3C7, 0x1F3C7,
+ 0x1F3CC, 0x1F3CC,
+ 0x1F3F3, 0x1F3F3,
+ 0x1F441, 0x1F441,
+ 0x1F46F, 0x1F46F,
+ 0x1F574, 0x1F574,
+ 0x1F6CC, 0x1F6CC,
+ };
+ r = add_ctype_to_cc_by_range(cc, -1, 0, env, sb_out, ranges);
+ if (r != 0) goto err;
+ }
r = add_ctype_to_cc(cc, propname2ctype(env, "Grapheme_Cluster_Break=E_Base"), 0, 0, env);
if (r != 0) goto err;
r = add_ctype_to_cc(cc, propname2ctype(env, "Grapheme_Cluster_Break=E_Base_GAZ"), 0, 0, env);
@@ -6344,11 +6256,10 @@ node_extended_grapheme_cluster(Node** np, ScanEnv* env)
list2 = tmp;
np1 = NULL;
- np1 = node_new_cclass();
+ r = ONIGENC_CODE_TO_MBC(env->enc, 0x200D, buf);
+ if (r < 0) goto err;
+ np1 = node_new_str_raw(buf, buf + r);
if (IS_NULL(np1)) goto err;
- cc = NCCLASS(np1);
- r = add_code_range(&(cc->mbuf), env, 0x200D, 0x200D);
- if (r != 0) goto err;
tmp = node_new_list(np1, list2);
if (IS_NULL(tmp)) goto err;
@@ -6421,11 +6332,10 @@ node_extended_grapheme_cluster(Node** np, ScanEnv* env)
np1 = NULL;
/* Prepend+ */
- np1 = node_new_cclass();
+ r = ONIGENC_CODE_TO_MBC(env->enc, 0x200D, buf);
+ if (r < 0) goto err;
+ np1 = node_new_str_raw(buf, buf + r);
if (IS_NULL(np1)) goto err;
- cc = NCCLASS(np1);
- r = add_code_range(&(cc->mbuf), env, 0x200D, 0x200D);
- if (r != 0) goto err;
tmp = node_new_quantifier(0, 1, 0);
if (IS_NULL(tmp)) goto err;
@@ -6462,39 +6372,60 @@ node_extended_grapheme_cluster(Node** np, ScanEnv* env)
if (IS_NULL(tmp)) goto err;
alt = tmp;
list = NULL;
-
- /* \x0D\x0A */
- num1 = ONIGENC_CODE_TO_MBC(env->enc, 0x0D, buf);
- if (num1 < 0) return num1;
- num2 = ONIGENC_CODE_TO_MBC(env->enc, 0x0A, buf + num1);
- if (num2 < 0) return num2;
- np1 = node_new_str_raw(buf, buf + num1 + num2);
- if (IS_NULL(np1)) goto err;
-
- tmp = onig_node_new_alt(np1, alt);
- if (IS_NULL(tmp)) goto err;
- alt = tmp;
- np1 = NULL;
-
- /* (?>...) */
- *np = node_new_enclose(ENCLOSE_STOP_BACKTRACK);
- if (IS_NULL(*np)) goto err;
- NENCLOSE(*np)->target = alt;
- return ONIG_NORMAL;
}
+ else
#endif /* USE_UNICODE_PROPERTIES */
- if (IS_NULL(*np)) {
+ {
/* PerlSyntax: (?s:.), RubySyntax: (?m:.) */
- OnigOptionType option;
np1 = node_new_anychar();
if (IS_NULL(np1)) goto err;
option = env->option;
ONOFF(option, ONIG_OPTION_MULTILINE, 0);
+ tmp = node_new_option(option);
+ if (IS_NULL(tmp)) goto err;
+ NENCLOSE(tmp)->target = np1;
+ np1 = tmp;
+
+ alt = onig_node_new_alt(np1, NULL_NODE);
+ if (IS_NULL(alt)) goto err;
+ np1 = NULL;
+ }
+
+ /* \x0D\x0A */
+ r = ONIGENC_CODE_TO_MBC(env->enc, 0x0D, buf);
+ if (r < 0) goto err;
+ num1 = r;
+ r = ONIGENC_CODE_TO_MBC(env->enc, 0x0A, buf + num1);
+ if (r < 0) goto err;
+ np1 = node_new_str_raw(buf, buf + num1 + r);
+ if (IS_NULL(np1)) goto err;
+
+ tmp = onig_node_new_alt(np1, alt);
+ if (IS_NULL(tmp)) goto err;
+ alt = tmp;
+ np1 = NULL;
+
+ /* (?>\x0D\x0A|...) */
+ tmp = node_new_enclose(ENCLOSE_STOP_BACKTRACK);
+ if (IS_NULL(tmp)) goto err;
+ NENCLOSE(tmp)->target = alt;
+ np1 = tmp;
+
+#ifdef USE_UNICODE_PROPERTIES
+ if (ONIGENC_IS_UNICODE(env->enc)) {
+ /* Don't ignore case. */
+ option = env->option;
+ ONOFF(option, ONIG_OPTION_IGNORECASE, 1);
*np = node_new_option(option);
if (IS_NULL(*np)) goto err;
NENCLOSE(*np)->target = np1;
}
+ else
+#endif
+ {
+ *np = np1;
+ }
return ONIG_NORMAL;
err:
@@ -6503,6 +6434,7 @@ node_extended_grapheme_cluster(Node** np, ScanEnv* env)
onig_node_free(list2);
onig_node_free(alt);
onig_node_free(alt2);
+ bbuf_free(pbuf1);
return (r == 0) ? ONIGERR_MEMORY : r;
}
@@ -6535,7 +6467,7 @@ is_onechar_cclass(CClassNode* cc, OnigCodePoint* code)
/* only one char found in the bbuf, save the code point. */
c = data[0];
if (((c < SINGLE_BYTE_SIZE) && BITSET_AT(cc->bs, c))) {
- /* skip if c is included in the bitset */
+ /* skip if c is included in the bitset */
c = not_found;
}
}
@@ -6549,9 +6481,9 @@ is_onechar_cclass(CClassNode* cc, OnigCodePoint* code)
Bits b1 = cc->bs[i];
if (b1 != 0) {
if (((b1 & (b1 - 1)) == 0) && (c == not_found)) {
- c = BITS_IN_ROOM * i + countbits(b1 - 1);
+ c = BITS_IN_ROOM * i + countbits(b1 - 1);
} else {
- return 0; /* the character class contains multiple chars */
+ return 0; /* the character class contains multiple chars */
}
}
}
@@ -6596,7 +6528,10 @@ parse_exp(Node** np, OnigToken* tok, int term,
env->option = NENCLOSE(*np)->option;
r = fetch_token(tok, src, end, env);
- if (r < 0) return r;
+ if (r < 0) {
+ env->option = prev;
+ return r;
+ }
r = parse_subexp(&target, tok, term, src, end, env);
env->option = prev;
if (r < 0) {
@@ -6749,69 +6684,13 @@ parse_exp(Node** np, OnigToken* tok, int term,
{
CClassNode* cc;
-#ifdef USE_SHARED_CCLASS_TABLE
- const OnigCodePoint *mbr;
- OnigCodePoint sb_out;
-
- r = ONIGENC_GET_CTYPE_CODE_RANGE(env->enc, tok->u.prop.ctype,
- &sb_out, &mbr);
- if (r == 0 &&
- ! IS_ASCII_RANGE(env->option) &&
- ONIGENC_CODE_RANGE_NUM(mbr)
- >= THRESHOLD_RANGE_NUM_FOR_SHARE_CCLASS) {
- type_cclass_key key;
- type_cclass_key* new_key;
-
- key.enc = env->enc;
- key.not = tok->u.prop.not;
- key.type = tok->u.prop.ctype;
-
- THREAD_ATOMIC_START;
-
- if (IS_NULL(OnigTypeCClassTable)) {
- OnigTypeCClassTable
- = onig_st_init_table_with_size(&type_type_cclass_hash, 10);
- if (IS_NULL(OnigTypeCClassTable)) {
- THREAD_ATOMIC_END;
- return ONIGERR_MEMORY;
- }
- }
- else {
- if (onig_st_lookup(OnigTypeCClassTable, (st_data_t )&key,
- (st_data_t* )np)) {
- THREAD_ATOMIC_END;
- break;
- }
- }
-
- *np = node_new_cclass_by_codepoint_range(tok->u.prop.not,
- sb_out, mbr);
- if (IS_NULL(*np)) {
- THREAD_ATOMIC_END;
- return ONIGERR_MEMORY;
- }
-
- cc = NCCLASS(*np);
- NCCLASS_SET_SHARE(cc);
- new_key = (type_cclass_key* )xmalloc(sizeof(type_cclass_key));
- xmemcpy(new_key, &key, sizeof(type_cclass_key));
- onig_st_add_direct(OnigTypeCClassTable, (st_data_t )new_key,
- (st_data_t )*np);
-
- THREAD_ATOMIC_END;
- }
- else {
-#endif
- *np = node_new_cclass();
- CHECK_NULL_RETURN_MEMERR(*np);
- cc = NCCLASS(*np);
- r = add_ctype_to_cc(cc, tok->u.prop.ctype, 0,
- IS_ASCII_RANGE(env->option), env);
- if (r != 0) return r;
- if (tok->u.prop.not != 0) NCCLASS_SET_NOT(cc);
-#ifdef USE_SHARED_CCLASS_TABLE
- }
-#endif
+ *np = node_new_cclass();
+ CHECK_NULL_RETURN_MEMERR(*np);
+ cc = NCCLASS(*np);
+ r = add_ctype_to_cc(cc, tok->u.prop.ctype, 0,
+ IS_ASCII_RANGE(env->option), env);
+ if (r != 0) return r;
+ if (tok->u.prop.not != 0) NCCLASS_SET_NOT(cc);
}
break;
@@ -7041,6 +6920,9 @@ parse_subexp(Node** top, OnigToken* tok, int term,
Node *node, **headp;
*top = NULL;
+ env->parse_depth++;
+ if (env->parse_depth > ParseDepthLimit)
+ return ONIGERR_PARSE_DEPTH_LIMIT_OVER;
r = parse_branch(&node, tok, term, src, end, env);
if (r < 0) {
onig_node_free(node);
@@ -7078,6 +6960,7 @@ parse_subexp(Node** top, OnigToken* tok, int term,
return ONIGERR_PARSER_BUG;
}
+ env->parse_depth--;
return r;
}