cslug.c_parse

The cslug.c_parse submodule provides some very bare-bones C parsing tools.

This module's sole purpose is to extract function and struct declarations to generate prototypes in automatically generated header files, and to parse type information from the declarations to be passed to cslug.Types.

For a proper C parser, use pycparser.

class cslug.c_parse.TokenType(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: IntFlag

CODE = 4
COMMENT = 1
LITERAL = 2
cslug.c_parse.filter(text, token_types)[source]
cslug.c_parse.lex(text)[source]
cslug.c_parse.parse_function(string, typedefs=None)[source]

Parse a function declaration into its name, its return type and its arguments.

cslug.c_parse.parse_functions(text, typedefs=None, definitions=True, prototypes=False)[source]
cslug.c_parse.parse_parameter(string, typedefs=None)[source]

Parse a variable or parameter declaration such as int * foo.

Parameters:
  • string (str) – Declaration to parse.

  • typedefs

Returns:

(ctypes_type_name, pointers, name)

Return type:

tuple[str, int, str]

The pointers output is a count of how many layers of pointer referencing cover the raw value. i.e. How many *s or []s.

cslug.c_parse.parse_struct(text)[source]
Parameters:

text

Returns:

cslug.c_parse.parse_structs(text)[source]

Search for and parse C Structure definitions in a block of text.

Parameters:

text

Returns:

Iterable of (name, parameters) pairs as given by parse_struct.

cslug.c_parse.search_functions(text, definitions=True, prototypes=False)[source]