comment_parser/
lib.rs

1//! This crate implements a (pull) parser for extracting comments
2//! from code in various programming languages.
3//!
4//! # Extract Comments from Rust Code
5//!
6//! ```no_run
7//! use comment_parser::CommentParser;
8//!
9//! let rust = r#"
10//! /* This is
11//! the main
12//! function */
13//! fn main() {
14//!     // println! is a macro
15//!     println!("Hello World"); // Prints "Hello World"
16//! }
17//! "#;
18//!
19//! let rules = comment_parser::get_syntax("rust").unwrap();
20//!
21//! let parser = CommentParser::new(rust, rules);
22//!
23//! for comment in parser {
24//!     println!("{:?}", comment);
25//! }
26//! ```
27//!
28//! This will output the following:
29//!
30//! ```text
31//! BlockComment(_, " This is\nthe main\nfunction ")
32//! LineComment(_, " println! is a macro")
33//! LineComment(_, " Prints \"Hello World\"")
34//! ```
35//!
36//! # Extract Comments from Python Code
37//!
38//! ```no_run
39//! use comment_parser::CommentParser;
40//!
41//! let python = r#"
42//! # In Python main is not a function
43//! if __name__ == "__main__":
44//!     # print is a function
45//!     print("Hello World")  # Prints "Hello World"
46//! "#;
47//!
48//! let rules = comment_parser::get_syntax("python").unwrap();
49//!
50//! let parser = CommentParser::new(python, rules);
51//!
52//! for comment in parser {
53//!     println!("{:?}", comment);
54//! }
55//! ```
56//!
57//! This will output the following:
58//!
59//! ```text
60//! LineComment(_, " In Python main is not a function")
61//! LineComment(_, " print is a function")
62//! LineComment(_, " Prints \"Hello World\"")
63//! ```
64
65#![forbid(unsafe_code)]
66#![deny(missing_docs)]
67// #![deny(missing_doc_code_examples)]
68#![deny(missing_debug_implementations)]
69#![warn(clippy::all)]
70
71mod languages;
72mod parse;
73mod syntax;
74
75pub use languages::{get_syntax, get_syntax_from_extension, get_syntax_from_path, LanguageError};
76pub use parse::{CommentParser, Event};
77pub use syntax::SyntaxRule;