mago_syntax/ast/
trivia.rs

1use serde::Serialize;
2use strum::Display;
3
4use mago_span::HasSpan;
5use mago_span::Span;
6
7use crate::ast::Sequence;
8
9/// Represents the kind of trivia.
10#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Serialize, PartialOrd, Ord, Display)]
11#[serde(tag = "type", content = "value")]
12pub enum TriviaKind {
13    WhiteSpace,
14    SingleLineComment,
15    MultiLineComment,
16    HashComment,
17    DocBlockComment,
18}
19
20/// Represents a trivia.
21///
22/// A trivia is a piece of information that is not part of the syntax tree,
23/// such as comments and white spaces.
24#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Serialize, PartialOrd, Ord)]
25pub struct Trivia<'arena> {
26    pub kind: TriviaKind,
27    pub span: Span,
28    pub value: &'arena str,
29}
30
31impl TriviaKind {
32    /// Returns `true` if the trivia kind is a comment.
33    #[inline]
34    pub const fn is_comment(&self) -> bool {
35        matches!(
36            self,
37            TriviaKind::SingleLineComment
38                | TriviaKind::MultiLineComment
39                | TriviaKind::HashComment
40                | TriviaKind::DocBlockComment
41        )
42    }
43
44    #[inline]
45    pub const fn is_docblock(&self) -> bool {
46        matches!(self, TriviaKind::DocBlockComment)
47    }
48
49    #[inline]
50    pub const fn is_block_comment(&self) -> bool {
51        matches!(self, TriviaKind::MultiLineComment | TriviaKind::DocBlockComment)
52    }
53
54    #[inline]
55    pub const fn is_single_line_comment(&self) -> bool {
56        matches!(self, TriviaKind::HashComment | TriviaKind::SingleLineComment)
57    }
58}
59
60impl HasSpan for Trivia<'_> {
61    fn span(&self) -> Span {
62        self.span
63    }
64}
65
66impl<'arena> Sequence<'arena, Trivia<'arena>> {
67    /// Returns an iterator over the comments in the sequence.
68    pub fn comments(&self) -> impl Iterator<Item = &Trivia<'arena>> {
69        self.iter().filter(|trivia| trivia.kind.is_comment())
70    }
71}