Return dict of {struct_name: raw_body_text} for all 'struct Name { ... };' definitions in text. Handles nested braces.
(text)
| 66 | |
| 67 | |
| 68 | def extract_structs(text): |
| 69 | """ |
| 70 | Return dict of {struct_name: raw_body_text} for all 'struct Name { ... };' |
| 71 | definitions in text. Handles nested braces. |
| 72 | """ |
| 73 | structs = {} |
| 74 | # Find "struct Name" followed (possibly after whitespace/newlines) by '{' |
| 75 | pattern = re.compile(r'\bstruct\s+(\w+)\s*\{') |
| 76 | pos = 0 |
| 77 | while True: |
| 78 | m = pattern.search(text, pos) |
| 79 | if not m: |
| 80 | break |
| 81 | name = m.group(1) |
| 82 | body_start = m.end() |
| 83 | # Walk forward counting brace depth |
| 84 | depth = 1 |
| 85 | i = body_start |
| 86 | while i < len(text) and depth > 0: |
| 87 | if text[i] == '{': |
| 88 | depth += 1 |
| 89 | elif text[i] == '}': |
| 90 | depth -= 1 |
| 91 | i += 1 |
| 92 | body = text[body_start:i - 1] |
| 93 | structs[name] = body |
| 94 | pos = m.start() + 1 |
| 95 | return structs |
| 96 | |
| 97 | |
| 98 | # --------------------------------------------------------------------------- |