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
|
//! Filter expression language.
//!
//! This module contains methods to parse a given string into an abstract filter tree, check its
//! type and convert it to a [`Filter`][super::filters::Filter].
// Make it available in the grammar mod.
use super::{filters, FightOutcome, SearchField};
use std::{error, fmt};
use itertools::Itertools;
use lalrpop_util::{lalrpop_mod, lexer::Token, ParseError};
use thiserror::Error;
lalrpop_mod!(#[allow(clippy::all)] pub grammar, "/fexpr/grammar.rs");
#[derive(Debug)]
pub struct FError {
location: usize,
data: String,
kind: FErrorKind,
}
impl fmt::Display for FError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} (at {})", self.kind, self.location)
}
}
impl error::Error for FError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
Some(&self.kind)
}
}
#[derive(Debug, Error)]
pub enum FErrorKind {
#[error("invalid regular expression: {0}")]
InvalidRegex(#[from] regex::Error),
#[error("invalid fight outcome")]
InvalidFightOutcome,
#[error("invalid weekday")]
InvalidWeekday,
#[error("invalid timestamp: {0}")]
InvalidTimestamp(#[from] chrono::format::ParseError),
#[error("invalid boss name")]
InvalidBoss,
}
/// Shortcut to create a new parser and parse the given input.
pub fn parse_logfilter<'a>(
input: &'a str,
) -> Result<Box<dyn filters::log::LogFilter>, ParseError<usize, Token<'a>, FError>> {
grammar::LogFilterParser::new().parse(input)
}
/// Extract the location from the given error.
pub fn location<T>(err: &ParseError<usize, T, FError>) -> usize {
match *err {
ParseError::InvalidToken { location } => location,
ParseError::UnrecognizedEOF { location, .. } => location,
ParseError::UnrecognizedToken {
token: (l, _, _), ..
} => l,
ParseError::ExtraToken { token: (l, _, _) } => l,
ParseError::User { ref error } => error.location,
}
}
/// "Re-quotes" a list of string pieces to a long, whitespace separated string.
///
/// This function is needed because the shell already does some argument parsing, so if the user
/// specifies `-player "godric gobbledygook"` on the command line, we will get `["-player", "godric
/// gobbledygook"]` as the arguments. Howvever, our parser expects a single string, so we re-join
/// the pieces and apply the quotes where necessary.
///
/// Note that this works on a "best guess" method, as we cannot reconstruct the shell's quotes 1:1.
/// This means that some things that work on the command line won't work in the REPL, and vice
/// versa.
///
/// ```
/// assert_eq!(
/// requote(&["-player", "godric gobbledygook"]),
/// r#"-player "godric gobbledygook""#,
/// );
/// ```
pub fn requote<S: AsRef<str>, T: IntoIterator<Item = S>>(items: T) -> String {
const SPECIAL_CHARS: &[char] = &[' ', '.', '^', '$', '+', '+'];
items
.into_iter()
.map(|part| {
let part = part.as_ref();
if part.contains(SPECIAL_CHARS) {
format!(r#""{}""#, part)
} else {
part.into()
}
})
.join(" ")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_requote() {
assert_eq!(
requote(&["-player", "godric gobbledygook"]),
r#"-player "godric gobbledygook""#,
);
assert_eq!(requote(&["-player", "godric"]), r#"-player godric"#,);
assert_eq!(requote(&["-player", "g.dric"]), r#"-player "g.dric""#,);
}
}
|