| 283 | } |
| 284 | |
| 285 | pub fn parse_expr(pairs: Pairs<Rule>) -> Result<Expr, ParseError> { |
| 286 | PRATT_PARSER |
| 287 | .map_primary(|primary| parse_primary(primary)) |
| 288 | .map_infix(|lhs, op, rhs| { |
| 289 | let bin_op_type = match op.as_rule() { |
| 290 | Rule::add => BinOpType::Add, |
| 291 | Rule::subtract => BinOpType::Sub, |
| 292 | Rule::multiply => BinOpType::Mul, |
| 293 | Rule::divide => BinOpType::Div, |
| 294 | Rule::pow => BinOpType::Pow, |
| 295 | Rule::eq => BinOpType::Eq, |
| 296 | Rule::modulo => BinOpType::Mod, |
| 297 | Rule::lt => BinOpType::Lt, |
| 298 | Rule::lte => BinOpType::Lte, |
| 299 | Rule::gt => BinOpType::Gt, |
| 300 | Rule::gte => BinOpType::Gte, |
| 301 | Rule::ne => BinOpType::Neq, |
| 302 | Rule::and => BinOpType::And, |
| 303 | Rule::or => BinOpType::Or, |
| 304 | Rule::xor => BinOpType::Xor, |
| 305 | Rule::IN => BinOpType::In, |
| 306 | Rule::contains => BinOpType::Contains, |
| 307 | Rule::starts_with => BinOpType::StartsWith, |
| 308 | Rule::ends_with => BinOpType::EndsWith, |
| 309 | rule => return unsupported("parse_expr", &rule), |
| 310 | }; |
| 311 | |
| 312 | Ok(Expr::BinOp { |
| 313 | op: bin_op_type, |
| 314 | left: Box::new(lhs?), |
| 315 | right: Box::new(rhs?), |
| 316 | }) |
| 317 | }) |
| 318 | .map_prefix(|op, expr| match op.as_rule() { |
| 319 | Rule::not => Ok(Expr::UnaryOp { |
| 320 | op: UnaryOpType::Not, |
| 321 | expr: Box::new(expr?), |
| 322 | }), |
| 323 | Rule::minus => Ok(Expr::UnaryOp { |
| 324 | op: UnaryOpType::Neg, |
| 325 | expr: Box::new(expr?), |
| 326 | }), |
| 327 | rule => unsupported("parse_expr", &rule), |
| 328 | }) |
| 329 | .map_postfix(|expr, op| match op.as_rule() { |
| 330 | Rule::is_null => { |
| 331 | if let Some(Rule::NOT) = op.into_inner().next().map(|op| op.as_rule()) { |
| 332 | Ok(Expr::is_not_null(expr?)) |
| 333 | } else { |
| 334 | Ok(Expr::is_null(expr?)) |
| 335 | } |
| 336 | } |
| 337 | rule => unsupported("parse_expr", &rule), |
| 338 | }) |
| 339 | .parse(pairs) |
| 340 | } |
| 341 | |
| 342 | pub fn parse_primary(pair: Pair<Rule>) -> Result<Expr, ParseError> { |