summaryrefslogtreecommitdiff
path: root/parse.y
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2023-09-25 20:25:38 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2023-09-25 23:04:09 +0900
commit03ef85bee7a9982f4ed30639a52402074ccf4202 (patch)
tree9004bd01358fdb399d62e3d694a59cb4ff90d48c /parse.y
parent56604c7a3bb5ee8485baf84c100d5662a2666176 (diff)
Extract class/module context checks
Diffstat (limited to 'parse.y')
-rw-r--r--parse.y30
1 files changed, 15 insertions, 15 deletions
diff --git a/parse.y b/parse.y
index ee524d36ba..09ea763120 100644
--- a/parse.y
+++ b/parse.y
@@ -1378,6 +1378,18 @@ endless_method_name(struct parser_params *p, NODE *defn, const YYLTYPE *loc)
} \
} while (0)
+#define begin_definition(k, loc_beg, loc_end) \
+ do { \
+ if (!(p->ctxt.in_class = (k)[0] != 0)) { \
+ p->ctxt.in_def = 0; \
+ } \
+ else if (p->ctxt.in_def) { \
+ YYLTYPE loc = code_loc_gen(loc_beg, loc_end); \
+ yyerror1(&loc, k " definition in method body"); \
+ } \
+ local_push(p, 0); \
+ } while (0)
+
#ifndef RIPPER
# define Qnone 0
# define Qnull 0
@@ -3606,12 +3618,7 @@ primary : literal
}
| k_class cpath superclass
{
- if (p->ctxt.in_def) {
- YYLTYPE loc = code_loc_gen(&@1, &@2);
- yyerror1(&loc, "class definition in method body");
- }
- p->ctxt.in_class = 1;
- local_push(p, 0);
+ begin_definition("class", &@1, &@2);
}
bodystmt
k_end
@@ -3629,9 +3636,7 @@ primary : literal
}
| k_class tLSHFT expr
{
- p->ctxt.in_def = 0;
- p->ctxt.in_class = 0;
- local_push(p, 0);
+ begin_definition("", &@1, &@2);
}
term
bodystmt
@@ -3651,12 +3656,7 @@ primary : literal
}
| k_module cpath
{
- if (p->ctxt.in_def) {
- YYLTYPE loc = code_loc_gen(&@1, &@2);
- yyerror1(&loc, "module definition in method body");
- }
- p->ctxt.in_class = 1;
- local_push(p, 0);
+ begin_definition("module", &@1, &@2);
}
bodystmt
k_end