Staging
v0.5.1
https://github.com/python/cpython
Raw File
Tip revision: c7536c4ff5d0ec6924a135004a53b9f0463f3480 authored by Benjamin Peterson on 15 March 2012, 18:57:38 UTC
merge heads
Tip revision: c7536c4
structparse.py
"""Rudimentary parser for C struct definitions."""

import re

PyObject_HEAD = "PyObject_HEAD"
PyObject_VAR_HEAD = "PyObject_VAR_HEAD"

rx_name = re.compile("} (\w+);")

class Struct:
    def __init__(self, name, head, members):
        self.name = name
        self.head = head
        self.members = members

def parse(s):
    """Parse a C struct definition.

    The parser is very restricted in what it will accept.
    """

    lines = [_f for _f in s.split("\n") if _f] # get non-empty lines
    assert lines[0].strip() == "typedef struct {"
    pyhead = lines[1].strip()
    assert (pyhead.startswith("PyObject") and
            pyhead.endswith("HEAD"))
    members = []
    for line in lines[2:]:
        line = line.strip()
        if line.startswith("}"):
            break

        assert line.endswith(";")
        line = line[:-1]
        words = line.split()
        name = words[-1]
        type = " ".join(words[:-1])
        if name[0] == "*":
            name = name[1:]
            type += " *"
        members.append((name, type))
    name = None
    mo = rx_name.search(line)
    assert mo is not None
    name = mo.group(1)
    return Struct(name, pyhead, members)
back to top