Skip to content

Commit 9f8c9c0

Browse files
committed
gh-153568: Reuse parsed numbers during backtracking
1 parent 674c781 commit 9f8c9c0

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
@@ -782,11 +782,19 @@ parsenumber(const char *s)
782782
expr_ty
783783
_PyPegen_number_token(Parser *p)
784784
{
785+
int mark = p->mark;
785786
Token *t = _PyPegen_expect_token(p, NUMBER);
786787
if (t == NULL) {
787788
return NULL;
788789
}
789790

791+
p->mark = mark;
792+
expr_ty cached = NULL;
793+
if (_PyPegen_is_memoized(p, NUMBER, &cached)) {
794+
return cached;
795+
}
796+
p->mark = mark + 1;
797+
790798
const char *num_raw = PyBytes_AsString(t->bytes);
791799
if (num_raw == NULL) {
792800
p->error_indicator = 1;
@@ -831,8 +839,13 @@ _PyPegen_number_token(Parser *p)
831839
return NULL;
832840
}
833841

834-
return _PyAST_Constant(c, NULL, t->lineno, t->col_offset, t->end_lineno,
835-
t->end_col_offset, p->arena);
842+
expr_ty result = _PyAST_Constant(c, NULL, t->lineno, t->col_offset,
843+
t->end_lineno, t->end_col_offset, p->arena);
844+
if (result != NULL && _PyPegen_insert_memo(p, mark, NUMBER, result) < 0) {
845+
p->error_indicator = 1;
846+
return NULL;
847+
}
848+
return result;
836849
}
837850

838851

0 commit comments

Comments
 (0)