use super::span::Span;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TriviaKind {
Whitespace,
Newline,
LineComment,
BlockComment,
LineContinuation,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Trivia {
pub kind: TriviaKind,
pub text: String,
pub span: Span,
}
impl Trivia {
pub fn is_comment(&self) -> bool {
matches!(
self.kind,
TriviaKind::LineComment | TriviaKind::BlockComment
)
}
pub fn is_line_break(&self) -> bool {
matches!(
self.kind,
TriviaKind::Newline | TriviaKind::LineContinuation
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn classification_helpers() {
let t = |kind, text: &str| Trivia {
kind,
text: text.to_string(),
span: Span::new(0, text.len()),
};
assert!(t(TriviaKind::LineComment, "# hi").is_comment());
assert!(t(TriviaKind::BlockComment, "<# hi #>").is_comment());
assert!(!t(TriviaKind::Whitespace, " ").is_comment());
assert!(t(TriviaKind::Newline, "\r\n").is_line_break());
assert!(t(TriviaKind::LineContinuation, "`\n").is_line_break());
assert!(!t(TriviaKind::LineComment, "# hi").is_line_break());
}
}