texform-transform 0.1.0

Profile-based AST transform engine 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
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
//! FlattenGroups removes structurally redundant explicit and implicit groups.

use crate::ast::{ArgumentValue, Ast, ContentMode, GroupKind, Node, NodeId, ParentLink, Slot};

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct FlattenGroupsConfig {
    pub enabled: bool,
    /// Semantic guard. Keep groups whose subtree contains a declarative command
    /// (for example `\cal` or `\bf`) to avoid leaking declarative scope into
    /// following siblings.
    pub preserve_group_containing_declarative_command: bool,
    /// Semantic guard. Keep groups occupying a `ScriptBase` slot to avoid
    /// changing which atom subscripts or superscripts attach to.
    pub preserve_group_in_script_base_slot: bool,
    /// Semantic guard. Keep all groups inside an environment body to preserve
    /// cell boundaries and intra-cell spacing.
    pub preserve_group_inside_env_body: bool,
    /// Semantic guard. Keep a `GroupChild` whose subtree contains an
    /// `\over`-style infix to preserve the infix scope.
    pub preserve_group_containing_infix: bool,
    /// Spacing guard. Keep a `GroupChild` when its preceding sibling or its
    /// first child is command-like.
    pub preserve_group_adjacent_to_command_like: bool,
    /// Spacing guard. Keep a risky group directly used as an argument of a
    /// command to preserve one spacing boundary without preserving redundant
    /// nesting.
    pub preserve_group_as_argument_of_command: bool,
    /// Spacing guard (sub-flag). Recurse through `Scripted` bases when
    /// classifying "command-like" for the adjacency check above.
    pub preserve_group_after_scripted_command_like: bool,
    /// Spacing guard. Keep empty `GroupChild`s (`{}`) to preserve spacing and
    /// kerning effects.
    pub preserve_empty_group: bool,
    /// Spacing guard. Keep singleton groups containing only one math
    /// atom-spacing character.
    pub preserve_group_with_lone_atom_spacing_char: bool,
    /// Spacing guard. Keep multi-child `GroupChild`s whose first child is a
    /// math atom-spacing character.
    pub preserve_group_starting_with_atom_spacing_char: bool,
    /// Spacing guard. Keep a `GroupChild` whose subtree contains a
    /// `\left...\right` delimited group.
    pub preserve_group_containing_delimited_pair: bool,
}

impl FlattenGroupsConfig {
    /// All preserve guards on.
    pub const STRICT: Self = Self {
        enabled: true,
        preserve_group_containing_declarative_command: true,
        preserve_group_in_script_base_slot: true,
        preserve_group_inside_env_body: true,
        preserve_group_containing_infix: true,
        preserve_group_adjacent_to_command_like: true,
        preserve_group_as_argument_of_command: true,
        preserve_group_after_scripted_command_like: true,
        preserve_empty_group: true,
        preserve_group_with_lone_atom_spacing_char: true,
        preserve_group_starting_with_atom_spacing_char: true,
        preserve_group_containing_delimited_pair: true,
    };
    /// Only Semantic guards on. All Spacing guards off.
    pub const STRUCTURAL_ONLY: Self = Self {
        enabled: true,
        preserve_group_containing_declarative_command: true,
        preserve_group_in_script_base_slot: true,
        preserve_group_inside_env_body: true,
        preserve_group_containing_infix: true,
        preserve_group_adjacent_to_command_like: false,
        preserve_group_as_argument_of_command: false,
        preserve_group_after_scripted_command_like: false,
        preserve_empty_group: false,
        preserve_group_with_lone_atom_spacing_char: false,
        preserve_group_starting_with_atom_spacing_char: false,
        preserve_group_containing_delimited_pair: false,
    };
    pub const ENABLED: Self = Self::STRICT;
    pub const DISABLED: Self = Self {
        enabled: false,
        ..Self::STRICT
    };
    pub const DEFAULTS: Self = Self::STRICT;
}

#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct FlattenGroupsReport {
    pub actions: FlattenGroupsActionCounts,
    pub guards: FlattenGroupsGuardCounts,
}

#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct FlattenGroupsActionCounts {
    pub removed_empty: usize,
    pub replaced_single_child: usize,
    pub inlined_multi_child: usize,
    pub unwrapped_slot: usize,
}

#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct FlattenGroupsGuardCounts {
    pub preserve_group_containing_declarative_command: usize,
    pub preserve_group_in_script_base_slot: usize,
    pub preserve_group_inside_env_body: usize,
    pub preserve_group_containing_infix: usize,
    pub preserve_group_adjacent_to_command_like: usize,
    pub preserve_group_as_argument_of_command: usize,
    pub preserve_group_after_scripted_command_like: usize,
    pub preserve_empty_group: usize,
    pub preserve_group_with_lone_atom_spacing_char: usize,
    pub preserve_group_starting_with_atom_spacing_char: usize,
    pub preserve_group_containing_delimited_pair: usize,
}

pub fn run(ast: &mut Ast, config: &FlattenGroupsConfig, report: &mut FlattenGroupsReport) {
    if !config.enabled {
        return;
    }

    visit(ast, ast.root(), false, config, report);
}

#[derive(Clone, Copy, Debug, Default)]
struct SubtreeFlags {
    has_declarative: bool,
    has_infix: bool,
    has_delimited: bool,
}

fn visit(
    ast: &mut Ast,
    node: NodeId,
    in_env_body: bool,
    config: &FlattenGroupsConfig,
    report: &mut FlattenGroupsReport,
) -> SubtreeFlags {
    let edges = ast.edges(node);
    let mut flags = SubtreeFlags {
        has_declarative: matches!(ast.node(node), Node::Declarative { .. }),
        has_infix: matches!(ast.node(node), Node::Infix { .. }),
        has_delimited: matches!(
            ast.node(node),
            Node::Group {
                kind: GroupKind::Delimited { .. },
                ..
            }
        ),
    };
    for (child, slot) in edges {
        if ast.contains(child) {
            let child_flags = visit(
                ast,
                child,
                in_env_body || slot == Slot::EnvBody,
                config,
                report,
            );
            flags.has_declarative |= child_flags.has_declarative;
            flags.has_infix |= child_flags.has_infix;
            flags.has_delimited |= child_flags.has_delimited;
        }
    }

    if ast.contains(node) {
        try_unwrap(ast, node, flags, in_env_body, config, report);
    }

    flags
}

fn try_unwrap(
    ast: &mut Ast,
    node: NodeId,
    flags: SubtreeFlags,
    in_env_body: bool,
    config: &FlattenGroupsConfig,
    report: &mut FlattenGroupsReport,
) {
    let (kind, mode, child_count) = match ast.node(node) {
        Node::Group {
            kind,
            mode,
            children,
        } => (kind.clone(), *mode, children.len()),
        _ => return,
    };
    if !matches!(kind, GroupKind::Explicit | GroupKind::Implicit) {
        return;
    }
    if config.preserve_group_containing_declarative_command && flags.has_declarative {
        report.guards.preserve_group_containing_declarative_command += 1;
        return;
    }
    if config.preserve_group_inside_env_body && in_env_body {
        report.guards.preserve_group_inside_env_body += 1;
        return;
    }

    let Some(link) = ast.parent(node) else {
        return;
    };
    if !slot_can_unwrap(link.slot, child_count) {
        return;
    }
    if matches!(link.slot, Slot::GroupChild(_))
        && config.preserve_group_containing_infix
        && flags.has_infix
    {
        report.guards.preserve_group_containing_infix += 1;
        return;
    }
    if matches!(link.slot, Slot::GroupChild(_))
        && config.preserve_group_containing_delimited_pair
        && flags.has_delimited
    {
        report.guards.preserve_group_containing_delimited_pair += 1;
        return;
    }
    if let Slot::GroupChild(index) = link.slot
        && config.preserve_group_adjacent_to_command_like
    {
        let command_contact = group_child_touches_command(
            ast,
            node,
            link.parent,
            index,
            config.preserve_group_after_scripted_command_like,
        );
        if command_contact.touches_command {
            report.guards.preserve_group_adjacent_to_command_like += 1;
            if command_contact.used_scripted_base {
                report.guards.preserve_group_after_scripted_command_like += 1;
            }
            return;
        }
    }
    let children = ast.children(node);
    let first_is_atom = children
        .first()
        .is_some_and(|child| is_atom_spacing_char(ast, *child));
    if matches!(link.slot, Slot::GroupChild(_)) {
        if config.preserve_empty_group && child_count == 0 {
            report.guards.preserve_empty_group += 1;
            return;
        }
        if config.preserve_group_with_lone_atom_spacing_char && child_count == 1 && first_is_atom {
            report.guards.preserve_group_with_lone_atom_spacing_char += 1;
            return;
        }
        if config.preserve_group_starting_with_atom_spacing_char && child_count > 1 && first_is_atom
        {
            report.guards.preserve_group_starting_with_atom_spacing_char += 1;
            return;
        }
    }
    if matches!(link.slot, Slot::ScriptBase)
        && config.preserve_group_with_lone_atom_spacing_char
        && child_count == 1
        && first_is_atom
    {
        report.guards.preserve_group_with_lone_atom_spacing_char += 1;
        return;
    }
    if matches!(link.slot, Slot::Argument(_))
        && config.preserve_group_as_argument_of_command
        && group_as_argument_of_command_needs_boundary(ast, node)
    {
        report.guards.preserve_group_as_argument_of_command += 1;
        return;
    }

    let Some(parent_mode) = context_mode(ast, link) else {
        return;
    };
    if mode != parent_mode {
        return;
    }

    if matches!(link.slot, Slot::ScriptBase)
        && config.preserve_group_in_script_base_slot
        && !is_atomic_base(ast, ast.children(node)[0])
    {
        report.guards.preserve_group_in_script_base_slot += 1;
        return;
    }

    match link.slot {
        Slot::GroupChild(index) => unwrap_group_child(ast, node, link.parent, index, report),
        Slot::Argument(_)
        | Slot::ScriptBase
        | Slot::ScriptSub
        | Slot::ScriptSup
        | Slot::InfixLeft
        | Slot::InfixRight => redirect_single_child_slot(ast, node, report),
        Slot::EnvBody => {}
    }
}

fn slot_can_unwrap(slot: Slot, child_count: usize) -> bool {
    match slot {
        Slot::GroupChild(_) => true,
        Slot::Argument(_)
        | Slot::ScriptBase
        | Slot::ScriptSub
        | Slot::ScriptSup
        | Slot::InfixLeft
        | Slot::InfixRight => child_count == 1,
        Slot::EnvBody => false,
    }
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
struct CommandContact {
    touches_command: bool,
    used_scripted_base: bool,
}

fn group_child_touches_command(
    ast: &Ast,
    node: NodeId,
    parent: NodeId,
    index: usize,
    include_scripted: bool,
) -> CommandContact {
    let previous = index
        .checked_sub(1)
        .and_then(|previous| ast.children(parent).get(previous).copied());
    let first_child = ast.children(node).first().copied();

    command_contact_for_node(ast, previous, include_scripted).merge(command_contact_for_node(
        ast,
        first_child,
        include_scripted,
    ))
}

impl CommandContact {
    fn merge(self, other: Self) -> Self {
        Self {
            touches_command: self.touches_command || other.touches_command,
            used_scripted_base: self.used_scripted_base || other.used_scripted_base,
        }
    }
}

fn command_contact_for_node(
    ast: &Ast,
    node: Option<NodeId>,
    include_scripted: bool,
) -> CommandContact {
    let Some(node) = node else {
        return CommandContact::default();
    };
    if is_command_like(ast, node, false) {
        return CommandContact {
            touches_command: true,
            used_scripted_base: false,
        };
    }
    if include_scripted && is_command_like(ast, node, true) {
        return CommandContact {
            touches_command: true,
            used_scripted_base: true,
        };
    }
    CommandContact::default()
}

fn is_atom_spacing_char(ast: &Ast, node: NodeId) -> bool {
    matches!(
        ast.node(node),
        Node::Char(
            '=' | '<' | '>' | '+' | '-' | ',' | ':' | ';' | '.' | '/' | '*' | '!' | '?' | '|' | '·'
        )
    )
}

fn is_command_like(ast: &Ast, node: NodeId, include_scripted: bool) -> bool {
    match ast.node(node) {
        Node::Command { .. } | Node::Declarative { .. } => true,
        Node::Scripted { base, .. } if include_scripted => is_command_like(ast, *base, true),
        _ => false,
    }
}

fn is_atomic_base(ast: &Ast, node: NodeId) -> bool {
    match ast.node(node) {
        Node::Char(_) | Node::Prime { .. } => true,
        Node::Command { name, args, .. } => {
            args.iter().all(Option::is_none)
                && !subtree_has_scripted(ast, node)
                && !is_script_placement_sensitive_command(name)
        }
        _ => false,
    }
}

fn group_as_argument_of_command_needs_boundary(ast: &Ast, node: NodeId) -> bool {
    let children = ast.children(node);
    if children.len() != 1 {
        return false;
    }
    subtree_has_command_like(ast, children[0])
}

fn subtree_has_command_like(ast: &Ast, node: NodeId) -> bool {
    if is_command_like(ast, node, false) {
        return true;
    }
    ast.edges(node)
        .into_iter()
        .any(|(child, _)| subtree_has_command_like(ast, child))
}

fn is_script_placement_sensitive_command(name: &str) -> bool {
    matches!(
        name,
        "arccos"
            | "arcsin"
            | "arctan"
            | "arg"
            | "bigcap"
            | "bigcup"
            | "bigodot"
            | "bigoplus"
            | "bigotimes"
            | "bigsqcup"
            | "bigtriangledown"
            | "bigtriangleup"
            | "biguplus"
            | "bigvee"
            | "bigwedge"
            | "cos"
            | "cosh"
            | "cot"
            | "coth"
            | "csc"
            | "deg"
            | "det"
            | "dim"
            | "exp"
            | "gcd"
            | "hom"
            | "inf"
            | "int"
            | "ker"
            | "lg"
            | "lim"
            | "liminf"
            | "limsup"
            | "ln"
            | "log"
            | "max"
            | "min"
            | "operatorname"
            | "Pr"
            | "prod"
            | "sec"
            | "sin"
            | "sinh"
            | "sup"
            | "sum"
            | "tan"
            | "tanh"
    )
}

fn subtree_has_scripted(ast: &Ast, node: NodeId) -> bool {
    if matches!(ast.node(node), Node::Scripted { .. }) {
        return true;
    }
    ast.edges(node)
        .into_iter()
        .any(|(child, _)| subtree_has_scripted(ast, child))
}

fn context_mode(ast: &Ast, link: ParentLink) -> Option<ContentMode> {
    match link.slot {
        Slot::GroupChild(_) => match ast.node(link.parent) {
            Node::Root { mode, .. } | Node::Group { mode, .. } => Some(*mode),
            _ => None,
        },
        Slot::Argument(index) => argument_slot_mode(ast, link.parent, index),
        Slot::ScriptBase
        | Slot::ScriptSub
        | Slot::ScriptSup
        | Slot::InfixLeft
        | Slot::InfixRight => Some(ContentMode::Math),
        Slot::EnvBody => None,
    }
}

fn argument_slot_mode(ast: &Ast, parent: NodeId, index: usize) -> Option<ContentMode> {
    let argument = ast.arg_slots(parent).get(index)?.as_ref()?;
    match argument.value {
        ArgumentValue::MathContent(_) => Some(ContentMode::Math),
        ArgumentValue::TextContent(_) => Some(ContentMode::Text),
        _ => None,
    }
}

fn unwrap_group_child(
    ast: &mut Ast,
    node: NodeId,
    parent: NodeId,
    index: usize,
    report: &mut FlattenGroupsReport,
) {
    let child_count = ast.children(node).len();
    let children = ast.detach_children_range(node, 0..child_count);
    let mut parent_children = ast.children(parent).to_vec();
    assert_eq!(
        parent_children.get(index),
        Some(&node),
        "group child index must match parent link"
    );

    parent_children.splice(index..index + 1, children);
    ast.replace_children(parent, parent_children);
    ast.remove_detached(node);

    match child_count {
        0 => report.actions.removed_empty += 1,
        1 => report.actions.replaced_single_child += 1,
        _ => report.actions.inlined_multi_child += 1,
    }
}

fn redirect_single_child_slot(ast: &mut Ast, node: NodeId, report: &mut FlattenGroupsReport) {
    let mut children = ast.detach_children_range(node, 0..1);
    let child = children
        .pop()
        .expect("single-child slot unwrap requires one child");
    ast.replace_content_child(node, child);
    ast.remove_detached(node);
    report.actions.unwrapped_slot += 1;
}