* parse.y (reg_compile_gen): obtain error info from errinfo.

* re.c (rb_reg_error_desc): make RegexpError for initialization error.

* re.c (rb_reg_compile): return nil and set errinfo if error.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13092 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2007-08-18 05:05:36 +00:00
parent 42cf6ee753
commit 3f025d2078
3 changed files with 34 additions and 16 deletions

View File

@ -1,3 +1,11 @@
Sat Aug 18 14:05:34 2007 Nobuyoshi Nakada <nobu@ruby-lang.org>
* parse.y (reg_compile_gen): obtain error info from errinfo.
* re.c (rb_reg_error_desc): make RegexpError for initialization error.
* re.c (rb_reg_compile): return nil and set errinfo if error.
Sat Aug 18 13:23:01 2007 Koichi Sasada <ko1@atdot.net> Sat Aug 18 13:23:01 2007 Koichi Sasada <ko1@atdot.net>
* eval.c: $! should not be writable. * eval.c: $! should not be writable.

View File

@ -8115,7 +8115,8 @@ reg_compile_gen(struct parser_params* parser, const char *ptr, long len, int opt
VALUE rb_reg_compile(const char *, long, int); VALUE rb_reg_compile(const char *, long, int);
VALUE re = rb_reg_compile(ptr, len, (options) & ~RE_OPTION_ONCE); VALUE re = rb_reg_compile(ptr, len, (options) & ~RE_OPTION_ONCE);
if (TYPE(re) == T_STRING) { if (NIL_P(re)) {
RB_GC_GUARD(re) = rb_obj_as_string(rb_errinfo());
compile_error(PARSER_ARG "%s", RSTRING_PTR(re)); compile_error(PARSER_ARG "%s", RSTRING_PTR(re));
return Qnil; return Qnil;
} }

39
re.c
View File

@ -621,6 +621,20 @@ rb_reg_raise(const char *s, long len, const char *err, VALUE re)
rb_raise(rb_eRegexpError, "%s: %s", err, RSTRING_PTR(desc)); rb_raise(rb_eRegexpError, "%s: %s", err, RSTRING_PTR(desc));
} }
static VALUE
rb_reg_error_desc(const char *s, long len, int options, onig_errmsg_buffer err)
{
char opts[6];
VALUE desc = rb_str_buf_new2(err);
rb_str_buf_cat2(desc, ": /");
rb_reg_expr_str(desc, s, len);
opts[0] = '/';
option_to_str(opts + 1, options);
strlcat(opts, arg_kcode(options), sizeof(opts));
rb_str_buf_cat2(desc, opts);
return rb_exc_new3(rb_eRegexpError, desc);
}
/* /*
* call-seq: * call-seq:
@ -1528,10 +1542,10 @@ VALUE
rb_reg_new(const char *s, long len, int options) rb_reg_new(const char *s, long len, int options)
{ {
VALUE re = rb_reg_s_alloc(rb_cRegexp); VALUE re = rb_reg_s_alloc(rb_cRegexp);
char err[ONIG_MAX_ERROR_MESSAGE_LEN]; onig_errmsg_buffer err;
if (rb_reg_initialize(re, s, len, options, err) != 0) { if (rb_reg_initialize(re, s, len, options, err) != 0) {
rb_reg_raise(s, len, err, re); rb_exc_raise(rb_reg_error_desc(s, len, options, err));
} }
return re; return re;
@ -1541,18 +1555,11 @@ VALUE
rb_reg_compile(const char *s, long len, int options) rb_reg_compile(const char *s, long len, int options)
{ {
VALUE re = rb_reg_s_alloc(rb_cRegexp); VALUE re = rb_reg_s_alloc(rb_cRegexp);
char err[ONIG_MAX_ERROR_MESSAGE_LEN]; onig_errmsg_buffer err;
if (rb_reg_initialize(re, s, len, options, err) != 0) { if (rb_reg_initialize(re, s, len, options, err) != 0) {
char opts[6]; rb_set_errinfo(rb_reg_error_desc(s, len, options, err));
VALUE desc = rb_str_buf_new2(err); return Qnil;
rb_str_buf_cat2(desc, ": /");
rb_reg_expr_str(desc, s, len);
opts[0] = '/';
option_to_str(opts + 1, options);
strlcat(opts, arg_kcode(options), sizeof(opts));
return rb_str_buf_cat2(desc, opts);
} }
FL_SET(re, REG_LITERAL); FL_SET(re, REG_LITERAL);
return re; return re;
@ -1870,7 +1877,7 @@ rb_reg_initialize_m(int argc, VALUE *argv, VALUE self)
len = RSTRING_LEN(argv[0]); len = RSTRING_LEN(argv[0]);
} }
if (rb_reg_initialize(self, s, len, flags, err) != 0) { if (rb_reg_initialize(self, s, len, flags, err) != 0) {
rb_reg_raise(s, len, err, self); rb_exc_raise(rb_reg_error_desc(s, len, flags, err));
} }
return self; return self;
} }
@ -2112,6 +2119,7 @@ rb_reg_init_copy(VALUE copy, VALUE re)
onig_errmsg_buffer err; onig_errmsg_buffer err;
const char *s; const char *s;
long len; long len;
int options;
if (copy == re) return copy; if (copy == re) return copy;
rb_check_frozen(copy); rb_check_frozen(copy);
@ -2122,8 +2130,9 @@ rb_reg_init_copy(VALUE copy, VALUE re)
rb_reg_check(re); rb_reg_check(re);
s = RREGEXP(re)->str; s = RREGEXP(re)->str;
len = RREGEXP(re)->len; len = RREGEXP(re)->len;
if (rb_reg_initialize(copy, s, len, rb_reg_options(re), err) != 0) { options = rb_reg_options(re);
rb_reg_raise(s, len, err, copy); if (rb_reg_initialize(copy, s, len, options, err) != 0) {
rb_exc_raise(rb_reg_error_desc(s, len, options, err));
} }
return copy; return copy;
} }