Skip to content

Commit 47c5a0f

Browse files
authored
pythongh-125588: Allow to regenerate the parser with Python < 3.12 (python#127969)
Signed-off-by: Pablo Galindo <[email protected]>
1 parent b74c8f5 commit 47c5a0f

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

Tools/peg_generator/pegen/parser.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ def string(self) -> Optional[tokenize.TokenInfo]:
207207

208208
@memoize
209209
def fstring_start(self) -> Optional[tokenize.TokenInfo]:
210-
FSTRING_START = getattr(token, "FSTRING_START")
210+
FSTRING_START = getattr(token, "FSTRING_START", None)
211211
if not FSTRING_START:
212212
return None
213213
tok = self._tokenizer.peek()
@@ -217,7 +217,7 @@ def fstring_start(self) -> Optional[tokenize.TokenInfo]:
217217

218218
@memoize
219219
def fstring_middle(self) -> Optional[tokenize.TokenInfo]:
220-
FSTRING_MIDDLE = getattr(token, "FSTRING_MIDDLE")
220+
FSTRING_MIDDLE = getattr(token, "FSTRING_MIDDLE", None)
221221
if not FSTRING_MIDDLE:
222222
return None
223223
tok = self._tokenizer.peek()
@@ -227,7 +227,7 @@ def fstring_middle(self) -> Optional[tokenize.TokenInfo]:
227227

228228
@memoize
229229
def fstring_end(self) -> Optional[tokenize.TokenInfo]:
230-
FSTRING_END = getattr(token, "FSTRING_END")
230+
FSTRING_END = getattr(token, "FSTRING_END", None)
231231
if not FSTRING_END:
232232
return None
233233
tok = self._tokenizer.peek()

Tools/peg_generator/pegen/parser_generator.py

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sys
12
import ast
23
import contextlib
34
import re
@@ -75,6 +76,11 @@ class RuleCheckingVisitor(GrammarVisitor):
7576
def __init__(self, rules: Dict[str, Rule], tokens: Set[str]):
7677
self.rules = rules
7778
self.tokens = tokens
79+
# If python < 3.12 add the virtual fstring tokens
80+
if sys.version_info < (3, 12):
81+
self.tokens.add("FSTRING_START")
82+
self.tokens.add("FSTRING_END")
83+
self.tokens.add("FSTRING_MIDDLE")
7884

7985
def visit_NameLeaf(self, node: NameLeaf) -> None:
8086
if node.value not in self.rules and node.value not in self.tokens:

0 commit comments

Comments
 (0)