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

Documentation on ZON files #15552

Open
Arnau478 opened this issue May 2, 2023 · 14 comments
Open

Documentation on ZON files #15552

Arnau478 opened this issue May 2, 2023 · 14 comments
Labels
Milestone

Comments

@Arnau478
Copy link
Contributor

Arnau478 commented May 2, 2023

Now that ZON files (Zig Object Notation) are a thing, they should be documented in langref
ZON files described on #14523

@Vexu Vexu added the docs label May 2, 2023
@Vexu Vexu added this to the 0.12.0 milestone May 2, 2023
@tisonkun
Copy link
Contributor

tisonkun commented May 6, 2023

Do we have a standing example now? I don't find a build.zig.zon in this repository even.

@Arnau478
Copy link
Contributor Author

Arnau478 commented May 6, 2023

Not sure, but it should be there by 0.12.0, so... Anyway, the syntax is very simple, just anonymous nested structs, like so:

.{
    .foo = .{
        .a = "hello",
    }
}

(then, they can be imported just like JSON in node.js)

Whenever the build system starts using ZON files we will see them on the repo I guess

EDIT: Zig compiler doesn't need a build.zig.zon, as it does not have any dependency. Also, it will never be a dependency, so not even the version field would be used.

@Earnestly
Copy link

Earnestly commented Aug 30, 2023

A major flaw with json is its inability to store non-UTF8 strings which inadvertently makes them unreliable for storing unix filenames (here a filename is defined as any sequence of bytes except NUL and /, including invalid UTF-8).

Will or does zon address this issue?

Some prior art on this would be Rust's raw string literal notation and qsn which is a format based on those ideas. It is documented quite nicely here, along with good general coverage of this topic: http://www.oilshell.org/release/latest/doc/qsn.html

@mlugg
Copy link
Member

mlugg commented Aug 30, 2023

ZON strings are no different from Zig string literals in this context, so the answer is: yes, ZON strings store any sequence of bytes, agnostic to their interpretation. The literal in file must itself be UTF-8 encoded, because Zig sources - and by extension ZON - are always UTF-8 encoded, but you can represent any byte sequence which is not valid UTF-8 by using simple escape sequences (the main relevant one here being \xNN).

@Earnestly
Copy link

Earnestly commented Aug 31, 2023

This is what I hope for, essentially exposing zig's string literals directly, but if zon is to be like json and independent of language then perhaps these aspects should be made explicit when documenting the format?

Then tools like fq can pick up the format making it useful for a wider array of purposes.

@Arnau478
Copy link
Contributor Author

Now that i'm thinking about this, it would be wise to wait for #14531, and document everything at once. More importantly, some aspects of ZON files might change.

@andrewrk andrewrk modified the milestones: 0.14.0, 0.12.0 Oct 14, 2023
@andrewrk
Copy link
Member

@Earnestly
Copy link

Closing this may be premature as that commit doesn't document ZON format, but instead documents the build.zig.zon file and its possible fields.

@Arnau478
Copy link
Contributor Author

I totally agree with @Earnestly, having documentation for build.zig.zon is good, but the .zon format itself should also be documented (maybe in langref?).

@mlugg
Copy link
Member

mlugg commented Oct 15, 2023

I definitely agree we should have documentation on the ZON format itself, not just the schema of build.zig.zon - reopening for this.

@mlugg mlugg reopened this Oct 15, 2023
@andrewrk andrewrk modified the milestones: 0.12.0, 1.0.0 Oct 15, 2023
@Durobot
Copy link

Durobot commented Dec 21, 2023

I definitely agree we should have documentation on the ZON format itself, not just the schema of build.zig.zon - reopening for this.

In the meantime, since there's no documentation on ZON, could someone please tell if ZON supports arrays?

If yes, what is the syntax?

Is it .my_arr = .{ 1, 2, 3 },, or .my_arr = { 1, 2, 3 }, - both of these pass std.zig.Ast.parse()?

Or is it something different?

@Arnau478
Copy link
Contributor Author

In the meantime, since there's no documentation on ZON, could someone please tell if ZON supports arrays?

You should think of a .zon file as a struct initialization. So yes, it does. And it does in the exact same way zig does. And to answer your question, just use a tuple (an anonymous struct literal with no field names):

.foo = .{
    something,
    hello_there,
},

Is it .my_arr = .{ 1, 2, 3 },, or .my_arr = { 1, 2, 3 }, - both of these pass std.zig.Ast.parse()?

The first one would work perfectly. The second one is not even valid in normal zig code.

But yeah, the first one should pass, as (someone correct me if I missed something) Ast.parse() only uses the mode parameter to call Parser.parseZon() instead of Parser.parseRoot(), and Parser.parseZon() basically calls the expression parsing routines. Acording to the grammar, this should cascade down to PrimaryTypeExpr.

@Durobot
Copy link

Durobot commented Dec 28, 2023

You should think of a .zon file as a struct initialization. So yes, it does. And it does in the exact same way zig does.

Thank you.

I have since written a small library (if it could be called that) to help extract field values from the ZON AST generated with Ast.parse(): https://github.com/Durobot/zon_get_fields

I don't want to hijack the comments for this issue.. but you wouldn't happen to know why negative numbers (field values) are represented by two tokens in AST - the minus sign ("-") and the number itself?

@Arnau478
Copy link
Contributor Author

I don't want to hijack the comments for this issue.. but you wouldn't happen to know why negative numbers (field values) are represented by two tokens in AST - the minus sign ("-") and the number itself?

Well, most compilers do that. And it kind of makes sense, as const foo = -bar; is valid. Plus, the compiler will figure it out the same way you do const foo = 1+2;. Anyway, this is a bit out of scope, as you said. So, if you have any further questions, contact me at @arnau478:matrix.org (matrix) or @arnau478 (discord)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants