jlens-schema 0.6.0

Structural metadata inference and JSONata language service (completion, diagnostics, hover)
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
//! Static typing of expressions against a schema.
//!
//! The walk mirrors the evaluator's context rules: each path step is evaluated against the
//! *elements* of the previous step's result (arrays are flattened), predicates see the elements
//! of their step, `@$var` and `:=` bind variables, and block scopes end with the block. The
//! types are sets of schema shapes; `partial` marks that something unknown contributed, in
//! which case no "missing field" warnings are produced.

use std::sync::LazyLock;

use jlens_core::ast::{BinaryOp, LambdaDef, Node, NodeKind, Span, Stage};

use crate::shape::{Field, JsonType, Shape};

/// The type of a value: the shapes it may have.
#[derive(Debug, Clone, Default)]
pub(crate) struct Ty<'s> {
    pub shapes: Vec<&'s Shape>,
    /// Something unknown contributed (a user function, an object constructor, no schema...).
    pub partial: bool,
}

impl<'s> Ty<'s> {
    pub fn unknown() -> Self {
        Ty {
            shapes: Vec::new(),
            partial: true,
        }
    }

    /// A known value without fields (number, string, boolean...).
    pub fn scalar() -> Self {
        Ty::default()
    }

    /// A value of one JSON type without fields: a literal, the result of an operator.
    pub fn literal(ty: JsonType) -> Self {
        static SHAPES: LazyLock<[Shape; 4]> = LazyLock::new(|| {
            [
                JsonType::Null,
                JsonType::Boolean,
                JsonType::Number,
                JsonType::String,
            ]
            .map(|t| Shape {
                types: vec![t],
                ..Shape::default()
            })
        });
        match SHAPES.iter().find(|s| s.types[0] == ty) {
            Some(shape) => Ty::of(shape),
            None => Ty::scalar(),
        }
    }

    pub fn of(shape: &'s Shape) -> Self {
        Ty {
            shapes: vec![shape],
            partial: shape.types.is_empty(),
        }
    }

    pub fn union(mut self, other: Ty<'s>) -> Self {
        for s in other.shapes {
            if !self.shapes.iter().any(|t| std::ptr::eq(*t, s)) {
                self.shapes.push(s);
            }
        }
        self.partial |= other.partial;
        self
    }

    /// The items a path step iterates over: arrays are replaced by their items, recursively.
    pub fn elements(&self) -> Ty<'s> {
        fn push<'s>(shape: &'s Shape, out: &mut Ty<'s>, depth: usize) {
            if shape.types.is_empty() {
                out.partial = true;
                return;
            }
            if shape.types.contains(&JsonType::Array) {
                match &shape.items {
                    Some(items) if depth < 32 => push(items, out, depth + 1),
                    _ => out.partial = true,
                }
            }
            if shape.types.iter().any(|t| *t != JsonType::Array)
                && !out.shapes.iter().any(|s| std::ptr::eq(*s, shape))
            {
                out.shapes.push(shape);
            }
        }
        let mut out = Ty {
            shapes: Vec::new(),
            partial: self.partial,
        };
        for shape in &self.shapes {
            push(shape, &mut out, 0);
        }
        out
    }

    /// The fields available on the (element) shapes, merged by name in first-seen order.
    pub fn fields(&self) -> Vec<(&'s str, &'s Field)> {
        let mut out: Vec<(&'s str, &'s Field)> = Vec::new();
        for shape in &self.elements().shapes {
            for (name, field) in &shape.fields {
                if !out.iter().any(|(n, _)| *n == name) {
                    out.push((name, field));
                }
            }
        }
        out
    }

    /// The value of field `name` on the element shapes.
    pub fn field(&self, name: &str) -> Ty<'s> {
        let elements = self.elements();
        let mut out = Ty {
            shapes: Vec::new(),
            partial: elements.partial,
        };
        for shape in &elements.shapes {
            if let Some(f) = shape.fields.get(name) {
                out = out.union(Ty::of(&f.shape));
            }
        }
        out
    }

    /// Whether this is known to be objects whose fields are all known.
    pub fn is_known_objects(&self) -> bool {
        let elements = self.elements();
        !elements.partial && elements.shapes.iter().any(|s| s.is_object())
    }

    /// The signature symbols of the values this type may have, and of the items of its arrays;
    /// `None` when the type is not fully known. Undefined (`m`) is left out.
    pub fn symbols(&self) -> Option<Symbols> {
        fn symbol(t: JsonType) -> char {
            match t {
                JsonType::Null => 'l',
                JsonType::Boolean => 'b',
                JsonType::Integer | JsonType::Number => 'n',
                JsonType::String => 's',
                JsonType::Array => 'a',
                JsonType::Object => 'o',
            }
        }
        fn add(set: &mut String, c: char) {
            if !set.contains(c) {
                set.push(c);
            }
        }
        if self.partial || self.shapes.is_empty() {
            return None;
        }
        let mut out = Symbols::default();
        for shape in &self.shapes {
            for &t in &shape.types {
                add(&mut out.values, symbol(t));
                if t == JsonType::Array {
                    let items = shape.items.as_deref().filter(|i| !i.types.is_empty())?;
                    for &i in &items.types {
                        add(&mut out.items, symbol(i));
                    }
                }
            }
        }
        Some(out)
    }

    fn all_field_values(&self) -> Ty<'s> {
        let mut out = Ty {
            shapes: Vec::new(),
            partial: self.elements().partial,
        };
        for (_, f) in self.fields() {
            out = out.union(Ty::of(&f.shape));
        }
        out
    }

    fn descendants(&self) -> Ty<'s> {
        fn collect<'s>(shape: &'s Shape, out: &mut Ty<'s>, depth: usize) {
            if depth > 32 {
                out.partial = true;
                return;
            }
            *out = std::mem::take(out).union(Ty::of(shape));
            for f in shape.fields.values() {
                collect(&f.shape, out, depth + 1);
            }
            if let Some(items) = &shape.items {
                collect(items, out, depth + 1);
            }
        }
        let mut out = Ty {
            shapes: Vec::new(),
            partial: self.partial,
        };
        for shape in self.elements().shapes {
            collect(shape, &mut out, 0);
        }
        out
    }
}

/// Signature symbols (`s`, `n`, `a`...), see [`Ty::symbols`].
#[derive(Debug, Clone, Default, PartialEq)]
pub(crate) struct Symbols {
    /// The types of the value.
    pub values: String,
    /// The types of the items of its arrays.
    pub items: String,
}

/// Variables in scope, innermost last.
pub(crate) type Scope<'s> = Vec<(String, Ty<'s>)>;

/// What the walk reports to its visitor.
pub(crate) enum Event<'a, 's> {
    /// A field name used as a path step, with the context it is looked up in.
    Field {
        node: &'a Node,
        name: &'a str,
        ctx: &'a Ty<'s>,
        scope: &'a Scope<'s>,
    },
    /// A variable reference.
    Variable {
        node: &'a Node,
        name: &'a str,
        ty: &'a Ty<'s>,
        scope: &'a Scope<'s>,
    },
    /// A call of a function named by a variable, `$name(...)`, or its partial application.
    Call {
        /// The `$name` node.
        node: &'a Node,
        name: &'a str,
        /// The number of arguments, the value applied with `~>` included.
        args: usize,
        /// The span and type of each argument; `None` for a partial application.
        types: Option<&'a [(Span, Ty<'s>)]>,
    },
    /// A variable bound by `:=`, a function parameter, `@$name` or `#$name`.
    Bind { name: &'a str },
}

/// Functions whose result has the type of their first argument.
const PASSTHROUGH: &[&str] = &[
    "sort", "filter", "reverse", "distinct", "shuffle", "single", "clone", "sift",
];
/// Functions whose result structure is not derived from their arguments.
const OPAQUE: &[&str] = &[
    "map", "each", "zip", "eval", "spread", "match", "lookup", "reduce", "error", "assert", "merge",
];

pub(crate) struct Walker<'s, 'v> {
    pub root: Ty<'s>,
    pub externals: &'v [(String, Ty<'s>)],
    pub visit: &'v mut dyn FnMut(Event<'_, 's>),
}

impl<'s> Walker<'s, '_> {
    fn lookup(&self, name: &str, scope: &Scope<'s>) -> Ty<'s> {
        if let Some((_, t)) = scope.iter().rev().find(|(n, _)| n == name) {
            return t.clone();
        }
        if let Some((_, t)) = self.externals.iter().find(|(n, _)| n == name) {
            return t.clone();
        }
        Ty::unknown()
    }

    /// Walks `node` evaluated against context `ctx`, returning the type of its result.
    pub fn walk(&mut self, node: &Node, ctx: &Ty<'s>, scope: &mut Scope<'s>) -> Ty<'s> {
        // Very deep expressions are typed only down to where the stack allows.
        if jlens_core::stack::exhausted() {
            return Ty::unknown();
        }
        jlens_core::stack::grow(|| self.walk_inner(node, ctx, scope))
    }

    fn walk_inner(&mut self, node: &Node, ctx: &Ty<'s>, scope: &mut Scope<'s>) -> Ty<'s> {
        let ty = match &node.kind {
            NodeKind::Path { steps, .. } => {
                let mark = scope.len();
                let mut cur = ctx.clone();
                let mut result = Ty::unknown();
                for step in steps {
                    let res = self.step(step, &cur, scope);
                    let elements = res.elements();
                    for stage in &step.attrs.stages {
                        if let Stage::Filter { expr, .. } = stage {
                            self.walk(expr, &elements, scope);
                        }
                    }
                    // Other steps are walked whole, group-by included.
                    if let Some(group) = &step.attrs.group
                        && Self::is_simple_step(step)
                    {
                        for (k, v) in &group.pairs {
                            self.walk(k, &elements, scope);
                            self.walk(v, &elements, scope);
                        }
                    }
                    if let Some(index) = &step.attrs.index {
                        (self.visit)(Event::Bind { name: index });
                        scope.push((index.clone(), Ty::scalar()));
                    }
                    match &step.attrs.focus {
                        Some(focus) => {
                            (self.visit)(Event::Bind { name: focus });
                            scope.push((focus.clone(), elements));
                        }
                        None => cur = elements,
                    }
                    result = res;
                }
                if let Some(group) = &node.attrs.group {
                    let elements = result.elements();
                    for (k, v) in &group.pairs {
                        self.walk(k, &elements, scope);
                        self.walk(v, &elements, scope);
                    }
                    result = Ty::unknown();
                }
                scope.truncate(mark);
                return result;
            }
            NodeKind::Name { name } => {
                (self.visit)(Event::Field {
                    node,
                    name,
                    ctx,
                    scope,
                });
                ctx.field(name)
            }
            NodeKind::Wildcard => ctx.all_field_values(),
            NodeKind::Descendant => ctx.descendants(),
            NodeKind::Variable { name } => {
                let ty = match name.as_str() {
                    "" => ctx.clone(),
                    "$" => self.root.clone(),
                    other => self.lookup(other, scope),
                };
                (self.visit)(Event::Variable {
                    node,
                    name,
                    ty: &ty,
                    scope,
                });
                ty
            }
            NodeKind::Binary { op, lhs, rhs } => {
                self.walk(lhs, ctx, scope);
                self.walk(rhs, ctx, scope);
                match op {
                    BinaryOp::Add
                    | BinaryOp::Sub
                    | BinaryOp::Mul
                    | BinaryOp::Div
                    | BinaryOp::Mod => Ty::literal(JsonType::Number),
                    BinaryOp::Concat => Ty::literal(JsonType::String),
                    BinaryOp::Range => Ty::scalar(),
                    _ => Ty::literal(JsonType::Boolean),
                }
            }
            NodeKind::Negate { expr } => {
                self.walk(expr, ctx, scope);
                Ty::literal(JsonType::Number)
            }
            NodeKind::Array { items } => {
                let mut ty = Ty::scalar();
                for item in items {
                    ty = ty.union(self.walk(item, ctx, scope));
                }
                ty
            }
            NodeKind::Object { pairs } => {
                for (k, v) in pairs {
                    self.walk(k, ctx, scope);
                    self.walk(v, ctx, scope);
                }
                Ty::unknown()
            }
            NodeKind::Condition {
                condition,
                then,
                otherwise,
            } => {
                self.walk(condition, ctx, scope);
                let t = self.walk(then, ctx, scope);
                match otherwise {
                    Some(e) => t.union(self.walk(e, ctx, scope)),
                    None => t,
                }
            }
            NodeKind::Block { exprs } => {
                let mark = scope.len();
                let mut last = Ty::unknown();
                for e in exprs {
                    last = self.walk(e, ctx, scope);
                }
                scope.truncate(mark);
                last
            }
            NodeKind::Bind { lhs, rhs } => {
                let ty = self.walk(rhs, ctx, scope);
                if let NodeKind::Variable { name } = &lhs.kind {
                    (self.visit)(Event::Bind { name });
                    scope.push((name.clone(), ty.clone()));
                }
                ty
            }
            NodeKind::Function { procedure, args } => self.call(procedure, args, None, ctx, scope),
            NodeKind::Partial { procedure, args } => {
                self.walk(procedure, ctx, scope);
                if let NodeKind::Variable { name } = &procedure.kind {
                    (self.visit)(Event::Call {
                        node: procedure,
                        name,
                        args: args.len(),
                        types: None,
                    });
                }
                for a in args {
                    self.walk(a, ctx, scope);
                }
                Ty::unknown()
            }
            NodeKind::Apply { lhs, rhs } => {
                let value = self.walk(lhs, ctx, scope);
                match &rhs.kind {
                    NodeKind::Function { procedure, args } => {
                        self.call(procedure, args, Some((lhs.span, value)), ctx, scope)
                    }
                    _ => {
                        self.walk(rhs, ctx, scope);
                        Ty::unknown()
                    }
                }
            }
            NodeKind::Lambda(def) => {
                self.lambda(def, &[], ctx, scope);
                Ty::unknown()
            }
            NodeKind::Transform(def) => {
                let matches = self.walk(&def.pattern, ctx, scope).elements();
                self.walk(&def.update, &matches, scope);
                if let Some(d) = &def.delete {
                    self.walk(d, &matches, scope);
                }
                Ty::unknown()
            }
            NodeKind::Sort { terms } => {
                for t in terms {
                    self.walk(&t.expr, ctx, scope);
                }
                ctx.clone()
            }
            NodeKind::String { .. } => Ty::literal(JsonType::String),
            NodeKind::Number { .. } => Ty::literal(JsonType::Number),
            NodeKind::Bool { .. } => Ty::literal(JsonType::Boolean),
            NodeKind::Null => Ty::literal(JsonType::Null),
            NodeKind::Regex { .. } => Ty::scalar(),
            NodeKind::Parent { .. } | NodeKind::Placeholder | NodeKind::Error { .. } => {
                Ty::unknown()
            }
        };
        let elements = ty.elements();
        for stage in &node.attrs.predicate {
            if let Stage::Filter { expr, .. } = stage {
                self.walk(expr, &elements, scope);
            }
        }
        if let Some(group) = &node.attrs.group {
            for (k, v) in &group.pairs {
                self.walk(k, &elements, scope);
                self.walk(v, &elements, scope);
            }
            return Ty::unknown();
        }
        ty
    }

    /// The result of one path step. Its stages and group-by are handled by the path.
    fn step(&mut self, step: &Node, ctx: &Ty<'s>, scope: &mut Scope<'s>) -> Ty<'s> {
        match &step.kind {
            NodeKind::Name { name } => {
                (self.visit)(Event::Field {
                    node: step,
                    name,
                    ctx,
                    scope,
                });
                ctx.field(name)
            }
            NodeKind::Wildcard => ctx.all_field_values(),
            NodeKind::Descendant => ctx.descendants(),
            NodeKind::Sort { terms } => {
                for t in terms {
                    self.walk(&t.expr, ctx, scope);
                }
                ctx.clone()
            }
            _ => self.walk(step, ctx, scope),
        }
    }

    fn is_simple_step(step: &Node) -> bool {
        matches!(
            step.kind,
            NodeKind::Name { .. }
                | NodeKind::Wildcard
                | NodeKind::Descendant
                | NodeKind::Sort { .. }
        )
    }

    fn lambda(&mut self, def: &LambdaDef, hints: &[Ty<'s>], ctx: &Ty<'s>, scope: &mut Scope<'s>) {
        let mark = scope.len();
        for (i, p) in def.params.iter().enumerate() {
            (self.visit)(Event::Bind { name: p });
            scope.push((p.clone(), hints.get(i).cloned().unwrap_or_else(Ty::unknown)));
        }
        self.walk(&def.body, ctx, scope);
        scope.truncate(mark);
    }

    fn call(
        &mut self,
        procedure: &Node,
        args: &[Node],
        applied: Option<(Span, Ty<'s>)>,
        ctx: &Ty<'s>,
        scope: &mut Scope<'s>,
    ) -> Ty<'s> {
        self.walk(procedure, ctx, scope);
        let name = match &procedure.kind {
            NodeKind::Variable { name } => Some(name.as_str()),
            _ => None,
        };
        let mut typed: Vec<(Span, Ty<'s>)> = applied.into_iter().collect();
        for arg in args {
            let first = typed.first().map_or_else(Ty::unknown, |(_, t)| t.clone());
            match &arg.kind {
                NodeKind::Lambda(def) if typed.len() == 1 => {
                    // Type the parameters of callbacks from the array they iterate over.
                    let elements = first.elements();
                    let hints = match name {
                        Some("map" | "filter" | "single") => vec![elements, Ty::scalar(), first],
                        Some("sort") => vec![elements.clone(), elements],
                        Some("reduce") => vec![Ty::unknown(), elements],
                        _ => Vec::new(),
                    };
                    self.lambda(def, &hints, ctx, scope);
                    typed.push((arg.span, Ty::unknown()));
                }
                _ => typed.push((arg.span, self.walk(arg, ctx, scope))),
            }
        }
        if let Some(name) = name {
            (self.visit)(Event::Call {
                node: procedure,
                name,
                args: typed.len(),
                types: Some(&typed),
            });
        }
        let types: Vec<Ty<'s>> = typed.into_iter().map(|(_, t)| t).collect();
        let builtin = name.is_some_and(|n| {
            jlens_core::function_catalog().iter().any(|f| f.name == n)
                && !scope.iter().any(|(v, _)| v == n)
                && !self.externals.iter().any(|(v, _)| v == n)
        });
        match name {
            Some(n) if builtin && PASSTHROUGH.contains(&n) => {
                types.first().cloned().unwrap_or_else(Ty::unknown)
            }
            Some("append") if builtin => {
                let mut it = types.into_iter();
                let a = it.next().unwrap_or_default();
                a.union(it.next().unwrap_or_default())
            }
            Some(n) if builtin && !OPAQUE.contains(&n) => Ty::scalar(),
            _ => Ty::unknown(),
        }
    }
}