2021-10-22 07:59:38 +08:00
|
|
|
//! BNF grammar:
|
|
|
|
//!
|
|
|
|
//! ```text
|
|
|
|
//! condition = value ("==" | ">" ...) value
|
|
|
|
//! to = value value TO value
|
|
|
|
//! ```
|
|
|
|
|
|
|
|
use nom::branch::alt;
|
|
|
|
use nom::bytes::complete::tag;
|
2022-06-15 15:14:19 +08:00
|
|
|
use nom::character::complete::multispace1;
|
2021-11-03 00:35:17 +08:00
|
|
|
use nom::combinator::cut;
|
2022-05-25 17:55:16 +08:00
|
|
|
use nom::sequence::{terminated, tuple};
|
2021-10-22 07:59:38 +08:00
|
|
|
use Condition::*;
|
|
|
|
|
2022-06-15 19:31:51 +08:00
|
|
|
use crate::{parse_value, FilterCondition, IResult, Span, Token};
|
2021-10-22 07:59:38 +08:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub enum Condition<'a> {
|
|
|
|
GreaterThan(Token<'a>),
|
|
|
|
GreaterThanOrEqual(Token<'a>),
|
|
|
|
Equal(Token<'a>),
|
|
|
|
NotEqual(Token<'a>),
|
2023-03-08 23:57:42 +08:00
|
|
|
Null,
|
2023-03-15 01:08:12 +08:00
|
|
|
Empty,
|
2022-06-14 22:42:09 +08:00
|
|
|
Exists,
|
2021-10-22 07:59:38 +08:00
|
|
|
LowerThan(Token<'a>),
|
|
|
|
LowerThanOrEqual(Token<'a>),
|
|
|
|
Between { from: Token<'a>, to: Token<'a> },
|
2024-07-17 22:54:33 +08:00
|
|
|
Contains { keyword: Token<'a>, word: Token<'a> },
|
2021-10-22 07:59:38 +08:00
|
|
|
}
|
|
|
|
|
2022-06-14 21:28:34 +08:00
|
|
|
/// condition = value ("==" | ">" ...) value
|
2021-11-03 03:27:07 +08:00
|
|
|
pub fn parse_condition(input: Span) -> IResult<FilterCondition> {
|
2021-10-22 07:59:38 +08:00
|
|
|
let operator = alt((tag("<="), tag(">="), tag("!="), tag("<"), tag(">"), tag("=")));
|
2021-11-09 07:45:46 +08:00
|
|
|
let (input, (fid, op, value)) = tuple((parse_value, operator, cut(parse_value)))(input)?;
|
2021-10-22 07:59:38 +08:00
|
|
|
|
2021-11-09 07:49:13 +08:00
|
|
|
let condition = match *op.fragment() {
|
|
|
|
"<=" => FilterCondition::Condition { fid, op: LowerThanOrEqual(value) },
|
|
|
|
">=" => FilterCondition::Condition { fid, op: GreaterThanOrEqual(value) },
|
2021-11-10 00:05:36 +08:00
|
|
|
"!=" => FilterCondition::Condition { fid, op: NotEqual(value) },
|
|
|
|
"<" => FilterCondition::Condition { fid, op: LowerThan(value) },
|
|
|
|
">" => FilterCondition::Condition { fid, op: GreaterThan(value) },
|
|
|
|
"=" => FilterCondition::Condition { fid, op: Equal(value) },
|
2021-10-22 07:59:38 +08:00
|
|
|
_ => unreachable!(),
|
2021-11-09 07:49:13 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok((input, condition))
|
2021-10-22 07:59:38 +08:00
|
|
|
}
|
|
|
|
|
2023-03-14 17:31:04 +08:00
|
|
|
/// null = value "IS" WS+ "NULL"
|
|
|
|
pub fn parse_is_null(input: Span) -> IResult<FilterCondition> {
|
|
|
|
let (input, key) = parse_value(input)?;
|
2023-03-08 23:57:42 +08:00
|
|
|
|
2023-03-14 17:31:04 +08:00
|
|
|
let (input, _) = tuple((tag("IS"), multispace1, tag("NULL")))(input)?;
|
2023-03-08 23:57:42 +08:00
|
|
|
Ok((input, FilterCondition::Condition { fid: key, op: Null }))
|
|
|
|
}
|
|
|
|
|
2023-03-14 17:31:04 +08:00
|
|
|
/// null = value "IS" WS+ "NOT" WS+ "NULL"
|
|
|
|
pub fn parse_is_not_null(input: Span) -> IResult<FilterCondition> {
|
2023-03-08 23:57:42 +08:00
|
|
|
let (input, key) = parse_value(input)?;
|
|
|
|
|
2023-03-14 17:31:04 +08:00
|
|
|
let (input, _) = tuple((tag("IS"), multispace1, tag("NOT"), multispace1, tag("NULL")))(input)?;
|
2023-03-08 23:57:42 +08:00
|
|
|
Ok((input, FilterCondition::Not(Box::new(FilterCondition::Condition { fid: key, op: Null }))))
|
|
|
|
}
|
|
|
|
|
2023-03-15 01:08:12 +08:00
|
|
|
/// empty = value "IS" WS+ "EMPTY"
|
|
|
|
pub fn parse_is_empty(input: Span) -> IResult<FilterCondition> {
|
|
|
|
let (input, key) = parse_value(input)?;
|
|
|
|
|
|
|
|
let (input, _) = tuple((tag("IS"), multispace1, tag("EMPTY")))(input)?;
|
|
|
|
Ok((input, FilterCondition::Condition { fid: key, op: Empty }))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// empty = value "IS" WS+ "NOT" WS+ "EMPTY"
|
|
|
|
pub fn parse_is_not_empty(input: Span) -> IResult<FilterCondition> {
|
|
|
|
let (input, key) = parse_value(input)?;
|
|
|
|
|
|
|
|
let (input, _) = tuple((tag("IS"), multispace1, tag("NOT"), multispace1, tag("EMPTY")))(input)?;
|
|
|
|
Ok((input, FilterCondition::Not(Box::new(FilterCondition::Condition { fid: key, op: Empty }))))
|
|
|
|
}
|
|
|
|
|
2022-06-15 15:14:19 +08:00
|
|
|
/// exist = value "EXISTS"
|
2022-06-14 22:42:09 +08:00
|
|
|
pub fn parse_exists(input: Span) -> IResult<FilterCondition> {
|
|
|
|
let (input, key) = terminated(parse_value, tag("EXISTS"))(input)?;
|
2022-05-25 17:55:16 +08:00
|
|
|
|
2022-10-14 22:44:10 +08:00
|
|
|
Ok((input, FilterCondition::Condition { fid: key, op: Exists }))
|
2022-06-14 22:42:09 +08:00
|
|
|
}
|
2022-06-16 15:12:37 +08:00
|
|
|
/// exist = value "NOT" WS+ "EXISTS"
|
2022-06-14 22:42:09 +08:00
|
|
|
pub fn parse_not_exists(input: Span) -> IResult<FilterCondition> {
|
2022-06-15 15:14:19 +08:00
|
|
|
let (input, key) = parse_value(input)?;
|
2022-06-14 22:42:09 +08:00
|
|
|
|
2022-06-15 15:14:19 +08:00
|
|
|
let (input, _) = tuple((tag("NOT"), multispace1, tag("EXISTS")))(input)?;
|
2022-10-14 22:44:10 +08:00
|
|
|
Ok((input, FilterCondition::Not(Box::new(FilterCondition::Condition { fid: key, op: Exists }))))
|
2022-05-25 17:55:16 +08:00
|
|
|
}
|
|
|
|
|
2024-07-17 17:13:37 +08:00
|
|
|
/// contains = value "CONTAINS" value
|
|
|
|
pub fn parse_contains(input: Span) -> IResult<FilterCondition> {
|
2024-07-17 22:54:33 +08:00
|
|
|
let (input, (fid, contains, value)) =
|
|
|
|
tuple((parse_value, tag("CONTAINS"), cut(parse_value)))(input)?;
|
|
|
|
Ok((
|
|
|
|
input,
|
|
|
|
FilterCondition::Condition {
|
|
|
|
fid,
|
|
|
|
op: Contains { keyword: Token { span: contains, value: None }, word: value },
|
|
|
|
},
|
|
|
|
))
|
2024-07-17 17:13:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// contains = value "NOT" WS+ "CONTAINS" value
|
|
|
|
pub fn parse_not_contains(input: Span) -> IResult<FilterCondition> {
|
|
|
|
let keyword = tuple((tag("NOT"), multispace1, tag("CONTAINS")));
|
2024-07-17 22:54:33 +08:00
|
|
|
let (input, (fid, (_not, _spaces, contains), value)) =
|
|
|
|
tuple((parse_value, keyword, cut(parse_value)))(input)?;
|
2024-07-17 17:13:37 +08:00
|
|
|
|
|
|
|
Ok((
|
|
|
|
input,
|
2024-07-17 22:54:33 +08:00
|
|
|
FilterCondition::Not(Box::new(FilterCondition::Condition {
|
|
|
|
fid,
|
|
|
|
op: Contains { keyword: Token { span: contains, value: None }, word: value },
|
|
|
|
})),
|
2024-07-17 17:13:37 +08:00
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2022-06-16 15:12:37 +08:00
|
|
|
/// to = value value "TO" WS+ value
|
2021-11-03 03:27:07 +08:00
|
|
|
pub fn parse_to(input: Span) -> IResult<FilterCondition> {
|
2022-06-16 15:12:37 +08:00
|
|
|
let (input, (key, from, _, _, to)) =
|
|
|
|
tuple((parse_value, parse_value, tag("TO"), multispace1, cut(parse_value)))(input)?;
|
2021-10-22 07:59:38 +08:00
|
|
|
|
2022-01-10 22:59:04 +08:00
|
|
|
Ok((input, FilterCondition::Condition { fid: key, op: Between { from, to } }))
|
2021-10-22 07:59:38 +08:00
|
|
|
}
|