aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--docs/jq_grammar.md6
-rw-r--r--src/jq/compile.zig7
-rw-r--r--src/jq/parse.zig8
-rw-r--r--src/root.zig7
4 files changed, 26 insertions, 2 deletions
diff --git a/docs/jq_grammar.md b/docs/jq_grammar.md
index 5405e95..ceb0733 100644
--- a/docs/jq_grammar.md
+++ b/docs/jq_grammar.md
@@ -95,6 +95,7 @@ primary:
'true'
'false'
NUMBER
+ STRING
'.'
FIELD
```
@@ -208,7 +209,10 @@ term:
term '[' query ':' ']'
term '[' ':' query ']'
term '?'
- LITERAL
+ 'null'
+ 'true'
+ 'false'
+ NUMBER
STRING
FORMAT
'-' term
diff --git a/src/jq/compile.zig b/src/jq/compile.zig
index fb2a691..78512b9 100644
--- a/src/jq/compile.zig
+++ b/src/jq/compile.zig
@@ -57,7 +57,12 @@ pub const Instr = union(Opcode) {
pub fn deinit(self: Self, allocator: std.mem.Allocator) void {
switch (self) {
.object_key => |key| allocator.free(key),
- .literal => |value| allocator.destroy(value),
+ .literal => |value| {
+ if (value.* == .string) {
+ allocator.free(value.string);
+ }
+ allocator.destroy(value);
+ },
else => {},
}
}
diff --git a/src/jq/parse.zig b/src/jq/parse.zig
index 5df1d14..d197c26 100644
--- a/src/jq/parse.zig
+++ b/src/jq/parse.zig
@@ -352,6 +352,14 @@ fn parsePrimary(allocator: std.mem.Allocator, parse_allocator: std.mem.Allocator
number_node.* = .{ .literal = number_value };
return number_node;
},
+ .string => |s| {
+ _ = try tokens.next();
+ const string_value = try allocator.create(jv.Value);
+ string_value.* = .{ .string = try allocator.dupe(u8, s) };
+ const string_node = try parse_allocator.create(Ast);
+ string_node.* = .{ .literal = string_value };
+ return string_node;
+ },
.dot => {
_ = try tokens.next();
const ast = try parse_allocator.create(Ast);
diff --git a/src/root.zig b/src/root.zig
index cce3ce5..650acdf 100644
--- a/src/root.zig
+++ b/src/root.zig
@@ -42,6 +42,13 @@ fn testRunMultiple(expected: []const []const u8, input: []const u8, query: []con
try std.testing.expectEqual(null, try runtime.next());
}
+test "literals" {
+ try testRun("\"hello\"", "null", "\"hello\"");
+ try testRun("\"\"", "null", "\"\"");
+ try testRun("\"hello\\nworld\"", "null", "\"hello\\nworld\"");
+ try testRun("\"hello\"", "{\"a\":1}", "\"hello\"");
+}
+
test "identity filter" {
try testRun("null", "null", ".");
try testRun("false", "false", ".");