Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion engine/src/ast/field_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use sliceslice::MemchrSearcher;
use std::cmp::Ordering;
use std::collections::BTreeSet;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::num::IntErrorKind;
#[cfg(any(target_arch = "x86", target_arch = "x86_64", target_arch = "wasm32"))]
use std::sync::LazyLock;

Expand Down Expand Up @@ -363,7 +364,22 @@ impl ComparisonExpr {
(Type::Ip, ComparisonOp::Ordering(op))
| (Type::Bytes, ComparisonOp::Ordering(op))
| (Type::Int, ComparisonOp::Ordering(op)) => {
let (rhs, input) = RhsValue::lex_with(input, lhs_type)?;
let (rhs, input) =
RhsValue::lex_with(input, lhs_type).map_err(|(kind, rest)| {
match (lhs_type, kind) {
(Type::Bytes, LexErrorKind::ParseInt { err, radix })
if *err.kind() == IntErrorKind::InvalidDigit && radix == 16 =>
{
(
LexErrorKind::ExpectedName(
"quoted utf8 or raw or hex string",
),
input,
)
}
(_, kind) => (kind, rest),
}
})?;
(ComparisonOpExpr::Ordering { op, rhs }, input)
}
(Type::Int, ComparisonOp::Int(op)) => {
Expand Down Expand Up @@ -1214,6 +1230,15 @@ mod tests {
);
}

// just check that parsing correctly reports errors
{
assert_err!(
FilterParser::new(&SCHEME).lex_as::<ComparisonExpr>(r#"http.host eq http.host"#),
LexErrorKind::ExpectedName("quoted utf8 or raw or hex string"),
"http.host"
);
}

let expr = assert_ok!(
FilterParser::new(&SCHEME).lex_as(r#"http.host == "example.org""#),
ComparisonExpr {
Expand Down
6 changes: 3 additions & 3 deletions engine/src/ast/index_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ mod tests {

assert_err!(
FilterParser::new(&SCHEME).lex_as::<IndexExpr>("test[-1]"),
LexErrorKind::ExpectedLiteral("expected positive integer as index"),
LexErrorKind::ExpectedName("positive integer as index"),
"-1]"
);
}
Expand Down Expand Up @@ -647,13 +647,13 @@ mod tests {
fn test_access_with_non_string() {
assert_err!(
FilterParser::new(&SCHEME).lex_as::<IndexExpr>(r#"test[a]"#),
LexErrorKind::ExpectedLiteral("expected quoted utf8 string or positive integer"),
LexErrorKind::ExpectedName("quoted utf8 string or positive integer"),
"a]"
);

assert_err!(
FilterParser::new(&SCHEME).lex_as::<IndexExpr>(r#"map[a]"#),
LexErrorKind::ExpectedLiteral("expected quoted utf8 string or positive integer"),
LexErrorKind::ExpectedName("quoted utf8 string or positive integer"),
"a]"
);
}
Expand Down
10 changes: 5 additions & 5 deletions engine/src/rhs_types/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ pub(crate) fn lex_raw_string_as_str(input: &str) -> LexResult<'_, (&str, u8)> {
// consume '"'`
if input.as_bytes().get(start_hash_count) != Some(&b'"') {
return Err((
LexErrorKind::ExpectedName("\" or #"),
LexErrorKind::ExpectedLiteral("\" or #"),
&full_input[start_hash_count..],
));
}
Expand Down Expand Up @@ -324,7 +324,7 @@ pub(crate) fn lex_quoted_or_raw_string(input: &str) -> LexResult<'_, BytesExpr>
match input.as_bytes().first() {
Some(b'"') => lex_quoted_string(&input[1..]),
Some(b'r') => lex_raw_string(&input[1..]),
Some(_) => Err((LexErrorKind::ExpectedName("\" or r"), input)),
Some(_) => Err((LexErrorKind::ExpectedLiteral("\" or r"), input)),
None => Err((LexErrorKind::EOF, input)),
}
}
Expand Down Expand Up @@ -587,17 +587,17 @@ mod test {
// Invalid character after 'r' or '#'
assert_err!(
BytesExpr::lex("r"),
LexErrorKind::ExpectedName("\" or #"),
LexErrorKind::ExpectedLiteral("\" or #"),
""
);
assert_err!(
BytesExpr::lex("r#ab"),
LexErrorKind::ExpectedName("\" or #"),
LexErrorKind::ExpectedLiteral("\" or #"),
"ab"
);
assert_err!(
BytesExpr::lex("r##ab"),
LexErrorKind::ExpectedName("\" or #"),
LexErrorKind::ExpectedLiteral("\" or #"),
"ab"
);

Expand Down
4 changes: 2 additions & 2 deletions engine/src/rhs_types/regex/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl<'i, 's> LexWith<'i, &FilterParser<'s>> for Regex {
match c {
b'"' => lex_regex_from_literal(&input[1..], parser),
b'r' => lex_regex_from_raw_string(&input[1..], parser),
_ => Err((LexErrorKind::ExpectedName("\" or r"), input)),
_ => Err((LexErrorKind::ExpectedLiteral("\" or r"), input)),
}
} else {
Err((LexErrorKind::EOF, input))
Expand Down Expand Up @@ -228,7 +228,7 @@ mod test {

assert_err!(
Regex::lex_with("x", &FilterParser::new(&scheme)),
LexErrorKind::ExpectedName("\" or r"),
LexErrorKind::ExpectedLiteral("\" or r"),
"x"
);
}
Expand Down
2 changes: 1 addition & 1 deletion engine/src/rhs_types/wildcard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ mod test {

assert_err!(
Wildcard::<STRICT>::lex_with("61:20:71:75:6F:74", &FilterParser::new(&scheme)),
LexErrorKind::ExpectedName("\" or r"),
LexErrorKind::ExpectedLiteral("\" or r"),
"61:20:71:75:6F:74"
);
}
Expand Down
49 changes: 43 additions & 6 deletions engine/src/scheme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ impl<'i> Lex<'i> for FieldIndex {
Ok(_) => RhsValue::lex_with(input, Type::Bytes),
Err(_) => RhsValue::lex_with(input, Type::Int).map_err(|_| {
(
LexErrorKind::ExpectedLiteral(
"expected quoted utf8 string or positive integer",
),
LexErrorKind::ExpectedName("quoted utf8 string or positive integer"),
input,
)
}),
Expand All @@ -78,13 +76,13 @@ impl<'i> Lex<'i> for FieldIndex {
RhsValue::Int(i) => match u32::try_from(i) {
Ok(u) => Ok((FieldIndex::ArrayIndex(u), rest)),
Err(_) => Err((
LexErrorKind::ExpectedLiteral("expected positive integer as index"),
LexErrorKind::ExpectedName("positive integer as index"),
input,
)),
},
RhsValue::Bytes(b) => match String::from_utf8(b.into()) {
Ok(s) => Ok((FieldIndex::MapKey(s), rest)),
Err(_) => Err((LexErrorKind::ExpectedLiteral("expected utf8 string"), input)),
Err(_) => Err((LexErrorKind::ExpectedName("utf8 string"), input)),
},
_ => unreachable!(),
}
Expand Down Expand Up @@ -1233,6 +1231,30 @@ fn test_parse_error() {
);
}

{
let err = scheme.parse("str eq str").unwrap_err();
assert_eq!(
err,
ParseError {
kind: LexErrorKind::ExpectedName("quoted utf8 or raw or hex string"),
input: "str eq str",
line_number: 0,
span_start: 7,
span_len: 3
}
);
assert_eq!(
err.to_string(),
indoc!(
r#"
Filter parsing error (1:8):
str eq str
^^^ expected quoted utf8 or raw or hex string
"#
)
);
}

{
let err = scheme.parse(indoc!(r"concat(0, 0) == 0")).unwrap_err();
assert_eq!(
Expand Down Expand Up @@ -1631,6 +1653,21 @@ fn test_parse_error_ordering_op() {
);
}

{
let filter = format!("str {op} str");
let err = scheme.parse(&filter).unwrap_err();
assert_eq!(
err,
ParseError {
kind: LexErrorKind::ExpectedName("quoted utf8 or raw or hex string"),
input: &filter,
line_number: 0,
span_start: 7,
span_len: 3,
}
);
}

{
let filter = format!("str_arr {op} 0");
let err = scheme.parse(&filter).unwrap_err();
Expand Down Expand Up @@ -1805,7 +1842,7 @@ fn test_field_lex_indexes() {
assert_ok!(FieldIndex::lex("0"), FieldIndex::ArrayIndex(0));
assert_err!(
FieldIndex::lex("-1"),
LexErrorKind::ExpectedLiteral("expected positive integer as index"),
LexErrorKind::ExpectedName("positive integer as index"),
"-1"
);

Expand Down