how to extract the functions from a c++ header file with haskell?
i need to get all the functions from a c++ header file 开发者_如何学Gowith haskell (not methods only functions), i need the input parameters and the return type also the method name.
how could i realise this? olny with regex? or must i write a whole parser (bottom-up or top-down)?
To extract all the function declarations from a header file, you'll need a parser, not a regex. Parsers for C++ are hard to come by, but luckily we have some:
- Language.C, for parsing C files
- LibClang, for parsing C++ files
Using either of these you'll be able to robustly process C or C++ headers, and extract declarations as Haskell data types, for further processing.
If you do decide to parse them, I recommend you use Parsec and try to see if anybody else has already done it.
But, there are many C++ parsers already out there - I recommend you use one of them and interface with its output.
I've written a program called cgen that generates C bindings to C++ libraries, and it parses C++ headers (using Parsec) and collects the function data like you describe. It's ridiculously far from being a complete C++ parser, but it can parse some simple header files - like this example.
It's not a library, though, so you may need to adapt the code to fit your needs (and if you make any improvements, do send me a patch). You can find the relevant code at github, particularly the modules HeaderParser and HeaderData.
精彩评论