diff options
author | alon@honor <none@none> | 2010-10-21 17:13:12 -0700 |
---|---|---|
committer | alon@honor <none@none> | 2010-10-21 17:13:12 -0700 |
commit | ce5008630e06a2238b2dc6e5fcbbeebc0ce01cd2 (patch) | |
tree | 495191e7bdcc12a3d49fae475d197a0b89fdb112 /tools | |
parent | 9f7e6ddbd245ecac6321941684d6d4a337d67fa1 (diff) |
namespace generator tool
Diffstat (limited to 'tools')
-rw-r--r-- | tools/namespacer.py | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/tools/namespacer.py b/tools/namespacer.py new file mode 100644 index 00000000..966e917c --- /dev/null +++ b/tools/namespacer.py @@ -0,0 +1,82 @@ +#!/usr/bin/python + +''' +Tool that generates namespace boilerplate. Given + + _mangled_name1_ = MyClass::MyClass() + _mangled_name2_ = MyClass::~MyClass() + _mangled_name3_ = MyClass::DoSomething(int) + +the tool will generate + + MyClass = { + 'MyClass': _mangled_name1_, + '~MyClass': _mangled_name2_, + 'DoSomething': _mangled_name3_ + }; + +You can then do the following in JavaScript: + + MyClass.MyClass(ptr); + MyClass['~MyClass'](ptr); // Need string here, due to |~| + MyClass.DoSomething(ptr, 17); + +Note that you need to send the |this| pointer yourself. TODO: +a more OO boilerplate on top of that. +''' + +import os, sys, json + +data = open(sys.argv[1], 'r').readlines() + +space = {} + +for line in data: + line = line.rstrip() + + realname, signature = line.split(' = ') + signature = signature.replace(') const', ')') + signature = signature[:-1].split('(') + func, params = signature[0], '('.join(signature[1:]) + + if ' ' in func: + i = func.index(' ') + ret = func[:i] + if '<' not in ret and '[' not in ret and '(' not in ret: + func = func[i+1:] + + funcparts = ['Namespace'] + func.split('::') + + currspace = space + for part in funcparts[:-1]: + currspace = currspace.setdefault(part, {}) + currspace.setdefault(funcparts[-1], []).append((realname,params)); + +def clean(currspace): + if type(currspace) is list: + if len(currspace) == 1: + return currspace[0][0] + else: + ret = {} + for item in currspace: + i = str(len(ret)/2) + ret[i] = item[0] + ret[i + '_params'] = item[1] + return ret + else: + for key in currspace.keys(): + currspace[key] = clean(currspace[key]) + return currspace + +space = clean(space) + +def finalize(line): + try: + key, val = line.split(': ') + assert val != '}' and '_params"' not in key + return key + ': ' + val.replace('"', '') + except: + return line + +print '\n'.join(map(finalize, json.dumps(space, sort_keys=True, indent=2).split('\n'))) + |