3 ms·
You can't manipulate ASTs (ie, Zig code), but nothing stops you from parsing string literals however you want. For example, I wrote a PEG-like parser combinato
by curtisf 7y ago
You can't manipulate ASTs (ie, Zig code), but nothing stops you from parsing string literals however you want.
For example, I wrote a PEG-like parser combinator library in Zig. Using it currently [looks like this](https://github.com/CurtisFenner/zsmol/blob/87de4c77dd8543011e67ffb603923ecdf48e5565/src/grammar.zig#L706-L724 https://github.com/CurtisFenner/zsmol/blob/87de4c77dd8543011...). However, as a library, I ^could provide a function that looks like
pub const UnionDefinition = comb.fromLiteral(
\\ _: KeyUnion
\\ union_name: TypeIden
\\ generics: Generics?
\\ implements: Implements?
\\ _: PuncCurlyOpen
\\ fields: Field*
\\ members: FunctionDef*
\\ _: PuncCurlyClose
);
etc. But, I find reading the code as it is good enough for now that I didn't want to spend time implementing such a library.
[^]: Being able to create brand new types at `comptime` isn't [yet implemented](https://github.com/ziglang/zig/issues/383 https://github.com/ziglang/zig/issues/383), so this can't quite be done yet, though you could fake it with `get`/`set` methods instead of real fields