@@ -19,7 +19,7 @@ use std::mem::MaybeUninit;
1919use std:: ptr:: NonNull ;
2020
2121pub use self :: bindings:: * ;
22- use ruby_prism_sys:: { pm_comment_t, pm_constant_id_list_t, pm_constant_id_t, pm_diagnostic_t, pm_integer_t, pm_location_t, pm_magic_comment_t, pm_node_destroy, pm_node_list, pm_node_t, pm_parse, pm_parser_free, pm_parser_init, pm_parser_t} ;
22+ use ruby_prism_sys:: { pm_comment_t, pm_comment_type_t , pm_constant_id_list_t, pm_constant_id_t, pm_diagnostic_t, pm_integer_t, pm_location_t, pm_magic_comment_t, pm_node_destroy, pm_node_list, pm_node_t, pm_parse, pm_parser_free, pm_parser_init, pm_parser_t} ;
2323
2424/// A range in the source file.
2525pub struct Location < ' pr > {
@@ -329,6 +329,15 @@ pub struct Comment<'pr> {
329329 marker : PhantomData < & ' pr pm_comment_t > ,
330330}
331331
332+ /// The type of the comment
333+ #[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
334+ pub enum CommentType {
335+ /// InlineComment corresponds to comments that start with #.
336+ InlineComment ,
337+ /// EmbDocComment corresponds to comments that are surrounded by =begin and =end.
338+ EmbDocComment ,
339+ }
340+
332341impl < ' pr > Comment < ' pr > {
333342 /// Returns the text of the comment.
334343 ///
@@ -339,6 +348,16 @@ impl<'pr> Comment<'pr> {
339348 self . location ( ) . as_slice ( )
340349 }
341350
351+ /// Returns the type of the comment.
352+ pub fn type_ ( & self ) -> CommentType {
353+ let type_ = unsafe { self . comment . as_ref ( ) . type_ } ;
354+ if type_ == pm_comment_type_t:: PM_COMMENT_EMBDOC {
355+ CommentType :: EmbDocComment
356+ } else {
357+ CommentType :: InlineComment
358+ }
359+ }
360+
342361 /// The location of the comment in the source.
343362 #[ must_use]
344363 pub fn location ( & self ) -> Location < ' pr > {
@@ -593,6 +612,7 @@ mod tests {
593612 let result = parse ( source. as_ref ( ) ) ;
594613
595614 for comment in result. comments ( ) {
615+ assert_eq ! ( super :: CommentType :: InlineComment , comment. type_( ) ) ;
596616 let text = std:: str:: from_utf8 ( comment. text ( ) ) . unwrap ( ) ;
597617 assert ! ( text. starts_with( "# comment" ) ) ;
598618 }
0 commit comments