Skip to content

Commit 0cd6ee2

Browse files
committed
gh-153568: Reuse parsed numbers during backtracking
1 parent e517f90 commit 0cd6ee2

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

Parser/pegen.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -788,11 +788,19 @@ parsenumber(const char *s)
788788
expr_ty
789789
_PyPegen_number_token(Parser *p)
790790
{
791+
int mark = p->mark;
791792
Token *t = _PyPegen_expect_token(p, NUMBER);
792793
if (t == NULL) {
793794
return NULL;
794795
}
795796

797+
p->mark = mark;
798+
expr_ty cached = NULL;
799+
if (_PyPegen_is_memoized(p, NUMBER, &cached)) {
800+
return cached;
801+
}
802+
p->mark = mark + 1;
803+
796804
const char *num_raw = PyBytes_AsString(t->bytes);
797805
if (num_raw == NULL) {
798806
p->error_indicator = 1;
@@ -837,8 +845,13 @@ _PyPegen_number_token(Parser *p)
837845
return NULL;
838846
}
839847

840-
return _PyAST_Constant(c, NULL, t->lineno, t->col_offset, t->end_lineno,
841-
t->end_col_offset, p->arena);
848+
expr_ty result = _PyAST_Constant(c, NULL, t->lineno, t->col_offset,
849+
t->end_lineno, t->end_col_offset, p->arena);
850+
if (result != NULL && _PyPegen_insert_memo(p, mark, NUMBER, result) < 0) {
851+
p->error_indicator = 1;
852+
return NULL;
853+
}
854+
return result;
842855
}
843856

844857

0 commit comments

Comments
 (0)