Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C parser in Javascript

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...?

like image 264
Olivier Lalonde Avatar asked Nov 01 '12 05:11

Olivier Lalonde


People also ask

What is parser in JavaScript?

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.

What is parsing script?

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).

What is used for parsing and running JavaScript in node JS?

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.


2 Answers

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).

like image 152
rici Avatar answered Sep 28 '22 08:09

rici


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.

like image 37
HBP Avatar answered Sep 28 '22 09:09

HBP