in experimental/piranha_playground/rule_inference/static_inference.py [0:0]
def write_query(self, node: Node, depth=0, prefix="", simplify=False):
"""
Write a query for a given node, considering its depth and prefix.
:param node: The node for which the query will be written.
:param depth: The current depth of the node.
:param prefix: Prefix for the current node.
:param simplify: If True, simplify the query.
:return: The query string for this node.
"""
indent = " " * depth
cursor: TreeCursor = node.walk()
self.count += 1
node_repr = node.text.decode("utf8")
node_name = f"@tag{self.count}n"
if node_repr in self.template_holes:
# Fixed node
node_types = self.template_holes[node_repr]
if len(node_types) == 1:
s_exp = indent + f"{prefix}({node_types[0]})"
else:
alternations = " ".join([f"({node_type})" for node_type in node_types])
s_exp = indent + f"{prefix}[{alternations}]"
else:
# Regular node
node_type = node.type
s_exp = indent + f"{prefix}({node_type} "
next_child = cursor.goto_first_child()
visited = 0
while next_child:
if cursor.node.is_named:
visited += 1
s_exp += self.strategy.process_child(cursor, depth)
next_child = cursor.goto_next_sibling()
# if the node is an identifier, add it to eq constraints
if visited == 0:
text = node.text.decode("utf8").replace("\n", " ")
self.query_ctrs.append(f'(#eq? {node_name} "{text}")')
s_exp += f")"
self.capture_groups[node_name] = node
self.outer_most_node = node_name
return s_exp + f" {node_name}"