I'd like to parse C header files in Javascript. Is there any such library available? Otherwise, any tips to help me get started?
Update: My ultimate goal is to automatically build interfaces for node-ffi. The parser doesn't necessarily have to be in Javascript as long as it can spit out a format understandable by Javascript. If it's very hard to develop by myself, I'll probably have to go with an off the shelf solution...?
Parsing means analyzing and converting a program into an internal format that a runtime environment can actually run, for example the JavaScript engine inside browsers. The browser parses HTML into a DOM tree.
Parsing, syntax analysis, or syntactic analysis is the process of analyzing a string of symbols, either in natural language, computer languages or data structures, conforming to the rules of a formal grammar. The term parsing comes from Latin pars (orationis), meaning part (of speech).
argv is the simplest way of parsing arguments in Node. js and you do not need to install any additional package or library for that. You just pass arguments to a Node. js application and these arguments are handled by process.
You should check out clang.
For a simple command-line invocation, you can try this:
clang -cc1 -ast-dump-xml myfile.h
Or you can build your own tool using clang
reasonably-well-documented parser library, which will build an AST for you, and let you walk it as you see fit (perhaps for output in JSON).
You might start by looking at peg.js which generates javascript code to parse a grammar given as input. Details avalable here https://pegjs.org/
Then yo would need to write or find a grammar for the header files you want to parse.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With