8000 gh-124889: Remove redundant artificial rules in PEG parser by efimov-mikhail · Pull Request #124893 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-124889: Remove redundant artificial rules in PEG parser #124893

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

Merged
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
gh-124889: Remove redundant artificial rules, some refactoring (#124893)
Auxiliary method CCallMakerVisitor._generate_artificial_rule_call is added.
Its purpose is abstracting work with artificial rules cache.
  • Loading branch information
efimov-mikhail committed Oct 3, 2024
commit 0a8f197feec270ea4dd7533fae0c9e146ed62983
91 changes: 39 additions & 52 deletions Tools/peg_generator/pegen/c_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import re
from dataclasses import dataclass, field
from enum import Enum
from typing import IO, Any, Dict, List, Optional, Set, Text, Tuple
from typing import IO, Any, Callable, Dict, List, Optional, Set, Text, Tuple

from pegen import grammar
from pegen.grammar import (
Expand Down Expand Up @@ -206,25 +206,6 @@ def visit_StringLeaf(self, node: StringLeaf) -> FunctionCall:
comment=f"token='{val}'",
)

def visit_Rhs(self, node: Rhs) -> FunctionCall:
if node.can_be_inlined:
return self.generate_call(node.alts[0].items[0])

node_str = f"{node}"
key = f"rhs_{node_str}"
if key in self.cache:
name = self.cache[key]
else:
name = self.gen.artificial_rule_from_rhs(node)
self.cache[key] = name

return FunctionCall(
assigned_variable=f"{name}_var",
function=f"{name}_rule",
arguments=["p"],
comment=node_str,
)

def visit_NamedItem(self, node: NamedItem) -> FunctionCall:
call = self.generate_call(node.item)
if node.name:
Expand Down Expand Up @@ -306,55 +287,61 @@ def visit_Opt(self, node: Op 8000 t) -> FunctionCall:
comment=f"{node}",
)

def visit_Repeat0(self, node: Repeat0) -> FunctionCall:
def _generate_artificial_rule_call(
self,
node: Any,
prefix: str,
rule_generation_func: Callable[[], str],
return_type: Optional[str] = None,
) -> FunctionCall:
node_str = f"{node}"
key = f"repeat0_{node_str}"
key = f"{prefix}_{node_str}"
if key in self.cache:
name = self.cache[key]
else:
name = self.gen.artificial_rule_from_repeat(node.node, False)
name = rule_generation_func()
self.cache[key] = name

return FunctionCall(
assigned_variable=f"{name}_var",
function=f"{name}_rule",
arguments=["p"],
return_type="asdl_seq *",
return_type=return_type,
comment=node_str,
)

def visit_Repeat1(self, node: Repeat1) -> FunctionCall:
node_str = f"{node}"
key = f"repeat1_{node_str}"
if key in self.cache:
name = self.cache[key]
else:
name = self.gen.artificial_rule_from_repeat(node.node, True)
self.cache[key] = name
def visit_Rhs(self, node: Rhs) -> FunctionCall:
if node.can_be_inlined:
return self.generate_call(node.alts[0].items[0])

return FunctionCall(
assigned_variable=f"{name}_var",
function=f"{name}_rule",
arguments=["p"],
return_type="asdl_seq *",
comment=node_str,
return self._generate_artificial_rule_call(
node,
"rhs",
lambda: self.gen.artificial_rule_from_rhs(node),
)

def visit_Gather(self, node: Gather) -> FunctionCall:
node_str = f"{node}"
key = f"gather_{node_str}"
if key in self.cache:
name = self.cache[key]
else:
name = self.gen.artificial_rule_from_gather(node)
self.cache[key] = name
def visit_Repeat0(self, node: Repeat0) -> FunctionCall:
return self._generate_artificial_rule_call(
node,
"repeat0",
lambda: self.gen.artificial_rule_from_repeat(node.node, False),
"asdl_seq *",
)

return FunctionCall(
assigned_variable=f"{name}_var",
function=f"{name}_rule",
arguments=["p"],
return_type="asdl_seq *",
comment=node_str,
def visit_Repeat1(self, node: Repeat1) -> FunctionCall:
return self._generate_artificial_rule_call(
node,
"repeat1",
lambda: self.gen.artificial_rule_from_repeat(node.node, True),
"asdl_seq *",
)

def visit_Gather(self, node: Gather) -> FunctionCall:
return self._generate_artificial_rule_call(
node,
"gather",
lambda: self.gen.artificial_rule_from_gather(node),
"asdl_seq *",
)

def visit_Group(self, node: Group) -> FunctionCall:
Expand Down
Loading
0