Show HN: A preprocessor, similar to a C preprocessor, for arbitrary text inputs
fossil.wanderinghorse.net
In the scope of the sqlite project's new JavaScript builds, we need a way to conditionally include, or not, sections of JavaScript code depending on whether the build is for an ES6 module (ESM) or not. Though JS's own introspection is normally suitable for determining such things at runtime, the ESM designers chose to include keywords which are not legal to reference from non-ESM code, which rules out a single-source-file solution for ESM and vanilla JS code. Attempts at using a C preprocessor (CPP) to achieve that unveiled several quirks which make CPP ill-suited for processing JavaScript code.
Necessity being the mother of invention, c-pp, the C-Minus Preprocessor, was born as a weekend side-project. It is conceptually and syntactically like a C preprocessor but:
- Has only a subset of CPP's features.
- Is suitable for use with essentially arbitrary text inputs.
- Has a configurable keyword prefix rather than being hard-coded to "#" like CPP is.
Specifically, it supports a feature subset useful to us in the sqlite project plus a few other low-hanging fruits which just seemed relevant at the time.
c-pp is itself implemented in a single file of portable, dependency-free C. It "should" build with any C89-capable compiler.