summaryrefslogtreecommitdiff
path: root/parse.y
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2019-08-24 02:21:30 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2019-09-24 21:57:54 +0900
commite73cc3eead75b27eabf93a7163bed511f2bb3b17 (patch)
treedff7dd9d9bd5ecd60a185319bd5ff696d2892a74 /parse.y
parenta1dcb9daa56874ff11e717ad141d36f26e0b9c13 (diff)
Added implicit block parameter
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/2431
Diffstat (limited to 'parse.y')
-rw-r--r--parse.y38
1 files changed, 30 insertions, 8 deletions
diff --git a/parse.y b/parse.y
index a51328cdce..3732924272 100644
--- a/parse.y
+++ b/parse.y
@@ -168,6 +168,7 @@ struct local_vars {
};
enum {
+ IMPLICIT_PARAM = -2,
ORDINAL_PARAM = -1,
NO_PARAM = 0,
NUMPARAM_MAX = 9,
@@ -8483,12 +8484,24 @@ static bool
parser_numbered_param(struct parser_params *p, unsigned long n)
{
if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
- compile_error(p, "numbered parameter outside block");
+ compile_error(p, "implicit parameter outside block");
return false;
}
if (p->max_numparam < NO_PARAM) {
- compile_error(p, "ordinary parameter is defined");
- return false;
+ if (p->max_numparam == ORDINAL_PARAM) {
+ compile_error(p, "ordinary parameter is defined");
+ return false;
+ }
+ else if (n > 0) {
+ compile_error(p, "implicit parameter is used");
+ return false;
+ }
+ }
+ else if (p->max_numparam > NO_PARAM) {
+ if (n == 0) {
+ compile_error(p, "numbered parameter is used");
+ return false;
+ }
}
set_yylval_name(numparam_id(p, (int)n));
SET_LEX_STATE(EXPR_ARG);
@@ -11228,10 +11241,15 @@ new_args_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, ID block,
static NODE *
args_with_numbered(struct parser_params *p, NODE *args, int max_numparam)
{
- if (max_numparam > NO_PARAM) {
+ if (max_numparam > NO_PARAM || max_numparam == IMPLICIT_PARAM) {
if (!args) args = new_args_tail(p, 0, 0, 0, 0);
- args->nd_ainfo->pre_args_num = max_numparam;
- args->nd_ainfo->rest_arg = NODE_SPECIAL_EXCESSIVE_COMMA;
+ if (max_numparam == IMPLICIT_PARAM) {
+ args->nd_ainfo->pre_args_num = 1;
+ }
+ else {
+ args->nd_ainfo->pre_args_num = max_numparam;
+ args->nd_ainfo->rest_arg = NODE_SPECIAL_EXCESSIVE_COMMA;
+ }
}
return args;
}
@@ -11240,8 +11258,12 @@ ID
rb_parser_numparam_id(struct parser_params *p, int idx)
{
struct vtable *args;
- if (idx <= 0) return (ID)0;
- if (p->max_numparam < idx) {
+ if (idx < 0) return (ID)0;
+ else if (idx == 0) {
+ p->max_numparam = IMPLICIT_PARAM;
+ idx = 1;
+ }
+ else if (p->max_numparam < idx) {
p->max_numparam = idx;
}
args = p->lvtbl->args;