From 070a990bcb81ed371fc60e3922f5329b1020aa44 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Sat, 10 Oct 2020 16:24:54 +0900 Subject: Save and pass lex_context wholely --- parse.y | 77 ++++++++++++++++++++++++++++++++++------------------------------- 1 file changed, 40 insertions(+), 37 deletions(-) diff --git a/parse.y b/parse.y index 0c192b627d..84a6efd9f6 100644 --- a/parse.y +++ b/parse.y @@ -34,6 +34,8 @@ struct lex_context { unsigned int shareable_constant_value: 1; }; +#define NO_LEX_CTXT (struct lex_context){0} + #include "internal.h" #include "internal/compile.h" #include "internal/complex.h" @@ -521,12 +523,12 @@ static NODE *aryset(struct parser_params*,NODE*,NODE*,const YYLTYPE*); static NODE *attrset(struct parser_params*,NODE*,ID,ID,const YYLTYPE*); static void rb_backref_error(struct parser_params*,NODE*); -static NODE *node_assign(struct parser_params*,NODE*,NODE*,int,const YYLTYPE*); +static NODE *node_assign(struct parser_params*,NODE*,NODE*,struct lex_context,const YYLTYPE*); -static NODE *new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, int shareable, const YYLTYPE *loc); +static NODE *new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc); static NODE *new_ary_op_assign(struct parser_params *p, NODE *ary, NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc); static NODE *new_attr_op_assign(struct parser_params *p, NODE *lhs, ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc); -static NODE *new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, int shareable, const YYLTYPE *loc); +static NODE *new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc); static NODE *new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc); static NODE *const_decl(struct parser_params *p, NODE* path, const YYLTYPE *loc); @@ -1181,7 +1183,7 @@ static int looking_at_eol_p(struct parser_params *p); %type f_kwrest f_label f_arg_asgn call_op call_op2 reswords relop dot_or_colon %type p_rest p_kwrest p_kwnorest p_any_kwrest p_kw_label %type f_no_kwarg f_any_kwrest args_forward excessed_comma -%type shareable + %type lex_ctxt /* keep in ripper */ %token END_OF_INPUT 0 "end-of-input" %token '.' /* escaped chars, should be ignored otherwise */ @@ -1520,7 +1522,7 @@ stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem /*% ripper: END!($3) %*/ } | command_asgn - | mlhs '=' shareable command_call + | mlhs '=' lex_ctxt command_call { /*%%%*/ value_expr($4); @@ -1528,7 +1530,7 @@ stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem /*% %*/ /*% ripper: massign!($1, $4) %*/ } - | lhs '=' shareable mrhs + | lhs '=' lex_ctxt mrhs { /*%%%*/ value_expr($4); @@ -1536,7 +1538,7 @@ stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem /*% %*/ /*% ripper: assign!($1, $4) %*/ } - | mlhs '=' shareable mrhs_arg modifier_rescue stmt + | mlhs '=' lex_ctxt mrhs_arg modifier_rescue stmt { /*%%%*/ YYLTYPE loc = code_loc_gen(&@5, &@6); @@ -1545,7 +1547,7 @@ stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem /*% %*/ /*% ripper: massign!($1, rescue_mod!($4, $6)) %*/ } - | mlhs '=' shareable mrhs_arg + | mlhs '=' lex_ctxt mrhs_arg { /*%%%*/ $$ = node_assign(p, $1, $4, $3, &@$); @@ -1555,21 +1557,21 @@ stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem | expr ; -command_asgn : lhs '=' shareable command_rhs +command_asgn : lhs '=' lex_ctxt command_rhs { /*%%%*/ $$ = node_assign(p, $1, $4, $3, &@$); /*% %*/ /*% ripper: assign!($1, $4) %*/ } - | var_lhs tOP_ASGN shareable command_rhs + | var_lhs tOP_ASGN lex_ctxt command_rhs { /*%%%*/ $$ = new_op_assign(p, $1, $2, $4, $3, &@$); /*% %*/ /*% ripper: opassign!($1, $2, $4) %*/ } - | primary_value '[' opt_call_args rbracket tOP_ASGN shareable command_rhs + | primary_value '[' opt_call_args rbracket tOP_ASGN lex_ctxt command_rhs { /*%%%*/ $$ = new_ary_op_assign(p, $1, $3, $5, $7, &@3, &@$); @@ -1577,21 +1579,21 @@ command_asgn : lhs '=' shareable command_rhs /*% ripper: opassign!(aref_field!($1, escape_Qundef($3)), $5, $7) %*/ } - | primary_value call_op tIDENTIFIER tOP_ASGN shareable command_rhs + | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt command_rhs { /*%%%*/ $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$); /*% %*/ /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/ } - | primary_value call_op tCONSTANT tOP_ASGN shareable command_rhs + | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt command_rhs { /*%%%*/ $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$); /*% %*/ /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/ } - | primary_value tCOLON2 tCONSTANT tOP_ASGN shareable command_rhs + | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt command_rhs { /*%%%*/ YYLTYPE loc = code_loc_gen(&@1, &@3); @@ -1599,14 +1601,14 @@ command_asgn : lhs '=' shareable command_rhs /*% %*/ /*% ripper: opassign!(const_path_field!($1, $3), $4, $6) %*/ } - | primary_value tCOLON2 tIDENTIFIER tOP_ASGN shareable command_rhs + | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt command_rhs { /*%%%*/ $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $6, &@$); /*% %*/ /*% ripper: opassign!(field!($1, ID2VAL(idCOLON2), $3), $4, $6) %*/ } - | backref tOP_ASGN shareable command_rhs + | backref tOP_ASGN lex_ctxt command_rhs { /*%%%*/ rb_backref_error(p, $1); @@ -2254,49 +2256,49 @@ reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__ | keyword_while | keyword_until ; -arg : lhs '=' shareable arg_rhs +arg : lhs '=' lex_ctxt arg_rhs { /*%%%*/ $$ = node_assign(p, $1, $4, $3, &@$); /*% %*/ /*% ripper: assign!($1, $4) %*/ } - | var_lhs tOP_ASGN shareable arg_rhs + | var_lhs tOP_ASGN lex_ctxt arg_rhs { /*%%%*/ $$ = new_op_assign(p, $1, $2, $4, $3, &@$); /*% %*/ /*% ripper: opassign!($1, $2, $4) %*/ } - | primary_value '[' opt_call_args rbracket tOP_ASGN shareable arg_rhs + | primary_value '[' opt_call_args rbracket tOP_ASGN lex_ctxt arg_rhs { /*%%%*/ $$ = new_ary_op_assign(p, $1, $3, $5, $7, &@3, &@$); /*% %*/ /*% ripper: opassign!(aref_field!($1, escape_Qundef($3)), $5, $7) %*/ } - | primary_value call_op tIDENTIFIER tOP_ASGN shareable arg_rhs + | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt arg_rhs { /*%%%*/ $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$); /*% %*/ /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/ } - | primary_value call_op tCONSTANT tOP_ASGN shareable arg_rhs + | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt arg_rhs { /*%%%*/ $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$); /*% %*/ /*% ripper: opassign!(field!($1, $2, $3), $4, $6) %*/ } - | primary_value tCOLON2 tIDENTIFIER tOP_ASGN shareable arg_rhs + | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt arg_rhs { /*%%%*/ $$ = new_attr_op_assign(p, $1, ID2VAL(idCOLON2), $3, $4, $6, &@$); /*% %*/ /*% ripper: opassign!(field!($1, ID2VAL(idCOLON2), $3), $4, $6) %*/ } - | primary_value tCOLON2 tCONSTANT tOP_ASGN shareable arg_rhs + | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt arg_rhs { /*%%%*/ YYLTYPE loc = code_loc_gen(&@1, &@3); @@ -2304,14 +2306,14 @@ arg : lhs '=' shareable arg_rhs /*% %*/ /*% ripper: opassign!(const_path_field!($1, $3), $4, $6) %*/ } - | tCOLON3 tCONSTANT tOP_ASGN shareable arg_rhs + | tCOLON3 tCONSTANT tOP_ASGN lex_ctxt arg_rhs { /*%%%*/ $$ = new_const_op_assign(p, NEW_COLON3($2, &@$), $3, $5, $4, &@$); /*% %*/ /*% ripper: opassign!(top_const_field!($2), $3, $5) %*/ } - | backref tOP_ASGN shareable arg_rhs + | backref tOP_ASGN lex_ctxt arg_rhs { /*%%%*/ rb_backref_error(p, $1); @@ -2551,13 +2553,13 @@ rel_expr : arg relop arg %prec '>' } ; -shareable : tSP +lex_ctxt : tSP { - $$ = p->ctxt.shareable_constant_value; + $$ = p->ctxt; } | none { - $$ = p->ctxt.shareable_constant_value; + $$ = p->ctxt; } ; @@ -3066,10 +3068,10 @@ primary : literal m->nd_next = $2; break; case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */ - m->nd_next = node_assign(p, $2, NEW_FOR_MASGN(internal_var, &@2), 0, &@2); + m->nd_next = node_assign(p, $2, NEW_FOR_MASGN(internal_var, &@2), NO_LEX_CTXT, &@2); break; default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */ - m->nd_next = node_assign(p, NEW_MASGN(NEW_LIST($2, &@2), 0, &@2), internal_var, 0, &@2); + m->nd_next = node_assign(p, NEW_MASGN(NEW_LIST($2, &@2), 0, &@2), internal_var, NO_LEX_CTXT, &@2); } /* {|*internal_id| = internal_id; ... } */ args = new_args(p, m, 0, id, 0, new_args_tail(p, 0, 0, 0, &@2), &@2); @@ -4469,7 +4471,7 @@ opt_rescue : k_rescue exc_list exc_var then { /*%%%*/ $$ = NEW_RESBODY($2, - $3 ? block_append(p, node_assign(p, $3, NEW_ERRINFO(&@3), 0, &@3), $5) : $5, + $3 ? block_append(p, node_assign(p, $3, NEW_ERRINFO(&@3), NO_LEX_CTXT, &@3), $5) : $5, $6, &@$); fixpos($$, $2?$2:$5); /*% %*/ @@ -10950,13 +10952,13 @@ shareable_constant_value(struct parser_params *p, NODE *value, int shareable, co } static NODE * -node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, int shareable, const YYLTYPE *loc) +node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc) { if (!lhs) return 0; switch (nd_type(lhs)) { case NODE_CDECL: - rhs = shareable_constant_value(p, rhs, shareable, loc); + rhs = shareable_constant_value(p, rhs, ctxt.shareable_constant_value, loc); /* fallthru */ case NODE_GASGN: @@ -11880,13 +11882,14 @@ new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc) #ifndef RIPPER static NODE * -new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, int shareable, const YYLTYPE *loc) +new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc) { NODE *asgn; if (lhs) { ID vid = lhs->nd_vid; YYLTYPE lhs_loc = lhs->nd_loc; + int shareable = ctxt.shareable_constant_value; if (shareable) { switch (nd_type(lhs)) { case NODE_CDECL: @@ -11966,12 +11969,12 @@ new_attr_op_assign(struct parser_params *p, NODE *lhs, } static NODE * -new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, int shareable, const YYLTYPE *loc) +new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc) { NODE *asgn; if (lhs) { - rhs = shareable_constant_value(p, rhs, shareable, loc); + rhs = shareable_constant_value(p, rhs, ctxt.shareable_constant_value, loc); asgn = NEW_OP_CDECL(lhs, op, rhs, loc); } else { @@ -12496,7 +12499,7 @@ reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end, return ST_CONTINUE; var = intern_cstr(s, len, enc); - node = node_assign(p, assignable(p, var, 0, arg->loc), NEW_LIT(ID2SYM(var), arg->loc), 0, arg->loc); + node = node_assign(p, assignable(p, var, 0, arg->loc), NEW_LIT(ID2SYM(var), arg->loc), NO_LEX_CTXT, arg->loc); succ = arg->succ_block; if (!succ) succ = NEW_BEGIN(0, arg->loc); succ = block_append(p, succ, node); -- cgit v1.2.3