aboutsummaryrefslogtreecommitdiff
path: root/bindgen/gen_util.py
diff options
context:
space:
mode:
authorAndre Weissflog <floooh@gmail.com>2022-11-03 18:41:41 +0100
committerAndre Weissflog <floooh@gmail.com>2022-11-03 18:41:41 +0100
commit471f125e0aaa0b5c1201c754812c14cbb8b98ff1 (patch)
treee80e328852e9a34c158c72e9e96263c75c82e6b5 /bindgen/gen_util.py
parent818d12e9970fd47e4ded16666bb5585717d22842 (diff)
Fix language bindings:
- clang-14 ast-dump has changed for array types, fix works both for new and old format - start moving common helper functions into gen_util.py
Diffstat (limited to 'bindgen/gen_util.py')
-rw-r--r--bindgen/gen_util.py57
1 files changed, 57 insertions, 0 deletions
diff --git a/bindgen/gen_util.py b/bindgen/gen_util.py
new file mode 100644
index 00000000..a76f4b2b
--- /dev/null
+++ b/bindgen/gen_util.py
@@ -0,0 +1,57 @@
+# common utility functions for all bindings generators
+import re
+
+re_1d_array = re.compile("^(?:const )?\w*\s*\*?\[\d*\]$")
+re_2d_array = re.compile("^(?:const )?\w*\s*\*?\[\d*\]\[\d*\]$")
+
+def is_1d_array_type(s):
+ return re_1d_array.match(s) is not None
+
+def is_2d_array_type(s):
+ return re_2d_array.match(s) is not None
+
+def is_array_type(s):
+ return is_1d_array_type(s) or is_2d_array_type(s)
+
+def extract_array_type(s):
+ return s[:s.index('[')].strip()
+
+def extract_array_sizes(s):
+ return s[s.index('['):].replace('[', ' ').replace(']', ' ').split()
+
+def is_string_ptr(s):
+ return s == "const char *"
+
+def is_const_void_ptr(s):
+ return s == "const void *"
+
+def is_void_ptr(s):
+ return s == "void *"
+
+def is_func_ptr(s):
+ return '(*)' in s
+
+def extract_ptr_type(s):
+ tokens = s.split()
+ if tokens[0] == 'const':
+ return tokens[1]
+ else:
+ return tokens[0]
+
+# PREFIX_BLA_BLUB to bla_blub
+def as_lower_snake_case(s, prefix):
+ outp = s.lower()
+ if outp.startswith(prefix):
+ outp = outp[len(prefix):]
+ return outp
+
+# prefix_bla_blub => blaBlub, PREFIX_BLA_BLUB => blaBlub
+def as_lower_camel_case(s, prefix):
+ outp = s.lower()
+ if outp.startswith(prefix):
+ outp = outp[len(prefix):]
+ parts = outp.split('_')
+ outp = parts[0]
+ for part in parts[1:]:
+ outp += part.capitalize()
+ return outp