summaryrefslogtreecommitdiffstats
path: root/Parser
diff options
context:
space:
mode:
authorPablo Galindo <Pablogsal@gmail.com>2021-04-08 23:48:53 (GMT)
committerGitHub <noreply@github.com>2021-04-08 23:48:53 (GMT)
commit4f642dae4ef9cb88673971bb5c9eff97d5675a0e (patch)
tree9569cd9d9da21290ecbc9bcf029b4adaa89ec209 /Parser
parentc0e11a3ceb9427e09db4224f394c7789bf6deec5 (diff)
downloadcpython-4f642dae4ef9cb88673971bb5c9eff97d5675a0e.zip
cpython-4f642dae4ef9cb88673971bb5c9eff97d5675a0e.tar.gz
cpython-4f642dae4ef9cb88673971bb5c9eff97d5675a0e.tar.bz2
Break down some complex functions in pegen.c for readability (GH-25292)
Diffstat (limited to 'Parser')
-rw-r--r--Parser/pegen.c170
1 files changed, 91 insertions, 79 deletions
diff --git a/Parser/pegen.c b/Parser/pegen.c
index 7b5a5e9..729b747 100644
--- a/Parser/pegen.c
+++ b/Parser/pegen.c
@@ -1856,136 +1856,147 @@ _get_defaults(Parser *p, asdl_seq *names_with_defaults)
return seq;
}
-/* Constructs an arguments_ty object out of all the parsed constructs in the parameters rule */
-arguments_ty
-_PyPegen_make_arguments(Parser *p, asdl_arg_seq *slash_without_default,
- SlashWithDefault *slash_with_default, asdl_arg_seq *plain_names,
- asdl_seq *names_with_default, StarEtc *star_etc)
-{
- asdl_arg_seq *posonlyargs;
+static int
+_make_posonlyargs(Parser *p,
+ asdl_arg_seq *slash_without_default,
+ SlashWithDefault *slash_with_default,
+ asdl_arg_seq **posonlyargs) {
if (slash_without_default != NULL) {
- posonlyargs = slash_without_default;
+ *posonlyargs = slash_without_default;
}
else if (slash_with_default != NULL) {
asdl_arg_seq *slash_with_default_names =
- _get_names(p, slash_with_default->names_with_defaults);
+ _get_names(p, slash_with_default->names_with_defaults);
if (!slash_with_default_names) {
- return NULL;
+ return -1;
}
- posonlyargs = (asdl_arg_seq*)_PyPegen_join_sequences(
+ *posonlyargs = (asdl_arg_seq*)_PyPegen_join_sequences(
p,
(asdl_seq*)slash_with_default->plain_names,
(asdl_seq*)slash_with_default_names);
- if (!posonlyargs) {
- return NULL;
- }
}
else {
- posonlyargs = _Py_asdl_arg_seq_new(0, p->arena);
- if (!posonlyargs) {
- return NULL;
- }
+ *posonlyargs = _Py_asdl_arg_seq_new(0, p->arena);
}
+ return *posonlyargs == NULL ? -1 : 0;
+}
- asdl_arg_seq *posargs;
+static int
+_make_posargs(Parser *p,
+ asdl_arg_seq *plain_names,
+ asdl_seq *names_with_default,
+ asdl_arg_seq **posargs) {
if (plain_names != NULL && names_with_default != NULL) {
asdl_arg_seq *names_with_default_names = _get_names(p, names_with_default);
if (!names_with_default_names) {
- return NULL;
- }
- posargs = (asdl_arg_seq*)_PyPegen_join_sequences(
- p,
- (asdl_seq*)plain_names,
- (asdl_seq*)names_with_default_names);
- if (!posargs) {
- return NULL;
+ return -1;
}
+ *posargs = (asdl_arg_seq*)_PyPegen_join_sequences(
+ p,(asdl_seq*)plain_names, (asdl_seq*)names_with_default_names);
}
else if (plain_names == NULL && names_with_default != NULL) {
- posargs = _get_names(p, names_with_default);
- if (!posargs) {
- return NULL;
- }
+ *posargs = _get_names(p, names_with_default);
}
else if (plain_names != NULL && names_with_default == NULL) {
- posargs = plain_names;
+ *posargs = plain_names;
}
else {
- posargs = _Py_asdl_arg_seq_new(0, p->arena);
- if (!posargs) {
- return NULL;
- }
+ *posargs = _Py_asdl_arg_seq_new(0, p->arena);
}
+ return *posargs == NULL ? -1 : 0;
+}
- asdl_expr_seq *posdefaults;
+static int
+_make_posdefaults(Parser *p,
+ SlashWithDefault *slash_with_default,
+ asdl_seq *names_with_default,
+ asdl_expr_seq **posdefaults) {
if (slash_with_default != NULL && names_with_default != NULL) {
asdl_expr_seq *slash_with_default_values =
- _get_defaults(p, slash_with_default->names_with_defaults);
+ _get_defaults(p, slash_with_default->names_with_defaults);
if (!slash_with_default_values) {
- return NULL;
+ return -1;
}
asdl_expr_seq *names_with_default_values = _get_defaults(p, names_with_default);
if (!names_with_default_values) {
- return NULL;
+ return -1;
}
- posdefaults = (asdl_expr_seq*)_PyPegen_join_sequences(
+ *posdefaults = (asdl_expr_seq*)_PyPegen_join_sequences(
p,
(asdl_seq*)slash_with_default_values,
(asdl_seq*)names_with_default_values);
- if (!posdefaults) {
- return NULL;
- }
}
else if (slash_with_default == NULL && names_with_default != NULL) {
- posdefaults = _get_defaults(p, names_with_default);
- if (!posdefaults) {
- return NULL;
- }
+ *posdefaults = _get_defaults(p, names_with_default);
}
else if (slash_with_default != NULL && names_with_default == NULL) {
- posdefaults = _get_defaults(p, slash_with_default->names_with_defaults);
- if (!posdefaults) {
- return NULL;
- }
+ *posdefaults = _get_defaults(p, slash_with_default->names_with_defaults);
}
else {
- posdefaults = _Py_asdl_expr_seq_new(0, p->arena);
- if (!posdefaults) {
- return NULL;
- }
- }
-
- arg_ty vararg = NULL;
- if (star_etc != NULL && star_etc->vararg != NULL) {
- vararg = star_etc->vararg;
+ *posdefaults = _Py_asdl_expr_seq_new(0, p->arena);
}
+ return *posdefaults == NULL ? -1 : 0;
+}
- asdl_arg_seq *kwonlyargs;
+static int
+_make_kwargs(Parser *p, StarEtc *star_etc,
+ asdl_arg_seq **kwonlyargs,
+ asdl_expr_seq **kwdefaults) {
if (star_etc != NULL && star_etc->kwonlyargs != NULL) {
- kwonlyargs = _get_names(p, star_etc->kwonlyargs);
- if (!kwonlyargs) {
- return NULL;
- }
+ *kwonlyargs = _get_names(p, star_etc->kwonlyargs);
}
else {
- kwonlyargs = _Py_asdl_arg_seq_new(0, p->arena);
- if (!kwonlyargs) {
- return NULL;
- }
+ *kwonlyargs = _Py_asdl_arg_seq_new(0, p->arena);
+ }
+
+ if (*kwonlyargs == NULL) {
+ return -1;
}
- asdl_expr_seq *kwdefaults;
if (star_etc != NULL && star_etc->kwonlyargs != NULL) {
- kwdefaults = _get_defaults(p, star_etc->kwonlyargs);
- if (!kwdefaults) {
- return NULL;
- }
+ *kwdefaults = _get_defaults(p, star_etc->kwonlyargs);
}
else {
- kwdefaults = _Py_asdl_expr_seq_new(0, p->arena);
- if (!kwdefaults) {
- return NULL;
- }
+ *kwdefaults = _Py_asdl_expr_seq_new(0, p->arena);
+ }
+
+ if (*kwdefaults == NULL) {
+ return -1;
+ }
+
+ return 0;
+}
+
+/* Constructs an arguments_ty object out of all the parsed constructs in the parameters rule */
+arguments_ty
+_PyPegen_make_arguments(Parser *p, asdl_arg_seq *slash_without_default,
+ SlashWithDefault *slash_with_default, asdl_arg_seq *plain_names,
+ asdl_seq *names_with_default, StarEtc *star_etc)
+{
+ asdl_arg_seq *posonlyargs;
+ if (_make_posonlyargs(p, slash_without_default, slash_with_default, &posonlyargs) == -1) {
+ return NULL;
+ }
+
+ asdl_arg_seq *posargs;
+ if (_make_posargs(p, plain_names, names_with_default, &posargs) == -1) {
+ return NULL;
+ }
+
+ asdl_expr_seq *posdefaults;
+ if (_make_posdefaults(p,slash_with_default, names_with_default, &posdefaults) == -1) {
+ return NULL;
+ }
+
+ arg_ty vararg = NULL;
+ if (star_etc != NULL && star_etc->vararg != NULL) {
+ vararg = star_etc->vararg;
+ }
+
+ asdl_arg_seq *kwonlyargs;
+ asdl_expr_seq *kwdefaults;
+ if (_make_kwargs(p, star_etc, &kwonlyargs, &kwdefaults) == -1) {
+ return NULL;
}
arg_ty kwarg = NULL;
@@ -1997,6 +2008,7 @@ _PyPegen_make_arguments(Parser *p, asdl_arg_seq *slash_without_default,
kwdefaults, kwarg, posdefaults, p->arena);
}
+
/* Constructs an empty arguments_ty object, that gets used when a function accepts no
* arguments. */
arguments_ty