full_moon/ast/
span.rs

1//! A representation of a "contained span", or a span within specific bounds.
2//!
3//! Examples of contained spans include:
4//! - Arguments in a function call use parentheses `(...)`
5//! - Indexing a table uses brackets `[...]`
6//! - Creating a table uses braces `{...}`
7//!
8//! Contained spans don't contain the inner data, just the start and end bounds.
9use crate::{
10    node::{Node, Tokens},
11    private::Sealed,
12    tokenizer::{Position, TokenReference},
13};
14
15use full_moon_derive::Visit;
16#[cfg(feature = "serde")]
17use serde::{Deserialize, Serialize};
18
19/// A contained span with the beginning and ending bounds.
20/// Refer to the [module documentation](index.html) for more details.
21#[derive(Clone, Debug, PartialEq, Eq, Visit)]
22#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
23pub struct ContainedSpan {
24    pub(crate) tokens: (TokenReference, TokenReference),
25}
26
27impl ContainedSpan {
28    /// Creates a contained span from the start and end bounds
29    pub fn new(start: TokenReference, end: TokenReference) -> Self {
30        Self {
31            tokens: (start, end),
32        }
33    }
34
35    /// Returns the start and end bounds in a tuple as references
36    pub fn tokens(&self) -> (&TokenReference, &TokenReference) {
37        (&self.tokens.0, &self.tokens.1)
38    }
39}
40
41impl Node for ContainedSpan {
42    fn start_position(&self) -> Option<Position> {
43        self.tokens.0.start_position()
44    }
45
46    fn end_position(&self) -> Option<Position> {
47        self.tokens.1.end_position()
48    }
49
50    fn similar(&self, other: &Self) -> bool {
51        self.tokens.0.similar(&other.tokens.0) && self.tokens.1.similar(&other.tokens.1)
52    }
53
54    fn tokens(&self) -> Tokens {
55        self.tokens.tokens()
56    }
57}
58
59impl Sealed for ContainedSpan {}