mdqy 0.1.3

jq for markdown: query and transform Markdown with a hybrid selector and jq DSL
Documentation
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
//! Tree evaluator.
//!
//! Walks an [`Expr`] over a [`Value`] and yields a stream of results.
//! This is the correctness baseline; [`crate::stream`] handles a
//! narrow subset without allocating a Node tree and is checked
//! against this implementation by `stream_and_tree_agree`.

use std::cmp::Ordering;
use std::collections::BTreeMap;
use std::sync::Arc;

use crate::ast::{attr, Node};
use crate::builtins;
use crate::error::RunError;
use crate::events::plain_text;
use crate::expr::{BinOp, CmpOp, Expr, Literal, ObjKey};
use crate::value::Value;

/// Scope: `$x` variables, user-defined functions, and filter-typed
/// parameters bound by those functions.
#[derive(Debug, Default, Clone)]
pub struct Env {
    bindings: BTreeMap<String, Value>,
    funcs: BTreeMap<String, Arc<UserFn>>,
    filters: BTreeMap<String, Arc<FilterClosure>>,
}

/// A `def name(params): body;` ready to be re-instantiated per call.
#[derive(Debug)]
pub(crate) struct UserFn {
    pub params: Vec<Arc<str>>,
    pub body: Expr,
}

/// Filter-typed argument bound at a call site. The expression has to
/// evaluate against the *caller's* environment, otherwise a recursive
/// `def f(n): ... f(n-1)` rebinds `n` to itself and the lookup loops
/// forever.
#[derive(Debug)]
pub(crate) struct FilterClosure {
    pub expr: Arc<Expr>,
    pub env: Env,
}

impl Env {
    /// Look up a `$x` binding.
    pub fn lookup(&self, name: &str) -> Option<&Value> {
        self.bindings.get(name)
    }

    /// Look up a user-defined function by name.
    pub(crate) fn lookup_func(&self, name: &str) -> Option<Arc<UserFn>> {
        self.funcs.get(name).cloned()
    }

    /// Look up a filter-typed parameter (a `def`'s argument).
    pub(crate) fn lookup_filter(&self, name: &str) -> Option<Arc<FilterClosure>> {
        self.filters.get(name).cloned()
    }

    /// Bind `name` to `value`. Chainable for `--arg` / `--argjson` /
    /// `as $x` rebinds.
    #[must_use]
    pub fn with(mut self, name: impl Into<String>, value: Value) -> Self {
        self.bindings.insert(name.into(), value);
        self
    }

    /// Register a user function.
    pub(crate) fn with_func(mut self, name: &str, f: Arc<UserFn>) -> Self {
        self.funcs.insert(name.to_string(), f);
        self
    }

    /// Bind a filter-typed parameter, capturing the env it should
    /// evaluate against.
    pub(crate) fn with_filter(mut self, name: &str, closure: Arc<FilterClosure>) -> Self {
        self.filters.insert(name.to_string(), closure);
        self
    }
}

type Stream = Box<dyn Iterator<Item = Result<Value, RunError>>>;

/// `a + b`. The `add` builtin folds with this.
pub(crate) fn apply_add(a: &Value, b: &Value) -> Result<Value, RunError> {
    apply_bin(a, BinOp::Add, b)
}

/// Total order matching jq's `sort`/`unique`.
pub(crate) fn value_cmp_for_sort(a: &Value, b: &Value) -> Ordering {
    value_cmp(a, b)
}

/// The dispatch. One match per [`Expr`] variant; helpers handle the
/// cases that aren't one-liners.
pub(crate) fn eval(expr: &Expr, input: Value, env: &Env) -> Stream {
    match expr {
        Expr::Identity => once(Ok(input)),
        Expr::RecurseAll => Box::new(RecurseAll { stack: vec![input] }),
        Expr::Field(name) => once(field(&input, name)),
        Expr::Index(idx) => index_stream(idx, input, env),
        Expr::Slice(lo, hi) => {
            let a = eval_int(lo.as_deref(), &input, env);
            let b = eval_int(hi.as_deref(), &input, env);
            once(a.and_then(|la| b.and_then(|hb| slice(&input, la, hb))))
        }
        Expr::Iterate => iterate(input),
        Expr::Pipe(l, r) => {
            let r = r.clone();
            let env = env.clone();
            Box::new(eval(l, input, &env.clone()).flat_map(move |x| match x {
                Ok(v) => eval(&r, v, &env),
                Err(e) => once(Err(e)),
            }))
        }
        Expr::Comma(a, b) => Box::new(eval(a, input.clone(), env).chain(eval(b, input, env))),
        Expr::Lit(l) => once(Ok(lit(l))),
        Expr::ArrayCtor(inner) => {
            let collected: Result<Vec<Value>, _> = eval(inner, input, env).collect();
            once(collected.map(|v| Value::Array(Arc::new(v))))
        }
        Expr::ObjectCtor(entries) => object(entries, input, env),
        Expr::Cmp(l, op, r) => cmp_stream(l, *op, r, input, env),
        Expr::Bin(l, op, r) => bin_stream(l, *op, r, input, env),
        Expr::Neg(x) => Box::new(eval(x, input, env).map(|r| r.and_then(neg))),
        Expr::Not(x) => Box::new(eval(x, input, env).map(|r| r.map(|v| Value::Bool(!v.truthy())))),
        Expr::If {
            branches,
            else_branch,
        } => if_stream(branches, else_branch.as_deref(), input, env),
        Expr::Var(name) => match env.lookup(name) {
            Some(v) => once(Ok(v.clone())),
            None => once(Err(RunError::Other(format!("${name} is not defined")))),
        },
        Expr::Call { name, args } => dispatch_call(name, args, input, env),
        Expr::Try(inner) => Box::new(eval(inner, input, env).filter_map(Result::ok).map(Ok)),
        Expr::As { bind, name, body } => {
            let body = body.clone();
            let name = name.clone();
            let env = env.clone();
            let outer = input.clone();
            Box::new(eval(bind, input, &env).flat_map(move |r| match r {
                Err(e) => once(Err(e)),
                Ok(v) => {
                    let bound = env.clone().with(name.as_ref(), v);
                    eval(&body, outer.clone(), &bound)
                }
            }))
        }
        Expr::Reduce {
            source,
            var,
            init,
            update,
        } => reduce_fold(source, var, init, update, None, input, env),
        Expr::Foreach {
            source,
            var,
            init,
            update,
            extract,
        } => reduce_fold(
            source,
            var,
            init,
            update,
            Some(extract.as_ref()),
            input,
            env,
        ),
        Expr::Def {
            name,
            params,
            body,
            rest,
        } => {
            let f = Arc::new(UserFn {
                params: params.clone(),
                body: (**body).clone(),
            });
            let new_env = env.clone().with_func(name, f);
            eval(rest, input, &new_env)
        }
        Expr::Assign(..) => once(Err(RunError::NotImplemented {
            feature: "mutation runs via Query::transform_bytes",
        })),
    }
}

// --- stream helpers ---------------------------------------------------------

fn once(r: Result<Value, RunError>) -> Stream {
    Box::new(std::iter::once(r))
}

fn type_err<S: Into<String>>(expected: S, got: &Value) -> RunError {
    RunError::Type {
        expected: expected.into(),
        got: got.type_name().into(),
    }
}

fn lit(l: &Literal) -> Value {
    match l {
        Literal::Null => Value::Null,
        Literal::Bool(b) => Value::Bool(*b),
        Literal::Number(n) => Value::Number(*n),
        Literal::String(s) => Value::String(Arc::from(s.as_ref())),
    }
}

// --- access -----------------------------------------------------------------

fn field(input: &Value, name: &str) -> Result<Value, RunError> {
    match input {
        Value::Null => Ok(Value::Null),
        Value::Object(m) => Ok(m.get(name).cloned().unwrap_or(Value::Null)),
        Value::Node(n) => Ok(node_field(n, name)),
        other => Err(type_err("object or node", other)),
    }
}

fn node_field(n: &Node, name: &str) -> Value {
    match name {
        "kind" => Value::from(n.kind.as_str()),
        "children" => Value::Array(Arc::new(n.children.clone())),
        "text" => Value::from(plain_text(&n.children)),
        "attrs" => {
            let m: BTreeMap<String, Value> = n
                .attrs
                .iter()
                .map(|(k, v)| ((*k).to_string(), v.clone()))
                .collect();
            Value::Object(Arc::new(m))
        }
        _ => n
            .attrs
            .get(attr::by_name(name).unwrap_or(""))
            .cloned()
            .unwrap_or(Value::Null),
    }
}

fn index_stream(idx: &Expr, input: Value, env: &Env) -> Stream {
    let idx = idx.clone();
    let env = env.clone();
    let host = input.clone();
    Box::new(eval(&idx, input, &env).map(move |r| r.and_then(|i| index(&host, &i))))
}

fn index(input: &Value, idx: &Value) -> Result<Value, RunError> {
    match (input, idx) {
        (Value::Null, _) => Ok(Value::Null),
        (Value::Array(a), Value::Number(n)) => Ok(at(a, *n)),
        (Value::Node(n), Value::Number(x)) => Ok(at(&n.children, *x)),
        (Value::Object(m), Value::String(s)) => {
            Ok(m.get(s.as_ref()).cloned().unwrap_or(Value::Null))
        }
        (Value::Node(n), Value::String(s)) => Ok(node_field(n, s)),
        (v, i) => Err(RunError::Type {
            expected: format!("index compatible with {}", v.type_name()),
            got: i.type_name().into(),
        }),
    }
}

fn at(arr: &[Value], n: f64) -> Value {
    let len = arr.len() as i64;
    let i = n as i64;
    let idx = if i < 0 { len + i } else { i };
    if (0..len).contains(&idx) {
        arr[idx as usize].clone()
    } else {
        Value::Null
    }
}

fn slice(input: &Value, lo: Option<i64>, hi: Option<i64>) -> Result<Value, RunError> {
    let arr: &[Value] = match input {
        Value::Array(a) => a,
        Value::Node(n) => &n.children,
        Value::Null => return Ok(Value::Null),
        other => return Err(type_err("array or node", other)),
    };
    let len = arr.len() as i64;
    let clamp = |x: i64| {
        let a = if x < 0 { len + x } else { x };
        a.clamp(0, len) as usize
    };
    let start = clamp(lo.unwrap_or(0));
    let end = clamp(hi.unwrap_or(len));
    Ok(Value::Array(Arc::new(if start <= end {
        arr[start..end].to_vec()
    } else {
        Vec::new()
    })))
}

fn iterate(input: Value) -> Stream {
    if matches!(input, Value::Null) {
        return Box::new(std::iter::empty());
    }
    match children_of(&input) {
        Some(children) => Box::new(children.into_iter().map(Ok)),
        None => once(Err(type_err("array, object, or node", &input))),
    }
}

/// Direct children of `v`. Shared by `.[]` and `..`. Scalars return
/// `None` so callers can pick their own "no children" behaviour.
fn children_of(v: &Value) -> Option<Vec<Value>> {
    Some(match v {
        Value::Array(a) => (**a).clone(),
        Value::Object(m) => m.values().cloned().collect(),
        Value::Node(n) => n.children.clone(),
        _ => return None,
    })
}

fn eval_int(expr: Option<&Expr>, input: &Value, env: &Env) -> Result<Option<i64>, RunError> {
    let Some(e) = expr else { return Ok(None) };
    match eval(e, input.clone(), env).next() {
        Some(Ok(Value::Number(n))) => Ok(Some(n as i64)),
        Some(Ok(other)) => Err(type_err("integer", &other)),
        Some(Err(err)) => Err(err),
        None => Ok(None),
    }
}

// --- compare / binary -------------------------------------------------------

/// Comparison: evaluate both sides, compare, emit a bool. No
/// short-circuit.
fn cmp_stream(l: &Expr, op: CmpOp, r: &Expr, input: Value, env: &Env) -> Stream {
    cross(l, r, input, env, move |lv, rv| {
        Ok(Value::Bool(match op {
            CmpOp::Eq => value_cmp(&lv, &rv).is_eq(),
            CmpOp::Ne => !value_cmp(&lv, &rv).is_eq(),
            CmpOp::Lt => value_cmp(&lv, &rv).is_lt(),
            CmpOp::Le => value_cmp(&lv, &rv).is_le(),
            CmpOp::Gt => value_cmp(&lv, &rv).is_gt(),
            CmpOp::Ge => value_cmp(&lv, &rv).is_ge(),
        }))
    })
}

/// Cross-product of two streams, combined via `f`.
fn cross<F>(l: &Expr, r: &Expr, input: Value, env: &Env, f: F) -> Stream
where
    F: Fn(Value, Value) -> Result<Value, RunError> + Clone + 'static,
{
    let env = env.clone();
    let r = r.clone();
    let outer = input.clone();
    Box::new(eval(l, input, &env).flat_map(move |x| match x {
        Err(e) => once(Err(e)),
        Ok(lv) => {
            let (env, outer, f) = (env.clone(), outer.clone(), f.clone());
            Box::new(eval(&r, outer, &env).map(move |y| y.and_then(|rv| f(lv.clone(), rv))))
                as Stream
        }
    }))
}

fn value_cmp(a: &Value, b: &Value) -> Ordering {
    match (a, b) {
        (Value::Null, Value::Null) => Ordering::Equal,
        (Value::Bool(x), Value::Bool(y)) => x.cmp(y),
        (Value::Number(x), Value::Number(y)) => x.partial_cmp(y).unwrap_or(Ordering::Equal),
        (Value::String(x), Value::String(y)) => x.as_ref().cmp(y.as_ref()),
        (Value::Array(x), Value::Array(y)) => x
            .iter()
            .zip(y.iter())
            .map(|(a, b)| value_cmp(a, b))
            .find(|o| !o.is_eq())
            .unwrap_or_else(|| x.len().cmp(&y.len())),
        _ => type_rank(a).cmp(&type_rank(b)),
    }
}

fn type_rank(v: &Value) -> u8 {
    match v {
        Value::Null => 0,
        Value::Bool(false) => 1,
        Value::Bool(true) => 2,
        Value::Number(_) => 3,
        Value::String(_) => 4,
        Value::Array(_) => 5,
        Value::Object(_) => 6,
        Value::Node(_) => 7,
    }
}

/// `l op r` as an `Expr::Bin`. Short-circuits `and`, `or`, and `//`
/// so the RHS runs only when the LHS doesn't settle the result.
fn bin_stream(l: &Expr, op: BinOp, r: &Expr, input: Value, env: &Env) -> Stream {
    let env = env.clone();
    let r = r.clone();
    let outer = input.clone();
    Box::new(eval(l, input, &env).flat_map(move |x| match x {
        Err(e) => once(Err(e)),
        Ok(lv) => {
            if let Some(v) = short_circuit(&lv, op) {
                return once(Ok(v));
            }
            let (env, outer) = (env.clone(), outer.clone());
            Box::new(eval(&r, outer, &env).map(move |y| y.and_then(|rv| apply_bin(&lv, op, &rv))))
                as Stream
        }
    }))
}

/// Decide on `and`/`or`/`//` from the LHS alone, or `None` to fall
/// through and evaluate the RHS.
fn short_circuit(lv: &Value, op: BinOp) -> Option<Value> {
    match op {
        BinOp::And if !lv.truthy() => Some(Value::Bool(false)),
        BinOp::Or if lv.truthy() => Some(Value::Bool(true)),
        BinOp::Alt if !matches!(lv, Value::Null | Value::Bool(false)) => Some(lv.clone()),
        _ => None,
    }
}

fn apply_bin(a: &Value, op: BinOp, b: &Value) -> Result<Value, RunError> {
    let arith: fn(f64, f64) -> f64 = match op {
        BinOp::And => return Ok(Value::Bool(a.truthy() && b.truthy())),
        BinOp::Or => return Ok(Value::Bool(a.truthy() || b.truthy())),
        BinOp::Alt => return Ok(b.clone()),
        BinOp::Add => return add(a, b),
        BinOp::Sub => |x, y| x - y,
        BinOp::Mul => |x, y| x * y,
        BinOp::Div => |x, y| x / y,
        BinOp::Mod => |x, y| x % y,
    };
    match (a, b) {
        (Value::Number(x), Value::Number(y)) => Ok(Value::Number(arith(*x, *y))),
        _ => Err(RunError::Type {
            expected: "number".into(),
            got: format!("{} op {}", a.type_name(), b.type_name()),
        }),
    }
}

/// `a + b`. Numbers add, strings concat, arrays extend. Null on
/// either side is identity.
fn add(a: &Value, b: &Value) -> Result<Value, RunError> {
    match (a, b) {
        (Value::Number(x), Value::Number(y)) => Ok(Value::Number(x + y)),
        (Value::String(x), Value::String(y)) => Ok(Value::String(Arc::from(format!("{x}{y}")))),
        (Value::Array(x), Value::Array(y)) => {
            let mut out = (**x).clone();
            out.extend_from_slice(y);
            Ok(Value::Array(Arc::new(out)))
        }
        (Value::Null, v) | (v, Value::Null) => Ok(v.clone()),
        _ => Err(RunError::Type {
            expected: "matching numeric/string/array operands".into(),
            got: format!("{} + {}", a.type_name(), b.type_name()),
        }),
    }
}

fn neg(v: Value) -> Result<Value, RunError> {
    if let Value::Number(n) = v {
        Ok(Value::Number(-n))
    } else {
        Err(type_err("number", &v))
    }
}

// --- control flow -----------------------------------------------------------

fn if_stream(
    branches: &[(Expr, Expr)],
    else_branch: Option<&Expr>,
    input: Value,
    env: &Env,
) -> Stream {
    for (cond, then_branch) in branches {
        match eval(cond, input.clone(), env).next() {
            Some(Ok(v)) if v.truthy() => return eval(then_branch, input, env),
            Some(Err(e)) => return once(Err(e)),
            _ => {}
        }
    }
    match else_branch {
        Some(e) => eval(e, input, env),
        None => once(Ok(input)),
    }
}

fn object(entries: &[(ObjKey, Expr)], input: Value, env: &Env) -> Stream {
    let mut map: BTreeMap<String, Value> = BTreeMap::new();
    for (k, v_expr) in entries {
        let key = match k {
            ObjKey::Ident(s) | ObjKey::Str(s) => s.to_string(),
            ObjKey::Expr(e) => match eval(e, input.clone(), env).next() {
                Some(Ok(Value::String(s))) => s.to_string(),
                Some(Ok(other)) => return once(Err(type_err("string key", &other))),
                Some(Err(e)) => return once(Err(e)),
                None => continue,
            },
        };
        let value = match eval(v_expr, input.clone(), env).next().transpose() {
            Ok(v) => v.unwrap_or(Value::Null),
            Err(e) => return once(Err(e)),
        };
        map.insert(key, value);
    }
    once(Ok(Value::Object(Arc::new(map))))
}

// --- recursive descent ------------------------------------------------------

struct RecurseAll {
    stack: Vec<Value>,
}

impl Iterator for RecurseAll {
    type Item = Result<Value, RunError>;

    fn next(&mut self) -> Option<Self::Item> {
        let v = self.stack.pop()?;
        // Push children in reverse so pre-order walks match source.
        if let Some(mut kids) = children_of(&v) {
            kids.reverse();
            self.stack.extend(kids);
        }
        Some(Ok(v))
    }
}

/// Call dispatch. Filter-typed parameters (from `def`) win first,
/// then user-defined functions, then the builtin registry.
fn dispatch_call(name: &Arc<str>, args: &[Expr], input: Value, env: &Env) -> Stream {
    if args.is_empty() {
        if let Some(filter) = env.lookup_filter(name) {
            return eval(&filter.expr, input, &filter.env);
        }
    }
    if let Some(f) = env.lookup_func(name) {
        if args.len() != f.params.len() {
            return once(Err(RunError::Other(format!(
                "{name}: expected {} arg(s), got {}",
                f.params.len(),
                args.len()
            ))));
        }
        // Capture caller's env so each filter argument evaluates in
        // the scope it was passed from, not the callee's scope.
        let caller_env = env.clone();
        let mut new_env = env.clone();
        for (p, a) in f.params.iter().zip(args.iter()) {
            let closure = Arc::new(FilterClosure {
                expr: Arc::new(a.clone()),
                env: caller_env.clone(),
            });
            new_env = new_env.with_filter(p.as_ref(), closure);
        }
        return eval(&f.body, input, &new_env);
    }
    builtins::invoke(name, args, input, env)
        .unwrap_or_else(|| once(Err(RunError::Other(format!("unknown builtin `{name}`")))))
}

/// Shared body for `reduce` and `foreach`. `extract == None` means
/// reduce (yield only the final accumulator); `Some(e)` means foreach
/// (yield `e(acc)` per iteration).
fn reduce_fold(
    source: &Expr,
    var: &Arc<str>,
    init: &Expr,
    update: &Expr,
    extract: Option<&Expr>,
    input: Value,
    env: &Env,
) -> Stream {
    let first_or_null = |expr, val, env: &Env| -> Result<Value, RunError> {
        eval(expr, val, env)
            .next()
            .transpose()
            .map(|o| o.unwrap_or(Value::Null))
    };
    let items: Vec<Value> = match eval(source, input.clone(), env).collect::<Result<_, _>>() {
        Ok(v) => v,
        Err(e) => return once(Err(e)),
    };
    let mut acc = match first_or_null(init, input, env) {
        Ok(v) => v,
        Err(e) => return once(Err(e)),
    };
    let mut out = Vec::new();
    for item in items {
        let bound = env.clone().with(var.as_ref(), item);
        acc = match first_or_null(update, acc, &bound) {
            Ok(v) => v,
            Err(e) => return once(Err(e)),
        };
        if let Some(e) = extract {
            out.push(first_or_null(e, acc.clone(), &bound));
        }
    }
    if extract.is_none() {
        out.push(Ok(acc));
    }
    Box::new(out.into_iter())
}