eliben / pycparser

:snake: Complete C99 parser in pure Python
Other
3.26k stars 612 forks source link

Implement the collections.abc.Sequence interface for e. g. class Node #541

Open TBSenseo opened 6 months ago

TBSenseo commented 6 months ago

I am using pycparser the first time. I had at first the problem that pycparser was not able to understand a preprocessor output file from Visual Studio. Then I read that it is recommended to use gcc or clang. As a good thing, I had already setup CodeBlocks and a ruby, where I had installed multiple gcc compiler variants (ucrt64, mingw32, mingw64, and clang64). I got CodeBlocks to generate a preprocessor file by making a dedicated "Preprocess" target with the "-E" argument. pycparser then failed to understand the __builtin_va_list symbol. I then read online that fake stdclib headers should be used instead. I installed pycparser-fake-libc to my Python distribution, reconfigured the Preprocess target to use "-nostdinc" and to include the directory of the pycparser-fake-libc package, and let CodeBlocks call the ucrt64 compiler to preprocess the code file again. Now, pycparser is able to parse the preprocessed file successfully. I use CodeBlocks since our project uses a lot of include paths which will make it cumbersome to reproduce that list again in Python, or a batch script, and update it in the future when the parsing is still needed.

Now to the request: I have only seen an FAQ, but not a documentation that says what object is what. Additionally, the ASTFile class has a children attribute, but I am unable to use the len function on it. The show and the repr function both generated so much lines that they wrapped in the console and I have now printed them each to a file. However, it would be more useful if the Node class, which is the base class for the ASTFile class would implement the collections.abc.Sequence interface. In that way, I would be able to walk piece by piece through the AST tree and search for the piece that I actually look for. Can you please implement the collections.abc.Sequence interface or similar to the Node class and wherever in the pycparser library this may be useful as well?

I have found that I can generate lists with the children and such be able to use len and access the children by index, but a direct method to investigate the children through the ASTFile class instance seems to save me from needing to create such lists.

TBSenseo commented 6 months ago

It is also quite unusual that the representation (repr function) has new line characters and indentation. Normally, this is functionality that would be in the __str__ function.