Skip to main content

mirascript_core/parser/
list_item.rs

1use std::ops::{Deref, DerefMut};
2
3use super::prelude::*;
4
5/// item ','?
6#[derive(Debug, Clone, PartialEq)]
7pub struct ListItem<'s, T>(pub Box<T>, pub Option<TokenRef<'s>>);
8
9impl<'s, T: AstWalker<'s>> AstWalker<'s> for ListItem<'s, T> {
10    fn collect_diagnostics(&mut self, collector: &mut DiagnosticsCollector<'_, '_>) {
11        self.0.collect_diagnostics(collector);
12        self.1.collect_diagnostics(collector);
13    }
14    fn range(&self) -> SourceRange {
15        match self.1 {
16            Some(ref tail_comma) => self.0.range().start..tail_comma.range.end,
17            None => self.0.range(),
18        }
19    }
20}
21
22impl<'s, T> ListItem<'s, T> {
23    pub fn new_with_comma(item: T, tail_comma: TokenRef<'s>) -> Self {
24        Self(Box::new(item), Some(tail_comma))
25    }
26
27    pub fn new(item: T) -> Self {
28        Self(Box::new(item), None)
29    }
30
31    pub fn has_tail_comma(&self) -> bool {
32        self.tail_comma().is_some()
33    }
34    pub fn tail_comma(&self) -> Option<&Token<'s>> {
35        self.1.as_deref()
36    }
37
38    pub fn unwrap(self) -> T {
39        *self.0
40    }
41}
42
43impl<T> Deref for ListItem<'_, T> {
44    type Target = T;
45
46    fn deref(&self) -> &Self::Target {
47        &self.0
48    }
49}
50
51impl<T> DerefMut for ListItem<'_, T> {
52    fn deref_mut(&mut self) -> &mut Self::Target {
53        &mut self.0
54    }
55}