Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Tracked by #14523
andrewrk opened this issue Feb 3, 2023 · 0 comments
Labels
bug Observed behavior contradicts documented or intended behavior contributor friendly This issue is limited in scope and/or knowledge of Zig internals. enhancement Solving this issue will likely involve adding new logic or components to the codebase. standard library This issue involves writing Zig code for the standard library.
Milestone

Comments

@andrewrk
Copy link
Member

andrewrk commented Feb 3, 2023

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.

@andrewrk andrewrk added bug Observed behavior contradicts documented or intended behavior enhancement Solving this issue will likely involve adding new logic or components to the codebase. contributor friendly This issue is limited in scope and/or knowledge of Zig internals. standard library This issue involves writing Zig code for the standard library. labels Feb 3, 2023
@andrewrk andrewrk added this to the 0.11.0 milestone Feb 3, 2023
tiehuis added a commit to tiehuis/zig that referenced this issue Apr 2, 2023
@andrewrk andrewrk modified the milestones: 0.11.0, 0.12.0 Jul 20, 2023
@MasonRemaley MasonRemaley mentioned this issue Jun 12, 2024
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior contributor friendly This issue is limited in scope and/or knowledge of Zig internals. enhancement Solving this issue will likely involve adding new logic or components to the codebase. standard library This issue involves writing Zig code for the standard library.
Projects
None yet
Development

No branches or pull requests

1 participant