panproto-parse 0.47.1

Tree-sitter full-AST parsers and emitters for panproto language protocols
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
//! The parse / emit pair as a verified asymmetric lens.
//!
//! `parse` and `emit_pretty` together form an asymmetric lens between a
//! source-byte object `B` and a schema-object `S(p)` in the image of
//! `parse_p`:
//!
//! ```text
//!     parse_p :  B  ─▶  (S(p), C)         get-direction (with complement)
//!     emit_p  :  S(p) ─▶ B                put-direction (drops complement)
//! ```
//!
//! The complement `C` carries everything the schema doesn't fix:
//! byte positions, interstitial whitespace, comments. `emit_pretty`
//! reconstitutes a *canonical* element of `parse_p^{-1}(s)` using a
//! whitespace policy in place of the discarded complement.
//!
//! In categorical terms this is a *retraction in the image of the
//! parser*: for every `s ∈ image(parse_p)`, the round-trip
//! `parse_p ∘ emit_p` returns a schema with the same vertex/edge kind
//! multiset as `s`. We don't get pointwise equality because byte
//! positions and interstitial constraints are not part of the
//! emit_pretty image; that's the price of a canonical section.
//!
//! The law-checkers in this module verify the retraction explicitly so
//! that any future change to `emit_pretty` is provably faithful.
//!
//! # Laws
//!
//! ## `EmitParse` (the retraction law)
//!
//! For all `s` in the image of `parse_p`,
//!
//! ```text
//!     kind_multiset(parse_p(emit_p(s))) = kind_multiset(s')
//! ```
//!
//! where `s'` is `s` with the byte-position constraints stripped (the
//! portion that emit_pretty cannot preserve by construction).
//!
//! ## `ParseEmit` (stability under round-trip)
//!
//! For all `b` such that `parse_p(b)` succeeds,
//!
//! ```text
//!     parse_p(emit_p(strip(parse_p(b)))) ≅_kinds strip(parse_p(b))
//! ```
//!
//! That is, the emit_pretty image is a fixed point of the round-trip
//! up to kind-multiset equivalence.

use std::collections::BTreeMap;

use panproto_schema::Schema;

use crate::error::ParseError;
use crate::registry::ParserRegistry;

/// A protolens for the source-bytes ↔ schema relation at one protocol.
///
/// `ParseEmitLens` is parameterised by a protocol name; calls into the
/// underlying `ParserRegistry` thread the parser and grammar through.
pub struct ParseEmitLens<'r> {
    registry: &'r ParserRegistry,
    protocol: String,
}

impl<'r> ParseEmitLens<'r> {
    /// Build a parse/emit lens for `protocol` against `registry`.
    #[must_use]
    pub fn new(registry: &'r ParserRegistry, protocol: impl Into<String>) -> Self {
        Self {
            registry,
            protocol: protocol.into(),
        }
    }

    /// Forward direction: source bytes → schema. The complement (byte
    /// positions, interstitials) lives on the schema as constraints.
    ///
    /// # Errors
    ///
    /// Returns the parser's error if `protocol` is unknown or `source`
    /// is unparseable for the protocol.
    pub fn parse(&self, source: &[u8]) -> Result<Schema, ParseError> {
        self.registry
            .parse_with_protocol(&self.protocol, source, "parse_emit_lens")
    }

    /// Backward direction: schema → canonical source bytes. Drops
    /// complement; output is one canonical representative of the
    /// parse-preimage of `schema`.
    ///
    /// # Errors
    ///
    /// Returns the emitter's error if `protocol` is unknown or the
    /// schema cannot be rendered (e.g. grammar.json was not vendored).
    pub fn emit(&self, schema: &Schema) -> Result<Vec<u8>, ParseError> {
        self.registry
            .emit_pretty_with_protocol(&self.protocol, schema)
    }
}

/// Possible failures when verifying the parse/emit lens laws.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum LawViolation {
    /// The retraction law `parse(emit(s)) ≅_kinds strip(s)` failed.
    #[error("EmitParse law violated for protocol {protocol}: {detail}")]
    EmitParse {
        /// Protocol whose lens failed the law.
        protocol: String,
        /// Human-readable description of the divergence.
        detail: String,
    },
    /// The stability law `parse(emit(strip(parse(b)))) ≅ strip(parse(b))` failed.
    #[error("ParseEmit law violated for protocol {protocol}: {detail}")]
    ParseEmit {
        /// Protocol whose lens failed the law.
        protocol: String,
        /// Human-readable description of the divergence.
        detail: String,
    },
    /// An underlying `parse` or `emit` step returned an error.
    #[error("underlying parse/emit error: {0}")]
    Underlying(#[from] ParseError),
}

/// Strip byte-position constraints from a schema.
///
/// Removes `start-byte`, `end-byte`, and `interstitial-*` constraints:
/// these are the "complement" portion that `emit_pretty` does not
/// reconstruct, so they are projected out before comparing.
pub fn strip_complement(schema: &mut Schema) {
    for constraints in schema.constraints.values_mut() {
        constraints.retain(|c| {
            let s = c.sort.as_ref();
            !(s == "start-byte" || s == "end-byte" || s.starts_with("interstitial-"))
        });
    }
}

/// Vertex-kind multiset, half of the equivalence class used for law checks.
///
/// See also [`edge_multiset`]; both are required for a faithful retraction
/// witness because two schemas can share a vertex-kind multiset while
/// differing in edge structure (e.g. a tree and its mirror).
#[must_use]
pub fn kind_multiset(schema: &Schema) -> BTreeMap<String, usize> {
    let mut map = BTreeMap::new();
    for v in schema.vertices.values() {
        *map.entry(v.kind.to_string()).or_insert(0) += 1;
    }
    map
}

/// Edge-shape multiset: counts of `(src_kind, edge_kind, tgt_kind)` triples.
///
/// Combined with [`kind_multiset`] this is the full witness the retraction
/// law requires — vertex kinds alone leave edge structure unconstrained.
///
/// Note: `Schema::edges` is a `HashMap<Edge, Name>` so distinct edges
/// (different src/tgt vertex IDs) are kept apart, but two edges that
/// happen to share `(src_id, tgt_id, kind, name)` collapse to one
/// entry. The shape multiset projects these structurally-distinct
/// edges onto a kind-level signature, which is the appropriate
/// granularity for the retraction law: `emit_pretty` cannot be expected
/// to preserve vertex IDs (the parser invents fresh ones) but it must
/// preserve the kind-shape of every edge.
#[must_use]
pub fn edge_multiset(schema: &Schema) -> BTreeMap<(String, String, String), usize> {
    let mut map: BTreeMap<(String, String, String), usize> = BTreeMap::new();
    for edge in schema.edges.keys() {
        let src_kind = schema
            .vertices
            .get(&edge.src)
            .map(|v| v.kind.to_string())
            .unwrap_or_default();
        let tgt_kind = schema
            .vertices
            .get(&edge.tgt)
            .map(|v| v.kind.to_string())
            .unwrap_or_default();
        *map.entry((src_kind, edge.kind.to_string(), tgt_kind))
            .or_insert(0) += 1;
    }
    map
}

/// Verify the `EmitParse` law on a given schema:
///
/// ```text
///     kind_multiset(parse(emit(strip(s)))) == kind_multiset(strip(s))
/// ```
///
/// Returns `Ok(())` iff the round-trip preserves the kind multiset.
///
/// # Errors
///
/// Returns [`LawViolation::EmitParse`] when the round-trip changes
/// the kind multiset, or [`LawViolation::Underlying`] if `parse` or
/// `emit` itself fails.
pub fn check_emit_parse(lens: &ParseEmitLens<'_>, schema: &Schema) -> Result<(), LawViolation> {
    let mut stripped = schema.clone();
    strip_complement(&mut stripped);
    let expected_kinds = kind_multiset(&stripped);
    let expected_edges = edge_multiset(&stripped);

    let bytes = lens.emit(&stripped)?;
    let mut round = lens.parse(&bytes)?;
    strip_complement(&mut round);
    let actual_kinds = kind_multiset(&round);
    let actual_edges = edge_multiset(&round);

    if expected_kinds != actual_kinds {
        return Err(LawViolation::EmitParse {
            protocol: lens.protocol.clone(),
            detail: format!(
                "vertex-kind multiset mismatch: expected {} distinct kinds, got {}; \
                 first divergence: {:?}",
                expected_kinds.len(),
                actual_kinds.len(),
                first_divergence(&expected_kinds, &actual_kinds),
            ),
        });
    }
    if expected_edges != actual_edges {
        return Err(LawViolation::EmitParse {
            protocol: lens.protocol.clone(),
            detail: format!(
                "edge-shape multiset mismatch: expected {} distinct edge shapes, got {}",
                expected_edges.len(),
                actual_edges.len(),
            ),
        });
    }
    Ok(())
}

/// Verify the `ParseEmit` stability law on a source byte string:
///
/// ```text
///     kind_multiset(parse(emit(strip(parse(b)))))
///         == kind_multiset(strip(parse(b)))
/// ```
///
/// Returns `Ok(())` iff `b` round-trips through the lens up to kind
/// multiset.
///
/// # Errors
///
/// Returns [`LawViolation::EmitParse`] when the round-trip changes
/// the kind multiset, or [`LawViolation::Underlying`] if `parse` or
/// `emit` itself fails.
pub fn check_parse_emit(lens: &ParseEmitLens<'_>, bytes: &[u8]) -> Result<(), LawViolation> {
    let parsed = lens.parse(bytes)?;
    check_emit_parse(lens, &parsed)
}

fn first_divergence(
    expected: &BTreeMap<String, usize>,
    actual: &BTreeMap<String, usize>,
) -> Option<(String, Option<usize>, Option<usize>)> {
    for (k, &v) in expected {
        if actual.get(k) != Some(&v) {
            return Some((k.clone(), Some(v), actual.get(k).copied()));
        }
    }
    for (k, &v) in actual {
        if !expected.contains_key(k) {
            return Some((k.clone(), None, Some(v)));
        }
    }
    None
}

#[cfg(test)]
#[cfg(feature = "grammars")]
#[allow(clippy::expect_used, clippy::unwrap_used, clippy::panic, dead_code)]
mod tests {
    use super::*;

    fn run_check(protocol: &str, source: &[u8]) {
        let registry = ParserRegistry::new();
        let lens = ParseEmitLens::new(&registry, protocol);
        check_parse_emit(&lens, source)
            .unwrap_or_else(|e| panic!("law check failed for {protocol}: {e}"));
    }

    #[cfg(feature = "lang-json")]
    #[test]
    fn json_lens_satisfies_laws() {
        std::thread::Builder::new()
            .stack_size(32 * 1024 * 1024)
            .spawn(|| run_check("json", br#"{"a": 1, "b": [2, 3]}"#))
            .expect("spawn")
            .join()
            .expect("worker panicked");
    }

    #[cfg(feature = "lang-toml")]
    #[test]
    fn toml_lens_satisfies_laws() {
        std::thread::Builder::new()
            .stack_size(32 * 1024 * 1024)
            .spawn(|| run_check("toml", b"name = \"foo\"\nversion = \"1.0\"\n"))
            .expect("spawn")
            .join()
            .expect("worker panicked");
    }

    #[cfg(feature = "lang-json")]
    #[test]
    fn json_check_emit_parse_directly() {
        std::thread::Builder::new()
            .stack_size(32 * 1024 * 1024)
            .spawn(|| {
                let registry = ParserRegistry::new();
                let lens = ParseEmitLens::new(&registry, "json");
                let parsed = lens.parse(br#"[1, 2, 3]"#).expect("parse");
                check_emit_parse(&lens, &parsed).expect("retraction holds for parsed schema");
            })
            .expect("spawn")
            .join()
            .expect("worker panicked");
    }

    #[cfg(feature = "lang-json")]
    #[test]
    fn strip_complement_removes_byte_constraints_only() {
        std::thread::Builder::new()
            .stack_size(32 * 1024 * 1024)
            .spawn(|| {
                let registry = ParserRegistry::new();
                let lens = ParseEmitLens::new(&registry, "json");
                let mut parsed = lens.parse(br#"{"a": 1}"#).expect("parse");

                let total_constraint_count: usize = parsed.constraints.values().map(Vec::len).sum();
                strip_complement(&mut parsed);
                let stripped_total: usize = parsed.constraints.values().map(Vec::len).sum();

                assert!(
                    stripped_total < total_constraint_count,
                    "strip_complement must remove byte-position constraints"
                );
                // Walker emits chose-alt-fingerprint and similar
                // constraints that strip_complement preserves.
                let preserved = parsed.constraints.values().any(|cs| {
                    cs.iter()
                        .any(|c| c.sort.as_ref() == "chose-alt-fingerprint")
                });
                assert!(
                    preserved,
                    "strip_complement must preserve chose-alt-fingerprint witnesses"
                );
            })
            .expect("spawn")
            .join()
            .expect("worker panicked");
    }

    #[cfg(feature = "lang-json")]
    #[test]
    fn edge_multiset_distinguishes_structurally_different_schemas() {
        std::thread::Builder::new()
            .stack_size(32 * 1024 * 1024)
            .spawn(|| {
                let registry = ParserRegistry::new();
                let lens = ParseEmitLens::new(&registry, "json");
                let s1 = lens.parse(br#"{"a": 1}"#).expect("parse");
                let s2 = lens.parse(br#"[1]"#).expect("parse");
                let m1 = edge_multiset(&s1);
                let m2 = edge_multiset(&s2);
                assert_ne!(
                    m1, m2,
                    "object and array schemas have distinct edge-shape multisets"
                );
            })
            .expect("spawn")
            .join()
            .expect("worker panicked");
    }

    #[test]
    fn first_divergence_finds_count_mismatch() {
        let mut a = BTreeMap::new();
        a.insert("x".to_owned(), 1);
        let mut b = BTreeMap::new();
        b.insert("x".to_owned(), 2);
        assert_eq!(
            first_divergence(&a, &b),
            Some(("x".to_owned(), Some(1), Some(2)))
        );
    }

    #[test]
    fn first_divergence_finds_extra_key_in_actual() {
        let a = BTreeMap::new();
        let mut b = BTreeMap::new();
        b.insert("y".to_owned(), 3);
        assert_eq!(
            first_divergence(&a, &b),
            Some(("y".to_owned(), None, Some(3)))
        );
    }

    #[test]
    fn first_divergence_returns_none_on_match() {
        let mut a = BTreeMap::new();
        a.insert("x".to_owned(), 1);
        let b = a.clone();
        assert_eq!(first_divergence(&a, &b), None);
    }
}