-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
359 lines (333 loc) · 11.8 KB
/
lib.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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
use itertools::Itertools;
use pest::{iterators::Pair, Parser};
use pest_derive::Parser;
use range::Bounds;
use std::{
collections::BTreeSet,
fs,
os::unix::fs::MetadataExt,
path::{Path, PathBuf},
};
use thiserror::Error;
mod bindings;
mod definition;
mod evdev_mappings;
mod range;
mod token;
pub use crate::bindings::Binding;
pub use crate::definition::{Definition, DefinitionUncompiled};
pub use crate::token::{Key, KeyAttribute, KeyRepr, Modifier, ModifierRepr};
#[derive(Debug, Error)]
pub enum ParseError {
#[error("unable to parse config file")]
// pest::error::Error being 184 bytes makes this entire enum
// expensive to copy, hence the box is used to put it on the heap.
Grammar(#[from] Box<pest::error::Error<Rule>>),
#[error("hotkey config must contain one and only one main section")]
MainSection,
#[error(transparent)]
ConfigRead(#[from] ConfigReadError),
#[error("`{0}` is not recongnized as a valid evdev key")]
InvalidKey(String),
}
#[derive(Parser)]
#[grammar = "template.pest"]
pub struct SwhkdGrammar;
#[derive(Default, Debug)]
pub struct Mode {
pub name: String,
pub oneoff: bool,
pub swallow: bool,
pub bindings: Vec<Binding>,
pub unbinds: Vec<Definition>,
}
#[derive(Debug)]
pub struct SwhkdParser {
pub bindings: Vec<Binding>,
pub unbinds: Vec<Definition>,
pub imports: BTreeSet<String>,
pub modes: Vec<Mode>,
}
/// Input to the grammar parser.
/// Can be either a string or a path.
pub enum ParserInput<'a> {
Raw(&'a str),
Path(&'a Path),
}
#[derive(Debug, Error)]
pub enum ConfigReadError {
#[error("unable to read config file")]
ReadingConfig(#[from] std::io::Error),
#[error("path `{0}` supplied as config is not a regular file")]
NotRegularFile(PathBuf),
#[error("the supplied config file {0} size exceeds the {1}MiB limit")]
TooLarge(PathBuf, u64),
}
pub fn read_config<P: AsRef<Path>>(path: P) -> Result<String, ConfigReadError> {
let path = path.as_ref();
let stat = fs::metadata(path)?;
if !stat.is_file() {
return Err(ConfigReadError::NotRegularFile(path.to_path_buf()));
}
let size = stat.size();
let mib_cap = std::option_env!("FILESIZE_CAP_MIB")
.and_then(|cap| cap.parse().ok())
.unwrap_or(50);
if size > (mib_cap << 20) {
return Err(ConfigReadError::TooLarge(path.to_path_buf(), mib_cap));
}
// TODO: Use mmap instead of fs::read_to_string
Ok(fs::read_to_string(path)?)
}
impl SwhkdParser {
pub fn from(input: ParserInput) -> Result<Self, ParseError> {
let mut imports = BTreeSet::new();
let root = Self::as_import(input, &mut imports)?;
let mut bindings: Vec<Binding> = vec![];
for binding in root.bindings {
if let Some(b) = bindings
.iter_mut()
.find(|b| b.definition == binding.definition)
{
b.command = binding.command;
b.mode_instructions = binding.mode_instructions;
continue;
}
if root
.unbinds
.iter()
.find(|b| binding.definition.eq(b))
.is_some()
{
continue;
}
bindings.push(binding);
}
Ok(SwhkdParser {
bindings,
imports,
unbinds: root.unbinds,
modes: root.modes,
})
}
fn as_import(input: ParserInput, seen: &mut BTreeSet<String>) -> Result<Self, ParseError> {
let (raw, source) = match input {
// If a config is loaded from a string instead of a path, name it `<anonymous>`
ParserInput::Raw(s) => (s.to_string(), "<anonymous>"),
ParserInput::Path(p) => (read_config(p)?, p.to_str().unwrap_or_default()),
};
let parse_result = SwhkdGrammar::parse(Rule::main, &raw)
.map_err(|err| ParseError::Grammar(Box::new(err.with_path(source))))?;
let Some(contents) = parse_result.into_iter().next() else {
return Err(ParseError::MainSection);
};
let mut bindings: Vec<Binding> = vec![];
let mut unbinds = vec![];
let mut imports = BTreeSet::new();
let mut modes = vec![];
for decl in contents.into_inner() {
match decl.as_rule() {
Rule::binding => bindings.extend(binding_parser(decl)?),
Rule::unbind => unbinds.extend(unbind_parser(decl)?),
Rule::mode => modes.push(mode_parser(decl)?),
Rule::import => imports.extend(import_parser(decl)),
// End of identifier
// Here, it means the end of the file.
Rule::EOI => {}
_ => unreachable!(),
}
}
while let Some(import) = imports.pop_first() {
if !seen.insert(import.clone()) {
continue;
}
let child = Self::as_import(ParserInput::Path(Path::new(&import)), seen)?;
bindings.extend(child.bindings);
imports.extend(child.imports);
unbinds.extend(child.unbinds);
modes.extend(child.modes);
}
Ok(SwhkdParser {
bindings,
unbinds,
imports,
modes,
})
}
}
fn pair_to_string(pair: Pair<'_, Rule>) -> String {
pair.as_str().to_string()
}
/// Unescapes a string that has been escaped using backslashes
/// but only for the charset of '{}|\-+~@,' that were allowed to
/// be escaped in the grammar in the first place.
/// ```ignore
/// use sweet::unescape;
/// fn main() {
/// assert_eq!(unescape(r"hello\\\{\}\|\-\+\~\@\,"), r"hello\{}|-+~@,");
/// }
/// ```
fn unescape(s: &str) -> String {
let mut unescaped = String::with_capacity(s.len());
let mut was_a_slash = None;
for char in s.chars() {
match (char, was_a_slash.take()) {
('\\', None) => was_a_slash = Some(()),
('\\', Some(())) => unescaped.push(char),
('{' | '}' | '|' | '-' | '+' | '~' | '@' | ',', Some(())) => unescaped.push(char),
(_, Some(())) => {}
_ => unescaped.push(char),
}
}
unescaped
}
fn unbind_parser(pair: Pair<'_, Rule>) -> Result<Vec<Definition>, ParseError> {
let mut uncompiled = DefinitionUncompiled::default();
for thing in pair.into_inner() {
uncompiled.ingest(thing)?;
}
Ok(uncompiled.compile())
}
fn import_parser(pair: Pair<'_, Rule>) -> Vec<String> {
pair.into_inner()
.filter(|component| matches!(component.as_rule(), Rule::import_file))
.map(pair_to_string)
.collect()
}
fn parse_key(component: Pair<'_, Rule>) -> KeyRepr {
let mut attribute = KeyAttribute::None;
let mut key = String::default();
for inner in component.into_inner() {
match inner.as_rule() {
Rule::send => attribute |= KeyAttribute::Send,
Rule::on_release => attribute |= KeyAttribute::OnRelease,
Rule::shorthand_allow | Rule::key_base => {
key = unescape(&inner.as_str().to_lowercase()).to_string()
}
_ => {}
}
}
KeyRepr { key, attribute }
}
fn parse_command_shorthand(pair: Pair<'_, Rule>) -> Result<Vec<String>, ParseError> {
let mut command_variants = vec![];
for component in pair.into_inner() {
match component.as_rule() {
Rule::command_component => {
command_variants.push(unescape(component.as_str()).to_string())
}
Rule::range => {
let (lower_bound, upper_bound) = Bounds::new(component).expand_commands()?;
command_variants.extend((lower_bound..=upper_bound).map(|key| key.to_string()));
}
_ => {}
}
}
Ok(command_variants)
}
fn mode_parser(pair: Pair<'_, Rule>) -> Result<Mode, ParseError> {
let mut mode = Mode::default();
for component in pair.into_inner() {
match component.as_rule() {
Rule::modename => mode.name = component.as_str().to_string(),
Rule::binding => mode.bindings.extend(binding_parser(component)?),
Rule::unbind => mode.unbinds.extend(unbind_parser(component)?),
Rule::oneoff => mode.oneoff = true,
Rule::swallow => mode.swallow = true,
_ => {}
}
}
Ok(mode)
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ModeInstruction {
Enter(String),
Escape,
}
fn binding_parser(pair: Pair<'_, Rule>) -> Result<Vec<Binding>, ParseError> {
let mut comm = vec![];
let mut mode_enters = vec![];
let mut mode_escapes = vec![];
let mut uncompiled = DefinitionUncompiled::default();
for component in pair.clone().into_inner() {
match component.as_rule() {
Rule::command => {
for subcomponent in component.into_inner() {
match subcomponent.as_rule() {
Rule::command_standalone => {
comm.push(vec![pair_to_string(subcomponent)]);
}
Rule::command_shorthand => {
comm.push(parse_command_shorthand(subcomponent)?);
}
Rule::command_double_ampersand => {
if comm
.last()
.is_some_and(|last| last.len() == 1 && last[0] == "&&")
{
continue;
}
comm.push(vec![pair_to_string(subcomponent)]);
}
Rule::enter_mode => {
// Safety: the first element is guaranteed to be a modename
// by the grammar.
let modename = subcomponent.into_inner().next().unwrap();
mode_enters.push(ModeInstruction::Enter(pair_to_string(modename)));
}
Rule::escape_mode => {
if mode_enters.pop().is_none() {
mode_escapes.push(ModeInstruction::Escape);
}
}
_ => {}
}
}
if comm
.last()
.is_some_and(|last| last.len() == 1 && last[0] == "&&")
{
comm.pop();
}
}
_ => uncompiled.ingest(component)?,
}
}
let bind_cartesian_product = uncompiled.compile();
let command_cartesian_product = comm
.into_iter()
.multi_cartesian_product()
.map(|c| c.join(""))
.collect_vec();
let bind_len = bind_cartesian_product.len();
let command_len = command_cartesian_product.len();
if bind_len != command_len {
let err = pest::error::Error::new_from_span(
pest::error::ErrorVariant::<Rule>::CustomError {
message: format!(
"the number of possible binding variants {0} does not equal the number of possible command variants {1}.",
bind_len, command_len
),
},
pair.as_span(),
);
return Err(Box::new(err).into());
}
let mut bindings: Vec<Binding> = bind_cartesian_product
.into_iter()
.zip(command_cartesian_product)
.map(|(definition, command)| Binding {
definition,
command,
mode_instructions: mode_enters
.iter()
.chain(mode_escapes.iter())
.cloned()
.collect(),
})
.collect();
for binding in bindings.iter_mut() {
binding.definition.modifiers.remove(&Modifier::Omission);
}
Ok(bindings)
}