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 { primitive: kind }
762 } else if can_be_label(&name_tok.kind) {
763 ParentType::Custom {
764 name: name_tok.text.clone(),
765 }
766 } else {
767 return Err(self.error_at_token(
768 &name_tok,
769 format!("Expected a type name, found {}", name_tok.kind),
770 ));
771 };
772
773 let from_spec = if self.at(&TokenKind::From)? {
775 self.next()?; let (from_name, _) = self.parse_spec_name()?;
777 let from_registry = from_name.starts_with('@');
778 let hash_pin = self.try_parse_hash_pin()?;
779 Some(SpecRef {
780 name: from_name,
781 from_registry,
782 hash_pin,
783 effective: None,
784 })
785 } else {
786 None
787 };
788
789 let mut commands = Vec::new();
791 while self.at(&TokenKind::Arrow)? {
792 self.next()?; let (cmd, cmd_args) = self.parse_command()?;
794 commands.push((cmd, cmd_args));
795 }
796
797 let constraints = if commands.is_empty() {
798 None
799 } else {
800 Some(commands)
801 };
802
803 Ok((base, from_spec, constraints))
804 }
805
806 fn parse_remaining_arrow_chain(&mut self) -> Result<TypeArrowChain, Error> {
807 let mut commands = Vec::new();
808 while self.at(&TokenKind::Arrow)? {
809 self.next()?; let (cmd, cmd_args) = self.parse_command()?;
811 commands.push((cmd, cmd_args));
812 }
813 let constraints = if commands.is_empty() {
814 None
815 } else {
816 Some(commands)
817 };
818 Ok((
819 ParentType::Custom {
820 name: String::new(),
821 },
822 None,
823 constraints,
824 ))
825 }
826
827 fn parse_command(&mut self) -> Result<(TypeConstraintCommand, Vec<CommandArg>), Error> {
828 let name_tok = self.next()?;
829 if !can_be_label(&name_tok.kind) && !is_type_keyword(&name_tok.kind) {
830 return Err(self.error_at_token(
831 &name_tok,
832 format!("Expected a command name, found {}", name_tok.kind),
833 ));
834 }
835 let cmd = try_parse_type_constraint_command(&name_tok.text).ok_or_else(|| {
836 self.error_at_token(
837 &name_tok,
838 format!(
839 "Unknown constraint command '{}'. Valid commands: help, default, unit, minimum, maximum, decimals, precision, option, options, length",
840 name_tok.text
841 ),
842 )
843 })?;
844
845 let mut args = Vec::new();
846 loop {
847 if self.at(&TokenKind::Arrow)?
850 || self.at(&TokenKind::RBracket)?
851 || self.at(&TokenKind::Eof)?
852 || is_spec_body_keyword(&self.peek()?.kind)
853 || self.at(&TokenKind::Spec)?
854 {
855 break;
856 }
857
858 let peek_kind = self.peek()?.kind.clone();
859 match peek_kind {
860 TokenKind::NumberLit => {
861 let tok = self.next()?;
862 args.push(CommandArg::Number(tok.text));
863 }
864 TokenKind::Minus | TokenKind::Plus => {
865 let second = self.lexer.peek_second()?.kind.clone();
866 if second == TokenKind::NumberLit {
867 let sign = self.next()?;
868 let num = self.next()?;
869 let text = format!("{}{}", sign.text, num.text);
870 args.push(CommandArg::Number(text));
871 } else {
872 break;
873 }
874 }
875 TokenKind::StringLit => {
876 let tok = self.next()?;
877 let content = unquote_string(&tok.text);
878 args.push(CommandArg::Text(content));
879 }
880 ref k if is_boolean_keyword(k) => {
881 let tok = self.next()?;
882 args.push(CommandArg::Boolean(token_kind_to_boolean_value(&tok.kind)));
883 }
884 ref k if can_be_label(k) || is_type_keyword(k) => {
885 let tok = self.next()?;
886 args.push(CommandArg::Label(tok.text));
887 }
888 _ => break,
889 }
890 }
891
892 Ok((cmd, args))
893 }
894
895 fn parse_meta(&mut self) -> Result<MetaField, Error> {
900 let meta_tok = self.expect(&TokenKind::Meta)?;
901 let start_span = meta_tok.span.clone();
902
903 let key_tok = self.next()?;
904 let key = key_tok.text.clone();
905
906 self.expect(&TokenKind::Colon)?;
907
908 let value = self.parse_meta_value()?;
909
910 let end_span = self.peek()?.span.clone();
911 let span = self.span_covering(&start_span, &end_span);
912
913 Ok(MetaField {
914 key,
915 value,
916 source_location: self.make_source(span),
917 })
918 }
919
920 fn parse_meta_value(&mut self) -> Result<MetaValue, Error> {
921 let peeked = self.peek()?;
923 match &peeked.kind {
924 TokenKind::StringLit => {
925 let value = self.parse_literal_value()?;
926 return Ok(MetaValue::Literal(value));
927 }
928 TokenKind::NumberLit => {
929 let value = self.parse_literal_value()?;
930 return Ok(MetaValue::Literal(value));
931 }
932 k if is_boolean_keyword(k) => {
933 let value = self.parse_literal_value()?;
934 return Ok(MetaValue::Literal(value));
935 }
936 _ => {}
937 }
938
939 let mut ident = String::new();
942 loop {
943 let peeked = self.peek()?;
944 match &peeked.kind {
945 k if k.is_identifier_like() => {
946 let tok = self.next()?;
947 ident.push_str(&tok.text);
948 }
949 TokenKind::Dot => {
950 self.next()?;
951 ident.push('.');
952 }
953 TokenKind::Slash => {
954 self.next()?;
955 ident.push('/');
956 }
957 TokenKind::Minus => {
958 self.next()?;
959 ident.push('-');
960 }
961 TokenKind::NumberLit => {
962 let tok = self.next()?;
963 ident.push_str(&tok.text);
964 }
965 _ => break,
966 }
967 }
968
969 if ident.is_empty() {
970 let tok = self.peek()?.clone();
971 return Err(self.error_at_token(&tok, "Expected a meta value"));
972 }
973
974 Ok(MetaValue::Unquoted(ident))
975 }
976
977 fn parse_literal_value(&mut self) -> Result<Value, Error> {
982 let peeked = self.peek()?;
983 match &peeked.kind {
984 TokenKind::StringLit => {
985 let tok = self.next()?;
986 let content = unquote_string(&tok.text);
987 Ok(Value::Text(content))
988 }
989 k if is_boolean_keyword(k) => {
990 let tok = self.next()?;
991 Ok(Value::Boolean(token_kind_to_boolean_value(&tok.kind)))
992 }
993 TokenKind::NumberLit => self.parse_number_literal(),
994 TokenKind::Minus | TokenKind::Plus => self.parse_signed_number_literal(),
995 _ => {
996 let tok = self.next()?;
997 Err(self.error_at_token(
998 &tok,
999 format!(
1000 "Expected a value (number, text, boolean, date, etc.), found '{}'",
1001 tok.text
1002 ),
1003 ))
1004 }
1005 }
1006 }
1007
1008 fn parse_signed_number_literal(&mut self) -> Result<Value, Error> {
1009 let sign_tok = self.next()?;
1010 let sign_span = sign_tok.span.clone();
1011 let is_negative = sign_tok.kind == TokenKind::Minus;
1012
1013 if !self.at(&TokenKind::NumberLit)? {
1014 let tok = self.peek()?.clone();
1015 return Err(self.error_at_token(
1016 &tok,
1017 format!(
1018 "Expected a number after '{}', found '{}'",
1019 sign_tok.text, tok.text
1020 ),
1021 ));
1022 }
1023
1024 let value = self.parse_number_literal()?;
1025 if !is_negative {
1026 return Ok(value);
1027 }
1028 match value {
1029 Value::Number(d) => Ok(Value::Number(-d)),
1030 Value::Scale(d, unit) => Ok(Value::Scale(-d, unit)),
1031 Value::Duration(d, unit) => Ok(Value::Duration(-d, unit)),
1032 Value::Ratio(d, label) => Ok(Value::Ratio(-d, label)),
1033 other => Err(Error::parsing(
1034 format!("Cannot negate this value: {}", other),
1035 self.make_source(sign_span),
1036 None::<String>,
1037 )),
1038 }
1039 }
1040
1041 fn parse_number_literal(&mut self) -> Result<Value, Error> {
1042 let num_tok = self.next()?;
1043 let num_text = &num_tok.text;
1044 let num_span = num_tok.span.clone();
1045
1046 if num_text.len() == 4
1048 && num_text.chars().all(|c| c.is_ascii_digit())
1049 && self.at(&TokenKind::Minus)?
1050 {
1051 return self.parse_date_literal(num_text.clone(), num_span);
1052 }
1053
1054 let peeked = self.peek()?;
1056
1057 if num_text.len() == 2
1059 && num_text.chars().all(|c| c.is_ascii_digit())
1060 && peeked.kind == TokenKind::Colon
1061 {
1062 return self.try_parse_time_literal(num_text.clone(), num_span);
1069 }
1070
1071 if peeked.kind == TokenKind::PercentPercent {
1073 let pp_tok = self.next()?;
1074 if let Ok(next_peek) = self.peek() {
1076 if next_peek.kind == TokenKind::NumberLit {
1077 return Err(self.error_at_token(
1078 &pp_tok,
1079 "Permille literal cannot be followed by a digit",
1080 ));
1081 }
1082 }
1083 let decimal = parse_decimal_string(num_text, &num_span, self)?;
1084 let ratio_value = decimal / Decimal::from(1000);
1085 return Ok(Value::Ratio(ratio_value, Some("permille".to_string())));
1086 }
1087
1088 if peeked.kind == TokenKind::Percent {
1090 let pct_tok = self.next()?;
1091 if let Ok(next_peek) = self.peek() {
1093 if next_peek.kind == TokenKind::NumberLit || next_peek.kind == TokenKind::Percent {
1094 return Err(self.error_at_token(
1095 &pct_tok,
1096 "Percent literal cannot be followed by a digit",
1097 ));
1098 }
1099 }
1100 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::PercentKw {
1107 self.next()?; let decimal = parse_decimal_string(num_text, &num_span, self)?;
1109 let ratio_value = decimal / Decimal::from(100);
1110 return Ok(Value::Ratio(ratio_value, Some("percent".to_string())));
1111 }
1112
1113 if peeked.kind == TokenKind::Permille {
1115 self.next()?; let decimal = parse_decimal_string(num_text, &num_span, self)?;
1117 let ratio_value = decimal / Decimal::from(1000);
1118 return Ok(Value::Ratio(ratio_value, Some("permille".to_string())));
1119 }
1120
1121 if is_duration_unit(&peeked.kind) && peeked.kind != TokenKind::PercentKw {
1123 let unit_tok = self.next()?;
1124 let decimal = parse_decimal_string(num_text, &num_span, self)?;
1125 let duration_unit = token_kind_to_duration_unit(&unit_tok.kind);
1126 return Ok(Value::Duration(decimal, duration_unit));
1127 }
1128
1129 if can_be_label(&peeked.kind) {
1131 let unit_tok = self.next()?;
1132 let decimal = parse_decimal_string(num_text, &num_span, self)?;
1133 return Ok(Value::Scale(decimal, unit_tok.text.clone()));
1134 }
1135
1136 let decimal = parse_decimal_string(num_text, &num_span, self)?;
1138 Ok(Value::Number(decimal))
1139 }
1140
1141 fn parse_date_literal(&mut self, year_text: String, start_span: Span) -> Result<Value, Error> {
1142 let mut dt_str = year_text;
1143
1144 self.expect(&TokenKind::Minus)?;
1146 dt_str.push('-');
1147 let month_tok = self.expect(&TokenKind::NumberLit)?;
1148 dt_str.push_str(&month_tok.text);
1149
1150 self.expect(&TokenKind::Minus)?;
1152 dt_str.push('-');
1153 let day_tok = self.expect(&TokenKind::NumberLit)?;
1154 dt_str.push_str(&day_tok.text);
1155
1156 if self.at(&TokenKind::Identifier)? {
1158 let peeked = self.peek()?;
1159 if peeked.text.len() >= 2
1160 && (peeked.text.starts_with('T') || peeked.text.starts_with('t'))
1161 {
1162 let t_tok = self.next()?;
1164 dt_str.push_str(&t_tok.text);
1165
1166 if self.at(&TokenKind::Colon)? {
1168 self.next()?;
1169 dt_str.push(':');
1170 let min_tok = self.next()?;
1171 dt_str.push_str(&min_tok.text);
1172
1173 if self.at(&TokenKind::Colon)? {
1175 self.next()?;
1176 dt_str.push(':');
1177 let sec_tok = self.next()?;
1178 dt_str.push_str(&sec_tok.text);
1179
1180 if self.at(&TokenKind::Dot)? {
1182 self.next()?;
1183 dt_str.push('.');
1184 let frac_tok = self.expect(&TokenKind::NumberLit)?;
1185 dt_str.push_str(&frac_tok.text);
1186 }
1187 }
1188 }
1189
1190 self.try_consume_timezone(&mut dt_str)?;
1192 }
1193 }
1194
1195 if let Ok(dtv) = dt_str.parse::<crate::literals::DateTimeValue>() {
1196 return Ok(Value::Date(dtv));
1197 }
1198
1199 Err(Error::parsing(
1200 format!("Invalid date/time format: '{}'", dt_str),
1201 self.make_source(start_span),
1202 None::<String>,
1203 ))
1204 }
1205
1206 fn try_consume_timezone(&mut self, dt_str: &mut String) -> Result<(), Error> {
1207 if self.at(&TokenKind::Identifier)? {
1209 let peeked = self.peek()?;
1210 if peeked.text == "Z" || peeked.text == "z" {
1211 let z_tok = self.next()?;
1212 dt_str.push_str(&z_tok.text);
1213 return Ok(());
1214 }
1215 }
1216
1217 if self.at(&TokenKind::Plus)? || self.at(&TokenKind::Minus)? {
1219 let sign_tok = self.next()?;
1220 dt_str.push_str(&sign_tok.text);
1221 let hour_tok = self.expect(&TokenKind::NumberLit)?;
1222 dt_str.push_str(&hour_tok.text);
1223 if self.at(&TokenKind::Colon)? {
1224 self.next()?;
1225 dt_str.push(':');
1226 let min_tok = self.expect(&TokenKind::NumberLit)?;
1227 dt_str.push_str(&min_tok.text);
1228 }
1229 }
1230
1231 Ok(())
1232 }
1233
1234 fn try_parse_time_literal(
1235 &mut self,
1236 hour_text: String,
1237 start_span: Span,
1238 ) -> Result<Value, Error> {
1239 let mut time_str = hour_text;
1240
1241 self.expect(&TokenKind::Colon)?;
1243 time_str.push(':');
1244 let min_tok = self.expect(&TokenKind::NumberLit)?;
1245 time_str.push_str(&min_tok.text);
1246
1247 if self.at(&TokenKind::Colon)? {
1249 self.next()?;
1250 time_str.push(':');
1251 let sec_tok = self.expect(&TokenKind::NumberLit)?;
1252 time_str.push_str(&sec_tok.text);
1253 }
1254
1255 self.try_consume_timezone(&mut time_str)?;
1257
1258 if let Ok(t) = time_str.parse::<chrono::NaiveTime>() {
1259 use chrono::Timelike;
1260 return Ok(Value::Time(TimeValue {
1261 hour: t.hour() as u8,
1262 minute: t.minute() as u8,
1263 second: t.second() as u8,
1264 timezone: None,
1265 }));
1266 }
1267
1268 Err(Error::parsing(
1269 format!("Invalid time format: '{}'", time_str),
1270 self.make_source(start_span),
1271 None::<String>,
1272 ))
1273 }
1274
1275 fn new_expression(
1280 &mut self,
1281 kind: ExpressionKind,
1282 source: Source,
1283 ) -> Result<Expression, Error> {
1284 self.expression_count += 1;
1285 if self.expression_count > self.max_expression_count {
1286 return Err(Error::resource_limit_exceeded(
1287 "max_expression_count",
1288 self.max_expression_count.to_string(),
1289 self.expression_count.to_string(),
1290 "Split logic into multiple rules to reduce expression count",
1291 Some(source),
1292 None,
1293 None,
1294 ));
1295 }
1296 Ok(Expression::new(kind, source))
1297 }
1298
1299 fn check_depth(&mut self) -> Result<(), Error> {
1300 if let Err(actual) = self.depth_tracker.push_depth() {
1301 let span = self.peek()?.span.clone();
1302 self.depth_tracker.pop_depth();
1303 return Err(Error::resource_limit_exceeded(
1304 "max_expression_depth",
1305 self.depth_tracker.max_depth().to_string(),
1306 actual.to_string(),
1307 "Simplify nested expressions or break into separate rules",
1308 Some(self.make_source(span)),
1309 None,
1310 None,
1311 ));
1312 }
1313 Ok(())
1314 }
1315
1316 fn parse_expression(&mut self) -> Result<Expression, Error> {
1317 self.check_depth()?;
1318 let result = self.parse_and_expression();
1319 self.depth_tracker.pop_depth();
1320 result
1321 }
1322
1323 fn parse_and_expression(&mut self) -> Result<Expression, Error> {
1324 let start_span = self.peek()?.span.clone();
1325 let mut left = self.parse_and_operand()?;
1326
1327 while self.at(&TokenKind::And)? {
1328 self.next()?; let right = self.parse_and_operand()?;
1330 let span = self.span_covering(
1331 &start_span,
1332 &right
1333 .source_location
1334 .as_ref()
1335 .map(|s| s.span.clone())
1336 .unwrap_or_else(|| start_span.clone()),
1337 );
1338 left = self.new_expression(
1339 ExpressionKind::LogicalAnd(Arc::new(left), Arc::new(right)),
1340 self.make_source(span),
1341 )?;
1342 }
1343
1344 Ok(left)
1345 }
1346
1347 fn parse_and_operand(&mut self) -> Result<Expression, Error> {
1348 if self.at(&TokenKind::Not)? {
1350 return self.parse_not_expression();
1351 }
1352
1353 self.parse_base_with_suffix()
1355 }
1356
1357 fn parse_not_expression(&mut self) -> Result<Expression, Error> {
1358 let not_tok = self.expect(&TokenKind::Not)?;
1359 let start_span = not_tok.span.clone();
1360
1361 self.check_depth()?;
1362 let operand = self.parse_and_operand()?;
1363 self.depth_tracker.pop_depth();
1364
1365 let end_span = operand
1366 .source_location
1367 .as_ref()
1368 .map(|s| s.span.clone())
1369 .unwrap_or_else(|| start_span.clone());
1370 let span = self.span_covering(&start_span, &end_span);
1371
1372 self.new_expression(
1373 ExpressionKind::LogicalNegation(Arc::new(operand), NegationType::Not),
1374 self.make_source(span),
1375 )
1376 }
1377
1378 fn parse_base_with_suffix(&mut self) -> Result<Expression, Error> {
1379 let start_span = self.peek()?.span.clone();
1380 let base = self.parse_base_expression()?;
1381
1382 let peeked = self.peek()?;
1384
1385 if is_comparison_operator(&peeked.kind) {
1387 return self.parse_comparison_suffix(base, start_span);
1388 }
1389
1390 if peeked.kind == TokenKind::Not {
1394 return self.parse_not_in_calendar_suffix(base, start_span);
1395 }
1396
1397 if peeked.kind == TokenKind::In {
1399 return self.parse_in_suffix(base, start_span);
1400 }
1401
1402 Ok(base)
1403 }
1404
1405 fn parse_comparison_suffix(
1406 &mut self,
1407 left: Expression,
1408 start_span: Span,
1409 ) -> Result<Expression, Error> {
1410 let operator = self.parse_comparison_operator()?;
1411
1412 let right = if self.at(&TokenKind::Not)? {
1414 self.parse_not_expression()?
1415 } else {
1416 let rhs = self.parse_base_expression()?;
1417 if self.at(&TokenKind::In)? {
1419 self.parse_in_suffix(rhs, start_span.clone())?
1420 } else {
1421 rhs
1422 }
1423 };
1424
1425 let end_span = right
1426 .source_location
1427 .as_ref()
1428 .map(|s| s.span.clone())
1429 .unwrap_or_else(|| start_span.clone());
1430 let span = self.span_covering(&start_span, &end_span);
1431
1432 self.new_expression(
1433 ExpressionKind::Comparison(Arc::new(left), operator, Arc::new(right)),
1434 self.make_source(span),
1435 )
1436 }
1437
1438 fn parse_comparison_operator(&mut self) -> Result<ComparisonComputation, Error> {
1439 let tok = self.next()?;
1440 match tok.kind {
1441 TokenKind::Gt => Ok(ComparisonComputation::GreaterThan),
1442 TokenKind::Lt => Ok(ComparisonComputation::LessThan),
1443 TokenKind::Gte => Ok(ComparisonComputation::GreaterThanOrEqual),
1444 TokenKind::Lte => Ok(ComparisonComputation::LessThanOrEqual),
1445 TokenKind::EqEq => Ok(ComparisonComputation::Equal),
1446 TokenKind::BangEq => Ok(ComparisonComputation::NotEqual),
1447 TokenKind::Is => {
1448 if self.at(&TokenKind::Not)? {
1450 self.next()?; Ok(ComparisonComputation::IsNot)
1452 } else {
1453 Ok(ComparisonComputation::Is)
1454 }
1455 }
1456 _ => Err(self.error_at_token(
1457 &tok,
1458 format!("Expected a comparison operator, found {}", tok.kind),
1459 )),
1460 }
1461 }
1462
1463 fn parse_not_in_calendar_suffix(
1464 &mut self,
1465 base: Expression,
1466 start_span: Span,
1467 ) -> Result<Expression, Error> {
1468 self.expect(&TokenKind::Not)?;
1469 self.expect(&TokenKind::In)?;
1470 self.expect(&TokenKind::Calendar)?;
1471 let unit = self.parse_calendar_unit()?;
1472 let end = self.peek()?.span.clone();
1473 let span = self.span_covering(&start_span, &end);
1474 self.new_expression(
1475 ExpressionKind::DateCalendar(DateCalendarKind::NotIn, unit, Arc::new(base)),
1476 self.make_source(span),
1477 )
1478 }
1479
1480 fn parse_in_suffix(&mut self, base: Expression, start_span: Span) -> Result<Expression, Error> {
1481 self.expect(&TokenKind::In)?;
1482
1483 let peeked = self.peek()?;
1484
1485 if peeked.kind == TokenKind::Past || peeked.kind == TokenKind::Future {
1487 let direction = self.next()?;
1488 let rel_kind = if direction.kind == TokenKind::Past {
1489 DateRelativeKind::InPast
1490 } else {
1491 DateRelativeKind::InFuture
1492 };
1493
1494 if self.at(&TokenKind::Calendar)? {
1496 self.next()?; let cal_kind = if direction.kind == TokenKind::Past {
1498 DateCalendarKind::Past
1499 } else {
1500 DateCalendarKind::Future
1501 };
1502 let unit = self.parse_calendar_unit()?;
1503 let end = self.peek()?.span.clone();
1504 let span = self.span_covering(&start_span, &end);
1505 return self.new_expression(
1506 ExpressionKind::DateCalendar(cal_kind, unit, Arc::new(base)),
1507 self.make_source(span),
1508 );
1509 }
1510
1511 let tolerance = if !self.at(&TokenKind::And)?
1513 && !self.at(&TokenKind::Unless)?
1514 && !self.at(&TokenKind::Then)?
1515 && !self.at(&TokenKind::Eof)?
1516 && !is_comparison_operator(&self.peek()?.kind)
1517 {
1518 let peek_kind = self.peek()?.kind.clone();
1519 if peek_kind == TokenKind::NumberLit
1520 || peek_kind == TokenKind::LParen
1521 || can_be_reference_segment(&peek_kind)
1522 || is_math_function(&peek_kind)
1523 {
1524 Some(Arc::new(self.parse_base_expression()?))
1525 } else {
1526 None
1527 }
1528 } else {
1529 None
1530 };
1531
1532 let end = self.peek()?.span.clone();
1533 let span = self.span_covering(&start_span, &end);
1534 return self.new_expression(
1535 ExpressionKind::DateRelative(rel_kind, Arc::new(base), tolerance),
1536 self.make_source(span),
1537 );
1538 }
1539
1540 if peeked.kind == TokenKind::Calendar {
1542 self.next()?; let unit = self.parse_calendar_unit()?;
1544 let end = self.peek()?.span.clone();
1545 let span = self.span_covering(&start_span, &end);
1546 return self.new_expression(
1547 ExpressionKind::DateCalendar(DateCalendarKind::Current, unit, Arc::new(base)),
1548 self.make_source(span),
1549 );
1550 }
1551
1552 let target_tok = self.next()?;
1554 let target = conversion_target_from_token(&target_tok.kind, &target_tok.text);
1555
1556 let converted = self.new_expression(
1557 ExpressionKind::UnitConversion(Arc::new(base), target),
1558 self.make_source(self.span_covering(&start_span, &target_tok.span)),
1559 )?;
1560
1561 if is_comparison_operator(&self.peek()?.kind) {
1563 return self.parse_comparison_suffix(converted, start_span);
1564 }
1565
1566 Ok(converted)
1567 }
1568
1569 fn parse_calendar_unit(&mut self) -> Result<CalendarUnit, Error> {
1570 let tok = self.next()?;
1571 if !is_calendar_unit_token(&tok.kind) {
1572 return Err(self.error_at_token(
1573 &tok,
1574 format!("Expected 'year', 'month', or 'week', found '{}'", tok.text),
1575 ));
1576 }
1577 Ok(token_kind_to_calendar_unit(&tok.kind))
1578 }
1579
1580 fn parse_base_expression(&mut self) -> Result<Expression, Error> {
1585 let start_span = self.peek()?.span.clone();
1586 let mut left = self.parse_term()?;
1587
1588 while self.at_any(&[TokenKind::Plus, TokenKind::Minus])? {
1589 let op_tok = self.next()?;
1592 let operation = match op_tok.kind {
1593 TokenKind::Plus => ArithmeticComputation::Add,
1594 TokenKind::Minus => ArithmeticComputation::Subtract,
1595 _ => unreachable!("BUG: only + and - should reach here"),
1596 };
1597
1598 let right = self.parse_term()?;
1599 let end_span = right
1600 .source_location
1601 .as_ref()
1602 .map(|s| s.span.clone())
1603 .unwrap_or_else(|| start_span.clone());
1604 let span = self.span_covering(&start_span, &end_span);
1605
1606 left = self.new_expression(
1607 ExpressionKind::Arithmetic(Arc::new(left), operation, Arc::new(right)),
1608 self.make_source(span),
1609 )?;
1610 }
1611
1612 Ok(left)
1613 }
1614
1615 fn parse_term(&mut self) -> Result<Expression, Error> {
1616 let start_span = self.peek()?.span.clone();
1617 let mut left = self.parse_power()?;
1618
1619 while self.at_any(&[TokenKind::Star, TokenKind::Slash, TokenKind::Percent])? {
1620 let op_tok = self.next()?;
1623 let operation = match op_tok.kind {
1624 TokenKind::Star => ArithmeticComputation::Multiply,
1625 TokenKind::Slash => ArithmeticComputation::Divide,
1626 TokenKind::Percent => ArithmeticComputation::Modulo,
1627 _ => unreachable!("BUG: only *, /, % should reach here"),
1628 };
1629
1630 let right = self.parse_power()?;
1631 let end_span = right
1632 .source_location
1633 .as_ref()
1634 .map(|s| s.span.clone())
1635 .unwrap_or_else(|| start_span.clone());
1636 let span = self.span_covering(&start_span, &end_span);
1637
1638 left = self.new_expression(
1639 ExpressionKind::Arithmetic(Arc::new(left), operation, Arc::new(right)),
1640 self.make_source(span),
1641 )?;
1642 }
1643
1644 Ok(left)
1645 }
1646
1647 fn parse_power(&mut self) -> Result<Expression, Error> {
1648 let start_span = self.peek()?.span.clone();
1649 let left = self.parse_factor()?;
1650
1651 if self.at(&TokenKind::Caret)? {
1652 self.next()?;
1653 self.check_depth()?;
1654 let right = self.parse_power()?;
1655 self.depth_tracker.pop_depth();
1656 let end_span = right
1657 .source_location
1658 .as_ref()
1659 .map(|s| s.span.clone())
1660 .unwrap_or_else(|| start_span.clone());
1661 let span = self.span_covering(&start_span, &end_span);
1662
1663 return self.new_expression(
1664 ExpressionKind::Arithmetic(
1665 Arc::new(left),
1666 ArithmeticComputation::Power,
1667 Arc::new(right),
1668 ),
1669 self.make_source(span),
1670 );
1671 }
1672
1673 Ok(left)
1674 }
1675
1676 fn parse_factor(&mut self) -> Result<Expression, Error> {
1677 let peeked = self.peek()?;
1678 let start_span = peeked.span.clone();
1679
1680 if peeked.kind == TokenKind::Minus {
1681 self.next()?;
1682 let operand = self.parse_primary_or_math()?;
1683 let end_span = operand
1684 .source_location
1685 .as_ref()
1686 .map(|s| s.span.clone())
1687 .unwrap_or_else(|| start_span.clone());
1688 let span = self.span_covering(&start_span, &end_span);
1689
1690 let zero = self.new_expression(
1691 ExpressionKind::Literal(Value::Number(Decimal::ZERO)),
1692 self.make_source(start_span),
1693 )?;
1694 return self.new_expression(
1695 ExpressionKind::Arithmetic(
1696 Arc::new(zero),
1697 ArithmeticComputation::Subtract,
1698 Arc::new(operand),
1699 ),
1700 self.make_source(span),
1701 );
1702 }
1703
1704 if peeked.kind == TokenKind::Plus {
1705 self.next()?;
1706 return self.parse_primary_or_math();
1707 }
1708
1709 self.parse_primary_or_math()
1710 }
1711
1712 fn parse_primary_or_math(&mut self) -> Result<Expression, Error> {
1713 let peeked = self.peek()?;
1714
1715 if is_math_function(&peeked.kind) {
1717 return self.parse_math_function();
1718 }
1719
1720 self.parse_primary()
1721 }
1722
1723 fn parse_math_function(&mut self) -> Result<Expression, Error> {
1724 let func_tok = self.next()?;
1725 let start_span = func_tok.span.clone();
1726
1727 let operator = match func_tok.kind {
1728 TokenKind::Sqrt => MathematicalComputation::Sqrt,
1729 TokenKind::Sin => MathematicalComputation::Sin,
1730 TokenKind::Cos => MathematicalComputation::Cos,
1731 TokenKind::Tan => MathematicalComputation::Tan,
1732 TokenKind::Asin => MathematicalComputation::Asin,
1733 TokenKind::Acos => MathematicalComputation::Acos,
1734 TokenKind::Atan => MathematicalComputation::Atan,
1735 TokenKind::Log => MathematicalComputation::Log,
1736 TokenKind::Exp => MathematicalComputation::Exp,
1737 TokenKind::Abs => MathematicalComputation::Abs,
1738 TokenKind::Floor => MathematicalComputation::Floor,
1739 TokenKind::Ceil => MathematicalComputation::Ceil,
1740 TokenKind::Round => MathematicalComputation::Round,
1741 _ => unreachable!("BUG: only math functions should reach here"),
1742 };
1743
1744 self.check_depth()?;
1745 let operand = self.parse_base_expression()?;
1746 self.depth_tracker.pop_depth();
1747
1748 let end_span = operand
1749 .source_location
1750 .as_ref()
1751 .map(|s| s.span.clone())
1752 .unwrap_or_else(|| start_span.clone());
1753 let span = self.span_covering(&start_span, &end_span);
1754
1755 self.new_expression(
1756 ExpressionKind::MathematicalComputation(operator, Arc::new(operand)),
1757 self.make_source(span),
1758 )
1759 }
1760
1761 fn parse_primary(&mut self) -> Result<Expression, Error> {
1762 let peeked = self.peek()?;
1763 let start_span = peeked.span.clone();
1764
1765 match &peeked.kind {
1766 TokenKind::LParen => {
1768 self.next()?; let inner = self.parse_expression()?;
1770 self.expect(&TokenKind::RParen)?;
1771 Ok(inner)
1772 }
1773
1774 TokenKind::Now => {
1776 let tok = self.next()?;
1777 self.new_expression(ExpressionKind::Now, self.make_source(tok.span))
1778 }
1779
1780 TokenKind::StringLit => {
1782 let tok = self.next()?;
1783 let content = unquote_string(&tok.text);
1784 self.new_expression(
1785 ExpressionKind::Literal(Value::Text(content)),
1786 self.make_source(tok.span),
1787 )
1788 }
1789
1790 k if is_boolean_keyword(k) => {
1792 let tok = self.next()?;
1793 self.new_expression(
1794 ExpressionKind::Literal(Value::Boolean(token_kind_to_boolean_value(&tok.kind))),
1795 self.make_source(tok.span),
1796 )
1797 }
1798
1799 TokenKind::NumberLit => self.parse_number_expression(),
1801
1802 k if can_be_reference_segment(k) => {
1804 let reference = self.parse_expression_reference()?;
1805 let end_span = self.peek()?.span.clone();
1806 let span = self.span_covering(&start_span, &end_span);
1807 self.new_expression(ExpressionKind::Reference(reference), self.make_source(span))
1808 }
1809
1810 _ => {
1811 let tok = self.next()?;
1812 Err(self.error_at_token(
1813 &tok,
1814 format!("Expected an expression, found '{}'", tok.text),
1815 ))
1816 }
1817 }
1818 }
1819
1820 fn parse_number_expression(&mut self) -> Result<Expression, Error> {
1821 let num_tok = self.next()?;
1822 let num_text = num_tok.text.clone();
1823 let start_span = num_tok.span.clone();
1824
1825 if num_text.len() == 4
1827 && num_text.chars().all(|c| c.is_ascii_digit())
1828 && self.at(&TokenKind::Minus)?
1829 {
1830 let minus_span = self.peek()?.span.clone();
1837 if minus_span.start == start_span.end {
1839 let value = self.parse_date_literal(num_text, start_span.clone())?;
1840 return self
1841 .new_expression(ExpressionKind::Literal(value), self.make_source(start_span));
1842 }
1843 }
1844
1845 if num_text.len() == 2
1847 && num_text.chars().all(|c| c.is_ascii_digit())
1848 && self.at(&TokenKind::Colon)?
1849 {
1850 let colon_span = self.peek()?.span.clone();
1851 if colon_span.start == start_span.end {
1852 let value = self.try_parse_time_literal(num_text, start_span.clone())?;
1853 return self
1854 .new_expression(ExpressionKind::Literal(value), self.make_source(start_span));
1855 }
1856 }
1857
1858 if self.at(&TokenKind::PercentPercent)? {
1860 let pp_tok = self.next()?;
1861 if let Ok(next_peek) = self.peek() {
1862 if next_peek.kind == TokenKind::NumberLit {
1863 return Err(self.error_at_token(
1864 &pp_tok,
1865 "Permille literal cannot be followed by a digit",
1866 ));
1867 }
1868 }
1869 let decimal = parse_decimal_string(&num_text, &start_span, self)?;
1870 let ratio_value = decimal / Decimal::from(1000);
1871 return self.new_expression(
1872 ExpressionKind::Literal(Value::Ratio(ratio_value, Some("permille".to_string()))),
1873 self.make_source(start_span),
1874 );
1875 }
1876
1877 if self.at(&TokenKind::Percent)? {
1879 let pct_span = self.peek()?.span.clone();
1880 let pct_tok = self.next()?;
1883 if let Ok(next_peek) = self.peek() {
1884 if next_peek.kind == TokenKind::NumberLit || next_peek.kind == TokenKind::Percent {
1885 return Err(self.error_at_token(
1886 &pct_tok,
1887 "Percent literal cannot be followed by a digit",
1888 ));
1889 }
1890 }
1891 let decimal = parse_decimal_string(&num_text, &start_span, self)?;
1892 let ratio_value = decimal / Decimal::from(100);
1893 return self.new_expression(
1894 ExpressionKind::Literal(Value::Ratio(ratio_value, Some("percent".to_string()))),
1895 self.make_source(self.span_covering(&start_span, &pct_span)),
1896 );
1897 }
1898
1899 if self.at(&TokenKind::PercentKw)? {
1901 self.next()?;
1902 let decimal = parse_decimal_string(&num_text, &start_span, self)?;
1903 let ratio_value = decimal / Decimal::from(100);
1904 return self.new_expression(
1905 ExpressionKind::Literal(Value::Ratio(ratio_value, Some("percent".to_string()))),
1906 self.make_source(start_span),
1907 );
1908 }
1909
1910 if self.at(&TokenKind::Permille)? {
1912 self.next()?;
1913 let decimal = parse_decimal_string(&num_text, &start_span, self)?;
1914 let ratio_value = decimal / Decimal::from(1000);
1915 return self.new_expression(
1916 ExpressionKind::Literal(Value::Ratio(ratio_value, Some("permille".to_string()))),
1917 self.make_source(start_span),
1918 );
1919 }
1920
1921 if is_duration_unit(&self.peek()?.kind) && self.peek()?.kind != TokenKind::PercentKw {
1923 let unit_tok = self.next()?;
1924 let decimal = parse_decimal_string(&num_text, &start_span, self)?;
1925 let duration_unit = token_kind_to_duration_unit(&unit_tok.kind);
1926 return self.new_expression(
1927 ExpressionKind::Literal(Value::Duration(decimal, duration_unit)),
1928 self.make_source(self.span_covering(&start_span, &unit_tok.span)),
1929 );
1930 }
1931
1932 if can_be_label(&self.peek()?.kind) {
1934 let unit_tok = self.next()?;
1935 let decimal = parse_decimal_string(&num_text, &start_span, self)?;
1936 return self.new_expression(
1937 ExpressionKind::UnresolvedUnitLiteral(decimal, unit_tok.text.clone()),
1938 self.make_source(self.span_covering(&start_span, &unit_tok.span)),
1939 );
1940 }
1941
1942 let decimal = parse_decimal_string(&num_text, &start_span, self)?;
1944 self.new_expression(
1945 ExpressionKind::Literal(Value::Number(decimal)),
1946 self.make_source(start_span),
1947 )
1948 }
1949
1950 fn parse_expression_reference(&mut self) -> Result<Reference, Error> {
1951 let mut segments = Vec::new();
1952
1953 let first = self.next()?;
1954 segments.push(first.text.clone());
1955
1956 while self.at(&TokenKind::Dot)? {
1957 self.next()?; let seg = self.next()?;
1959 if !can_be_reference_segment(&seg.kind) {
1960 return Err(self.error_at_token(
1961 &seg,
1962 format!("Expected an identifier after '.', found {}", seg.kind),
1963 ));
1964 }
1965 segments.push(seg.text.clone());
1966 }
1967
1968 Ok(Reference::from_path(segments))
1969 }
1970}
1971
1972fn unquote_string(s: &str) -> String {
1977 if s.len() >= 2 && s.starts_with('"') && s.ends_with('"') {
1978 s[1..s.len() - 1].to_string()
1979 } else {
1980 s.to_string()
1981 }
1982}
1983
1984fn parse_decimal_string(text: &str, span: &Span, parser: &Parser) -> Result<Decimal, Error> {
1985 let clean = text.replace(['_', ','], "");
1986 Decimal::from_str(&clean).map_err(|_| {
1987 Error::parsing(
1988 format!(
1989 "Invalid number: '{}'. Expected a valid decimal number (e.g., 42, 3.14, 1_000_000)",
1990 text
1991 ),
1992 parser.make_source(span.clone()),
1993 None::<String>,
1994 )
1995 })
1996}
1997
1998fn is_comparison_operator(kind: &TokenKind) -> bool {
1999 matches!(
2000 kind,
2001 TokenKind::Gt
2002 | TokenKind::Lt
2003 | TokenKind::Gte
2004 | TokenKind::Lte
2005 | TokenKind::EqEq
2006 | TokenKind::BangEq
2007 | TokenKind::Is
2008 )
2009}
2010
2011impl TokenKind {
2013 fn is_identifier_like(&self) -> bool {
2014 matches!(self, TokenKind::Identifier)
2015 || can_be_label(self)
2016 || is_type_keyword(self)
2017 || is_boolean_keyword(self)
2018 || is_duration_unit(self)
2019 || is_math_function(self)
2020 }
2021}