ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
34.47k stars 2.52k forks source link

promote self-hosted C tokenizer and parser to a general-purpose C tokenizer and parser in the standard library #4045

Open andrewrk opened 4 years ago

andrewrk commented 4 years ago

Currently Zig has the file src-self-hosted/c_tokenizer.zig which is used to tokenize macros for translate-c purposes. It also has some functions in src-self-hosted/translate_c.zig which do AST parsing of C tokens:

Zig has set a (in my opinion successful) precedent in exposing its own tokenizer, parser, and AST in the standard library, in these files:

Now it is time to do the same for C:

cc @Vexu

Vexu commented 4 years ago

I'm assuming we also want a preprocessor? Are we trying to eventually replace clang in translate-c completely?

andrewrk commented 4 years ago

That could be a direction to go with this, although I wouldn't call it "planned" yet. The main idea here is to make the API abstraction layer of the C tokenization and parsing clear and understandable. That will have the benefit of making it easier to test - there are plenty of pre-existing test cases for parsing C code, for example. So this will result in a more robust translate-c feature.

andrewrk commented 4 years ago

Vexu landed #4317 in 0cc2489d22a27b2dc82ee9ef72e945b9fa97c8fe. This started the efforts toward implementing this issue. What remains until this can be closed:

moosichu commented 3 years ago

This issue interests me because I'm using zig within the context of a C/C++ project (for now) - and being able to do simple static reflection on my codebase from build.zig using this library would be great. So I've subscribed to the issue for now, but if there is anyway I could help/contribute I might be able to look at it sometime down the line if no-one has done so before-then :)

Vexu commented 3 years ago

I've started work on a C parser/compiler that could eventually be upstreamed. I haven't touched it for some time but I'm interested in continuing it especially if someone would want to contribute.

moosichu commented 3 years ago

Let me know if know there's anything I can do to help.

g-berthiaume commented 1 year ago

I'm commenting to follow this thread. I'm considering making a metaprogramming tool for C and the ability to have a C parser/tokenizer out-of-the-box in zig would facilitate this project greatly.

As always, thanks for your work!

Vexu commented 1 year ago

This issue will likely be closed in favor of using https://github.com/Vexu/arocc as a package.