git-afsantos / bonsai

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

Templates not correctly parsed when not explicitly in tokens #12

Closed git-afsantos closed 5 years ago

git-afsantos commented 5 years ago

When calling some functions (e.g. constructors without the new operator), sometimes the templates are not parsed, because they do not appear explicitly in the text tokens. However, they are available in the type information. E.g. (exceprt):

#include <ros/ros.h>
#include <actionlib/server/simple_action_server.h>
#include <actionlib_tutorials/FibonacciAction.h>

class FibonacciAction {
protected:
  ros::NodeHandle nh_;
  actionlib::SimpleActionServer<actionlib_tutorials::FibonacciAction> as_;
  std::string action_name_;
public:
  FibonacciAction(std::string name) :
    as_(nh_, name, boost::bind(&FibonacciAction::executeCB, this, _1), false),
    action_name_(name)
  {
    as_.start();
  }

When parsing the code above, specifically the line

    as_(nh_, name, boost::bind(&FibonacciAction::executeCB, this, _1), false),

The type of the expression is correctly identified as actionlib::SimpleActionServer<actionlib_tutorials::FibonacciAction>, even though the templates are not explicit in the tokens of the function call.

The current version of the parser only looks at the tokens, as seen below.

    def _parse_templates(self, name, tokens):
        templates = []
        text = "".join(tokens)
        start = text.find("<")
        if (start >= 0 and not "<" in name and not ">" in name
                       and text[:start].endswith(name)):
            matches = 1
            i = start + 1
            while matches > 0 and i < len(text):
                if text[i] == "<":
                    matches += 1
                elif text[i] == ">":
                    matches -= 1
                elif text[i] == "," and matches == 1:
                    templates.append(text[start+1:i])
                    start = i
                i += 1
            templates.append(text[start+1:i-1])
        return tuple(templates)

This function should be modified and reused to search for templates in type information when it is not available in the tokens.