liveBook · Manning

Comptime parser

As explained in Systems Programming with Zig

Comptime parsing is the compile-time transformation of raw input data into program values or data structures.

A comptime parser transforms input into program values during compilation. For a wordlist, Zig can count newline-delimited words first, then use that count as the length of the returned array. section 4.1.3

A common pattern is to stream through the input with an iterator, count its values, and write them into an array. The wordlist parser tokenizes twice: once to count and once to fill the array. Zig memoizes side-effect-free comptime calls with the same input. section 4.1.3

Listing 4.4 Parsing the wordlist into an array
fn parseWordlist(
    comptime wordlist: []const u8, #1
) [countWordlist(wordlist)][4]u8 { #2
    var list: [countWordlist(wordlist)][4]u8 = undefined;
    var i: usize = 0;

    var word_iter = std.mem.tokenizeScalar(u8, wordlist, '\n');
    while (word_iter.next()) |word| : (i += 1) { #3
        if (word.len != 4) {
            @compileError(std.fmt.comptimePrint( #4
                "the word '{s}' is not four characters long.",
                .{word},
            ));
        }
        list[i] = word[0..4].*; #5
    }

    return list;
}

The parser returns an array of four-byte words and uses @compileError with std.fmt.comptimePrint to reject a word of the wrong length. section 4.1.3

Listing 4.6 Using our parser
const words = blk: { #1
    @setEvalBranchQuota(1_000_000); #2
    break :blk parseWordlist(@embedFile("words.txt")); #3
};

A labeled block raises the evaluation branch quota, embeds the wordlist file, and returns the parsed array. section 4.1.3

How Systems Programming with Zig covers it

How deep it goes
Works through counting, array construction, validation, memoization, and parser invocation.
Examples
Uses a newline-delimited wordlist and invites readers to trigger an error with a word that is not four characters long.
What you can do after
Apply it via Zig to construct and validate a fixed-size word array during compilation.
How it connects
Connects compile-time evaluation with tokenization, iterators, arrays, and compile-time errors.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Systems Programming with Zig. The section links point to the parts of the book it draws on.