lib_ruby_parser/source/comment.rs
1use crate::loc_ext::LocExt;
2use crate::source::DecodedInput;
3use crate::Loc;
4
5/// Enum of all possible comment types
6#[repr(C)]
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub enum CommentType {
9 /// Inline comment like
10 ///
11 /// ```text
12 /// # comment
13 /// ```
14 Inline,
15
16 /// Document comment like
17 ///
18 /// ```text
19 /// =begin
20 /// comment
21 /// =end
22 /// ```
23 Document,
24
25 /// Uknknown comment type,
26 /// most probably means that either `Loc` or given `Input` is invalid
27 Unknown,
28}
29
30/// A struct that represents a comment in Ruby
31#[repr(C)]
32#[derive(Debug, Clone, PartialEq, Eq)]
33pub struct Comment {
34 /// Location of the comment (starts with `#` and ends with the last char)
35 pub location: Loc,
36
37 /// Kind of the comment
38 pub kind: CommentType,
39}
40
41impl Comment {
42 /// Constructs a new comment by `Loc` and `Input`
43 pub fn new(location: Loc, input: &DecodedInput) -> Self {
44 let kind = match location.source(input) {
45 Some(source) => {
46 if source.starts_with('#') {
47 CommentType::Inline
48 } else if source.starts_with("=begin") {
49 CommentType::Document
50 } else {
51 CommentType::Unknown
52 }
53 }
54 None => CommentType::Unknown,
55 };
56 Self { location, kind }
57 }
58}