parse_tree.py 12.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
import collections
from textwrap import fill
from filters import *
try:
  # Python 2.7+
  basestring
except NameError:
  # Python 3.3+
  basestring = str

class ParseTree(object):
    """
    The ParseTree class produces a semantic tree of C++ definitions given
    the output of the CppHeaderParser (from opencv/modules/python/src2/hdr_parser.py)

    The full hierarchy is as follows:

      Namespaces
        |
        |- name
        |- Classes
            |
            |- name
            |- Methods
            |- Constants
        |- Methods
            |
            |- name
            |- static (T/F)
            |- return type
            |- required Arguments
                |
                |- name
                |- const (T/F)
                |- reference ('&'/'*')
                |- type
                |- input
                |- output (pass return by reference)
                |- default value
            |- optional Arguments
        |- Constants
            |
            |- name
            |- const (T/F)
            |- reference ('&'/'*')
            |- type
            |- value

    The semantic tree contains substantial information for easily introspecting
    information about objects. How many methods does the 'core' namespace have?
    Does the 'randn' method have any return by reference (output) arguments?
    How many required and optional arguments does the 'add' method have? Is the
    variable passed by reference or raw pointer?

    Individual definitions from the parse tree (Classes, Functions, Constants)
    are passed to the Jinja2 template engine where they are manipulated to
    produce Matlab mex sources.

    A common call tree for constructing and using a ParseTree object is:

      # parse a set of definitions into a dictionary of namespaces
      parser = CppHeaderParser()
      ns['core'] = parser.parse('path/to/opencv/core.hpp')

      # refactor into a semantic tree
      parse_tree = ParseTree()
      parse_tree.build(ns)

      # iterate over the tree
      for namespace in parse_tree.namespaces:
        for clss in namespace.classes:
          # do stuff
        for method in namespace.methods:
          # do stuff

    Calling 'print' on a ParseTree object will reconstruct the definitions
    to produce an output resembling the original C++ code.
    """
    def __init__(self, namespaces=None):
        self.namespaces = namespaces if namespaces else []

    def __str__(self):
        return '\n\n\n'.join(ns.__str__() for ns in self.namespaces)

    def build(self, namespaces):
        babel = Translator()
        for name, definitions in namespaces.items():
            class_tree = {}
            methods = []
            constants = []
            for defn in definitions:
                obj = babel.translate(defn)
                if obj is None:
                    continue
                if type(obj) is Class or obj.clss:
                    self.insertIntoClassTree(obj, class_tree)
                elif type(obj) is Method:
                    methods.append(obj)
                elif type(obj) is Constant:
                    constants.append(obj)
                else:
                    raise TypeError('Unexpected object type: '+str(type(obj)))
            self.namespaces.append(Namespace(name, constants, list(class_tree.values()), methods))

    def insertIntoClassTree(self, obj, class_tree):
        cname = obj.name if type(obj) is Class else obj.clss
        if not cname:
            return
        if not cname in class_tree:
          # add a new class to the tree
            class_tree[cname] = Class(cname)
        # insert the definition into the class
        val = class_tree[cname]
        if type(obj) is Method:
            val.methods.append(obj)
        elif type(obj) is Constant:
            val.constants.append(obj)
        else:
            raise TypeError('Unexpected object type: '+str(type(obj)))



class Translator(object):
    """
    The Translator class does the heavy lifting of translating the nested
    list representation of the hdr_parser into individual definitions that
    are inserted into the ParseTree.
    Translator consists of a top-level method: translate()
    along with a number of helper methods: translateClass(), translateMethod(),
    translateArgument(), translateConstant(), translateName(), and
    translateClassName()
    """
    def translate(self, defn):
        # --- class ---
        # classes have 'class' prefixed on their name
        if 'class' in defn[0].split(' ') or 'struct' in defn[0].split(' '):
            return self.translateClass(defn)
        # --- operators! ---
        #TODO: implement operators: http://www.mathworks.com.au/help/matlab/matlab_oop/implementing-operators-for-your-class.html
        if 'operator' in defn[0]:
            return
        # --- constant ---
        elif convertibleToInt(defn[1]):
            return self.translateConstant(defn)
        # --- function ---
        # functions either need to have input arguments, or not uppercase names
        elif defn[3] or not self.translateName(defn[0]).split('_')[0].isupper():
            return self.translateMethod(defn)
        # --- constant ---
        else:
            return self.translateConstant(defn)

    def translateClass(self, defn):
        return Class()

    def translateMethod(self, defn, class_tree=None):
        name = self.translateName(defn[0])
        clss = self.translateClassName(defn[0])
        rtp  = defn[1]
        static = True if 'S' in ''.join(defn[2]) else False
        args = defn[3]
        req  = []
        opt = []
        for arg in args:
            if arg:
                a = self.translateArgument(arg)
                opt.append(a) if a.default else req.append(a)
        return Method(name, clss, static, '', rtp, False, req, opt)

    def translateConstant(self, defn):
        const = True if 'const' in defn[0] else False
        name  = self.translateName(defn[0])
        clss  = self.translateClassName(defn[0])
        tp    = 'int'
        val   = defn[1]
        return Constant(name, clss, tp, const, '', val)

    def translateArgument(self, defn):
        ref   = '*' if '*' in defn[0] else ''
        ref   = '&' if '&' in defn[0] else ref
        const = ' const ' in ' '+defn[0]+' '
        tp    = " ".join([word for word in defn[0].replace(ref, '').split() if not ' const ' in ' '+word+' '])
        name = defn[1]
        default = defn[2] if defn[2] else ''
        modifiers = ''.join(defn[3])
        I = True if not modifiers or 'I' in modifiers else False
        O = True if 'O' in modifiers else False
        return Argument(name, tp, const, I, O, ref, default)

    def translateName(self, name):
        return name.split(' ')[-1].split('.')[-1]

    def translateClassName(self, name):
        name  = name.split(' ')[-1]
        parts = name.split('.')
        return parts[-2] if len(parts) > 1 and not parts[-2] == 'cv' else ''



class Namespace(object):
    """
    Namespace
      |
      |- name
      |- Constants
      |- Methods
      |- Constants
    """
    def __init__(self, name='', constants=None, classes=None, methods=None):
        self.name = name
        self.constants = constants if constants else []
        self.classes   = classes   if classes   else []
        self.methods = methods if methods else []

    def __str__(self):
        return 'namespace '+self.name+' {\n\n'+\
          ('\n'.join(c.__str__() for c in self.constants)+'\n\n' if self.constants else '')+\
          ('\n'.join(f.__str__() for f in self.methods)+'\n\n'   if self.methods   else '')+\
          ('\n\n'.join(o.__str__() for o in self.classes)        if self.classes   else '')+'\n};'

class Class(object):
    """
    Class
      |
      |- name
      |- Methods
      |- Constants
    """
    def __init__(self, name='', namespace='', constants=None, methods=None):
        self.name = name
        self.namespace = namespace
        self.constants = constants if constants else []
        self.methods = methods if methods else []

    def __str__(self):
        return 'class '+self.name+' {\n\t'+\
          ('\n\t'.join(c.__str__() for c in self.constants)+'\n\n\t' if self.constants else '')+\
          ('\n\t'.join(f.__str__() for f in self.methods)            if self.methods   else '')+'\n};'

class Method(object):
    """
    Method
    int VideoWriter::read( cv::Mat& frame, const cv::Mat& mask=cv::Mat() );
    ---    -----     ----     --------           ----------------
    rtp    class     name     required               optional

    name      the method name
    clss      the class the method belongs to ('' if free)
    static    static?
    namespace the namespace the method belongs to ('' if free)
    rtp       the return type
    const     const?
    req       list of required arguments
    opt       list of optional arguments
    """
    def __init__(self, name='', clss='', static=False, namespace='', rtp='', const=False, req=None, opt=None):
        self.name  = name
        self.clss  = clss
        self.constructor = True if name == clss else False
        self.static = static
        self.const = const
        self.namespace = namespace
        self.rtp = rtp
        self.req = req if req else []
        self.opt = opt if opt else []

    def __str__(self):
        return (self.rtp+' ' if self.rtp else '')+self.name+'('+\
          ', '.join(arg.__str__() for arg in self.req+self.opt)+\
          ')'+(' const' if self.const else '')+';'

class Argument(object):
    """
    Argument
    const cv::Mat&  mask=cv::Mat()
    -----  ---- --- ----  -------
    const   tp  ref name  default

    name    the argument name
    tp      the argument type
    const   const?
    I       is the argument treated as an input?
    O       is the argument treated as an output (return by reference)
    ref     is the argument passed by reference? ('*'/'&')
    default the default value of the argument ('' if required)
    """
    def __init__(self, name='', tp='', const=False, I=True, O=False, ref='', default=''):
        self.name = name
        self.tp   = tp
        self.ref  = ref
        self.I    = I
        self.O    = O
        self.const = const
        self.default = default

    def __str__(self):
        return ('const ' if self.const else '')+self.tp+self.ref+\
                ' '+self.name+('='+self.default if self.default else '')

class Constant(object):
    """
    Constant
    DFT_COMPLEX_OUTPUT = 12;
         ----          -------
         name          default

    name    the name of the constant
    clss    the class that the constant belongs to ('' if free)
    tp      the type of the constant ('' if int)
    const   const?
    ref     is the constant a reference? ('*'/'&')
    default default value, required for constants
    """
    def __init__(self, name='', clss='', tp='', const=False, ref='', default=''):
        self.name = name
        self.clss = clss
        self.tp   = tp
        self.ref  = ref
        self.const = const
        self.default = default

    def __str__(self):
        return ('const ' if self.const else '')+self.tp+self.ref+\
                ' '+self.name+('='+self.default if self.default else '')+';'

def constants(tree):
    """
    recursive generator to strip all Constant objects from the ParseTree
    and place them into a flat dictionary of { name, value (default) }
    """
    if isinstance(tree, dict) and 'constants' in tree and isinstance(tree['constants'], list):
        for node in tree['constants']:
            yield (node['name'], node['default'])
    if isinstance(tree, dict):
        for key, val in tree.items():
            for gen in constants(val):
                yield gen
    if isinstance(tree, list):
        for val in tree:
            for gen in constants(val):
                yield gen


def todict(obj):
    """
    Recursively convert a Python object graph to sequences (lists)
    and mappings (dicts) of primitives (bool, int, float, string, ...)
    """
    if isinstance(obj, basestring):
        return obj
    elif isinstance(obj, dict):
        return dict((key, todict(val)) for key, val in obj.items())
    elif isinstance(obj, collections.Iterable):
        return [todict(val) for val in obj]
    elif hasattr(obj, '__dict__'):
        return todict(vars(obj))
    elif hasattr(obj, '__slots__'):
        return todict(dict((name, getattr(obj, name)) for name in getattr(obj, '__slots__')))
    return obj