Skip to main content

ruby_prism/parse_result/
comments.rs

1//! Comment handling for the prism parser.
2
3use std::marker::PhantomData;
4
5use ruby_prism_sys::{pm_comment_location, pm_comment_t, pm_comment_type, pm_comment_type_t, pm_magic_comment_key, pm_magic_comment_t, pm_magic_comment_value, pm_node_comment, pm_node_t, pm_parser_start, pm_parser_t};
6
7use super::Location;
8
9/// The type of the comment
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11pub enum CommentType {
12    /// `InlineComment` corresponds to comments that start with #.
13    InlineComment,
14    /// `EmbDocComment` corresponds to comments that are surrounded by =begin and =end.
15    EmbDocComment,
16}
17
18/// A comment that was found during parsing.
19#[derive(Debug)]
20pub struct Comment<'pr> {
21    raw: *const pm_comment_t,
22    parser: *const pm_parser_t,
23    marker: PhantomData<&'pr pm_comment_t>,
24}
25
26impl<'pr> Comment<'pr> {
27    /// Returns the text of the comment.
28    #[must_use]
29    pub fn text(&self) -> &[u8] {
30        self.location().as_slice()
31    }
32
33    /// Returns the type of the comment.
34    #[must_use]
35    pub fn type_(&self) -> CommentType {
36        let type_ = unsafe { pm_comment_type(self.raw) };
37        if type_ == pm_comment_type_t::PM_COMMENT_EMBDOC {
38            CommentType::EmbDocComment
39        } else {
40            CommentType::InlineComment
41        }
42    }
43
44    /// The location of the comment in the source.
45    #[must_use]
46    pub fn location(&self) -> Location<'pr> {
47        let loc = unsafe { pm_comment_location(self.raw) };
48        Location {
49            parser: self.parser,
50            start: loc.start,
51            length: loc.length,
52            marker: PhantomData,
53        }
54    }
55}
56
57/// An iterator over comments collected from the parse result.
58pub struct Comments<'pr> {
59    ptrs: Vec<*const pm_comment_t>,
60    index: usize,
61    parser: *const pm_parser_t,
62    marker: PhantomData<&'pr pm_comment_t>,
63}
64
65impl Comments<'_> {
66    pub(crate) const fn new(ptrs: Vec<*const pm_comment_t>, parser: *const pm_parser_t) -> Self {
67        Comments { ptrs, index: 0, parser, marker: PhantomData }
68    }
69}
70
71impl<'pr> Iterator for Comments<'pr> {
72    type Item = Comment<'pr>;
73
74    fn next(&mut self) -> Option<Self::Item> {
75        if self.index < self.ptrs.len() {
76            let comment = self.ptrs[self.index];
77            self.index += 1;
78            Some(Comment { raw: comment, parser: self.parser, marker: PhantomData })
79        } else {
80            None
81        }
82    }
83}
84
85/// An iterator over the comments attached to a node.
86///
87/// Comments are only attached when the source is parsed with the
88/// [`attach_comments`](crate::Options::attach_comments) option.
89pub struct NodeComments<'pr> {
90    parser: *const pm_parser_t,
91    node: *const pm_node_t,
92    index: u32,
93    count: u32,
94    marker: PhantomData<&'pr pm_comment_t>,
95}
96
97impl NodeComments<'_> {
98    pub(crate) const fn new(parser: *const pm_parser_t, node: *const pm_node_t, count: u32) -> Self {
99        NodeComments { parser, node, index: 0, count, marker: PhantomData }
100    }
101}
102
103impl<'pr> Iterator for NodeComments<'pr> {
104    type Item = Comment<'pr>;
105
106    fn next(&mut self) -> Option<Self::Item> {
107        if self.index < self.count {
108            let comment = unsafe { pm_node_comment(self.parser, self.node, self.index) };
109            self.index += 1;
110            Some(Comment { raw: comment, parser: self.parser, marker: PhantomData })
111        } else {
112            None
113        }
114    }
115
116    fn size_hint(&self) -> (usize, Option<usize>) {
117        let remaining = (self.count - self.index) as usize;
118        (remaining, Some(remaining))
119    }
120}
121
122impl ExactSizeIterator for NodeComments<'_> {}
123
124/// A magic comment that was found during parsing.
125#[derive(Debug)]
126pub struct MagicComment<'pr> {
127    parser: *const pm_parser_t,
128    raw: *const pm_magic_comment_t,
129    marker: PhantomData<&'pr pm_magic_comment_t>,
130}
131
132impl MagicComment<'_> {
133    /// Returns the text of the comment's key.
134    #[must_use]
135    pub fn key(&self) -> &[u8] {
136        unsafe {
137            let loc = pm_magic_comment_key(self.raw);
138            let start = pm_parser_start(self.parser).add(loc.start as usize);
139            std::slice::from_raw_parts(start, loc.length as usize)
140        }
141    }
142
143    /// Returns the text of the comment's value.
144    #[must_use]
145    pub fn value(&self) -> &[u8] {
146        unsafe {
147            let loc = pm_magic_comment_value(self.raw);
148            let start = pm_parser_start(self.parser).add(loc.start as usize);
149            std::slice::from_raw_parts(start, loc.length as usize)
150        }
151    }
152}
153
154/// An iterator over magic comments collected from the parse result.
155pub struct MagicComments<'pr> {
156    ptrs: Vec<*const pm_magic_comment_t>,
157    index: usize,
158    parser: *const pm_parser_t,
159    marker: PhantomData<&'pr pm_magic_comment_t>,
160}
161
162impl MagicComments<'_> {
163    pub(crate) const fn new(ptrs: Vec<*const pm_magic_comment_t>, parser: *const pm_parser_t) -> Self {
164        MagicComments { ptrs, index: 0, parser, marker: PhantomData }
165    }
166}
167
168impl<'pr> Iterator for MagicComments<'pr> {
169    type Item = MagicComment<'pr>;
170
171    fn next(&mut self) -> Option<Self::Item> {
172        if self.index < self.ptrs.len() {
173            let comment = self.ptrs[self.index];
174            self.index += 1;
175            Some(MagicComment { parser: self.parser, raw: comment, marker: PhantomData })
176        } else {
177            None
178        }
179    }
180}