Boolean operators parsing
This commit is contained in:
parent
b96cd2d781
commit
83b5431994
2 changed files with 189 additions and 60 deletions
|
@ -1,7 +1,7 @@
|
|||
use chumsky::{
|
||||
error::Simple,
|
||||
prelude::{choice, filter, just, none_of},
|
||||
text::{int, keyword, TextParser},
|
||||
prelude::{choice, end, filter, just, none_of, recursive},
|
||||
text::{int, keyword, whitespace, TextParser},
|
||||
Parser,
|
||||
};
|
||||
|
||||
|
@ -49,16 +49,22 @@ pub enum Literal {
|
|||
Number(i32),
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum BoolOp {
|
||||
And,
|
||||
Or,
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
pub enum Expr {
|
||||
Fuzzy(Literal),
|
||||
TextCmp(TextField, TextOp, String),
|
||||
NumberCmp(NumberField, NumberOp, i32),
|
||||
And(Box<Expr>, Box<Expr>),
|
||||
Or(Box<Expr>, Box<Expr>),
|
||||
Combined(Box<Expr>, BoolOp, Box<Expr>),
|
||||
}
|
||||
|
||||
pub fn make_parser() -> impl Parser<char, Expr, Error = Simple<char>> {
|
||||
let combined = recursive(|expr| {
|
||||
let quoted_str = just('"')
|
||||
.ignore_then(none_of('"').repeated().collect::<String>())
|
||||
.then_ignore(just('"'));
|
||||
|
@ -70,7 +76,7 @@ pub fn make_parser() -> impl Parser<char, Expr, Error = Simple<char>> {
|
|||
|
||||
let str_ = choice((quoted_str, raw_str)).padded();
|
||||
|
||||
let number = int(10).map(|n: String| n.parse::<i32>().unwrap()).padded();
|
||||
let number = int(10).from_str().unwrapped().padded();
|
||||
|
||||
let text_field = choice((
|
||||
keyword("album").to(TextField::Album),
|
||||
|
@ -123,7 +129,24 @@ pub fn make_parser() -> impl Parser<char, Expr, Error = Simple<char>> {
|
|||
let literal = number.map(Literal::Number).or(str_.map(Literal::Text));
|
||||
let fuzzy = literal.map(Expr::Fuzzy);
|
||||
|
||||
text_cmp.or(number_cmp).or(fuzzy)
|
||||
let filter = text_cmp.or(number_cmp).or(fuzzy);
|
||||
let atom = filter;
|
||||
|
||||
let bool_op = choice((just("&&").to(BoolOp::And), just("||").to(BoolOp::Or))).padded();
|
||||
|
||||
let combined = atom
|
||||
.clone()
|
||||
.then(bool_op.then(atom).repeated())
|
||||
.foldl(|a, (b, c)| Expr::Combined(Box::new(a), b, Box::new(c)));
|
||||
|
||||
combined
|
||||
});
|
||||
|
||||
combined
|
||||
.clone()
|
||||
.then(whitespace().ignore_then(combined).repeated())
|
||||
.foldl(|a: Expr, b: Expr| Expr::Combined(Box::new(a), BoolOp::And, Box::new(b)))
|
||||
.then_ignore(end())
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -250,3 +273,101 @@ fn can_parse_number_operators() {
|
|||
Expr::NumberCmp(NumberField::DiscNumber, NumberOp::LessOrEq, 6),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn can_use_boolean_operators() {
|
||||
let parser = make_parser();
|
||||
|
||||
assert_eq!(
|
||||
parser.parse(r#"album % lands && title % "sword""#).unwrap(),
|
||||
Expr::Combined(
|
||||
Box::new(Expr::TextCmp(
|
||||
TextField::Album,
|
||||
TextOp::Like,
|
||||
"lands".to_owned()
|
||||
)),
|
||||
BoolOp::And,
|
||||
Box::new(Expr::TextCmp(
|
||||
TextField::Title,
|
||||
TextOp::Like,
|
||||
"sword".to_owned()
|
||||
))
|
||||
),
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
parser.parse(r#"album % lands || title % "sword""#).unwrap(),
|
||||
Expr::Combined(
|
||||
Box::new(Expr::TextCmp(
|
||||
TextField::Album,
|
||||
TextOp::Like,
|
||||
"lands".to_owned()
|
||||
)),
|
||||
BoolOp::Or,
|
||||
Box::new(Expr::TextCmp(
|
||||
TextField::Title,
|
||||
TextOp::Like,
|
||||
"sword".to_owned()
|
||||
))
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn boolean_operators_share_precedence() {
|
||||
let parser = make_parser();
|
||||
|
||||
assert_eq!(
|
||||
parser
|
||||
.parse(r#"album % lands || album % tales && title % "sword""#)
|
||||
.unwrap(),
|
||||
Expr::Combined(
|
||||
Box::new(Expr::Combined(
|
||||
Box::new(Expr::TextCmp(
|
||||
TextField::Album,
|
||||
TextOp::Like,
|
||||
"lands".to_owned()
|
||||
)),
|
||||
BoolOp::Or,
|
||||
Box::new(Expr::TextCmp(
|
||||
TextField::Album,
|
||||
TextOp::Like,
|
||||
"tales".to_owned()
|
||||
))
|
||||
)),
|
||||
BoolOp::And,
|
||||
Box::new(Expr::TextCmp(
|
||||
TextField::Title,
|
||||
TextOp::Like,
|
||||
"sword".to_owned()
|
||||
))
|
||||
),
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
parser
|
||||
.parse(r#"album % lands && album % tales || title % "sword""#)
|
||||
.unwrap(),
|
||||
Expr::Combined(
|
||||
Box::new(Expr::Combined(
|
||||
Box::new(Expr::TextCmp(
|
||||
TextField::Album,
|
||||
TextOp::Like,
|
||||
"lands".to_owned()
|
||||
)),
|
||||
BoolOp::And,
|
||||
Box::new(Expr::TextCmp(
|
||||
TextField::Album,
|
||||
TextOp::Like,
|
||||
"tales".to_owned()
|
||||
))
|
||||
)),
|
||||
BoolOp::Or,
|
||||
Box::new(Expr::TextCmp(
|
||||
TextField::Title,
|
||||
TextOp::Like,
|
||||
"sword".to_owned()
|
||||
))
|
||||
),
|
||||
);
|
||||
}
|
||||
|
|
|
@ -5,6 +5,8 @@ use crate::app::index::{
|
|||
storage::SongKey,
|
||||
};
|
||||
|
||||
use super::query::BoolOp;
|
||||
|
||||
struct SearchIndex {}
|
||||
|
||||
impl SearchIndex {
|
||||
|
@ -13,12 +15,18 @@ impl SearchIndex {
|
|||
Expr::Fuzzy(s) => self.eval_fuzzy(s),
|
||||
Expr::TextCmp(field, op, s) => self.eval_text_operator(*field, *op, &s),
|
||||
Expr::NumberCmp(field, op, n) => self.eval_number_operator(*field, *op, *n),
|
||||
Expr::And(e, f) => self
|
||||
Expr::Combined(e, op, f) => self.combine(e, *op, f),
|
||||
}
|
||||
}
|
||||
|
||||
fn combine(&self, e: &Box<Expr>, op: BoolOp, f: &Box<Expr>) -> HashSet<SongKey> {
|
||||
match op {
|
||||
BoolOp::And => self
|
||||
.eval_expr(e)
|
||||
.intersection(&self.eval_expr(f))
|
||||
.cloned()
|
||||
.collect(),
|
||||
Expr::Or(e, f) => self
|
||||
BoolOp::Or => self
|
||||
.eval_expr(e)
|
||||
.union(&self.eval_expr(f))
|
||||
.cloned()
|
||||
|
|
Loading…
Add table
Reference in a new issue