Crates.io | comment-parser |
lib.rs | comment-parser |
version | 0.1.0 |
source | src |
created_at | 2020-02-18 18:04:08.785395 |
updated_at | 2020-02-18 18:04:08.785395 |
description | Extract comments from code in various programming languages |
homepage | |
repository | https://github.com/vallentin/comment-parser |
max_upload_size | |
id | 210382 |
size | 32,183 |
This crate implements a (pull) parser for extracting comments from code in various programming languages.
Add this to your Cargo.toml
:
[dependencies]
comment-parser = "0.1"
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\"")
use comment_parser::CommentParser;
let python = r#"
# In Python main is not a function
if __name__ == "__main__":
# print is a function
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\"")