Expand description
This crate implements a (pull) parser for extracting comments from code in various programming languages.
§Extract Comments from Rust Code
use comment_parser::CommentParser;
let rust = r#"
/* This is
the main
function */
fn main() {
// println! is a macro
println!("Hello World"); // Prints "Hello World"
}
"#;
let rules = comment_parser::get_syntax("rust").unwrap();
let parser = CommentParser::new(rust, rules);
for comment in parser {
println!("{:?}", comment);
}
This will output the following:
BlockComment(_, " This is\nthe main\nfunction ")
LineComment(_, " println! is a macro")
LineComment(_, " Prints \"Hello World\"")
§Extract Comments from Python Code
use comment_parser::CommentParser;
let python = r#"
if __name__ == "__main__":
print("Hello World") # Prints "Hello World"
"#;
let rules = comment_parser::get_syntax("python").unwrap();
let parser = CommentParser::new(python, rules);
for comment in parser {
println!("{:?}", comment);
}
This will output the following:
LineComment(_, " In Python main is not a function")
LineComment(_, " print is a function")
LineComment(_, " Prints \"Hello World\"")
Structs§
- Comment
Parser CommentParser
parsestext
and producesEvent
s.
Enums§
- Event
- Events contain
raw
andtext
. - Language
Error LanguageError
is an error that can be returned byget_syntax_from_path
andget_syntax_from_extension
.- Syntax
Rule - The parser uses a few syntax rules, to be able to identify comments and strings.
Functions§
- get_
syntax - Given a language name, get syntax rules for a predefined
language included in the crate.
Returns
None
if the language is not supported. - get_
syntax_ from_ extension - Given a file
extension
, get syntax rules for a predefined language included in the crate. The casing of theextension
does not affect the result. - get_
syntax_ from_ path - Given a
Path
, get syntax rules for a predefined language included in the crate. The language is identified from the path extension, and the casing of the extension does not affect the result.