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
use core::fmt::{self, Debug, Formatter};

use either::Either;

use crate::utils::DebugFn;

use super::Rule;

pub struct PrintContext<'src> {
    src: &'src str,
    debug: bool,
}

enum IterSpecialCase<I: Iterator> {
    Zero,
    One(I::Item),
    Many(I),
}

fn iter_special_case<T>(
    iter: impl IntoIterator<Item = T>,
) -> IterSpecialCase<impl Iterator<Item = T>> {
    let mut iter = iter.into_iter();
    match iter.size_hint() {
        (_, Some(0)) => IterSpecialCase::Zero,
        (_, Some(1)) => match iter.next() {
            Some(item) => IterSpecialCase::One(item),
            None => IterSpecialCase::Zero,
        },
        (2, _) => IterSpecialCase::Many(Either::Left(iter)),
        _ => {
            let Some(first) = iter.next() else {
                return IterSpecialCase::Zero;
            };

            let Some(second) = iter.next() else {
                return IterSpecialCase::One(first);
            };

            IterSpecialCase::Many(Either::Right([first, second].into_iter().chain(iter)))
        }
    }
}

impl<'src> PrintContext<'src> {
    pub fn src(&self) -> &'src str {
        self.src
    }

    pub fn new(src: &'src str) -> Self {
        Self { src, debug: false }
    }

    pub fn debuggable<'lt, R: Rule + ?Sized>(&'lt self, ast: &'lt R) -> impl Debug + 'lt {
        DebugFn(move |f| ast.print_tree(self, f))
    }

    pub fn filter_ignored<'short, 'item, R: Rule + ?Sized>(
        &'short self,
        items: impl IntoIterator<Item = &'item R> + 'short,
    ) -> impl Iterator<Item = &'item R> + 'short {
        items
            .into_iter()
            .filter(|item| match item.print_visibility(self) {
                PrintVisibility::Never => false,
                PrintVisibility::DebugOnly => self.debug,
                PrintVisibility::Always => true,
            })
    }

    pub fn fold_printable<'item, T>(
        &self,
        items: impl IntoIterator<Item = &'item dyn Rule>,
        init: T,
        mut f: impl FnMut(T, &dyn Debug) -> T,
    ) -> T {
        items
            .into_iter()
            .fold(init, move |acc, ast| f(acc, &self.debuggable(ast)))
    }

    pub fn debug_tuple<'item>(
        &self,
        name: &str,
        f: &mut Formatter,
        items: impl IntoIterator<Item = &'item dyn Rule>,
    ) -> fmt::Result {
        match iter_special_case(self.filter_ignored(items)) {
            IterSpecialCase::Zero => f.write_str("()"),
            IterSpecialCase::One(item) => item.print_tree(self, f),
            IterSpecialCase::Many(items) => self
                .fold_printable(items, &mut f.debug_tuple(name), |d, item| d.field(item))
                .finish(),
        }
    }

    pub fn debug_list<'item>(
        &self,
        f: &mut Formatter,
        items: impl IntoIterator<Item = &'item dyn Rule>,
    ) -> fmt::Result {
        match iter_special_case(self.filter_ignored(items)) {
            IterSpecialCase::Zero => f.write_str("[]"),
            IterSpecialCase::One(item) => {
                f.write_str("[")?;
                item.print_tree(self, f)?;
                f.write_str("]")
            }
            IterSpecialCase::Many(items) => self
                .fold_printable(items, &mut f.debug_list(), |d, item| d.entry(item))
                .finish(),
        }
    }

    pub fn debug_rule<'item>(
        &self,
        f: &mut Formatter,
        items: impl IntoIterator<Item = &'item dyn Rule>,
    ) -> fmt::Result {
        match iter_special_case(self.filter_ignored(items)) {
            IterSpecialCase::Zero => f.write_str("{}"),
            IterSpecialCase::One(item) => item.print_tree(self, f),
            IterSpecialCase::Many(items) => self
                .fold_printable(items, &mut f.debug_set(), |d, item| d.entry(item))
                .finish(),
        }
    }

    pub fn is_debug(&self) -> bool {
        self.debug
    }

    pub fn set_debug(&mut self, debug: bool) -> &mut Self {
        self.debug = debug;
        self
    }
}

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum PrintVisibility {
    Never,
    DebugOnly,
    #[default]
    Always,
}

impl PrintVisibility {
    pub fn should_print(self, cx: &PrintContext) -> bool {
        match self {
            PrintVisibility::Never => false,
            PrintVisibility::DebugOnly => cx.is_debug(),
            PrintVisibility::Always => true,
        }
    }
}