pub const Error = union(enum) invalid_escape_character: usizeThe character after backslash is missing or not recognized.
expected_hex_digit: usizeExpected hex digit at this index.
empty_unicode_escape_sequence: usizeUnicode escape sequence had no digits with rbrace at this index.
expected_hex_digit_or_rbrace: usizeExpected hex digit or '}' at this index.
invalid_unicode_codepoint: usizeInvalid unicode codepoint at this index.
expected_lbrace: usizeExpected '{' at this index.
expected_rbrace: usizeExpected '}' at this index.
expected_single_quote: usizeExpected ''' at this index.
invalid_character: usizeThe character at this index cannot be represented without an escape sequence.
empty_char_literal: empty_char_literal''. Not returned for string literals.
fn formatMessage(self: FormatMessage, writer: *Writer) Writer.Error!voidpub fn fmt(self: @This(), raw_string: []const u8) std.fmt.Alt(FormatMessage, formatMessage)pub fn offset(err: Error) usize21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112pub const Error = union(enum) {
/// The character after backslash is missing or not recognized.
invalid_escape_character: usize,
/// Expected hex digit at this index.
expected_hex_digit: usize,
/// Unicode escape sequence had no digits with rbrace at this index.
empty_unicode_escape_sequence: usize,
/// Expected hex digit or '}' at this index.
expected_hex_digit_or_rbrace: usize,
/// Invalid unicode codepoint at this index.
invalid_unicode_codepoint: usize,
/// Expected '{' at this index.
expected_lbrace: usize,
/// Expected '}' at this index.
expected_rbrace: usize,
/// Expected '\'' at this index.
expected_single_quote: usize,
/// The character at this index cannot be represented without an escape sequence.
invalid_character: usize,
/// `''`. Not returned for string literals.
empty_char_literal,
const FormatMessage = struct {
err: Error,
raw_string: []const u8,
};
fn formatMessage(self: FormatMessage, writer: *Writer) Writer.Error!void {
switch (self.err) {
.invalid_escape_character => |bad_index| try writer.print(
"invalid escape character: '{c}'",
.{self.raw_string[bad_index]},
),
.expected_hex_digit => |bad_index| try writer.print(
"expected hex digit, found '{c}'",
.{self.raw_string[bad_index]},
),
.empty_unicode_escape_sequence => try writer.writeAll(
"empty unicode escape sequence",
),
.expected_hex_digit_or_rbrace => |bad_index| try writer.print(
"expected hex digit or '}}', found '{c}'",
.{self.raw_string[bad_index]},
),
.invalid_unicode_codepoint => try writer.writeAll(
"unicode escape does not correspond to a valid unicode scalar value",
),
.expected_lbrace => |bad_index| try writer.print(
"expected '{{', found '{c}'",
.{self.raw_string[bad_index]},
),
.expected_rbrace => |bad_index| try writer.print(
"expected '}}', found '{c}'",
.{self.raw_string[bad_index]},
),
.expected_single_quote => |bad_index| try writer.print(
"expected single quote ('), found '{c}'",
.{self.raw_string[bad_index]},
),
.invalid_character => |bad_index| try writer.print(
"invalid byte in string or character literal: '{c}'",
.{self.raw_string[bad_index]},
),
.empty_char_literal => try writer.writeAll(
"empty character literal",
),
}
}
pub fn fmt(self: @This(), raw_string: []const u8) std.fmt.Alt(FormatMessage, formatMessage) {
return .{ .data = .{
.err = self,
.raw_string = raw_string,
} };
}
pub fn offset(err: Error) usize {
return switch (err) {
inline .invalid_escape_character,
.expected_hex_digit,
.empty_unicode_escape_sequence,
.expected_hex_digit_or_rbrace,
.invalid_unicode_codepoint,
.expected_lbrace,
.expected_rbrace,
.expected_single_quote,
.invalid_character,
=> |n| n,
.empty_char_literal => 0,
};
}
}