Skip to content

Commit b596bba

Browse files
committed
Deduplicate code with macro
1 parent d385c29 commit b596bba

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

Parser/pegen/pegen.c

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2063,27 +2063,25 @@ _PyPegen_get_invalid_target(expr_ty e)
20632063
if (e == NULL) {
20642064
return NULL;
20652065
}
2066+
2067+
#define VISIT_CONTAINER(CONTAINER, TYPE) do { \
2068+
Py_ssize_t len = asdl_seq_LEN(CONTAINER->v.TYPE.elts);\
2069+
for (Py_ssize_t i = 0; i < len; i++) {\
2070+
expr_ty other = asdl_seq_GET(CONTAINER->v.TYPE.elts, i);\
2071+
expr_ty child = _PyPegen_get_invalid_target(other);\
2072+
if (child != NULL) {\
2073+
return child;\
2074+
}\
2075+
}\
2076+
} while (0)
2077+
20662078
switch (e->kind) {
20672079
case List_kind: {
2068-
Py_ssize_t len = asdl_seq_LEN(e->v.List.elts);
2069-
for (Py_ssize_t i = 0; i < len; i++) {
2070-
expr_ty other = asdl_seq_GET(e->v.List.elts, i);
2071-
expr_ty child = _PyPegen_get_invalid_target(other);
2072-
if (child != NULL) {
2073-
return child;
2074-
}
2075-
}
2080+
VISIT_CONTAINER(e, List);
20762081
return NULL;
20772082
}
20782083
case Tuple_kind: {
2079-
Py_ssize_t len = asdl_seq_LEN(e->v.Tuple.elts);
2080-
for (Py_ssize_t i = 0; i < len; i++) {
2081-
expr_ty other = asdl_seq_GET(e->v.Tuple.elts, i);
2082-
expr_ty child = _PyPegen_get_invalid_target(other);
2083-
if (child != NULL) {
2084-
return child;
2085-
}
2086-
}
2084+
VISIT_CONTAINER(e, Tuple);
20872085
return NULL;
20882086
}
20892087
case Starred_kind:

0 commit comments

Comments
 (0)