1use 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
18trait IteratorExt<'a> {
20 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#[derive(Debug, Parser)]
41#[grammar = "varpulis.pest"]
42pub struct VarpulisParser;
43
44const PARSE_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10);
52
53pub 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 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
93const MAX_NESTING_DEPTH: usize = 10;
102
103const MAX_UNMATCHED_OPEN_BRACKETS: usize = 6;
111
112fn 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 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 if b == b'"' {
137 i += 1;
138 while i < len {
139 if bytes[i] == b'\\' {
140 i += 2; continue;
142 }
143 if bytes[i] == b'"' {
144 i += 1;
145 break;
146 }
147 i += 1;
148 }
149 continue;
150 }
151
152 if b == b'#' {
154 i += 1;
155 while i < len && bytes[i] != b'\n' {
156 i += 1;
157 }
158 continue;
159 }
160
161 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 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 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 let expanded =
239 crate::expand::expand_declaration_loops(source).map_err(|e| ParseError::InvalidToken {
240 position: 0,
241 message: e,
242 })?;
243 let preprocessed = preprocess_indentation(&expanded);
245
246 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 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 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 (
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
318fn 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
348fn 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
434fn 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 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
462fn 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
552fn 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 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 let item = parse_sase_item_inner(pair, false)?;
729
730 if item.alias.is_none() && item.kleene.is_none() && item.filter.is_none() {
732 Ok(SasePatternExpr::Event(item.event_type))
733 } else {
734 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 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 let interval = parse_expr(inner.expect_next("timer interval")?)?;
926
927 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 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 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 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 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 let (name, items_start) = if first.as_rule() == Rule::identifier {
1697 (first.as_str().to_string(), None)
1698 } else {
1699 ("default".to_string(), Some(first))
1701 };
1702
1703 let mut items = Vec::new();
1704
1705 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_array => {
1733 let values: Vec<ConfigValue> = inner
1734 .into_inner()
1735 .map(parse_config_value)
1736 .collect::<ParseResult<Vec<_>>>()?;
1737 Ok(ConfigValue::Array(values))
1738 }
1739 Rule::integer => Ok(ConfigValue::Int(inner.as_str().parse().unwrap_or(0))),
1740 Rule::float => Ok(ConfigValue::Float(inner.as_str().parse().unwrap_or(0.0))),
1741 Rule::string => {
1742 let s = inner.as_str();
1743 Ok(ConfigValue::Str(s[1..s.len() - 1].to_string()))
1744 }
1745 Rule::duration => Ok(ConfigValue::Duration(
1746 parse_duration(inner.as_str()).map_err(ParseError::InvalidDuration)?,
1747 )),
1748 Rule::boolean => Ok(ConfigValue::Bool(inner.as_str() == "true")),
1749 Rule::identifier => Ok(ConfigValue::Ident(inner.as_str().to_string())),
1750 _ => Ok(ConfigValue::Ident(inner.as_str().to_string())),
1751 }
1752}
1753
1754fn parse_import_stmt(pair: pest::iterators::Pair<Rule>) -> ParseResult<Stmt> {
1755 let mut inner = pair.into_inner();
1756 let path_pair = inner.expect_next("import path")?;
1757 let path = path_pair.as_str();
1758 let path = path[1..path.len() - 1].to_string();
1759 let alias = inner.next().map(|p| p.as_str().to_string());
1760 Ok(Stmt::Import { path, alias })
1761}
1762
1763fn parse_if_stmt(pair: pest::iterators::Pair<Rule>) -> ParseResult<Stmt> {
1764 let mut inner = pair.into_inner();
1765 let cond = parse_expr(inner.expect_next("if condition")?)?;
1766
1767 let mut then_branch = Vec::new();
1768 let mut elif_branches = Vec::new();
1769 let mut else_branch = None;
1770
1771 for p in inner {
1772 match p.as_rule() {
1773 Rule::block => then_branch = parse_block(p)?,
1774 Rule::statement => then_branch.push(parse_statement(p)?),
1775 Rule::elif_clause => {
1776 let mut elif_inner = p.into_inner();
1777 let elif_cond = parse_expr(elif_inner.expect_next("elif condition")?)?;
1778 let mut elif_body = Vec::new();
1779 for ep in elif_inner {
1780 match ep.as_rule() {
1781 Rule::block => elif_body = parse_block(ep)?,
1782 Rule::statement => elif_body.push(parse_statement(ep)?),
1783 _ => {}
1784 }
1785 }
1786 elif_branches.push((elif_cond, elif_body));
1787 }
1788 Rule::else_clause => {
1789 let mut else_body = Vec::new();
1790 for ep in p.into_inner() {
1791 match ep.as_rule() {
1792 Rule::block => else_body = parse_block(ep)?,
1793 Rule::statement => else_body.push(parse_statement(ep)?),
1794 _ => {}
1795 }
1796 }
1797 else_branch = Some(else_body);
1798 }
1799 _ => {}
1800 }
1801 }
1802
1803 Ok(Stmt::If {
1804 cond,
1805 then_branch,
1806 elif_branches,
1807 else_branch,
1808 })
1809}
1810
1811fn parse_for_stmt(pair: pest::iterators::Pair<Rule>) -> ParseResult<Stmt> {
1812 let mut inner = pair.into_inner();
1813 let var = inner.expect_next("loop variable")?.as_str().to_string();
1814 let iter = parse_expr(inner.expect_next("iterable expression")?)?;
1815 let mut body = Vec::new();
1816 for p in inner {
1817 match p.as_rule() {
1818 Rule::block => body = parse_block(p)?,
1819 Rule::statement => body.push(parse_statement(p)?),
1820 _ => {}
1821 }
1822 }
1823 Ok(Stmt::For { var, iter, body })
1824}
1825
1826fn parse_while_stmt(pair: pest::iterators::Pair<Rule>) -> ParseResult<Stmt> {
1827 let mut inner = pair.into_inner();
1828 let cond = parse_expr(inner.expect_next("while condition")?)?;
1829 let mut body = Vec::new();
1830 for p in inner {
1831 match p.as_rule() {
1832 Rule::block => body = parse_block(p)?,
1833 Rule::statement => body.push(parse_statement(p)?),
1834 _ => {}
1835 }
1836 }
1837 Ok(Stmt::While { cond, body })
1838}
1839
1840fn parse_return_stmt(pair: pest::iterators::Pair<Rule>) -> ParseResult<Stmt> {
1841 let expr = pair.into_inner().next().map(parse_expr).transpose()?;
1842 Ok(Stmt::Return(expr))
1843}
1844
1845fn parse_emit_stmt(pair: pest::iterators::Pair<Rule>) -> ParseResult<Stmt> {
1846 let mut inner = pair.into_inner();
1847 let event_type = inner.expect_next("event type name")?.as_str().to_string();
1848 let mut fields = Vec::new();
1849 for p in inner {
1850 if p.as_rule() == Rule::named_arg_list {
1851 for arg in p.into_inner() {
1852 fields.push(parse_named_arg(arg)?);
1853 }
1854 }
1855 }
1856 Ok(Stmt::Emit { event_type, fields })
1857}
1858
1859fn parse_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
1864 let inner = pair.into_inner().next();
1865
1866 match inner {
1867 Some(p) => parse_expr_inner(p),
1868 None => Ok(Expr::Null),
1869 }
1870}
1871
1872fn parse_expr_inner(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
1873 match pair.as_rule() {
1874 Rule::expr => parse_expr(pair),
1875 Rule::lambda_expr => parse_lambda_expr(pair),
1876 Rule::range_expr => parse_range_expr(pair),
1877 Rule::or_expr => parse_or_expr(pair),
1878 Rule::and_expr => parse_and_expr(pair),
1879 Rule::not_expr => parse_not_expr(pair),
1880 Rule::comparison_expr => parse_comparison_expr(pair),
1881 Rule::bitwise_or_expr => parse_bitwise_or_expr(pair),
1882 Rule::bitwise_xor_expr => parse_bitwise_xor_expr(pair),
1883 Rule::bitwise_and_expr => parse_bitwise_and_expr(pair),
1884 Rule::shift_expr => parse_shift_expr(pair),
1885 Rule::additive_expr => parse_additive_expr(pair),
1886 Rule::multiplicative_expr => parse_multiplicative_expr(pair),
1887 Rule::power_expr => parse_power_expr(pair),
1888 Rule::unary_expr => parse_unary_expr(pair),
1889 Rule::postfix_expr => parse_postfix_expr(pair),
1890 Rule::primary_expr => parse_primary_expr(pair),
1891 Rule::literal => parse_literal(pair),
1892 Rule::identifier => Ok(Expr::Ident(pair.as_str().to_string())),
1893 Rule::if_expr => parse_if_expr(pair),
1894 _ => Ok(Expr::Ident(pair.as_str().to_string())),
1895 }
1896}
1897
1898fn parse_lambda_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
1899 let mut inner = pair.into_inner();
1900 let mut params = Vec::new();
1901
1902 let first = inner.expect_next("lambda parameters")?;
1904 match first.as_rule() {
1905 Rule::identifier_list => {
1906 for p in first.into_inner() {
1907 params.push(p.as_str().to_string());
1908 }
1909 }
1910 Rule::identifier => {
1911 params.push(first.as_str().to_string());
1912 }
1913 _ => {}
1914 }
1915
1916 let body_pair = inner.expect_next("lambda body")?;
1918 let body = match body_pair.as_rule() {
1919 Rule::lambda_block => parse_lambda_block(body_pair)?,
1920 _ => parse_expr_inner(body_pair)?,
1921 };
1922
1923 Ok(Expr::Lambda {
1924 params,
1925 body: Box::new(body),
1926 })
1927}
1928
1929fn parse_lambda_block(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
1930 let mut stmts = Vec::new();
1931 let mut final_expr = None;
1932
1933 for p in pair.into_inner() {
1934 match p.as_rule() {
1935 Rule::statement => {
1936 let stmt = parse_statement(p)?;
1938 match &stmt.node {
1939 Stmt::VarDecl {
1940 mutable,
1941 name,
1942 ty,
1943 value,
1944 } => {
1945 stmts.push((name.clone(), ty.clone(), value.clone(), *mutable));
1946 }
1947 Stmt::Expr(e) => {
1948 final_expr = Some(e.clone());
1950 }
1951 _ => {
1952 }
1954 }
1955 }
1956 _ => {
1957 final_expr = Some(parse_expr_inner(p)?);
1959 }
1960 }
1961 }
1962
1963 if stmts.is_empty() {
1965 Ok(final_expr.unwrap_or(Expr::Null))
1966 } else {
1967 Ok(Expr::Block {
1968 stmts,
1969 result: Box::new(final_expr.unwrap_or(Expr::Null)),
1970 })
1971 }
1972}
1973
1974fn parse_pattern_expr_as_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
1975 let mut inner = pair.into_inner();
1978 let mut left = parse_pattern_and_as_expr(inner.expect_next("pattern expression")?)?;
1979
1980 for right_pair in inner {
1981 let right = parse_pattern_and_as_expr(right_pair)?;
1982 left = Expr::Binary {
1983 op: BinOp::Or,
1984 left: Box::new(left),
1985 right: Box::new(right),
1986 };
1987 }
1988 Ok(left)
1989}
1990
1991fn parse_pattern_and_as_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
1992 let mut inner = pair.into_inner();
1993 let mut left = parse_pattern_xor_as_expr(inner.expect_next("and expression")?)?;
1994
1995 for right_pair in inner {
1996 let right = parse_pattern_xor_as_expr(right_pair)?;
1997 left = Expr::Binary {
1998 op: BinOp::And,
1999 left: Box::new(left),
2000 right: Box::new(right),
2001 };
2002 }
2003 Ok(left)
2004}
2005
2006fn parse_pattern_xor_as_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2007 let mut inner = pair.into_inner();
2008 let mut left = parse_pattern_unary_as_expr(inner.expect_next("xor expression")?)?;
2009
2010 for right_pair in inner {
2011 let right = parse_pattern_unary_as_expr(right_pair)?;
2012 left = Expr::Binary {
2013 op: BinOp::Xor,
2014 left: Box::new(left),
2015 right: Box::new(right),
2016 };
2017 }
2018 Ok(left)
2019}
2020
2021fn parse_pattern_unary_as_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2022 let mut inner = pair.into_inner();
2023 let first = inner.expect_next("unary expression or operand")?;
2024
2025 if first.as_str() == "not" {
2026 let expr = parse_pattern_primary_as_expr(inner.expect_next("pattern expression")?)?;
2027 Ok(Expr::Unary {
2028 op: UnaryOp::Not,
2029 expr: Box::new(expr),
2030 })
2031 } else {
2032 parse_pattern_primary_as_expr(first)
2033 }
2034}
2035
2036fn parse_pattern_primary_as_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2037 let inner = pair
2038 .into_inner()
2039 .expect_next("pattern primary expression")?;
2040
2041 match inner.as_rule() {
2042 Rule::pattern_or_expr => parse_pattern_expr_as_expr(inner),
2043 Rule::pattern_sequence => parse_pattern_sequence_as_expr(inner),
2044 _ => Ok(Expr::Ident(inner.as_str().to_string())),
2045 }
2046}
2047
2048fn parse_pattern_sequence_as_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2049 let mut inner = pair.into_inner();
2052 let mut left = Expr::Ident(inner.expect_next("sequence start")?.as_str().to_string());
2053
2054 for right_pair in inner {
2055 let right = Expr::Ident(right_pair.as_str().to_string());
2056 left = Expr::Binary {
2057 op: BinOp::FollowedBy,
2058 left: Box::new(left),
2059 right: Box::new(right),
2060 };
2061 }
2062 Ok(left)
2063}
2064
2065fn parse_filter_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2066 let inner = pair.into_inner().expect_next("filter expression")?;
2067 parse_filter_or_expr(inner)
2068}
2069
2070fn parse_filter_or_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2071 let mut inner = pair.into_inner();
2072 let mut left = parse_filter_and_expr(inner.expect_next("or expression operand")?)?;
2073
2074 for right_pair in inner {
2075 let right = parse_filter_and_expr(right_pair)?;
2076 left = Expr::Binary {
2077 op: BinOp::Or,
2078 left: Box::new(left),
2079 right: Box::new(right),
2080 };
2081 }
2082 Ok(left)
2083}
2084
2085fn parse_filter_and_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2086 let mut inner = pair.into_inner();
2087 let mut left = parse_filter_not_expr(inner.expect_next("and expression operand")?)?;
2088
2089 for right_pair in inner {
2090 let right = parse_filter_not_expr(right_pair)?;
2091 left = Expr::Binary {
2092 op: BinOp::And,
2093 left: Box::new(left),
2094 right: Box::new(right),
2095 };
2096 }
2097 Ok(left)
2098}
2099
2100fn parse_filter_not_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2101 let mut inner = pair.into_inner();
2102 let first = inner.expect_next("not or expression")?;
2103
2104 if first.as_str() == "not" {
2105 let expr = parse_filter_comparison_expr(inner.expect_next("expression after not")?)?;
2106 Ok(Expr::Unary {
2107 op: UnaryOp::Not,
2108 expr: Box::new(expr),
2109 })
2110 } else {
2111 parse_filter_comparison_expr(first)
2112 }
2113}
2114
2115fn parse_filter_comparison_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2116 let mut inner = pair.into_inner();
2117 let left = parse_filter_additive_expr(inner.expect_next("comparison left operand")?)?;
2118
2119 if let Some(op_pair) = inner.next() {
2120 let op = match op_pair.as_str() {
2121 "==" => BinOp::Eq,
2122 "!=" => BinOp::NotEq,
2123 "<" => BinOp::Lt,
2124 "<=" => BinOp::Le,
2125 ">" => BinOp::Gt,
2126 ">=" => BinOp::Ge,
2127 "in" => BinOp::In,
2128 "is" => BinOp::Is,
2129 s if s.contains("not") && s.contains("in") => BinOp::NotIn,
2130 _ => BinOp::Eq,
2131 };
2132 let right = parse_filter_additive_expr(inner.expect_next("comparison right operand")?)?;
2133 Ok(Expr::Binary {
2134 op,
2135 left: Box::new(left),
2136 right: Box::new(right),
2137 })
2138 } else {
2139 Ok(left)
2140 }
2141}
2142
2143fn parse_filter_additive_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2144 let mut inner = pair.into_inner();
2145 let mut left =
2146 parse_filter_multiplicative_expr(inner.expect_next("additive expression operand")?)?;
2147
2148 while let Some(op_pair) = inner.next() {
2149 let op = if op_pair.as_str() == "-" {
2150 BinOp::Sub
2151 } else {
2152 BinOp::Add
2153 };
2154 if let Some(right_pair) = inner.next() {
2155 let right = parse_filter_multiplicative_expr(right_pair)?;
2156 left = Expr::Binary {
2157 op,
2158 left: Box::new(left),
2159 right: Box::new(right),
2160 };
2161 }
2162 }
2163 Ok(left)
2164}
2165
2166fn parse_filter_multiplicative_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2167 let mut inner = pair.into_inner();
2168 let mut left =
2169 parse_filter_unary_expr(inner.expect_next("multiplicative expression operand")?)?;
2170
2171 while let Some(op_pair) = inner.next() {
2172 let op = match op_pair.as_str() {
2173 "*" => BinOp::Mul,
2174 "/" => BinOp::Div,
2175 "%" => BinOp::Mod,
2176 _ => BinOp::Mul,
2177 };
2178 if let Some(right_pair) = inner.next() {
2179 let right = parse_filter_unary_expr(right_pair)?;
2180 left = Expr::Binary {
2181 op,
2182 left: Box::new(left),
2183 right: Box::new(right),
2184 };
2185 }
2186 }
2187 Ok(left)
2188}
2189
2190fn parse_filter_unary_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2191 let mut inner = pair.into_inner();
2192 let first = inner.expect_next("unary operator or expression")?;
2193
2194 if first.as_rule() == Rule::filter_unary_op {
2196 let op_str = first.as_str();
2197 let expr =
2198 parse_filter_postfix_expr(inner.expect_next("expression after unary operator")?)?;
2199 let op = match op_str {
2200 "-" => UnaryOp::Neg,
2201 "~" => UnaryOp::BitNot,
2202 _ => unreachable!("Grammar only allows - or ~"),
2203 };
2204 Ok(Expr::Unary {
2205 op,
2206 expr: Box::new(expr),
2207 })
2208 } else {
2209 parse_filter_postfix_expr(first)
2210 }
2211}
2212
2213fn parse_filter_postfix_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2214 let mut inner = pair.into_inner();
2215 let mut expr = parse_filter_primary_expr(inner.expect_next("postfix expression base")?)?;
2216
2217 for suffix in inner {
2218 expr = parse_filter_postfix_suffix(expr, suffix)?;
2219 }
2220 Ok(expr)
2221}
2222
2223fn parse_filter_postfix_suffix(expr: Expr, pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2224 let mut inner = pair.into_inner();
2225
2226 if let Some(first) = inner.next() {
2227 match first.as_rule() {
2228 Rule::identifier => {
2229 Ok(Expr::Member {
2231 expr: Box::new(expr),
2232 member: first.as_str().to_string(),
2233 })
2234 }
2235 Rule::optional_member_access => {
2236 let member = first
2237 .into_inner()
2238 .expect_next("member name")?
2239 .as_str()
2240 .to_string();
2241 Ok(Expr::OptionalMember {
2242 expr: Box::new(expr),
2243 member,
2244 })
2245 }
2246 Rule::index_access => {
2247 let index = parse_expr(first.into_inner().expect_next("index expression")?)?;
2248 Ok(Expr::Index {
2249 expr: Box::new(expr),
2250 index: Box::new(index),
2251 })
2252 }
2253 Rule::call_args => {
2254 let args = first
2255 .into_inner()
2256 .filter(|p| p.as_rule() == Rule::arg_list)
2257 .flat_map(|p| p.into_inner())
2258 .map(parse_arg)
2259 .collect::<ParseResult<Vec<_>>>()?;
2260 Ok(Expr::Call {
2261 func: Box::new(expr),
2262 args,
2263 })
2264 }
2265 _ => Ok(expr),
2266 }
2267 } else {
2268 Ok(expr)
2269 }
2270}
2271
2272fn parse_filter_primary_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2273 let inner = pair.into_inner().expect_next("filter primary expression")?;
2274
2275 match inner.as_rule() {
2276 Rule::literal => parse_literal(inner),
2277 Rule::identifier => Ok(Expr::Ident(inner.as_str().to_string())),
2278 Rule::filter_expr => parse_filter_expr(inner),
2279 _ => Ok(Expr::Ident(inner.as_str().to_string())),
2280 }
2281}
2282
2283fn parse_range_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2284 let mut inner = pair.into_inner();
2285 let left = parse_expr_inner(inner.expect_next("range start")?)?;
2286
2287 if let Some(op_pair) = inner.next() {
2288 let inclusive = op_pair.as_str() == "..=";
2289 let right = parse_expr_inner(inner.expect_next("range end")?)?;
2290 Ok(Expr::Range {
2291 start: Box::new(left),
2292 end: Box::new(right),
2293 inclusive,
2294 })
2295 } else {
2296 Ok(left)
2297 }
2298}
2299
2300fn parse_or_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2301 let mut inner = pair.into_inner();
2302 let mut left = parse_expr_inner(inner.expect_next("or expression operand")?)?;
2303
2304 for right_pair in inner {
2305 let right = parse_expr_inner(right_pair)?;
2306 left = Expr::Binary {
2307 op: BinOp::Or,
2308 left: Box::new(left),
2309 right: Box::new(right),
2310 };
2311 }
2312
2313 Ok(left)
2314}
2315
2316fn parse_and_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2317 let mut inner = pair.into_inner();
2318 let mut left = parse_expr_inner(inner.expect_next("and expression operand")?)?;
2319
2320 for right_pair in inner {
2321 let right = parse_expr_inner(right_pair)?;
2322 left = Expr::Binary {
2323 op: BinOp::And,
2324 left: Box::new(left),
2325 right: Box::new(right),
2326 };
2327 }
2328
2329 Ok(left)
2330}
2331
2332fn parse_not_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2333 let mut inner = pair.into_inner();
2334 let first = inner.expect_next("not keyword or expression")?;
2335
2336 if first.as_str() == "not" {
2337 let expr = parse_expr_inner(inner.expect_next("expression after not")?)?;
2338 Ok(Expr::Unary {
2339 op: UnaryOp::Not,
2340 expr: Box::new(expr),
2341 })
2342 } else {
2343 parse_expr_inner(first)
2344 }
2345}
2346
2347fn parse_comparison_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2348 let mut inner = pair.into_inner();
2349 let left = parse_expr_inner(inner.expect_next("comparison left operand")?)?;
2350
2351 if let Some(op_pair) = inner.next() {
2352 let op_str = op_pair.as_str();
2353 let op = match op_str {
2354 "==" => BinOp::Eq,
2355 "!=" => BinOp::NotEq,
2356 "<" => BinOp::Lt,
2357 "<=" => BinOp::Le,
2358 ">" => BinOp::Gt,
2359 ">=" => BinOp::Ge,
2360 "in" => BinOp::In,
2361 "is" => BinOp::Is,
2362 s if s.contains("not") && s.contains("in") => BinOp::NotIn,
2363 _ => BinOp::Eq,
2364 };
2365 let right = parse_expr_inner(inner.expect_next("comparison right operand")?)?;
2366 Ok(Expr::Binary {
2367 op,
2368 left: Box::new(left),
2369 right: Box::new(right),
2370 })
2371 } else {
2372 Ok(left)
2373 }
2374}
2375
2376fn parse_bitwise_or_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2377 parse_binary_chain(pair, BinOp::BitOr)
2378}
2379
2380fn parse_bitwise_xor_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2381 parse_binary_chain(pair, BinOp::BitXor)
2382}
2383
2384fn parse_bitwise_and_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2385 parse_binary_chain(pair, BinOp::BitAnd)
2386}
2387
2388fn parse_shift_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2389 let mut inner = pair.into_inner();
2390 let mut left = parse_expr_inner(inner.expect_next("shift expression operand")?)?;
2391
2392 while let Some(op_or_expr) = inner.next() {
2393 let op = match op_or_expr.as_str() {
2394 "<<" => BinOp::Shl,
2395 ">>" => BinOp::Shr,
2396 _ => {
2397 let right = parse_expr_inner(op_or_expr)?;
2398 left = Expr::Binary {
2399 op: BinOp::Shl,
2400 left: Box::new(left),
2401 right: Box::new(right),
2402 };
2403 continue;
2404 }
2405 };
2406 if let Some(right_pair) = inner.next() {
2407 let right = parse_expr_inner(right_pair)?;
2408 left = Expr::Binary {
2409 op,
2410 left: Box::new(left),
2411 right: Box::new(right),
2412 };
2413 }
2414 }
2415
2416 Ok(left)
2417}
2418
2419fn parse_additive_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2420 let mut inner = pair.into_inner();
2421 let mut left = parse_expr_inner(inner.expect_next("additive expression operand")?)?;
2422
2423 while let Some(op_pair) = inner.next() {
2424 let op_text = op_pair.as_str();
2425 let op = if op_text == "-" {
2426 BinOp::Sub
2427 } else {
2428 BinOp::Add
2429 };
2430
2431 if let Some(right_pair) = inner.next() {
2432 let right = parse_expr_inner(right_pair)?;
2433 left = Expr::Binary {
2434 op,
2435 left: Box::new(left),
2436 right: Box::new(right),
2437 };
2438 }
2439 }
2440
2441 Ok(left)
2442}
2443
2444fn parse_multiplicative_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2445 let mut inner = pair.into_inner();
2446 let mut left = parse_expr_inner(inner.expect_next("multiplicative expression operand")?)?;
2447
2448 while let Some(op_pair) = inner.next() {
2449 let op_text = op_pair.as_str();
2450 let op = match op_text {
2451 "*" => BinOp::Mul,
2452 "/" => BinOp::Div,
2453 "%" => BinOp::Mod,
2454 _ => BinOp::Mul,
2455 };
2456
2457 if let Some(right_pair) = inner.next() {
2458 let right = parse_expr_inner(right_pair)?;
2459 left = Expr::Binary {
2460 op,
2461 left: Box::new(left),
2462 right: Box::new(right),
2463 };
2464 }
2465 }
2466
2467 Ok(left)
2468}
2469
2470fn parse_power_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2471 let mut inner = pair.into_inner();
2472 let base = parse_expr_inner(inner.expect_next("power expression base")?)?;
2473
2474 if let Some(exp_pair) = inner.next() {
2475 let exp = parse_expr_inner(exp_pair)?;
2476 Ok(Expr::Binary {
2477 op: BinOp::Pow,
2478 left: Box::new(base),
2479 right: Box::new(exp),
2480 })
2481 } else {
2482 Ok(base)
2483 }
2484}
2485
2486fn parse_unary_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2487 let mut inner = pair.into_inner();
2488 let first = inner.expect_next("unary operator or expression")?;
2489
2490 match first.as_rule() {
2492 Rule::unary_op => {
2493 let op_str = first.as_str();
2494 let expr = parse_expr_inner(inner.expect_next("expression after unary operator")?)?;
2495 let op = match op_str {
2496 "-" => UnaryOp::Neg,
2497 "~" => UnaryOp::BitNot,
2498 _ => unreachable!("Grammar only allows - or ~"),
2499 };
2500 Ok(Expr::Unary {
2501 op,
2502 expr: Box::new(expr),
2503 })
2504 }
2505 _ => parse_expr_inner(first),
2506 }
2507}
2508
2509fn parse_postfix_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2510 let mut inner = pair.into_inner();
2511 let mut expr = parse_expr_inner(inner.expect_next("postfix expression base")?)?;
2512
2513 for suffix in inner {
2514 expr = parse_postfix_suffix(expr, suffix)?;
2515 }
2516
2517 Ok(expr)
2518}
2519
2520fn parse_postfix_suffix(expr: Expr, pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2521 let inner = pair.into_inner().expect_next("postfix suffix")?;
2522
2523 match inner.as_rule() {
2524 Rule::member_access => {
2525 let member = inner
2526 .into_inner()
2527 .expect_next("member name")?
2528 .as_str()
2529 .to_string();
2530 Ok(Expr::Member {
2531 expr: Box::new(expr),
2532 member,
2533 })
2534 }
2535 Rule::optional_member_access => {
2536 let member = inner
2537 .into_inner()
2538 .expect_next("member name")?
2539 .as_str()
2540 .to_string();
2541 Ok(Expr::OptionalMember {
2542 expr: Box::new(expr),
2543 member,
2544 })
2545 }
2546 Rule::slice_access => {
2547 let slice_range = inner.into_inner().expect_next("slice range")?;
2549 let slice_inner = slice_range.into_inner();
2550
2551 let mut start = None;
2552 let mut end = None;
2553
2554 for p in slice_inner {
2555 match p.as_rule() {
2556 Rule::slice_start => {
2557 start = Some(Box::new(parse_expr_inner(
2558 p.into_inner().expect_next("slice start expression")?,
2559 )?));
2560 }
2561 Rule::slice_end => {
2562 end = Some(Box::new(parse_expr_inner(
2563 p.into_inner().expect_next("slice end expression")?,
2564 )?));
2565 }
2566 _ => {}
2567 }
2568 }
2569
2570 Ok(Expr::Slice {
2571 expr: Box::new(expr),
2572 start,
2573 end,
2574 })
2575 }
2576 Rule::index_access => {
2577 let index = parse_expr(inner.into_inner().expect_next("index expression")?)?;
2578 Ok(Expr::Index {
2579 expr: Box::new(expr),
2580 index: Box::new(index),
2581 })
2582 }
2583 Rule::call_args => {
2584 let mut args = Vec::new();
2585 for p in inner.into_inner() {
2586 if p.as_rule() == Rule::arg_list {
2587 for arg in p.into_inner() {
2588 args.push(parse_arg(arg)?);
2589 }
2590 }
2591 }
2592 Ok(Expr::Call {
2593 func: Box::new(expr),
2594 args,
2595 })
2596 }
2597 _ => Ok(expr),
2598 }
2599}
2600
2601fn parse_arg(pair: pest::iterators::Pair<Rule>) -> ParseResult<Arg> {
2602 let mut inner = pair.into_inner();
2603 let first = inner.expect_next("argument")?;
2604
2605 if let Some(second) = inner.next() {
2606 Ok(Arg::Named(
2607 first.as_str().to_string(),
2608 parse_expr_inner(second)?,
2609 ))
2610 } else {
2611 Ok(Arg::Positional(parse_expr_inner(first)?))
2612 }
2613}
2614
2615fn parse_primary_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2616 let inner = pair.into_inner().expect_next("primary expression")?;
2617
2618 match inner.as_rule() {
2619 Rule::if_expr => parse_if_expr(inner),
2620 Rule::literal => parse_literal(inner),
2621 Rule::identifier => Ok(Expr::Ident(inner.as_str().to_string())),
2622 Rule::array_literal => parse_array_literal(inner),
2623 Rule::map_literal => parse_map_literal(inner),
2624 Rule::expr => parse_expr(inner),
2625 _ => Ok(Expr::Ident(inner.as_str().to_string())),
2626 }
2627}
2628
2629fn parse_if_expr(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2630 let mut inner = pair.into_inner();
2631 let cond = parse_expr_inner(inner.expect_next("if condition")?)?;
2632 let then_branch = parse_expr_inner(inner.expect_next("then branch")?)?;
2633 let else_branch = parse_expr_inner(inner.expect_next("else branch")?)?;
2634
2635 Ok(Expr::If {
2636 cond: Box::new(cond),
2637 then_branch: Box::new(then_branch),
2638 else_branch: Box::new(else_branch),
2639 })
2640}
2641
2642fn parse_literal(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2643 let inner = pair.into_inner().expect_next("literal value")?;
2644
2645 match inner.as_rule() {
2646 Rule::integer => inner
2647 .as_str()
2648 .parse::<i64>()
2649 .map(Expr::Int)
2650 .map_err(|e| ParseError::InvalidNumber(format!("'{}': {}", inner.as_str(), e))),
2651 Rule::float => inner
2652 .as_str()
2653 .parse::<f64>()
2654 .map(Expr::Float)
2655 .map_err(|e| ParseError::InvalidNumber(format!("'{}': {}", inner.as_str(), e))),
2656 Rule::string => {
2657 let s = inner.as_str();
2658 Ok(Expr::Str(s[1..s.len() - 1].to_string()))
2659 }
2660 Rule::duration => Ok(Expr::Duration(
2661 parse_duration(inner.as_str()).map_err(ParseError::InvalidDuration)?,
2662 )),
2663 Rule::timestamp => Ok(Expr::Timestamp(parse_timestamp(inner.as_str()))),
2664 Rule::boolean => Ok(Expr::Bool(inner.as_str() == "true")),
2665 Rule::null => Ok(Expr::Null),
2666 _ => Ok(Expr::Null),
2667 }
2668}
2669
2670fn parse_array_literal(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2671 let mut items = Vec::new();
2672 for p in pair.into_inner() {
2673 if p.as_rule() == Rule::expr_list {
2674 for expr in p.into_inner() {
2675 items.push(parse_expr(expr)?);
2676 }
2677 }
2678 }
2679 Ok(Expr::Array(items))
2680}
2681
2682fn parse_map_literal(pair: pest::iterators::Pair<Rule>) -> ParseResult<Expr> {
2683 let mut entries = Vec::new();
2684 for p in pair.into_inner() {
2685 if p.as_rule() == Rule::map_entry_list {
2686 for entry in p.into_inner() {
2687 let mut inner = entry.into_inner();
2688 let key = inner.expect_next("map key")?.as_str().to_string();
2689 let key = if key.starts_with('"') {
2690 key[1..key.len() - 1].to_string()
2691 } else {
2692 key
2693 };
2694 let value = parse_expr(inner.expect_next("map value")?)?;
2695 entries.push((key, value));
2696 }
2697 }
2698 }
2699 Ok(Expr::Map(entries))
2700}
2701
2702fn parse_binary_chain(pair: pest::iterators::Pair<Rule>, op: BinOp) -> ParseResult<Expr> {
2703 let mut inner = pair.into_inner();
2704 let mut left = parse_expr_inner(inner.expect_next("binary chain operand")?)?;
2705
2706 for right_pair in inner {
2707 let right = parse_expr_inner(right_pair)?;
2708 left = Expr::Binary {
2709 op,
2710 left: Box::new(left),
2711 right: Box::new(right),
2712 };
2713 }
2714
2715 Ok(left)
2716}
2717
2718#[cfg(test)]
2719mod tests {
2720 use super::*;
2721
2722 #[test]
2723 fn test_parse_simple_stream() {
2724 let result = parse("stream output = input");
2725 assert!(result.is_ok(), "Failed: {:?}", result.err());
2726 }
2727
2728 #[test]
2729 fn test_parse_stream_with_filter() {
2730 let result = parse("stream output = input.where(value > 100)");
2731 assert!(result.is_ok(), "Failed: {:?}", result.err());
2732 }
2733
2734 #[test]
2735 fn test_parse_stream_with_map() {
2736 let result = parse("stream output = input.map(x * 2)");
2737 assert!(result.is_ok(), "Failed: {:?}", result.err());
2738 }
2739
2740 #[test]
2741 fn test_parse_event_declaration() {
2742 let result = parse("event SensorReading:\n sensor_id: str\n value: float");
2743 assert!(result.is_ok(), "Failed: {:?}", result.err());
2744 }
2745
2746 #[test]
2747 fn test_parse_variable() {
2748 let result = parse("let x = 42");
2749 assert!(result.is_ok(), "Failed: {:?}", result.err());
2750 }
2751
2752 #[test]
2753 fn test_parse_function() {
2754 let result = parse("fn add(a: int, b: int) -> int:\n return a + b");
2755 assert!(result.is_ok(), "Failed: {:?}", result.err());
2756 }
2757
2758 #[test]
2759 fn test_parse_lambda() {
2760 let result = parse("let f = (x) => x * 2");
2761 assert!(result.is_ok(), "Failed: {:?}", result.err());
2762 }
2763
2764 #[test]
2765 fn test_parse_if_expression() {
2766 let result = parse("let x = if a > b then a else b");
2767 assert!(result.is_ok(), "Failed: {:?}", result.err());
2768 }
2769
2770 #[test]
2771 fn test_parse_followed_by() {
2772 let result = parse("stream alerts = orders.where(amount > 1000) -> Payment where payment.order_id == orders.id");
2773 assert!(result.is_ok(), "Failed: {:?}", result.err());
2774 }
2775
2776 #[test]
2777 fn test_parse_window() {
2778 let result = parse("stream windowed = input.window(5s)");
2779 assert!(result.is_ok(), "Failed: {:?}", result.err());
2780 }
2781
2782 #[test]
2783 fn test_parse_aggregate() {
2784 let result =
2785 parse("stream stats = input.window(1m).aggregate(count: count(), avg: avg(value))");
2786 assert!(result.is_ok(), "Failed: {:?}", result.err());
2787 }
2788
2789 #[test]
2790 fn test_parse_merge() {
2791 let result = parse("stream combined = merge(stream1, stream2)");
2792 assert!(result.is_ok(), "Failed: {:?}", result.err());
2793 }
2794
2795 #[test]
2796 fn test_parse_sequence() {
2797 let result = parse("stream seq = sequence(a: EventA, b: EventB where b.id == a.id)");
2798 assert!(result.is_ok(), "Failed: {:?}", result.err());
2799 }
2800
2801 #[test]
2802 fn test_parse_config() {
2803 let result = parse("config:\n window_size: 5s\n batch_size: 100");
2804 assert!(result.is_ok(), "Failed: {:?}", result.err());
2805 }
2806
2807 #[test]
2808 fn test_parse_complex_expression() {
2809 let result = parse("let x = (a + b) * c / d - e");
2810 assert!(result.is_ok(), "Failed: {:?}", result.err());
2811 }
2812
2813 #[test]
2814 fn test_parse_sliding_window() {
2815 let result = parse("stream output = input.window(5m, sliding: 1m)");
2816 assert!(result.is_ok(), "Failed: {:?}", result.err());
2817 }
2818
2819 #[test]
2820 fn test_parse_fork_construct() {
2821 let result =
2822 parse("stream forked = input.fork(branch1: .where(x > 0), branch2: .where(x < 0))");
2823 assert!(result.is_ok(), "Failed: {:?}", result.err());
2824 }
2825
2826 #[test]
2827 fn test_parse_aggregate_functions() {
2828 let result = parse(
2829 "stream stats = input.window(1h).aggregate(total: sum(value), average: avg(value))",
2830 );
2831 assert!(result.is_ok(), "Failed: {:?}", result.err());
2832 }
2833
2834 #[test]
2835 fn test_parse_complex_parentheses() {
2836 let result = parse("let x = ((a + b) * (c - d)) / e");
2837 assert!(result.is_ok(), "Failed: {:?}", result.err());
2838 }
2839
2840 #[test]
2841 fn test_parse_sequence_with_alias() {
2842 let result = parse(
2843 r#"
2844 stream TwoTicks = StockTick as first
2845 -> StockTick as second
2846 .emit(result: "two_ticks")
2847 "#,
2848 );
2849 assert!(result.is_ok(), "Failed: {:?}", result.err());
2850 }
2851
2852 #[test]
2853 fn test_parse_followed_by_with_alias() {
2854 let result = parse("stream alerts = Order as a -> Payment as b");
2855 assert!(result.is_ok(), "Failed: {:?}", result.err());
2856 }
2857
2858 #[test]
2859 fn test_parse_followed_by_with_filter_and_alias() {
2860 let result = parse(
2862 r#"
2863 stream Test = A as a
2864 -> B where value == a.base + 10 as b
2865 .emit(status: "matched")
2866 "#,
2867 );
2868 assert!(result.is_ok(), "Failed: {:?}", result.err());
2869 }
2870
2871 #[test]
2872 fn test_parse_pattern_with_lambda() {
2873 let result =
2874 parse("stream Test = Trade.window(1m).pattern(p: x => x.len() > 3).emit(alert: true)");
2875 assert!(result.is_ok(), "Failed: {:?}", result.err());
2876 }
2877
2878 #[test]
2879 fn test_parse_sase_pattern_decl_simple() {
2880 let result = parse("pattern SimpleAlert = SEQ(Login, Transaction)");
2881 assert!(result.is_ok(), "Failed: {:?}", result.err());
2882 }
2883
2884 #[test]
2885 fn test_parse_sase_pattern_decl_with_kleene() {
2886 let result = parse("pattern MultiTx = SEQ(Login, Transaction+ where amount > 1000)");
2887 assert!(result.is_ok(), "Failed: {:?}", result.err());
2888 }
2889
2890 #[test]
2891 fn test_parse_sase_pattern_decl_with_alias() {
2892 let result = parse("pattern AliasedPattern = SEQ(Login as login, Transaction as tx)");
2893 assert!(result.is_ok(), "Failed: {:?}", result.err());
2894 }
2895
2896 #[test]
2897 fn test_parse_sase_pattern_decl_with_within() {
2898 let result = parse("pattern TimedPattern = SEQ(A, B) within 10m");
2899 assert!(result.is_ok(), "Failed: {:?}", result.err());
2900 }
2901
2902 #[test]
2903 fn test_parse_sase_pattern_decl_with_partition() {
2904 let result = parse("pattern PartitionedPattern = SEQ(A, B) partition by user_id");
2905 assert!(result.is_ok(), "Failed: {:?}", result.err());
2906 }
2907
2908 #[test]
2909 fn test_parse_sase_pattern_decl_full() {
2910 let result = parse(
2912 "pattern SuspiciousActivity = SEQ(Transaction+ where amount > 1000 as txs) within 10m partition by user_id"
2913 );
2914 assert!(result.is_ok(), "Failed: {:?}", result.err());
2915 }
2916
2917 #[test]
2918 fn test_parse_sase_pattern_decl_or() {
2919 let result = parse("pattern AlertOrWarn = Login OR Logout");
2920 assert!(result.is_ok(), "Failed: {:?}", result.err());
2921 }
2922
2923 #[test]
2924 fn test_parse_sase_pattern_decl_and() {
2925 let result = parse("pattern BothEvents = Login AND Transaction");
2926 assert!(result.is_ok(), "Failed: {:?}", result.err());
2927 }
2928
2929 #[test]
2930 fn test_parse_sase_pattern_decl_not() {
2931 let result = parse("pattern NoLogout = SEQ(Login, NOT Logout, Transaction)");
2932 assert!(result.is_ok(), "Failed: {:?}", result.err());
2933 }
2934
2935 #[test]
2936 fn test_parse_having() {
2937 let result = parse(
2938 "stream filtered = input.window(1m).aggregate(count: count(), total: sum(value)).having(count > 10)",
2939 );
2940 assert!(result.is_ok(), "Failed: {:?}", result.err());
2941 }
2942
2943 #[test]
2944 fn test_parse_having_with_partition() {
2945 let result = parse(
2946 "stream grouped = input.partition_by(category).window(5m).aggregate(avg_price: avg(price)).having(avg_price > 100.0)",
2947 );
2948 assert!(result.is_ok(), "Failed: {:?}", result.err());
2949 }
2950
2951 #[test]
2952 fn test_parse_timer_source() {
2953 let result = parse("stream heartbeat = timer(5s).emit(type: \"heartbeat\")");
2954 assert!(result.is_ok(), "Failed: {:?}", result.err());
2955 }
2956
2957 #[test]
2958 fn test_parse_timer_source_with_initial_delay() {
2959 let result =
2960 parse("stream delayed_timer = timer(1m, initial_delay: 10s).emit(type: \"periodic\")");
2961 assert!(result.is_ok(), "Failed: {:?}", result.err());
2962 }
2963
2964 #[test]
2965 fn test_parse_var_decl() {
2966 let result = parse("var threshold: float = 10.0");
2967 assert!(result.is_ok(), "Failed: {:?}", result.err());
2968 }
2969
2970 #[test]
2971 fn test_parse_let_decl() {
2972 let result = parse("let max_count: int = 100");
2973 assert!(result.is_ok(), "Failed: {:?}", result.err());
2974 }
2975
2976 #[test]
2977 fn test_parse_assignment() {
2978 let result = parse("threshold := threshold + 10.0");
2979 assert!(result.is_ok(), "Failed: {:?}", result.err());
2980 }
2981
2982 #[test]
2983 fn test_parse_assignment_with_expression() {
2984 let result = parse("count := count * 2 + offset");
2985 assert!(result.is_ok(), "Failed: {:?}", result.err());
2986 }
2987
2988 #[test]
2989 fn test_parse_nested_stream_reference() {
2990 let result = parse("stream Base = Event\nstream Derived = Base.where(x > 0)");
2991 assert!(result.is_ok(), "Failed: {:?}", result.err());
2992 }
2993
2994 #[test]
2995 fn test_parse_multi_stage_pipeline() {
2996 let result = parse(
2997 "stream L1 = Raw\nstream L2 = L1.where(a > 1)\nstream L3 = L2.window(5).aggregate(cnt: count())",
2998 );
2999 assert!(result.is_ok(), "Failed: {:?}", result.err());
3000 }
3001
3002 #[test]
3003 fn test_parse_stream_with_operations_chain() {
3004 let result = parse(
3005 "stream Processed = Source.where(valid).window(10).aggregate(sum: sum(value)).having(sum > 100)",
3006 );
3007 assert!(result.is_ok(), "Failed: {:?}", result.err());
3008 }
3009
3010 #[test]
3015 fn test_parse_connector_mqtt() {
3016 let result = parse(
3017 r#"connector MqttBroker = mqtt (
3018 host: "localhost",
3019 port: 1883,
3020 client_id: "varpulis"
3021 )"#,
3022 );
3023 assert!(result.is_ok(), "Failed: {:?}", result.err());
3024 }
3025
3026 #[test]
3027 fn test_parse_connector_kafka() {
3028 let result = parse(
3029 r#"connector KafkaCluster = kafka (
3030 brokers: ["kafka1:9092", "kafka2:9092"],
3031 group_id: "my-group"
3032 )"#,
3033 );
3034 assert!(result.is_ok(), "Failed: {:?}", result.err());
3035 }
3036
3037 #[test]
3038 fn test_parse_connector_http() {
3039 let result = parse(
3040 r#"connector ApiEndpoint = http (
3041 base_url: "https://api.example.com"
3042 )"#,
3043 );
3044 assert!(result.is_ok(), "Failed: {:?}", result.err());
3045 }
3046
3047 #[test]
3048 fn test_parse_stream_with_from_connector() {
3049 let result = parse(
3050 r#"stream Temperatures = TemperatureReading.from(MqttSensors, topic: "sensors/temp/#")"#,
3051 );
3052 assert!(result.is_ok(), "Failed: {:?}", result.err());
3053 }
3054
3055 #[test]
3056 fn test_parse_stream_with_from_and_operations() {
3057 let result = parse(
3058 r#"stream HighTemp = TemperatureReading
3059 .from(MqttSensors, topic: "sensors/#")
3060 .where(value > 30)
3061 .emit(alert: "high_temp")"#,
3062 );
3063 assert!(result.is_ok(), "Failed: {:?}", result.err());
3064 }
3065
3066 #[test]
3067 fn test_parse_full_connectivity_pipeline() {
3068 let result = parse(
3069 r#"
3070 connector MqttSensors = mqtt (host: "localhost", port: 1883)
3071 connector KafkaAlerts = kafka (brokers: ["kafka:9092"])
3072
3073 event TemperatureReading:
3074 sensor_id: str
3075 value: float
3076 ts: timestamp
3077
3078 stream Temperatures = TemperatureReading.from(MqttSensors, topic: "sensors/#")
3079
3080 stream HighTempAlert = Temperatures
3081 .where(value > 30)
3082 .emit(alert_type: "HIGH_TEMP", temperature: value)
3083
3084 "#,
3085 );
3086 assert!(result.is_ok(), "Failed: {:?}", result.err());
3087 }
3088
3089 #[test]
3090 fn test_parse_emit_as_type() {
3091 let result = parse(
3092 r#"stream Alerts = Temperatures
3093 .where(value > 30)
3094 .emit as AlertEvent(severity: "high", temp: value)"#,
3095 );
3096 assert!(result.is_ok(), "Failed: {:?}", result.err());
3097 }
3098
3099 #[test]
3100 fn test_parse_stream_with_to_connector() {
3101 let result = parse(
3102 r#"stream Output = Input
3103 .where(x > 0)
3104 .emit(y: x * 2)
3105 .to(KafkaOutput, topic: "output")"#,
3106 );
3107 assert!(result.is_ok(), "Failed: {:?}", result.err());
3108 }
3109
3110 #[test]
3111 fn test_emit_stmt_parses() {
3112 let result = parse(
3113 r"fn test():
3114 emit Pixel(x: 1, y: 2)",
3115 );
3116 assert!(result.is_ok(), "Failed: {:?}", result.err());
3117 let program = result.unwrap();
3118 if let Stmt::FnDecl { body, .. } = &program.statements[0].node {
3120 match &body[0].node {
3121 Stmt::Emit { event_type, fields } => {
3122 assert_eq!(event_type, "Pixel");
3123 assert_eq!(fields.len(), 2);
3124 assert_eq!(fields[0].name, "x");
3125 assert_eq!(fields[1].name, "y");
3126 }
3127 other => panic!("Expected Stmt::Emit, got {other:?}"),
3128 }
3129 } else {
3130 panic!("Expected FnDecl");
3131 }
3132 }
3133
3134 #[test]
3135 fn test_emit_stmt_no_args() {
3136 let result = parse(
3137 r"fn test():
3138 emit Done()",
3139 );
3140 assert!(result.is_ok(), "Failed: {:?}", result.err());
3141 let program = result.unwrap();
3142 if let Stmt::FnDecl { body, .. } = &program.statements[0].node {
3143 match &body[0].node {
3144 Stmt::Emit { event_type, fields } => {
3145 assert_eq!(event_type, "Done");
3146 assert!(fields.is_empty());
3147 }
3148 other => panic!("Expected Stmt::Emit, got {other:?}"),
3149 }
3150 } else {
3151 panic!("Expected FnDecl");
3152 }
3153 }
3154
3155 #[test]
3156 fn test_emit_in_function_with_for_loop() {
3157 let result = parse(
3158 r"fn generate(n: int):
3159 for i in 0..n:
3160 emit Item(index: i, value: i * 2)",
3161 );
3162 assert!(result.is_ok(), "Failed: {:?}", result.err());
3163 }
3164
3165 #[test]
3166 fn test_parse_process_op() {
3167 let result = parse(
3168 r"fn do_work():
3169 emit Result(v: 42)
3170
3171stream S = timer(1s).process(do_work())",
3172 );
3173 assert!(result.is_ok(), "Failed: {:?}", result.err());
3174 }
3175
3176 #[test]
3177 fn test_parse_trend_aggregate_count_trends() {
3178 let result = parse(
3179 r#"stream S = StockTick as first
3180 -> all StockTick where price > first.price as rising
3181 -> StockTick where price < rising.price as drop
3182 .within(60s)
3183 .trend_aggregate(count: count_trends())
3184 .emit(event_type: "TrendStats", trends: count)"#,
3185 );
3186 assert!(result.is_ok(), "Failed: {:?}", result.err());
3187 let program = result.unwrap();
3188 for stmt in &program.statements {
3190 if let Stmt::StreamDecl { ops, .. } = &stmt.node {
3191 let has_trend_agg = ops
3192 .iter()
3193 .any(|op| matches!(op, StreamOp::TrendAggregate(_)));
3194 assert!(has_trend_agg, "Expected TrendAggregate op in stream ops");
3195 for op in ops {
3197 if let StreamOp::TrendAggregate(items) = op {
3198 assert_eq!(items.len(), 1);
3199 assert_eq!(items[0].alias, "count");
3200 assert_eq!(items[0].func, "count_trends");
3201 assert!(items[0].arg.is_none());
3202 }
3203 }
3204 return;
3205 }
3206 }
3207 panic!("No stream declaration found");
3208 }
3209
3210 #[test]
3211 fn test_parse_trend_aggregate_multiple_items() {
3212 let result = parse(
3213 r"stream S = StockTick as first
3214 -> all StockTick as rising
3215 .within(60s)
3216 .trend_aggregate(
3217 trend_count: count_trends(),
3218 event_count: count_events(rising)
3219 )
3220 .emit(trends: trend_count, events: event_count)",
3221 );
3222 assert!(result.is_ok(), "Failed: {:?}", result.err());
3223 let program = result.unwrap();
3224 for stmt in &program.statements {
3225 if let Stmt::StreamDecl { ops, .. } = &stmt.node {
3226 for op in ops {
3227 if let StreamOp::TrendAggregate(items) = op {
3228 assert_eq!(items.len(), 2);
3229 assert_eq!(items[0].alias, "trend_count");
3230 assert_eq!(items[0].func, "count_trends");
3231 assert_eq!(items[1].alias, "event_count");
3232 assert_eq!(items[1].func, "count_events");
3233 assert!(items[1].arg.is_some());
3234 return;
3235 }
3236 }
3237 }
3238 }
3239 panic!("No TrendAggregate found");
3240 }
3241
3242 #[test]
3243 fn test_parse_score_basic() {
3244 let result = parse(
3245 r#"stream S = TradeEvent
3246 .score(model: "models/fraud.onnx", inputs: [amount, risk_score], outputs: [fraud_prob, category])"#,
3247 );
3248 assert!(result.is_ok(), "Failed: {:?}", result.err());
3249 let program = result.unwrap();
3250 for stmt in &program.statements {
3251 if let Stmt::StreamDecl { ops, .. } = &stmt.node {
3252 for op in ops {
3253 if let StreamOp::Score(spec) = op {
3254 assert_eq!(spec.model_path, "models/fraud.onnx");
3255 assert_eq!(spec.inputs, vec!["amount", "risk_score"]);
3256 assert_eq!(spec.outputs, vec!["fraud_prob", "category"]);
3257 return;
3258 }
3259 }
3260 }
3261 }
3262 panic!("No Score op found");
3263 }
3264
3265 #[test]
3266 fn test_parse_score_single_field() {
3267 let result = parse(
3268 r#"stream S = Event
3269 .score(model: "model.onnx", inputs: [value], outputs: [prediction])"#,
3270 );
3271 assert!(result.is_ok(), "Failed: {:?}", result.err());
3272 let program = result.unwrap();
3273 for stmt in &program.statements {
3274 if let Stmt::StreamDecl { ops, .. } = &stmt.node {
3275 for op in ops {
3276 if let StreamOp::Score(spec) = op {
3277 assert_eq!(spec.model_path, "model.onnx");
3278 assert_eq!(spec.inputs, vec!["value"]);
3279 assert_eq!(spec.outputs, vec!["prediction"]);
3280 return;
3281 }
3282 }
3283 }
3284 }
3285 panic!("No Score op found");
3286 }
3287
3288 #[test]
3293 fn fuzz_regression_unmatched_brackets_timeout() {
3294 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";
3296 let start = std::time::Instant::now();
3297 let result = parse(input);
3298 let elapsed = start.elapsed();
3299 assert!(
3300 result.is_err(),
3301 "Should reject deeply nested unmatched brackets"
3302 );
3303 assert!(
3304 elapsed.as_millis() < 100,
3305 "Parser should reject fast, took {elapsed:?}"
3306 );
3307 }
3308
3309 #[test]
3310 fn fuzz_regression_deeply_nested_brackets_slow_unit() {
3311 let input = "stream x[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[";
3313 let start = std::time::Instant::now();
3314 let result = parse(input);
3315 let elapsed = start.elapsed();
3316 assert!(result.is_err(), "Should reject deeply nested brackets");
3317 assert!(
3318 elapsed.as_millis() < 100,
3319 "Parser should reject fast, took {elapsed:?}"
3320 );
3321 }
3322
3323 #[test]
3324 fn nesting_depth_allows_reasonable_programs() {
3325 let input = "let x = foo(bar(baz(qux(a, [1, [2, [3, [4]]]]))))";
3327 let result = parse(input);
3328 if let Err(ref e) = result {
3331 let msg = format!("{e}");
3332 assert!(
3333 !msg.contains("Nesting depth"),
3334 "Should allow 10 levels of nesting: {msg}"
3335 );
3336 }
3337 }
3338
3339 #[test]
3340 fn nesting_depth_ignores_brackets_in_comments() {
3341 let input = "# [[[[[[[[[[[[[[[[[[[[[[[[[[\nstream x = y";
3343 let result = parse(input);
3344 assert!(
3345 result.is_ok(),
3346 "Brackets in comments should be ignored: {:?}",
3347 result.err()
3348 );
3349 }
3350
3351 #[test]
3352 fn nesting_depth_ignores_brackets_in_strings() {
3353 let input = r#"let x = "[[[[[[[[[[[[[[[[[[[[[[[[[[""#;
3355 let result = parse(input);
3356 if let Err(ref e) = result {
3357 let msg = format!("{e}");
3358 assert!(
3359 !msg.contains("Nesting depth"),
3360 "Brackets in strings should be ignored: {msg}"
3361 );
3362 }
3363 }
3364}