Project

General

Profile

0001-support-ES6-like-hash-literals.patch

shugo (Shugo Maeda), 04/29/2015 12:31 AM

View differences:

parse.y
477 477
static NODE *reg_named_capture_assign_gen(struct parser_params* parser, VALUE regexp, NODE *match);
478 478
#define reg_named_capture_assign(regexp,match) reg_named_capture_assign_gen(parser,(regexp),(match))
479 479

  
480
static NODE *assoc_concat_gen(struct parser_params*,NODE*,NODE*);
481
#define assoc_concat(assocs,tail) assoc_concat_gen(parser,(assocs),(tail))
482
static ID var_to_assoc_key(ID);
483

  
480 484
#define get_id(id) (id)
481 485
#define get_value(val) (val)
482 486
#else
......
820 824
%type <node> command_asgn mrhs mrhs_arg superclass block_call block_command
821 825
%type <node> f_block_optarg f_block_opt
822 826
%type <node> f_arglist f_args f_arg f_arg_item f_optarg f_marg f_marg_list f_margs
823
%type <node> assoc_list assocs assoc undef_list backref string_dvar for_var
827
%type <node> assoc_list assoc_items assoc_item assocs assoc undef_list backref string_dvar for_var
824 828
%type <node> block_param opt_block_param block_param_def f_opt
825 829
%type <node> f_kwarg f_kw f_block_kwarg f_block_kw
826 830
%type <node> bv_decls opt_bv_decl bvar
......
5019 5023
		;
5020 5024

  
5021 5025
assoc_list	: none
5022
		| assocs trailer
5026
		| assoc_items trailer
5023 5027
		    {
5024 5028
		    /*%%%*/
5025 5029
			$$ = $1;
......
5029 5033
		    }
5030 5034
		;
5031 5035

  
5036
assoc_items	: assoc_item
5037
		    /*%c%*/
5038
		    /*%c
5039
		    {
5040
			$$ = rb_ary_new3(1, $1);
5041
		    }
5042
		    %*/
5043
		| assoc_items ',' assoc_item
5044
		    {
5045
		    /*%%%*/
5046
			$$ = assoc_concat($1, $3);
5047
		    /*%
5048
			$$ = rb_ary_push($1, $3);
5049
		    %*/
5050
		    }
5051
		;
5052

  
5053
assoc_item	: assoc
5054
		| user_variable
5055
		    {
5056
		    /*%%%*/
5057
			ID k = var_to_assoc_key($1);
5058
			NODE *key, *val;
5059
			key = NEW_LIT(ID2SYM(k));
5060
			if (!(val = gettable($1))) val = NEW_BEGIN(0);
5061
			$$ = list_append(NEW_LIST(key), val);
5062
		    /*%
5063
			$$ = dispatch2(assoc_new, $1, id_is_var(get_id($1)) ? dispatch1(var_ref, $1) : dispatch1(vcall, $1));
5064
		    %*/
5065
		    }
5066
		;
5067

  
5032 5068
assocs		: assoc
5033 5069
		    /*%c%*/
5034 5070
		    /*%c
......
5039 5075
		| assocs ',' assoc
5040 5076
		    {
5041 5077
		    /*%%%*/
5042
			NODE *assocs = $1;
5043
			NODE *tail = $3;
5044
			if (!assocs) {
5045
			    assocs = tail;
5046
			}
5047
			else if (tail) {
5048
			    if (assocs->nd_head &&
5049
				!tail->nd_head && nd_type(tail->nd_next) == NODE_ARRAY &&
5050
				nd_type(tail->nd_next->nd_head) == NODE_HASH) {
5051
				/* DSTAR */
5052
				tail = tail->nd_next->nd_head->nd_head;
5053
			    }
5054
			    assocs = list_concat(assocs, tail);
5055
			}
5056
			$$ = assocs;
5078
			$$ = assoc_concat($1, $3);
5057 5079
		    /*%
5058 5080
			$$ = rb_ary_push($1, $3);
5059 5081
		    %*/
......
10439 10461
    return re;
10440 10462
}
10441 10463

  
10464
static NODE *
10465
assoc_concat_gen(struct parser_params *parser, NODE *assocs, NODE *tail)
10466
{
10467
    if (!assocs) {
10468
	assocs = tail;
10469
    }
10470
    else if (tail) {
10471
	if (assocs->nd_head &&
10472
	    !tail->nd_head && nd_type(tail->nd_next) == NODE_ARRAY &&
10473
	    nd_type(tail->nd_next->nd_head) == NODE_HASH) {
10474
	    /* DSTAR */
10475
	    tail = tail->nd_next->nd_head->nd_head;
10476
	}
10477
	assocs = list_concat(assocs, tail);
10478
    }
10479
    return assocs;
10480
}
10481

  
10482
static ID var_to_assoc_key(ID var)
10483
{
10484
    switch (id_type(var)) {
10485
      case ID_LOCAL:
10486
      case ID_CONST:
10487
	return var;
10488
      default: {
10489
	const char *name = rb_id2name(var);
10490
	while (*name == '$' || *name == '@')
10491
	    name++;
10492
	return rb_intern(name);
10493
      }
10494
    }
10495
}
10496

  
10442 10497
NODE*
10443 10498
rb_parser_append_print(VALUE vparser, NODE *node)
10444 10499
{
10445
-