Skip to content

Commit

Permalink
parse.y: lbracket
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
nobu committed Nov 2, 2015
1 parent 840e6b6 commit dbf73f6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
@@ -1,3 +1,7 @@
Mon Nov 2 20:07:10 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>

* parse.y (lbracket): support .? before aref. [Feature #11537]

Sun Nov 1 17:14:36 2015 Koichi Sasada <ko1@atdot.net>

* id_table.c (mix_id_table_insert): do not touch list during
Expand Down
33 changes: 25 additions & 8 deletions parse.y
Expand Up @@ -373,6 +373,9 @@ static int parser_yyerror(struct parser_params*, const char*);

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

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

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

NO_QCALL($2, "lhs of op_asgn");
/*%%%*/
value_expr($6);
if (!$3) $3 = NEW_ZARRAY();
args = arg_concat($3, $6);
Expand Down Expand Up @@ -1709,8 +1716,9 @@ mlhs_node : user_variable
{
$$ = assignable($1, 0);
}
| primary_value '[' opt_call_args rbracket
| primary_value lbracket opt_call_args rbracket
{
NO_QCALL($2, "mlhs");
/*%%%*/
$$ = aryset($1, $3);
/*%
Expand Down Expand Up @@ -1800,8 +1808,9 @@ lhs : user_variable
$$ = dispatch1(var_field, $$);
%*/
}
| primary_value '[' opt_call_args rbracket
| primary_value lbracket opt_call_args rbracket
{
NO_QCALL($2, "lhs");
/*%%%*/
$$ = aryset($1, $3);
/*%
Expand Down Expand Up @@ -2042,11 +2051,15 @@ arg : lhs '=' arg
%*/
$$ = new_op_assign($1, $2, $3);
}
| primary_value '[' opt_call_args rbracket tOP_ASGN arg
| primary_value lbracket opt_call_args rbracket tOP_ASGN arg
{
/*%%%*/
NODE *args;
/*%
%*/

NO_QCALL($2, "lhs of op_asgn");
/*%%%*/
value_expr($6);
if (!$3) $3 = NEW_ZARRAY();
if (nd_type($3) == NODE_BLOCK_PASS) {
Expand Down Expand Up @@ -3741,13 +3754,13 @@ method_call : fcall paren_args
$$ = dispatch0(zsuper);
%*/
}
| primary_value '[' opt_call_args rbracket
| primary_value lbracket opt_call_args rbracket
{
/*%%%*/
if ($1 && nd_type($1) == NODE_SELF)
if ($2 != tDOTQ && $1 && nd_type($1) == NODE_SELF)
$$ = NEW_FCALL(tAREF, $3);
else
$$ = NEW_CALL($1, tAREF, $3);
$$ = NEW_QCALL($2, $1, tAREF, $3);
fixpos($$, $1);
/*%
$$ = dispatch2(aref, $1, escape_Qundef($3));
Expand Down Expand Up @@ -5158,6 +5171,10 @@ opt_nl : /* none */
rparen : opt_nl ')'
;

lbracket : '[' {$$ = 0;}
| tDOTQ '[' {$$ = tDOTQ;}
;

rbracket : opt_nl ']'
;

Expand Down
4 changes: 4 additions & 0 deletions test/ruby/test_call.rb
Expand Up @@ -46,9 +46,13 @@ def test_safe_call
assert_equal(5, o.y)
o.?z ||= 6
assert_equal(6, o.z)
assert_equal(42, o.?[:x])
assert_equal(42, o.?["x"])

o = nil
assert_nil(o.?x)
assert_nil(o.?[:x])
assert_nil(o.?["x"])
assert_nothing_raised(NoMethodError) {o.?x = 6}
assert_nothing_raised(NoMethodError) {o.?x *= 7}
end
Expand Down

0 comments on commit dbf73f6

Please sign in to comment.