Zig 0.17.0-dev (Split by item)

This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig library itself.

Pattern

PatternList.Pattern
const Pattern = struct

File

lib/compiler/translate-c/PatternList.zig:84

Code

const Pattern = struct {
    slicer: MacroSlicer,
    impl: Impl,

    fn init(pl: *Pattern, allocator: mem.Allocator, template: Template) Error!void {
        const source = template[0];
        const impl = template[1];
        var tok_list: std.ArrayList(CToken) = .empty;
        defer tok_list.deinit(allocator);

        pl.* = .{
            .slicer = try tokenizeMacro(allocator, source, &tok_list),
            .impl = impl,
        };
    }

    fn deinit(pl: *Pattern, allocator: mem.Allocator) void {
        allocator.free(pl.slicer.tokens);
        pl.* = undefined;
    }

    /// This function assumes that `ms` has already been validated to contain a function-like
    /// macro, and that the parsed template macro in `pl` also contains a function-like
    /// macro. Please review this logic carefully if changing that assumption. Two
    /// function-like macros are considered equivalent if and only if they contain the same
    /// list of tokens, modulo parameter names.
    fn matches(pat: Pattern, ms: MacroSlicer) bool {
        if (ms.params != pat.slicer.params) return false;
        if (ms.tokens.len != pat.slicer.tokens.len) return false;

        for (ms.tokens, pat.slicer.tokens) |macro_tok, pat_tok| {
            if (macro_tok.id != pat_tok.id) return false;
            switch (macro_tok.id) {
                .macro_param, .macro_param_no_expand => {
                    // `.end` is the parameter index.
                    if (macro_tok.end != pat_tok.end) return false;
                },
                .identifier, .extended_identifier, .string_literal, .char_literal, .pp_num => {
                    const macro_bytes = ms.slice(macro_tok);
                    const pattern_bytes = pat.slicer.slice(pat_tok);

                    if (!mem.eql(u8, pattern_bytes, macro_bytes)) return false;
                },
                else => {
                    // other tags correspond to keywords and operators that do not contain a "payload"
                    // that can vary
                },
            }
        }
        return true;
    }
}