1"""Rudimentary parser for C struct definitions.""" 2 3import re 4 5PyObject_HEAD = "PyObject_HEAD" 6PyObject_VAR_HEAD = "PyObject_VAR_HEAD" 7 8rx_name = re.compile("} (\w+);") 9 10class Struct: 11 def __init__(self, name, head, members): 12 self.name = name 13 self.head = head 14 self.members = members 15 16def parse(s): 17 """Parse a C struct definition. 18 19 The parser is very restricted in what it will accept. 20 """ 21 22 lines = filter(None, s.split("\n")) # get non-empty lines 23 assert lines[0].strip() == "typedef struct {" 24 pyhead = lines[1].strip() 25 assert (pyhead.startswith("PyObject") and 26 pyhead.endswith("HEAD")) 27 members = [] 28 for line in lines[2:]: 29 line = line.strip() 30 if line.startswith("}"): 31 break 32 33 assert line.endswith(";") 34 line = line[:-1] 35 words = line.split() 36 name = words[-1] 37 type = " ".join(words[:-1]) 38 if name[0] == "*": 39 name = name[1:] 40 type += " *" 41 members.append((name, type)) 42 name = None 43 mo = rx_name.search(line) 44 assert mo is not None 45 name = mo.group(1) 46 return Struct(name, pyhead, members) 47