• Stars
    star
    403
  • Rank 107,140 (Top 3 %)
  • Language
    Rust
  • License
    Other
  • Created over 5 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

SystemVerilog parser library fully compliant with IEEE 1800-2017

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.1"

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

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.

More Repositories

1

procs

A modern replacement for ps written in Rust
Rust
5,092
star
2

amber

A code search / replace tool
Rust
832
star
3

svls

SystemVerilog language server
Rust
461
star
4

svlint

SystemVerilog linter
Rust
309
star
5

veryl

Veryl: A Modern Hardware Description Language
Rust
226
star
6

ptags

A parallel universal-ctags wrapper for git repository
Rust
123
star
7

termbg

A Rust library for terminal background color detection
Rust
86
star
8

D2dControl

WPF Control for Direct2D with SharpDX
C#
82
star
9

pipecolor

A terminal filter to colorize output
Rust
63
star
10

nom-tracable

Extension of nom to trace parser
Rust
48
star
11

prosafe_exporter

Prometheus exporter for NETGEAR switches supported by ProSAFE Plus utility
Rust
44
star
12

gip

a command-line tool to get global IP address
Rust
35
star
13

softether_exporter

Prometheus Exporter of SoftEther VPN Server
Rust
33
star
14

cargo-trend

Cargo subcommand to generate trend graph of dependent crates
Rust
31
star
15

flexlint

A flexible linter with rules defined by regular expression
Rust
28
star
16

structopt-toml

An default value loader from TOML for structopt
Rust
28
star
17

nom-packrat

Extension of nom to apply "Packrat Parsing"
Rust
21
star
18

svls-vscode

SystemVerilog language server client for Visual Studio Code
TypeScript
20
star
19

fts-rs

A Rust library for high performance directory walking using libc fts.
Rust
18
star
20

nom-recursive

Extension of nom to handle left recursion
Rust
16
star
21

mdbook-transcheck

Checker for translated mdbook
Rust
16
star
22

git-skel

A git subcommand to apply skeleton repository continuously
Rust
12
star
23

sdcx

Rust
12
star
24

sdc-parser

Rust
11
star
25

nom-greedyerror

Custom error type of nom to improve accuracy of error position
Rust
10
star
26

softfloat-wrapper

a safe wrapper of Berkeley SoftFloat based on softfloat-sys
Rust
10
star
27

svlint-action

SystemVerilog
7
star
28

bitpattern

bitwise pattern matching and extracting
Rust
6
star
29

vseq.vim

A Vim plugin for generating sequential number vertically
Vim Script
5
star
30

proc-reader

A std::io::Read implementation for stdout/stderr of other process
Rust
5
star
31

sample-rs

Rust
4
star
32

NeovimClient

Neovim msgpack-rpc Client by C#
C#
4
star
33

svlint-plugin-sample

A sample project of svlint plugin
Rust
4
star
34

rust-plugin-sample

Rust
4
star
35

softfloat_bench

Rust softfloat libarary benchmark
Rust
4
star
36

sv4state

Rust
3
star
37

rust-dpi-sample

SystemVerilog
3
star
38

rustified

Rust
2
star
39

veryl.vim

Vim Script
2
star
40

cc-version

gcc version detection for build.rs
Rust
2
star
41

custom_attribute_sample

Rust
2
star
42

dotfiles

Vim Script
2
star
43

bsd-kvm

Rust
1
star
44

rust-i586

rust-lang build environment for i586
Shell
1
star