8000 bpo-40334: Support Python 3.6 in the PEG generator by serhiy-storchaka · Pull Request #19786 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-40334: Support Python 3.6 in the PEG generator #19786

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

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions Tools/peg_generator/pegen/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def main() -> None:


if __name__ == "__main__":
if sys.version_info < (3, 8):
print("ERROR: using pegen requires at least Python 3.8!", file=sys.stderr)
if sys.version_info < (3, 6):
print("ERROR: using pegen requires at least Python 3.6!", file=sys.stderr)
sys.exit(1)
main()
52 changes: 25 additions & 27 deletions Tools/peg_generator/pegen/grammar.py
EDBE
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import annotations

from abc import abstractmethod
from typing import (
AbstractSet,
Expand Down Expand Up @@ -45,7 +43,7 @@ def generic_visit(self, node: Iterable[Any], *args: Any, **kwargs: Any) -> None:


class Grammar:
def __init__(self, rules: Iterable[Rule], metas: Iterable[Tuple[str, Optional[str]]]):
def __init__(self, rules: Iterable['Rule'], metas: Iterable[Tuple[str, Optional[str]]]):
self.rules = {rule.name: rule for rule in rules}
self.metas = dict(metas)

Expand All @@ -62,7 +60,7 @@ def __repr__(self) -> str:
lines.append(")")
return "\n".join(lines)

def __iter__(self) -> Iterator[Rule]:
def __iter__(self) -> Iterator['Rule']:
yield from self.rules.values()


Expand All @@ -71,7 +69,7 @@ def __iter__(self) -> Iterator[Rule]:


class Rule:
def __init__(self, name: str, type: Optional[str], rhs: Rhs, memo: Optional[object] = None):
def __init__(self, name: str, type: Optional[str], rhs: 'Rhs', memo: Optional[object] = None):
self.name = name
self.type = type
self.rhs = rhs
Expand Down Expand Up @@ -101,10 +99,10 @@ def __str__(self) -> str:
def __repr__(self) -> str:
return f"Rule({self.name!r}, {self.type!r}, {self.rhs!r})"

def __iter__(self) -> Iterator[Rhs]:
def __iter__(self) -> Iterator['Rhs']:
yield self.rhs

def nullable_visit(self, rules: Dict[str, Rule]) -> bool:
def nullable_visit(self, rules: Dict[str, 'Rule']) -> bool:
if self.visited:
# A left-recursive rule is considered non-nullable.
return False
Expand All @@ -115,7 +113,7 @@ def nullable_visit(self, rules: Dict[str, Rule]) -> bool:
def initial_names(self) -> AbstractSet[str]:
return self.rhs.initial_names()

def flatten(self) -> Rhs:
def flatten(self) -> 'Rhs':
# If it's a single parenthesized group, flatten it.
rhs = self.rhs
if (
Expand All @@ -127,7 +125,7 @@ def flatten(self) -> Rhs:
rhs = rhs.alts[0].items[0].item.rhs
return rhs

def collect_todo(self, gen: ParserGenerator) -> None:
def collect_todo(self, gen: 'ParserGenerator') -> None:
rhs = self.flatten()
rhs.collect_todo(gen)

Expand Down Expand Up @@ -188,7 +186,7 @@ def initial_names(self) -> AbstractSet[str]:


class Rhs:
def __init__(self, alts: List[Alt]):
def __init__(self, alts: List['Alt']):
self.alts = alts
self.memo: Optional[Tuple[Optional[str], str]] = None

Expand All @@ -198,7 +196,7 @@ def __str__(self) -> str:
def __repr__(self) -> str:
return f"Rhs({self.alts!r})"

def __iter__(self) -> Iterator[List[Alt]]:
def __iter__(self) -> Iterator[List['Alt']]:
yield self.alts

def nullable_visit(self, rules: Dict[str, Rule]) -> bool:
Expand All @@ -213,13 +211,13 @@ def initial_names(self) -> AbstractSet[str]:
names |= alt.initial_names()
return names

def collect_todo(self, gen: ParserGenerator) -> None:
def collect_todo(self, gen: 'ParserGenerator') -> None:
for alt in self.alts:
alt.collect_todo(gen)


class Alt:
def __init__(self, items: List[NamedItem], *, icut: int = -1, action: Optional[str] = None):
def __init__(self, items: List['NamedItem'], *, icut: int = -1, action: Optional[str] = None):
self.items = items
self.icut = icut
self.action = action
Expand All @@ -239,7 +237,7 @@ def __repr__(self) -> str:
args.append(f"action={self.action!r}")
return f"Alt({', '.join(args)})"

def __iter__(self) -> Iterator[List[NamedItem]]:
def __iter__(self) -> Iterator[List['NamedItem']]:
yield self.items

def nullable_visit(self, rules: Dict[str, Rule]) -> bool:
Expand All @@ -256,13 +254,13 @@ def initial_names(self) -> AbstractSet[str]:
break
return names

def collect_todo(self, gen: ParserGenerator) -> None:
def collect_todo(self, gen: 'ParserGenerator') -> None:
for item in self.items:
item.collect_todo(gen)


class NamedItem:
def __init__(self, name: Optional[str], item: Item):
def __init__(self, name: Optional[str], item: 'Item'):
self.name = name
self.item = item
self.nullable = False
Expand All @@ -276,7 +274,7 @@ def __str__(self) -> str:
def __repr__(self) -> str:
return f"NamedItem({self.name!r}, {self.item!r})"

def __iter__(self) -> Iterator[Item]:
def __iter__(self) -> Iterator['Item']:
yield self.item

def nullable_visit(self, rules: Dict[str, Rule]) -> bool:
Expand All @@ -286,19 +284,19 @@ def nullable_visit(self, rules: Dict[str, Rule]) -> bool:
def initial_names(self) -> AbstractSet[str]:
return self.item.initial_names()

def collect_todo(self, gen: ParserGenerator) -> None:
def collect_todo(self, gen: 'ParserGenerator') -> None:
gen.callmakervisitor.visit(self.item)


class Lookahead:
def __init__(self, node: Plain, sign: str):
def __init__(self, node: 'Plain', sign: str):
self.node = node
self.sign = sign

def __str__(self) -> str:
return f"{self.sign}{self.node}"

def __iter__(self) -> Iterator[Plain]:
def __iter__(self) -> Iterator['Plain']:
yield self.node

def nullable_visit(self, rules: Dict[str, Rule]) -> bool:
Expand All @@ -309,23 +307,23 @@ def initial_names(self) -> AbstractSet[str]:


class PositiveLookahead(Lookahead):
def __init__(self, node: Plain):
def __init__(self, node: 'Plain'):
super().__init__(node, "&")

def __repr__(self) -> str:
return f"PositiveLookahead({self.node!r})"


class NegativeLookahead(Lookahead):
def __init__(self, node: Plain):
def __init__(self, node: 'Plain'):
super().__init__(node, "!")

def __repr__(self) -> str:
return f"NegativeLookahead({self.node!r})"


class Opt:
def __init__(self, node: Item):
def __init__(self, node: 'Item'):
self.node = node

def __str__(self) -> str:
Expand All @@ -339,7 +337,7 @@ def __str__(self) -> str:
def __repr__(self) -> str:
return f"Opt({self.node!r})"

def __iter__(self) -> Iterator[Item]:
def __iter__(self) -> Iterator['Item']:
yield self.node

def nullable_visit(self, rules: Dict[str, Rule]) -> bool:
Expand All @@ -352,15 +350,15 @@ def initial_names(self) -> AbstractSet[str]:
class Repeat:
"""Shared base class for x* and x+."""

def __init__(self, node: Plain):
def __init__(self, node: 'Plain'):
self.node = node
self.memo: Optional[Tuple[Optional[str], str]] = None

@abstractmethod
def nullable_visit(self, rules: Dict[str, Rule]) -> bool:
raise NotImplementedError

def __iter__(self) -> Iterator[Plain]:
def __iter__(self) -> Iterator['Plain']:
yield self.node

def initial_names(self) -> AbstractSet[str]:
Expand Down Expand Up @@ -400,7 +398,7 @@ def nullable_visit(self, rules: Dict[str, Rule]) -> bool:


class Gather(Repeat):
def __init__(self, separator: Plain, node: Plain):
def __init__(self, separator: 'Plain', node: 'Plain'):
self.separator = separator
self.node = node

Expand Down
Loading
0