Skip to content

Refactor error handling code in Parser/pegen/pegen.c #20440

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 27, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions Parser/pegen/pegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -756,24 +756,18 @@ _PyPegen_expect_token(Parser *p, int type)
expr_ty
_PyPegen_expect_soft_keyword(Parser *p, const char *keyword)
{
if (p->mark == p->fill) {
if (_PyPegen_fill_token(p) < 0) {
p->error_indicator = 1;
return NULL;
}
}
Token *t = p->tokens[p->mark];
if (t->type != NAME) {
expr_ty res = _PyPegen_name_token(p);
if (!res) {
return NULL;
}
char* s = PyBytes_AsString(t->bytes);
const char *s = PyUnicode_AsUTF8(res->v.Name.id);
if (!s) {
p->error_indicator = 1;
return NULL;
}
if (strcmp(s, keyword) != 0) {
return NULL;
}
expr_ty res = _PyPegen_name_token(p);
return res;
}

Expand All @@ -800,10 +794,12 @@ _PyPegen_name_token(Parser *p)
}
char* s = PyBytes_AsString(t->bytes);
if (!s) {
p->error_indicator = 1;
return NULL;
}
PyObject *id = _PyPegen_new_identifier(p, s);
if (id == NULL) {
p->error_indicator = 1;
return NULL;
}
return Name(id, Load, t->lineno, t->col_offset, t->end_lineno, t->end_col_offset,
Expand Down Expand Up @@ -896,6 +892,7 @@ _PyPegen_number_token(Parser *p)

char *num_raw = PyBytes_AsString(t->bytes);
if (num_raw == NULL) {
p->error_indicator = 1;
return NULL;
}

Expand All @@ -908,11 +905,13 @@ _PyPegen_number_token(Parser *p)
PyObject *c = parsenumber(num_raw);

if (c == NULL) {
p->error_indicator = 1;
return NULL;
}

if (PyArena_AddPyObject(p->arena, c) < 0) {
Py_DECREF(c);
p->error_indicator = 1;
return NULL;
}

Expand Down