Skip to content

Commit

Permalink
pythongh-125588: Allow to regenerate the parser with Python < 3.12 (p…
Browse files Browse the repository at this point in the history
…ython#127969)

Signed-off-by: Pablo Galindo <[email protected]>
  • Loading branch information
pablogsal authored Dec 15, 2024
1 parent b74c8f5 commit 47c5a0f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Tools/peg_generator/pegen/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def string(self) -> Optional[tokenize.TokenInfo]:

@memoize
def fstring_start(self) -> Optional[tokenize.TokenInfo]:
FSTRING_START = getattr(token, "FSTRING_START")
FSTRING_START = getattr(token, "FSTRING_START", None)
if not FSTRING_START:
return None
tok = self._tokenizer.peek()
Expand All @@ -217,7 +217,7 @@ def fstring_start(self) -> Optional[tokenize.TokenInfo]:

@memoize
def fstring_middle(self) -> Optional[tokenize.TokenInfo]:
FSTRING_MIDDLE = getattr(token, "FSTRING_MIDDLE")
FSTRING_MIDDLE = getattr(token, "FSTRING_MIDDLE", None)
if not FSTRING_MIDDLE:
return None
tok = self._tokenizer.peek()
Expand All @@ -227,7 +227,7 @@ def fstring_middle(self) -> Optional[tokenize.TokenInfo]:

@memoize
def fstring_end(self) -> Optional[tokenize.TokenInfo]:
FSTRING_END = getattr(token, "FSTRING_END")
FSTRING_END = getattr(token, "FSTRING_END", None)
if not FSTRING_END:
return None
tok = self._tokenizer.peek()
Expand Down
6 changes: 6 additions & 0 deletions Tools/peg_generator/pegen/parser_generator.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
import ast
import contextlib
import re
Expand Down Expand Up @@ -75,6 +76,11 @@ class RuleCheckingVisitor(GrammarVisitor):
def __init__(self, rules: Dict[str, Rule], tokens: Set[str]):
self.rules = rules
self.tokens = tokens
# If python < 3.12 add the virtual fstring tokens
if sys.version_info < (3, 12):
self.tokens.add("FSTRING_START")
self.tokens.add("FSTRING_END")
self.tokens.add("FSTRING_MIDDLE")

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

0 comments on commit 47c5a0f

Please sign in to comment.