texform-core 0.1.0

Parser, document tree, and serializer for TeXForm (internal; use the texform crate)
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
use texform_core::ast::{
    Argument, ArgumentKind, ArgumentSlot, ArgumentValue, Ast, ContentMode, GroupKind, Node, NodeId,
    NodeKind, ParentLink, Slot,
};

fn explicit_group(ast: &mut Ast, mode: ContentMode) -> NodeId {
    let id = ast.new_node(Node::Group {
        children: Vec::new(),
        kind: GroupKind::Explicit,
        mode,
    });
    ast.assert_invariants();
    id
}

fn content_arg(kind: ArgumentKind, child: NodeId) -> ArgumentSlot {
    Some(Argument {
        kind,
        value: ArgumentValue::MathContent(child),
    })
}

#[test]
fn test_new_ast_starts_with_math_root() {
    let ast = Ast::new();
    let root = ast.root();

    assert_eq!(ast.kind(root), NodeKind::Root);
    assert!(ast.parent(root).is_none());

    match ast.node(root) {
        Node::Root { children, mode } => {
            assert!(children.is_empty());
            assert_eq!(*mode, ContentMode::Math);
        }
        other => panic!("Expected root node, got {:?}", other),
    }

    ast.assert_invariants();
}

#[test]
#[should_panic(expected = "Cannot create detached root node")]
fn test_new_node_rejects_root_variant() {
    let mut ast = Ast::new();
    let _ = ast.new_node(Node::Root {
        children: Vec::new(),
        mode: ContentMode::Math,
    });
}

#[test]
#[should_panic(expected = "Cannot replace node with root variant")]
fn test_replace_node_rejects_root_variant() {
    let mut ast = Ast::new();
    let root = ast.root();

    let child = ast.new_node(Node::Char('x'));
    ast.assert_invariants();
    ast.append_child(root, child);
    ast.assert_invariants();

    let _ = ast.replace_node(
        child,
        Node::Root {
            children: Vec::new(),
            mode: ContentMode::Math,
        },
    );
}

#[test]
fn test_group_child_editing_updates_navigation_and_detached_roots() {
    let mut ast = Ast::new();
    let root = ast.root();

    let a = ast.new_node(Node::Char('a'));
    ast.assert_invariants();
    let b = ast.new_node(Node::Char('b'));
    ast.assert_invariants();
    let c = ast.new_node(Node::Char('c'));
    ast.assert_invariants();

    ast.append_child(root, a);
    ast.assert_invariants();
    ast.append_child(root, c);
    ast.assert_invariants();
    ast.insert_child(root, 1, b);
    ast.assert_invariants();

    assert_eq!(ast.children(root), [a, b, c].as_slice());
    assert_eq!(
        ast.parent(a),
        Some(ParentLink {
            parent: root,
            slot: Slot::GroupChild(0),
        })
    );
    assert_eq!(
        ast.parent(b),
        Some(ParentLink {
            parent: root,
            slot: Slot::GroupChild(1),
        })
    );
    assert_eq!(
        ast.parent(c),
        Some(ParentLink {
            parent: root,
            slot: Slot::GroupChild(2),
        })
    );
    assert_eq!(ast.next_sibling(a), Some(b));
    assert_eq!(ast.next_sibling(b), Some(c));
    assert_eq!(ast.prev_sibling(c), Some(b));
    assert_eq!(ast.prev_sibling(a), None);

    let detached = ast.detach(b);
    ast.assert_invariants();

    assert_eq!(detached, b);
    assert_eq!(ast.children(root), [a, c].as_slice());
    assert_eq!(ast.parent(b), None);
    assert_eq!(ast.next_sibling(a), Some(c));
    assert_eq!(ast.prev_sibling(c), Some(a));

    let removed = ast.remove_detached(detached);
    ast.assert_invariants();

    assert_eq!(removed, Node::Char('b'));
    assert!(!ast.contains(b));
}

#[test]
fn test_command_arg_slots_keep_non_content_values_out_of_tree_edges() {
    let mut ast = Ast::new();
    let root = ast.root();

    let group = explicit_group(&mut ast, ContentMode::Math);
    let x = ast.new_node(Node::Char('x'));
    ast.assert_invariants();
    ast.append_child(group, x);
    ast.assert_invariants();

    let command = ast.new_node(Node::Command {
        name: "probe".to_string(),
        args: vec![
            Some(Argument {
                kind: ArgumentKind::Star,
                value: ArgumentValue::Boolean(true),
            }),
            None,
            content_arg(ArgumentKind::Mandatory, group),
            Some(Argument {
                kind: ArgumentKind::Optional,
                value: ArgumentValue::Dimension("1em".to_string()),
            }),
        ],
        known: true,
    });
    ast.assert_invariants();

    ast.append_child(root, command);
    ast.assert_invariants();

    let slots = ast.arg_slots(command);
    assert_eq!(slots.len(), 4);

    let star = slots[0]
        .as_ref()
        .unwrap_or_else(|| panic!("Expected star slot to be present"));
    assert_eq!(star.kind, ArgumentKind::Star);
    assert_eq!(star.value, ArgumentValue::Boolean(true));

    assert!(slots[1].is_none());

    let dimension = slots[3]
        .as_ref()
        .unwrap_or_else(|| panic!("Expected dimension slot to be present"));
    assert_eq!(dimension.value, ArgumentValue::Dimension("1em".to_string()));

    assert_eq!(ast.edges(command), vec![(group, Slot::Argument(2))]);
    assert_eq!(
        ast.parent(group),
        Some(ParentLink {
            parent: command,
            slot: Slot::Argument(2),
        })
    );
    assert_eq!(
        ast.find(root, |node| matches!(node, Node::Char('x'))),
        Some(x)
    );
    assert_eq!(
        ast.find_all(root, |node| matches!(node, Node::Char(_))),
        vec![x]
    );
}

#[test]
fn test_command_arg_slots_keep_scalar_variants_opaque() {
    let mut ast = Ast::new();
    let root = ast.root();

    let content = explicit_group(&mut ast, ContentMode::Math);
    let x = ast.new_node(Node::Char('x'));
    ast.append_child(content, x);

    let command = ast.new_node(Node::Command {
        name: "probe".to_string(),
        args: vec![
            Some(Argument {
                kind: ArgumentKind::Optional,
                value: ArgumentValue::Integer("12".to_string()),
            }),
            Some(Argument {
                kind: ArgumentKind::Mandatory,
                value: ArgumentValue::MathContent(content),
            }),
        ],
        known: true,
    });
    ast.append_child(root, command);
    ast.assert_invariants();

    assert_eq!(ast.edges(command), vec![(content, Slot::Argument(1))]);
}

#[test]
fn test_replace_node_reuses_children_and_detaches_removed_subtrees() {
    let mut ast = Ast::new();
    let root = ast.root();

    let lhs = explicit_group(&mut ast, ContentMode::Math);
    let a = ast.new_node(Node::Char('a'));
    ast.assert_invariants();
    ast.append_child(lhs, a);
    ast.assert_invariants();

    let rhs = explicit_group(&mut ast, ContentMode::Math);
    let b = ast.new_node(Node::Char('b'));
    ast.assert_invariants();
    ast.append_child(rhs, b);
    ast.assert_invariants();

    let command = ast.new_node(Node::Command {
        name: "pair".to_string(),
        args: vec![
            content_arg(ArgumentKind::Mandatory, lhs),
            content_arg(ArgumentKind::Mandatory, rhs),
        ],
        known: true,
    });
    ast.assert_invariants();
    ast.append_child(root, command);
    ast.assert_invariants();

    let sub_id = ast.new_node(Node::Char('i'));
    ast.assert_invariants();

    let old_node = ast.replace_node(
        command,
        Node::Scripted {
            base: lhs,
            subscript: Some(sub_id),
            superscript: None,
        },
    );
    ast.assert_invariants();

    match old_node {
        Node::Command { name, args, .. } => {
            assert_eq!(name, "pair");
            assert_eq!(args.len(), 2);
        }
        other => panic!("Expected old command node, got {:?}", other),
    }

    match ast.node(command) {
        Node::Scripted {
            base,
            subscript,
            superscript,
        } => {
            assert_eq!(*base, lhs);
            assert_eq!(*subscript, Some(sub_id));
            assert_eq!(*superscript, None);
        }
        other => panic!("Expected scripted node, got {:?}", other),
    }

    assert_eq!(
        ast.parent(lhs),
        Some(ParentLink {
            parent: command,
            slot: Slot::ScriptBase,
        })
    );
    assert_eq!(
        ast.parent(sub_id),
        Some(ParentLink {
            parent: command,
            slot: Slot::ScriptSub,
        })
    );
    assert_eq!(ast.parent(rhs), None);

    let removed_rhs = ast.remove_detached(rhs);
    ast.assert_invariants();

    match removed_rhs {
        Node::Group { kind, mode, .. } => {
            assert_eq!(kind, GroupKind::Explicit);
            assert_eq!(mode, ContentMode::Math);
        }
        other => panic!("Expected detached group, got {:?}", other),
    }

    assert!(!ast.contains(rhs));
    assert!(!ast.contains(b));
}

#[test]
fn test_remove_node_deletes_attached_subtree() {
    let mut ast = Ast::new();
    let root = ast.root();

    let numerator = explicit_group(&mut ast, ContentMode::Math);
    let a = ast.new_node(Node::Char('a'));
    ast.assert_invariants();
    ast.append_child(numerator, a);
    ast.assert_invariants();

    let denominator = explicit_group(&mut ast, ContentMode::Math);
    let b = ast.new_node(Node::Char('b'));
    ast.assert_invariants();
    ast.append_child(denominator, b);
    ast.assert_invariants();

    let frac = ast.new_node(Node::Command {
        name: "frac".to_string(),
        args: vec![
            content_arg(ArgumentKind::Mandatory, numerator),
            content_arg(ArgumentKind::Mandatory, denominator),
        ],
        known: true,
    });
    ast.assert_invariants();
    ast.append_child(root, frac);
    ast.assert_invariants();

    ast.remove_node(frac);
    ast.assert_invariants();

    assert!(ast.children(root).is_empty());
    assert!(!ast.contains(frac));
    assert!(!ast.contains(numerator));
    assert!(!ast.contains(denominator));
    assert!(!ast.contains(a));
    assert!(!ast.contains(b));
}

#[test]
#[should_panic(expected = "Cannot attach child that already has a parent")]
fn test_append_child_rejects_attached_child() {
    let mut ast = Ast::new();
    let root = ast.root();

    let group = explicit_group(&mut ast, ContentMode::Math);
    let child = ast.new_node(Node::Char('x'));
    ast.assert_invariants();

    ast.append_child(root, group);
    ast.assert_invariants();
    ast.append_child(group, child);
    ast.assert_invariants();

    ast.append_child(root, child);
}

#[test]
#[should_panic(expected = "Can only detach GroupChild nodes")]
fn test_detach_rejects_non_group_child_nodes() {
    let mut ast = Ast::new();
    let root = ast.root();

    let arg = explicit_group(&mut ast, ContentMode::Math);
    let command = ast.new_node(Node::Command {
        name: "cmd".to_string(),
        args: vec![content_arg(ArgumentKind::Mandatory, arg)],
        known: true,
    });
    ast.assert_invariants();

    ast.append_child(root, command);
    ast.assert_invariants();

    ast.detach(arg);
}

#[test]
#[should_panic(expected = "Can only remove detached roots")]
fn test_remove_detached_rejects_attached_nodes() {
    let mut ast = Ast::new();
    let root = ast.root();

    let child = ast.new_node(Node::Char('x'));
    ast.assert_invariants();

    ast.append_child(root, child);
    ast.assert_invariants();

    ast.remove_detached(child);
}

#[test]
#[should_panic(expected = "Environment body must be a Group node")]
fn test_environment_body_must_be_group() {
    let mut ast = Ast::new();

    let body = ast.new_node(Node::Char('x'));
    ast.assert_invariants();

    let _ = ast.new_node(Node::Environment {
        name: "aligned".to_string(),
        args: vec![],
        known: true,
        body,
    });
}