2022-02-16 19:10:41 +03:00
|
|
|
include "internal";
|
|
|
|
include "options";
|
2022-02-21 00:25:46 +03:00
|
|
|
include "eval";
|
2021-09-24 16:41:23 +03:00
|
|
|
include "query";
|
2022-02-16 19:10:41 +03:00
|
|
|
include "decode";
|
2022-07-16 19:39:57 +03:00
|
|
|
include "interp";
|
2022-02-16 19:10:41 +03:00
|
|
|
include "funcs";
|
2022-03-02 20:24:01 +03:00
|
|
|
include "ansi";
|
2021-09-24 16:41:23 +03:00
|
|
|
|
2022-02-21 00:25:46 +03:00
|
|
|
# TODO: currently only make sense to allow keywords starting a term or directive
|
2021-09-22 22:08:36 +03:00
|
|
|
def _complete_keywords:
|
2023-07-27 14:21:04 +03:00
|
|
|
[ "and"
|
|
|
|
#"as"
|
|
|
|
#"break"
|
|
|
|
#"catch"
|
|
|
|
, "def"
|
|
|
|
#"elif"
|
|
|
|
#"else"
|
|
|
|
#"end"
|
|
|
|
, "false"
|
|
|
|
, "foreach"
|
|
|
|
, "if"
|
|
|
|
, "import"
|
|
|
|
, "include"
|
|
|
|
, "label"
|
|
|
|
, "module"
|
|
|
|
, "null"
|
|
|
|
, "or"
|
|
|
|
, "reduce"
|
|
|
|
#"then"
|
|
|
|
, "true"
|
|
|
|
, "try"
|
2021-09-22 22:08:36 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
def _complete_scope:
|
2022-08-14 17:01:58 +03:00
|
|
|
[ (scope | map(split("/")[0]) | unique)
|
|
|
|
, _complete_keywords
|
|
|
|
] | add;
|
2022-02-11 20:30:15 +03:00
|
|
|
def _complete_keys:
|
2022-03-03 01:40:33 +03:00
|
|
|
# uses try as []? will not catch errors
|
|
|
|
[try keys[] catch empty, try _extkeys[] catch empty];
|
2021-09-22 22:08:36 +03:00
|
|
|
|
2022-08-14 17:01:58 +03:00
|
|
|
# TODO: "def zzz: null; abc" scope won't see zzz after completion rewrite as def will be inside map(def zzz: null; abc | ...)
|
2021-09-22 22:08:36 +03:00
|
|
|
# TODO: handle variables via ast walk?
|
|
|
|
# TODO: refactor this
|
|
|
|
# TODO: completionMode
|
|
|
|
# TODO: return escaped identifier, not sure current readline implementation supports
|
|
|
|
# modifying "previous" characters if quoting is needed
|
|
|
|
# completions that needs to change previous input, ex: .a\t -> ."a \" b" etc
|
|
|
|
def _complete($line; $cursor_pos):
|
|
|
|
# TODO: reverse this? word or non-ident char?
|
|
|
|
def _is_separator: . as $c | " .;[]()|=" | contains($c);
|
|
|
|
def _is_internal: startswith("_") or startswith("$_");
|
|
|
|
def _query_index_or_key($q):
|
2022-07-16 19:39:57 +03:00
|
|
|
( ([.[] | _eval($q; {}) | type]) as $n
|
2021-09-22 22:08:36 +03:00
|
|
|
| if ($n | all(. == "object")) then "."
|
|
|
|
elif ($n | all(. == "array")) then "[]"
|
|
|
|
else null
|
|
|
|
end
|
|
|
|
);
|
|
|
|
# only complete if at end or there is a whitespace for now
|
2022-04-10 12:51:14 +03:00
|
|
|
if ($line[$cursor_pos] | . == null or _is_separator) then
|
2021-09-22 22:08:36 +03:00
|
|
|
( . as $c
|
|
|
|
| $line[0:$cursor_pos]
|
|
|
|
| . as $line_query
|
|
|
|
# expr -> map(partial-expr | . | f?) | add
|
|
|
|
# TODO: move map/add logic to here?
|
|
|
|
| _query_completion(
|
|
|
|
if .type | . == "func" or . == "var" then "_complete_scope"
|
2022-02-11 20:30:15 +03:00
|
|
|
elif .type == "index" then "_complete_keys"
|
2021-09-22 22:08:36 +03:00
|
|
|
else error("unreachable")
|
|
|
|
end
|
|
|
|
) as {$type, $query, $prefix}
|
2023-07-27 14:21:04 +03:00
|
|
|
| { prefix: $prefix
|
|
|
|
, names: (
|
2021-09-22 22:08:36 +03:00
|
|
|
if $type == "none" then
|
|
|
|
( $c
|
|
|
|
| _query_index_or_key($line_query)
|
|
|
|
| if . then [.] else [] end
|
|
|
|
)
|
|
|
|
else
|
|
|
|
( $c
|
2022-07-16 19:39:57 +03:00
|
|
|
| _eval($query; {})
|
2021-09-22 22:08:36 +03:00
|
|
|
| ($prefix | _is_internal) as $prefix_is_internal
|
|
|
|
| map(
|
|
|
|
select(
|
|
|
|
strings and
|
|
|
|
# TODO: var type really needed? just func?
|
|
|
|
(_is_ident or $type == "var") and
|
2022-02-11 20:30:15 +03:00
|
|
|
((_is_internal | not) or $prefix_is_internal) and
|
2021-09-22 22:08:36 +03:00
|
|
|
startswith($prefix)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
| unique
|
|
|
|
| sort
|
|
|
|
| if length == 1 and .[0] == $prefix then
|
|
|
|
( $c
|
|
|
|
| _query_index_or_key($line_query)
|
|
|
|
| if . then [$prefix+.] else [$prefix] end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
else
|
|
|
|
{prefix: "", names: []}
|
|
|
|
end;
|
|
|
|
def _complete($line): _complete($line; $line | length);
|
|
|
|
|
2021-10-28 12:18:01 +03:00
|
|
|
# empty input []
|
|
|
|
# >* empty>
|
|
|
|
# single input [v]
|
|
|
|
# >* VALUE_PATH VALUE_PREVIEW>
|
|
|
|
# multiple inputs [v,...]
|
|
|
|
# >* VALUE_PATH VALUE_PREVIEW, ...[#]>
|
|
|
|
# single/multi inputs where first input is array [[v,...], ...]
|
|
|
|
# >* [VALUE_PATH VALUE_PREVIEW, ...][#], ...[#]>
|
2022-03-02 20:24:01 +03:00
|
|
|
def _prompt($opts):
|
2021-10-28 12:18:01 +03:00
|
|
|
def _repl_level:
|
|
|
|
(_options_stack | length | if . > 2 then ((.-2) * ">") else empty end);
|
|
|
|
def _value_path:
|
2021-12-09 19:15:21 +03:00
|
|
|
(._path? // []) | if . == [] then empty else _path_to_expr($opts) end;
|
2021-10-28 12:18:01 +03:00
|
|
|
def _value_preview($depth):
|
2022-05-20 17:38:28 +03:00
|
|
|
if $depth == 0 and format == null and _is_array then
|
2021-10-28 12:18:01 +03:00
|
|
|
[ "["
|
|
|
|
, if length == 0 then empty
|
|
|
|
else
|
|
|
|
( (.[0] | _value_preview(1))
|
|
|
|
, if length > 1 then ", ..." else empty end
|
2021-10-27 17:40:39 +03:00
|
|
|
)
|
2021-10-28 12:18:01 +03:00
|
|
|
end
|
2021-09-22 22:08:36 +03:00
|
|
|
, "]"
|
2022-03-02 20:24:01 +03:00
|
|
|
, if length > 1 then
|
|
|
|
( ("[" | _ansi_if($opts; "array"))
|
2023-05-11 18:48:02 +03:00
|
|
|
, ("0" | _ansi_if($opts; "number"))
|
2022-03-02 20:24:01 +03:00
|
|
|
, ":"
|
|
|
|
, (length | tostring | _ansi_if($opts; "number"))
|
|
|
|
, ("]" | _ansi_if($opts; "array"))
|
|
|
|
)
|
|
|
|
else empty
|
|
|
|
end
|
2021-10-28 12:18:01 +03:00
|
|
|
] | join("")
|
|
|
|
else
|
|
|
|
( . as $c
|
|
|
|
| format
|
|
|
|
| if . != null then
|
|
|
|
( .
|
|
|
|
+ if $c._error then "!" else "" end
|
|
|
|
)
|
|
|
|
else
|
2023-05-11 18:48:02 +03:00
|
|
|
( $c
|
|
|
|
| if _is_decode_value then type
|
|
|
|
else (_exttype // type)
|
|
|
|
end
|
|
|
|
)
|
2021-10-28 12:18:01 +03:00
|
|
|
end
|
2022-03-02 20:24:01 +03:00
|
|
|
) | _ansi_if($opts; "prompt_value")
|
2021-09-22 22:08:36 +03:00
|
|
|
end;
|
2021-10-28 12:18:01 +03:00
|
|
|
def _value:
|
|
|
|
[ _value_path
|
|
|
|
, _value_preview(0)
|
|
|
|
] | join(" ");
|
|
|
|
def _values:
|
|
|
|
if length == 0 then "empty"
|
|
|
|
else
|
|
|
|
[ (.[0] | _value)
|
2022-03-02 20:24:01 +03:00
|
|
|
, if length > 1 then
|
|
|
|
( ", ..."
|
|
|
|
, ("[" | _ansi_if($opts; "array"))
|
|
|
|
, ("0" | _ansi_if($opts; "number"))
|
|
|
|
, ":"
|
|
|
|
, (length | tostring | _ansi_if($opts; "number"))
|
|
|
|
, ("]" | _ansi_if($opts; "array"))
|
|
|
|
, "[]"
|
|
|
|
)
|
|
|
|
else empty
|
|
|
|
end
|
2021-10-28 12:18:01 +03:00
|
|
|
] | join("")
|
|
|
|
end;
|
2022-03-02 20:24:01 +03:00
|
|
|
[ (_repl_level | _ansi_if($opts; "prompt_repl_level")) , _values
|
2021-10-28 12:18:01 +03:00
|
|
|
] | join(" ") + "> ";
|
2022-03-03 23:23:31 +03:00
|
|
|
def _prompt: _prompt(null);
|
2022-02-21 00:25:46 +03:00
|
|
|
|
|
|
|
# user expr error
|
|
|
|
def _repl_on_expr_error:
|
2021-11-01 14:57:55 +03:00
|
|
|
( if _eval_is_compile_error then _eval_compile_error_tostring
|
2022-02-21 00:25:46 +03:00
|
|
|
else tostring
|
|
|
|
end
|
|
|
|
| _error_str
|
|
|
|
| println
|
|
|
|
);
|
2022-03-11 14:41:49 +03:00
|
|
|
# other expr error, interrupted or something unexpected happened
|
2022-02-21 00:25:46 +03:00
|
|
|
def _repl_on_error:
|
2022-03-11 14:41:49 +03:00
|
|
|
# was interrupted by user, just ignore
|
|
|
|
if .error | _is_context_canceled_error then empty
|
|
|
|
else halt_error(_exit_code_expr_error)
|
|
|
|
end;
|
2022-02-21 00:25:46 +03:00
|
|
|
# compile error
|
|
|
|
def _repl_on_compile_error:
|
|
|
|
( if .error | _eval_is_compile_error then
|
|
|
|
( # TODO: move, redo as: def _symbols: if unicode then {...} else {...} end?
|
|
|
|
def _arrow_up: if options.unicode then "⬆" else "^" end;
|
|
|
|
if .error.column != 0 then
|
2022-03-03 23:23:31 +03:00
|
|
|
( ((.input | _prompt | length) + .error.column-1) as $pos
|
2022-02-21 00:25:46 +03:00
|
|
|
| " " * $pos + "\(_arrow_up) \(.error.error)"
|
|
|
|
)
|
|
|
|
else
|
|
|
|
( .error
|
|
|
|
| _eval_compile_error_tostring
|
|
|
|
| _error_str
|
|
|
|
)
|
|
|
|
end
|
|
|
|
)
|
|
|
|
else .error | _error_str
|
2021-11-01 14:57:55 +03:00
|
|
|
end
|
2022-02-21 00:25:46 +03:00
|
|
|
| println
|
2021-09-22 22:08:36 +03:00
|
|
|
);
|
2022-03-11 18:22:23 +03:00
|
|
|
def _repl_display:
|
|
|
|
display(_display_default_opts);
|
|
|
|
def _repl_eval($expr; on_error; on_compile_error):
|
2022-02-21 00:25:46 +03:00
|
|
|
eval(
|
|
|
|
$expr;
|
2022-03-11 18:22:23 +03:00
|
|
|
{ slurps:
|
2023-07-27 14:21:04 +03:00
|
|
|
{ repl: "_repl_slurp"
|
|
|
|
, help: "_help_slurp"
|
|
|
|
, slurp: "_slurp"
|
|
|
|
}
|
2022-03-11 18:22:23 +03:00
|
|
|
# input to repl is always array of values to iterate
|
2023-07-27 14:21:04 +03:00
|
|
|
, input_query: (_query_ident | _query_iter) # .[]
|
2023-05-14 16:44:39 +03:00
|
|
|
# each input should be evaluated separately like cli file args, so catch and just print errors
|
2023-07-27 14:21:04 +03:00
|
|
|
, catch_query: _query_func("_repl_on_expr_error")
|
2022-03-11 18:22:23 +03:00
|
|
|
# run display in sub eval so it can be interrupted
|
2023-07-27 14:21:04 +03:00
|
|
|
, output_query: _query_func("_repl_display")
|
2022-02-21 00:25:46 +03:00
|
|
|
};
|
|
|
|
on_error;
|
|
|
|
on_compile_error
|
2021-11-01 18:50:28 +03:00
|
|
|
);
|
2021-09-22 22:08:36 +03:00
|
|
|
|
|
|
|
# run read-eval-print-loop
|
2022-02-18 16:26:55 +03:00
|
|
|
# input is array of inputs to iterate
|
2022-02-21 00:25:46 +03:00
|
|
|
def _repl($opts):
|
2021-09-22 22:08:36 +03:00
|
|
|
def _read_expr:
|
2021-09-23 15:39:03 +03:00
|
|
|
_repeat_break(
|
|
|
|
# both _prompt and _complete want input arrays
|
2023-07-27 14:21:04 +03:00
|
|
|
( _readline(
|
|
|
|
{ prompt: _prompt(options($opts))
|
|
|
|
, complete: "_complete"
|
|
|
|
, timeout: options.completion_timeout
|
|
|
|
}
|
|
|
|
)
|
2021-09-23 15:39:03 +03:00
|
|
|
| if trim == "" then empty
|
|
|
|
else (., error("break"))
|
|
|
|
end
|
|
|
|
)
|
2021-09-22 22:08:36 +03:00
|
|
|
);
|
|
|
|
def _repl_loop:
|
2022-02-18 16:26:55 +03:00
|
|
|
try
|
2022-03-11 18:22:23 +03:00
|
|
|
_repl_eval(
|
|
|
|
_read_expr;
|
|
|
|
_repl_on_error;
|
|
|
|
_repl_on_compile_error
|
2022-02-18 16:26:55 +03:00
|
|
|
)
|
|
|
|
catch
|
|
|
|
if . == "interrupt" then empty
|
|
|
|
elif . == "eof" then error("break")
|
|
|
|
elif _eval_is_compile_error then _repl_on_error
|
|
|
|
else error
|
|
|
|
end;
|
2022-02-21 00:25:46 +03:00
|
|
|
if $opts | type != "object" then
|
|
|
|
error("options must be an object")
|
|
|
|
elif _is_completing | not then
|
2022-02-10 20:46:23 +03:00
|
|
|
( _options_stack(. + [$opts]) as $_
|
|
|
|
| _finally(
|
|
|
|
_repeat_break(_repl_loop);
|
|
|
|
_options_stack(.[:-1])
|
|
|
|
)
|
2021-09-22 22:08:36 +03:00
|
|
|
)
|
2022-02-10 20:46:23 +03:00
|
|
|
else empty
|
|
|
|
end;
|
2021-09-22 22:08:36 +03:00
|
|
|
|
2022-02-21 00:25:46 +03:00
|
|
|
def _repl_slurp_eval($query):
|
|
|
|
try
|
|
|
|
[ eval(
|
|
|
|
$query | _query_tostring;
|
|
|
|
{};
|
|
|
|
_repl_on_expr_error;
|
|
|
|
error
|
|
|
|
)
|
|
|
|
]
|
|
|
|
catch
|
|
|
|
error(.error);
|
2021-09-22 22:08:36 +03:00
|
|
|
|
2022-02-21 00:25:46 +03:00
|
|
|
def _repl_slurp($query):
|
|
|
|
if ($query.slurp_args | length) > 1 then
|
|
|
|
_eval_error("compile"; "repl requires none or one options argument. ex: ... | repl or ... | repl({compact: true})")
|
|
|
|
else
|
|
|
|
# only allow one output for args, multiple would be confusing i think (would start multiples repl:s)
|
|
|
|
( ( if ($query.slurp_args | length) > 0 then
|
|
|
|
first(_repl_slurp_eval($query.slurp_args[0])[])
|
|
|
|
else {}
|
|
|
|
end
|
|
|
|
) as $opts
|
|
|
|
| if $opts | type != "object" then
|
|
|
|
_eval_error("compile"; "options must be an object")
|
|
|
|
end
|
|
|
|
| _repl_slurp_eval($query.rewrite)
|
|
|
|
| _repl($opts)
|
|
|
|
)
|
|
|
|
end;
|
2021-11-19 13:44:23 +03:00
|
|
|
|
2021-09-22 22:08:36 +03:00
|
|
|
# just gives error, call appearing last will be renamed to _repl_slurp
|
2022-02-21 00:25:46 +03:00
|
|
|
def repl($_): error("repl must be last in pipeline. ex: ... | repl");
|
2021-09-22 22:08:36 +03:00
|
|
|
def repl: repl(null);
|
2022-02-21 00:25:46 +03:00
|
|
|
|
|
|
|
def _slurp($query):
|
|
|
|
if ($query.slurp_args | length != 1) then
|
|
|
|
_eval_error("compile"; "slurp requires one string argument. ex: ... | slurp(\"name\")")
|
|
|
|
else
|
|
|
|
# TODO: allow only one output?
|
|
|
|
( _repl_slurp_eval($query.slurp_args[0])[] as $name
|
|
|
|
| if ($name | _is_ident | not) then
|
|
|
|
_eval_error("compile"; "invalid slurp name \"\($name)\", must be a valid identifier. ex: ... | slurp(\"name\")")
|
|
|
|
else
|
|
|
|
( _repl_slurp_eval($query.rewrite) as $v
|
|
|
|
| _slurps(.[$name] |= $v)
|
|
|
|
| empty
|
|
|
|
)
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end;
|
|
|
|
|
|
|
|
def slurp($_): error("slurp must be last in pipeline. ex: ... | slurp(\"name\")");
|
|
|
|
def slurp: slurp(null);
|
|
|
|
|
|
|
|
def spew($name):
|
|
|
|
( _slurps[$name]
|
|
|
|
| if . then .[]
|
|
|
|
else error("no such slurp: \($name)")
|
|
|
|
end
|
|
|
|
);
|
|
|
|
def spew:
|
|
|
|
_slurps;
|