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