Skip to content

Commit 409d807

Browse files
committed
Add function pointer parse. #66
1 parent c9813ef commit 409d807

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

CppHeaderParser/CppHeaderParser.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2375,13 +2375,30 @@ def _evaluate_method_stack(self):
23752375
self.stack = []
23762376
self.stmtTokens = []
23772377

2378+
_function_point_typedef_format = re.compile(r".*?\(.*?\*(.*?)\).*?\(.*?\).*?")
2379+
def _function_point_typedef_parse(self, stack):
2380+
idx = stack.index("typedef")
2381+
expression = "".join(stack[idx + 1 :])
2382+
m = self._function_point_typedef_format.match(expression)
2383+
if m is None:
2384+
return {}
2385+
2386+
name = m.group(1)
2387+
s = " ".join([i for i in stack if i != name])
2388+
r = {"name": name, "raw": s, "type": s}
2389+
return r
2390+
23782391
def _parse_typedef(self, stack, namespace=""):
23792392
if not stack or "typedef" not in stack:
23802393
return
23812394
stack = list(stack) # copy just to be safe
23822395
if stack[-1] == ";":
23832396
stack.pop()
23842397

2398+
r = self._function_point_typedef_parse(stack)
2399+
if len(r) == 3:
2400+
return r
2401+
23852402
while stack and stack[-1].isdigit():
23862403
stack.pop() # throw away array size for now
23872404

test/test_CppHeaderParser.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4038,5 +4038,23 @@ def test_fn(self):
40384038
self.assertEqual(m["parameters"][0]["type"], "typename TP<D >::S")
40394039

40404040

4041+
class FunctionPointerParse(unittest.TestCase):
4042+
def setUp(self):
4043+
self.cppHeader = CppHeaderParser.CppHeader(
4044+
"""
4045+
typedef int U32;
4046+
typedef unsigned int( * p )(int, int);
4047+
typedef int( * mmmmp )(int, int) ;
4048+
""",
4049+
"string",
4050+
)
4051+
4052+
def test_fn(self):
4053+
c = self.cppHeader
4054+
self.assertEqual(c.typedefs["U32"], "int")
4055+
self.assertEqual(c.typedefs["p"], "typedef unsigned int ( * ) ( int , int )")
4056+
self.assertEqual(c.typedefs["mmmmp"], "typedef int ( * ) ( int , int )")
4057+
4058+
40414059
if __name__ == "__main__":
40424060
unittest.main()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy