bpo-40334: Generate comments in the parser code to improve debugging (GH-19966)

This commit is contained in:
Pablo Galindo 2020-05-06 23:14:43 +01:00 committed by GitHub
parent 99db2a1db7
commit 470aac4d8e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 1088 additions and 1074 deletions

File diff suppressed because it is too large Load diff

View file

@ -68,6 +68,7 @@ class FunctionCall:
return_type: Optional[str] = None
nodetype: Optional[NodeTypes] = None
force_true: bool = False
comment: Optional[str] = None
def __str__(self) -> str:
parts = []
@ -78,6 +79,8 @@ def __str__(self) -> str:
parts.append(", 1")
if self.assigned_variable:
parts = ["(", self.assigned_variable, " = ", *parts, ")"]
if self.comment:
parts.append(f" // {self.comment}")
return "".join(parts)
@ -103,6 +106,7 @@ def keyword_helper(self, keyword: str) -> FunctionCall:
arguments=["p", self.keyword_cache[keyword]],
return_type="Token *",
nodetype=NodeTypes.KEYWORD,
comment=f"token='{keyword}'",
)
def visit_NameLeaf(self, node: NameLeaf) -> FunctionCall:
@ -115,6 +119,7 @@ def visit_NameLeaf(self, node: NameLeaf) -> FunctionCall:
arguments=["p"],
nodetype=BASE_NODETYPES[name],
return_type="expr_ty",
comment=name,
)
return FunctionCall(
assigned_variable=f"{name.lower()}_var",
@ -122,6 +127,7 @@ def visit_NameLeaf(self, node: NameLeaf) -> FunctionCall:
arguments=["p", name],
nodetype=NodeTypes.GENERIC_TOKEN,
return_type="Token *",
comment=f"token='{name}'",
)
type = None
@ -134,6 +140,7 @@ def visit_NameLeaf(self, node: NameLeaf) -> FunctionCall:
function=f"{name}_rule",
arguments=["p"],
return_type=type,
comment=f"{node}"
)
def visit_StringLeaf(self, node: StringLeaf) -> FunctionCall:
@ -149,6 +156,7 @@ def visit_StringLeaf(self, node: StringLeaf) -> FunctionCall:
arguments=["p", type],
nodetype=NodeTypes.GENERIC_TOKEN,
return_type="Token *",
comment=f"token='{val}'",
)
def visit_Rhs(self, node: Rhs) -> FunctionCall:
@ -168,6 +176,7 @@ def can_we_inline(node: Rhs) -> int:
name = self.gen.name_node(node)
self.cache[node] = FunctionCall(
assigned_variable=f"{name}_var", function=f"{name}_rule", arguments=["p"],
comment=f"{node}"
)
return self.cache[node]
@ -190,6 +199,7 @@ def lookahead_call_helper(self, node: Lookahead, positive: int) -> FunctionCall:
function=f"_PyPegen_lookahead_with_int",
arguments=[positive, call.function, *call.arguments],
return_type="int",
comment=f"token={node.node}",
)
else:
return FunctionCall(
@ -211,6 +221,7 @@ def visit_Opt(self, node: Opt) -> FunctionCall:
function=call.function,
arguments=call.arguments,
force_true=True,
comment=f"{node}"
)
def visit_Repeat0(self, node: Repeat0) -> FunctionCall:
@ -222,6 +233,7 @@ def visit_Repeat0(self, node: Repeat0) -> FunctionCall:
function=f"{name}_rule",
arguments=["p"],
return_type="asdl_seq *",
comment=f"{node}",
)
return self.cache[node]
@ -234,6 +246,7 @@ def visit_Repeat1(self, node: Repeat1) -> FunctionCall:
function=f"{name}_rule",
arguments=["p"],
return_type="asdl_seq *",
comment=f"{node}",
)
return self.cache[node]
@ -246,6 +259,7 @@ def visit_Gather(self, node: Gather) -> FunctionCall:
function=f"{name}_rule",
arguments=["p"],
return_type="asdl_seq *",
comment=f"{node}",
)
return self.cache[node]