git-afsantos / bonsai

Simplified interface for syntax trees and program models.
MIT License
16 stars 8 forks source link

Improve Query Interface #1

Open git-afsantos opened 7 years ago

git-afsantos commented 7 years ago

First, and foremost, CodeQuery must provide support for other objects, besides references and function calls. It should also provide methods to look for functions, variables, classes, etc.

Secondly, it could use some readability improvements when specifying where clauses. There could be a class, WhereClause that is instantiated upon calling a single where property, after selecting the query objects (function, etc.). This helper class would provide methods to set the attributes that make sense, i.e., at least name and result. An alternative would be to instantiate a specific where class depending on the query objects, that would contain all the attributes that make sense.

Ideal example:

q = CodeQuery(root)
q.all_functions.where.name("main").result("int").get()
q.all_references.where.name("var").result("int").is_field_of.has_reference.get()
git-afsantos commented 4 years ago

A few ideas:

{
    "name": "advertise",
    "canonical_type": "ros::Publisher"
}

def q_function_calls(self, query_data):
    matches = []
    for obj in self._get_function_calls():
        for key, value in query_data.items():
            if hasattr(obj, key):
                if callable(value) and value(getattr(obj, key)):
                    matches.append(obj)
                elif getattr(obj, key) == value:
                    matches.append(obj)
    return matches