Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add location information to lexed tokens and expand filter error messages to take advantage of it #40

Merged
merged 4 commits into from
Aug 7, 2024
Merged
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
6 changes: 4 additions & 2 deletions src/filter/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ impl<'a, 'b> ExprVisitor<std::fmt::Result> for ExprPrinter<'a, 'b> {
mod tests {
use rstest::rstest;

use crate::filter::location::Loc;

use super::*;

#[rstest]
Expand All @@ -92,15 +94,15 @@ mod tests {
#[case(
Expr::Binary(
Box::new(Expr::Literal("value".into())),
Token::In,
Token::In(Loc::new(1, 8)),
Box::new(Expr::Property("test")),
),
"(in \"value\" (property test))"
)]
#[case(
Expr::Logical(
Box::new(Expr::Literal("value".into())),
Token::And,
Token::And(Loc::new(1, 8)),
Box::new(Expr::Property("test")),
),
"(&& \"value\" (property test))"
Expand Down
22 changes: 11 additions & 11 deletions src/filter/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ impl<'a, T: Filterable> ExprVisitor<FilterValue> for FilterContext<'a, T> {
let left = self.visit_expr(left);
let right = self.visit_expr(right);
match operator {
Token::Equals => (left == right).into(),
Token::NotEquals => (left != right).into(),
Token::Contains => left.contains(&right).into(),
Token::In => right.contains(&left).into(),
Token::StartsWith => left.startswith(&right).into(),
Token::EndsWith => left.endswith(&right).into(),
Token::Equals(..) => (left == right).into(),
Token::NotEquals(..) => (left != right).into(),
Token::Contains(..) => left.contains(&right).into(),
Token::In(..) => right.contains(&left).into(),
Token::StartsWith(..) => left.startswith(&right).into(),
Token::EndsWith(..) => left.endswith(&right).into(),
token => unreachable!("Encountered an unexpected binary operator '{token}'"),
}
}
Expand All @@ -41,10 +41,10 @@ impl<'a, T: Filterable> ExprVisitor<FilterValue> for FilterContext<'a, T> {
let left = self.visit_expr(left);

match operator {
Token::And if left.is_truthy() => self.visit_expr(right),
Token::And => false.into(),
Token::Or if !left.is_truthy() => self.visit_expr(right),
Token::Or => true.into(),
Token::And(..) if left.is_truthy() => self.visit_expr(right),
Token::And(..) => false.into(),
Token::Or(..) if !left.is_truthy() => self.visit_expr(right),
Token::Or(..) => true.into(),
token => unreachable!("Encountered an unexpected logical operator '{token}'"),
}
}
Expand All @@ -53,7 +53,7 @@ impl<'a, T: Filterable> ExprVisitor<FilterValue> for FilterContext<'a, T> {
let right = self.visit_expr(right);

match operator {
Token::Not => {
Token::Not(..) => {
if right.is_truthy() {
false.into()
} else {
Expand Down
Loading
Loading