jq implementation of jq
See the codejqjq is a jq implementation in jq.
It started out researching how to write format decoders in jq for fq which ended up involving some AST fiddling and then escalated from a joke into a fun and educational project. But it's also a nice way to show off jq as a very expressive, capable and neat language!
It can currently run with jq (1.8 or higher), gojq, jaq and jqjq itself.
This jqplay demo snippet can be used to play around with it.
jqjq wrapper$ ./jqjq -n 'def f: 1,8; [f,f] | map(.+105) | implode'
"jqjq"
$ ./jqjq '.+. | map(.+105) | implode' <<< '[1,8]'
"jqjq"
# evaluate jqjq in jqjq, will take some time and recently only tested with gojq.
# eval the concatenation of jqjq.jq as a string and the filter to run
$ ./jqjq --jq gojq "eval($(jq -Rs . jqjq.jq)+.)" <<< '"eval(\"def f: 1,8; [f,f] | map(.+105) | implode\")"'
"jqjq"
# jqjq has a REPL
$ ./jqjq --repl
> 1,2,3 | .*2
2
4
6
> "jqjq" | explode | map(.-32) | implode
"JQJQ"
> "jqjq" | [eval("explode[] | .-32")] | implode
"JQJQ"
> ^D
# run with gojq or jaq
$ ./jqjq --jq gojq -n 1+2
3
$ ./jqjq --jq jaq -n 1+2
3
# run 01mf02's adaptation of itchyny's bf.jq running fib.bf
$ ./jqjq -n "\"$(cat fib.bf)\" | $(cat bf.jq)"
"1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233"
jq$ jq -n -L . 'include "jqjq"; eval("def f: 1,8; [f,f] | map(.+105) | implode")'
"jqjq"
$ jq -L . 'include "jqjq"; eval("(.+.) | map(.+105) | implode")' <<< '[1,8]'
"jqjq"
# can be used as path expression (only gojq and jaq for now because of jq issues like https://github.com/jqlang/jq/issues/3128)
$ gojq -cn -L . 'include "jqjq"; {a:0, b:1} | eval(".a, .b") += 1'
{"a":1,"b":2}
$ jaq -cn -L . 'include "jqjq"; {a:0, b:1} | eval(".a, .b") += 1'
{"a":1,"b":2}
# run jqjq's tests with jq and jqjq (via jq)
$ make test
# run jqjq's tests with jqjq (via jaq)
$ JQ=jaq make test-jqjq
Note that the tests are meant to be used with jq 1.7.1.
123, .123, 1.23, 1.23e2, 1.23e+2, "abc", true, false, null Scalar literals
"\ud83d\ude03"\ud800-\udfff, should translate to codepoint."\"\n\r\t\f\b\\\/""abc \(123)" String interpolation{key: "value"} Object literal
{key}{"key"}{$key}{(f): f}{("a","b"): (1,2), c: 2} Multiple key/value outputs{"\("abc")": 123} Key string interpolation{key: 1 | .} Multi value queries[1,2,3] Array literal, collect1, 2 Comma operator1 | 2 Pipe operator+, -, *, /, % Arithmetic operators+123, -1 Unary operators==, !=, <, <=, >, >= Comparison operators123 as $a | ... Binding
(1,2,3) as $a | ... Binding per output{a: [123]} as {a: [$v]} Destructuring binding. Identity.a, ."a", .[1], .[f] Index.key[123]."key"[f] Suffix expressions
.a.b Multi index.a.b? Optional index.a[] Iterate index.[]? Try iterate.[] Iterate.[start:stop], .[:stop], .[start:] Array slicing
.[{start: 123, stop: 123}] Slice using objectpath(.[1:2]) -> [{"start":1,"end":2}]try f, Shorthand for try f catch emptyf? Shorthand for try f catch emptyand, or operatorsnot operatorif f then 2 else 3 end Conditional
if f then 2 end Optional elseif f then 2 elif f then 3 end Else if clausesif true,false then "a" else "b" end Multiple condition outputsreduce f as $a (init; update) Reduce outputs from f into one outputforeach f as $a (init; update; extract) Foreach outputs of f update state and output extracted value
f = v Assignmentf |= v, f += Update assignment+=, -=, *=, /=, %= Arithmetic update assignmenteval($expr) (jqjq specific)path(f) Output paths for finput, inputsascii_upcase, ascii_downcaseaddall, all(cond), all(gen; cond)any, any(cond), any(gen; cond)bsearch($target)capture($val), capture(re; mods)debug (passthrough)debug(msgs)del(f)delpaths($paths) (passthrough)empty (passthrough)endswith($s)enverror($v) (passthrough)error (passthrough)explode (passthrough)first(f)firstflatten, flatten($depth)from_entriesfromjsongetpath(path) (passthrough)group, group_by(f)gsub($regex; f) (passthrough)gsub($regex; f; $flags) (passthrough)halt_error, halt_error($exit_code)has($key) (passthrough)implode (passthrough)in(xs)index($i)indices($i)isemptyjoin($s)last(f)lastlength (passthrough)limit($n; f)map(f)match($regex; $flags) (passthrough)match($val)max, max_by(f)min, min_by(f)nth($n; f); nth($n)pathsrange($to), range($from; $to), range($from; $to; $by)recurse, recurse(f)repeatreverserindex($i)scalarsselect(f)setpath (passthrough)sort, sort_by(f)split($s)split($re; flags)splits($re), splits($re; flags)startswith($s)test($val)test($regex; $flags) (passthrough)to_entriestojsontonumber (passthrough)tostring (passthrough)transposetype (passthrough)unique, unique_by(f)until(cond; next)while(cond; update)with_entriessin/0, ... atan/2, ...def f: . Function declaration
def f(lambda): lambda Lambda argument(def f: 123; f) | . Closure functiondef f: def _f: 123; _f; f Local functiondef f($binding): $binding Binding argumentsdef f: f; Recursion.. Recurse input, same as recurse// Alternative operator?// Alternative destructuring operator$ENV@format "abc \(.)" Format string
@base32@base64@csv@html@sh@tsv@uri@json@textlabel $out | break $out Break outinclude "f", import "f" Include--help / -h--null-input / -n--raw-input / -R--slurp / -s--binary / -b--compact-output / -c--raw-output / -r--raw-output0--join-output / -j--color-output / -C--monochrome-output / -M--tab--indent n--unbuffered--from-file / -f-L directory--arg name value--argjson name text--rawfile name filename--slurpfile name filename--args--jsonargs--run-tests--run-tests [filename]----jq--lex--parse--repl--no-builtinsdebug, maths, jqjq lex returns empty (regexp issues?)$ ./jqjq --run-tests < ../jq/tests/jq.test | grep passed
312 of 469 tests passed
Note that expected test values are based on stedolan's jq. If you run with a different jq implementation like gojq some tests might fail because of different error messages, support for arbitrary precision integers etc.
jqjq has the common lex, parse, eval design.
Lexer gets a string and chews off parts from left to right producing an array of tokens [{<name>: ...}, ...]. Each chew is done by testing regex:s in a priority order to make sure to match longer prefixes first, ex: += is matched before +. For a match a lambda is evaluated, usually {<token-name>: .}, but in some cases like for quoted strings it is a bit more complicated.
The lexer also has a stack to keep track of balance of seen (, ) and \( to properly know how to chop of a string with interpolation into tokens. e.g. is ) a right parenthesis or continuation of a string as in "abc \(123) def"?
You can use ./jqjq --lex '...' to lex and see the tokens.
Parser takes an array of tokens and uses a left-to-right (LR) parser with backtracking in combination with precedence climbing for infix operators to not end up in an infinite loop (ex parser rule E -> E + E). Backtracking is done by outputting empty for non-match and // to try the next rule, ex: a // b // ... // error where a and b are functions that try to match a rule. When a rule has matched it returns an array with the pair [<tokens left>, <ast>]. <ast> uses the same AST design as gojq.
You can use ./jqjq --parse '...' to lex and parse and see the AST tree.
Eval is done by traversing the AST tree and evaluates each AST node and also keeps track of the current path and environment.
Path is used in jq to keep track of current path to where you are in the input. The tracking can be done as long as you only operate on the input and don't create and output new values. I.e path(.a[1], .b) outputs ["a",1] and ["b"]. This is also used to implement assignment.
Environment is an object with current functions and bindings. Functions have the key name <name>/<arity> and the value is a function AST. Bindings use the key name $<name>/0 and the value is {value: <value>} where value is normal jq value.
When evaluating the AST eval function get the current AST node, path and environment and will output zero, one or more arrays with the pair [<path>, <value>]. Path can be [null] if the evaluation produced a "new" value etc so that path tracking is not possible.
[] outputs values in wrong order."\(1,2) \(3,4)".== should fail, ex: 1 == 2 == 3.reduce/foreach via recursive function? similar to if or {}-literal?try/catch via some backtrack return value? change [path, value] to include an error somehow?label/break?delpaths (usd by del etc). Have to keep paths the same while deleting a group of paths? use sentinel value? work with paths instead?if and some others do rewrite (optional parts etc) while evaluating.[null] is used as sentinel value.{a:123} | .a |= empty should remove the key.def f(no_used): 123; f(does_not_exist) and def f: $does_not_exist, does_not_exist; 123 should fail.$ENV/$ARGS somehow? extend the test format?builtin_undefined_funcjq -n --debug-dump-disasm '...' show jq byte codejq -n --debug-trace=all '...' show jq byte code run tracejq -n '{a: "hello"} | debug' 2> >(jq -R 'gsub("\u001b\\[.*?m";"") | fromjson' >&2) pretty print debug messagesGOJQ_DEBUG=1 go run -tags gojq_debug cmd/gojq/main.go -n '...' run gojq in debug modefq -n '".a.b" | _query_fromstring' gojq parse tree for stringfq -n '{...} | _query_tostring' jq expression string for gojq parse tree_query_fromstring). gojq also fixes some confusing jq bugs and has better error messages which saves a lot of time.Copyright (c) 2022 Mattias Wadman
jqjq is distributed under the terms of the MIT License.
See the LICENSE file for license details.
(top 24 of 55)
236 followers · starred Nov 2022
261 followers · starred Jul 2024
351 followers · starred Jul 2024
472 followers · starred Nov 2022
jq implementation of jq
See the codejqjq is a jq implementation in jq.
It started out researching how to write format decoders in jq for fq which ended up involving some AST fiddling and then escalated from a joke into a fun and educational project. But it's also a nice way to show off jq as a very expressive, capable and neat language!
It can currently run with jq (1.8 or higher), gojq, jaq and jqjq itself.
This jqplay demo snippet can be used to play around with it.
jqjq wrapper$ ./jqjq -n 'def f: 1,8; [f,f] | map(.+105) | implode'
"jqjq"
$ ./jqjq '.+. | map(.+105) | implode' <<< '[1,8]'
"jqjq"
# evaluate jqjq in jqjq, will take some time and recently only tested with gojq.
# eval the concatenation of jqjq.jq as a string and the filter to run
$ ./jqjq --jq gojq "eval($(jq -Rs . jqjq.jq)+.)" <<< '"eval(\"def f: 1,8; [f,f] | map(.+105) | implode\")"'
"jqjq"
# jqjq has a REPL
$ ./jqjq --repl
> 1,2,3 | .*2
2
4
6
> "jqjq" | explode | map(.-32) | implode
"JQJQ"
> "jqjq" | [eval("explode[] | .-32")] | implode
"JQJQ"
> ^D
# run with gojq or jaq
$ ./jqjq --jq gojq -n 1+2
3
$ ./jqjq --jq jaq -n 1+2
3
# run 01mf02's adaptation of itchyny's bf.jq running fib.bf
$ ./jqjq -n "\"$(cat fib.bf)\" | $(cat bf.jq)"
"1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233"
jq$ jq -n -L . 'include "jqjq"; eval("def f: 1,8; [f,f] | map(.+105) | implode")'
"jqjq"
$ jq -L . 'include "jqjq"; eval("(.+.) | map(.+105) | implode")' <<< '[1,8]'
"jqjq"
# can be used as path expression (only gojq and jaq for now because of jq issues like https://github.com/jqlang/jq/issues/3128)
$ gojq -cn -L . 'include "jqjq"; {a:0, b:1} | eval(".a, .b") += 1'
{"a":1,"b":2}
$ jaq -cn -L . 'include "jqjq"; {a:0, b:1} | eval(".a, .b") += 1'
{"a":1,"b":2}
# run jqjq's tests with jq and jqjq (via jq)
$ make test
# run jqjq's tests with jqjq (via jaq)
$ JQ=jaq make test-jqjq
Note that the tests are meant to be used with jq 1.7.1.
123, .123, 1.23, 1.23e2, 1.23e+2, "abc", true, false, null Scalar literals
"\ud83d\ude03"\ud800-\udfff, should translate to codepoint."\"\n\r\t\f\b\\\/""abc \(123)" String interpolation{key: "value"} Object literal
{key}{"key"}{$key}{(f): f}{("a","b"): (1,2), c: 2} Multiple key/value outputs{"\("abc")": 123} Key string interpolation{key: 1 | .} Multi value queries[1,2,3] Array literal, collect1, 2 Comma operator1 | 2 Pipe operator+, -, *, /, % Arithmetic operators+123, -1 Unary operators==, !=, <, <=, >, >= Comparison operators123 as $a | ... Binding
(1,2,3) as $a | ... Binding per output{a: [123]} as {a: [$v]} Destructuring binding. Identity.a, ."a", .[1], .[f] Index.key[123]."key"[f] Suffix expressions
.a.b Multi index.a.b? Optional index.a[] Iterate index.[]? Try iterate.[] Iterate.[start:stop], .[:stop], .[start:] Array slicing
.[{start: 123, stop: 123}] Slice using objectpath(.[1:2]) -> [{"start":1,"end":2}]try f, Shorthand for try f catch emptyf? Shorthand for try f catch emptyand, or operatorsnot operatorif f then 2 else 3 end Conditional
if f then 2 end Optional elseif f then 2 elif f then 3 end Else if clausesif true,false then "a" else "b" end Multiple condition outputsreduce f as $a (init; update) Reduce outputs from f into one outputforeach f as $a (init; update; extract) Foreach outputs of f update state and output extracted value
f = v Assignmentf |= v, f += Update assignment+=, -=, *=, /=, %= Arithmetic update assignmenteval($expr) (jqjq specific)path(f) Output paths for finput, inputsascii_upcase, ascii_downcaseaddall, all(cond), all(gen; cond)any, any(cond), any(gen; cond)bsearch($target)capture($val), capture(re; mods)debug (passthrough)debug(msgs)del(f)delpaths($paths) (passthrough)empty (passthrough)endswith($s)enverror($v) (passthrough)error (passthrough)explode (passthrough)first(f)firstflatten, flatten($depth)from_entriesfromjsongetpath(path) (passthrough)group, group_by(f)gsub($regex; f) (passthrough)gsub($regex; f; $flags) (passthrough)halt_error, halt_error($exit_code)has($key) (passthrough)implode (passthrough)in(xs)index($i)indices($i)isemptyjoin($s)last(f)lastlength (passthrough)limit($n; f)map(f)match($regex; $flags) (passthrough)match($val)max, max_by(f)min, min_by(f)nth($n; f); nth($n)pathsrange($to), range($from; $to), range($from; $to; $by)recurse, recurse(f)repeatreverserindex($i)scalarsselect(f)setpath (passthrough)sort, sort_by(f)split($s)split($re; flags)splits($re), splits($re; flags)startswith($s)test($val)test($regex; $flags) (passthrough)to_entriestojsontonumber (passthrough)tostring (passthrough)transposetype (passthrough)unique, unique_by(f)until(cond; next)while(cond; update)with_entriessin/0, ... atan/2, ...def f: . Function declaration
def f(lambda): lambda Lambda argument(def f: 123; f) | . Closure functiondef f: def _f: 123; _f; f Local functiondef f($binding): $binding Binding argumentsdef f: f; Recursion.. Recurse input, same as recurse// Alternative operator?// Alternative destructuring operator$ENV@format "abc \(.)" Format string
@base32@base64@csv@html@sh@tsv@uri@json@textlabel $out | break $out Break outinclude "f", import "f" Include--help / -h--null-input / -n--raw-input / -R--slurp / -s--binary / -b--compact-output / -c--raw-output / -r--raw-output0--join-output / -j--color-output / -C--monochrome-output / -M--tab--indent n--unbuffered--from-file / -f-L directory--arg name value--argjson name text--rawfile name filename--slurpfile name filename--args--jsonargs--run-tests--run-tests [filename]----jq--lex--parse--repl--no-builtinsdebug, maths, jqjq lex returns empty (regexp issues?)$ ./jqjq --run-tests < ../jq/tests/jq.test | grep passed
312 of 469 tests passed
Note that expected test values are based on stedolan's jq. If you run with a different jq implementation like gojq some tests might fail because of different error messages, support for arbitrary precision integers etc.
jqjq has the common lex, parse, eval design.
Lexer gets a string and chews off parts from left to right producing an array of tokens [{<name>: ...}, ...]. Each chew is done by testing regex:s in a priority order to make sure to match longer prefixes first, ex: += is matched before +. For a match a lambda is evaluated, usually {<token-name>: .}, but in some cases like for quoted strings it is a bit more complicated.
The lexer also has a stack to keep track of balance of seen (, ) and \( to properly know how to chop of a string with interpolation into tokens. e.g. is ) a right parenthesis or continuation of a string as in "abc \(123) def"?
You can use ./jqjq --lex '...' to lex and see the tokens.
Parser takes an array of tokens and uses a left-to-right (LR) parser with backtracking in combination with precedence climbing for infix operators to not end up in an infinite loop (ex parser rule E -> E + E). Backtracking is done by outputting empty for non-match and // to try the next rule, ex: a // b // ... // error where a and b are functions that try to match a rule. When a rule has matched it returns an array with the pair [<tokens left>, <ast>]. <ast> uses the same AST design as gojq.
You can use ./jqjq --parse '...' to lex and parse and see the AST tree.
Eval is done by traversing the AST tree and evaluates each AST node and also keeps track of the current path and environment.
Path is used in jq to keep track of current path to where you are in the input. The tracking can be done as long as you only operate on the input and don't create and output new values. I.e path(.a[1], .b) outputs ["a",1] and ["b"]. This is also used to implement assignment.
Environment is an object with current functions and bindings. Functions have the key name <name>/<arity> and the value is a function AST. Bindings use the key name $<name>/0 and the value is {value: <value>} where value is normal jq value.
When evaluating the AST eval function get the current AST node, path and environment and will output zero, one or more arrays with the pair [<path>, <value>]. Path can be [null] if the evaluation produced a "new" value etc so that path tracking is not possible.
[] outputs values in wrong order."\(1,2) \(3,4)".== should fail, ex: 1 == 2 == 3.reduce/foreach via recursive function? similar to if or {}-literal?try/catch via some backtrack return value? change [path, value] to include an error somehow?label/break?delpaths (usd by del etc). Have to keep paths the same while deleting a group of paths? use sentinel value? work with paths instead?if and some others do rewrite (optional parts etc) while evaluating.[null] is used as sentinel value.{a:123} | .a |= empty should remove the key.def f(no_used): 123; f(does_not_exist) and def f: $does_not_exist, does_not_exist; 123 should fail.$ENV/$ARGS somehow? extend the test format?builtin_undefined_funcjq -n --debug-dump-disasm '...' show jq byte codejq -n --debug-trace=all '...' show jq byte code run tracejq -n '{a: "hello"} | debug' 2> >(jq -R 'gsub("\u001b\\[.*?m";"") | fromjson' >&2) pretty print debug messagesGOJQ_DEBUG=1 go run -tags gojq_debug cmd/gojq/main.go -n '...' run gojq in debug modefq -n '".a.b" | _query_fromstring' gojq parse tree for stringfq -n '{...} | _query_tostring' jq expression string for gojq parse tree_query_fromstring). gojq also fixes some confusing jq bugs and has better error messages which saves a lot of time.Copyright (c) 2022 Mattias Wadman
jqjq is distributed under the terms of the MIT License.
See the LICENSE file for license details.
(top 24 of 55)
236 followers · starred Nov 2022
261 followers · starred Jul 2024
351 followers · starred Jul 2024
472 followers · starred Nov 2022