Skip to main content

Crate jaq_core

Crate jaq_core 

Source
Expand description

jq language parser, compiler, and interpreter.

This crate allows you to parse, compile, and execute jq-like filters.

The example below demonstrates how to use this crate. See the implementation in the jaq crate if you are interested in more complex use cases, such as lazy JSON file loading, error handling etc.

use jaq_core::{data, unwrap_valr, Compiler, Ctx, Vars};
use jaq_core::load::{Arena, File, Loader};
use jaq_json::{read, Val};

let input = r#"["Hello", "world"]"#;
let input = read::parse_single(&input.as_bytes()).unwrap();
let program = File { code: ".[]", path: () };

// named filters, such as `keys`, `map`, ...
let defs = jaq_core::defs().chain(jaq_std::defs()).chain(jaq_json::defs());
let funs = jaq_core::funs().chain(jaq_std::funs()).chain(jaq_json::funs());

let loader = Loader::new(defs);
let arena = Arena::default();

// parse the filter
let modules = loader.load(&arena, program).unwrap();

// compile the filter
let filter = jaq_core::Compiler::default()
    .with_funs(funs)
    .compile(modules)
    .unwrap();

// context for filter execution
let ctx = Ctx::<data::JustLut<Val>>::new(&filter.lut, Vars::new([]));
// iterator over the output values
let mut out = filter.id.run((ctx, input)).map(unwrap_valr);

assert_eq!(out.next(), Some(Ok(Val::from("Hello".to_owned()))));;
assert_eq!(out.next(), Some(Ok(Val::from("world".to_owned()))));;
assert_eq!(out.next(), None);;

Re-exports§

pub use data::DataT;
pub use val::unwrap_valr;
pub use val::ValR;
pub use val::ValT;
pub use val::ValX;
pub use val::ValXs;

Modules§

box_iter
Boxed iterators.
compile
Program compilation.
data
Mapping from compile-time data types to run-time data types.
load
Combined file loading, lexing, and parsing for multiple modules.
native
Native filter construction tools.
ops
Binary operations.
path
Paths and their parts.
val
Values that can be processed by jaq.

Structs§

Ctx
Filter execution context.
Error
Error that occurred during filter execution.
Exn
Exception.
Native
A filter which is implemented using function pointers.
Vars
List of bindings.

Enums§

Bind
Argument of a definition, such as $v or f in def foo($v; f): ....

Functions§

defs
Minimal set of definitions.
funs
Minimal set of filters that are generic over the value type.

Type Aliases§

Compiler
jq program compiler.
Cv
Combination of context and input value.
Filter
Function from a value to a stream of value results.
Lut
Lookup table for terms and functions.
PathsPtr
Paths function pointer (see Id::paths).
RunPtr
Run function pointer (see Id::run).
UpdatePtr
Update function pointer (see Id::update).