Skip to content

implement syntax errors inside zig parser code in ZON mode when illegal syntax is used #14532

@andrewrk

Description

@andrewrk

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

/// 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviorcontributor friendlyThis issue is limited in scope and/or knowledge of Zig internals.enhancementSolving this issue will likely involve adding new logic or components to the codebase.standard libraryThis issue involves writing Zig code for the standard library.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions