Compiler for converting AST nodes to TypeQL strings.
QueryCompiler
Compiles AST nodes into TypeQL strings.
compile
Compile a single node to TypeQL.
Source code in type_bridge/query/compiler.py
| def compile(self, node: QueryNode) -> str:
"""Compile a single node to TypeQL."""
# Try Rust compiler for Clause nodes
if _core_compiler is not None and isinstance(node, Clause):
try:
return _core_compiler.compile_dicts([_clause_to_dict(node)])
except Exception:
logger.debug("Rust compiler failed, falling back to Python", exc_info=True)
if isinstance(node, Clause):
return self._compile_clause(node)
elif isinstance(node, Pattern):
return self._compile_pattern(node)
elif isinstance(node, Statement):
return self._compile_statement(node)
elif isinstance(node, Constraint):
return self._compile_constraint(node)
elif isinstance(node, Value):
return self._compile_value(node)
else:
raise ValueError(f"Unknown node type: {type(node).__name__}")
|
compile_batch
compile_batch(nodes, operation='')
Compile a list of nodes into a single query string.
Parameters:
| Name |
Type |
Description |
Default |
nodes
|
list[QueryNode]
|
List of clauses to compile
|
required
|
operation
|
str
|
Optional operation keyword (e.g., "match", "insert") to prepend
if the nodes are just raw patterns/statements.
|
''
|
Source code in type_bridge/query/compiler.py
| def compile_batch(self, nodes: list[QueryNode], operation: str = "") -> str:
"""Compile a list of nodes into a single query string.
Args:
nodes: List of clauses to compile
operation: Optional operation keyword (e.g., "match", "insert") to prepend
if the nodes are just raw patterns/statements.
"""
# Try Rust compiler if all nodes are Clauses
if _core_compiler is not None and all(isinstance(n, Clause) for n in nodes):
try:
dicts = [_clause_to_dict(n) for n in nodes] # type: ignore[arg-type]
result = _core_compiler.compile_dicts(dicts)
if operation:
return f"{operation}\n{result}"
return result
except Exception:
logger.debug("Rust compiler failed, falling back to Python", exc_info=True)
parts = [self.compile(node) for node in nodes]
query = "\n".join(parts)
if operation:
return f"{operation}\n{query}"
return query
|