microcad_lang/syntax/expression/
range_expression.rs

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