2013-03-14 15:26:18 +08:00
|
|
|
from string import join
|
|
|
|
from textwrap import fill
|
2013-06-27 15:25:48 +08:00
|
|
|
from filters import *
|
2013-03-14 15:26:18 +08:00
|
|
|
|
|
|
|
class ParseTree(object):
|
2013-03-18 10:37:42 +08:00
|
|
|
def __init__(self, namespaces=None):
|
|
|
|
self.namespaces = namespaces if namespaces else []
|
2013-03-14 15:26:18 +08:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return join((ns.__str__() for ns in self.namespaces), '\n\n\n')
|
|
|
|
|
|
|
|
def build(self, namespaces):
|
|
|
|
babel = Translator()
|
|
|
|
for name, definitions in namespaces.items():
|
|
|
|
class_tree = {}
|
|
|
|
functions = []
|
|
|
|
constants = []
|
|
|
|
for defn in definitions:
|
|
|
|
obj = babel.translate(defn)
|
|
|
|
if type(obj) is Class or obj.clss:
|
|
|
|
self.insertIntoClassTree(obj, class_tree)
|
|
|
|
elif type(obj) is Function:
|
|
|
|
functions.append(obj)
|
|
|
|
elif type(obj) is Constant:
|
|
|
|
constants.append(obj)
|
|
|
|
else:
|
|
|
|
raise TypeError('Unexpected object type: '+str(type(obj)))
|
2013-03-18 10:37:42 +08:00
|
|
|
self.namespaces.append(Namespace(name, constants, class_tree.values(), functions))
|
2013-03-14 15:26:18 +08:00
|
|
|
|
|
|
|
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 Function:
|
|
|
|
val.functions.append(obj)
|
|
|
|
elif type(obj) is Constant:
|
|
|
|
val.constants.append(obj)
|
|
|
|
else:
|
|
|
|
raise TypeError('Unexpected object type: '+str(type(obj)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Translator(object):
|
|
|
|
def translate(self, defn):
|
|
|
|
# --- class ---
|
|
|
|
# classes have 'class' prefixed on their name
|
2013-06-28 02:45:19 +08:00
|
|
|
if 'class' in defn[0].split(' ') or 'struct' in defn[0].split(' '):
|
2013-03-14 15:26:18 +08:00
|
|
|
return self.translateClass(defn)
|
|
|
|
# --- function ---
|
|
|
|
# functions either need to have input arguments, or not uppercase names
|
2013-06-27 15:25:48 +08:00
|
|
|
elif defn[3] or not self.translateName(defn[0]).split('_')[0].isupper():
|
2013-03-14 15:26:18 +08:00
|
|
|
return self.translateFunction(defn)
|
|
|
|
# --- constant ---
|
|
|
|
else:
|
|
|
|
return self.translateConstant(defn)
|
|
|
|
|
|
|
|
def translateClass(self, defn):
|
|
|
|
return Class()
|
|
|
|
|
|
|
|
def translateFunction(self, defn, class_tree=None):
|
|
|
|
name = self.translateName(defn[0])
|
|
|
|
clss = self.translateClassName(defn[0])
|
|
|
|
rtp = defn[1]
|
2013-06-22 13:55:48 +08:00
|
|
|
static = True if 'S' in ''.join(defn[2]) else False
|
2013-03-14 15:26:18 +08:00
|
|
|
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)
|
2013-06-22 13:55:48 +08:00
|
|
|
return Function(name, clss, static, '', rtp, False, req, opt)
|
2013-03-14 15:26:18 +08:00
|
|
|
|
|
|
|
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):
|
2013-06-22 13:55:48 +08:00
|
|
|
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+' '])
|
2013-03-14 15:26:18 +08:00
|
|
|
name = defn[1]
|
2013-06-22 08:33:24 +08:00
|
|
|
default = defn[2] if defn[2] else ''
|
2013-06-22 13:55:48 +08:00
|
|
|
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)
|
2013-03-14 15:26:18 +08:00
|
|
|
|
|
|
|
def translateName(self, name):
|
|
|
|
return name.split(' ')[-1].split('.')[-1]
|
|
|
|
|
|
|
|
def translateClassName(self, name):
|
2013-06-28 02:45:19 +08:00
|
|
|
name = name.split(' ')[-1]
|
2013-03-14 15:26:18 +08:00
|
|
|
parts = name.split('.')
|
2013-06-28 02:45:19 +08:00
|
|
|
return parts[-2] if len(parts) > 1 and not parts[-2] == 'cv' else ''
|
2013-03-14 15:26:18 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Namespace(object):
|
|
|
|
def __init__(self, name='', constants=None, classes=None, functions=None):
|
|
|
|
self.name = name
|
|
|
|
self.constants = constants if constants else []
|
|
|
|
self.classes = classes if classes else []
|
|
|
|
self.functions = functions if functions else []
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return 'namespace '+self.name+' {\n\n'+\
|
2013-03-14 15:31:39 +08:00
|
|
|
(join((c.__str__() for c in self.constants), '\n')+'\n\n' if self.constants else '')+\
|
2013-03-18 10:37:42 +08:00
|
|
|
(join((f.__str__() for f in self.functions), '\n')+'\n\n' if self.functions else '')+\
|
2013-03-14 15:31:39 +08:00
|
|
|
(join((o.__str__() for o in self.classes), '\n\n') if self.classes else '')+'\n};'
|
2013-03-14 15:26:18 +08:00
|
|
|
|
|
|
|
class Class(object):
|
|
|
|
def __init__(self, name='', namespace='', constants=None, functions=None):
|
|
|
|
self.name = name
|
|
|
|
self.namespace = namespace
|
|
|
|
self.constants = constants if constants else []
|
|
|
|
self.functions = functions if functions else []
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return 'class '+self.name+' {\n\t'+\
|
|
|
|
(join((c.__str__() for c in self.constants), '\n\t')+'\n\n\t' if self.constants else '')+\
|
|
|
|
(join((f.__str__() for f in self.functions), '\n\t') if self.functions else '')+'\n};'
|
|
|
|
|
|
|
|
class Function(object):
|
2013-06-22 13:55:48 +08:00
|
|
|
def __init__(self, name='', clss='', static=False, namespace='', rtp='', const=False, req=None, opt=None):
|
2013-03-14 15:26:18 +08:00
|
|
|
self.name = name
|
|
|
|
self.clss = clss
|
2013-06-22 13:55:48 +08:00
|
|
|
self.static = static
|
2013-03-14 15:26:18 +08:00
|
|
|
self.const = const
|
|
|
|
self.namespace = namespace
|
|
|
|
self.rtp = rtp
|
|
|
|
self.req = req if req else []
|
|
|
|
self.opt = opt if opt else []
|
|
|
|
|
|
|
|
def __str__(self):
|
2013-03-18 12:48:10 +08:00
|
|
|
return (self.rtp+' ' if self.rtp else '')+self.name+'('+\
|
2013-03-14 15:26:18 +08:00
|
|
|
join((arg.__str__() for arg in self.req+self.opt), ', ')+\
|
2013-03-18 12:48:10 +08:00
|
|
|
')'+(' const' if self.const else '')+';'
|
2013-03-14 15:26:18 +08:00
|
|
|
|
|
|
|
class Argument(object):
|
2013-06-22 13:55:48 +08:00
|
|
|
def __init__(self, name='', tp='', const=False, I=True, O=False, ref='', default=''):
|
2013-03-14 15:26:18 +08:00
|
|
|
self.name = name
|
|
|
|
self.tp = tp
|
|
|
|
self.ref = ref
|
2013-06-22 13:55:48 +08:00
|
|
|
self.I = I
|
|
|
|
self.O = O
|
2013-03-14 15:26:18 +08:00
|
|
|
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):
|
|
|
|
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 '')+';'
|
|
|
|
|
2013-06-27 06:45:05 +08:00
|
|
|
def constants(tree):
|
|
|
|
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
|
2013-03-14 15:26:18 +08:00
|
|
|
|
|
|
|
def todict(obj, classkey=None):
|
|
|
|
if isinstance(obj, dict):
|
|
|
|
for k in obj.keys():
|
|
|
|
obj[k] = todict(obj[k], classkey)
|
|
|
|
return obj
|
|
|
|
elif hasattr(obj, "__iter__"):
|
|
|
|
return [todict(v, classkey) for v in obj]
|
|
|
|
elif hasattr(obj, "__dict__"):
|
|
|
|
data = dict([(key, todict(value, classkey))
|
|
|
|
for key, value in obj.__dict__.iteritems()
|
|
|
|
if not callable(value) and not key.startswith('_')])
|
|
|
|
if classkey is not None and hasattr(obj, "__class__"):
|
|
|
|
data[classkey] = obj.__class__.__name__
|
|
|
|
return data
|
|
|
|
else:
|
|
|
|
return obj
|