1 个不稳定版本

0.1.0 2020年2月18日

#2348解析器实现


2 crate 中使用

MIT 许可证

32KB
362

comment-parser

Build Status Latest Version Docs License

此 crate 实现了一个 (拉取) 解析器,用于从各种编程语言的代码中提取注释。

使用方法

将以下内容添加到您的 Cargo.toml

[dependencies]
comment-parser = "0.1"

从 Rust 代码中提取注释

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);
}

这将输出以下内容

BlockComment(_, " This is\nthe main\nfunction ")
LineComment(_, " println! is a macro")
LineComment(_, " Prints \"Hello World\"")

从 Python 代码中提取注释

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);
}

这将输出以下内容

LineComment(_, " In Python main is not a function")
LineComment(_, " print is a function")
LineComment(_, " Prints \"Hello World\"")

依赖项

~57KB