dsq_parser/parser/
mod.rs

1//! Parser implementation for DSQ filter language
2//!
3//! This module contains the main parser that converts DSQ filter strings
4//! into AST representations using nom parser combinators.
5
6use nom::{
7    branch::alt,
8    character::complete::multispace1,
9    combinator::{all_consuming, map, opt},
10    sequence::preceded,
11    IResult, Parser,
12};
13
14use crate::ast::{OrderBy, OrderDirection, *};
15use crate::error::{ParseError, Result};
16
17mod expressions;
18mod identifiers;
19mod literals;
20mod operators;
21mod utils;
22
23use expressions::{parse_comma_sequence, parse_if};
24use identifiers::parse_identifier;
25use utils::keyword;
26
27/// Main parser for DSQ filter expressions
28pub struct FilterParser {
29    // future parser configuration could go here
30}
31
32impl FilterParser {
33    /// Create a new parser instance
34    pub fn new() -> Self {
35        Self {}
36    }
37
38    /// Parse a DSQ filter string into an AST
39    pub fn parse(&self, input: &str) -> Result<Filter> {
40        let input = input.trim();
41        if input.is_empty() {
42            return Err(ParseError::EmptyInput);
43        }
44
45        match all_consuming(parse_filter).parse(input) {
46            Ok((_, filter)) => Ok(filter),
47            Err(e) => Err(ParseError::from(e)),
48        }
49    }
50}
51
52/// Parse a complete filter
53fn parse_filter(input: &str) -> IResult<&str, Filter> {
54    all_consuming(map(alt((parse_if, parse_comma_sequence)), |expr| Filter {
55        expr,
56    }))
57    .parse(input)
58}
59
60/// Parse order by clause
61#[allow(dead_code)]
62fn parse_order_by(input: &str) -> IResult<&str, OrderBy> {
63    map(
64        (
65            parse_identifier,
66            opt(preceded(
67                multispace1,
68                alt((
69                    map(keyword("asc"), |_| OrderDirection::Asc),
70                    map(keyword("desc"), |_| OrderDirection::Desc),
71                )),
72            )),
73        ),
74        |(column, direction)| OrderBy {
75            column,
76            direction: direction.unwrap_or(OrderDirection::Asc),
77        },
78    )
79    .parse(input)
80}
81
82impl Default for FilterParser {
83    fn default() -> Self {
84        Self::new()
85    }
86}