1
1
mirror of https://github.com/wader/fq.git synced 2024-12-23 13:22:58 +03:00

repl: Add comments and query from/to helper

This commit is contained in:
Mattias Wadman 2022-02-18 14:26:55 +01:00
parent 9975ec1aaf
commit ca8cdadbe3
3 changed files with 26 additions and 22 deletions

View File

@ -29,5 +29,4 @@ def base64: empty;
def eval($expr; $filename): empty;
def eval($expr): empty;
def open: empty;
def options: empty;
def scope: empty;

View File

@ -207,3 +207,10 @@ def _query_iter_wrap:
| _query_iter
| _query_pipe($q)
);
# query rewrite helper, takes care of from/to
def _query_fromto(f):
( _query_fromstring
| f
| _query_tostring
);

View File

@ -181,6 +181,7 @@ def _repl_eval($expr):
);
# run read-eval-print-loop
# input is array of inputs to iterate
def _repl($opts): #:: a|(Opts) => @
def _read_expr:
_repeat_break(
@ -195,30 +196,27 @@ def _repl($opts): #:: a|(Opts) => @
end
)
);
def _query_rewrite:
( _query_fromstring
| if _query_pipe_last | _query_is_func("repl") then
_query_slurp_wrap(_query_func_rename("_repl_slurp"))
else
_query_iter_wrap
end
| _query_tostring
);
def _repl_loop:
( . as $c
| try
try
_repl_eval(
( _read_expr
| _query_rewrite as $expr
| $c
| _repl_eval($expr)
| _query_fromto(
if _query_pipe_last | _query_is_func("repl") then
# "... | repl" -> "map(... | .) | _repl_slurp"
_query_slurp_wrap(_query_func_rename("_repl_slurp"))
else
# "..." to -> ".[] | ..."
_query_iter_wrap
end
)
)
catch
if . == "interrupt" then empty
elif . == "eof" then error("break")
elif _eval_is_compile_error then _repl_on_error
else error
end
);
)
catch
if . == "interrupt" then empty
elif . == "eof" then error("break")
elif _eval_is_compile_error then _repl_on_error
else error
end;
if _is_completing | not then
( _options_stack(. + [$opts]) as $_
| _finally(