From 7e1abb20c0799a7e8b7fa5987c506543509ecf32 Mon Sep 17 00:00:00 2001 From: nobu Date: Sat, 18 Aug 2007 05:05:36 +0000 Subject: * 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: http://svn.ruby-lang.org/repos/ruby/trunk@13092 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 8 ++++++++ parse.y | 3 ++- re.c | 39 ++++++++++++++++++++++++--------------- 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/ChangeLog b/ChangeLog index c2a5dcd09..1f384c557 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +Sat Aug 18 14:05:34 2007 Nobuyoshi Nakada + + * 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 * eval.c: $! should not be writable. diff --git a/parse.y b/parse.y index af4fa6f8d..583bc45bd 100644 --- a/parse.y +++ b/parse.y @@ -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 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)); return Qnil; } diff --git a/re.c b/re.c index 91766f927..608e43dda 100644 --- a/re.c +++ b/re.c @@ -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)); } +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: @@ -1528,10 +1542,10 @@ VALUE rb_reg_new(const char *s, long len, int options) { 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) { - rb_reg_raise(s, len, err, re); + rb_exc_raise(rb_reg_error_desc(s, len, options, err)); } return re; @@ -1541,18 +1555,11 @@ VALUE rb_reg_compile(const char *s, long len, int options) { 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) { - 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)); - return rb_str_buf_cat2(desc, opts); + rb_set_errinfo(rb_reg_error_desc(s, len, options, err)); + return Qnil; } FL_SET(re, REG_LITERAL); return re; @@ -1870,7 +1877,7 @@ rb_reg_initialize_m(int argc, VALUE *argv, VALUE self) len = RSTRING_LEN(argv[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; } @@ -2112,6 +2119,7 @@ rb_reg_init_copy(VALUE copy, VALUE re) onig_errmsg_buffer err; const char *s; long len; + int options; if (copy == re) return copy; rb_check_frozen(copy); @@ -2122,8 +2130,9 @@ rb_reg_init_copy(VALUE copy, VALUE re) rb_reg_check(re); s = RREGEXP(re)->str; len = RREGEXP(re)->len; - if (rb_reg_initialize(copy, s, len, rb_reg_options(re), err) != 0) { - rb_reg_raise(s, len, err, copy); + options = rb_reg_options(re); + if (rb_reg_initialize(copy, s, len, options, err) != 0) { + rb_exc_raise(rb_reg_error_desc(s, len, options, err)); } return copy; } -- cgit