1use crate::error::Error;
2use crate::limits::ResourceLimits;
3use crate::parsing::ast::{try_parse_type_constraint_command, *};
4use crate::parsing::lexer::{
5 can_be_label, can_be_reference_segment, conversion_target_from_token, is_boolean_keyword,
6 is_calendar_unit_token, is_duration_unit, is_math_function, is_spec_body_keyword,
7 is_structural_keyword, is_type_keyword, token_kind_to_boolean_value,
8 token_kind_to_calendar_unit, token_kind_to_duration_unit, token_kind_to_primitive, Lexer,
9 Token, TokenKind,
10};
11use crate::parsing::source::Source;
12use rust_decimal::Decimal;
13use std::str::FromStr;
14use std::sync::Arc;
15
16type TypeArrowChain = (ParentType, Option<SpecRef>, Option<Vec<Constraint>>);
17
18pub struct ParseResult {
19 pub specs: Vec<LemmaSpec>,
20 pub expression_count: usize,
21}
22
23pub fn parse(
24 content: &str,
25 attribute: &str,
26 limits: &ResourceLimits,
27) -> Result<ParseResult, Error> {
28 if content.len() > limits.max_file_size_bytes {
29 return Err(Error::resource_limit_exceeded(
30 "max_file_size_bytes",
31 format!(
32 "{} bytes ({} MB)",
33 limits.max_file_size_bytes,
34 limits.max_file_size_bytes / (1024 * 1024)
35 ),
36 format!(
37 "{} bytes ({:.2} MB)",
38 content.len(),
39 content.len() as f64 / (1024.0 * 1024.0)
40 ),
41 "Reduce file size or split into multiple specs",
42 None,
43 None,
44 None,
45 ));
46 }
47
48 let mut parser = Parser::new(content, attribute, limits);
49 let specs = parser.parse_file()?;
50 Ok(ParseResult {
51 specs,
52 expression_count: parser.expression_count,
53 })
54}
55
56struct Parser {
57 lexer: Lexer,
58 depth_tracker: DepthTracker,
59 expression_count: usize,
60 max_expression_count: usize,
61}
62
63impl Parser {
64 fn new(content: &str, attribute: &str, limits: &ResourceLimits) -> Self {
65 Parser {
66 lexer: Lexer::new(content, attribute),
67 depth_tracker: DepthTracker::with_max_depth(limits.max_expression_depth),
68 expression_count: 0,
69 max_expression_count: limits.max_expression_count,
70 }
71 }
72
73 fn attribute(&self) -> String {
74 self.lexer.attribute().to_string()
75 }
76
77 fn peek(&mut self) -> Result<&Token, Error> {
78 self.lexer.peek()
79 }
80
81 fn next(&mut self) -> Result<Token, Error> {
82 self.lexer.next_token()
83 }
84
85 fn at(&mut self, kind: &TokenKind) -> Result<bool, Error> {
86 Ok(&self.peek()?.kind == kind)
87 }
88
89 fn at_any(&mut self, kinds: &[TokenKind]) -> Result<bool, Error> {
90 let current = &self.peek()?.kind;
91 Ok(kinds.contains(current))
92 }
93
94 fn expect(&mut self, kind: &TokenKind) -> Result<Token, Error> {
95 let token = self.next()?;
96 if &token.kind == kind {
97 Ok(token)
98 } else {
99 Err(self.error_at_token(&token, format!("Expected {}, found {}", kind, token.kind)))
100 }
101 }
102
103 fn error_at_token(&self, token: &Token, message: impl Into<String>) -> Error {
104 Error::parsing(
105 message,
106 Source::new(self.lexer.attribute(), token.span.clone()),
107 None::<String>,
108 )
109 }
110
111 fn error_at_token_with_suggestion(
112 &self,
113 token: &Token,
114 message: impl Into<String>,
115 suggestion: impl Into<String>,
116 ) -> Error {
117 Error::parsing(
118 message,
119 Source::new(self.lexer.attribute(), token.span.clone()),
120 Some(suggestion),
121 )
122 }
123
124 fn try_parse_hash_pin(&mut self) -> Result<Option<String>, Error> {
129 if !self.at(&TokenKind::Tilde)? {
130 return Ok(None);
131 }
132 let tilde_span = self.next()?.span;
133 let hash = self.lexer.scan_raw_alphanumeric()?;
134 if hash.len() != 8 {
135 return Err(Error::parsing(
136 format!(
137 "Expected an 8-character alphanumeric plan hash after '~', found '{}'",
138 hash
139 ),
140 self.make_source(tilde_span),
141 None::<String>,
142 ));
143 }
144 Ok(Some(hash))
145 }
146
147 fn make_source(&self, span: Span) -> Source {
148 Source::new(self.lexer.attribute(), span)
149 }
150
151 fn span_from(&self, start: &Span) -> Span {
152 Span {
155 start: start.start,
156 end: start.end.max(start.start),
157 line: start.line,
158 col: start.col,
159 }
160 }
161
162 fn span_covering(&self, start: &Span, end: &Span) -> Span {
163 Span {
164 start: start.start,
165 end: end.end,
166 line: start.line,
167 col: start.col,
168 }
169 }
170
171 fn parse_file(&mut self) -> Result<Vec<LemmaSpec>, Error> {
176 let mut specs = Vec::new();
177 loop {
178 if self.at(&TokenKind::Eof)? {
179 break;
180 }
181 if self.at(&TokenKind::Spec)? {
182 specs.push(self.parse_spec()?);
183 } else {
184 let token = self.next()?;
185 return Err(self.error_at_token_with_suggestion(
186 &token,
187 format!(
188 "Expected a spec declaration (e.g. 'spec my_spec'), found {}",
189 token.kind
190 ),
191 "A Lemma file must start with 'spec <name>'",
192 ));
193 }
194 }
195 Ok(specs)
196 }
197
198 fn parse_spec(&mut self) -> Result<LemmaSpec, Error> {
199 let spec_token = self.expect(&TokenKind::Spec)?;
200 let start_line = spec_token.span.line;
201
202 let (name, _name_span) = self.parse_spec_name()?;
203
204 let effective_from = self.try_parse_effective_from()?;
205
206 let commentary = self.try_parse_commentary()?;
207
208 let attribute = self.attribute();
209 let mut spec = LemmaSpec::new(name.clone())
210 .with_attribute(attribute)
211 .with_start_line(start_line);
212 spec.effective_from = effective_from;
213
214 if let Some(commentary_text) = commentary {
215 spec = spec.set_commentary(commentary_text);
216 }
217
218 let mut facts = Vec::new();
222 let mut rules = Vec::new();
223 let mut types = Vec::new();
224 let mut meta_fields = Vec::new();
225
226 loop {
227 let peek_kind = self.peek()?.kind.clone();
228 match peek_kind {
229 TokenKind::Fact => {
230 let fact = self.parse_fact()?;
231 facts.push(fact);
232 }
233 TokenKind::Rule => {
234 let rule = self.parse_rule()?;
235 rules.push(rule);
236 }
237 TokenKind::Type => {
238 let type_def = self.parse_type_def()?;
239 types.push(type_def);
240 }
241 TokenKind::Meta => {
242 let meta = self.parse_meta()?;
243 meta_fields.push(meta);
244 }
245 TokenKind::Spec | TokenKind::Eof => break,
246 _ => {
247 let token = self.next()?;
248 return Err(self.error_at_token_with_suggestion(
249 &token,
250 format!(
251 "Expected 'fact', 'rule', 'type', 'meta', or a new 'spec', found '{}'",
252 token.text
253 ),
254 "Check the spelling or add the appropriate keyword",
255 ));
256 }
257 }
258 }
259
260 for type_def in types {
261 spec = spec.add_type(type_def);
262 }
263 for fact in facts {
264 spec = spec.add_fact(fact);
265 }
266 for rule in rules {
267 spec = spec.add_rule(rule);
268 }
269 for meta in meta_fields {
270 spec = spec.add_meta_field(meta);
271 }
272
273 Ok(spec)
274 }
275
276 fn parse_spec_name(&mut self) -> Result<(String, Span), Error> {
279 let mut name = String::new();
280 let start_span;
281
282 if self.at(&TokenKind::At)? {
283 let at_tok = self.next()?;
284 start_span = at_tok.span.clone();
285 name.push('@');
286 } else {
287 start_span = self.peek()?.span.clone();
288 }
289
290 let first = self.next()?;
292 if !first.kind.is_identifier_like() {
293 return Err(self.error_at_token(
294 &first,
295 format!("Expected a spec name, found {}", first.kind),
296 ));
297 }
298 name.push_str(&first.text);
299 let mut end_span = first.span.clone();
300
301 while self.at(&TokenKind::Slash)? {
303 self.next()?; name.push('/');
305 let seg = self.next()?;
306 if !seg.kind.is_identifier_like() {
307 return Err(self.error_at_token(
308 &seg,
309 format!(
310 "Expected identifier after '/' in spec name, found {}",
311 seg.kind
312 ),
313 ));
314 }
315 name.push_str(&seg.text);
316 end_span = seg.span.clone();
317 }
318
319 while self.at(&TokenKind::Minus)? {
321 let minus_span = self.peek()?.span.clone();
324 self.next()?; if let Ok(peeked) = self.peek() {
326 if peeked.kind.is_identifier_like() {
327 let seg = self.next()?;
328 name.push('-');
329 name.push_str(&seg.text);
330 end_span = seg.span.clone();
331 while self.at(&TokenKind::Slash)? {
333 self.next()?; name.push('/');
335 let seg2 = self.next()?;
336 if !seg2.kind.is_identifier_like() {
337 return Err(self.error_at_token(
338 &seg2,
339 format!(
340 "Expected identifier after '/' in spec name, found {}",
341 seg2.kind
342 ),
343 ));
344 }
345 name.push_str(&seg2.text);
346 end_span = seg2.span.clone();
347 }
348 } else {
349 let span = self.span_covering(&start_span, &minus_span);
351 return Err(Error::parsing(
352 "Trailing '-' after spec name",
353 self.make_source(span),
354 None::<String>,
355 ));
356 }
357 }
358 }
359
360 let full_span = self.span_covering(&start_span, &end_span);
361 Ok((name, full_span))
362 }
363
364 fn try_parse_effective_from(&mut self) -> Result<Option<DateTimeValue>, Error> {
365 if !self.at(&TokenKind::NumberLit)? {
370 return Ok(None);
371 }
372
373 let peeked = self.peek()?;
374 let peeked_text = peeked.text.clone();
375 let peeked_span = peeked.span.clone();
376
377 if peeked_text.len() == 4 && peeked_text.chars().all(|c| c.is_ascii_digit()) {
379 let mut dt_str = String::new();
381 let num_tok = self.next()?; dt_str.push_str(&num_tok.text);
383
384 while self.at(&TokenKind::Minus)? {
386 self.next()?; dt_str.push('-');
388 let part = self.next()?;
389 dt_str.push_str(&part.text);
390 }
391
392 if self.at(&TokenKind::Identifier)? {
394 let peeked = self.peek()?;
395 if peeked.text.starts_with('T') || peeked.text.starts_with('t') {
396 let time_part = self.next()?;
397 dt_str.push_str(&time_part.text);
398 while self.at(&TokenKind::Colon)? {
400 self.next()?;
401 dt_str.push(':');
402 let part = self.next()?;
403 dt_str.push_str(&part.text);
404 }
405 if self.at(&TokenKind::Plus)? {
407 self.next()?;
408 dt_str.push('+');
409 let tz_part = self.next()?;
410 dt_str.push_str(&tz_part.text);
411 if self.at(&TokenKind::Colon)? {
412 self.next()?;
413 dt_str.push(':');
414 let tz_min = self.next()?;
415 dt_str.push_str(&tz_min.text);
416 }
417 }
418 }
419 }
420
421 if let Ok(dtv) = dt_str.parse::<DateTimeValue>() {
423 return Ok(Some(dtv));
424 }
425
426 return Err(Error::parsing(
427 format!("Invalid date/time in spec declaration: '{}'", dt_str),
428 self.make_source(peeked_span),
429 None::<String>,
430 ));
431 }
432
433 Ok(None)
434 }
435
436 fn try_parse_commentary(&mut self) -> Result<Option<String>, Error> {
437 if !self.at(&TokenKind::Commentary)? {
438 return Ok(None);
439 }
440 let token = self.next()?;
441 let trimmed = token.text.trim().to_string();
442 if trimmed.is_empty() {
443 Ok(None)
444 } else {
445 Ok(Some(trimmed))
446 }
447 }
448
449 fn parse_fact(&mut self) -> Result<LemmaFact, Error> {
454 let fact_token = self.expect(&TokenKind::Fact)?;
455 let start_span = fact_token.span.clone();
456
457 let reference = self.parse_reference()?;
459
460 self.expect(&TokenKind::Colon)?;
461
462 let value = self.parse_fact_value()?;
463
464 let end_span = self.peek()?.span.clone();
465 let span = self.span_covering(&start_span, &end_span);
466 let source = self.make_source(span);
467
468 Ok(LemmaFact::new(reference, value, source))
469 }
470
471 fn parse_reference(&mut self) -> Result<Reference, Error> {
472 let mut segments = Vec::new();
473
474 let first = self.next()?;
475 if is_structural_keyword(&first.kind) {
478 return Err(self.error_at_token_with_suggestion(
479 &first,
480 format!(
481 "'{}' is a reserved keyword and cannot be used as a name",
482 first.text
483 ),
484 "Choose a different name that is not a reserved keyword",
485 ));
486 }
487
488 if !can_be_reference_segment(&first.kind) {
489 return Err(self.error_at_token(
490 &first,
491 format!("Expected an identifier, found {}", first.kind),
492 ));
493 }
494
495 segments.push(first.text.clone());
496
497 while self.at(&TokenKind::Dot)? {
499 self.next()?; let seg = self.next()?;
501 if !can_be_reference_segment(&seg.kind) {
502 return Err(self.error_at_token(
503 &seg,
504 format!("Expected an identifier after '.', found {}", seg.kind),
505 ));
506 }
507 segments.push(seg.text.clone());
508 }
509
510 Ok(Reference::from_path(segments))
511 }
512
513 fn parse_fact_value(&mut self) -> Result<FactValue, Error> {
514 if self.at(&TokenKind::LBracket)? {
516 return self.parse_type_declaration_or_inline();
517 }
518
519 if self.at(&TokenKind::Spec)? {
521 return self.parse_fact_spec_reference();
522 }
523
524 let value = self.parse_literal_value()?;
526 Ok(FactValue::Literal(value))
527 }
528
529 fn parse_type_declaration_or_inline(&mut self) -> Result<FactValue, Error> {
530 self.expect(&TokenKind::LBracket)?;
531
532 let (base, from_spec, constraints) = self.parse_type_arrow_chain()?;
534
535 self.expect(&TokenKind::RBracket)?;
536
537 Ok(FactValue::TypeDeclaration {
538 base,
539 constraints,
540 from: from_spec,
541 })
542 }
543
544 fn parse_fact_spec_reference(&mut self) -> Result<FactValue, Error> {
545 self.expect(&TokenKind::Spec)?;
546
547 let (name, _name_span) = self.parse_spec_name()?;
548 let from_registry = name.starts_with('@');
549
550 let hash_pin = self.try_parse_hash_pin()?;
551
552 let mut effective = None;
553 if self.at(&TokenKind::NumberLit)? {
555 let peeked = self.peek()?;
556 if peeked.text.len() == 4 && peeked.text.chars().all(|c| c.is_ascii_digit()) {
557 effective = self.try_parse_effective_from()?;
559 }
560 }
561
562 Ok(FactValue::SpecReference(SpecRef {
563 name,
564 from_registry,
565 hash_pin,
566 effective,
567 }))
568 }
569
570 fn parse_rule(&mut self) -> Result<LemmaRule, Error> {
575 let rule_token = self.expect(&TokenKind::Rule)?;
576 let start_span = rule_token.span.clone();
577
578 let name_tok = self.next()?;
579 if is_structural_keyword(&name_tok.kind) {
580 return Err(self.error_at_token_with_suggestion(
581 &name_tok,
582 format!(
583 "'{}' is a reserved keyword and cannot be used as a rule name",
584 name_tok.text
585 ),
586 "Choose a different name that is not a reserved keyword",
587 ));
588 }
589 if !can_be_label(&name_tok.kind) && !is_type_keyword(&name_tok.kind) {
590 return Err(self.error_at_token(
591 &name_tok,
592 format!("Expected a rule name, found {}", name_tok.kind),
593 ));
594 }
595 let rule_name = name_tok.text.clone();
596
597 self.expect(&TokenKind::Colon)?;
598
599 let expression = if self.at(&TokenKind::Veto)? {
601 self.parse_veto_expression()?
602 } else {
603 self.parse_expression()?
604 };
605
606 let mut unless_clauses = Vec::new();
608 while self.at(&TokenKind::Unless)? {
609 unless_clauses.push(self.parse_unless_clause()?);
610 }
611
612 let end_span = if let Some(last_unless) = unless_clauses.last() {
613 last_unless.source_location.span.clone()
614 } else if let Some(ref loc) = expression.source_location {
615 loc.span.clone()
616 } else {
617 start_span.clone()
618 };
619
620 let span = self.span_covering(&start_span, &end_span);
621 Ok(LemmaRule {
622 name: rule_name,
623 expression,
624 unless_clauses,
625 source_location: self.make_source(span),
626 })
627 }
628
629 fn parse_veto_expression(&mut self) -> Result<Expression, Error> {
630 let veto_tok = self.expect(&TokenKind::Veto)?;
631 let start_span = veto_tok.span.clone();
632
633 let message = if self.at(&TokenKind::StringLit)? {
634 let str_tok = self.next()?;
635 let content = unquote_string(&str_tok.text);
636 Some(content)
637 } else {
638 None
639 };
640
641 let span = self.span_from(&start_span);
642 self.new_expression(
643 ExpressionKind::Veto(VetoExpression { message }),
644 self.make_source(span),
645 )
646 }
647
648 fn parse_unless_clause(&mut self) -> Result<UnlessClause, Error> {
649 let unless_tok = self.expect(&TokenKind::Unless)?;
650 let start_span = unless_tok.span.clone();
651
652 let condition = self.parse_expression()?;
653
654 self.expect(&TokenKind::Then)?;
655
656 let result = if self.at(&TokenKind::Veto)? {
657 self.parse_veto_expression()?
658 } else {
659 self.parse_expression()?
660 };
661
662 let end_span = result
663 .source_location
664 .as_ref()
665 .map(|s| s.span.clone())
666 .unwrap_or_else(|| start_span.clone());
667 let span = self.span_covering(&start_span, &end_span);
668
669 Ok(UnlessClause {
670 condition,
671 result,
672 source_location: self.make_source(span),
673 })
674 }
675
676 fn parse_type_def(&mut self) -> Result<TypeDef, Error> {
681 let type_tok = self.expect(&TokenKind::Type)?;
682 let start_span = type_tok.span.clone();
683
684 let name_tok = self.next()?;
686 let type_name = name_tok.text.clone();
687
688 if self.at(&TokenKind::From)? {
690 return self.parse_type_import(type_name, start_span);
691 }
692
693 if self.at(&TokenKind::Colon)? {
695 self.next()?; } else {
697 let peek = self.peek()?.clone();
700 return Err(self.error_at_token(
701 &peek,
702 format!(
703 "Expected ':' or 'from' after type name '{}', found {}",
704 type_name, peek.kind
705 ),
706 ));
707 }
708
709 let (parent, _from, constraints) = self.parse_type_arrow_chain()?;
710
711 let end_span = self.peek()?.span.clone();
712 let span = self.span_covering(&start_span, &end_span);
713 Ok(TypeDef::Regular {
714 source_location: self.make_source(span),
715 name: type_name,
716 parent,
717 constraints,
718 })
719 }
720
721 fn parse_type_import(&mut self, type_name: String, start_span: Span) -> Result<TypeDef, Error> {
722 self.expect(&TokenKind::From)?;
723
724 let (from_name, _from_span) = self.parse_spec_name()?;
725 let from_registry = from_name.starts_with('@');
726 let hash_pin = self.try_parse_hash_pin()?;
727
728 let from = SpecRef {
729 name: from_name,
730 from_registry,
731 hash_pin,
732 effective: None,
733 };
734
735 let constraints = if self.at(&TokenKind::Arrow)? {
737 let (_, _, constraints) = self.parse_remaining_arrow_chain()?;
738 constraints
739 } else {
740 None
741 };
742
743 let end_span = self.peek()?.span.clone();
744 let span = self.span_covering(&start_span, &end_span);
745
746 let source_type = type_name.clone();
747
748 Ok(TypeDef::Import {
749 source_location: self.make_source(span),
750 name: type_name,
751 source_type,
752 from,
753 constraints,
754 })
755 }
756
757 fn parse_type_arrow_chain(&mut self) -> Result<TypeArrowChain, Error> {
759 let name_tok = self.next()?;
760 let base = if let Some(kind) = token_kind_to_primitive(&name_tok.kind) {
761 ParentType::Primitive(kind)
762 } else if can_be_label(&name_tok.kind) {
763 ParentType::Custom(name_tok.text.clone())
764 } else {
765 return Err(self.error_at_token(
766 &name_tok,
767 format!("Expected a type name, found {}", name_tok.kind),
768 ));
769 };
770
771 let from_spec = if self.at(&TokenKind::From)? {
773 self.next()?; let (from_name, _) = self.parse_spec_name()?;
775 let from_registry = from_name.starts_with('@');
776 let hash_pin = self.try_parse_hash_pin()?;
777 Some(SpecRef {
778 name: from_name,
779 from_registry,
780 hash_pin,
781 effective: None,
782 })
783 } else {
784 None
785 };
786
787 let mut commands = Vec::new();
789 while self.at(&TokenKind::Arrow)? {
790 self.next()?; let (cmd, cmd_args) = self.parse_command()?;
792 commands.push((cmd, cmd_args));
793 }
794
795 let constraints = if commands.is_empty() {
796 None
797 } else {
798 Some(commands)
799 };
800
801 Ok((base, from_spec, constraints))
802 }
803
804 fn parse_remaining_arrow_chain(&mut self) -> Result<TypeArrowChain, Error> {
805 let mut commands = Vec::new();
806 while self.at(&TokenKind::Arrow)? {
807 self.next()?; let (cmd, cmd_args) = self.parse_command()?;
809 commands.push((cmd, cmd_args));
810 }
811 let constraints = if commands.is_empty() {
812 None
813 } else {
814 Some(commands)
815 };
816 Ok((ParentType::Custom(String::new()), None, constraints))
817 }
818
819 fn parse_command(&mut self) -> Result<(TypeConstraintCommand, Vec<CommandArg>), Error> {
820 let name_tok = self.next()?;
821 if !can_be_label(&name_tok.kind) && !is_type_keyword(&name_tok.kind) {
822 return Err(self.error_at_token(
823 &name_tok,
824 format!("Expected a command name, found {}", name_tok.kind),
825 ));
826 }
827 let cmd = try_parse_type_constraint_command(&name_tok.text).ok_or_else(|| {
828 self.error_at_token(
829 &name_tok,
830 format!(
831 "Unknown constraint command '{}'. Valid commands: help, default, unit, minimum, maximum, decimals, precision, option, options, length",
832 name_tok.text
833 ),
834 )
835 })?;
836
837 let mut args = Vec::new();
838 loop {
839 if self.at(&TokenKind::Arrow)?
842 || self.at(&TokenKind::RBracket)?
843 || self.at(&TokenKind::Eof)?
844 || is_spec_body_keyword(&self.peek()?.kind)
845 || self.at(&TokenKind::Spec)?
846 {
847 break;
848 }
849
850 let peek_kind = self.peek()?.kind.clone();
851 match peek_kind {
852 TokenKind::NumberLit => {
853 let tok = self.next()?;
854 args.push(CommandArg::Number(tok.text));
855 }
856 TokenKind::Minus | TokenKind::Plus => {
857 let second = self.lexer.peek_second()?.kind.clone();
858 if second == TokenKind::NumberLit {
859 let sign = self.next()?;
860 let num = self.next()?;
861 let text = format!("{}{}", sign.text, num.text);
862 args.push(CommandArg::Number(text));
863 } else {
864 break;
865 }
866 }
867 TokenKind::StringLit => {
868 let tok = self.next()?;
869 let content = unquote_string(&tok.text);
870 args.push(CommandArg::Text(content));
871 }
872 ref k if is_boolean_keyword(k) => {
873 let tok = self.next()?;
874 args.push(CommandArg::Boolean(token_kind_to_boolean_value(&tok.kind)));
875 }
876 ref k if can_be_label(k) || is_type_keyword(k) => {
877 let tok = self.next()?;
878 args.push(CommandArg::Label(tok.text));
879 }
880 _ => break,
881 }
882 }
883
884 Ok((cmd, args))
885 }
886
887 fn parse_meta(&mut self) -> Result<MetaField, Error> {
892 let meta_tok = self.expect(&TokenKind::Meta)?;
893 let start_span = meta_tok.span.clone();
894
895 let key_tok = self.next()?;
896 let key = key_tok.text.clone();
897
898 self.expect(&TokenKind::Colon)?;
899
900 let value = self.parse_meta_value()?;
901
902 let end_span = self.peek()?.span.clone();
903 let span = self.span_covering(&start_span, &end_span);
904
905 Ok(MetaField {
906 key,
907 value,
908 source_location: self.make_source(span),
909 })
910 }
911
912 fn parse_meta_value(&mut self) -> Result<MetaValue, Error> {
913 let peeked = self.peek()?;
915 match &peeked.kind {
916 TokenKind::StringLit => {
917 let value = self.parse_literal_value()?;
918 return Ok(MetaValue::Literal(value));
919 }
920 TokenKind::NumberLit => {
921 let value = self.parse_literal_value()?;
922 return Ok(MetaValue::Literal(value));
923 }
924 k if is_boolean_keyword(k) => {
925 let value = self.parse_literal_value()?;
926 return Ok(MetaValue::Literal(value));
927 }
928 _ => {}
929 }
930
931 let mut ident = String::new();
934 loop {
935 let peeked = self.peek()?;
936 match &peeked.kind {
937 k if k.is_identifier_like() => {
938 let tok = self.next()?;
939 ident.push_str(&tok.text);
940 }
941 TokenKind::Dot => {
942 self.next()?;
943 ident.push('.');
944 }
945 TokenKind::Slash => {
946 self.next()?;
947 ident.push('/');
948 }
949 TokenKind::Minus => {
950 self.next()?;
951 ident.push('-');
952 }
953 TokenKind::NumberLit => {
954 let tok = self.next()?;
955 ident.push_str(&tok.text);
956 }
957 _ => break,
958 }
959 }
960
961 if ident.is_empty() {
962 let tok = self.peek()?.clone();
963 return Err(self.error_at_token(&tok, "Expected a meta value"));
964 }
965
966 Ok(MetaValue::Unquoted(ident))
967 }
968
969 fn parse_literal_value(&mut self) -> Result<Value, Error> {
974 let peeked = self.peek()?;
975 match &peeked.kind {
976 TokenKind::StringLit => {
977 let tok = self.next()?;
978 let content = unquote_string(&tok.text);
979 Ok(Value::Text(content))
980 }
981 k if is_boolean_keyword(k) => {
982 let tok = self.next()?;
983 Ok(Value::Boolean(token_kind_to_boolean_value(&tok.kind)))
984 }
985 TokenKind::NumberLit => self.parse_number_literal(),
986 TokenKind::Minus | TokenKind::Plus => self.parse_signed_number_literal(),
987 _ => {
988 let tok = self.next()?;
989 Err(self.error_at_token(
990 &tok,
991 format!(
992 "Expected a value (number, text, boolean, date, etc.), found '{}'",
993 tok.text
994 ),
995 ))
996 }
997 }
998 }
999
1000 fn parse_signed_number_literal(&mut self) -> Result<Value, Error> {
1001 let sign_tok = self.next()?;
1002 let sign_span = sign_tok.span.clone();
1003 let is_negative = sign_tok.kind == TokenKind::Minus;
1004
1005 if !self.at(&TokenKind::NumberLit)? {
1006 let tok = self.peek()?.clone();
1007 return Err(self.error_at_token(
1008 &tok,
1009 format!(
1010 "Expected a number after '{}', found '{}'",
1011 sign_tok.text, tok.text
1012 ),
1013 ));
1014 }
1015
1016 let value = self.parse_number_literal()?;
1017 if !is_negative {
1018 return Ok(value);
1019 }
1020 match value {
1021 Value::Number(d) => Ok(Value::Number(-d)),
1022 Value::Scale(d, unit) => Ok(Value::Scale(-d, unit)),
1023 Value::Duration(d, unit) => Ok(Value::Duration(-d, unit)),
1024 Value::Ratio(d, label) => Ok(Value::Ratio(-d, label)),
1025 other => Err(Error::parsing(
1026 format!("Cannot negate this value: {}", other),
1027 self.make_source(sign_span),
1028 None::<String>,
1029 )),
1030 }
1031 }
1032
1033 fn parse_number_literal(&mut self) -> Result<Value, Error> {
1034 let num_tok = self.next()?;
1035 let num_text = &num_tok.text;
1036 let num_span = num_tok.span.clone();
1037
1038 if num_text.len() == 4
1040 && num_text.chars().all(|c| c.is_ascii_digit())
1041 && self.at(&TokenKind::Minus)?
1042 {
1043 return self.parse_date_literal(num_text.clone(), num_span);
1044 }
1045
1046 let peeked = self.peek()?;
1048
1049 if num_text.len() == 2
1051 && num_text.chars().all(|c| c.is_ascii_digit())
1052 && peeked.kind == TokenKind::Colon
1053 {
1054 return self.try_parse_time_literal(num_text.clone(), num_span);
1061 }
1062
1063 if peeked.kind == TokenKind::PercentPercent {
1065 let pp_tok = self.next()?;
1066 if let Ok(next_peek) = self.peek() {
1068 if next_peek.kind == TokenKind::NumberLit {
1069 return Err(self.error_at_token(
1070 &pp_tok,
1071 "Permille literal cannot be followed by a digit",
1072 ));
1073 }
1074 }
1075 let decimal = parse_decimal_string(num_text, &num_span, self)?;
1076 let ratio_value = decimal / Decimal::from(1000);
1077 return Ok(Value::Ratio(ratio_value, Some("permille".to_string())));
1078 }
1079
1080 if peeked.kind == TokenKind::Percent {
1082 let pct_tok = self.next()?;
1083 if let Ok(next_peek) = self.peek() {
1085 if next_peek.kind == TokenKind::NumberLit || next_peek.kind == TokenKind::Percent {
1086 return Err(self.error_at_token(
1087 &pct_tok,
1088 "Percent literal cannot be followed by a digit",
1089 ));
1090 }
1091 }
1092 let decimal = parse_decimal_string(num_text, &num_span, self)?;
1093 let ratio_value = decimal / Decimal::from(100);
1094 return Ok(Value::Ratio(ratio_value, Some("percent".to_string())));
1095 }
1096
1097 if peeked.kind == TokenKind::PercentKw {
1099 self.next()?; let decimal = parse_decimal_string(num_text, &num_span, self)?;
1101 let ratio_value = decimal / Decimal::from(100);
1102 return Ok(Value::Ratio(ratio_value, Some("percent".to_string())));
1103 }
1104
1105 if peeked.kind == TokenKind::Permille {
1107 self.next()?; let decimal = parse_decimal_string(num_text, &num_span, self)?;
1109 let ratio_value = decimal / Decimal::from(1000);
1110 return Ok(Value::Ratio(ratio_value, Some("permille".to_string())));
1111 }
1112
1113 if is_duration_unit(&peeked.kind) && peeked.kind != TokenKind::PercentKw {
1115 let unit_tok = self.next()?;
1116 let decimal = parse_decimal_string(num_text, &num_span, self)?;
1117 let duration_unit = token_kind_to_duration_unit(&unit_tok.kind);
1118 return Ok(Value::Duration(decimal, duration_unit));
1119 }
1120
1121 if can_be_label(&peeked.kind) {
1123 let unit_tok = self.next()?;
1124 let decimal = parse_decimal_string(num_text, &num_span, self)?;
1125 return Ok(Value::Scale(decimal, unit_tok.text.clone()));
1126 }
1127
1128 let decimal = parse_decimal_string(num_text, &num_span, self)?;
1130 Ok(Value::Number(decimal))
1131 }
1132
1133 fn parse_date_literal(&mut self, year_text: String, start_span: Span) -> Result<Value, Error> {
1134 let mut dt_str = year_text;
1135
1136 self.expect(&TokenKind::Minus)?;
1138 dt_str.push('-');
1139 let month_tok = self.expect(&TokenKind::NumberLit)?;
1140 dt_str.push_str(&month_tok.text);
1141
1142 self.expect(&TokenKind::Minus)?;
1144 dt_str.push('-');
1145 let day_tok = self.expect(&TokenKind::NumberLit)?;
1146 dt_str.push_str(&day_tok.text);
1147
1148 if self.at(&TokenKind::Identifier)? {
1150 let peeked = self.peek()?;
1151 if peeked.text.len() >= 2
1152 && (peeked.text.starts_with('T') || peeked.text.starts_with('t'))
1153 {
1154 let t_tok = self.next()?;
1156 dt_str.push_str(&t_tok.text);
1157
1158 if self.at(&TokenKind::Colon)? {
1160 self.next()?;
1161 dt_str.push(':');
1162 let min_tok = self.next()?;
1163 dt_str.push_str(&min_tok.text);
1164
1165 if self.at(&TokenKind::Colon)? {
1167 self.next()?;
1168 dt_str.push(':');
1169 let sec_tok = self.next()?;
1170 dt_str.push_str(&sec_tok.text);
1171
1172 if self.at(&TokenKind::Dot)? {
1174 self.next()?;
1175 dt_str.push('.');
1176 let frac_tok = self.expect(&TokenKind::NumberLit)?;
1177 dt_str.push_str(&frac_tok.text);
1178 }
1179 }
1180 }
1181
1182 self.try_consume_timezone(&mut dt_str)?;
1184 }
1185 }
1186
1187 if let Ok(dtv) = dt_str.parse::<crate::literals::DateTimeValue>() {
1188 return Ok(Value::Date(dtv));
1189 }
1190
1191 Err(Error::parsing(
1192 format!("Invalid date/time format: '{}'", dt_str),
1193 self.make_source(start_span),
1194 None::<String>,
1195 ))
1196 }
1197
1198 fn try_consume_timezone(&mut self, dt_str: &mut String) -> Result<(), Error> {
1199 if self.at(&TokenKind::Identifier)? {
1201 let peeked = self.peek()?;
1202 if peeked.text == "Z" || peeked.text == "z" {
1203 let z_tok = self.next()?;
1204 dt_str.push_str(&z_tok.text);
1205 return Ok(());
1206 }
1207 }
1208
1209 if self.at(&TokenKind::Plus)? || self.at(&TokenKind::Minus)? {
1211 let sign_tok = self.next()?;
1212 dt_str.push_str(&sign_tok.text);
1213 let hour_tok = self.expect(&TokenKind::NumberLit)?;
1214 dt_str.push_str(&hour_tok.text);
1215 if self.at(&TokenKind::Colon)? {
1216 self.next()?;
1217 dt_str.push(':');
1218 let min_tok = self.expect(&TokenKind::NumberLit)?;
1219 dt_str.push_str(&min_tok.text);
1220 }
1221 }
1222
1223 Ok(())
1224 }
1225
1226 fn try_parse_time_literal(
1227 &mut self,
1228 hour_text: String,
1229 start_span: Span,
1230 ) -> Result<Value, Error> {
1231 let mut time_str = hour_text;
1232
1233 self.expect(&TokenKind::Colon)?;
1235 time_str.push(':');
1236 let min_tok = self.expect(&TokenKind::NumberLit)?;
1237 time_str.push_str(&min_tok.text);
1238
1239 if self.at(&TokenKind::Colon)? {
1241 self.next()?;
1242 time_str.push(':');
1243 let sec_tok = self.expect(&TokenKind::NumberLit)?;
1244 time_str.push_str(&sec_tok.text);
1245 }
1246
1247 self.try_consume_timezone(&mut time_str)?;
1249
1250 if let Ok(t) = time_str.parse::<chrono::NaiveTime>() {
1251 use chrono::Timelike;
1252 return Ok(Value::Time(TimeValue {
1253 hour: t.hour() as u8,
1254 minute: t.minute() as u8,
1255 second: t.second() as u8,
1256 timezone: None,
1257 }));
1258 }
1259
1260 Err(Error::parsing(
1261 format!("Invalid time format: '{}'", time_str),
1262 self.make_source(start_span),
1263 None::<String>,
1264 ))
1265 }
1266
1267 fn new_expression(
1272 &mut self,
1273 kind: ExpressionKind,
1274 source: Source,
1275 ) -> Result<Expression, Error> {
1276 self.expression_count += 1;
1277 if self.expression_count > self.max_expression_count {
1278 return Err(Error::resource_limit_exceeded(
1279 "max_expression_count",
1280 self.max_expression_count.to_string(),
1281 self.expression_count.to_string(),
1282 "Split logic into multiple rules to reduce expression count",
1283 Some(source),
1284 None,
1285 None,
1286 ));
1287 }
1288 Ok(Expression::new(kind, source))
1289 }
1290
1291 fn check_depth(&mut self) -> Result<(), Error> {
1292 if let Err(actual) = self.depth_tracker.push_depth() {
1293 let span = self.peek()?.span.clone();
1294 self.depth_tracker.pop_depth();
1295 return Err(Error::resource_limit_exceeded(
1296 "max_expression_depth",
1297 self.depth_tracker.max_depth().to_string(),
1298 actual.to_string(),
1299 "Simplify nested expressions or break into separate rules",
1300 Some(self.make_source(span)),
1301 None,
1302 None,
1303 ));
1304 }
1305 Ok(())
1306 }
1307
1308 fn parse_expression(&mut self) -> Result<Expression, Error> {
1309 self.check_depth()?;
1310 let result = self.parse_and_expression();
1311 self.depth_tracker.pop_depth();
1312 result
1313 }
1314
1315 fn parse_and_expression(&mut self) -> Result<Expression, Error> {
1316 let start_span = self.peek()?.span.clone();
1317 let mut left = self.parse_and_operand()?;
1318
1319 while self.at(&TokenKind::And)? {
1320 self.next()?; let right = self.parse_and_operand()?;
1322 let span = self.span_covering(
1323 &start_span,
1324 &right
1325 .source_location
1326 .as_ref()
1327 .map(|s| s.span.clone())
1328 .unwrap_or_else(|| start_span.clone()),
1329 );
1330 left = self.new_expression(
1331 ExpressionKind::LogicalAnd(Arc::new(left), Arc::new(right)),
1332 self.make_source(span),
1333 )?;
1334 }
1335
1336 Ok(left)
1337 }
1338
1339 fn parse_and_operand(&mut self) -> Result<Expression, Error> {
1340 if self.at(&TokenKind::Not)? {
1342 return self.parse_not_expression();
1343 }
1344
1345 self.parse_base_with_suffix()
1347 }
1348
1349 fn parse_not_expression(&mut self) -> Result<Expression, Error> {
1350 let not_tok = self.expect(&TokenKind::Not)?;
1351 let start_span = not_tok.span.clone();
1352
1353 self.check_depth()?;
1354 let operand = self.parse_and_operand()?;
1355 self.depth_tracker.pop_depth();
1356
1357 let end_span = operand
1358 .source_location
1359 .as_ref()
1360 .map(|s| s.span.clone())
1361 .unwrap_or_else(|| start_span.clone());
1362 let span = self.span_covering(&start_span, &end_span);
1363
1364 self.new_expression(
1365 ExpressionKind::LogicalNegation(Arc::new(operand), NegationType::Not),
1366 self.make_source(span),
1367 )
1368 }
1369
1370 fn parse_base_with_suffix(&mut self) -> Result<Expression, Error> {
1371 let start_span = self.peek()?.span.clone();
1372 let base = self.parse_base_expression()?;
1373
1374 let peeked = self.peek()?;
1376
1377 if is_comparison_operator(&peeked.kind) {
1379 return self.parse_comparison_suffix(base, start_span);
1380 }
1381
1382 if peeked.kind == TokenKind::Not {
1386 return self.parse_not_in_calendar_suffix(base, start_span);
1387 }
1388
1389 if peeked.kind == TokenKind::In {
1391 return self.parse_in_suffix(base, start_span);
1392 }
1393
1394 Ok(base)
1395 }
1396
1397 fn parse_comparison_suffix(
1398 &mut self,
1399 left: Expression,
1400 start_span: Span,
1401 ) -> Result<Expression, Error> {
1402 let operator = self.parse_comparison_operator()?;
1403
1404 let right = if self.at(&TokenKind::Not)? {
1406 self.parse_not_expression()?
1407 } else {
1408 let rhs = self.parse_base_expression()?;
1409 if self.at(&TokenKind::In)? {
1411 self.parse_in_suffix(rhs, start_span.clone())?
1412 } else {
1413 rhs
1414 }
1415 };
1416
1417 let end_span = right
1418 .source_location
1419 .as_ref()
1420 .map(|s| s.span.clone())
1421 .unwrap_or_else(|| start_span.clone());
1422 let span = self.span_covering(&start_span, &end_span);
1423
1424 self.new_expression(
1425 ExpressionKind::Comparison(Arc::new(left), operator, Arc::new(right)),
1426 self.make_source(span),
1427 )
1428 }
1429
1430 fn parse_comparison_operator(&mut self) -> Result<ComparisonComputation, Error> {
1431 let tok = self.next()?;
1432 match tok.kind {
1433 TokenKind::Gt => Ok(ComparisonComputation::GreaterThan),
1434 TokenKind::Lt => Ok(ComparisonComputation::LessThan),
1435 TokenKind::Gte => Ok(ComparisonComputation::GreaterThanOrEqual),
1436 TokenKind::Lte => Ok(ComparisonComputation::LessThanOrEqual),
1437 TokenKind::EqEq => Ok(ComparisonComputation::Equal),
1438 TokenKind::BangEq => Ok(ComparisonComputation::NotEqual),
1439 TokenKind::Is => {
1440 if self.at(&TokenKind::Not)? {
1442 self.next()?; Ok(ComparisonComputation::IsNot)
1444 } else {
1445 Ok(ComparisonComputation::Is)
1446 }
1447 }
1448 _ => Err(self.error_at_token(
1449 &tok,
1450 format!("Expected a comparison operator, found {}", tok.kind),
1451 )),
1452 }
1453 }
1454
1455 fn parse_not_in_calendar_suffix(
1456 &mut self,
1457 base: Expression,
1458 start_span: Span,
1459 ) -> Result<Expression, Error> {
1460 self.expect(&TokenKind::Not)?;
1461 self.expect(&TokenKind::In)?;
1462 self.expect(&TokenKind::Calendar)?;
1463 let unit = self.parse_calendar_unit()?;
1464 let end = self.peek()?.span.clone();
1465 let span = self.span_covering(&start_span, &end);
1466 self.new_expression(
1467 ExpressionKind::DateCalendar(DateCalendarKind::NotIn, unit, Arc::new(base)),
1468 self.make_source(span),
1469 )
1470 }
1471
1472 fn parse_in_suffix(&mut self, base: Expression, start_span: Span) -> Result<Expression, Error> {
1473 self.expect(&TokenKind::In)?;
1474
1475 let peeked = self.peek()?;
1476
1477 if peeked.kind == TokenKind::Past || peeked.kind == TokenKind::Future {
1479 let direction = self.next()?;
1480 let rel_kind = if direction.kind == TokenKind::Past {
1481 DateRelativeKind::InPast
1482 } else {
1483 DateRelativeKind::InFuture
1484 };
1485
1486 if self.at(&TokenKind::Calendar)? {
1488 self.next()?; let cal_kind = if direction.kind == TokenKind::Past {
1490 DateCalendarKind::Past
1491 } else {
1492 DateCalendarKind::Future
1493 };
1494 let unit = self.parse_calendar_unit()?;
1495 let end = self.peek()?.span.clone();
1496 let span = self.span_covering(&start_span, &end);
1497 return self.new_expression(
1498 ExpressionKind::DateCalendar(cal_kind, unit, Arc::new(base)),
1499 self.make_source(span),
1500 );
1501 }
1502
1503 let tolerance = if !self.at(&TokenKind::And)?
1505 && !self.at(&TokenKind::Unless)?
1506 && !self.at(&TokenKind::Then)?
1507 && !self.at(&TokenKind::Eof)?
1508 && !is_comparison_operator(&self.peek()?.kind)
1509 {
1510 let peek_kind = self.peek()?.kind.clone();
1511 if peek_kind == TokenKind::NumberLit
1512 || peek_kind == TokenKind::LParen
1513 || can_be_reference_segment(&peek_kind)
1514 || is_math_function(&peek_kind)
1515 {
1516 Some(Arc::new(self.parse_base_expression()?))
1517 } else {
1518 None
1519 }
1520 } else {
1521 None
1522 };
1523
1524 let end = self.peek()?.span.clone();
1525 let span = self.span_covering(&start_span, &end);
1526 return self.new_expression(
1527 ExpressionKind::DateRelative(rel_kind, Arc::new(base), tolerance),
1528 self.make_source(span),
1529 );
1530 }
1531
1532 if peeked.kind == TokenKind::Calendar {
1534 self.next()?; let unit = self.parse_calendar_unit()?;
1536 let end = self.peek()?.span.clone();
1537 let span = self.span_covering(&start_span, &end);
1538 return self.new_expression(
1539 ExpressionKind::DateCalendar(DateCalendarKind::Current, unit, Arc::new(base)),
1540 self.make_source(span),
1541 );
1542 }
1543
1544 let target_tok = self.next()?;
1546 let target = conversion_target_from_token(&target_tok.kind, &target_tok.text);
1547
1548 let converted = self.new_expression(
1549 ExpressionKind::UnitConversion(Arc::new(base), target),
1550 self.make_source(self.span_covering(&start_span, &target_tok.span)),
1551 )?;
1552
1553 if is_comparison_operator(&self.peek()?.kind) {
1555 return self.parse_comparison_suffix(converted, start_span);
1556 }
1557
1558 Ok(converted)
1559 }
1560
1561 fn parse_calendar_unit(&mut self) -> Result<CalendarUnit, Error> {
1562 let tok = self.next()?;
1563 if !is_calendar_unit_token(&tok.kind) {
1564 return Err(self.error_at_token(
1565 &tok,
1566 format!("Expected 'year', 'month', or 'week', found '{}'", tok.text),
1567 ));
1568 }
1569 Ok(token_kind_to_calendar_unit(&tok.kind))
1570 }
1571
1572 fn parse_base_expression(&mut self) -> Result<Expression, Error> {
1577 let start_span = self.peek()?.span.clone();
1578 let mut left = self.parse_term()?;
1579
1580 while self.at_any(&[TokenKind::Plus, TokenKind::Minus])? {
1581 let op_tok = self.next()?;
1584 let operation = match op_tok.kind {
1585 TokenKind::Plus => ArithmeticComputation::Add,
1586 TokenKind::Minus => ArithmeticComputation::Subtract,
1587 _ => unreachable!("BUG: only + and - should reach here"),
1588 };
1589
1590 let right = self.parse_term()?;
1591 let end_span = right
1592 .source_location
1593 .as_ref()
1594 .map(|s| s.span.clone())
1595 .unwrap_or_else(|| start_span.clone());
1596 let span = self.span_covering(&start_span, &end_span);
1597
1598 left = self.new_expression(
1599 ExpressionKind::Arithmetic(Arc::new(left), operation, Arc::new(right)),
1600 self.make_source(span),
1601 )?;
1602 }
1603
1604 Ok(left)
1605 }
1606
1607 fn parse_term(&mut self) -> Result<Expression, Error> {
1608 let start_span = self.peek()?.span.clone();
1609 let mut left = self.parse_power()?;
1610
1611 while self.at_any(&[TokenKind::Star, TokenKind::Slash, TokenKind::Percent])? {
1612 let op_tok = self.next()?;
1615 let operation = match op_tok.kind {
1616 TokenKind::Star => ArithmeticComputation::Multiply,
1617 TokenKind::Slash => ArithmeticComputation::Divide,
1618 TokenKind::Percent => ArithmeticComputation::Modulo,
1619 _ => unreachable!("BUG: only *, /, % should reach here"),
1620 };
1621
1622 let right = self.parse_power()?;
1623 let end_span = right
1624 .source_location
1625 .as_ref()
1626 .map(|s| s.span.clone())
1627 .unwrap_or_else(|| start_span.clone());
1628 let span = self.span_covering(&start_span, &end_span);
1629
1630 left = self.new_expression(
1631 ExpressionKind::Arithmetic(Arc::new(left), operation, Arc::new(right)),
1632 self.make_source(span),
1633 )?;
1634 }
1635
1636 Ok(left)
1637 }
1638
1639 fn parse_power(&mut self) -> Result<Expression, Error> {
1640 let start_span = self.peek()?.span.clone();
1641 let left = self.parse_factor()?;
1642
1643 if self.at(&TokenKind::Caret)? {
1644 self.next()?;
1645 self.check_depth()?;
1646 let right = self.parse_power()?;
1647 self.depth_tracker.pop_depth();
1648 let end_span = right
1649 .source_location
1650 .as_ref()
1651 .map(|s| s.span.clone())
1652 .unwrap_or_else(|| start_span.clone());
1653 let span = self.span_covering(&start_span, &end_span);
1654
1655 return self.new_expression(
1656 ExpressionKind::Arithmetic(
1657 Arc::new(left),
1658 ArithmeticComputation::Power,
1659 Arc::new(right),
1660 ),
1661 self.make_source(span),
1662 );
1663 }
1664
1665 Ok(left)
1666 }
1667
1668 fn parse_factor(&mut self) -> Result<Expression, Error> {
1669 let peeked = self.peek()?;
1670 let start_span = peeked.span.clone();
1671
1672 if peeked.kind == TokenKind::Minus {
1673 self.next()?;
1674 let operand = self.parse_primary_or_math()?;
1675 let end_span = operand
1676 .source_location
1677 .as_ref()
1678 .map(|s| s.span.clone())
1679 .unwrap_or_else(|| start_span.clone());
1680 let span = self.span_covering(&start_span, &end_span);
1681
1682 let zero = self.new_expression(
1683 ExpressionKind::Literal(Value::Number(Decimal::ZERO)),
1684 self.make_source(start_span),
1685 )?;
1686 return self.new_expression(
1687 ExpressionKind::Arithmetic(
1688 Arc::new(zero),
1689 ArithmeticComputation::Subtract,
1690 Arc::new(operand),
1691 ),
1692 self.make_source(span),
1693 );
1694 }
1695
1696 if peeked.kind == TokenKind::Plus {
1697 self.next()?;
1698 return self.parse_primary_or_math();
1699 }
1700
1701 self.parse_primary_or_math()
1702 }
1703
1704 fn parse_primary_or_math(&mut self) -> Result<Expression, Error> {
1705 let peeked = self.peek()?;
1706
1707 if is_math_function(&peeked.kind) {
1709 return self.parse_math_function();
1710 }
1711
1712 self.parse_primary()
1713 }
1714
1715 fn parse_math_function(&mut self) -> Result<Expression, Error> {
1716 let func_tok = self.next()?;
1717 let start_span = func_tok.span.clone();
1718
1719 let operator = match func_tok.kind {
1720 TokenKind::Sqrt => MathematicalComputation::Sqrt,
1721 TokenKind::Sin => MathematicalComputation::Sin,
1722 TokenKind::Cos => MathematicalComputation::Cos,
1723 TokenKind::Tan => MathematicalComputation::Tan,
1724 TokenKind::Asin => MathematicalComputation::Asin,
1725 TokenKind::Acos => MathematicalComputation::Acos,
1726 TokenKind::Atan => MathematicalComputation::Atan,
1727 TokenKind::Log => MathematicalComputation::Log,
1728 TokenKind::Exp => MathematicalComputation::Exp,
1729 TokenKind::Abs => MathematicalComputation::Abs,
1730 TokenKind::Floor => MathematicalComputation::Floor,
1731 TokenKind::Ceil => MathematicalComputation::Ceil,
1732 TokenKind::Round => MathematicalComputation::Round,
1733 _ => unreachable!("BUG: only math functions should reach here"),
1734 };
1735
1736 self.check_depth()?;
1737 let operand = self.parse_base_expression()?;
1738 self.depth_tracker.pop_depth();
1739
1740 let end_span = operand
1741 .source_location
1742 .as_ref()
1743 .map(|s| s.span.clone())
1744 .unwrap_or_else(|| start_span.clone());
1745 let span = self.span_covering(&start_span, &end_span);
1746
1747 self.new_expression(
1748 ExpressionKind::MathematicalComputation(operator, Arc::new(operand)),
1749 self.make_source(span),
1750 )
1751 }
1752
1753 fn parse_primary(&mut self) -> Result<Expression, Error> {
1754 let peeked = self.peek()?;
1755 let start_span = peeked.span.clone();
1756
1757 match &peeked.kind {
1758 TokenKind::LParen => {
1760 self.next()?; let inner = self.parse_expression()?;
1762 self.expect(&TokenKind::RParen)?;
1763 Ok(inner)
1764 }
1765
1766 TokenKind::Now => {
1768 let tok = self.next()?;
1769 self.new_expression(ExpressionKind::Now, self.make_source(tok.span))
1770 }
1771
1772 TokenKind::StringLit => {
1774 let tok = self.next()?;
1775 let content = unquote_string(&tok.text);
1776 self.new_expression(
1777 ExpressionKind::Literal(Value::Text(content)),
1778 self.make_source(tok.span),
1779 )
1780 }
1781
1782 k if is_boolean_keyword(k) => {
1784 let tok = self.next()?;
1785 self.new_expression(
1786 ExpressionKind::Literal(Value::Boolean(token_kind_to_boolean_value(&tok.kind))),
1787 self.make_source(tok.span),
1788 )
1789 }
1790
1791 TokenKind::NumberLit => self.parse_number_expression(),
1793
1794 k if can_be_reference_segment(k) => {
1796 let reference = self.parse_expression_reference()?;
1797 let end_span = self.peek()?.span.clone();
1798 let span = self.span_covering(&start_span, &end_span);
1799 self.new_expression(ExpressionKind::Reference(reference), self.make_source(span))
1800 }
1801
1802 _ => {
1803 let tok = self.next()?;
1804 Err(self.error_at_token(
1805 &tok,
1806 format!("Expected an expression, found '{}'", tok.text),
1807 ))
1808 }
1809 }
1810 }
1811
1812 fn parse_number_expression(&mut self) -> Result<Expression, Error> {
1813 let num_tok = self.next()?;
1814 let num_text = num_tok.text.clone();
1815 let start_span = num_tok.span.clone();
1816
1817 if num_text.len() == 4
1819 && num_text.chars().all(|c| c.is_ascii_digit())
1820 && self.at(&TokenKind::Minus)?
1821 {
1822 let minus_span = self.peek()?.span.clone();
1829 if minus_span.start == start_span.end {
1831 let value = self.parse_date_literal(num_text, start_span.clone())?;
1832 return self
1833 .new_expression(ExpressionKind::Literal(value), self.make_source(start_span));
1834 }
1835 }
1836
1837 if num_text.len() == 2
1839 && num_text.chars().all(|c| c.is_ascii_digit())
1840 && self.at(&TokenKind::Colon)?
1841 {
1842 let colon_span = self.peek()?.span.clone();
1843 if colon_span.start == start_span.end {
1844 let value = self.try_parse_time_literal(num_text, start_span.clone())?;
1845 return self
1846 .new_expression(ExpressionKind::Literal(value), self.make_source(start_span));
1847 }
1848 }
1849
1850 if self.at(&TokenKind::PercentPercent)? {
1852 let pp_tok = self.next()?;
1853 if let Ok(next_peek) = self.peek() {
1854 if next_peek.kind == TokenKind::NumberLit {
1855 return Err(self.error_at_token(
1856 &pp_tok,
1857 "Permille literal cannot be followed by a digit",
1858 ));
1859 }
1860 }
1861 let decimal = parse_decimal_string(&num_text, &start_span, self)?;
1862 let ratio_value = decimal / Decimal::from(1000);
1863 return self.new_expression(
1864 ExpressionKind::Literal(Value::Ratio(ratio_value, Some("permille".to_string()))),
1865 self.make_source(start_span),
1866 );
1867 }
1868
1869 if self.at(&TokenKind::Percent)? {
1871 let pct_span = self.peek()?.span.clone();
1872 let pct_tok = self.next()?;
1875 if let Ok(next_peek) = self.peek() {
1876 if next_peek.kind == TokenKind::NumberLit || next_peek.kind == TokenKind::Percent {
1877 return Err(self.error_at_token(
1878 &pct_tok,
1879 "Percent literal cannot be followed by a digit",
1880 ));
1881 }
1882 }
1883 let decimal = parse_decimal_string(&num_text, &start_span, self)?;
1884 let ratio_value = decimal / Decimal::from(100);
1885 return self.new_expression(
1886 ExpressionKind::Literal(Value::Ratio(ratio_value, Some("percent".to_string()))),
1887 self.make_source(self.span_covering(&start_span, &pct_span)),
1888 );
1889 }
1890
1891 if self.at(&TokenKind::PercentKw)? {
1893 self.next()?;
1894 let decimal = parse_decimal_string(&num_text, &start_span, self)?;
1895 let ratio_value = decimal / Decimal::from(100);
1896 return self.new_expression(
1897 ExpressionKind::Literal(Value::Ratio(ratio_value, Some("percent".to_string()))),
1898 self.make_source(start_span),
1899 );
1900 }
1901
1902 if self.at(&TokenKind::Permille)? {
1904 self.next()?;
1905 let decimal = parse_decimal_string(&num_text, &start_span, self)?;
1906 let ratio_value = decimal / Decimal::from(1000);
1907 return self.new_expression(
1908 ExpressionKind::Literal(Value::Ratio(ratio_value, Some("permille".to_string()))),
1909 self.make_source(start_span),
1910 );
1911 }
1912
1913 if is_duration_unit(&self.peek()?.kind) && self.peek()?.kind != TokenKind::PercentKw {
1915 let unit_tok = self.next()?;
1916 let decimal = parse_decimal_string(&num_text, &start_span, self)?;
1917 let duration_unit = token_kind_to_duration_unit(&unit_tok.kind);
1918 return self.new_expression(
1919 ExpressionKind::Literal(Value::Duration(decimal, duration_unit)),
1920 self.make_source(self.span_covering(&start_span, &unit_tok.span)),
1921 );
1922 }
1923
1924 if can_be_label(&self.peek()?.kind) {
1926 let unit_tok = self.next()?;
1927 let decimal = parse_decimal_string(&num_text, &start_span, self)?;
1928 return self.new_expression(
1929 ExpressionKind::UnresolvedUnitLiteral(decimal, unit_tok.text.clone()),
1930 self.make_source(self.span_covering(&start_span, &unit_tok.span)),
1931 );
1932 }
1933
1934 let decimal = parse_decimal_string(&num_text, &start_span, self)?;
1936 self.new_expression(
1937 ExpressionKind::Literal(Value::Number(decimal)),
1938 self.make_source(start_span),
1939 )
1940 }
1941
1942 fn parse_expression_reference(&mut self) -> Result<Reference, Error> {
1943 let mut segments = Vec::new();
1944
1945 let first = self.next()?;
1946 segments.push(first.text.clone());
1947
1948 while self.at(&TokenKind::Dot)? {
1949 self.next()?; let seg = self.next()?;
1951 if !can_be_reference_segment(&seg.kind) {
1952 return Err(self.error_at_token(
1953 &seg,
1954 format!("Expected an identifier after '.', found {}", seg.kind),
1955 ));
1956 }
1957 segments.push(seg.text.clone());
1958 }
1959
1960 Ok(Reference::from_path(segments))
1961 }
1962}
1963
1964fn unquote_string(s: &str) -> String {
1969 if s.len() >= 2 && s.starts_with('"') && s.ends_with('"') {
1970 s[1..s.len() - 1].to_string()
1971 } else {
1972 s.to_string()
1973 }
1974}
1975
1976fn parse_decimal_string(text: &str, span: &Span, parser: &Parser) -> Result<Decimal, Error> {
1977 let clean = text.replace(['_', ','], "");
1978 Decimal::from_str(&clean).map_err(|_| {
1979 Error::parsing(
1980 format!(
1981 "Invalid number: '{}'. Expected a valid decimal number (e.g., 42, 3.14, 1_000_000)",
1982 text
1983 ),
1984 parser.make_source(span.clone()),
1985 None::<String>,
1986 )
1987 })
1988}
1989
1990fn is_comparison_operator(kind: &TokenKind) -> bool {
1991 matches!(
1992 kind,
1993 TokenKind::Gt
1994 | TokenKind::Lt
1995 | TokenKind::Gte
1996 | TokenKind::Lte
1997 | TokenKind::EqEq
1998 | TokenKind::BangEq
1999 | TokenKind::Is
2000 )
2001}
2002
2003impl TokenKind {
2005 fn is_identifier_like(&self) -> bool {
2006 matches!(self, TokenKind::Identifier)
2007 || can_be_label(self)
2008 || is_type_keyword(self)
2009 || is_boolean_keyword(self)
2010 || is_duration_unit(self)
2011 || is_math_function(self)
2012 }
2013}