1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#![no_std]
extern crate alloc;
#[cfg(feature = "std")]
extern crate std;
mod error;
mod filter;
mod path;
mod unparse;
mod val;
pub use jaq_parse as parse;
pub use error::Error;
pub use val::Val;
use alloc::{collections::BTreeMap, string::String, vec::Vec};
use parse::{Def, Main};
use unparse::unparse;
pub struct Filter(crate::filter::Filter);
impl Filter {
pub fn run(&self, val: Val) -> val::ValRs {
self.0.run_with_empty_ctx(val)
}
}
pub struct Definitions(BTreeMap<(String, usize), filter::Filter>);
impl Definitions {
pub fn core() -> Self {
Self(filter::Filter::core().into_iter().collect())
}
pub fn insert(&mut self, def: Def, errs: &mut Vec<parse::Error>) {
let f = unparse(&self.get(), &def.args, Vec::new(), def.body, errs);
self.0.insert((def.name, def.args.len()), f);
}
pub fn finish(mut self, (defs, body): Main, errs: &mut Vec<parse::Error>) -> Filter {
defs.into_iter().for_each(|def| self.insert(def, errs));
Filter(unparse(&self.get(), &[], Vec::new(), body, errs))
}
fn get(&self) -> impl Fn(&(String, usize)) -> Option<filter::Filter> + '_ {
|fun| self.0.get(fun).cloned()
}
}