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
|
use std::fmt::Display;
#[derive(Debug, Clone, PartialEq)]
pub enum Composite {
SelectionFunction {
func: BuiltinSelectionFn,
search_mod: Option<SearchModifier>,
result_transform: Option<ResultTransform>,
},
TextFunction {
func: BuiltinTextFn,
swapped: bool,
},
Hook {
kind: HookKind,
f: Box<Composite>,
g: Box<Composite>,
},
Train2 {
f: Box<Composite>,
g: Box<Composite>,
},
Train3 {
f: Box<Composite>,
g: Box<Composite>,
h: Box<Composite>,
},
Group {
operations: Vec<Composite>,
},
}
impl Display for Composite {
fn fmt(&self, fmtr: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use crate::expression::Composite::*;
fn s<T: Display>(x: &Option<T>) -> String {
if let Some(x) = x {
x.to_string()
} else {
String::new()
}
}
match self {
SelectionFunction {
func,
search_mod,
result_transform,
} => write!(fmtr, "{}{}{func}", s(result_transform), s(search_mod)),
TextFunction { func, swapped } => {
write!(fmtr, "{}{func}", if *swapped { "@" } else { "" })
}
Hook { kind, f, g } => write!(fmtr, "({f}{kind}{g})"),
Train2 { f, g } => write!(fmtr, "({f} {g})"),
Train3 { f, g, h } => write!(fmtr, "({f} {g} {h})"),
Group { operations } => {
write!(fmtr, "{{ ")?;
for (i, op) in operations.iter().enumerate() {
if i > 0 {
write!(fmtr, " , ")?;
}
write!(fmtr, "{}", op)?;
}
write!(fmtr, " }}")
}
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum BuiltinSelectionFn {
Empty,
EndOfBuffer,
Span,
CharOffset(usize),
Line(usize),
Regex(String),
AllMatches(String),
RelativeLine(usize),
BufferMatch(String),
}
impl Display for BuiltinSelectionFn {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use crate::expression::BuiltinSelectionFn::*;
match self {
Empty => write!(f, "e"),
EndOfBuffer => write!(f, "$"),
Span => write!(f, "-"),
CharOffset(pos) => write!(f, "#{}", pos),
Line(pos) => write!(f, "{}", pos),
Regex(pat) => write!(f, "/{}/", pat),
AllMatches(pat) => write!(f, "x/{}/", pat),
RelativeLine(pos) => write!(f, "+{}", pos),
BufferMatch(pat) => write!(f, "B/{}/", pat),
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum BuiltinTextFn {
Dot,
Change,
Insert,
Append,
Delete,
Write,
Pipe,
Literal(String),
}
impl Display for BuiltinTextFn {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use crate::expression::BuiltinTextFn::*;
match self {
Dot => write!(f, "."),
Change => write!(f, "c"),
Insert => write!(f, "i"),
Append => write!(f, "a"),
Delete => write!(f, "d"),
Write => write!(f, "w"),
Pipe => write!(f, "|"),
Literal(s) => write!(f, "\"{}\"", s),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum SearchModifier {
Reverse,
Sequential,
ReverseSequential,
SequentialReverse,
}
impl Display for SearchModifier {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use crate::expression::SearchModifier::*;
match self {
Reverse => write!(f, "'"),
Sequential => write!(f, ";"),
ReverseSequential => write!(f, "';"),
SequentialReverse => write!(f, ";'"),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ResultTransform {
Complement,
Conditional,
ComplementConditional,
ConditionalComplement,
}
impl Display for ResultTransform {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use crate::expression::ResultTransform::*;
match self {
Complement => write!(f, "~"),
Conditional => write!(f, "?"),
ComplementConditional => write!(f, "~?"),
ConditionalComplement => write!(f, "?~"),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum HookKind {
Before,
After,
}
impl Display for HookKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
HookKind::Before => write!(f, ">"),
HookKind::After => write!(f, "<"),
}
}
}
#[cfg(test)]
mod tests {
use crate::expression::*;
#[test]
fn format_3train() {
// nonsense, but it exercises the code
let comp = Composite::Train3 {
f: Box::new(Composite::SelectionFunction {
func: BuiltinSelectionFn::AllMatches(String::from("re")),
result_transform: Some(ResultTransform::Complement),
search_mod: Some(SearchModifier::Reverse),
}),
g: Box::new(Composite::Train2 {
f: Box::new(Composite::TextFunction {
func: BuiltinTextFn::Change,
swapped: false,
}),
g: Box::new(Composite::SelectionFunction {
func: BuiltinSelectionFn::Line(5),
result_transform: None,
search_mod: None,
}),
}),
h: Box::new(Composite::Hook {
kind: HookKind::Before,
f: Box::new(Composite::SelectionFunction {
func: BuiltinSelectionFn::Empty,
result_transform: None,
search_mod: None,
}),
g: Box::new(Composite::TextFunction {
func: BuiltinTextFn::Delete,
swapped: false,
}),
}),
};
let formatted = format!("{}", comp);
println!("Formatted: {}", formatted);
assert_eq!(formatted, "(~'x/re/ (c 5) (e>d))");
}
}
|