lib/ruby_parser.yy in ruby_parser-3.14.2 vs lib/ruby_parser.yy in ruby_parser-3.15.0

- old
+ new

@@ -12,10 +12,12 @@ class Ruby24Parser #elif V == 25 class Ruby25Parser #elif V == 26 class Ruby26Parser +#elif V == 27 +class Ruby27Parser #else fail "version not specified or supported on code generation" #endif token kCLASS kMODULE kDEF kUNDEF kBEGIN kRESCUE kENSURE kEND kIF kUNLESS @@ -284,17 +286,19 @@ end result.line = val[0].line } | primary_value tCOLON2 tCONSTANT tOP_ASGN command_rhs { - result = s(:op_asgn, val[0], val[4], val[2], val[3]) - debug20 4, val, result + lhs1, _, lhs2, op, rhs = val + + result = s(:op_asgn, lhs1, rhs, lhs2.to_sym, op.to_sym) } | primary_value tCOLON2 tIDENTIFIER tOP_ASGN command_rhs { - result = s(:op_asgn, val[0], val[4], val[2], val[3]) - debug20 5, val, result + lhs1, _, lhs2, op, rhs = val + + result = s(:op_asgn, lhs1, rhs, lhs2.to_sym, op.to_sym) } | backref tOP_ASGN command_rhs { self.backref_assign_error val[0] } @@ -1076,16 +1080,43 @@ { result = call_args val } command_args: { - result = lexer.cmdarg.store true + # parse26.y line 2200 + + # If call_args starts with a open paren '(' or + # '[', look-ahead reading of the letters calls + # CMDARG_PUSH(0), but the push must be done + # after CMDARG_PUSH(1). So this code makes them + # consistent by first cancelling the premature + # CMDARG_PUSH(0), doing CMDARG_PUSH(1), and + # finally redoing CMDARG_PUSH(0). + + result = yychar = self.last_token_type.first + lookahead = [:tLPAREN, :tLPAREN_ARG, :tLPAREN2, :tLBRACK, :tLBRACK2].include?(yychar) + lexer.cmdarg.pop if lookahead + lexer.cmdarg.push true + lexer.cmdarg.push false if lookahead } call_args { - lexer.cmdarg.restore val[0] - result = val[1] + yychar, args = val + + # call_args can be followed by tLBRACE_ARG (that + # does CMDARG_PUSH(0) in the lexer) but the push + # must be done after CMDARG_POP() in the parser. + # So this code does CMDARG_POP() to pop 0 pushed + # by tLBRACE_ARG, CMDARG_POP() to pop 1 pushed + # by command_args, and CMDARG_PUSH(0) to restore + # back the flag set by tLBRACE_ARG. + + lookahead = [:tLBRACE_ARG].include?(yychar) + lexer.cmdarg.pop if lookahead + lexer.cmdarg.pop + lexer.cmdarg.push false if lookahead + result = args } block_arg: tAMPER arg_value { _, arg = val @@ -1099,12 +1130,13 @@ | none args: arg_value { arg, = val + lineno = arg.line || lexer.lineno # HACK - result = s(:array, arg).line arg.line + result = s(:array, arg).line lineno } | tSTAR arg_value { _, arg = val result = s(:array, s(:splat, arg).line(arg.line)).line arg.line @@ -1112,13 +1144,15 @@ | args tCOMMA arg_value { args, _, id = val result = self.list_append args, id } - | args tCOMMA tSTAR { result = lexer.lineno } arg_value + | args tCOMMA tSTAR arg_value { - args, _, _, line, id = val + # TODO: the line number from tSTAR has been dropped + args, _, _, id = val + line = lexer.lineno result = self.list_append args, s(:splat, id).line(line) } #if V >= 21 mrhs_arg: mrhs @@ -1135,11 +1169,10 @@ { result = val[0] << val[2] } | args tCOMMA tSTAR arg_value { - # FIX: bad shift/reduce conflict with rhs' comma star prod # TODO: make all tXXXX terminals include lexer.lineno arg, _, _, splat = val result = self.arg_concat arg, splat } | tSTAR arg_value @@ -1163,25 +1196,17 @@ msg, = val result = new_call nil, msg.to_sym } | k_begin { + lexer.cmdarg.push false result = self.lexer.lineno - # TODO: - # $<val>1 = cmdarg_stack; - # CMDARG_SET(0); } bodystmt k_end { - # TODO: CMDARG_SET($<val>1); - unless val[2] then - result = s(:nil) - else - result = s(:begin, val[2]) - end - - result.line = val[1] + lexer.cmdarg.pop + result = new_begin val } | tLPAREN_ARG { lexer.lex_state = EXPR_ENDARG result = lexer.lineno @@ -1190,22 +1215,18 @@ { _, line, _ = val result = s(:begin).line line } | tLPAREN_ARG - { - result = lexer.cmdarg.store false - } stmt { lexer.lex_state = EXPR_ENDARG } rparen { - _, cmdarg, stmt, _, _, = val - warning "(...) interpreted as grouped expression" - lexer.cmdarg.restore cmdarg + _, stmt, _, _, = val + # warning "(...) interpreted as grouped expression" result = stmt } | tLPAREN compstmt tRPAREN { _, stmt, _ = val @@ -1384,52 +1405,65 @@ self.env.unextend self.lexer.comments # we don't care about comments in the body } | k_def fname { - result = [self.in_def, self.lexer.cmdarg.stack.dup] + result = self.in_def - self.comments.push self.lexer.comments - self.in_def = true + self.in_def = true # group = local_push self.env.extend - # TODO: local->cmdargs = cmdarg_stack; - # TODO: port local_push_gen and local_pop_gen - lexer.cmdarg.stack.replace [false] + lexer.cmdarg.push false + lexer.cond.push false + + self.comments.push self.lexer.comments } f_arglist bodystmt { result = lexer.lineno } k_end { - in_def, cmdarg = val[2] + in_def = val[2] result = new_defn val - lexer.cmdarg.stack.replace cmdarg + lexer.cond.pop # group = local_pop + lexer.cmdarg.pop self.env.unextend self.in_def = in_def + self.lexer.comments # we don't care about comments in the body } | k_def singleton dot_or_colon { - self.comments.push self.lexer.comments lexer.lex_state = EXPR_FNAME } fname { - self.in_single += 1 + result = [self.in_def, lexer.lineno] + + self.in_single += 1 # TODO: remove? + + self.in_def = true # local_push self.env.extend - lexer.lex_state = EXPR_ENDFN # force for args - result = [lexer.lineno, self.lexer.cmdarg.stack.dup] - lexer.cmdarg.stack.replace [false] + lexer.cmdarg.push false + lexer.cond.push false + + lexer.lex_state = EXPR_ENDFN|EXPR_LABEL + self.comments.push self.lexer.comments } f_arglist bodystmt k_end { - _, cmdarg = val[5] + _, _recv, _, _, _name, (in_def, _lineno), _args, _body, _ = val + result = new_defs val - lexer.cmdarg.stack.replace cmdarg - + lexer.cond.pop # group = local_pop + lexer.cmdarg.pop self.env.unextend + self.in_def = in_def + self.in_single -= 1 + + # TODO: restore cur_arg ? what's cur_arg? + self.lexer.comments # we don't care about comments in the body } | kBREAK { (_, line), = val @@ -1710,24 +1744,23 @@ lexer.paren_nest += 1 lexer.lpar_beg = lexer.paren_nest } f_larglist { - result = lexer.cmdarg.store(false) + lexer.cmdarg.push false } lambda_body { - (line, lpar), args, cmdarg, body = val + (line, lpar), args, _cmdarg, body = val lexer.lpar_beg = lpar - lexer.cmdarg.restore cmdarg - lexer.cmdarg.lexpop + lexer.cmdarg.pop call = s(:lambda).line line result = new_iter call, args, body result.line = line - self.env.unextend + self.env.unextend # TODO: dynapush & dynapop } f_larglist: tLPAREN2 f_args opt_bv_decl rparen { result = args val @@ -1876,23 +1909,23 @@ lexer.cmdarg.restore cmdarg lexer.cmdarg.pop # because of: cmdarg_stack >> 1 ? } do_body: { self.env.extend :dynamic; result = self.lexer.lineno } - { result = lexer.cmdarg.store(false) } + { lexer.cmdarg.push false } opt_block_param #if V >= 25 bodystmt #else compstmt #endif { - line, cmdarg, param, cmpstmt = val + line, _cmdarg, param, cmpstmt = val result = new_do_body param, cmpstmt, line + lexer.cmdarg.pop self.env.unextend - lexer.cmdarg.restore cmdarg } case_body: k_when { result = self.lexer.lineno @@ -1988,11 +2021,11 @@ result = new_string val } xstring: tXSTRING_BEG xstring_contents tSTRING_END { - result = new_xstring val[1] + result = new_xstring val # TODO: dedent?!?! SERIOUSLY?!? } regexp: tREGEXP_BEG regexp_contents tREGEXP_END { @@ -2130,16 +2163,17 @@ | tSTRING_DBEG { result = [lexer.lex_strterm, lexer.brace_nest, lexer.string_nest, # TODO: remove - lexer.cond.store, - lexer.cmdarg.store, lexer.lex_state, lexer.lineno, ] + lexer.cmdarg.push false + lexer.cond.push false + lexer.lex_strterm = nil lexer.brace_nest = 0 lexer.string_nest = 0 lexer.lex_state = EXPR_BEG @@ -2147,17 +2181,18 @@ compstmt tSTRING_DEND { _, memo, stmt, _ = val - lex_strterm, brace_nest, string_nest, oldcond, oldcmdarg, oldlex_state, line = memo + lex_strterm, brace_nest, string_nest, oldlex_state, line = memo + # TODO: heredoc_indent lexer.lex_strterm = lex_strterm lexer.brace_nest = brace_nest lexer.string_nest = string_nest - lexer.cond.restore oldcond - lexer.cmdarg.restore oldcmdarg + lexer.cmdarg.pop + lexer.cond.pop lexer.lex_state = oldlex_state case stmt when Sexp then