pretty_printing/
print.rs

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
use crate::notation::{NRef, N};

pub fn pretty_print(n_ref: NRef, max_width: u32) -> String {
    let mut printer = PrettyPrinter::new(n_ref, max_width);
    printer.print()
}

struct PrettyPrinter<'a> {
    max_width: u32,
    col: u32,
    chunks: Vec<Chunk<'a>>,
}

#[derive(Debug, Clone, Copy)]
struct Chunk<'a> {
    n_ref: NRef<'a>,
    indent: u32,
    flat: bool,
}

impl<'a> Chunk<'a> {
    fn with_n(self, n_ref: NRef<'a>) -> Self {
        Chunk {
            n_ref,
            indent: self.indent,
            flat: self.flat,
        }
    }

    fn indented(self, indent: u32, n_ref: NRef<'a>) -> Self {
        Chunk {
            n_ref,
            indent: self.indent + indent,
            flat: self.flat,
        }
    }

    fn flat(self, n_ref: NRef<'a>) -> Self {
        Chunk {
            n_ref,
            indent: self.indent,
            flat: true,
        }
    }
}

impl<'a> PrettyPrinter<'a> {
    fn new(n_ref: NRef<'a>, width: u32) -> Self {
        let chunk = Chunk {
            n_ref,
            indent: 0,
            flat: false,
        };

        Self {
            max_width: width,
            col: 0,
            chunks: vec![chunk],
        }
    }

    fn print(&mut self) -> String {
        let mut result = String::new();

        while let Some(chunk) = self.chunks.pop() {
            match chunk.n_ref {
                N::Newline => {
                    result.push('\n');
                    for _ in 0..chunk.indent {
                        result.push(' ');
                    }
                    self.col = chunk.indent;
                }
                N::Text(text, width) => {
                    result.push_str(text);
                    self.col += width;
                }
                N::Flat(x) => self.chunks.push(chunk.flat(x)),
                N::Indent(i, x) => self.chunks.push(chunk.indented(*i, x)),
                N::Concat(seq) => {
                    for n in seq.iter().rev() {
                        self.chunks.push(chunk.with_n(n));
                    }
                }
                N::Choice(x, y) => {
                    if chunk.flat || self.fits(chunk.with_n(x)) {
                        self.chunks.push(chunk.with_n(x));
                    } else {
                        self.chunks.push(chunk.with_n(y));
                    }
                }
            }
        }
        result
    }

    fn fits(&self, chunk: Chunk<'a>) -> bool {
        let mut remaining_width = self.max_width.saturating_sub(self.col);
        let mut stack = vec![chunk];
        let mut chunks = &self.chunks as &[Chunk];

        loop {
            let chunk = if let Some(chunk) = stack.pop() {
                chunk
            } else if let Some((chunk, more_chunks)) = chunks.split_last() {
                chunks = more_chunks;
                *chunk
            } else {
                return true;
            };

            match chunk.n_ref {
                N::Newline => return true,
                N::Text(_, text_width) => {
                    if *text_width <= remaining_width {
                        remaining_width -= text_width;
                    } else {
                        return false;
                    }
                }
                N::Flat(x) => stack.push(chunk.flat(x)),
                N::Indent(i, x) => stack.push(chunk.indented(*i, x)),
                N::Concat(seq) => {
                    for n in seq.iter().rev() {
                        stack.push(chunk.with_n(n));
                    }
                }
                N::Choice(x, y) => {
                    if chunk.flat {
                        stack.push(chunk.with_n(x));
                    } else {
                        // With assumption: for every choice `x | y`,
                        // the first line of `y` is no longer than the first line of `x`.
                        stack.push(chunk.with_n(y));
                    }
                }
            }
        }
    }
}