forked from defold/defold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_util.py
86 lines (69 loc) · 2.49 KB
/
gen_util.py
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
# Copyright 2020-2024 The Defold Foundation
# Copyright 2014-2020 King
# Copyright 2009-2014 Ragnar Svensson, Christian Murray
# Licensed under the Defold License version 1.0 (the "License"); you may not use
# this file except in compliance with the License.
#
# You may obtain a copy of the License, together with FAQs at
# https://www.defold.com/license
#
# Unless required by applicable law or agreed to in writing, software distributed
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
# 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*\]$")
re_dmarray = re.compile("^\s*dmArray\s*<(.*)>.*$")
re_ptr_type = re.compile("^(const )?\s*([\w0-9]+)\s*(\*+)$")
def is_1d_array_type(s):
return re_1d_array.match(s) is not None
def is_dmarray_type(s):
return re_dmarray.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_dmarray_type(s):
m = re_dmarray.match(s)
return m.group(1).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
# https://regex101.com/r/2tP07D/1
def extract_ptr_type(s):
m = re_ptr_type.match(s)
if m is not None:
return '%s %s' % (m.group(2), m.group(3)[1:]) # group 3 may contain "**"
return s
def extract_ptr_type2(s):
m = re_ptr_type.match(s)
if m is not None:
return (m.group(2), m.group(3)) # ("Vec2f", "**")
return (s, '')
# 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