1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// SPDX-License-Identifier: Apache-2.0
use crate::ParserConfig;
use std::collections::HashMap;
use std::error::Error;
use std::ops::Index;
use std::path::Path;
use std::str::FromStr;
pub fn extract_packages_from_verilog_file(
verilog: &Path,
ignore_unknown_modules: bool,
) -> Result<HashMap<String, Package>, Box<dyn Error>> {
extract_packages_from_verilog_files(&[verilog], ignore_unknown_modules)
}
pub fn extract_packages_from_verilog_files(
verilog: &[&Path],
ignore_unknown_modules: bool,
) -> Result<HashMap<String, Package>, Box<dyn Error>> {
let cfg = ParserConfig {
sources: &verilog
.iter()
.map(|path| path.to_str().unwrap())
.collect::<Vec<_>>(),
ignore_unknown_modules,
..Default::default()
};
extract_packages_with_config(&cfg)
}
pub fn extract_packages_from_verilog(
verilog: impl AsRef<str>,
ignore_unknown_modules: bool,
) -> Result<HashMap<String, Package>, Box<dyn Error>> {
let verilog = slang_rs::str2tmpfile(verilog.as_ref()).unwrap();
let cfg = ParserConfig {
sources: &[verilog.path().to_str().unwrap()],
ignore_unknown_modules,
..Default::default()
};
extract_packages_with_config(&cfg)
}
pub fn extract_packages_with_config(
cfg: &ParserConfig,
) -> Result<HashMap<String, Package>, Box<dyn Error>> {
let pkgs = slang_rs::extract_packages(&cfg.to_slang_config())?;
Ok(pkgs
.into_iter()
.map(|(name, pkg)| {
(
name.clone(),
Package {
name: name.clone(),
parameters: pkg
.parameters
.into_iter()
.map(|(name, param)| {
(
name.clone(),
Parameter {
name: name.clone(),
value: param.value,
},
)
})
.collect(),
},
)
})
.collect())
}
#[derive(Debug, PartialEq)]
pub struct Parameter {
pub name: String,
pub value: String,
}
impl Parameter {
/// Parse the parameter's `value` into any type that implements [`FromStr`].
///
/// # Type Parameters
///
/// * **`T`** – The numeric type you want (`i64`, `u128`,
/// [`num_bigint::BigInt`], [`num_bigint::BigUint`], `f64`, ...). `T` only
/// needs to satisfy `T: FromStr`.
///
/// # Errors
///
/// Returns `Err(T::Err)` if `value` is *not* a valid textual
/// representation for `T`.
///
/// # Examples
///
/// ```rust
/// use num_bigint::{BigInt, BigUint};
/// use std::convert::TryFrom;
///
/// # use topstitch::Parameter;
/// let p = Parameter { name: "answer".into(), value: "42".into() };
///
/// // Primitive integer (type inferred)
/// let n: i32 = p.parse().unwrap();
///
/// // Explicit turbofish when inference can’t decide
/// let n128 = p.parse::<u128>().unwrap();
///
/// // Big integers
/// let big: BigInt = p.parse().unwrap();
/// let ubig: BigUint = p.parse().unwrap();
/// ```
pub fn parse<T>(&self) -> Result<T, T::Err>
where
T: FromStr,
{
self.value.parse()
}
pub fn calc_type_width(&self) -> Result<usize, Box<dyn Error>> {
let value = self.parse::<String>()?;
match slang_rs::parse_type_definition(&value)?.width() {
Ok(width) => Ok(width),
Err(e) => Err(e.into()),
}
}
}
#[derive(Debug, PartialEq)]
pub struct Package {
pub name: String,
pub parameters: HashMap<String, Parameter>,
}
impl Index<&str> for Package {
type Output = Parameter;
fn index(&self, key: &str) -> &Self::Output {
&self.parameters[key]
}
}
impl Package {
/// Get a parameter from the package, returning `None` if it doesn't exist.
pub fn get(&self, key: &str) -> Option<&Parameter> {
self.parameters.get(key)
}
}