Project

General

Profile

Feature #14973 ยป hash_expand.patch

osyo (manga osyo), 08/07/2018 03:00 PM

View differences:

ext/ripper/eventids2.c
42 42
    ID ripper_id_words_beg;
43 43
    ID ripper_id_qwords_beg;
44 44
    ID ripper_id_qsymbols_beg;
45
    ID ripper_id_hashexpand_beg;
45 46
    ID ripper_id_symbols_beg;
46 47
    ID ripper_id_words_sep;
47 48
    ID ripper_id_rational;
......
104 105
    set_id2(words_beg);
105 106
    set_id2(qwords_beg);
106 107
    set_id2(qsymbols_beg);
108
    set_id2(hashexpand_beg);
107 109
    set_id2(symbols_beg);
108 110
    set_id2(words_sep);
109 111
    set_id2(rational);
......
250 252
    {tPOW,			O(op)},
251 253
    {tQWORDS_BEG,		O(qwords_beg)},
252 254
    {tQSYMBOLS_BEG,		O(qsymbols_beg)},
255
    {tHASHEXPAND_BEG,		O(hashexpand_beg)},
253 256
    {tSYMBOLS_BEG,		O(symbols_beg)},
254 257
    {tRATIONAL,			O(rational)},
255 258
    {tREGEXP_BEG,		O(regexp_beg)},
parse.y
848 848

  
849 849
%type <node> singleton strings string string1 xstring regexp
850 850
%type <node> string_contents xstring_contents regexp_contents string_content
851
%type <node> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
851
%type <node> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word  hashexpand hashexpand_list
852 852
%type <node> literal numeric simple_numeric dsym cpath
853 853
%type <node> top_compstmt top_stmts top_stmt begin_block
854 854
%type <node> bodystmt compstmt stmts stmt_or_begin stmt expr arg primary command command_call method_call
......
932 932
%token tDSTAR		"**arg"
933 933
%token tAMPER		"&"
934 934
%token tLAMBDA		"->"
935
%token tSYMBEG tSTRING_BEG tXSTRING_BEG tREGEXP_BEG tWORDS_BEG tQWORDS_BEG tSYMBOLS_BEG tQSYMBOLS_BEG
935
%token tSYMBEG tSTRING_BEG tXSTRING_BEG tREGEXP_BEG tWORDS_BEG tQWORDS_BEG tSYMBOLS_BEG tQSYMBOLS_BEG tHASHEXPAND_BEG
936 936
%token tSTRING_DBEG tSTRING_DEND tSTRING_DVAR tSTRING_END tLAMBEG tLABEL_END
937 937

  
938 938
/*
......
2359 2359
		| qwords
2360 2360
		| symbols
2361 2361
		| qsymbols
2362
		| hashexpand
2362 2363
		| var_ref
2363 2364
		| backref
2364 2365
		| tFID
......
3719 3720
		| qwords
3720 3721
		| symbols
3721 3722
		| qsymbols
3723
		| hashexpand
3722 3724
		| keyword_variable
3723 3725
		    {
3724 3726
		    /*%%%*/
......
3955 3957
		    }
3956 3958
		;
3957 3959

  
3960
hashexpand	: tHASHEXPAND_BEG ' ' hashexpand_list tSTRING_END
3961
		    {
3962
		    /*%%%*/
3963
			$$ = new_hash(p, $3, &@$);
3964
		    /*% %*/
3965
		    /*% ripper: hash!($3) %*/
3966
		    }
3967
		;
3968

  
3969
hashexpand_list	: /* none */
3970
		    {
3971
		    /*%%%*/
3972
			$$ = 0;
3973
		    /*% %*/
3974
		    /*% ripper: hashexpand_new! %*/
3975
		    }
3976
		| hashexpand_list string_content ' '
3977
		    {
3978
		    /*%%%*/
3979
			NODE *key, *val, *estr;
3980
			estr = evstr2dstr(p, $2);
3981
			estr = $2;
3982
			key = NEW_CALL(estr, rb_intern("to_sym"), 0, &@$);
3983
			val = NEW_FCALL(rb_intern("eval"), NEW_LIST(estr, &@$), &@$);
3984
			$$ = list_append(p, list_append(p, $1, key), val);
3985
		    /*% %*/
3986
		    /*% ripper: hashexpand_add!($1, $2) %*/
3987
		    }
3988
		;
3989

  
3958 3990
string_contents : /* none */
3959 3991
		    {
3960 3992
		    /*%%%*/
......
7695 7727
	    SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
7696 7728
	    return tSYMBEG;
7697 7729

  
7730
	  case 'h':
7731
	    p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
7732
	    return tHASHEXPAND_BEG;
7733

  
7698 7734
	  default:
7699 7735
	    yyerror0("unknown type of %string");
7700 7736
	    return 0;
test/ripper/dummyparser.rb
244 244
    symbols.push Node::Sym.new(symbol)
245 245
  end
246 246

  
247
  def on_hashexpand_new
248
#     NodeList.new
249
    Node.new('assoc')
250
  end
251

  
252
  def on_hashexpand_add(hashexpand, str)
253
    pp hashexpand
254
#     hashexpand.push Node::Sym.new(str)
255
#     hashexpand.push str
256
  end
257

  
247 258
  def on_mlhs_new
248 259
    NodeList.new
249 260
  end
test/ripper/test_parser_events.rb
1045 1045
    assert_equal '[array([:a])]', tree
1046 1046
  end
1047 1047

  
1048
  def test_hashexpand_add
1049
    thru_hasexpand_add = false
1050
    a = 1
1051
    b = 2
1052
    tree = parse('%h[a,b]', :on_hashexpand_add) {thru_hasexpand_add = true}
1053
    pp tree
1054
#     assert_equal true, thru_hasexpand_add
1055
#     assert_equal '[hash([:a,a,:b,b])]', tree
1056
  end
1057

  
1048 1058
  def test_qwords_new
1049 1059
    thru_qwords_new = false
1050 1060
    parse('%w[]', :on_qwords_new) {thru_qwords_new = true}
......
1063 1073
    assert_equal true, thru_symbols_new
1064 1074
  end
1065 1075

  
1076
  def test_hashexpand_new
1077
    thru_hashexpand_new = false
1078
    parse('%h[]', :on_hashexpand_new) {thru_hashexpand_new = true}
1079
    assert_equal true, thru_hashexpand_new
1080
  end
1081

  
1066 1082
  def test_redo
1067 1083
    thru_redo = false
1068 1084
    parse('redo', :on_redo) {thru_redo = true}
test/ripper/test_scanner_events.rb
682 682
                 scan('symbols_beg', "%I(\nw)")
683 683
  end
684 684

  
685
  def test_hashexpand_beg
686
    assert_equal [],
687
                 scan('hashexpand_beg', '')
688
    assert_equal ['%h('],
689
                 scan('hashexpand_beg', '%h()')
690
    assert_equal ['%h('],
691
                 scan('hashexpand_beg', '%h(w w w)')
692
    assert_equal ['%h('],
693
                 scan('hashexpand_beg', '%h( w w w )')
694
    assert_equal ['%h('],
695
                 scan('hashexpand_beg', "%h(\nw)")
696
  end
697

  
685 698
  def test_words_sep
686 699
    assert_equal [],
687 700
                 scan('words_sep', '')
test/ruby/test_parse.rb
1200 1200
    assert_valid_syntax('let () { m(a) do; end }')
1201 1201
  end
1202 1202

  
1203
  def test_hashexpand_meth
1204
    "test_hashexpand_meth"
1205
  end
1206

  
1207
  def test_hashexpand
1208
    hoge = 1
1209
    foo = "str"
1210
    bar = nil
1211
    @test_hashexpand_value = { a: 1 }
1212
    @@test_hashexpand_value = [1, 2]
1213
    hash = %h(hoge foo bar @test_hashexpand_value @@test_hashexpand_value test_hashexpand_meth)
1214

  
1215
    assert_equal(
1216
      hash,
1217
      { hoge: 1, foo: "str", bar: nil, "@test_hashexpand_value": { a: 1}, "@@test_hashexpand_value": [1, 2], test_hashexpand_meth: "test_hashexpand_meth" }
1218
    )
1219

  
1220
    assert_equal(
1221
      hash.keys,
1222
      %i(hoge foo bar @test_hashexpand_value @@test_hashexpand_value test_hashexpand_meth)
1223
    )
1224

  
1225
    str = 42
1226
    pp %h(#{foo})
1227
    assert_equal(%h(#{foo}), { str: 42 })
1228

  
1229
    assert_operator(Hash, :===, hash)
1230

  
1231
    proc { |value|
1232
      hoge = -1
1233
      assert_equal(%h(hoge foo value), { hoge: -1, foo: "str", value: 42 })
1234
    }.call 42
1235

  
1236
    assert_equal(%h(hoge foo hoge), { hoge: hoge, foo: foo })
1237
    assert_equal(%h(hoge foo hoge).keys, %i(hoge foo))
1238

  
1239
    assert_raise(NameError) do
1240
      %h(not_found)
1241
    end
1242
  end
1243

  
1203 1244
=begin
1204 1245
  def test_past_scope_variable
1205 1246
    assert_warning(/past scope/) {catch {|tag| eval("BEGIN{throw tag}; tap {a = 1}; a")}}