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

Allow commas in unquoted string + empty map #42

Merged
merged 2 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion src/Tokenizer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const testing = std.testing;

buffer: []const u8,
index: usize = 0,
in_flow: usize = 0,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How come it's not enough for in_flow to be a bool?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this handle nesting of list/mapping. If there are several nested list, I need to remember how many closing tag I need to see before setting "in_flow" to false.


pub const Token = struct {
id: Id,
Expand Down Expand Up @@ -173,11 +174,13 @@ pub fn next(self: *Tokenizer) Token {
'[' => {
result.id = .flow_seq_start;
self.index += 1;
self.in_flow += 1;
break;
},
']' => {
result.id = .flow_seq_end;
self.index += 1;
self.in_flow -|= 1;
break;
},
':' => {
Expand All @@ -188,11 +191,13 @@ pub fn next(self: *Tokenizer) Token {
'{' => {
result.id = .flow_map_start;
self.index += 1;
self.in_flow += 1;
break;
},
'}' => {
result.id = .flow_map_end;
self.index += 1;
self.in_flow -|= 1;
break;
},
'\'' => {
Expand Down Expand Up @@ -264,10 +269,16 @@ pub fn next(self: *Tokenizer) Token {
},

.literal => switch (c) {
'\r', '\n', ' ', '\'', '"', ',', ':', ']', '}' => {
'\r', '\n', ' ', '\'', '"', ':', ']', '}' => {
result.id = .literal;
break;
},
',', '[', '{' => {
result.id = .literal;
if (self.in_flow > 0) {
break;
}
},
else => {
result.id = .literal;
},
Expand Down Expand Up @@ -573,3 +584,34 @@ test "quoted literals" {
.eof,
});
}

test "unquoted literals" {
try testExpected(
\\key1: helloWorld
\\key2: hello,world
\\key3: [hello,world]
, &[_]Token.Id{
// key1
.literal,
.map_value_ind,
.space,
.literal, // helloWorld
.new_line,
// key2
.literal,
.map_value_ind,
.space,
.literal, // hello,world
.new_line,
// key3
.literal,
.map_value_ind,
.space,
.flow_seq_start,
.literal, // hello
.comma,
.literal, // world
.flow_seq_end,
.eof,
});
}
4 changes: 4 additions & 0 deletions src/parse.zig
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,11 @@ const Parser = struct {
self.token_it.seekBy(-1);
break;
},
.flow_map_end => {
break;
},
else => {
log.err("Unhandled token in map: {}", .{key});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let me know if you find this is too much

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fine but I think if we had some error reporting layer setup it would have been even better.

// TODO key not being a literal
return error.Unhandled;
},
Expand Down
12 changes: 10 additions & 2 deletions src/parse/test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ test "explicit doc" {

test "leaf in quotes" {
const source =
\\key1: no quotes
\\key1: no quotes, comma
\\key2: 'single quoted'
\\key3: "double quoted"
;
Expand Down Expand Up @@ -101,7 +101,7 @@ test "leaf in quotes" {
const end = tree.tokens[value.base.end];
try testing.expectEqual(start.id, .literal);
try testing.expectEqual(end.id, .literal);
try testing.expectEqualStrings("no quotes", tree.source[start.start..end.end]);
try testing.expectEqualStrings("no quotes, comma", tree.source[start.start..end.end]);
}
}

Expand Down Expand Up @@ -750,6 +750,14 @@ test "empty list" {
);
}

test "empty map" {
try parseSuccess(
\\a:
\\ b: {}
\\ c: { }
);
}

test "comment within a bracketed list is an error" {
try parseError(
\\[ # something
Expand Down