Extracted from #14523.
When the following function is used, compile errors should be detected when any syntax that is not legal in ZON is used.
|
/// Parse in ZON mode. Subset of the language. |
|
/// TODO: set a flag in Parse struct, and honor that flag |
|
/// by emitting compilation errors when non-zon nodes are encountered. |
|
pub fn parseZon(p: *Parse) !void { |
|
// We must use index 0 so that 0 can be used as null elsewhere. |
|
p.nodes.appendAssumeCapacity(.{ |
|
.tag = .root, |
|
.main_token = 0, |
|
.data = undefined, |
|
}); |
|
const node_index = p.expectExpr() catch |err| switch (err) { |
|
error.ParseError => { |
|
assert(p.errors.items.len > 0); |
|
return; |
|
}, |
|
else => |e| return e, |
|
}; |
|
if (p.token_tags[p.tok_i] != .eof) { |
|
try p.warnExpected(.eof); |
|
} |
|
p.nodes.items(.data)[0] = .{ |
|
.lhs = node_index, |
|
.rhs = undefined, |
|
}; |
|
} |
ZON is only allowed to use a subset of the Zig language:
- string literal
- boolean literal
- integer literal
- float literal
- null literal
- undefined literal
- anonymous struct literal
- tuple literal
- anonymous enum literal
Anything else, such as functions, control flow logic, mathematical operations, etc., should be rejected by the parser.
Be careful with negative numbers, which are certainly allowed in ZON.
For testing, look into augmenting lib/std/zig/parser_test.zig.
Extracted from #14523.
When the following function is used, compile errors should be detected when any syntax that is not legal in ZON is used.
zig/lib/std/zig/Parse.zig
Lines 180 to 204 in 60935de
ZON is only allowed to use a subset of the Zig language:
Anything else, such as functions, control flow logic, mathematical operations, etc., should be rejected by the parser.
Be careful with negative numbers, which are certainly allowed in ZON.
For testing, look into augmenting lib/std/zig/parser_test.zig.