Skip to content

Allow parsing into Zoir.Node.Index (it's easier to parse build.zig.zon at runtime now) #22973

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

Merged
merged 3 commits into from
Apr 2, 2025

Conversation

MasonRemaley
Copy link
Contributor

@MasonRemaley MasonRemaley commented Feb 22, 2025

Some schemas can't be represented by Zig types.

For example, as #22775 points out, build.zig.zon's schema can't be represented by a Zig type since it uses field names as map keys:

.{
    .name = "foo",
    .dependencies = .{
        .these = .{
           // ...
        },
        .fields = .{
          // ...
        },
        .could_be_named_anything = .{
            // ...
        },
    },
    .paths = .{""},
}

This will be addressed for importing zon at comptime by #22907.

It's possible to handle this case at runtime by dropping down to std.zig.Zoir, but this is cumbersome--especially if most of your type conforms to a Zig type, but a few fields don't.

This PR makes it possible to, for example, parse the build.zig.zon above at runtime using the following schema:

struct {
    name: []const u8,
    dependencies: Zoir.Node.Index, // <-- this wasn't a thing before
    paths: []const u8,
}

Name and paths will be parsed as expected. dependencies will get the index of the Zoir node, allowing you to handle that field as you like--possibly even calling back into std.zon.fromZoirNode at some point on pieces of it if you want!

This allows you to mix and match the higher and lower level parsing APIs at will, making it much easier to parse build.zig.zon and other schemas that don't map directly to Zig types at runtime.

Other Changes

  • Status renamed to diagnostics. This better matches its usage now that you may want to read the parsed Zoir from it.
  • The AST and Zoir fields on diagnostics are no longer nullable, instead they just default to empty. This saves space, but more importantly, it makes the API simpler.

@alexrp alexrp added this to the 0.14.0 milestone Feb 22, 2025
@andrewrk andrewrk removed this from the 0.14.0 milestone Mar 1, 2025
This allows using `std.zon` to parse schemas which are not directly
representable in the Zig type system; for instance, `build.zig.zon`.
This name is more appropriate and in line with the rest of `std`.
@mlugg mlugg force-pushed the zon-stop-on-node branch from 7ef9071 to fa1695a Compare April 2, 2025 05:10
@mlugg mlugg enabled auto-merge April 2, 2025 05:11
@mlugg mlugg merged commit 896ffe6 into ziglang:master Apr 2, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants