71个版本

0.13.3 2023年11月29日
0.13.1 2023年3月23日
0.12.2 2022年11月9日
0.12.0 2022年7月5日
0.4.5 2019年11月28日

#2907解析器实现

Download history 384/week @ 2024-04-08 344/week @ 2024-04-15 412/week @ 2024-04-22 544/week @ 2024-04-29 426/week @ 2024-05-06 385/week @ 2024-05-13 595/week @ 2024-05-20 413/week @ 2024-05-27 434/week @ 2024-06-03 504/week @ 2024-06-10 309/week @ 2024-06-17 418/week @ 2024-06-24 196/week @ 2024-07-01 336/week @ 2024-07-08 652/week @ 2024-07-15 642/week @ 2024-07-22

1,860 每月下载量
9 仓库中使用 (3 个直接使用)

MIT/Apache

7KB

sv-parser

符合IEEE 1800-2017的SystemVerilog解析器库。

Actions Status Crates.io Docs.rs

使用sv-parser的工具

  • morty: SystemVerilog源文件序列化工具
  • svinst: 确定SystemVerilog文件中声明的模块和实例化的模块
  • svlint: SystemVerilog代码检查器
  • svls: SystemVerilog语言服务器

用法

[dependencies]
sv-parser = "0.13.3"

sv-parser提供了一个parse_sv函数,该函数返回SyntaxTreeSyntaxTree显示具体语法树。它包含预处理字符串和解析树。

RefNode显示对SyntaxTree中任何节点的引用。您可以通过SyntaxTree的迭代器获取RefNodeRefNode的变体名称遵循IEEE 1800-2017的“A正式语法”。

Locate显示标记的位置。所有SyntaxTree的叶节点都是Locate。您可以通过get_strLocate获取字符串。

示例

以下示例解析SystemVerilog源文件并显示模块名称。

use std::collections::HashMap;
use std::env;
use std::path::PathBuf;
use sv_parser::{parse_sv, unwrap_node, Locate, RefNode};

fn main() {
    let args: Vec<String> = env::args().collect();

    // The path of SystemVerilog source file
    let path = PathBuf::from(&args[1]);
    // The list of defined macros
    let defines = HashMap::new();
    // The list of include paths
    let includes: Vec<PathBuf> = Vec::new();

    // Parse
    let result = parse_sv(&path, &defines, &includes, false, false);

    if let Ok((syntax_tree, _)) = result {
        // &SyntaxTree is iterable
        for node in &syntax_tree {
            // The type of each node is RefNode
            match node {
                RefNode::ModuleDeclarationNonansi(x) => {
                    // unwrap_node! gets the nearest ModuleIdentifier from x
                    let id = unwrap_node!(x, ModuleIdentifier).unwrap();

                    let id = get_identifier(id).unwrap();

                    // Original string can be got by SyntaxTree::get_str(self, locate: &Locate)
                    let id = syntax_tree.get_str(&id).unwrap();
                    println!("module: {}", id);
                }
                RefNode::ModuleDeclarationAnsi(x) => {
                    let id = unwrap_node!(x, ModuleIdentifier).unwrap();
                    let id = get_identifier(id).unwrap();
                    let id = syntax_tree.get_str(&id).unwrap();
                    println!("module: {}", id);
                }
                _ => (),
            }
        }
    } else {
        println!("Parse failed");
    }
}

fn get_identifier(node: RefNode) -> Option<Locate> {
    // unwrap_node! can take multiple types
    match unwrap_node!(node, SimpleIdentifier, EscapedIdentifier) {
        Some(RefNode::SimpleIdentifier(x)) => {
            return Some(x.nodes.0);
        }
        Some(RefNode::EscapedIdentifier(x)) => {
            return Some(x.nodes.0);
        }
        _ => None,
    }
}

许可证

在以下许可证中选择一个

任选。

贡献

除非您明确声明,否则根据Apache-2.0许可定义的,您有意提交以包含在作品中的任何贡献,应作为上述双重许可,不附加任何额外条款或条件。

依赖项

~250–700KB
~16K SLoC