Skip to content
Open
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
62 changes: 32 additions & 30 deletions jqjq.jq
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ eval "$( \
# Notes:
# - AST is more or less identical to the one used by gojq to make it easier to test parser
# - jq bindings $<name>_ is used if <name> is a keyword as jq (not gojq) does not allow it
# - "string_middle" token is used to distingush between a string that could be an index and
# - "string_middle" token is used to distinguish between a string that could be an index and
# a string that is parts of string interpolation.
#

Expand Down Expand Up @@ -227,9 +227,13 @@ def parse:
# https://www.engr.mun.ca/~theo/Misc/exp_parsing.htm#climbing
# filter is used to disable operators, ex in keyval query
def _op_prec_climb($p; filter):
# debug({_op_prec_climb: .}) |
def _ops:
# debug({ops: .}) |
if filter then null
elif .pipe then {prec: 0, name: "|", assoc: "right"}
# TODO: not as keyword?
elif .ident == "as" then {prec: 0, name: "|", assoc: "right"}
# TODO: understand why jq has left associativity for "," but right seems to give correct parse tree
elif .comma then {prec: 1, name: ",", assoc: "right"}
elif .slash_slash then {prec: 2, name: "//", assoc: "right"}
Expand Down Expand Up @@ -263,6 +267,18 @@ def parse:
| ($next | if . != null then _ops end) as $next_op
| if $next_op and $next_op.prec >= $p then
( .[1:] # consume
#| debug({rest: .})
| ( if $next.ident == "as" then
( _p("pattern") as [$rest, $pattern]
| $rest
| _consume(.pipe) as [$rest, $_]
| [$rest, $pattern]
)
else [., null]
end
) as [$rest, $as_pattern]
#| debug({$as_pattern})
| $rest
| ( if $next_op.assoc == "right" then
_op_prec_climb($next_op.prec; filter)
elif $next_op.assoc == "left" then
Expand All @@ -283,6 +299,7 @@ def parse:
, left: ($t | del(.func_defs))
, right: $t1
}
| if $as_pattern then .pattern = $as_pattern end
| if $func_defs then
.func_defs = $func_defs
else .
Expand Down Expand Up @@ -877,21 +894,6 @@ def parse:
, {optional: true}
]
)
//
( _keyword("as")
| _p("pattern") as [$rest, $pattern]
| $rest
| _consume(.pipe)[0]
| _p("query") as [$rest, $body]
| $rest
| [ .
, { bind:
{ body: $body
, patterns: [$pattern]
}
}
]
)
);

# .
Expand Down Expand Up @@ -1327,6 +1329,7 @@ def eval_ast($query; $path; $env; undefined_func):
, suffix_list: [{$optional}]
}
, $op
, $pattern
, $func_defs
}
| ( ( ($func_defs // [])
Expand Down Expand Up @@ -1939,17 +1942,7 @@ def eval_ast($query; $path; $env; undefined_func):
def _e_suffix($suffix; $path; $input; $opt):
( . as [$p, $v]
| $input
| if $suffix.bind then
# as $name | <body>
( $suffix.bind as {$body, patterns: [$pattern]}
| ($pattern | _e_pattern($v)) as $pattern_env
| _e(
$suffix.bind.body;
$path;
$query_env + $pattern_env
)
)
elif $suffix.index then
| if $suffix.index then
# .<index>
try
( $v
Expand Down Expand Up @@ -2028,9 +2021,18 @@ def eval_ast($query; $path; $env; undefined_func):
def _r: _e($right; $path; $query_env);
if $op == "," then _l, _r
elif $op == "|" then
( _e($left; $path; $query_env) as [$p, $v]
| $v
| _e($right; $p; $query_env)
( . as $input
| _e($left; $path; $query_env) as [$p, $v]
| if $pattern then
( ($pattern | _e_pattern($v)) as $pattern_env
| _e(
$right;
$path;
$query_env + $pattern_env
)
)
else $v | _e($right; $p; $query_env)
end
)
elif $op == "or" then _l[1] or _r[1] | [[null], .]
elif $op == "and" then _l[1] and _r[1] | [[null], .]
Expand Down
27 changes: 26 additions & 1 deletion jqjq.test
Original file line number Diff line number Diff line change
Expand Up @@ -1173,10 +1173,35 @@ ascii_downcase
"123@ABCDEFGHIJKLMNOPQSTUVXYZ["
"123@abcdefghijklmnopqstuvxyz["

# precedence of expr as $v | ...
# https://github.com/jqlang/jq/pull/3326
1 + 2 as $x | -$x
null
-3

1 + 2 as $x | 3 | [$x]
null
[3]

# SKIP_JQ
# test below does not work with standard jq because of missing features or
# they test jqjq specific things

1, 2 as $x | 3 | [$x]
null
[1]
[2]

1, 2 | 3 | [.]
null
[3]
[3]

1, 2 as $x | 3 | [.]
null
[3]
[3]

# TODO: proper message
# test binding with path
def t(v; f): try path(v | f) catch "invalid"; [(null, true, false, 1, "abc", [], {}) as $v | t($v; .)]
Expand Down Expand Up @@ -1270,6 +1295,6 @@ null
3

# and env don't leak into eval (maybe should be an option?)
try 3 as $e | eval("$e") catch .
try (3 as $e | eval("$e")) catch .
null
"undefined function $e"