diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-08-26 08:29:17 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-08-26 08:29:17 +0000 |
commit | a153263b89f25bfac5fbfb1014ba024353375361 (patch) | |
tree | b38261e245d4c6d39f30c95ac1126b1008909c49 /parse.y | |
parent | 32a7fbc996114863966a3d9f0b059a4078026a20 (diff) | |
download | ruby-a153263b89f25bfac5fbfb1014ba024353375361.tar.gz |
parse.y: new_regexp_gen
* parse.y (new_regexp_gen): extract from the rule.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56009 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'parse.y')
-rw-r--r-- | parse.y | 160 |
1 files changed, 87 insertions, 73 deletions
@@ -501,6 +501,9 @@ static NODE *new_hash_gen(struct parser_params *parser, NODE *hash); #define new_defined(expr) NEW_DEFINED(remove_begin_all(expr)) +static NODE *new_regexp_gen(struct parser_params *, NODE *, int); +#define new_regexp(node, opt) new_regexp_gen(parser, node, opt) + static NODE *match_op_gen(struct parser_params*,NODE*,NODE*); #define match_op(node1,node2) match_op_gen(parser, (node1), (node2)) @@ -557,6 +560,10 @@ static VALUE new_op_assign_gen(struct parser_params *parser, VALUE lhs, VALUE op static VALUE new_attr_op_assign_gen(struct parser_params *parser, VALUE lhs, VALUE type, VALUE attr, VALUE op, VALUE rhs); #define new_attr_op_assign(lhs, type, attr, op, rhs) new_attr_op_assign_gen(parser, (lhs), (type), (attr), (op), (rhs)) #define new_const_op_assign(lhs, op, rhs) new_op_assign(lhs, op, rhs) + +static VALUE new_regexp_gen(struct parser_params *, VALUE, VALUE); +#define new_regexp(node, opt) new_regexp_gen(parser, node, opt) + #define const_path_field(w, n) dispatch2(const_path_field, (w), (n)) #define top_const_field(n) dispatch1(top_const_field, (n)) static VALUE const_decl_gen(struct parser_params *parser, VALUE path); @@ -3923,79 +3930,7 @@ xstring : tXSTRING_BEG xstring_contents tSTRING_END regexp : tREGEXP_BEG regexp_contents tREGEXP_END { - /*%%%*/ - int options = $3; - NODE *node = $2; - NODE *list, *prev; - if (!node) { - node = NEW_LIT(reg_compile(STR_NEW0(), options)); - } - else switch (nd_type(node)) { - case NODE_STR: - { - VALUE src = node->nd_lit; - nd_set_type(node, NODE_LIT); - node->nd_lit = reg_compile(src, options); - } - break; - default: - node = NEW_NODE(NODE_DSTR, STR_NEW0(), 1, NEW_LIST(node)); - case NODE_DSTR: - if (options & RE_OPTION_ONCE) { - nd_set_type(node, NODE_DREGX_ONCE); - } - else { - nd_set_type(node, NODE_DREGX); - } - node->nd_cflag = options & RE_OPTION_MASK; - if (!NIL_P(node->nd_lit)) reg_fragment_check(node->nd_lit, options); - for (list = (prev = node)->nd_next; list; list = list->nd_next) { - if (nd_type(list->nd_head) == NODE_STR) { - VALUE tail = list->nd_head->nd_lit; - if (reg_fragment_check(tail, options) && prev && !NIL_P(prev->nd_lit)) { - VALUE lit = prev == node ? prev->nd_lit : prev->nd_head->nd_lit; - if (!literal_concat0(parser, lit, tail)) { - node = 0; - break; - } - rb_str_resize(tail, 0); - prev->nd_next = list->nd_next; - rb_gc_force_recycle((VALUE)list->nd_head); - rb_gc_force_recycle((VALUE)list); - list = prev; - } - else { - prev = list; - } - } - else { - prev = 0; - } - } - if (!node->nd_next) { - VALUE src = node->nd_lit; - nd_set_type(node, NODE_LIT); - node->nd_lit = reg_compile(src, options); - } - break; - } - $$ = node; - /*% - VALUE re = $2, opt = $3, src = 0, err; - int options = 0; - if (ripper_is_node_yylval(re)) { - $2 = RNODE(re)->nd_rval; - src = RNODE(re)->nd_cval; - } - if (ripper_is_node_yylval(opt)) { - $3 = RNODE(opt)->nd_rval; - options = (int)RNODE(opt)->nd_tag; - } - if (src && NIL_P(parser_reg_compile(parser, src, options, &err))) { - compile_error(PARSER_ARG "%"PRIsVALUE, err); - } - $$ = dispatch2(regexp_literal, $2, $3); - %*/ + $$ = new_regexp($2, $3); } ; @@ -9123,6 +9058,66 @@ kwd_append(NODE *kwlist, NODE *kw) } return kwlist; } + +static NODE * +new_regexp_gen(struct parser_params *parser, NODE *node, int options) +{ + NODE *list, *prev; + + if (!node) { + return NEW_LIT(reg_compile(STR_NEW0(), options)); + } + switch (nd_type(node)) { + case NODE_STR: + { + VALUE src = node->nd_lit; + nd_set_type(node, NODE_LIT); + node->nd_lit = reg_compile(src, options); + } + break; + default: + node = NEW_NODE(NODE_DSTR, STR_NEW0(), 1, NEW_LIST(node)); + case NODE_DSTR: + if (options & RE_OPTION_ONCE) { + nd_set_type(node, NODE_DREGX_ONCE); + } + else { + nd_set_type(node, NODE_DREGX); + } + node->nd_cflag = options & RE_OPTION_MASK; + if (!NIL_P(node->nd_lit)) reg_fragment_check(node->nd_lit, options); + for (list = (prev = node)->nd_next; list; list = list->nd_next) { + if (nd_type(list->nd_head) == NODE_STR) { + VALUE tail = list->nd_head->nd_lit; + if (reg_fragment_check(tail, options) && prev && !NIL_P(prev->nd_lit)) { + VALUE lit = prev == node ? prev->nd_lit : prev->nd_head->nd_lit; + if (!literal_concat0(parser, lit, tail)) { + node = 0; + break; + } + rb_str_resize(tail, 0); + prev->nd_next = list->nd_next; + rb_gc_force_recycle((VALUE)list->nd_head); + rb_gc_force_recycle((VALUE)list); + list = prev; + } + else { + prev = list; + } + } + else { + prev = 0; + } + } + if (!node->nd_next) { + VALUE src = node->nd_lit; + nd_set_type(node, NODE_LIT); + node->nd_lit = reg_compile(src, options); + } + break; + } + return node; +} #else /* !RIPPER */ static int id_is_var_gen(struct parser_params *parser, ID id) @@ -9141,6 +9136,25 @@ id_is_var_gen(struct parser_params *parser, ID id) compile_error(PARSER_ARG "identifier %s is not valid to get", rb_id2str(id)); return 0; } + +static VALUE +new_regexp_gen(struct parser_params *parser, VALUE re, VALUE opt) +{ + VALUE src = 0, err; + int options = 0; + if (ripper_is_node_yylval(re)) { + src = RNODE(re)->nd_cval; + re = RNODE(re)->nd_rval; + } + if (ripper_is_node_yylval(opt)) { + options = (int)RNODE(opt)->nd_tag; + opt = RNODE(opt)->nd_rval; + } + if (src && NIL_P(parser_reg_compile(parser, src, options, &err))) { + compile_error(PARSER_ARG "%"PRIsVALUE, err); + } + return dispatch2(regexp_literal, re, opt); +} #endif /* !RIPPER */ static const char lex_state_names[][13] = { |