ned14 / pcpp

A C99 preprocessor written in pure Python
Other
220 stars 41 forks source link

Document hooks behavior #43

Closed sztomi closed 4 years ago

sztomi commented 4 years ago

I was trying to locate some directives with their full extents in a header file. I figured using on_directive_handle would help me, but nothing I did seemed to call the hook functions. It turns out I had to call write after parsing in order for the hooks to be called.

Can you clarify what exactly parse does and if it's really needed to write the preprocessed output in order to get the hooks called? Is there a way to avoid that?

ned14 commented 4 years ago

It isn't write which does this. It's causing the parse to begin. The parser is a pull based parser, so only if you pull tokens from its end does it parse the input, and call hooks etc. Everything then proceeds incrementally.

parse is implemented as a Python generator, so when you call it, it suspends. You then need to iterate the generator returned to progress the parse.

I hope this helps, and sorry for the four month delay in replying. My kids went back to school yesterday, I missed a lot of stuff like this during the past eight months. Sorry.

sztomi commented 4 years ago

No need to apologize, I appreciate the answer and the excellent library. Thanks a lot!