Skip to content

Commit dbf73f6

Browse files
committedNov 2, 2015
parse.y: lbracket
* parse.y (lbracket): support .? before aref. [Feature #11537] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52422 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent 840e6b6 commit dbf73f6

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
lines changed
 

‎ChangeLog

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
Mon Nov 2 20:07:10 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
2+
3+
* parse.y (lbracket): support .? before aref. [Feature #11537]
4+
15
Sun Nov 1 17:14:36 2015 Koichi Sasada <ko1@atdot.net>
26

37
* id_table.c (mix_id_table_insert): do not touch list during

‎parse.y

+25-8
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,9 @@ static int parser_yyerror(struct parser_params*, const char*);
373373

374374
#define NODE_CALL_Q(q) (((q) == tDOTQ) ? NODE_QCALL : NODE_CALL)
375375
#define NEW_QCALL(q,r,m,a) NEW_NODE(NODE_CALL_Q(q),r,m,a)
376+
#define NO_QCALL(q, here) \
377+
((q) != tDOTQ ? (void)0 : \
378+
yyerror(".? in "here" is not supported yet"))
376379

377380
static int yylex(YYSTYPE*, struct parser_params*);
378381

@@ -847,7 +850,7 @@ static void token_info_pop(struct parser_params*, const char *token, size_t len)
847850
%type <node> mlhs mlhs_head mlhs_basic mlhs_item mlhs_node mlhs_post mlhs_inner
848851
%type <id> fsym keyword_variable user_variable sym symbol operation operation2 operation3
849852
%type <id> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_bad_arg
850-
%type <id> f_kwrest f_label f_arg_asgn call_op call_op2
853+
%type <id> f_kwrest f_label f_arg_asgn call_op call_op2 lbracket
851854
/*%%%*/
852855
/*%
853856
%type <val> program reswords then do dot_or_colon
@@ -1243,11 +1246,15 @@ stmt : keyword_alias fitem {lex_state = EXPR_FNAME;} fitem
12431246
value_expr($3);
12441247
$$ = new_op_assign($1, $2, $3);
12451248
}
1246-
| primary_value '[' opt_call_args rbracket tOP_ASGN command_call
1249+
| primary_value lbracket opt_call_args rbracket tOP_ASGN command_call
12471250
{
12481251
/*%%%*/
12491252
NODE *args;
1253+
/*%
1254+
%*/
12501255

1256+
NO_QCALL($2, "lhs of op_asgn");
1257+
/*%%%*/
12511258
value_expr($6);
12521259
if (!$3) $3 = NEW_ZARRAY();
12531260
args = arg_concat($3, $6);
@@ -1709,8 +1716,9 @@ mlhs_node : user_variable
17091716
{
17101717
$$ = assignable($1, 0);
17111718
}
1712-
| primary_value '[' opt_call_args rbracket
1719+
| primary_value lbracket opt_call_args rbracket
17131720
{
1721+
NO_QCALL($2, "mlhs");
17141722
/*%%%*/
17151723
$$ = aryset($1, $3);
17161724
/*%
@@ -1800,8 +1808,9 @@ lhs : user_variable
18001808
$$ = dispatch1(var_field, $$);
18011809
%*/
18021810
}
1803-
| primary_value '[' opt_call_args rbracket
1811+
| primary_value lbracket opt_call_args rbracket
18041812
{
1813+
NO_QCALL($2, "lhs");
18051814
/*%%%*/
18061815
$$ = aryset($1, $3);
18071816
/*%
@@ -2042,11 +2051,15 @@ arg : lhs '=' arg
20422051
%*/
20432052
$$ = new_op_assign($1, $2, $3);
20442053
}
2045-
| primary_value '[' opt_call_args rbracket tOP_ASGN arg
2054+
| primary_value lbracket opt_call_args rbracket tOP_ASGN arg
20462055
{
20472056
/*%%%*/
20482057
NODE *args;
2058+
/*%
2059+
%*/
20492060

2061+
NO_QCALL($2, "lhs of op_asgn");
2062+
/*%%%*/
20502063
value_expr($6);
20512064
if (!$3) $3 = NEW_ZARRAY();
20522065
if (nd_type($3) == NODE_BLOCK_PASS) {
@@ -3741,13 +3754,13 @@ method_call : fcall paren_args
37413754
$$ = dispatch0(zsuper);
37423755
%*/
37433756
}
3744-
| primary_value '[' opt_call_args rbracket
3757+
| primary_value lbracket opt_call_args rbracket
37453758
{
37463759
/*%%%*/
3747-
if ($1 && nd_type($1) == NODE_SELF)
3760+
if ($2 != tDOTQ && $1 && nd_type($1) == NODE_SELF)
37483761
$$ = NEW_FCALL(tAREF, $3);
37493762
else
3750-
$$ = NEW_CALL($1, tAREF, $3);
3763+
$$ = NEW_QCALL($2, $1, tAREF, $3);
37513764
fixpos($$, $1);
37523765
/*%
37533766
$$ = dispatch2(aref, $1, escape_Qundef($3));
@@ -5158,6 +5171,10 @@ opt_nl : /* none */
51585171
rparen : opt_nl ')'
51595172
;
51605173

5174+
lbracket : '[' {$$ = 0;}
5175+
| tDOTQ '[' {$$ = tDOTQ;}
5176+
;
5177+
51615178
rbracket : opt_nl ']'
51625179
;
51635180

‎test/ruby/test_call.rb

+4
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,13 @@ def test_safe_call
4646
assert_equal(5, o.y)
4747
o.?z ||= 6
4848
assert_equal(6, o.z)
49+
assert_equal(42, o.?[:x])
50+
assert_equal(42, o.?["x"])
4951

5052
o = nil
5153
assert_nil(o.?x)
54+
assert_nil(o.?[:x])
55+
assert_nil(o.?["x"])
5256
assert_nothing_raised(NoMethodError) {o.?x = 6}
5357
assert_nothing_raised(NoMethodError) {o.?x *= 7}
5458
end

0 commit comments

Comments
 (0)
Please sign in to comment.