def test_speaker_default(): speaker = Speaker(nodes={"BinaryExpr": "binary expression"}, fields={"left": "left part"}) node = parse("1 + 1") str_tmp = "The {field_name} of the {node_name}" assert speaker.describe(node, str_tmp, "left") == str_tmp.format( field_name="left part", node_name="binary expression")
def test_speaker_node_cfg(): node_cnfg = {"name": "binary expression", "fields": {"left": "left part"}} speaker = Speaker(nodes={"BinaryExpr": node_cnfg}, fields={"left": "should not occur!"}) node = parse("1 + 1") str_tmp = "The {field_name} of the {node_name}" assert speaker.describe(node, str_tmp, "left") == str_tmp.format( field_name="left part", node_name="binary expression")
def test_print_speaker(code, start): speaker = Speaker(**yaml.safe_load(open("antlr_plsql/speaker.yml"))) tree = ast.parse(code, start=start) print("\n\n{} -----------------------\n\n".format(tree.__class__.__name__)) # node printout print(speaker.describe(tree)) # fields for field_name in tree._fields: print( speaker.describe(tree, field=field_name, fmt="The {field_name} of the {node_name}"))
def speaker(): return Speaker( nodes={ "SelectStmt": "SELECT statement", "Call": "function call `{node.name}`" }, fields={"target_list": "target list"}, )
# TODO: port from remove_terminal: # - table_ref_list # Add visit methods to Transformer for all nodes (in _rules) that convert to AliasNode instances alias_nodes = get_alias_nodes(globals().values()) Transformer.bind_alias_nodes(alias_nodes) # Create Speaker speaker_cfg = yaml.safe_load(pkg_resources.resource_stream("antlr_plsql", "speaker.yml")) speaker = Speaker(**speaker_cfg) class AstVisitor(BaseAstVisitor): def visitTerminal(self, ctx): """Converts case insensitive keywords and identifiers to lowercase""" text = str(super().visitTerminal(ctx)) quotes = ["'", '"'] if not (text[0] in quotes and text[-1] in quotes): text = text.lower() return Terminal.from_text(text, ctx) if __name__ == "__main__": query = """ SELECT id FROM artists WHERE id > 100