RegExp - please help me!

Lele Gaifax lele at metapensiero.it
Wed Dec 27 03:31:39 EST 2017


szykcech at gmail.com writes:

> Please help me with this regexp or tell me that I neeed do this in other way.

I think that using regexps to parse those structures is fragile and difficult
to get right[0], as there are lots of corner cases (comments, complex types,
...).

I'd suggest using a tool designed to do that, for example pycparser[1], that
provides the required infrastructure to parse C units into an AST: from there
you can easily extract interesting pieces and write out in whatever format you
need.

As an example, I used it to extract[2] enums and defines from PostgreSQL C
headers[3] and rewrite them as Python definitions[4].

Good luck,
ciao, lele.

[0] http://regex.info/blog/2006-09-15/247
[1] https://github.com/eliben/pycparser
[2] https://github.com/lelit/pg_query/blob/master/tools/extract_enums.py
[3] https://github.com/lfittl/libpg_query/blob/43ce2e8cdf54e4e1e8b0352e37adbd72e568e100/src/postgres/include/nodes/parsenodes.h
[4] https://github.com/lelit/pg_query/blob/master/pg_query/enums/parsenodes.py
-- 
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
lele at metapensiero.it  |                 -- Fortunato Depero, 1929.




More information about the Python-list mailing list