1
1
mirror of https://github.com/wader/fq.git synced 2024-12-26 23:15:04 +03:00
fq/pkg/interp/query.jq

198 lines
3.9 KiB
Plaintext
Raw Normal View History

2021-08-19 19:11:37 +03:00
# a() -> b()
2021-09-04 01:54:51 +03:00
def _query_func_rename(name):
.term.func.name = name;
2021-08-19 19:11:37 +03:00
# . | r
def _query_pipe(r):
{ op: "|",
left: .,
right: r
};
2021-09-07 02:38:52 +03:00
def _query_ident:
{term: {type: "TermTypeIdentity"}};
2021-08-19 19:11:37 +03:00
2021-09-07 02:38:52 +03:00
def _query_try(f):
{ term: {
try: {
body: f,
},
type: "TermTypeTry"
2021-08-19 19:11:37 +03:00
}
};
2021-09-05 14:04:58 +03:00
def _query_func($name; $args):
2021-09-07 02:38:52 +03:00
{ term: {
func: {
args: $args,
name: $name
2021-09-05 14:04:58 +03:00
},
2021-09-07 02:38:52 +03:00
type: "TermTypeFunc"
2021-09-05 14:04:58 +03:00
}
};
def _query_func($name):
_query_func($name; null);
2021-09-04 01:54:51 +03:00
def _query_is_func(name):
.term.func.name == name;
2021-08-19 19:11:37 +03:00
# last query in pipeline
def _query_pipe_last:
2021-09-04 01:54:51 +03:00
if .term.suffix_list then
( .term.suffix_list[-1]
| if .bind.body then
( .bind.body
| _query_pipe_last
2021-08-19 19:11:37 +03:00
)
end
)
2021-09-04 01:54:51 +03:00
elif .op == "|" then
( .right
| _query_pipe_last
)
2021-08-19 19:11:37 +03:00
end;
2021-09-07 02:38:52 +03:00
def _query_transform_pipe_last(f):
2021-08-19 19:11:37 +03:00
def _f:
if .term.suffix_list then
.term.suffix_list[-1] |=
2021-09-04 01:54:51 +03:00
if .bind.body then
.bind.body |= _f
2021-08-19 19:11:37 +03:00
else f
end
2021-09-04 01:54:51 +03:00
elif .op == "|" then
.right |= _f
2021-08-19 19:11:37 +03:00
else f
end;
_f;
2021-09-07 02:38:52 +03:00
# last term, the right most
def _query_last:
if .term.suffix_list then
( .term.suffix_list[-1]
| if .bind.body then
( .bind.body
| _query_last
)
end
)
elif .op then
( .right
| _query_last
)
end;
# TODO: rename? what to call when need to transform suffix_list
def _query_transform_last(f):
def _f:
if .term.suffix_list then
( .
| if .term.suffix_list[-1].bind.body then
.term.suffix_list[-1].bind.body |= _f
else f
end
)
elif .op then
.right |= _f
else f
end;
_f;
def _query_completion_type:
( . as $q
| _query_last
| if .index.name then
{ query:
($q | _query_transform_last(
del(.term.suffix_list[-1])
)),
type: "index",
prefix: .index.name
}
elif .term.index.name then
{ query:
($q | _query_transform_last(
_query_ident
)),
type: "index",
prefix: .term.index.name
}
elif .term.func then
{ query:
($q | _query_transform_last(
_query_ident
)),
type:
( .term.func.name
| if startswith("$") then "var"
else "func"
end
),
prefix: .term.func.name
}
else
null
end
);
# TODO: simplify
def _query_completion(f):
( . as $expr
# HACK: if ends with . or $, add a dummy prefix to make the query
# valid and then trim it later
| ( if (.[-1] | . == "." or . == "$") then "a"
else ""
end
) as $probesuffix
| ($expr + $probesuffix)
| try
( try _query_fromstring
catch .error
# move directives to new root query
| . as {$meta, $imports}
| del(.meta)
| del(.imports)
| _query_completion_type
| . as $c
| if . then
( .query |=
( _query_func("map"; [
_query_pipe(
_query_try(
_query_func($c | f)
)
)
])
| _query_pipe(
_query_func("add")
)
| .meta = $meta
| .imports = $imports
| _query_tostring
)
| .prefix |= rtrimstr($probesuffix)
)
else
{type: "none"}
end
)
catch {type: "error", name: "", error: .}
);
# <filter...> | <slurp_func> ->
# map(<filter...> | .) | (<slurp_func> | f)
2021-08-19 19:11:37 +03:00
def _query_slurp_wrap(f):
2021-09-05 14:04:58 +03:00
# save and move directives to new root query
2021-09-04 01:54:51 +03:00
( . as {$meta, $imports}
| del(.meta)
| del(.imports)
| _query_pipe_last as $lq
2021-09-07 02:38:52 +03:00
| _query_transform_pipe_last(_query_ident) as $pipe
2021-09-05 14:04:58 +03:00
| _query_func("map"; [$pipe])
2021-08-19 19:11:37 +03:00
| _query_pipe($lq | f)
2021-09-04 01:54:51 +03:00
| .meta = $meta
| .imports = $imports
2021-08-19 19:11:37 +03:00
);