Skip to main content

microcad_lang/syntax/expression/
range_expression.rs

1// Copyright © 2025-2026 The µcad authors <info@microcad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4//! Range expression
5
6use derive_more::Deref;
7use microcad_lang_base::{SrcRef, SrcReferrer, TreeDisplay, TreeState};
8
9use crate::syntax::*;
10
11/// Range start.
12#[derive(Clone, Default, Deref, PartialEq)]
13pub struct RangeFirst(pub Box<Expression>);
14
15impl SrcReferrer for RangeFirst {
16    fn src_ref(&self) -> SrcRef {
17        self.0.src_ref()
18    }
19}
20
21impl std::fmt::Display for RangeFirst {
22    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
23        write!(f, "{}", self.0)
24    }
25}
26
27impl std::fmt::Debug for RangeFirst {
28    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
29        write!(f, "{:?}", self.0)
30    }
31}
32
33impl TreeDisplay for RangeFirst {
34    fn tree_print(&self, f: &mut std::fmt::Formatter, mut depth: TreeState) -> std::fmt::Result {
35        writeln!(f, "{:depth$}RangeStart:", "")?;
36        depth.indent();
37        self.0.tree_print(f, depth)
38    }
39}
40
41/// Range end.
42#[derive(Clone, Default, Deref, PartialEq)]
43pub struct RangeLast(pub Box<Expression>);
44
45impl SrcReferrer for RangeLast {
46    fn src_ref(&self) -> SrcRef {
47        self.0.src_ref()
48    }
49}
50
51impl std::fmt::Display for RangeLast {
52    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
53        write!(f, "{}", self.0)
54    }
55}
56
57impl std::fmt::Debug for RangeLast {
58    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
59        write!(f, "{:?}", self.0)
60    }
61}
62
63impl TreeDisplay for RangeLast {
64    fn tree_print(&self, f: &mut std::fmt::Formatter, mut depth: TreeState) -> std::fmt::Result {
65        writeln!(f, "{:depth$}RangeLast:", "")?;
66        depth.indent();
67        self.0.tree_print(f, depth)
68    }
69}
70
71/// Range expression, e.g. `a..b`.
72#[derive(Clone, Default, PartialEq)]
73pub struct RangeExpression {
74    /// First value in the range.
75    pub first: RangeFirst,
76    /// Last value in the range.
77    pub last: RangeLast,
78    /// Source code reference.
79    pub src_ref: SrcRef,
80}
81
82impl SrcReferrer for RangeExpression {
83    fn src_ref(&self) -> SrcRef {
84        self.src_ref.clone()
85    }
86}
87
88impl std::fmt::Display for RangeExpression {
89    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
90        write!(f, "{}..{}", self.first, self.last)
91    }
92}
93
94impl std::fmt::Debug for RangeExpression {
95    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
96        write!(f, "{:?}..{:?}", self.first, self.last)
97    }
98}
99
100impl TreeDisplay for RangeExpression {
101    fn tree_print(&self, f: &mut std::fmt::Formatter, mut depth: TreeState) -> std::fmt::Result {
102        writeln!(f, "{:depth$}RangeExpression:", "")?;
103        depth.indent();
104        self.first.tree_print(f, depth)?;
105        self.last.tree_print(f, depth)
106    }
107}