comment-parser 0.1.0

Extract comments from code in various programming languages
Documentation
  • Coverage
  • 100%
    15 out of 15 items documented6 out of 6 items with examples
  • Size
  • Source code size: 32.25 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 3.32 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 9s Average build duration of successful builds.
  • all releases: 9s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • vallentin/comment-parser
    10 3 3
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • vallentin

comment-parser

Build Status Latest Version Docs License

This crate implements a (pull) parser for extracting comments from code in various programming languages.

Usage

Add this to your Cargo.toml:

[dependencies]

comment-parser = "0.1"

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#"
# 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\"")