mago_syntax/ast/
trivia.rs1use serde::Serialize;
2use strum::Display;
3
4use mago_span::HasSpan;
5use mago_span::Span;
6
7use crate::ast::Sequence;
8
9#[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#[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 #[inline]
34 #[must_use]
35 pub const fn is_comment(&self) -> bool {
36 matches!(
37 self,
38 TriviaKind::SingleLineComment
39 | TriviaKind::MultiLineComment
40 | TriviaKind::HashComment
41 | TriviaKind::DocBlockComment
42 )
43 }
44
45 #[inline]
46 #[must_use]
47 pub const fn is_docblock(&self) -> bool {
48 matches!(self, TriviaKind::DocBlockComment)
49 }
50
51 #[inline]
52 #[must_use]
53 pub const fn is_block_comment(&self) -> bool {
54 matches!(self, TriviaKind::MultiLineComment | TriviaKind::DocBlockComment)
55 }
56
57 #[inline]
58 #[must_use]
59 pub const fn is_single_line_comment(&self) -> bool {
60 matches!(self, TriviaKind::HashComment | TriviaKind::SingleLineComment)
61 }
62}
63
64impl HasSpan for Trivia<'_> {
65 fn span(&self) -> Span {
66 self.span
67 }
68}
69
70impl<'arena> Sequence<'arena, Trivia<'arena>> {
71 pub fn comments(&self) -> impl Iterator<Item = &Trivia<'arena>> {
73 self.iter().filter(|trivia| trivia.kind.is_comment())
74 }
75}