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
use crate::doc::*;
use failure::Error;
#[derive(Clone, Debug, Default)]
pub struct DelWriter<S: Schema> {
pub past: Vec<DelElement<S>>,
stack: Vec<Vec<DelElement<S>>>,
}
impl<S: Schema> DelWriter<S> {
pub fn new() -> DelWriter<S> {
DelWriter {
past: vec![],
stack: vec![],
}
}
pub fn begin(&mut self) {
let past = self.past.clone();
self.past = vec![];
self.stack.push(past);
}
pub fn exit(&mut self) {
let past = self.past.clone();
self.past = self
.stack
.pop()
.expect("Cannot exit(), as we aren't in a group");
if past.is_continuous_skip() {
self.past.place(&DelSkip(1));
} else {
self.past.place(&DelWithGroup(past));
}
}
pub fn close(&mut self) {
let past = self.past.clone();
self.past = self
.stack
.pop()
.expect("Cannot close(), as we aren't in a group");
self.past.place(&DelGroup(past));
}
pub fn exit_all(&mut self) {
while !self.stack.is_empty() {
self.exit();
}
}
pub fn place(&mut self, elem: &DelElement<S>) {
self.past.place(elem);
}
pub fn place_all(&mut self, span: &DelSpan<S>) {
self.past.place_all(span);
}
pub fn result(self) -> DelSpan<S> {
if !self.stack.is_empty() {
println!("{:?}", self);
assert!(false, "cannot get result when stack is still full");
}
self.past
}
}
#[derive(Clone, Debug, Default)]
pub struct AddWriter<S: Schema> {
pub past: Vec<AddElement<S>>,
stack: Vec<Vec<AddElement<S>>>,
}
impl<S: Schema> AddWriter<S> {
pub fn new() -> AddWriter<S> {
AddWriter {
past: vec![],
stack: vec![],
}
}
pub fn begin(&mut self) {
let past = self.past.clone();
self.past = vec![];
self.stack.push(past);
}
pub fn exit(&mut self) {
let past = self.past.clone();
self.past = self
.stack
.pop()
.expect("Cannot exit(), as we aren't in a group");
if past.is_continuous_skip() {
self.past.place(&AddSkip(1));
} else {
self.past.place(&AddWithGroup(past));
}
}
pub fn close(&mut self, attrs: S::GroupProperties) {
let past = self.past.clone();
self.past = self
.stack
.pop()
.expect("Cannot close(), as we aren't in a group");
self.past.place(&AddGroup(attrs, past));
}
pub fn exit_all(&mut self) {
while !self.stack.is_empty() {
self.exit();
}
}
pub fn place(&mut self, elem: &AddElement<S>) {
self.past.place(elem);
}
pub fn place_all(&mut self, span: &AddSpan<S>) {
self.past.place_all(span);
}
pub fn result(self) -> AddSpan<S> {
if !self.stack.is_empty() {
println!("{:?}", self);
assert!(false, "cannot get result when stack is still full");
}
self.past
}
}
pub struct OpWriter<S: Schema> {
pub del: DelWriter<S>,
pub add: AddWriter<S>,
}
impl<S: Schema> OpWriter<S> {
pub fn exit_result(mut self) -> Op<S> {
self.del.exit_all();
self.add.exit_all();
self.result()
}
pub fn result(self) -> Op<S> {
Op(self.del.result(), self.add.result())
}
}
#[derive(Clone, Debug, Default)]
pub struct CurWriter {
pub past: Vec<CurElement>,
stack: Vec<Vec<CurElement>>,
}
impl CurWriter {
pub fn new() -> CurWriter {
CurWriter {
past: vec![],
stack: vec![],
}
}
pub fn begin(&mut self) {
let past = self.past.clone();
self.past = vec![];
self.stack.push(past);
}
pub fn exit(&mut self) {
let past = self.past.clone();
self.past = self.stack.pop().unwrap();
self.past.push(CurWithGroup(past));
}
pub fn exit_all(&mut self) {
while !self.stack.is_empty() {
self.exit();
}
}
pub fn place(&mut self, elem: &CurElement) {
self.past.place(elem);
}
pub fn place_all(&mut self, span: &CurSpan) {
self.past.place_all(span);
}
pub fn result(self) -> CurSpan {
if !self.stack.is_empty() {
println!("{:?}", self);
assert!(false, "cannot get result when stack is still full");
}
self.past
}
}
#[derive(Clone, Debug)]
pub struct DocWriter<S: Schema> {
pub(crate) past: Vec<DocElement<S>>,
stack: Vec<Vec<DocElement<S>>>,
}
impl<S: Schema> DocWriter<S> {
pub fn new() -> DocWriter<S> {
DocWriter {
past: vec![],
stack: vec![],
}
}
pub fn begin(&mut self) {
let past = self.past.clone();
self.past = vec![];
self.stack.push(past);
}
pub fn close(&mut self, attrs: S::GroupProperties) {
let past = self.past.clone();
self.past = self.stack.pop().unwrap();
self.past.push(DocGroup(attrs, past));
}
pub fn place(&mut self, elem: &DocElement<S>) {
self.past.place(elem);
}
pub fn place_all(&mut self, span: &DocSpan<S>) {
self.past.place_all(span);
}
pub fn result(self) -> Result<DocSpan<S>, Error> {
if !self.stack.is_empty() {
println!("{:?}", self);
bail!("cannot get result when stack is still full");
}
Ok(self.past)
}
pub(crate) fn unwrap_self(&mut self) {
while !self.past.is_empty() {
self.stack.last_mut().unwrap().push(self.past.remove(0));
}
self.past = self.stack.pop().unwrap();
}
pub(crate) fn wrap_previous(&mut self, count: usize, attrs: S::GroupProperties) {
let start = self.past.len() - count;
let group = self.past.split_off(start);
self.past.push(DocGroup(attrs, group));
}
}