2 days ago
- The Zig parser uses a MultiArrayList for efficient memory and cache locality, storing each struct field in separate contiguous arrays.
- AST node data is stored in three locations: the token stream (e.g., identifiers), the node list (child AST nodes), and the extra data list (additional metadata).
- The parser maintains state in a struct called Parser, which includes token arrays, node list, extra data, errors, and scratch space.
- Parsing proceeds by inspecting the current token and using conditional logic to determine expected tokens, building AST nodes incrementally.
- Variable declarations are parsed by first consuming 'const' or 'var', then expecting an identifier, optional type expression, and optional initialization expression.
- Function definitions are parsed by reserving a node index, parsing parameters, calling convention, optional return type, and then setting the node with extra data.
- The final AST structure includes the source text, tokens, nodes, extra data, and accumulated errors, ready for the next compilation stage (AstGen).