std.zon: update to new I/O API

This commit is contained in:
Andrew Kelley 2025-07-19 16:59:16 -07:00
parent b956b02187
commit c3da98cf5a
5 changed files with 979 additions and 993 deletions

View File

@ -161,17 +161,19 @@ pub fn formatEscapeString(path: Path, writer: *std.io.Writer) std.io.Writer.Erro
}
}
/// Deprecated, use double quoted escape to print paths.
pub fn fmtEscapeChar(path: Path) std.fmt.Formatter(Path, formatEscapeChar) {
return .{ .data = path };
}
/// Deprecated, use double quoted escape to print paths.
pub fn formatEscapeChar(path: Path, writer: *std.io.Writer) std.io.Writer.Error!void {
if (path.root_dir.path) |p| {
try std.zig.charEscape(p, writer);
if (path.sub_path.len > 0) try std.zig.charEscape(fs.path.sep_str, writer);
for (p) |byte| try std.zig.charEscape(byte, writer);
if (path.sub_path.len > 0) try writer.writeByte(fs.path.sep);
}
if (path.sub_path.len > 0) {
try std.zig.charEscape(path.sub_path, writer);
for (path.sub_path) |byte| try std.zig.charEscape(byte, writer);
}
}

View File

@ -446,8 +446,8 @@ pub fn fmtString(bytes: []const u8) std.fmt.Formatter([]const u8, stringEscape)
}
/// Return a formatter for escaping a single quoted Zig string.
pub fn fmtChar(bytes: []const u8) std.fmt.Formatter([]const u8, charEscape) {
return .{ .data = bytes };
pub fn fmtChar(c: u21) std.fmt.Formatter(u21, charEscape) {
return .{ .data = c };
}
test fmtString {
@ -458,9 +458,7 @@ test fmtString {
}
test fmtChar {
try std.testing.expectFmt(
\\" \\ hi \x07 \x11 " derp \'"
, "\"{f}\"", .{fmtChar(" \\ hi \x07 \x11 \" derp '")});
try std.testing.expectFmt("c \\u{26a1}", "{f} {f}", .{ fmtChar('c'), fmtChar('⚡') });
}
/// Print the string as escaped contents of a double quoted string.
@ -480,21 +478,26 @@ pub fn stringEscape(bytes: []const u8, w: *Writer) Writer.Error!void {
};
}
/// Print the string as escaped contents of a single-quoted string.
pub fn charEscape(bytes: []const u8, w: *Writer) Writer.Error!void {
for (bytes) |byte| switch (byte) {
/// Print as escaped contents of a single-quoted string.
pub fn charEscape(codepoint: u21, w: *Writer) Writer.Error!void {
switch (codepoint) {
'\n' => try w.writeAll("\\n"),
'\r' => try w.writeAll("\\r"),
'\t' => try w.writeAll("\\t"),
'\\' => try w.writeAll("\\\\"),
'"' => try w.writeByte('"'),
'\'' => try w.writeAll("\\'"),
' ', '!', '#'...'&', '('...'[', ']'...'~' => try w.writeByte(byte),
'"', ' ', '!', '#'...'&', '('...'[', ']'...'~' => try w.writeByte(@intCast(codepoint)),
else => {
try w.writeAll("\\x");
try w.printInt(byte, 16, .lower, .{ .width = 2, .fill = '0' });
if (std.math.cast(u8, codepoint)) |byte| {
try w.writeAll("\\x");
try w.printInt(byte, 16, .lower, .{ .width = 2, .fill = '0' });
} else {
try w.writeAll("\\u{");
try w.printInt(codepoint, 16, .lower, .{});
try w.writeByte('}');
}
},
};
}
}
pub fn isValidId(bytes: []const u8) bool {

View File

@ -574,7 +574,7 @@ pub fn renderError(tree: Ast, parse_error: Error, w: *Writer) Writer.Error!void
'/' => "comment",
else => unreachable,
},
std.zig.fmtChar(tok_slice[parse_error.extra.offset..][0..1]),
std.zig.fmtChar(tok_slice[parse_error.extra.offset]),
});
},

View File

@ -64,14 +64,14 @@ pub const Error = union(enum) {
}
};
fn formatMessage(self: []const u8, w: *std.io.Writer) std.io.Writer.Error!void {
fn formatMessage(self: []const u8, w: *std.Io.Writer) std.Io.Writer.Error!void {
// Just writes the string for now, but we're keeping this behind a formatter so we have
// the option to extend it in the future to print more advanced messages (like `Error`
// does) without breaking the API.
try w.writeAll(self);
}
pub fn fmtMessage(self: Note, diag: *const Diagnostics) std.fmt.Formatter([]const u8, Note.formatMessage) {
pub fn fmtMessage(self: Note, diag: *const Diagnostics) std.fmt.Alt([]const u8, Note.formatMessage) {
return .{ .data = switch (self) {
.zoir => |note| note.msg.get(diag.zoir),
.type_check => |note| note.msg,
@ -147,14 +147,14 @@ pub const Error = union(enum) {
diag: *const Diagnostics,
};
fn formatMessage(self: FormatMessage, w: *std.io.Writer) std.io.Writer.Error!void {
fn formatMessage(self: FormatMessage, w: *std.Io.Writer) std.Io.Writer.Error!void {
switch (self.err) {
.zoir => |err| try w.writeAll(err.msg.get(self.diag.zoir)),
.type_check => |tc| try w.writeAll(tc.message),
}
}
pub fn fmtMessage(self: @This(), diag: *const Diagnostics) std.fmt.Formatter(FormatMessage, formatMessage) {
pub fn fmtMessage(self: @This(), diag: *const Diagnostics) std.fmt.Alt(FormatMessage, formatMessage) {
return .{ .data = .{
.err = self,
.diag = diag,
@ -226,7 +226,7 @@ pub const Diagnostics = struct {
return .{ .diag = self };
}
pub fn format(self: *const @This(), w: *std.io.Writer) std.io.Writer.Error!void {
pub fn format(self: *const @This(), w: *std.Io.Writer) std.Io.Writer.Error!void {
var errors = self.iterateErrors();
while (errors.next()) |err| {
const loc = err.getLocation(self);
@ -606,7 +606,7 @@ const Parser = struct {
}
}
fn parseSlicePointer(self: *@This(), T: type, node: Zoir.Node.Index) !T {
fn parseSlicePointer(self: *@This(), T: type, node: Zoir.Node.Index) ParseExprInnerError!T {
switch (node.get(self.zoir)) {
.string_literal => return self.parseString(T, node),
.array_literal => |nodes| return self.parseSlice(T, nodes),
@ -1048,6 +1048,7 @@ const Parser = struct {
name: []const u8,
) error{ OutOfMemory, ParseZon } {
@branchHint(.cold);
const gpa = self.gpa;
const token = if (field) |f| b: {
var buf: [2]Ast.Node.Index = undefined;
const struct_init = self.ast.fullStructInit(&buf, node.getAstNode(self.zoir)).?;
@ -1065,13 +1066,12 @@ const Parser = struct {
};
} else b: {
const msg = "supported: ";
var buf: std.ArrayListUnmanaged(u8) = try .initCapacity(self.gpa, 64);
defer buf.deinit(self.gpa);
const writer = buf.writer(self.gpa);
try writer.writeAll(msg);
var buf: std.ArrayListUnmanaged(u8) = try .initCapacity(gpa, 64);
defer buf.deinit(gpa);
try buf.appendSlice(gpa, msg);
inline for (info.fields, 0..) |field_info, i| {
if (i != 0) try writer.writeAll(", ");
try writer.print("'{f}'", .{std.zig.fmtIdFlags(field_info.name, .{
if (i != 0) try buf.appendSlice(gpa, ", ");
try buf.print(gpa, "'{f}'", .{std.zig.fmtIdFlags(field_info.name, .{
.allow_primitive = true,
.allow_underscore = true,
})});
@ -1079,7 +1079,7 @@ const Parser = struct {
break :b .{
.token = token,
.offset = 0,
.msg = try buf.toOwnedSlice(self.gpa),
.msg = try buf.toOwnedSlice(gpa),
.owned = true,
};
};

File diff suppressed because it is too large Load Diff