Skip to content

Instantly share code, notes, and snippets.

@riicchhaarrd
Created May 10, 2023 19:29
Show Gist options
  • Save riicchhaarrd/ee5b7db8543049dbc6087ce0a28e471b to your computer and use it in GitHub Desktop.
Save riicchhaarrd/ee5b7db8543049dbc6087ce0a28e471b to your computer and use it in GitHub Desktop.
import re
class Struct:
def __init__(self, name, body):
self.name = name
self.references = []
self.body = body
def __str__(self) -> str:
return f'{self.name}: {self.references}'
references = {}
def parse(text):
global references
structs = {}
pattern = r"(?:struct|union)\s+([a-zA-Z0-9_]+)\s*{([^}]*)};"
matches = re.findall(pattern, text, re.DOTALL)
for match in matches:
name = match[0]
body = match[1]
s = Struct(name, body)
fields = body.split(';')
for f in fields:
type = f.split(' ')[0].strip()
s.references.append(type)
if not type in references:
references[type] = []
references[type].append(s.name)
structs[name] = s
return structs
with open('structs.h', 'r') as f:
c = f.read()
structs = parse(c)
for key, value in references.items():
if key in structs:
print(key)
for v in value:
print(f'\t{value}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment