MCPcopy Index your code
hub / github.com/dalance/sv-parser

github.com/dalance/sv-parser @v0.13.5

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.13.5 ↗ · + Follow
2,785 symbols 6,576 edges 184 files 4 documented · 0%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

sv-parser

SystemVerilog parser library fully compliant with IEEE 1800-2017.

Actions Status Crates.io Docs.rs

Tools using sv-parser

  • morty: A SystemVerilog source file pickler
  • svinst: Determines the modules declared and instantiated in a SystemVerilog file
  • svlint: SystemVerilog linter
  • svls: SystemVerilog language server

Usage

[dependencies]
sv-parser = "0.13.5"

sv-parser provides parse_sv function which returns SyntaxTree. SyntaxTree shows Concrete Syntax Tree. It has the preprocessed string and the parsed tree.

RefNode shows a reference to any node of SyntaxTree. You can get RefNode through an iterator of SyntaxTree. Variant names of RefNode follows "Annex A Formal syntax" of IEEE 1800-2017.

Locate shows a position of token. All leaf node of SyntaxTree is Locate. You can get string from Locate by get_str.

Example

The following example parses a SystemVerilog source file and shows module names.

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,
    }
}

License

Licensed under either of

  • Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
  • MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Extension points exported contracts — how you extend this code

Node (Interface)
(no doc) [1 implementers]
sv-parser-syntaxtree/src/lib.rs

Core symbols most depended-on inside this repo

new
called by 1221
sv-parser-parser/src/source_text/class_items.rs
symbol
called by 878
sv-parser-parser/src/utils.rs
keyword
called by 658
sv-parser-parser/src/utils.rs
paren
called by 197
sv-parser-parser/src/utils.rs
list
called by 115
sv-parser-parser/src/utils.rs
push
called by 78
sv-parser-pp/src/preprocess.rs
triple
called by 66
sv-parser-parser/src/utils.rs
identifier
called by 59
sv-parser-parser/src/general/identifiers.rs

Shape

Function 1,489
Class 956
Enum 310
Method 29
Interface 1

Languages

Rust100%

Modules by API surface

sv-parser-parser/src/general/identifiers.rs102 symbols
sv-parser-syntaxtree/src/general/identifiers.rs99 symbols
sv-parser-parser/src/declarations/assertion_declarations.rs90 symbols
sv-parser-syntaxtree/src/declarations/assertion_declarations.rs85 symbols
sv-parser-pp/src/preprocess.rs73 symbols
sv-parser-parser/src/tests.rs69 symbols
sv-parser-parser/src/declarations/covergroup_declarations.rs68 symbols
sv-parser-syntaxtree/src/declarations/covergroup_declarations.rs67 symbols
sv-parser-parser/src/general/compiler_directives.rs60 symbols
sv-parser-syntaxtree/src/general/compiler_directives.rs48 symbols
sv-parser-syntaxtree/src/source_text/system_verilog_source_text.rs43 symbols
sv-parser-parser/src/source_text/system_verilog_source_text.rs43 symbols

For agents

$ claude mcp add sv-parser \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact