Skip to main content

varpulis_parser/
pest_parser.rs

1//! Pest-based parser for VPL
2//!
3//! This module provides parsing using the pest PEG parser generator.
4//!
5//! The `Rule` enum and its variants are auto-generated by `pest_derive`
6//! from the grammar file and cannot carry doc comments.
7
8use pest::Parser;
9use pest_derive::Parser;
10use varpulis_core::ast::*;
11use varpulis_core::span::{Span, Spanned};
12use varpulis_core::types::Type;
13
14use crate::error::{ParseError, ParseResult};
15use crate::helpers::{parse_duration, parse_timestamp};
16use crate::indent::preprocess_indentation;
17
18/// Extension trait for safer iterator extraction
19trait IteratorExt<'a> {
20    /// Get the next element or return an error with the expected rule description
21    fn expect_next(&mut self, expected: &str) -> ParseResult<pest::iterators::Pair<'a, Rule>>;
22}
23
24impl<'a> IteratorExt<'a> for pest::iterators::Pairs<'a, Rule> {
25    fn expect_next(&mut self, expected: &str) -> ParseResult<pest::iterators::Pair<'a, Rule>> {
26        self.next().ok_or_else(|| ParseError::Located {
27            line: 0,
28            column: 0,
29            position: 0,
30            message: format!("Expected {expected}"),
31            hint: None,
32        })
33    }
34}
35
36/// Pest-based VPL parser.
37///
38/// The grammar rules are auto-generated from `varpulis.pest` by the
39/// `pest_derive` macro. Use [`parse`] instead of calling this directly.
40#[derive(Debug, Parser)]
41#[grammar = "varpulis.pest"]
42pub struct VarpulisParser;
43
44/// Maximum wall-clock time the parser is allowed to run before being aborted.
45///
46/// PEG recursive-descent parsers can exhibit exponential backtracking on
47/// adversarial inputs even after bracket-depth and unmatched-bracket pre-scans.
48/// A hard timeout protects callers (CLI, LSP, fuzz targets) from hangs.
49/// 10 seconds is orders of magnitude more than any real VPL program needs
50/// (typical parse time is <5 ms for a 1000-line file).
51const PARSE_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10);
52
53/// Parse a VPL source string into a Program AST.
54///
55/// Runs pest parsing in a dedicated thread with a 16 MB stack and a wall-clock
56/// timeout to guard against stack overflow and exponential backtracking on
57/// adversarial inputs.
58pub fn parse(source: &str) -> ParseResult<Program> {
59    let source = source.to_string();
60    let handle = std::thread::Builder::new()
61        .stack_size(16 * 1024 * 1024)
62        .spawn(move || parse_inner(&source))
63        .map_err(|e| ParseError::InvalidToken {
64            position: 0,
65            message: format!("Failed to spawn parser thread: {e}"),
66        })?;
67
68    // Park the current thread until the parser finishes or the timeout fires.
69    let deadline = std::time::Instant::now() + PARSE_TIMEOUT;
70    loop {
71        if handle.is_finished() {
72            return handle.join().unwrap_or_else(|_| {
73                Err(ParseError::InvalidToken {
74                    position: 0,
75                    message: "Parser stack overflow on deeply nested input".to_string(),
76                })
77            });
78        }
79        let remaining = deadline.saturating_duration_since(std::time::Instant::now());
80        if remaining.is_zero() {
81            return Err(ParseError::InvalidToken {
82                position: 0,
83                message: format!(
84                    "Parser timed out after {}s on pathological input",
85                    PARSE_TIMEOUT.as_secs()
86                ),
87            });
88        }
89        std::thread::sleep(std::time::Duration::from_millis(5).min(remaining));
90    }
91}
92
93/// Maximum bracket nesting depth allowed before pest parsing.
94///
95/// PEG recursive descent can cause exponential backtracking when unmatched
96/// brackets create ambiguity between array_literal, index_access, and
97/// slice_access rules.  Measured scaling is O(2.35^depth): depth 20 takes
98/// 1200s+, depth 16 takes 39s, depth 10 takes under 0.3s.  10 levels is
99/// generous for real VPL programs (typical nesting is 3-6 levels, extreme
100/// real-world is about 8) while keeping worst-case parse time under 1 second.
101const MAX_NESTING_DEPTH: usize = 10;
102
103/// Maximum number of unmatched open brackets allowed.
104///
105/// Even when nesting depth stays within limits, many unmatched open brackets
106/// at different positions (e.g. `a[b[c[d[` with no closing `]`) cause
107/// combinatorial explosion as pest tries each `[` as a potential array_literal,
108/// index_access, or slice_access start. 6 is generous for real VPL programs
109/// (unmatched brackets are always parse errors anyway).
110const MAX_UNMATCHED_OPEN_BRACKETS: usize = 6;
111
112/// O(n) pre-scan that rejects inputs with bracket nesting deeper than
113/// `MAX_NESTING_DEPTH` or too many unmatched open brackets. Respects string
114/// literals and comments so that brackets inside `"..."`, `# ...`, or
115/// `/* ... */` are ignored.
116fn check_nesting_depth(source: &str) -> ParseResult<()> {
117    let mut depth: usize = 0;
118    let mut max_depth: usize = 0;
119    let mut max_depth_pos: usize = 0;
120    let bytes = source.as_bytes();
121    let len = bytes.len();
122    let mut i = 0;
123
124    // Per-bracket-type counters for unmatched detection
125    let mut paren_open: usize = 0;
126    let mut paren_close: usize = 0;
127    let mut square_open: usize = 0;
128    let mut square_close: usize = 0;
129    let mut curly_open: usize = 0;
130    let mut curly_close: usize = 0;
131
132    while i < len {
133        let b = bytes[i];
134
135        // Skip double-quoted strings
136        if b == b'"' {
137            i += 1;
138            while i < len {
139                if bytes[i] == b'\\' {
140                    i += 2; // skip escaped char
141                    continue;
142                }
143                if bytes[i] == b'"' {
144                    i += 1;
145                    break;
146                }
147                i += 1;
148            }
149            continue;
150        }
151
152        // Skip VPL line comments (# to end of line)
153        if b == b'#' {
154            i += 1;
155            while i < len && bytes[i] != b'\n' {
156                i += 1;
157            }
158            continue;
159        }
160
161        // Skip block comments
162        if b == b'/' && i + 1 < len && bytes[i + 1] == b'*' {
163            i += 2;
164            while i + 1 < len {
165                if bytes[i] == b'*' && bytes[i + 1] == b'/' {
166                    i += 2;
167                    break;
168                }
169                i += 1;
170            }
171            continue;
172        }
173
174        // Track bracket depth
175        match b {
176            b'(' => {
177                depth += 1;
178                paren_open += 1;
179            }
180            b'[' => {
181                depth += 1;
182                square_open += 1;
183            }
184            b'{' => {
185                depth += 1;
186                curly_open += 1;
187            }
188            b')' => {
189                depth = depth.saturating_sub(1);
190                paren_close += 1;
191            }
192            b']' => {
193                depth = depth.saturating_sub(1);
194                square_close += 1;
195            }
196            b'}' => {
197                depth = depth.saturating_sub(1);
198                curly_close += 1;
199            }
200            _ => {}
201        }
202
203        if depth > max_depth {
204            max_depth = depth;
205            max_depth_pos = i;
206        }
207
208        if max_depth > MAX_NESTING_DEPTH {
209            return Err(ParseError::InvalidToken {
210                position: max_depth_pos,
211                message: format!("Nesting depth exceeds maximum of {MAX_NESTING_DEPTH} levels"),
212            });
213        }
214
215        i += 1;
216    }
217
218    // Reject inputs with too many unmatched open brackets.
219    // Unmatched brackets are always parse errors, but they cause exponential
220    // backtracking in pest before it can report the error.
221    let unmatched = paren_open.saturating_sub(paren_close)
222        + square_open.saturating_sub(square_close)
223        + curly_open.saturating_sub(curly_close);
224    if unmatched > MAX_UNMATCHED_OPEN_BRACKETS {
225        return Err(ParseError::InvalidToken {
226            position: max_depth_pos,
227            message: format!(
228                "Too many unmatched open brackets ({unmatched}); maximum is {MAX_UNMATCHED_OPEN_BRACKETS}"
229            ),
230        });
231    }
232
233    Ok(())
234}
235
236fn parse_inner(source: &str) -> ParseResult<Program> {
237    // Expand compile-time declaration loops (top-level for with {var} interpolation)
238    let expanded =
239        crate::expand::expand_declaration_loops(source).map_err(|e| ParseError::InvalidToken {
240            position: 0,
241            message: e,
242        })?;
243    // Preprocess to add INDENT/DEDENT markers
244    let preprocessed = preprocess_indentation(&expanded);
245
246    // Reject deeply nested input before pest parsing to prevent stack overflow
247    check_nesting_depth(&preprocessed)?;
248
249    let pairs = VarpulisParser::parse(Rule::program, &preprocessed).map_err(convert_pest_error)?;
250
251    let mut statements = Vec::new();
252
253    for pair in pairs {
254        if pair.as_rule() == Rule::program {
255            for inner in pair.into_inner() {
256                if inner.as_rule() == Rule::statement {
257                    statements.push(parse_statement(inner)?);
258                }
259            }
260        }
261    }
262
263    Ok(crate::optimize::fold_program(Program { statements }))
264}
265
266fn convert_pest_error(e: pest::error::Error<Rule>) -> ParseError {
267    let position = match e.location {
268        pest::error::InputLocation::Pos(p) => p,
269        pest::error::InputLocation::Span((s, _)) => s,
270    };
271
272    // Extract line/column from pest error
273    let (line, column) = match e.line_col {
274        pest::error::LineColLocation::Pos((l, c)) => (l, c),
275        pest::error::LineColLocation::Span((l, c), _) => (l, c),
276    };
277
278    // Create a human-readable message based on what was expected
279    let (message, hint) = match &e.variant {
280        pest::error::ErrorVariant::ParsingError {
281            positives,
282            negatives: _,
283        } => {
284            if positives.is_empty() {
285                ("Unexpected token".to_string(), None)
286            } else if is_stream_op_error(positives) {
287                // All positives are stream operations — produce a concise error
288                (
289                    "unknown stream operation".to_string(),
290                    Some(
291                        "valid operations: .where(), .select(), .emit(), .window(), .aggregate(), \
292                         .partition_by(), .within(), .having(), .to(), .context(), .log(), .print(), \
293                         .enrich(), .forecast(), .trend_aggregate(), .watermark(), .tap()"
294                            .to_string(),
295                    ),
296                )
297            } else {
298                let expected: Vec<String> = positives.iter().map(format_rule_name).collect();
299                if expected.len() == 1 {
300                    (format!("Expected {}", expected[0]), None)
301                } else {
302                    (format!("Expected one of: {}", expected.join(", ")), None)
303                }
304            }
305        }
306        pest::error::ErrorVariant::CustomError { message } => (message.clone(), None),
307    };
308
309    ParseError::Located {
310        line,
311        column,
312        position,
313        message,
314        hint,
315    }
316}
317
318/// Convert pest Rule names to human-readable format
319fn format_rule_name(rule: &Rule) -> String {
320    match rule {
321        Rule::identifier => "identifier".to_string(),
322        Rule::integer => "number".to_string(),
323        Rule::float => "number".to_string(),
324        Rule::string => "string".to_string(),
325        Rule::primitive_type => "type (int, float, bool, str, timestamp, duration)".to_string(),
326        Rule::type_expr => "type".to_string(),
327        Rule::expr => "expression".to_string(),
328        Rule::statement => "statement".to_string(),
329        Rule::context_decl => "context declaration".to_string(),
330        Rule::stream_decl => "stream declaration".to_string(),
331        Rule::pattern_decl => "pattern declaration".to_string(),
332        Rule::event_decl => "event declaration".to_string(),
333        Rule::fn_decl => "function declaration".to_string(),
334        Rule::INDENT => "indented block".to_string(),
335        Rule::DEDENT => "end of block".to_string(),
336        Rule::field => "field declaration (name: type)".to_string(),
337        Rule::comparison_op => "comparison operator (==, !=, <, >, <=, >=)".to_string(),
338        Rule::additive_op => "operator (+, -)".to_string(),
339        Rule::multiplicative_op => "operator (*, /, %)".to_string(),
340        Rule::postfix_suffix => "method call or member access".to_string(),
341        Rule::sase_pattern_expr => "SASE pattern expression".to_string(),
342        Rule::sase_seq_expr => "SEQ expression".to_string(),
343        Rule::kleene_op => "Kleene operator (+, *, ?)".to_string(),
344        _ => format!("{rule:?}").to_lowercase().replace('_', " "),
345    }
346}
347
348/// Returns true when all positives look like stream operation rules.
349/// This is used to produce a concise "unknown stream operation" error
350/// instead of listing 30+ individual operation names.
351fn is_stream_op_error(positives: &[Rule]) -> bool {
352    const STREAM_OP_RULES: &[Rule] = &[
353        Rule::context_op,
354        Rule::where_op,
355        Rule::select_op,
356        Rule::window_op,
357        Rule::aggregate_op,
358        Rule::having_op,
359        Rule::partition_by_op,
360        Rule::order_by_op,
361        Rule::limit_op,
362        Rule::distinct_op,
363        Rule::map_op,
364        Rule::filter_op,
365        Rule::tap_op,
366        Rule::print_op,
367        Rule::log_op,
368        Rule::emit_op,
369        Rule::to_op,
370        Rule::pattern_op,
371        Rule::concurrent_op,
372        Rule::process_op,
373        Rule::on_error_op,
374        Rule::collect_op,
375        Rule::on_op,
376        Rule::within_op,
377        Rule::not_op,
378        Rule::fork_op,
379        Rule::any_op,
380        Rule::all_op,
381        Rule::first_op,
382        Rule::watermark_op,
383        Rule::allowed_lateness_op,
384        Rule::trend_aggregate_op,
385        Rule::score_op,
386        Rule::forecast_op,
387        Rule::enrich_op,
388    ];
389    positives.len() >= 10 && positives.iter().all(|r| STREAM_OP_RULES.contains(r))
390}
391
392fn parse_statement(pair: pest::iterators::Pair<Rule>) -> ParseResult<Spanned<Stmt>> {
393    let span = Span::new(pair.as_span().start(), pair.as_span().end());
394    let inner = pair.into_inner().expect_next("statement body")?;
395
396    let stmt = match inner.as_rule() {
397        Rule::context_decl => parse_context_decl(inner)?,
398        Rule::connector_decl => parse_connector_decl(inner)?,
399        Rule::stream_decl => parse_stream_decl(inner)?,
400        Rule::pattern_decl => parse_pattern_decl(inner)?,
401        Rule::event_decl => parse_event_decl(inner)?,
402        Rule::type_decl => parse_type_decl(inner)?,
403        Rule::var_decl => parse_var_decl(inner)?,
404        Rule::const_decl => parse_const_decl(inner)?,
405        Rule::fn_decl => parse_fn_decl(inner)?,
406        Rule::config_block => parse_config_block(inner)?,
407        Rule::import_stmt => parse_import_stmt(inner)?,
408        Rule::if_stmt => parse_if_stmt(inner)?,
409        Rule::for_stmt => parse_for_stmt(inner)?,
410        Rule::while_stmt => parse_while_stmt(inner)?,
411        Rule::return_stmt => parse_return_stmt(inner)?,
412        Rule::break_stmt => Stmt::Break,
413        Rule::continue_stmt => Stmt::Continue,
414        Rule::emit_stmt => parse_emit_stmt(inner)?,
415        Rule::assignment_stmt => {
416            let mut inner = inner.into_inner();
417            let name = inner.expect_next("variable name")?.as_str().to_string();
418            let value = parse_expr(inner.expect_next("assignment value")?)?;
419            Stmt::Assignment { name, value }
420        }
421        Rule::expr_stmt => Stmt::Expr(parse_expr(inner.into_inner().expect_next("expression")?)?),
422        _ => {
423            return Err(ParseError::UnexpectedToken {
424                position: span.start,
425                expected: "statement".to_string(),
426                found: format!("{:?}", inner.as_rule()),
427            })
428        }
429    };
430
431    Ok(Spanned::new(stmt, span))
432}
433
434// ============================================================================
435// Context Declaration Parsing
436// ============================================================================
437
438fn parse_context_decl(pair: pest::iterators::Pair<Rule>) -> ParseResult<Stmt> {
439    let mut inner = pair.into_inner();
440    let name = inner.expect_next("context name")?.as_str().to_string();
441    let mut cores = None;
442
443    for p in inner {
444        if p.as_rule() == Rule::context_params {
445            for param in p.into_inner() {
446                if param.as_rule() == Rule::context_param {
447                    // Parse cores: [0, 1, 2]
448                    let core_ids: Vec<usize> = param
449                        .into_inner()
450                        .filter(|p| p.as_rule() == Rule::integer)
451                        .map(|p| p.as_str().parse::<usize>().unwrap_or(0))
452                        .collect();
453                    cores = Some(core_ids);
454                }
455            }
456        }
457    }
458
459    Ok(Stmt::ContextDecl { name, cores })
460}
461
462// ============================================================================
463// Connector Parsing
464// ============================================================================
465
466fn parse_connector_decl(pair: pest::iterators::Pair<Rule>) -> ParseResult<Stmt> {
467    let mut inner = pair.into_inner();
468    let name = inner.expect_next("connector name")?.as_str().to_string();
469    let connector_type = inner.expect_next("connector type")?.as_str().to_string();
470    let mut params = Vec::new();
471
472    for p in inner {
473        if p.as_rule() == Rule::connector_params {
474            params = parse_connector_params(p)?;
475        }
476    }
477
478    Ok(Stmt::ConnectorDecl {
479        name,
480        connector_type,
481        params,
482    })
483}
484
485fn parse_connector_params(pair: pest::iterators::Pair<Rule>) -> ParseResult<Vec<ConnectorParam>> {
486    let mut params = Vec::new();
487    for p in pair.into_inner() {
488        if p.as_rule() == Rule::connector_param {
489            let mut inner = p.into_inner();
490            let name = inner.expect_next("param name")?.as_str().to_string();
491            let value_pair = inner.expect_next("param value")?;
492            let value = parse_config_value(value_pair)?;
493            params.push(ConnectorParam { name, value });
494        }
495    }
496    Ok(params)
497}
498
499fn parse_stream_decl(pair: pest::iterators::Pair<Rule>) -> ParseResult<Stmt> {
500    let mut inner = pair.into_inner();
501    let name = inner.expect_next("stream name")?.as_str().to_string();
502
503    let mut type_annotation = None;
504    let mut source = StreamSource::Ident(String::new());
505    let mut ops = Vec::new();
506    let mut op_spans = Vec::new();
507
508    for p in inner {
509        match p.as_rule() {
510            Rule::type_annotation => {
511                type_annotation = Some(parse_type(p.into_inner().expect_next("type")?)?);
512            }
513            Rule::stream_expr => {
514                let (s, o, spans) = parse_stream_expr(p)?;
515                source = s;
516                ops = o;
517                op_spans = spans;
518            }
519            _ => {}
520        }
521    }
522
523    Ok(Stmt::StreamDecl {
524        name,
525        type_annotation,
526        source,
527        ops,
528        op_spans,
529    })
530}
531
532fn parse_stream_expr(
533    pair: pest::iterators::Pair<Rule>,
534) -> ParseResult<(StreamSource, Vec<StreamOp>, Vec<varpulis_core::span::Span>)> {
535    let mut inner = pair.into_inner();
536    let source = parse_stream_source(inner.expect_next("stream source")?)?;
537    let mut ops = Vec::new();
538    let mut op_spans = Vec::new();
539
540    for p in inner {
541        if p.as_rule() == Rule::stream_op {
542            let pest_span = p.as_span();
543            let span = varpulis_core::span::Span::new(pest_span.start(), pest_span.end());
544            ops.push(parse_stream_op(p)?);
545            op_spans.push(span);
546        }
547    }
548
549    Ok((source, ops, op_spans))
550}
551
552// ============================================================================
553// SASE+ Pattern Declaration Parsing
554// ============================================================================
555
556fn parse_pattern_decl(pair: pest::iterators::Pair<Rule>) -> ParseResult<Stmt> {
557    let mut inner = pair.into_inner();
558    let name = inner.expect_next("pattern name")?.as_str().to_string();
559
560    let mut expr = SasePatternExpr::Event(String::new());
561    let mut within = None;
562    let mut partition_by = None;
563
564    for p in inner {
565        match p.as_rule() {
566            Rule::sase_pattern_expr => {
567                expr = parse_sase_pattern_expr(p)?;
568            }
569            Rule::pattern_within_clause => {
570                let dur_pair = p.into_inner().expect_next("within duration")?;
571                within = Some(Expr::Duration(
572                    parse_duration(dur_pair.as_str()).map_err(ParseError::InvalidDuration)?,
573                ));
574            }
575            Rule::pattern_partition_clause => {
576                let key = p
577                    .into_inner()
578                    .expect_next("partition key")?
579                    .as_str()
580                    .to_string();
581                partition_by = Some(Expr::Ident(key));
582            }
583            _ => {}
584        }
585    }
586
587    Ok(Stmt::PatternDecl {
588        name,
589        expr,
590        within,
591        partition_by,
592    })
593}
594
595fn parse_sase_pattern_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<SasePatternExpr> {
596    let inner = pair.into_inner().expect_next("SASE pattern expression")?;
597    parse_sase_or_expr(inner)
598}
599
600fn parse_sase_or_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<SasePatternExpr> {
601    let mut inner = pair.into_inner();
602    let mut left = parse_sase_and_expr(inner.expect_next("OR expression operand")?)?;
603
604    for right_pair in inner {
605        let right = parse_sase_and_expr(right_pair)?;
606        left = SasePatternExpr::Or(Box::new(left), Box::new(right));
607    }
608
609    Ok(left)
610}
611
612fn parse_sase_and_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<SasePatternExpr> {
613    let mut inner = pair.into_inner();
614    let mut left = parse_sase_not_expr(inner.expect_next("AND expression operand")?)?;
615
616    for right_pair in inner {
617        let right = parse_sase_not_expr(right_pair)?;
618        left = SasePatternExpr::And(Box::new(left), Box::new(right));
619    }
620
621    Ok(left)
622}
623
624fn parse_sase_not_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<SasePatternExpr> {
625    let mut inner = pair.into_inner();
626    let first = inner.expect_next("NOT or primary expression")?;
627
628    if first.as_str() == "NOT" {
629        let expr = parse_sase_primary_expr(inner.expect_next("expression after NOT")?)?;
630        Ok(SasePatternExpr::Not(Box::new(expr)))
631    } else {
632        parse_sase_primary_expr(first)
633    }
634}
635
636fn parse_sase_primary_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<SasePatternExpr> {
637    let inner = pair.into_inner().expect_next("SASE primary expression")?;
638
639    match inner.as_rule() {
640        Rule::sase_seq_expr => parse_sase_seq_expr(inner),
641        Rule::sase_grouped_expr => {
642            let nested = inner.into_inner().expect_next("grouped expression")?;
643            let expr = parse_sase_pattern_expr(nested)?;
644            Ok(SasePatternExpr::Group(Box::new(expr)))
645        }
646        Rule::sase_event_ref => parse_sase_event_ref(inner),
647        _ => Ok(SasePatternExpr::Event(inner.as_str().to_string())),
648    }
649}
650
651fn parse_sase_seq_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<SasePatternExpr> {
652    let mut items = Vec::new();
653
654    for p in pair.into_inner() {
655        if p.as_rule() == Rule::sase_seq_items {
656            for item in p.into_inner() {
657                if item.as_rule() == Rule::sase_seq_item {
658                    items.push(parse_sase_seq_item(item)?);
659                }
660            }
661        }
662    }
663
664    Ok(SasePatternExpr::Seq(items))
665}
666
667fn parse_sase_seq_item(pair: pest::iterators::Pair<Rule>) -> ParseResult<SasePatternItem> {
668    let inner = pair.into_inner().expect_next("sequence item")?;
669
670    match inner.as_rule() {
671        Rule::sase_negated_item => parse_sase_item_inner(inner, true),
672        Rule::sase_positive_item => parse_sase_item_inner(inner, false),
673        _ => parse_sase_item_inner(inner, false),
674    }
675}
676
677fn parse_sase_item_inner(
678    pair: pest::iterators::Pair<Rule>,
679    _negated: bool,
680) -> ParseResult<SasePatternItem> {
681    let mut inner = pair.into_inner();
682    let event_type = inner.expect_next("event type")?.as_str().to_string();
683
684    let mut kleene = None;
685    let mut filter = None;
686    let mut alias = None;
687
688    for p in inner {
689        match p.as_rule() {
690            Rule::kleene_op => {
691                kleene = Some(match p.as_str() {
692                    "+" => KleeneOp::Plus,
693                    "*" => KleeneOp::Star,
694                    "?" => KleeneOp::Optional,
695                    _ => KleeneOp::Plus,
696                });
697            }
698            Rule::sase_where_clause => {
699                filter = Some(parse_expr(
700                    p.into_inner().expect_next("filter expression")?,
701                )?);
702            }
703            Rule::sase_alias_clause => {
704                alias = Some(p.into_inner().expect_next("alias")?.as_str().to_string());
705            }
706            _ => {}
707        }
708    }
709
710    // For negated items, we prefix with "!" to indicate negation
711    // The runtime will interpret this
712    let event_type = if _negated {
713        format!("!{event_type}")
714    } else {
715        event_type
716    };
717
718    Ok(SasePatternItem {
719        event_type,
720        alias,
721        kleene,
722        filter,
723    })
724}
725
726fn parse_sase_event_ref(pair: pest::iterators::Pair<Rule>) -> ParseResult<SasePatternExpr> {
727    // Single event reference with optional kleene, where, alias
728    let item = parse_sase_item_inner(pair, false)?;
729
730    // If it's a simple event with no modifiers, return Event variant
731    if item.alias.is_none() && item.kleene.is_none() && item.filter.is_none() {
732        Ok(SasePatternExpr::Event(item.event_type))
733    } else {
734        // Otherwise wrap in a single-item Seq
735        Ok(SasePatternExpr::Seq(vec![item]))
736    }
737}
738
739fn parse_stream_source(pair: pest::iterators::Pair<Rule>) -> ParseResult<StreamSource> {
740    let inner = pair.into_inner().expect_next("stream source type")?;
741
742    match inner.as_rule() {
743        Rule::from_connector_source => {
744            let mut inner_iter = inner.into_inner();
745            let event_type = inner_iter.expect_next("event type")?.as_str().to_string();
746            let connector_name = inner_iter
747                .expect_next("connector name")?
748                .as_str()
749                .to_string();
750            let mut params = Vec::new();
751            for p in inner_iter {
752                if p.as_rule() == Rule::connector_params {
753                    params = parse_connector_params(p)?;
754                }
755            }
756            Ok(StreamSource::FromConnector {
757                event_type,
758                connector_name,
759                params,
760            })
761        }
762        Rule::merge_source => {
763            let mut streams = Vec::new();
764            for p in inner.into_inner() {
765                if p.as_rule() == Rule::inline_stream_list {
766                    for is in p.into_inner() {
767                        streams.push(parse_inline_stream(is)?);
768                    }
769                }
770            }
771            Ok(StreamSource::Merge(streams))
772        }
773        Rule::join_source
774        | Rule::left_join_source
775        | Rule::right_join_source
776        | Rule::full_join_source => {
777            let join_type = match inner.as_rule() {
778                Rule::left_join_source => varpulis_core::ast::JoinType::Left,
779                Rule::right_join_source => varpulis_core::ast::JoinType::Right,
780                Rule::full_join_source => varpulis_core::ast::JoinType::Full,
781                _ => varpulis_core::ast::JoinType::Inner,
782            };
783            let mut clauses = Vec::new();
784            for p in inner.into_inner() {
785                if p.as_rule() == Rule::join_clause_list {
786                    for jc in p.into_inner() {
787                        clauses.push(parse_join_clause(jc, join_type)?);
788                    }
789                }
790            }
791            Ok(StreamSource::Join(clauses))
792        }
793        Rule::sequence_source => {
794            let decl =
795                parse_sequence_decl(inner.into_inner().expect_next("sequence declaration")?)?;
796            Ok(StreamSource::Sequence(decl))
797        }
798        Rule::timer_source => {
799            let timer_args = inner.into_inner().expect_next("timer arguments")?;
800            let decl = parse_timer_decl(timer_args)?;
801            Ok(StreamSource::Timer(decl))
802        }
803        Rule::all_source => {
804            let mut inner_iter = inner.into_inner();
805            let name = inner_iter.expect_next("event name")?.as_str().to_string();
806            let alias = inner_iter.next().map(|p| p.as_str().to_string());
807            Ok(StreamSource::AllWithAlias { name, alias })
808        }
809        Rule::aliased_source => {
810            let mut inner_iter = inner.into_inner();
811            let name = inner_iter.expect_next("event name")?.as_str().to_string();
812            let alias = inner_iter.expect_next("alias")?.as_str().to_string();
813            Ok(StreamSource::IdentWithAlias { name, alias })
814        }
815        Rule::identifier => Ok(StreamSource::Ident(inner.as_str().to_string())),
816        _ => Err(ParseError::UnexpectedToken {
817            position: 0,
818            expected: "stream source".to_string(),
819            found: format!("{:?}", inner.as_rule()),
820        }),
821    }
822}
823
824fn parse_inline_stream(pair: pest::iterators::Pair<Rule>) -> ParseResult<InlineStreamDecl> {
825    let mut inner = pair.into_inner();
826
827    // Check if it's a simple identifier or full declaration
828    let first = inner.expect_next("stream identifier")?;
829    if first.as_rule() == Rule::identifier && inner.clone().next().is_none() {
830        let name = first.as_str().to_string();
831        return Ok(InlineStreamDecl {
832            name: name.clone(),
833            source: name,
834            filter: None,
835        });
836    }
837
838    let name = first.as_str().to_string();
839    let source = inner.expect_next("stream source")?.as_str().to_string();
840    let filter = inner.next().map(|p| parse_expr(p)).transpose()?;
841
842    Ok(InlineStreamDecl {
843        name,
844        source,
845        filter,
846    })
847}
848
849fn parse_join_clause(
850    pair: pest::iterators::Pair<Rule>,
851    join_type: varpulis_core::ast::JoinType,
852) -> ParseResult<JoinClause> {
853    let mut inner = pair.into_inner();
854
855    let first = inner.expect_next("join clause identifier")?;
856    if first.as_rule() == Rule::identifier && inner.clone().next().is_none() {
857        let name = first.as_str().to_string();
858        return Ok(JoinClause {
859            name: name.clone(),
860            source: name,
861            on: None,
862            join_type,
863        });
864    }
865
866    let name = first.as_str().to_string();
867    let source = inner.expect_next("join source")?.as_str().to_string();
868    let on = inner.next().map(|p| parse_expr(p)).transpose()?;
869
870    Ok(JoinClause {
871        name,
872        source,
873        on,
874        join_type,
875    })
876}
877
878fn parse_sequence_decl(pair: pest::iterators::Pair<Rule>) -> ParseResult<SequenceDecl> {
879    let mut steps = Vec::new();
880
881    for p in pair.into_inner() {
882        if p.as_rule() == Rule::sequence_step {
883            steps.push(parse_sequence_step(p)?);
884        }
885    }
886
887    Ok(SequenceDecl {
888        match_all: false,
889        timeout: None,
890        steps,
891    })
892}
893
894fn parse_sequence_step(pair: pest::iterators::Pair<Rule>) -> ParseResult<SequenceStepDecl> {
895    let mut inner = pair.into_inner();
896    let alias = inner.expect_next("step alias")?.as_str().to_string();
897    let event_type = inner.expect_next("event type")?.as_str().to_string();
898
899    let mut filter = None;
900    let mut timeout = None;
901
902    for p in inner {
903        match p.as_rule() {
904            Rule::or_expr => filter = Some(parse_expr(p)?),
905            Rule::within_suffix => {
906                let expr = p.into_inner().expect_next("within duration")?;
907                timeout = Some(Box::new(parse_expr(expr)?));
908            }
909            _ => {}
910        }
911    }
912
913    Ok(SequenceStepDecl {
914        alias,
915        event_type,
916        filter,
917        timeout,
918    })
919}
920
921fn parse_timer_decl(pair: pest::iterators::Pair<Rule>) -> ParseResult<TimerDecl> {
922    let mut inner = pair.into_inner();
923
924    // First argument is the interval (duration expression)
925    let interval = parse_expr(inner.expect_next("timer interval")?)?;
926
927    // Optional second argument is initial_delay (named argument)
928    let mut initial_delay = None;
929    for p in inner {
930        if p.as_rule() == Rule::named_arg {
931            let arg = parse_named_arg(p)?;
932            if arg.name == "initial_delay" {
933                initial_delay = Some(Box::new(arg.value));
934            }
935        }
936    }
937
938    Ok(TimerDecl {
939        interval,
940        initial_delay,
941    })
942}
943
944fn parse_stream_op(pair: pest::iterators::Pair<Rule>) -> ParseResult<StreamOp> {
945    let inner = pair.into_inner().expect_next("stream operation")?;
946
947    match inner.as_rule() {
948        Rule::dot_op => {
949            let op_inner = inner.into_inner().expect_next("dot operation")?;
950            parse_dot_op(op_inner)
951        }
952        Rule::followed_by_op => parse_followed_by_op(inner),
953        _ => Err(ParseError::UnexpectedToken {
954            position: 0,
955            expected: "stream operation".to_string(),
956            found: format!("{:?}", inner.as_rule()),
957        }),
958    }
959}
960
961fn parse_dot_op(pair: pest::iterators::Pair<Rule>) -> ParseResult<StreamOp> {
962    match pair.as_rule() {
963        Rule::context_op => {
964            let name = pair
965                .into_inner()
966                .expect_next("context name")?
967                .as_str()
968                .to_string();
969            Ok(StreamOp::Context(name))
970        }
971        Rule::where_op => {
972            let expr = parse_expr(pair.into_inner().expect_next("where expression")?)?;
973            Ok(StreamOp::Where(expr))
974        }
975        Rule::select_op => {
976            let mut items = Vec::new();
977            for p in pair.into_inner() {
978                if p.as_rule() == Rule::select_list {
979                    for si in p.into_inner() {
980                        items.push(parse_select_item(si)?);
981                    }
982                }
983            }
984            Ok(StreamOp::Select(items))
985        }
986        Rule::window_op => {
987            let args = parse_window_args(pair.into_inner().expect_next("window arguments")?)?;
988            Ok(StreamOp::Window(args))
989        }
990        Rule::aggregate_op => {
991            let mut items = Vec::new();
992            for p in pair.into_inner() {
993                if p.as_rule() == Rule::agg_list {
994                    for ai in p.into_inner() {
995                        items.push(parse_agg_item(ai)?);
996                    }
997                }
998            }
999            Ok(StreamOp::Aggregate(items))
1000        }
1001        Rule::having_op => {
1002            let expr = parse_expr(pair.into_inner().expect_next("having expression")?)?;
1003            Ok(StreamOp::Having(expr))
1004        }
1005        Rule::map_op => {
1006            let expr = parse_expr(pair.into_inner().expect_next("map expression")?)?;
1007            Ok(StreamOp::Map(expr))
1008        }
1009        Rule::filter_op => {
1010            let expr = parse_expr(pair.into_inner().expect_next("filter expression")?)?;
1011            Ok(StreamOp::Filter(expr))
1012        }
1013        Rule::within_op => {
1014            let expr = parse_expr(pair.into_inner().expect_next("within duration")?)?;
1015            Ok(StreamOp::Within(expr))
1016        }
1017        Rule::emit_op => {
1018            let mut output_type = None;
1019            let mut fields = Vec::new();
1020            let mut target_context = None;
1021            for p in pair.into_inner() {
1022                match p.as_rule() {
1023                    Rule::emit_type_cast => {
1024                        output_type = Some(
1025                            p.into_inner()
1026                                .expect_next("type name")?
1027                                .as_str()
1028                                .to_string(),
1029                        );
1030                    }
1031                    Rule::named_arg_list => {
1032                        for arg in p.into_inner() {
1033                            let parsed = parse_named_arg(arg)?;
1034                            // Extract `context: ctx_name` as target_context
1035                            if parsed.name == "context" {
1036                                if let Expr::Ident(ctx_name) = &parsed.value {
1037                                    target_context = Some(ctx_name.clone());
1038                                    continue;
1039                                }
1040                            }
1041                            fields.push(parsed);
1042                        }
1043                    }
1044                    _ => {}
1045                }
1046            }
1047            Ok(StreamOp::Emit {
1048                output_type,
1049                fields,
1050                target_context,
1051            })
1052        }
1053        Rule::print_op => {
1054            let exprs = pair
1055                .into_inner()
1056                .filter(|p| p.as_rule() == Rule::expr_list)
1057                .flat_map(|p| p.into_inner())
1058                .map(parse_expr)
1059                .collect::<ParseResult<Vec<_>>>()?;
1060            Ok(StreamOp::Print(exprs))
1061        }
1062        Rule::collect_op => Ok(StreamOp::Collect),
1063        Rule::pattern_op => {
1064            let def_pair = pair.into_inner().expect_next("pattern definition")?;
1065            let mut inner = def_pair.into_inner();
1066            let name = inner.expect_next("pattern name")?.as_str().to_string();
1067            let body_pair = inner.expect_next("pattern body")?;
1068
1069            // pattern_body can be lambda_expr or pattern_or_expr
1070            let body_inner = body_pair.into_inner().expect_next("pattern expression")?;
1071            let matcher = match body_inner.as_rule() {
1072                Rule::lambda_expr => parse_lambda_expr(body_inner)?,
1073                Rule::pattern_or_expr => parse_pattern_expr_as_expr(body_inner)?,
1074                _ => parse_expr_inner(body_inner)?,
1075            };
1076            Ok(StreamOp::Pattern(PatternDef { name, matcher }))
1077        }
1078        Rule::partition_by_op => {
1079            let expr = parse_expr(pair.into_inner().expect_next("partition expression")?)?;
1080            Ok(StreamOp::PartitionBy(expr))
1081        }
1082        Rule::order_by_op => {
1083            let mut items = Vec::new();
1084            for p in pair.into_inner() {
1085                if p.as_rule() == Rule::order_list {
1086                    for oi in p.into_inner() {
1087                        items.push(parse_order_item(oi)?);
1088                    }
1089                }
1090            }
1091            Ok(StreamOp::OrderBy(items))
1092        }
1093        Rule::limit_op => {
1094            let expr = parse_expr(pair.into_inner().expect_next("limit expression")?)?;
1095            Ok(StreamOp::Limit(expr))
1096        }
1097        Rule::distinct_op => {
1098            let expr = pair.into_inner().next().map(parse_expr).transpose()?;
1099            Ok(StreamOp::Distinct(expr))
1100        }
1101        Rule::tap_op => {
1102            let args = pair
1103                .into_inner()
1104                .filter(|p| p.as_rule() == Rule::named_arg_list)
1105                .flat_map(|p| p.into_inner())
1106                .map(parse_named_arg)
1107                .collect::<ParseResult<Vec<_>>>()?;
1108            Ok(StreamOp::Tap(args))
1109        }
1110        Rule::log_op => {
1111            let args = pair
1112                .into_inner()
1113                .filter(|p| p.as_rule() == Rule::named_arg_list)
1114                .flat_map(|p| p.into_inner())
1115                .map(parse_named_arg)
1116                .collect::<ParseResult<Vec<_>>>()?;
1117            Ok(StreamOp::Log(args))
1118        }
1119        Rule::to_op => {
1120            let mut inner = pair.into_inner();
1121            let connector_name = inner.expect_next("connector name")?.as_str().to_string();
1122            let mut params = Vec::new();
1123            for p in inner {
1124                if p.as_rule() == Rule::connector_params {
1125                    params = parse_connector_params(p)?;
1126                }
1127            }
1128            Ok(StreamOp::To {
1129                connector_name,
1130                params,
1131            })
1132        }
1133        Rule::process_op => {
1134            let expr = parse_expr(pair.into_inner().expect_next("process expression")?)?;
1135            Ok(StreamOp::Process(expr))
1136        }
1137        Rule::on_error_op => {
1138            let expr = parse_expr(pair.into_inner().expect_next("on_error handler")?)?;
1139            Ok(StreamOp::OnError(expr))
1140        }
1141        Rule::on_op => {
1142            let expr = parse_expr(pair.into_inner().expect_next("on handler")?)?;
1143            Ok(StreamOp::On(expr))
1144        }
1145        Rule::not_op => {
1146            let mut inner = pair.into_inner();
1147            let event_type = inner.expect_next("event type")?.as_str().to_string();
1148            let filter = inner.next().map(parse_expr).transpose()?;
1149            Ok(StreamOp::Not(FollowedByClause {
1150                event_type,
1151                filter,
1152                alias: None,
1153                match_all: false,
1154            }))
1155        }
1156        Rule::fork_op => {
1157            let mut paths = Vec::new();
1158            for p in pair.into_inner() {
1159                if p.as_rule() == Rule::fork_path_list {
1160                    for fp in p.into_inner() {
1161                        paths.push(parse_fork_path(fp)?);
1162                    }
1163                }
1164            }
1165            Ok(StreamOp::Fork(paths))
1166        }
1167        Rule::any_op => {
1168            let count = pair
1169                .into_inner()
1170                .next()
1171                .map(|p| p.as_str().parse().unwrap_or(1));
1172            Ok(StreamOp::Any(count))
1173        }
1174        Rule::all_op => Ok(StreamOp::All),
1175        Rule::first_op => Ok(StreamOp::First),
1176        Rule::concurrent_op => {
1177            let args = pair
1178                .into_inner()
1179                .filter(|p| p.as_rule() == Rule::named_arg_list)
1180                .flat_map(|p| p.into_inner())
1181                .map(parse_named_arg)
1182                .collect::<ParseResult<Vec<_>>>()?;
1183            Ok(StreamOp::Concurrent(args))
1184        }
1185        Rule::watermark_op => {
1186            let args = pair
1187                .into_inner()
1188                .filter(|p| p.as_rule() == Rule::named_arg_list)
1189                .flat_map(|p| p.into_inner())
1190                .map(parse_named_arg)
1191                .collect::<ParseResult<Vec<_>>>()?;
1192            Ok(StreamOp::Watermark(args))
1193        }
1194        Rule::allowed_lateness_op => {
1195            let expr = parse_expr(pair.into_inner().expect_next("allowed lateness duration")?)?;
1196            Ok(StreamOp::AllowedLateness(expr))
1197        }
1198        Rule::trend_aggregate_op => {
1199            let mut items = Vec::new();
1200            for p in pair.into_inner() {
1201                if p.as_rule() == Rule::trend_agg_list {
1202                    for item_pair in p.into_inner() {
1203                        if item_pair.as_rule() == Rule::trend_agg_item {
1204                            let mut inner = item_pair.into_inner();
1205                            let alias = inner.expect_next("trend agg alias")?.as_str().to_string();
1206                            let func_pair = inner.expect_next("trend agg function")?;
1207                            let mut func_inner = func_pair.into_inner();
1208                            let func_name = func_inner
1209                                .expect_next("function name")?
1210                                .as_str()
1211                                .to_string();
1212                            let arg = func_inner.next().map(parse_expr).transpose()?;
1213                            items.push(TrendAggItem {
1214                                alias,
1215                                func: func_name,
1216                                arg,
1217                            });
1218                        }
1219                    }
1220                }
1221            }
1222            Ok(StreamOp::TrendAggregate(items))
1223        }
1224        Rule::forecast_op => {
1225            let mut confidence = None;
1226            let mut horizon = None;
1227            let mut warmup = None;
1228            let mut max_depth = None;
1229            let mut hawkes = None;
1230            let mut conformal = None;
1231            let mut mode = None;
1232            for p in pair.into_inner() {
1233                if p.as_rule() == Rule::forecast_params {
1234                    for param_pair in p.into_inner() {
1235                        if param_pair.as_rule() == Rule::forecast_param {
1236                            let mut inner = param_pair.into_inner();
1237                            let name = inner.expect_next("forecast param name")?.as_str();
1238                            let value_pair = inner.expect_next("forecast param value")?;
1239                            let expr = parse_expr(value_pair)?;
1240                            match name {
1241                                "confidence" => confidence = Some(expr),
1242                                "horizon" => horizon = Some(expr),
1243                                "warmup" => warmup = Some(expr),
1244                                "max_depth" => max_depth = Some(expr),
1245                                "hawkes" => hawkes = Some(expr),
1246                                "conformal" => conformal = Some(expr),
1247                                "mode" => mode = Some(expr),
1248                                _ => {}
1249                            }
1250                        }
1251                    }
1252                }
1253            }
1254            Ok(StreamOp::Forecast(ForecastSpec {
1255                confidence,
1256                horizon,
1257                warmup,
1258                max_depth,
1259                hawkes,
1260                conformal,
1261                mode,
1262            }))
1263        }
1264        Rule::enrich_op => {
1265            let mut inner = pair.into_inner();
1266            let connector_name = inner.expect_next("connector name")?.as_str().to_string();
1267            let mut key_expr = None;
1268            let mut fields = Vec::new();
1269            let mut cache_ttl = None;
1270            let mut timeout = None;
1271            let mut fallback = None;
1272            for p in inner {
1273                if p.as_rule() == Rule::enrich_params {
1274                    for param_pair in p.into_inner() {
1275                        if param_pair.as_rule() == Rule::enrich_param {
1276                            let param_inner =
1277                                param_pair.into_inner().expect_next("enrich param")?;
1278                            match param_inner.as_rule() {
1279                                Rule::enrich_key_param => {
1280                                    let expr_pair =
1281                                        param_inner.into_inner().expect_next("key expression")?;
1282                                    key_expr = Some(parse_expr(expr_pair)?);
1283                                }
1284                                Rule::enrich_fields_param => {
1285                                    for field in param_inner.into_inner() {
1286                                        if field.as_rule() == Rule::identifier {
1287                                            fields.push(field.as_str().to_string());
1288                                        }
1289                                    }
1290                                }
1291                                Rule::enrich_cache_ttl_param => {
1292                                    let expr_pair = param_inner
1293                                        .into_inner()
1294                                        .expect_next("cache_ttl expression")?;
1295                                    cache_ttl = Some(parse_expr(expr_pair)?);
1296                                }
1297                                Rule::enrich_timeout_param => {
1298                                    let expr_pair = param_inner
1299                                        .into_inner()
1300                                        .expect_next("timeout expression")?;
1301                                    timeout = Some(parse_expr(expr_pair)?);
1302                                }
1303                                Rule::enrich_fallback_param => {
1304                                    let literal_pair =
1305                                        param_inner.into_inner().expect_next("fallback literal")?;
1306                                    fallback = Some(parse_expr(literal_pair)?);
1307                                }
1308                                _ => {}
1309                            }
1310                        }
1311                    }
1312                }
1313            }
1314            let key = key_expr.ok_or_else(|| ParseError::Located {
1315                line: 0,
1316                column: 0,
1317                position: 0,
1318                message: ".enrich() requires a key: parameter".to_string(),
1319                hint: Some("add key: <expression> to .enrich()".to_string()),
1320            })?;
1321            Ok(StreamOp::Enrich(EnrichSpec {
1322                connector_name,
1323                key_expr: Box::new(key),
1324                fields,
1325                cache_ttl,
1326                timeout,
1327                fallback,
1328            }))
1329        }
1330        Rule::score_op => {
1331            let mut model_path = String::new();
1332            let mut inputs = Vec::new();
1333            let mut outputs = Vec::new();
1334            let mut gpu = false;
1335            let mut batch_size: usize = 1;
1336            let mut device_id: i32 = 0;
1337            for p in pair.into_inner() {
1338                if p.as_rule() == Rule::score_params {
1339                    for param_pair in p.into_inner() {
1340                        if param_pair.as_rule() == Rule::score_param {
1341                            let mut inner = param_pair.into_inner();
1342                            let name = inner.expect_next("score param name")?.as_str();
1343                            let value_pair = inner.expect_next("score param value")?;
1344                            match name {
1345                                "model" => {
1346                                    let raw = value_pair.as_str();
1347                                    model_path = raw.trim_matches('"').to_string();
1348                                }
1349                                "inputs" => {
1350                                    if value_pair.as_rule() == Rule::score_field_list {
1351                                        for field in value_pair.into_inner() {
1352                                            if field.as_rule() == Rule::identifier {
1353                                                inputs.push(field.as_str().to_string());
1354                                            }
1355                                        }
1356                                    }
1357                                }
1358                                "outputs" => {
1359                                    if value_pair.as_rule() == Rule::score_field_list {
1360                                        for field in value_pair.into_inner() {
1361                                            if field.as_rule() == Rule::identifier {
1362                                                outputs.push(field.as_str().to_string());
1363                                            }
1364                                        }
1365                                    }
1366                                }
1367                                "gpu" => {
1368                                    gpu = value_pair.as_str() == "true";
1369                                }
1370                                "batch_size" => {
1371                                    batch_size = value_pair.as_str().parse().unwrap_or(1);
1372                                }
1373                                "device" | "device_id" => {
1374                                    device_id = value_pair.as_str().parse().unwrap_or(0);
1375                                }
1376                                _ => {}
1377                            }
1378                        }
1379                    }
1380                }
1381            }
1382            Ok(StreamOp::Score(ScoreSpec {
1383                model_path,
1384                inputs,
1385                outputs,
1386                gpu,
1387                batch_size,
1388                device_id,
1389            }))
1390        }
1391        _ => Err(ParseError::UnexpectedToken {
1392            position: 0,
1393            expected: "stream operation".to_string(),
1394            found: format!("{:?}", pair.as_rule()),
1395        }),
1396    }
1397}
1398
1399fn parse_order_item(pair: pest::iterators::Pair<Rule>) -> ParseResult<OrderItem> {
1400    let mut inner = pair.into_inner();
1401    let expr = parse_expr(inner.expect_next("order expression")?)?;
1402    let desc = inner.next().is_some_and(|p| p.as_str() == "desc");
1403    Ok(OrderItem {
1404        expr,
1405        descending: desc,
1406    })
1407}
1408
1409fn parse_fork_path(pair: pest::iterators::Pair<Rule>) -> ParseResult<ForkPath> {
1410    let mut inner = pair.into_inner();
1411    let name = inner.expect_next("fork path name")?.as_str().to_string();
1412    let mut ops = Vec::new();
1413    for p in inner {
1414        if p.as_rule() == Rule::stream_op {
1415            ops.push(parse_stream_op(p)?);
1416        }
1417    }
1418    Ok(ForkPath { name, ops })
1419}
1420
1421fn parse_followed_by_op(pair: pest::iterators::Pair<Rule>) -> ParseResult<StreamOp> {
1422    let mut inner = pair.into_inner();
1423    let mut match_all = false;
1424
1425    let first = inner.expect_next("event type or match_all")?;
1426    let event_type = if first.as_rule() == Rule::match_all_keyword {
1427        match_all = true;
1428        inner.expect_next("event type")?.as_str().to_string()
1429    } else {
1430        first.as_str().to_string()
1431    };
1432
1433    let mut filter = None;
1434    let mut alias = None;
1435
1436    for p in inner {
1437        match p.as_rule() {
1438            Rule::or_expr => filter = Some(parse_or_expr(p)?),
1439            Rule::filter_expr => filter = Some(parse_filter_expr(p)?),
1440            Rule::identifier => alias = Some(p.as_str().to_string()),
1441            _ => {}
1442        }
1443    }
1444
1445    Ok(StreamOp::FollowedBy(FollowedByClause {
1446        event_type,
1447        filter,
1448        alias,
1449        match_all,
1450    }))
1451}
1452
1453fn parse_select_item(pair: pest::iterators::Pair<Rule>) -> ParseResult<SelectItem> {
1454    let mut inner = pair.into_inner();
1455    let first = inner.expect_next("select field or alias")?;
1456
1457    if let Some(second) = inner.next() {
1458        Ok(SelectItem::Alias(
1459            first.as_str().to_string(),
1460            parse_expr(second)?,
1461        ))
1462    } else {
1463        Ok(SelectItem::Field(first.as_str().to_string()))
1464    }
1465}
1466
1467fn parse_window_args(pair: pest::iterators::Pair<Rule>) -> ParseResult<WindowArgs> {
1468    let raw = pair.as_str().trim();
1469    let is_session = raw.starts_with("session");
1470
1471    let mut inner = pair.into_inner();
1472
1473    if is_session {
1474        // session: <gap_expr>
1475        let gap_expr = parse_expr(inner.expect_next("session gap duration")?)?;
1476        return Ok(WindowArgs {
1477            duration: gap_expr.clone(),
1478            sliding: None,
1479            policy: None,
1480            session_gap: Some(gap_expr),
1481        });
1482    }
1483
1484    let duration = parse_expr(inner.expect_next("window duration")?)?;
1485
1486    let mut sliding = None;
1487    let mut policy = None;
1488
1489    for p in inner {
1490        if p.as_rule() == Rule::expr {
1491            // Need to determine if it's sliding or policy based on context
1492            if sliding.is_none() {
1493                sliding = Some(parse_expr(p)?);
1494            } else {
1495                policy = Some(parse_expr(p)?);
1496            }
1497        }
1498    }
1499
1500    Ok(WindowArgs {
1501        duration,
1502        sliding,
1503        policy,
1504        session_gap: None,
1505    })
1506}
1507
1508fn parse_agg_item(pair: pest::iterators::Pair<Rule>) -> ParseResult<AggItem> {
1509    let mut inner = pair.into_inner();
1510    let alias = inner.expect_next("aggregate alias")?.as_str().to_string();
1511    let expr = parse_expr(inner.expect_next("aggregate expression")?)?;
1512    Ok(AggItem { alias, expr })
1513}
1514
1515fn parse_named_arg(pair: pest::iterators::Pair<Rule>) -> ParseResult<NamedArg> {
1516    let mut inner = pair.into_inner();
1517    let name = inner.expect_next("argument name")?.as_str().to_string();
1518    let value = parse_expr(inner.expect_next("argument value")?)?;
1519    Ok(NamedArg { name, value })
1520}
1521
1522fn parse_event_decl(pair: pest::iterators::Pair<Rule>) -> ParseResult<Stmt> {
1523    let mut inner = pair.into_inner();
1524    let name = inner.expect_next("event name")?.as_str().to_string();
1525
1526    let mut extends = None;
1527    let mut fields = Vec::new();
1528
1529    for p in inner {
1530        match p.as_rule() {
1531            Rule::identifier => extends = Some(p.as_str().to_string()),
1532            Rule::field => fields.push(parse_field(p)?),
1533            _ => {}
1534        }
1535    }
1536
1537    Ok(Stmt::EventDecl {
1538        name,
1539        extends,
1540        fields,
1541    })
1542}
1543
1544fn parse_field(pair: pest::iterators::Pair<Rule>) -> ParseResult<Field> {
1545    let mut inner = pair.into_inner();
1546    let name = inner.expect_next("field name")?.as_str().to_string();
1547    let ty = parse_type(inner.expect_next("field type")?)?;
1548    let optional = inner.next().is_some();
1549    Ok(Field { name, ty, optional })
1550}
1551
1552fn parse_type_decl(pair: pest::iterators::Pair<Rule>) -> ParseResult<Stmt> {
1553    let mut inner = pair.into_inner();
1554    let name = inner.expect_next("type name")?.as_str().to_string();
1555    let ty = parse_type(inner.expect_next("type definition")?)?;
1556    Ok(Stmt::TypeDecl { name, ty })
1557}
1558
1559fn parse_type(pair: pest::iterators::Pair<Rule>) -> ParseResult<Type> {
1560    let cloned = pair.clone();
1561    let inner = pair.into_inner().next().unwrap_or(cloned);
1562
1563    match inner.as_rule() {
1564        Rule::primitive_type => match inner.as_str() {
1565            "int" => Ok(Type::Int),
1566            "float" => Ok(Type::Float),
1567            "bool" => Ok(Type::Bool),
1568            "str" => Ok(Type::Str),
1569            "timestamp" => Ok(Type::Timestamp),
1570            "duration" => Ok(Type::Duration),
1571            _ => Ok(Type::Named(inner.as_str().to_string())),
1572        },
1573        Rule::array_type => {
1574            let inner_type = parse_type(inner.into_inner().expect_next("array element type")?)?;
1575            Ok(Type::Array(Box::new(inner_type)))
1576        }
1577        Rule::map_type => {
1578            let mut inner_pairs = inner.into_inner();
1579            let key_type = parse_type(inner_pairs.expect_next("map key type")?)?;
1580            let val_type = parse_type(inner_pairs.expect_next("map value type")?)?;
1581            Ok(Type::Map(Box::new(key_type), Box::new(val_type)))
1582        }
1583        Rule::tuple_type => {
1584            let types: Vec<Type> = inner
1585                .into_inner()
1586                .map(parse_type)
1587                .collect::<ParseResult<Vec<_>>>()?;
1588            Ok(Type::Tuple(types))
1589        }
1590        Rule::stream_type => {
1591            let inner_type = parse_type(inner.into_inner().expect_next("stream element type")?)?;
1592            Ok(Type::Stream(Box::new(inner_type)))
1593        }
1594        Rule::optional_type => {
1595            let inner_type = parse_type(inner.into_inner().expect_next("optional inner type")?)?;
1596            Ok(Type::Optional(Box::new(inner_type)))
1597        }
1598        Rule::named_type | Rule::identifier => Ok(Type::Named(inner.as_str().to_string())),
1599        _ => Ok(Type::Named(inner.as_str().to_string())),
1600    }
1601}
1602
1603fn parse_var_decl(pair: pest::iterators::Pair<Rule>) -> ParseResult<Stmt> {
1604    let mut inner = pair.into_inner();
1605    let keyword = inner.expect_next("var_keyword")?.as_str();
1606    let mutable = keyword == "var";
1607    let name = inner.expect_next("variable name")?.as_str().to_string();
1608
1609    let mut ty = None;
1610    let mut value = Expr::Null;
1611
1612    for p in inner {
1613        match p.as_rule() {
1614            Rule::type_annotation => ty = Some(parse_type(p.into_inner().expect_next("type")?)?),
1615            _ => value = parse_expr(p)?,
1616        }
1617    }
1618
1619    Ok(Stmt::VarDecl {
1620        mutable,
1621        name,
1622        ty,
1623        value,
1624    })
1625}
1626
1627fn parse_const_decl(pair: pest::iterators::Pair<Rule>) -> ParseResult<Stmt> {
1628    let mut inner = pair.into_inner();
1629    let name = inner.expect_next("constant name")?.as_str().to_string();
1630
1631    let mut ty = None;
1632    let mut value = Expr::Null;
1633
1634    for p in inner {
1635        match p.as_rule() {
1636            Rule::type_annotation => ty = Some(parse_type(p.into_inner().expect_next("type")?)?),
1637            _ => value = parse_expr(p)?,
1638        }
1639    }
1640
1641    Ok(Stmt::ConstDecl { name, ty, value })
1642}
1643
1644fn parse_fn_decl(pair: pest::iterators::Pair<Rule>) -> ParseResult<Stmt> {
1645    let mut inner = pair.into_inner();
1646    let name = inner.expect_next("function name")?.as_str().to_string();
1647
1648    let mut params = Vec::new();
1649    let mut ret = None;
1650    let mut body = Vec::new();
1651
1652    for p in inner {
1653        match p.as_rule() {
1654            Rule::param_list => {
1655                for param in p.into_inner() {
1656                    params.push(parse_param(param)?);
1657                }
1658            }
1659            Rule::type_expr => ret = Some(parse_type(p)?),
1660            Rule::block => body = parse_block(p)?,
1661            Rule::statement => body.push(parse_statement(p)?),
1662            _ => {}
1663        }
1664    }
1665
1666    Ok(Stmt::FnDecl {
1667        name,
1668        params,
1669        ret,
1670        body,
1671    })
1672}
1673
1674fn parse_block(pair: pest::iterators::Pair<Rule>) -> ParseResult<Vec<Spanned<Stmt>>> {
1675    let mut statements = Vec::new();
1676    for p in pair.into_inner() {
1677        if p.as_rule() == Rule::statement {
1678            statements.push(parse_statement(p)?);
1679        }
1680    }
1681    Ok(statements)
1682}
1683
1684fn parse_param(pair: pest::iterators::Pair<Rule>) -> ParseResult<Param> {
1685    let mut inner = pair.into_inner();
1686    let name = inner.expect_next("parameter name")?.as_str().to_string();
1687    let ty = parse_type(inner.expect_next("parameter type")?)?;
1688    Ok(Param { name, ty })
1689}
1690
1691fn parse_config_block(pair: pest::iterators::Pair<Rule>) -> ParseResult<Stmt> {
1692    let mut inner = pair.into_inner();
1693    let first = inner.expect_next("config name or item")?;
1694
1695    // Check if first token is identifier (new syntax) or config_item (old syntax)
1696    let (name, items_start) = if first.as_rule() == Rule::identifier {
1697        (first.as_str().to_string(), None)
1698    } else {
1699        // Old syntax: config: with indentation - use "default" as name
1700        ("default".to_string(), Some(first))
1701    };
1702
1703    let mut items = Vec::new();
1704
1705    // If we have a config_item from old syntax, parse it first
1706    if let Some(first_item) = items_start {
1707        if first_item.as_rule() == Rule::config_item {
1708            items.push(parse_config_item(first_item)?);
1709        }
1710    }
1711
1712    for p in inner {
1713        if p.as_rule() == Rule::config_item {
1714            items.push(parse_config_item(p)?);
1715        }
1716    }
1717    Ok(Stmt::Config { name, items })
1718}
1719
1720fn parse_config_item(pair: pest::iterators::Pair<Rule>) -> ParseResult<ConfigItem> {
1721    let mut inner = pair.into_inner();
1722    let key = inner.expect_next("config key")?.as_str().to_string();
1723    let value = parse_config_value(inner.expect_next("config value")?)?;
1724    Ok(ConfigItem::Value(key, value))
1725}
1726
1727fn parse_config_value(pair: pest::iterators::Pair<Rule>) -> ParseResult<ConfigValue> {
1728    let cloned = pair.clone();
1729    let inner = pair.into_inner().next().unwrap_or(cloned);
1730
1731    match inner.as_rule() {
1732        Rule::config_concat => {
1733            let parts: Vec<ConfigValue> = inner
1734                .into_inner()
1735                .map(|atom| {
1736                    // Each atom is a config_atom containing either string or identifier
1737                    let inner_atom = atom.into_inner().next().expect("config_atom child");
1738                    match inner_atom.as_rule() {
1739                        Rule::string => {
1740                            let s = inner_atom.as_str();
1741                            Ok(ConfigValue::Str(s[1..s.len() - 1].to_string()))
1742                        }
1743                        Rule::identifier => Ok(ConfigValue::Ident(inner_atom.as_str().to_string())),
1744                        _ => Ok(ConfigValue::Ident(inner_atom.as_str().to_string())),
1745                    }
1746                })
1747                .collect::<ParseResult<Vec<_>>>()?;
1748            Ok(ConfigValue::Concat(parts))
1749        }
1750        Rule::config_array => {
1751            let values: Vec<ConfigValue> = inner
1752                .into_inner()
1753                .map(parse_config_value)
1754                .collect::<ParseResult<Vec<_>>>()?;
1755            Ok(ConfigValue::Array(values))
1756        }
1757        Rule::integer => Ok(ConfigValue::Int(inner.as_str().parse().unwrap_or(0))),
1758        Rule::float => Ok(ConfigValue::Float(inner.as_str().parse().unwrap_or(0.0))),
1759        Rule::string => {
1760            let s = inner.as_str();
1761            Ok(ConfigValue::Str(s[1..s.len() - 1].to_string()))
1762        }
1763        Rule::duration => Ok(ConfigValue::Duration(
1764            parse_duration(inner.as_str()).map_err(ParseError::InvalidDuration)?,
1765        )),
1766        Rule::boolean => Ok(ConfigValue::Bool(inner.as_str() == "true")),
1767        Rule::identifier => Ok(ConfigValue::Ident(inner.as_str().to_string())),
1768        _ => Ok(ConfigValue::Ident(inner.as_str().to_string())),
1769    }
1770}
1771
1772fn parse_import_stmt(pair: pest::iterators::Pair<Rule>) -> ParseResult<Stmt> {
1773    let mut inner = pair.into_inner();
1774    let path_pair = inner.expect_next("import path")?;
1775    let path = path_pair.as_str();
1776    let path = path[1..path.len() - 1].to_string();
1777    let alias = inner.next().map(|p| p.as_str().to_string());
1778    Ok(Stmt::Import { path, alias })
1779}
1780
1781fn parse_if_stmt(pair: pest::iterators::Pair<Rule>) -> ParseResult<Stmt> {
1782    let mut inner = pair.into_inner();
1783    let cond = parse_expr(inner.expect_next("if condition")?)?;
1784
1785    let mut then_branch = Vec::new();
1786    let mut elif_branches = Vec::new();
1787    let mut else_branch = None;
1788
1789    for p in inner {
1790        match p.as_rule() {
1791            Rule::block => then_branch = parse_block(p)?,
1792            Rule::statement => then_branch.push(parse_statement(p)?),
1793            Rule::elif_clause => {
1794                let mut elif_inner = p.into_inner();
1795                let elif_cond = parse_expr(elif_inner.expect_next("elif condition")?)?;
1796                let mut elif_body = Vec::new();
1797                for ep in elif_inner {
1798                    match ep.as_rule() {
1799                        Rule::block => elif_body = parse_block(ep)?,
1800                        Rule::statement => elif_body.push(parse_statement(ep)?),
1801                        _ => {}
1802                    }
1803                }
1804                elif_branches.push((elif_cond, elif_body));
1805            }
1806            Rule::else_clause => {
1807                let mut else_body = Vec::new();
1808                for ep in p.into_inner() {
1809                    match ep.as_rule() {
1810                        Rule::block => else_body = parse_block(ep)?,
1811                        Rule::statement => else_body.push(parse_statement(ep)?),
1812                        _ => {}
1813                    }
1814                }
1815                else_branch = Some(else_body);
1816            }
1817            _ => {}
1818        }
1819    }
1820
1821    Ok(Stmt::If {
1822        cond,
1823        then_branch,
1824        elif_branches,
1825        else_branch,
1826    })
1827}
1828
1829fn parse_for_stmt(pair: pest::iterators::Pair<Rule>) -> ParseResult<Stmt> {
1830    let mut inner = pair.into_inner();
1831    let var = inner.expect_next("loop variable")?.as_str().to_string();
1832    let iter = parse_expr(inner.expect_next("iterable expression")?)?;
1833    let mut body = Vec::new();
1834    for p in inner {
1835        match p.as_rule() {
1836            Rule::block => body = parse_block(p)?,
1837            Rule::statement => body.push(parse_statement(p)?),
1838            _ => {}
1839        }
1840    }
1841    Ok(Stmt::For { var, iter, body })
1842}
1843
1844fn parse_while_stmt(pair: pest::iterators::Pair<Rule>) -> ParseResult<Stmt> {
1845    let mut inner = pair.into_inner();
1846    let cond = parse_expr(inner.expect_next("while condition")?)?;
1847    let mut body = Vec::new();
1848    for p in inner {
1849        match p.as_rule() {
1850            Rule::block => body = parse_block(p)?,
1851            Rule::statement => body.push(parse_statement(p)?),
1852            _ => {}
1853        }
1854    }
1855    Ok(Stmt::While { cond, body })
1856}
1857
1858fn parse_return_stmt(pair: pest::iterators::Pair<Rule>) -> ParseResult<Stmt> {
1859    let expr = pair.into_inner().next().map(parse_expr).transpose()?;
1860    Ok(Stmt::Return(expr))
1861}
1862
1863fn parse_emit_stmt(pair: pest::iterators::Pair<Rule>) -> ParseResult<Stmt> {
1864    let mut inner = pair.into_inner();
1865    let event_type = inner.expect_next("event type name")?.as_str().to_string();
1866    let mut fields = Vec::new();
1867    for p in inner {
1868        if p.as_rule() == Rule::named_arg_list {
1869            for arg in p.into_inner() {
1870                fields.push(parse_named_arg(arg)?);
1871            }
1872        }
1873    }
1874    Ok(Stmt::Emit { event_type, fields })
1875}
1876
1877// ============================================================================
1878// Expression Parsing
1879// ============================================================================
1880
1881fn parse_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
1882    let inner = pair.into_inner().next();
1883
1884    match inner {
1885        Some(p) => parse_expr_inner(p),
1886        None => Ok(Expr::Null),
1887    }
1888}
1889
1890fn parse_expr_inner(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
1891    match pair.as_rule() {
1892        Rule::expr => parse_expr(pair),
1893        Rule::lambda_expr => parse_lambda_expr(pair),
1894        Rule::range_expr => parse_range_expr(pair),
1895        Rule::or_expr => parse_or_expr(pair),
1896        Rule::and_expr => parse_and_expr(pair),
1897        Rule::not_expr => parse_not_expr(pair),
1898        Rule::comparison_expr => parse_comparison_expr(pair),
1899        Rule::bitwise_or_expr => parse_bitwise_or_expr(pair),
1900        Rule::bitwise_xor_expr => parse_bitwise_xor_expr(pair),
1901        Rule::bitwise_and_expr => parse_bitwise_and_expr(pair),
1902        Rule::shift_expr => parse_shift_expr(pair),
1903        Rule::additive_expr => parse_additive_expr(pair),
1904        Rule::multiplicative_expr => parse_multiplicative_expr(pair),
1905        Rule::power_expr => parse_power_expr(pair),
1906        Rule::unary_expr => parse_unary_expr(pair),
1907        Rule::postfix_expr => parse_postfix_expr(pair),
1908        Rule::primary_expr => parse_primary_expr(pair),
1909        Rule::literal => parse_literal(pair),
1910        Rule::identifier => Ok(Expr::Ident(pair.as_str().to_string())),
1911        Rule::if_expr => parse_if_expr(pair),
1912        _ => Ok(Expr::Ident(pair.as_str().to_string())),
1913    }
1914}
1915
1916fn parse_lambda_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
1917    let mut inner = pair.into_inner();
1918    let mut params = Vec::new();
1919
1920    // Parse parameters
1921    let first = inner.expect_next("lambda parameters")?;
1922    match first.as_rule() {
1923        Rule::identifier_list => {
1924            for p in first.into_inner() {
1925                params.push(p.as_str().to_string());
1926            }
1927        }
1928        Rule::identifier => {
1929            params.push(first.as_str().to_string());
1930        }
1931        _ => {}
1932    }
1933
1934    // Parse body - could be expression or block
1935    let body_pair = inner.expect_next("lambda body")?;
1936    let body = match body_pair.as_rule() {
1937        Rule::lambda_block => parse_lambda_block(body_pair)?,
1938        _ => parse_expr_inner(body_pair)?,
1939    };
1940
1941    Ok(Expr::Lambda {
1942        params,
1943        body: Box::new(body),
1944    })
1945}
1946
1947fn parse_lambda_block(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
1948    let mut stmts = Vec::new();
1949    let mut final_expr = None;
1950
1951    for p in pair.into_inner() {
1952        match p.as_rule() {
1953            Rule::statement => {
1954                // Check if it's a var_decl to extract for Block
1955                let stmt = parse_statement(p)?;
1956                match &stmt.node {
1957                    Stmt::VarDecl {
1958                        mutable,
1959                        name,
1960                        ty,
1961                        value,
1962                    } => {
1963                        stmts.push((name.clone(), ty.clone(), value.clone(), *mutable));
1964                    }
1965                    Stmt::Expr(e) => {
1966                        // Last expression becomes result
1967                        final_expr = Some(e.clone());
1968                    }
1969                    _ => {
1970                        // Other statements - treat as expression if possible
1971                    }
1972                }
1973            }
1974            _ => {
1975                // Expression at end of block
1976                final_expr = Some(parse_expr_inner(p)?);
1977            }
1978        }
1979    }
1980
1981    // If we have variable declarations, wrap in a Block expression
1982    if stmts.is_empty() {
1983        Ok(final_expr.unwrap_or(Expr::Null))
1984    } else {
1985        Ok(Expr::Block {
1986            stmts,
1987            result: Box::new(final_expr.unwrap_or(Expr::Null)),
1988        })
1989    }
1990}
1991
1992fn parse_pattern_expr_as_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
1993    // Convert pattern_or_expr to an Expr representation
1994    // pattern_or_expr = pattern_and_expr ~ ("or" ~ pattern_and_expr)*
1995    let mut inner = pair.into_inner();
1996    let mut left = parse_pattern_and_as_expr(inner.expect_next("pattern expression")?)?;
1997
1998    for right_pair in inner {
1999        let right = parse_pattern_and_as_expr(right_pair)?;
2000        left = Expr::Binary {
2001            op: BinOp::Or,
2002            left: Box::new(left),
2003            right: Box::new(right),
2004        };
2005    }
2006    Ok(left)
2007}
2008
2009fn parse_pattern_and_as_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2010    let mut inner = pair.into_inner();
2011    let mut left = parse_pattern_xor_as_expr(inner.expect_next("and expression")?)?;
2012
2013    for right_pair in inner {
2014        let right = parse_pattern_xor_as_expr(right_pair)?;
2015        left = Expr::Binary {
2016            op: BinOp::And,
2017            left: Box::new(left),
2018            right: Box::new(right),
2019        };
2020    }
2021    Ok(left)
2022}
2023
2024fn parse_pattern_xor_as_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2025    let mut inner = pair.into_inner();
2026    let mut left = parse_pattern_unary_as_expr(inner.expect_next("xor expression")?)?;
2027
2028    for right_pair in inner {
2029        let right = parse_pattern_unary_as_expr(right_pair)?;
2030        left = Expr::Binary {
2031            op: BinOp::Xor,
2032            left: Box::new(left),
2033            right: Box::new(right),
2034        };
2035    }
2036    Ok(left)
2037}
2038
2039fn parse_pattern_unary_as_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2040    let mut inner = pair.into_inner();
2041    let first = inner.expect_next("unary expression or operand")?;
2042
2043    if first.as_str() == "not" {
2044        let expr = parse_pattern_primary_as_expr(inner.expect_next("pattern expression")?)?;
2045        Ok(Expr::Unary {
2046            op: UnaryOp::Not,
2047            expr: Box::new(expr),
2048        })
2049    } else {
2050        parse_pattern_primary_as_expr(first)
2051    }
2052}
2053
2054fn parse_pattern_primary_as_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2055    let inner = pair
2056        .into_inner()
2057        .expect_next("pattern primary expression")?;
2058
2059    match inner.as_rule() {
2060        Rule::pattern_or_expr => parse_pattern_expr_as_expr(inner),
2061        Rule::pattern_sequence => parse_pattern_sequence_as_expr(inner),
2062        _ => Ok(Expr::Ident(inner.as_str().to_string())),
2063    }
2064}
2065
2066fn parse_pattern_sequence_as_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2067    // pattern_sequence = identifier ~ ("->" ~ identifier)*
2068    // Convert to a chain of FollowedBy binary operations
2069    let mut inner = pair.into_inner();
2070    let mut left = Expr::Ident(inner.expect_next("sequence start")?.as_str().to_string());
2071
2072    for right_pair in inner {
2073        let right = Expr::Ident(right_pair.as_str().to_string());
2074        left = Expr::Binary {
2075            op: BinOp::FollowedBy,
2076            left: Box::new(left),
2077            right: Box::new(right),
2078        };
2079    }
2080    Ok(left)
2081}
2082
2083fn parse_filter_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2084    let inner = pair.into_inner().expect_next("filter expression")?;
2085    parse_filter_or_expr(inner)
2086}
2087
2088fn parse_filter_or_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2089    let mut inner = pair.into_inner();
2090    let mut left = parse_filter_and_expr(inner.expect_next("or expression operand")?)?;
2091
2092    for right_pair in inner {
2093        let right = parse_filter_and_expr(right_pair)?;
2094        left = Expr::Binary {
2095            op: BinOp::Or,
2096            left: Box::new(left),
2097            right: Box::new(right),
2098        };
2099    }
2100    Ok(left)
2101}
2102
2103fn parse_filter_and_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2104    let mut inner = pair.into_inner();
2105    let mut left = parse_filter_not_expr(inner.expect_next("and expression operand")?)?;
2106
2107    for right_pair in inner {
2108        let right = parse_filter_not_expr(right_pair)?;
2109        left = Expr::Binary {
2110            op: BinOp::And,
2111            left: Box::new(left),
2112            right: Box::new(right),
2113        };
2114    }
2115    Ok(left)
2116}
2117
2118fn parse_filter_not_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2119    let mut inner = pair.into_inner();
2120    let first = inner.expect_next("not or expression")?;
2121
2122    if first.as_str() == "not" {
2123        let expr = parse_filter_comparison_expr(inner.expect_next("expression after not")?)?;
2124        Ok(Expr::Unary {
2125            op: UnaryOp::Not,
2126            expr: Box::new(expr),
2127        })
2128    } else {
2129        parse_filter_comparison_expr(first)
2130    }
2131}
2132
2133fn parse_filter_comparison_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2134    let mut inner = pair.into_inner();
2135    let left = parse_filter_additive_expr(inner.expect_next("comparison left operand")?)?;
2136
2137    if let Some(op_pair) = inner.next() {
2138        let op = match op_pair.as_str() {
2139            "==" => BinOp::Eq,
2140            "!=" => BinOp::NotEq,
2141            "<" => BinOp::Lt,
2142            "<=" => BinOp::Le,
2143            ">" => BinOp::Gt,
2144            ">=" => BinOp::Ge,
2145            "in" => BinOp::In,
2146            "is" => BinOp::Is,
2147            s if s.contains("not") && s.contains("in") => BinOp::NotIn,
2148            _ => BinOp::Eq,
2149        };
2150        let right = parse_filter_additive_expr(inner.expect_next("comparison right operand")?)?;
2151        Ok(Expr::Binary {
2152            op,
2153            left: Box::new(left),
2154            right: Box::new(right),
2155        })
2156    } else {
2157        Ok(left)
2158    }
2159}
2160
2161fn parse_filter_additive_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2162    let mut inner = pair.into_inner();
2163    let mut left =
2164        parse_filter_multiplicative_expr(inner.expect_next("additive expression operand")?)?;
2165
2166    while let Some(op_pair) = inner.next() {
2167        let op = if op_pair.as_str() == "-" {
2168            BinOp::Sub
2169        } else {
2170            BinOp::Add
2171        };
2172        if let Some(right_pair) = inner.next() {
2173            let right = parse_filter_multiplicative_expr(right_pair)?;
2174            left = Expr::Binary {
2175                op,
2176                left: Box::new(left),
2177                right: Box::new(right),
2178            };
2179        }
2180    }
2181    Ok(left)
2182}
2183
2184fn parse_filter_multiplicative_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2185    let mut inner = pair.into_inner();
2186    let mut left =
2187        parse_filter_unary_expr(inner.expect_next("multiplicative expression operand")?)?;
2188
2189    while let Some(op_pair) = inner.next() {
2190        let op = match op_pair.as_str() {
2191            "*" => BinOp::Mul,
2192            "/" => BinOp::Div,
2193            "%" => BinOp::Mod,
2194            _ => BinOp::Mul,
2195        };
2196        if let Some(right_pair) = inner.next() {
2197            let right = parse_filter_unary_expr(right_pair)?;
2198            left = Expr::Binary {
2199                op,
2200                left: Box::new(left),
2201                right: Box::new(right),
2202            };
2203        }
2204    }
2205    Ok(left)
2206}
2207
2208fn parse_filter_unary_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2209    let mut inner = pair.into_inner();
2210    let first = inner.expect_next("unary operator or expression")?;
2211
2212    // Check if first is a unary operator or the expression itself
2213    if first.as_rule() == Rule::filter_unary_op {
2214        let op_str = first.as_str();
2215        let expr =
2216            parse_filter_postfix_expr(inner.expect_next("expression after unary operator")?)?;
2217        let op = match op_str {
2218            "-" => UnaryOp::Neg,
2219            "~" => UnaryOp::BitNot,
2220            _ => unreachable!("Grammar only allows - or ~"),
2221        };
2222        Ok(Expr::Unary {
2223            op,
2224            expr: Box::new(expr),
2225        })
2226    } else {
2227        parse_filter_postfix_expr(first)
2228    }
2229}
2230
2231fn parse_filter_postfix_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2232    let mut inner = pair.into_inner();
2233    let mut expr = parse_filter_primary_expr(inner.expect_next("postfix expression base")?)?;
2234
2235    for suffix in inner {
2236        expr = parse_filter_postfix_suffix(expr, suffix)?;
2237    }
2238    Ok(expr)
2239}
2240
2241fn parse_filter_postfix_suffix(expr: Expr, pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2242    let mut inner = pair.into_inner();
2243
2244    if let Some(first) = inner.next() {
2245        match first.as_rule() {
2246            Rule::identifier => {
2247                // Member access: .identifier
2248                Ok(Expr::Member {
2249                    expr: Box::new(expr),
2250                    member: first.as_str().to_string(),
2251                })
2252            }
2253            Rule::optional_member_access => {
2254                let member = first
2255                    .into_inner()
2256                    .expect_next("member name")?
2257                    .as_str()
2258                    .to_string();
2259                Ok(Expr::OptionalMember {
2260                    expr: Box::new(expr),
2261                    member,
2262                })
2263            }
2264            Rule::index_access => {
2265                let index = parse_expr(first.into_inner().expect_next("index expression")?)?;
2266                Ok(Expr::Index {
2267                    expr: Box::new(expr),
2268                    index: Box::new(index),
2269                })
2270            }
2271            Rule::call_args => {
2272                let args = first
2273                    .into_inner()
2274                    .filter(|p| p.as_rule() == Rule::arg_list)
2275                    .flat_map(|p| p.into_inner())
2276                    .map(parse_arg)
2277                    .collect::<ParseResult<Vec<_>>>()?;
2278                Ok(Expr::Call {
2279                    func: Box::new(expr),
2280                    args,
2281                })
2282            }
2283            _ => Ok(expr),
2284        }
2285    } else {
2286        Ok(expr)
2287    }
2288}
2289
2290fn parse_filter_primary_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2291    let inner = pair.into_inner().expect_next("filter primary expression")?;
2292
2293    match inner.as_rule() {
2294        Rule::literal => parse_literal(inner),
2295        Rule::identifier => Ok(Expr::Ident(inner.as_str().to_string())),
2296        Rule::filter_expr => parse_filter_expr(inner),
2297        _ => Ok(Expr::Ident(inner.as_str().to_string())),
2298    }
2299}
2300
2301fn parse_range_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2302    let mut inner = pair.into_inner();
2303    let left = parse_expr_inner(inner.expect_next("range start")?)?;
2304
2305    if let Some(op_pair) = inner.next() {
2306        let inclusive = op_pair.as_str() == "..=";
2307        let right = parse_expr_inner(inner.expect_next("range end")?)?;
2308        Ok(Expr::Range {
2309            start: Box::new(left),
2310            end: Box::new(right),
2311            inclusive,
2312        })
2313    } else {
2314        Ok(left)
2315    }
2316}
2317
2318fn parse_or_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2319    let mut inner = pair.into_inner();
2320    let mut left = parse_expr_inner(inner.expect_next("or expression operand")?)?;
2321
2322    for right_pair in inner {
2323        let right = parse_expr_inner(right_pair)?;
2324        left = Expr::Binary {
2325            op: BinOp::Or,
2326            left: Box::new(left),
2327            right: Box::new(right),
2328        };
2329    }
2330
2331    Ok(left)
2332}
2333
2334fn parse_and_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2335    let mut inner = pair.into_inner();
2336    let mut left = parse_expr_inner(inner.expect_next("and expression operand")?)?;
2337
2338    for right_pair in inner {
2339        let right = parse_expr_inner(right_pair)?;
2340        left = Expr::Binary {
2341            op: BinOp::And,
2342            left: Box::new(left),
2343            right: Box::new(right),
2344        };
2345    }
2346
2347    Ok(left)
2348}
2349
2350fn parse_not_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2351    let mut inner = pair.into_inner();
2352    let first = inner.expect_next("not keyword or expression")?;
2353
2354    if first.as_str() == "not" {
2355        let expr = parse_expr_inner(inner.expect_next("expression after not")?)?;
2356        Ok(Expr::Unary {
2357            op: UnaryOp::Not,
2358            expr: Box::new(expr),
2359        })
2360    } else {
2361        parse_expr_inner(first)
2362    }
2363}
2364
2365fn parse_comparison_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2366    let mut inner = pair.into_inner();
2367    let left = parse_expr_inner(inner.expect_next("comparison left operand")?)?;
2368
2369    if let Some(op_pair) = inner.next() {
2370        let op_str = op_pair.as_str();
2371        let op = match op_str {
2372            "==" => BinOp::Eq,
2373            "!=" => BinOp::NotEq,
2374            "<" => BinOp::Lt,
2375            "<=" => BinOp::Le,
2376            ">" => BinOp::Gt,
2377            ">=" => BinOp::Ge,
2378            "in" => BinOp::In,
2379            "is" => BinOp::Is,
2380            s if s.contains("not") && s.contains("in") => BinOp::NotIn,
2381            _ => BinOp::Eq,
2382        };
2383        let right = parse_expr_inner(inner.expect_next("comparison right operand")?)?;
2384        Ok(Expr::Binary {
2385            op,
2386            left: Box::new(left),
2387            right: Box::new(right),
2388        })
2389    } else {
2390        Ok(left)
2391    }
2392}
2393
2394fn parse_bitwise_or_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2395    parse_binary_chain(pair, BinOp::BitOr)
2396}
2397
2398fn parse_bitwise_xor_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2399    parse_binary_chain(pair, BinOp::BitXor)
2400}
2401
2402fn parse_bitwise_and_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2403    parse_binary_chain(pair, BinOp::BitAnd)
2404}
2405
2406fn parse_shift_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2407    let mut inner = pair.into_inner();
2408    let mut left = parse_expr_inner(inner.expect_next("shift expression operand")?)?;
2409
2410    while let Some(op_or_expr) = inner.next() {
2411        let op = match op_or_expr.as_str() {
2412            "<<" => BinOp::Shl,
2413            ">>" => BinOp::Shr,
2414            _ => {
2415                let right = parse_expr_inner(op_or_expr)?;
2416                left = Expr::Binary {
2417                    op: BinOp::Shl,
2418                    left: Box::new(left),
2419                    right: Box::new(right),
2420                };
2421                continue;
2422            }
2423        };
2424        if let Some(right_pair) = inner.next() {
2425            let right = parse_expr_inner(right_pair)?;
2426            left = Expr::Binary {
2427                op,
2428                left: Box::new(left),
2429                right: Box::new(right),
2430            };
2431        }
2432    }
2433
2434    Ok(left)
2435}
2436
2437fn parse_additive_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2438    let mut inner = pair.into_inner();
2439    let mut left = parse_expr_inner(inner.expect_next("additive expression operand")?)?;
2440
2441    while let Some(op_pair) = inner.next() {
2442        let op_text = op_pair.as_str();
2443        let op = if op_text == "-" {
2444            BinOp::Sub
2445        } else {
2446            BinOp::Add
2447        };
2448
2449        if let Some(right_pair) = inner.next() {
2450            let right = parse_expr_inner(right_pair)?;
2451            left = Expr::Binary {
2452                op,
2453                left: Box::new(left),
2454                right: Box::new(right),
2455            };
2456        }
2457    }
2458
2459    Ok(left)
2460}
2461
2462fn parse_multiplicative_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2463    let mut inner = pair.into_inner();
2464    let mut left = parse_expr_inner(inner.expect_next("multiplicative expression operand")?)?;
2465
2466    while let Some(op_pair) = inner.next() {
2467        let op_text = op_pair.as_str();
2468        let op = match op_text {
2469            "*" => BinOp::Mul,
2470            "/" => BinOp::Div,
2471            "%" => BinOp::Mod,
2472            _ => BinOp::Mul,
2473        };
2474
2475        if let Some(right_pair) = inner.next() {
2476            let right = parse_expr_inner(right_pair)?;
2477            left = Expr::Binary {
2478                op,
2479                left: Box::new(left),
2480                right: Box::new(right),
2481            };
2482        }
2483    }
2484
2485    Ok(left)
2486}
2487
2488fn parse_power_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2489    let mut inner = pair.into_inner();
2490    let base = parse_expr_inner(inner.expect_next("power expression base")?)?;
2491
2492    if let Some(exp_pair) = inner.next() {
2493        let exp = parse_expr_inner(exp_pair)?;
2494        Ok(Expr::Binary {
2495            op: BinOp::Pow,
2496            left: Box::new(base),
2497            right: Box::new(exp),
2498        })
2499    } else {
2500        Ok(base)
2501    }
2502}
2503
2504fn parse_unary_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2505    let mut inner = pair.into_inner();
2506    let first = inner.expect_next("unary operator or expression")?;
2507
2508    // Check if first is a unary operator or the expression itself
2509    match first.as_rule() {
2510        Rule::unary_op => {
2511            let op_str = first.as_str();
2512            let expr = parse_expr_inner(inner.expect_next("expression after unary operator")?)?;
2513            let op = match op_str {
2514                "-" => UnaryOp::Neg,
2515                "~" => UnaryOp::BitNot,
2516                _ => unreachable!("Grammar only allows - or ~"),
2517            };
2518            Ok(Expr::Unary {
2519                op,
2520                expr: Box::new(expr),
2521            })
2522        }
2523        _ => parse_expr_inner(first),
2524    }
2525}
2526
2527fn parse_postfix_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2528    let mut inner = pair.into_inner();
2529    let mut expr = parse_expr_inner(inner.expect_next("postfix expression base")?)?;
2530
2531    for suffix in inner {
2532        expr = parse_postfix_suffix(expr, suffix)?;
2533    }
2534
2535    Ok(expr)
2536}
2537
2538fn parse_postfix_suffix(expr: Expr, pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2539    let inner = pair.into_inner().expect_next("postfix suffix")?;
2540
2541    match inner.as_rule() {
2542        Rule::member_access => {
2543            let member = inner
2544                .into_inner()
2545                .expect_next("member name")?
2546                .as_str()
2547                .to_string();
2548            Ok(Expr::Member {
2549                expr: Box::new(expr),
2550                member,
2551            })
2552        }
2553        Rule::optional_member_access => {
2554            let member = inner
2555                .into_inner()
2556                .expect_next("member name")?
2557                .as_str()
2558                .to_string();
2559            Ok(Expr::OptionalMember {
2560                expr: Box::new(expr),
2561                member,
2562            })
2563        }
2564        Rule::slice_access => {
2565            // Parse slice: [start:end], [:end], [start:], [:]
2566            let slice_range = inner.into_inner().expect_next("slice range")?;
2567            let slice_inner = slice_range.into_inner();
2568
2569            let mut start = None;
2570            let mut end = None;
2571
2572            for p in slice_inner {
2573                match p.as_rule() {
2574                    Rule::slice_start => {
2575                        start = Some(Box::new(parse_expr_inner(
2576                            p.into_inner().expect_next("slice start expression")?,
2577                        )?));
2578                    }
2579                    Rule::slice_end => {
2580                        end = Some(Box::new(parse_expr_inner(
2581                            p.into_inner().expect_next("slice end expression")?,
2582                        )?));
2583                    }
2584                    _ => {}
2585                }
2586            }
2587
2588            Ok(Expr::Slice {
2589                expr: Box::new(expr),
2590                start,
2591                end,
2592            })
2593        }
2594        Rule::index_access => {
2595            let index = parse_expr(inner.into_inner().expect_next("index expression")?)?;
2596            Ok(Expr::Index {
2597                expr: Box::new(expr),
2598                index: Box::new(index),
2599            })
2600        }
2601        Rule::call_args => {
2602            let mut args = Vec::new();
2603            for p in inner.into_inner() {
2604                if p.as_rule() == Rule::arg_list {
2605                    for arg in p.into_inner() {
2606                        args.push(parse_arg(arg)?);
2607                    }
2608                }
2609            }
2610            Ok(Expr::Call {
2611                func: Box::new(expr),
2612                args,
2613            })
2614        }
2615        _ => Ok(expr),
2616    }
2617}
2618
2619fn parse_arg(pair: pest::iterators::Pair<Rule>) -> ParseResult<Arg> {
2620    let mut inner = pair.into_inner();
2621    let first = inner.expect_next("argument")?;
2622
2623    if let Some(second) = inner.next() {
2624        Ok(Arg::Named(
2625            first.as_str().to_string(),
2626            parse_expr_inner(second)?,
2627        ))
2628    } else {
2629        Ok(Arg::Positional(parse_expr_inner(first)?))
2630    }
2631}
2632
2633fn parse_primary_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2634    let inner = pair.into_inner().expect_next("primary expression")?;
2635
2636    match inner.as_rule() {
2637        Rule::if_expr => parse_if_expr(inner),
2638        Rule::literal => parse_literal(inner),
2639        Rule::identifier => Ok(Expr::Ident(inner.as_str().to_string())),
2640        Rule::array_literal => parse_array_literal(inner),
2641        Rule::map_literal => parse_map_literal(inner),
2642        Rule::expr => parse_expr(inner),
2643        _ => Ok(Expr::Ident(inner.as_str().to_string())),
2644    }
2645}
2646
2647fn parse_if_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2648    let mut inner = pair.into_inner();
2649    let cond = parse_expr_inner(inner.expect_next("if condition")?)?;
2650    let then_branch = parse_expr_inner(inner.expect_next("then branch")?)?;
2651    let else_branch = parse_expr_inner(inner.expect_next("else branch")?)?;
2652
2653    Ok(Expr::If {
2654        cond: Box::new(cond),
2655        then_branch: Box::new(then_branch),
2656        else_branch: Box::new(else_branch),
2657    })
2658}
2659
2660fn parse_literal(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2661    let inner = pair.into_inner().expect_next("literal value")?;
2662
2663    match inner.as_rule() {
2664        Rule::integer => inner
2665            .as_str()
2666            .parse::<i64>()
2667            .map(Expr::Int)
2668            .map_err(|e| ParseError::InvalidNumber(format!("'{}': {}", inner.as_str(), e))),
2669        Rule::float => inner
2670            .as_str()
2671            .parse::<f64>()
2672            .map(Expr::Float)
2673            .map_err(|e| ParseError::InvalidNumber(format!("'{}': {}", inner.as_str(), e))),
2674        Rule::string => {
2675            let s = inner.as_str();
2676            Ok(Expr::Str(s[1..s.len() - 1].to_string()))
2677        }
2678        Rule::duration => Ok(Expr::Duration(
2679            parse_duration(inner.as_str()).map_err(ParseError::InvalidDuration)?,
2680        )),
2681        Rule::timestamp => Ok(Expr::Timestamp(parse_timestamp(inner.as_str()))),
2682        Rule::boolean => Ok(Expr::Bool(inner.as_str() == "true")),
2683        Rule::null => Ok(Expr::Null),
2684        _ => Ok(Expr::Null),
2685    }
2686}
2687
2688fn parse_array_literal(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2689    let mut items = Vec::new();
2690    for p in pair.into_inner() {
2691        if p.as_rule() == Rule::expr_list {
2692            for expr in p.into_inner() {
2693                items.push(parse_expr(expr)?);
2694            }
2695        }
2696    }
2697    Ok(Expr::Array(items))
2698}
2699
2700fn parse_map_literal(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2701    let mut entries = Vec::new();
2702    for p in pair.into_inner() {
2703        if p.as_rule() == Rule::map_entry_list {
2704            for entry in p.into_inner() {
2705                let mut inner = entry.into_inner();
2706                let key = inner.expect_next("map key")?.as_str().to_string();
2707                let key = if key.starts_with('"') {
2708                    key[1..key.len() - 1].to_string()
2709                } else {
2710                    key
2711                };
2712                let value = parse_expr(inner.expect_next("map value")?)?;
2713                entries.push((key, value));
2714            }
2715        }
2716    }
2717    Ok(Expr::Map(entries))
2718}
2719
2720fn parse_binary_chain(pair: pest::iterators::Pair<Rule>, op: BinOp) -> ParseResult<Expr> {
2721    let mut inner = pair.into_inner();
2722    let mut left = parse_expr_inner(inner.expect_next("binary chain operand")?)?;
2723
2724    for right_pair in inner {
2725        let right = parse_expr_inner(right_pair)?;
2726        left = Expr::Binary {
2727            op,
2728            left: Box::new(left),
2729            right: Box::new(right),
2730        };
2731    }
2732
2733    Ok(left)
2734}
2735
2736#[cfg(test)]
2737mod tests {
2738    use super::*;
2739
2740    #[test]
2741    fn test_parse_simple_stream() {
2742        let result = parse("stream output = input");
2743        assert!(result.is_ok(), "Failed: {:?}", result.err());
2744    }
2745
2746    #[test]
2747    fn test_parse_stream_with_filter() {
2748        let result = parse("stream output = input.where(value > 100)");
2749        assert!(result.is_ok(), "Failed: {:?}", result.err());
2750    }
2751
2752    #[test]
2753    fn test_parse_stream_with_map() {
2754        let result = parse("stream output = input.map(x * 2)");
2755        assert!(result.is_ok(), "Failed: {:?}", result.err());
2756    }
2757
2758    #[test]
2759    fn test_parse_event_declaration() {
2760        let result = parse("event SensorReading:\n  sensor_id: str\n  value: float");
2761        assert!(result.is_ok(), "Failed: {:?}", result.err());
2762    }
2763
2764    #[test]
2765    fn test_parse_variable() {
2766        let result = parse("let x = 42");
2767        assert!(result.is_ok(), "Failed: {:?}", result.err());
2768    }
2769
2770    #[test]
2771    fn test_parse_function() {
2772        let result = parse("fn add(a: int, b: int) -> int:\n  return a + b");
2773        assert!(result.is_ok(), "Failed: {:?}", result.err());
2774    }
2775
2776    #[test]
2777    fn test_parse_lambda() {
2778        let result = parse("let f = (x) => x * 2");
2779        assert!(result.is_ok(), "Failed: {:?}", result.err());
2780    }
2781
2782    #[test]
2783    fn test_parse_if_expression() {
2784        let result = parse("let x = if a > b then a else b");
2785        assert!(result.is_ok(), "Failed: {:?}", result.err());
2786    }
2787
2788    #[test]
2789    fn test_parse_followed_by() {
2790        let result = parse("stream alerts = orders.where(amount > 1000) -> Payment where payment.order_id == orders.id");
2791        assert!(result.is_ok(), "Failed: {:?}", result.err());
2792    }
2793
2794    #[test]
2795    fn test_parse_window() {
2796        let result = parse("stream windowed = input.window(5s)");
2797        assert!(result.is_ok(), "Failed: {:?}", result.err());
2798    }
2799
2800    #[test]
2801    fn test_parse_aggregate() {
2802        let result =
2803            parse("stream stats = input.window(1m).aggregate(count: count(), avg: avg(value))");
2804        assert!(result.is_ok(), "Failed: {:?}", result.err());
2805    }
2806
2807    #[test]
2808    fn test_parse_merge() {
2809        let result = parse("stream combined = merge(stream1, stream2)");
2810        assert!(result.is_ok(), "Failed: {:?}", result.err());
2811    }
2812
2813    #[test]
2814    fn test_parse_sequence() {
2815        let result = parse("stream seq = sequence(a: EventA, b: EventB where b.id == a.id)");
2816        assert!(result.is_ok(), "Failed: {:?}", result.err());
2817    }
2818
2819    #[test]
2820    fn test_parse_config() {
2821        let result = parse("config:\n  window_size: 5s\n  batch_size: 100");
2822        assert!(result.is_ok(), "Failed: {:?}", result.err());
2823    }
2824
2825    #[test]
2826    fn test_parse_complex_expression() {
2827        let result = parse("let x = (a + b) * c / d - e");
2828        assert!(result.is_ok(), "Failed: {:?}", result.err());
2829    }
2830
2831    #[test]
2832    fn test_parse_sliding_window() {
2833        let result = parse("stream output = input.window(5m, sliding: 1m)");
2834        assert!(result.is_ok(), "Failed: {:?}", result.err());
2835    }
2836
2837    #[test]
2838    fn test_parse_fork_construct() {
2839        let result =
2840            parse("stream forked = input.fork(branch1: .where(x > 0), branch2: .where(x < 0))");
2841        assert!(result.is_ok(), "Failed: {:?}", result.err());
2842    }
2843
2844    #[test]
2845    fn test_parse_aggregate_functions() {
2846        let result = parse(
2847            "stream stats = input.window(1h).aggregate(total: sum(value), average: avg(value))",
2848        );
2849        assert!(result.is_ok(), "Failed: {:?}", result.err());
2850    }
2851
2852    #[test]
2853    fn test_parse_complex_parentheses() {
2854        let result = parse("let x = ((a + b) * (c - d)) / e");
2855        assert!(result.is_ok(), "Failed: {:?}", result.err());
2856    }
2857
2858    #[test]
2859    fn test_parse_sequence_with_alias() {
2860        let result = parse(
2861            r#"
2862            stream TwoTicks = StockTick as first
2863                -> StockTick as second
2864                .emit(result: "two_ticks")
2865        "#,
2866        );
2867        assert!(result.is_ok(), "Failed: {:?}", result.err());
2868    }
2869
2870    #[test]
2871    fn test_parse_followed_by_with_alias() {
2872        let result = parse("stream alerts = Order as a -> Payment as b");
2873        assert!(result.is_ok(), "Failed: {:?}", result.err());
2874    }
2875
2876    #[test]
2877    fn test_parse_followed_by_with_filter_and_alias() {
2878        // This is the problematic case: 'as b' should be the alias, not part of the expression
2879        let result = parse(
2880            r#"
2881            stream Test = A as a
2882                -> B where value == a.base + 10 as b
2883                .emit(status: "matched")
2884        "#,
2885        );
2886        assert!(result.is_ok(), "Failed: {:?}", result.err());
2887    }
2888
2889    #[test]
2890    fn test_parse_pattern_with_lambda() {
2891        let result =
2892            parse("stream Test = Trade.window(1m).pattern(p: x => x.len() > 3).emit(alert: true)");
2893        assert!(result.is_ok(), "Failed: {:?}", result.err());
2894    }
2895
2896    #[test]
2897    fn test_parse_sase_pattern_decl_simple() {
2898        let result = parse("pattern SimpleAlert = SEQ(Login, Transaction)");
2899        assert!(result.is_ok(), "Failed: {:?}", result.err());
2900    }
2901
2902    #[test]
2903    fn test_parse_sase_pattern_decl_with_kleene() {
2904        let result = parse("pattern MultiTx = SEQ(Login, Transaction+ where amount > 1000)");
2905        assert!(result.is_ok(), "Failed: {:?}", result.err());
2906    }
2907
2908    #[test]
2909    fn test_parse_sase_pattern_decl_with_alias() {
2910        let result = parse("pattern AliasedPattern = SEQ(Login as login, Transaction as tx)");
2911        assert!(result.is_ok(), "Failed: {:?}", result.err());
2912    }
2913
2914    #[test]
2915    fn test_parse_sase_pattern_decl_with_within() {
2916        let result = parse("pattern TimedPattern = SEQ(A, B) within 10m");
2917        assert!(result.is_ok(), "Failed: {:?}", result.err());
2918    }
2919
2920    #[test]
2921    fn test_parse_sase_pattern_decl_with_partition() {
2922        let result = parse("pattern PartitionedPattern = SEQ(A, B) partition by user_id");
2923        assert!(result.is_ok(), "Failed: {:?}", result.err());
2924    }
2925
2926    #[test]
2927    fn test_parse_sase_pattern_decl_full() {
2928        // Note: In SASE+ syntax, 'where' comes before 'as' (filter then alias)
2929        let result = parse(
2930            "pattern SuspiciousActivity = SEQ(Transaction+ where amount > 1000 as txs) within 10m partition by user_id"
2931        );
2932        assert!(result.is_ok(), "Failed: {:?}", result.err());
2933    }
2934
2935    #[test]
2936    fn test_parse_sase_pattern_decl_or() {
2937        let result = parse("pattern AlertOrWarn = Login OR Logout");
2938        assert!(result.is_ok(), "Failed: {:?}", result.err());
2939    }
2940
2941    #[test]
2942    fn test_parse_sase_pattern_decl_and() {
2943        let result = parse("pattern BothEvents = Login AND Transaction");
2944        assert!(result.is_ok(), "Failed: {:?}", result.err());
2945    }
2946
2947    #[test]
2948    fn test_parse_sase_pattern_decl_not() {
2949        let result = parse("pattern NoLogout = SEQ(Login, NOT Logout, Transaction)");
2950        assert!(result.is_ok(), "Failed: {:?}", result.err());
2951    }
2952
2953    #[test]
2954    fn test_parse_having() {
2955        let result = parse(
2956            "stream filtered = input.window(1m).aggregate(count: count(), total: sum(value)).having(count > 10)",
2957        );
2958        assert!(result.is_ok(), "Failed: {:?}", result.err());
2959    }
2960
2961    #[test]
2962    fn test_parse_having_with_partition() {
2963        let result = parse(
2964            "stream grouped = input.partition_by(category).window(5m).aggregate(avg_price: avg(price)).having(avg_price > 100.0)",
2965        );
2966        assert!(result.is_ok(), "Failed: {:?}", result.err());
2967    }
2968
2969    #[test]
2970    fn test_parse_timer_source() {
2971        let result = parse("stream heartbeat = timer(5s).emit(type: \"heartbeat\")");
2972        assert!(result.is_ok(), "Failed: {:?}", result.err());
2973    }
2974
2975    #[test]
2976    fn test_parse_timer_source_with_initial_delay() {
2977        let result =
2978            parse("stream delayed_timer = timer(1m, initial_delay: 10s).emit(type: \"periodic\")");
2979        assert!(result.is_ok(), "Failed: {:?}", result.err());
2980    }
2981
2982    #[test]
2983    fn test_parse_var_decl() {
2984        let result = parse("var threshold: float = 10.0");
2985        assert!(result.is_ok(), "Failed: {:?}", result.err());
2986    }
2987
2988    #[test]
2989    fn test_parse_let_decl() {
2990        let result = parse("let max_count: int = 100");
2991        assert!(result.is_ok(), "Failed: {:?}", result.err());
2992    }
2993
2994    #[test]
2995    fn test_parse_assignment() {
2996        let result = parse("threshold := threshold + 10.0");
2997        assert!(result.is_ok(), "Failed: {:?}", result.err());
2998    }
2999
3000    #[test]
3001    fn test_parse_assignment_with_expression() {
3002        let result = parse("count := count * 2 + offset");
3003        assert!(result.is_ok(), "Failed: {:?}", result.err());
3004    }
3005
3006    #[test]
3007    fn test_parse_nested_stream_reference() {
3008        let result = parse("stream Base = Event\nstream Derived = Base.where(x > 0)");
3009        assert!(result.is_ok(), "Failed: {:?}", result.err());
3010    }
3011
3012    #[test]
3013    fn test_parse_multi_stage_pipeline() {
3014        let result = parse(
3015            "stream L1 = Raw\nstream L2 = L1.where(a > 1)\nstream L3 = L2.window(5).aggregate(cnt: count())",
3016        );
3017        assert!(result.is_ok(), "Failed: {:?}", result.err());
3018    }
3019
3020    #[test]
3021    fn test_parse_stream_with_operations_chain() {
3022        let result = parse(
3023            "stream Processed = Source.where(valid).window(10).aggregate(sum: sum(value)).having(sum > 100)",
3024        );
3025        assert!(result.is_ok(), "Failed: {:?}", result.err());
3026    }
3027
3028    // =========================================================================
3029    // Connectivity Architecture Tests
3030    // =========================================================================
3031
3032    #[test]
3033    fn test_parse_connector_mqtt() {
3034        let result = parse(
3035            r#"connector MqttBroker = mqtt (
3036                host: "localhost",
3037                port: 1883,
3038                client_id: "varpulis"
3039            )"#,
3040        );
3041        assert!(result.is_ok(), "Failed: {:?}", result.err());
3042    }
3043
3044    #[test]
3045    fn test_parse_connector_kafka() {
3046        let result = parse(
3047            r#"connector KafkaCluster = kafka (
3048                brokers: ["kafka1:9092", "kafka2:9092"],
3049                group_id: "my-group"
3050            )"#,
3051        );
3052        assert!(result.is_ok(), "Failed: {:?}", result.err());
3053    }
3054
3055    #[test]
3056    fn test_parse_connector_http() {
3057        let result = parse(
3058            r#"connector ApiEndpoint = http (
3059                base_url: "https://api.example.com"
3060            )"#,
3061        );
3062        assert!(result.is_ok(), "Failed: {:?}", result.err());
3063    }
3064
3065    #[test]
3066    fn test_parse_stream_with_from_connector() {
3067        let result = parse(
3068            r#"stream Temperatures = TemperatureReading.from(MqttSensors, topic: "sensors/temp/#")"#,
3069        );
3070        assert!(result.is_ok(), "Failed: {:?}", result.err());
3071    }
3072
3073    #[test]
3074    fn test_parse_stream_with_from_and_operations() {
3075        let result = parse(
3076            r#"stream HighTemp = TemperatureReading
3077                .from(MqttSensors, topic: "sensors/#")
3078                .where(value > 30)
3079                .emit(alert: "high_temp")"#,
3080        );
3081        assert!(result.is_ok(), "Failed: {:?}", result.err());
3082    }
3083
3084    #[test]
3085    fn test_parse_full_connectivity_pipeline() {
3086        let result = parse(
3087            r#"
3088            connector MqttSensors = mqtt (host: "localhost", port: 1883)
3089            connector KafkaAlerts = kafka (brokers: ["kafka:9092"])
3090
3091            event TemperatureReading:
3092                sensor_id: str
3093                value: float
3094                ts: timestamp
3095
3096            stream Temperatures = TemperatureReading.from(MqttSensors, topic: "sensors/#")
3097
3098            stream HighTempAlert = Temperatures
3099                .where(value > 30)
3100                .emit(alert_type: "HIGH_TEMP", temperature: value)
3101
3102            "#,
3103        );
3104        assert!(result.is_ok(), "Failed: {:?}", result.err());
3105    }
3106
3107    #[test]
3108    fn test_parse_emit_as_type() {
3109        let result = parse(
3110            r#"stream Alerts = Temperatures
3111                .where(value > 30)
3112                .emit as AlertEvent(severity: "high", temp: value)"#,
3113        );
3114        assert!(result.is_ok(), "Failed: {:?}", result.err());
3115    }
3116
3117    #[test]
3118    fn test_parse_stream_with_to_connector() {
3119        let result = parse(
3120            r#"stream Output = Input
3121                .where(x > 0)
3122                .emit(y: x * 2)
3123                .to(KafkaOutput, topic: "output")"#,
3124        );
3125        assert!(result.is_ok(), "Failed: {:?}", result.err());
3126    }
3127
3128    #[test]
3129    fn test_emit_stmt_parses() {
3130        let result = parse(
3131            r"fn test():
3132    emit Pixel(x: 1, y: 2)",
3133        );
3134        assert!(result.is_ok(), "Failed: {:?}", result.err());
3135        let program = result.unwrap();
3136        // Find the fn_decl
3137        if let Stmt::FnDecl { body, .. } = &program.statements[0].node {
3138            match &body[0].node {
3139                Stmt::Emit { event_type, fields } => {
3140                    assert_eq!(event_type, "Pixel");
3141                    assert_eq!(fields.len(), 2);
3142                    assert_eq!(fields[0].name, "x");
3143                    assert_eq!(fields[1].name, "y");
3144                }
3145                other => panic!("Expected Stmt::Emit, got {other:?}"),
3146            }
3147        } else {
3148            panic!("Expected FnDecl");
3149        }
3150    }
3151
3152    #[test]
3153    fn test_emit_stmt_no_args() {
3154        let result = parse(
3155            r"fn test():
3156    emit Done()",
3157        );
3158        assert!(result.is_ok(), "Failed: {:?}", result.err());
3159        let program = result.unwrap();
3160        if let Stmt::FnDecl { body, .. } = &program.statements[0].node {
3161            match &body[0].node {
3162                Stmt::Emit { event_type, fields } => {
3163                    assert_eq!(event_type, "Done");
3164                    assert!(fields.is_empty());
3165                }
3166                other => panic!("Expected Stmt::Emit, got {other:?}"),
3167            }
3168        } else {
3169            panic!("Expected FnDecl");
3170        }
3171    }
3172
3173    #[test]
3174    fn test_emit_in_function_with_for_loop() {
3175        let result = parse(
3176            r"fn generate(n: int):
3177    for i in 0..n:
3178        emit Item(index: i, value: i * 2)",
3179        );
3180        assert!(result.is_ok(), "Failed: {:?}", result.err());
3181    }
3182
3183    #[test]
3184    fn test_parse_process_op() {
3185        let result = parse(
3186            r"fn do_work():
3187    emit Result(v: 42)
3188
3189stream S = timer(1s).process(do_work())",
3190        );
3191        assert!(result.is_ok(), "Failed: {:?}", result.err());
3192    }
3193
3194    #[test]
3195    fn test_parse_trend_aggregate_count_trends() {
3196        let result = parse(
3197            r#"stream S = StockTick as first
3198    -> all StockTick where price > first.price as rising
3199    -> StockTick where price < rising.price as drop
3200    .within(60s)
3201    .trend_aggregate(count: count_trends())
3202    .emit(event_type: "TrendStats", trends: count)"#,
3203        );
3204        assert!(result.is_ok(), "Failed: {:?}", result.err());
3205        let program = result.unwrap();
3206        // Find the stream declaration and check for TrendAggregate op
3207        for stmt in &program.statements {
3208            if let Stmt::StreamDecl { ops, .. } = &stmt.node {
3209                let has_trend_agg = ops
3210                    .iter()
3211                    .any(|op| matches!(op, StreamOp::TrendAggregate(_)));
3212                assert!(has_trend_agg, "Expected TrendAggregate op in stream ops");
3213                // Check that TrendAggregate has the right item
3214                for op in ops {
3215                    if let StreamOp::TrendAggregate(items) = op {
3216                        assert_eq!(items.len(), 1);
3217                        assert_eq!(items[0].alias, "count");
3218                        assert_eq!(items[0].func, "count_trends");
3219                        assert!(items[0].arg.is_none());
3220                    }
3221                }
3222                return;
3223            }
3224        }
3225        panic!("No stream declaration found");
3226    }
3227
3228    #[test]
3229    fn test_parse_trend_aggregate_multiple_items() {
3230        let result = parse(
3231            r"stream S = StockTick as first
3232    -> all StockTick as rising
3233    .within(60s)
3234    .trend_aggregate(
3235        trend_count: count_trends(),
3236        event_count: count_events(rising)
3237    )
3238    .emit(trends: trend_count, events: event_count)",
3239        );
3240        assert!(result.is_ok(), "Failed: {:?}", result.err());
3241        let program = result.unwrap();
3242        for stmt in &program.statements {
3243            if let Stmt::StreamDecl { ops, .. } = &stmt.node {
3244                for op in ops {
3245                    if let StreamOp::TrendAggregate(items) = op {
3246                        assert_eq!(items.len(), 2);
3247                        assert_eq!(items[0].alias, "trend_count");
3248                        assert_eq!(items[0].func, "count_trends");
3249                        assert_eq!(items[1].alias, "event_count");
3250                        assert_eq!(items[1].func, "count_events");
3251                        assert!(items[1].arg.is_some());
3252                        return;
3253                    }
3254                }
3255            }
3256        }
3257        panic!("No TrendAggregate found");
3258    }
3259
3260    #[test]
3261    fn test_parse_score_basic() {
3262        let result = parse(
3263            r#"stream S = TradeEvent
3264    .score(model: "models/fraud.onnx", inputs: [amount, risk_score], outputs: [fraud_prob, category])"#,
3265        );
3266        assert!(result.is_ok(), "Failed: {:?}", result.err());
3267        let program = result.unwrap();
3268        for stmt in &program.statements {
3269            if let Stmt::StreamDecl { ops, .. } = &stmt.node {
3270                for op in ops {
3271                    if let StreamOp::Score(spec) = op {
3272                        assert_eq!(spec.model_path, "models/fraud.onnx");
3273                        assert_eq!(spec.inputs, vec!["amount", "risk_score"]);
3274                        assert_eq!(spec.outputs, vec!["fraud_prob", "category"]);
3275                        return;
3276                    }
3277                }
3278            }
3279        }
3280        panic!("No Score op found");
3281    }
3282
3283    #[test]
3284    fn test_parse_score_single_field() {
3285        let result = parse(
3286            r#"stream S = Event
3287    .score(model: "model.onnx", inputs: [value], outputs: [prediction])"#,
3288        );
3289        assert!(result.is_ok(), "Failed: {:?}", result.err());
3290        let program = result.unwrap();
3291        for stmt in &program.statements {
3292            if let Stmt::StreamDecl { ops, .. } = &stmt.node {
3293                for op in ops {
3294                    if let StreamOp::Score(spec) = op {
3295                        assert_eq!(spec.model_path, "model.onnx");
3296                        assert_eq!(spec.inputs, vec!["value"]);
3297                        assert_eq!(spec.outputs, vec!["prediction"]);
3298                        return;
3299                    }
3300                }
3301            }
3302        }
3303        panic!("No Score op found");
3304    }
3305
3306    // Regression tests for fuzz-discovered parser hangs (2026-02-21).
3307    // Unmatched `[` brackets cause exponential backtracking in pest's PEG
3308    // recursive descent through array_literal / index_access / slice_access.
3309
3310    #[test]
3311    fn fuzz_regression_unmatched_brackets_timeout() {
3312        // Simplified version of the fuzzer-discovered timeout input (28 unmatched '[')
3313        let input = "c2222222s[s[22s[U2s[U6[U6[22222222s[s[22s[U2s[U6[U6[222*2222s[U6[U6[222*2222s[22s[U6[U6[22*2222s[U6[U6[222*2222s[22s[U6[U6[222*26[U6[222*2";
3314        let start = std::time::Instant::now();
3315        let result = parse(input);
3316        let elapsed = start.elapsed();
3317        assert!(
3318            result.is_err(),
3319            "Should reject deeply nested unmatched brackets"
3320        );
3321        assert!(
3322            elapsed.as_millis() < 100,
3323            "Parser should reject fast, took {elapsed:?}"
3324        );
3325    }
3326
3327    #[test]
3328    fn fuzz_regression_deeply_nested_brackets_slow_unit() {
3329        // 30 unmatched '[' — must be rejected by nesting depth check
3330        let input = "stream x[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[";
3331        let start = std::time::Instant::now();
3332        let result = parse(input);
3333        let elapsed = start.elapsed();
3334        assert!(result.is_err(), "Should reject deeply nested brackets");
3335        assert!(
3336            elapsed.as_millis() < 100,
3337            "Parser should reject fast, took {elapsed:?}"
3338        );
3339    }
3340
3341    #[test]
3342    fn nesting_depth_allows_reasonable_programs() {
3343        // 10 levels of nesting — well within the limit
3344        let input = "let x = foo(bar(baz(qux(a, [1, [2, [3, [4]]]]))))";
3345        let result = parse(input);
3346        // May fail for other reasons (not a valid program) but should NOT
3347        // fail with "Nesting depth exceeds maximum"
3348        if let Err(ref e) = result {
3349            let msg = format!("{e}");
3350            assert!(
3351                !msg.contains("Nesting depth"),
3352                "Should allow 10 levels of nesting: {msg}"
3353            );
3354        }
3355    }
3356
3357    #[test]
3358    fn nesting_depth_ignores_brackets_in_comments() {
3359        // Brackets inside # comments should not count
3360        let input = "# [[[[[[[[[[[[[[[[[[[[[[[[[[\nstream x = y";
3361        let result = parse(input);
3362        assert!(
3363            result.is_ok(),
3364            "Brackets in comments should be ignored: {:?}",
3365            result.err()
3366        );
3367    }
3368
3369    #[test]
3370    fn nesting_depth_ignores_brackets_in_strings() {
3371        // Brackets inside strings should not count
3372        let input = r#"let x = "[[[[[[[[[[[[[[[[[[[[[[[[[[""#;
3373        let result = parse(input);
3374        if let Err(ref e) = result {
3375            let msg = format!("{e}");
3376            assert!(
3377                !msg.contains("Nesting depth"),
3378                "Brackets in strings should be ignored: {msg}"
3379            );
3380        }
3381    }
3382}