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
#![forbid(unsafe_code)]
#![no_std]
extern crate alloc;
extern crate core;
use alloc::{borrow, fmt, format, string::String, vec, vec::Vec};
#[cfg(test)]
mod tests;
pub mod indention;
use indention::{Indented, Unindent};
#[derive(Clone, Debug)]
#[cfg_attr(any(feature = "extra-traits", test), derive(PartialEq))]
pub struct Block<S> {
pub head: S,
pub subs: Vec<Block<S>>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "extra-traits", derive(PartialOrd, Ord, Hash))]
pub enum EmptyLinesMode {
Skip,
HonorIndent,
PrevIndent,
}
impl<A> Block<A> {
pub fn map_ref<B, F>(&self, mut f: F) -> Block<B>
where
F: FnMut(&A) -> B,
{
fn intern_<A, B, F: FnMut(&A) -> B>(this: &Block<A>, f: &mut F) -> Block<B> {
let head = f(&this.head);
Block {
head,
subs: this.subs.iter().map(move |i| intern_(i, f)).collect(),
}
}
intern_(self, &mut f)
}
pub fn map<B, F>(self, mut f: F) -> Block<B>
where
F: FnMut(A) -> B,
{
fn intern_<A, B, F: FnMut(A) -> B>(this: Block<A>, f: &mut F) -> Block<B> {
let Block { head, subs } = this;
let head = f(head);
Block {
head,
subs: subs.into_iter().map(move |i| intern_(i, f)).collect(),
}
}
intern_(self, &mut f)
}
}
impl<'a, T> Block<&'a T>
where
T: borrow::ToOwned,
{
#[inline(always)]
pub fn to_owned(&self) -> Block<<T as borrow::ToOwned>::Owned> {
self.map_ref(|&i| borrow::ToOwned::to_owned(i))
}
}
impl<A: fmt::Display> Block<A> {
pub fn append_to_string(&self, ret: &mut String, single_indent: &str, upper_indent: &str) {
ret.push_str(upper_indent);
*ret += &format!("{}\n", self.head);
let new_indent = format!("{}{}", upper_indent, single_indent);
for i in &self.subs {
i.append_to_string(ret, single_indent, &new_indent);
}
}
#[inline]
pub fn to_string(&self, single_indent: &str) -> String {
let mut ret = String::new();
self.append_to_string(&mut ret, single_indent, "");
ret
}
}
fn get_indent(s: &str) -> Indented<&str> {
let (indent, data) = s.split_at(
s.chars()
.take_while(|x| x.is_whitespace())
.map(|x| x.len_utf8())
.sum(),
);
Indented { indent, data }
}
pub fn parse_nested_blocks(
s: &str,
empty_lines_mode: EmptyLinesMode,
) -> Vec<Block<Indented<&str>>> {
let mut base = vec![];
let mut stack: Vec<Block<Indented<&str>>> = vec![];
let mut lit = s
.lines()
.map(get_indent)
.filter(|&Indented { data, .. }| {
!(empty_lines_mode == EmptyLinesMode::Skip && data.is_empty())
})
.map(|head| Block {
head,
subs: Vec::new(),
});
loop {
let i = lit.next();
let i_indent = i
.as_ref()
.and_then(|j| match empty_lines_mode {
_ if !j.head.data.is_empty() => Some(j),
EmptyLinesMode::HonorIndent => Some(j),
EmptyLinesMode::PrevIndent => stack.last(),
EmptyLinesMode::Skip => unreachable!(),
})
.map(|x| x.head.indent)
.unwrap_or("");
while {
stack
.last()
.map(|Block { ref head, .. }| {
!i_indent.starts_with(head.indent) || i_indent == head.indent
})
.unwrap_or(false)
} {
let old_top = stack.pop().unwrap();
stack
.last_mut()
.map(|top2| &mut top2.subs)
.unwrap_or(&mut base)
.push(old_top);
}
assert!(i_indent.starts_with(stack.last().map(|top| top.head.indent).unwrap_or("")));
match i {
Some(j) => stack.push(j),
None => break,
}
}
assert!(stack.is_empty());
base
}
pub fn blocks_to_string<B>(blks: Vec<Block<B>>, single_indent: &str) -> String
where
B: Unindent,
<B as Unindent>::Item: fmt::Display,
{
blks.into_iter()
.map(Unindent::unindent)
.fold(String::new(), |mut acc, x| {
x.append_to_string(&mut acc, single_indent, "");
acc
})
}