Skip to content

Commit

Permalink
fix: support colon in unquoted strings
Browse files Browse the repository at this point in the history
Fix originally implemented by @freylax in #27
so credits should go that user.

However, since the author hasn't replied I thought
I'd open a new PR for this so we can merge it.

Also fixes the tests in main.
  • Loading branch information
lunjon committed Nov 22, 2024
1 parent bd6a184 commit 80336db
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
.zigmod
deps.zig
zig-cache
.zig-cache
zig-out
103 changes: 103 additions & 0 deletions changes.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
From 1ec1b63a89578280ead4dc2793899be7cbe48663 Mon Sep 17 00:00:00 2001
From: Jonathan Lundholm <[email protected]>
Date: Fri, 22 Nov 2024 13:01:19 +0100
Subject: [PATCH] fix: support colon in unquoted strings

Fix originally implemented by @freylax in #27
so credits should go that user.

However, since the author hasn't replied I thought
I'd open a new PR for this so we can merge it.

Also fixes the tests in main.
---
.gitignore | 1 +
src/Tokenizer.zig | 28 +++++++++++++++++++++++++++-
test/test.zig | 4 ++--
3 files changed, 30 insertions(+), 3 deletions(-)

diff --git a/.gitignore b/.gitignore
index 8fb68bf..aca0ff0 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,4 +2,5 @@
.zigmod
deps.zig
zig-cache
+.zig-cache
zig-out
diff --git a/src/Tokenizer.zig b/src/Tokenizer.zig
index c3d7f5b..b758a0b 100644
--- a/src/Tokenizer.zig
+++ b/src/Tokenizer.zig
@@ -269,7 +269,7 @@ pub fn next(self: *Tokenizer) Token {
},

.literal => switch (c) {
- '\r', '\n', ' ', '\'', '"', ':', ']', '}' => {
+ '\r', '\n', ' ', '\'', '"', ']', '}' => {
result.id = .literal;
break;
},
@@ -279,6 +279,12 @@ pub fn next(self: *Tokenizer) Token {
break;
}
},
+ ':' => {
+ result.id = .literal;
+ if (self.matchesPattern(": ") or self.matchesPattern(":\n") or self.matchesPattern(":\r")) {
+ break;
+ }
+ },
else => {
result.id = .literal;
},
@@ -615,3 +621,23 @@ test "unquoted literals" {
.eof,
});
}
+
+test "unquoted literal containing colon" {
+ try testExpected(
+ \\key1: val:ue
+ \\key2: val::ue
+ , &[_]Token.Id{
+ // key1
+ .literal,
+ .map_value_ind,
+ .space,
+ .literal, // val:ue
+ .new_line,
+ // key2
+ .literal,
+ .map_value_ind,
+ .space,
+ .literal, // val::ue
+ .eof,
+ });
+}
diff --git a/test/test.zig b/test/test.zig
index 41b6a1a..e99cb03 100644
--- a/test/test.zig
+++ b/test/test.zig
@@ -55,7 +55,7 @@ test "simple" {
defer parsed.deinit();

const result = try parsed.parse(Simple);
- const expected = .{
+ const expected = Simple{
.names = &[_][]const u8{ "John Doe", "MacIntosh", "Jane Austin" },
.numbers = &[_]i16{ 10, -8, 6 },
.nested = .{
@@ -180,7 +180,7 @@ test "single lib tbd" {
defer parsed.deinit();

const result = try parsed.parse(LibTbd);
- const expected = .{
+ const expected = LibTbd{
.tbd_version = 4,
.targets = &[_][]const u8{
"x86_64-macos",
--
2.43.0

28 changes: 27 additions & 1 deletion src/Tokenizer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ pub fn next(self: *Tokenizer) Token {
},

.literal => switch (c) {
'\r', '\n', ' ', '\'', '"', ':', ']', '}' => {
'\r', '\n', ' ', '\'', '"', ']', '}' => {
result.id = .literal;
break;
},
Expand All @@ -279,6 +279,12 @@ pub fn next(self: *Tokenizer) Token {
break;
}
},
':' => {
result.id = .literal;
if (self.matchesPattern(": ") or self.matchesPattern(":\n") or self.matchesPattern(":\r")) {
break;
}
},
else => {
result.id = .literal;
},
Expand Down Expand Up @@ -615,3 +621,23 @@ test "unquoted literals" {
.eof,
});
}

test "unquoted literal containing colon" {
try testExpected(
\\key1: val:ue
\\key2: val::ue
, &[_]Token.Id{
// key1
.literal,
.map_value_ind,
.space,
.literal, // val:ue
.new_line,
// key2
.literal,
.map_value_ind,
.space,
.literal, // val::ue
.eof,
});
}
4 changes: 2 additions & 2 deletions test/test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ test "simple" {
defer parsed.deinit();

const result = try parsed.parse(Simple);
const expected = .{
const expected = Simple{
.names = &[_][]const u8{ "John Doe", "MacIntosh", "Jane Austin" },
.numbers = &[_]i16{ 10, -8, 6 },
.nested = .{
Expand Down Expand Up @@ -180,7 +180,7 @@ test "single lib tbd" {
defer parsed.deinit();

const result = try parsed.parse(LibTbd);
const expected = .{
const expected = LibTbd{
.tbd_version = 4,
.targets = &[_][]const u8{
"x86_64-macos",
Expand Down

0 comments on commit 80336db

Please sign in to comment.