fixed most warnings

This commit is contained in:
Yehowshua Immanuel 2022-08-02 19:31:35 -04:00
parent e931d9a825
commit 21f0682500
10 changed files with 116 additions and 154 deletions

View file

@ -1,5 +1,5 @@
[package] [package]
name = "VCDViewer" name = "fastwave"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"

View file

@ -2,7 +2,6 @@ use std::{fs::File};
use clap::Parser; use clap::Parser;
pub mod test; pub mod test;
use test::*;
pub mod vcd; pub mod vcd;
use vcd::parse_vcd; use vcd::parse_vcd;

View file

@ -1,6 +1,6 @@
// TODO: we should eventually be able to only test on just // TODO: we should eventually be able to only test on just
// the files const // the files const
pub const files : [&str; 30] = [ pub const FILES : [&str; 30] = [
"./test-vcd-files/aldec/SPI_Write.vcd", "./test-vcd-files/aldec/SPI_Write.vcd",
"./test-vcd-files/ghdl/alu.vcd", "./test-vcd-files/ghdl/alu.vcd",
"./test-vcd-files/ghdl/idea.vcd", "./test-vcd-files/ghdl/idea.vcd",
@ -34,7 +34,7 @@ pub const files : [&str; 30] = [
"./test-vcd-files/xilinx_isim/test2x2_regex22_string1.vcd" "./test-vcd-files/xilinx_isim/test2x2_regex22_string1.vcd"
]; ];
pub const good_date_files : [&str; 24] = [ pub const GOOD_DATE_FILES : [&str; 24] = [
"./test-vcd-files/aldec/SPI_Write.vcd", "./test-vcd-files/aldec/SPI_Write.vcd",
"./test-vcd-files/ghdl/alu.vcd", "./test-vcd-files/ghdl/alu.vcd",
"./test-vcd-files/ghdl/idea.vcd", "./test-vcd-files/ghdl/idea.vcd",
@ -61,7 +61,7 @@ pub const good_date_files : [&str; 24] = [
"./test-vcd-files/xilinx_isim/test2x2_regex22_string1.vcd" "./test-vcd-files/xilinx_isim/test2x2_regex22_string1.vcd"
]; ];
pub const bad_date_files : [&str; 6] = [ pub const BAD_DATE_FILES : [&str; 6] = [
"./test-vcd-files/ncsim/ffdiv_32bit_tb.vcd", "./test-vcd-files/ncsim/ffdiv_32bit_tb.vcd",
"./test-vcd-files/quartus/mipsHardware.vcd", "./test-vcd-files/quartus/mipsHardware.vcd",
"./test-vcd-files/quartus/wave_registradores.vcd", "./test-vcd-files/quartus/wave_registradores.vcd",

View file

@ -19,8 +19,6 @@ use scopes::*;
mod events; mod events;
use events::*; use events::*;
use function_name::named;
pub fn parse_vcd(file : File) -> Result<VCD, String> { pub fn parse_vcd(file : File) -> Result<VCD, String> {
let mut word_gen = WordReader::new(file); let mut word_gen = WordReader::new(file);
@ -30,7 +28,7 @@ pub fn parse_vcd(file : File) -> Result<VCD, String> {
// respective signal indexes // respective signal indexes
let mut signal_map = std::collections::HashMap::new(); let mut signal_map = std::collections::HashMap::new();
// after we parse metadata, we form VCD object // after we parse metadata, we form the VCD object
let mut vcd = VCD{ let mut vcd = VCD{
metadata : header, metadata : header,
timeline : vec![], timeline : vec![],
@ -40,7 +38,7 @@ pub fn parse_vcd(file : File) -> Result<VCD, String> {
scope_roots : vec![], scope_roots : vec![],
}; };
parse_scopes(&mut word_gen, None, &mut vcd, &mut signal_map)?; parse_scopes(&mut word_gen, &mut vcd, &mut signal_map)?;
parse_events(&mut word_gen, &mut vcd, &mut signal_map)?; parse_events(&mut word_gen, &mut vcd, &mut signal_map)?;
Ok(vcd) Ok(vcd)
@ -56,7 +54,7 @@ mod tests {
// TODO: eventually, once all dates pass, merge the following // TODO: eventually, once all dates pass, merge the following
// two loops // two loops
// testing dates // testing dates
for file in test::good_date_files { for file in test::GOOD_DATE_FILES {
let metadata = parse_metadata( let metadata = parse_metadata(
&mut WordReader::new( &mut WordReader::new(
File::open(file) File::open(file)
@ -67,7 +65,7 @@ mod tests {
assert!(metadata.unwrap().date.is_some()); assert!(metadata.unwrap().date.is_some());
} }
for file in test::files { for file in test::FILES {
let metadata = parse_metadata( let metadata = parse_metadata(
&mut WordReader::new( &mut WordReader::new(
File::open(file) File::open(file)
@ -76,7 +74,7 @@ mod tests {
); );
assert!(metadata.is_ok()); assert!(metadata.is_ok());
let (scalar, timescale) = metadata.unwrap().timescale; let (scalar, _timescale) = metadata.unwrap().timescale;
assert!(scalar.is_some()); assert!(scalar.is_some());
} }
@ -85,7 +83,7 @@ mod tests {
#[test] #[test]
fn scopes() { fn scopes() {
// see if we can parse all signal trees successfully // see if we can parse all signal trees successfully
for file_name in test::files { for file_name in test::FILES {
let file = File::open(file_name).unwrap(); let file = File::open(file_name).unwrap();
let vcd = parse_vcd(file); let vcd = parse_vcd(file);

View file

@ -14,7 +14,7 @@ pub(super) fn take_until<'a>(word : &'a str, pattern : u8) -> ParseResult<'a> {
let mut new_start = 0; let mut new_start = 0;
for chr in word.as_bytes() { for chr in word.as_bytes() {
if (*chr == pattern) { if *chr == pattern {
break break
} }
else { else {
@ -30,25 +30,11 @@ pub(super) fn take_until<'a>(word : &'a str, pattern : u8) -> ParseResult<'a> {
} }
// TODO: if I end up using simulator specific date parsers, ``take_until`` may
// suffice rendering this function obselete, at which point I should delete it.
pub(super) fn truncate_last_chr_when<'a>(word : &'a str, cond : fn(u8) -> bool) -> &'a str {
let last_chr = word.as_bytes().last().unwrap();
let mut new_end_index = word.len();
if cond(*last_chr) {
new_end_index -= 1;
}
return &word[0..new_end_index]
}
pub(super) fn take_while<'a>(word : &'a str, cond : fn(u8) -> bool) -> ParseResult<'a> { pub(super) fn take_while<'a>(word : &'a str, cond : fn(u8) -> bool) -> ParseResult<'a> {
let mut new_start = 0; let mut new_start = 0;
for chr in word.as_bytes() { for chr in word.as_bytes() {
if (cond(*chr)) { if cond(*chr) {
new_start += 1; new_start += 1;
} }
else { else {

View file

@ -1,11 +1,9 @@
//! part of the vcd parser that handles parsing the signal tree and //! part of the vcd parser that handles parsing the signal tree and
//! building the resulting signal tree //! building the resulting signal tree
use function_name::named;
use super::*; use super::*;
#[derive(Debug)] #[derive(Debug)]
pub(super) enum BinaryParserErrTypes {x_value, z_value, u_value, other_value(char), too_long} pub(super) enum BinaryParserErrTypes {XValue, ZValue, UValue, OtherValue(char), TooLong}
// We build a quick and not so dirty bit string parser. // We build a quick and not so dirty bit string parser.
fn base2_str_to_byte(word : &[u8]) -> Result<u8, BinaryParserErrTypes> { fn base2_str_to_byte(word : &[u8]) -> Result<u8, BinaryParserErrTypes> {
@ -14,10 +12,7 @@ fn base2_str_to_byte(word : &[u8]) -> Result<u8, BinaryParserErrTypes> {
// shouldn't have more than 8 chars in str // shouldn't have more than 8 chars in str
let len = word.len(); let len = word.len();
if len > 8 { if len > 8 {
let (f, l )= (file!(), line!()); return Err(BinaryParserErrTypes::TooLong)
let err = format!(
"Error near {f}:{l}. Base2 string has length {len} > 8.");
return Err(BinaryParserErrTypes::too_long)
} }
let bit_lut = [ let bit_lut = [
@ -35,10 +30,10 @@ fn base2_str_to_byte(word : &[u8]) -> Result<u8, BinaryParserErrTypes> {
match chr { match chr {
b'1' => {val = bit_lut[idx] | val} b'1' => {val = bit_lut[idx] | val}
b'0' => {} b'0' => {}
b'x' | b'X' => {return Err(BinaryParserErrTypes::x_value)} b'x' | b'X' => {return Err(BinaryParserErrTypes::XValue)}
b'z' | b'Z' => {return Err(BinaryParserErrTypes::z_value)} b'z' | b'Z' => {return Err(BinaryParserErrTypes::ZValue)}
b'u' | b'U' => {return Err(BinaryParserErrTypes::u_value)} b'u' | b'U' => {return Err(BinaryParserErrTypes::UValue)}
_ => {return Err(BinaryParserErrTypes::other_value(*chr as char))} _ => {return Err(BinaryParserErrTypes::OtherValue(*chr as char))}
} }
} }
@ -48,7 +43,7 @@ fn base2_str_to_byte(word : &[u8]) -> Result<u8, BinaryParserErrTypes> {
fn binary_str_to_vec_u8(binary_str : &str) -> Result<Vec<u8>, BinaryParserErrTypes> { fn binary_str_to_vec_u8(binary_str : &str) -> Result<Vec<u8>, BinaryParserErrTypes> {
let mut vec_u8 : Vec<u8> = Vec::new(); let mut vec_u8 : Vec<u8> = Vec::new();
let mut binary_str_as_bytes = binary_str.as_bytes(); let binary_str_as_bytes = binary_str.as_bytes();
let mut tail_idx = binary_str_as_bytes.len(); let mut tail_idx = binary_str_as_bytes.len();
// clamp head if provided binary str is less than 8 long // clamp head if provided binary str is less than 8 long
@ -57,7 +52,7 @@ fn binary_str_to_vec_u8(binary_str : &str) -> Result<Vec<u8>, BinaryParserErrTyp
{binary_str_as_bytes.len() - 8} {binary_str_as_bytes.len() - 8}
else else
{0}; {0};
while {tail_idx > 0} { while tail_idx > 0 {
let curr_b_val = &binary_str_as_bytes[head_idx..tail_idx]; let curr_b_val = &binary_str_as_bytes[head_idx..tail_idx];
let val_u8 = base2_str_to_byte(curr_b_val)?; let val_u8 = base2_str_to_byte(curr_b_val)?;
vec_u8.push(val_u8); vec_u8.push(val_u8);
@ -81,11 +76,10 @@ fn binary_str_to_vec_u8(binary_str : &str) -> Result<Vec<u8>, BinaryParserErrTyp
Ok(vec_u8) Ok(vec_u8)
} }
#[named]
pub(super) fn parse_events<'a>( pub(super) fn parse_events<'a>(
word_reader : &mut WordReader, word_reader : &mut WordReader,
vcd : &'a mut VCD, vcd : &'a mut VCD,
signal_map : &mut HashMap<String, Signal_Idx> signal_map : &mut HashMap<String, SignalIdx>
) -> Result<(), String> { ) -> Result<(), String> {
loop { loop {
@ -110,7 +104,7 @@ pub(super) fn parse_events<'a>(
// configurable. // configurable.
let (f, l )= (file!(), line!()); let (f, l )= (file!(), line!());
let start_idx = u32::try_from(vcd.timeline.len()).map_err( let start_idx = u32::try_from(vcd.timeline.len()).map_err(
|e| format!("Error near {f}:{l}. Failed to convert from usize to u32."))?; |_| format!("Error near {f}:{l}. Failed to convert from usize to u32."))?;
vcd.timeline_markers.push(StartIdx(start_idx)); vcd.timeline_markers.push(StartIdx(start_idx));
vcd.timeline.append(&mut value); vcd.timeline.append(&mut value);
} }
@ -119,7 +113,6 @@ pub(super) fn parse_events<'a>(
"b" => { "b" => {
let binary_value = &word[1..]; let binary_value = &word[1..];
let observed_num_bits = binary_value.len(); let observed_num_bits = binary_value.len();
let (f, l )= (file!(), line!());
let mut value_u8 : Vec<u8> = Vec::new(); let mut value_u8 : Vec<u8> = Vec::new();
let mut value_string = String::new(); let mut value_string = String::new();
@ -131,9 +124,9 @@ pub(super) fn parse_events<'a>(
// Or else, we we propagate up other errors. // Or else, we we propagate up other errors.
match binary_str_to_vec_u8(binary_value) { match binary_str_to_vec_u8(binary_value) {
Ok(result) => {value_u8 = result;} Ok(result) => {value_u8 = result;}
Err(BinaryParserErrTypes::x_value | Err(BinaryParserErrTypes::XValue |
BinaryParserErrTypes::z_value | BinaryParserErrTypes::ZValue |
BinaryParserErrTypes::u_value BinaryParserErrTypes::UValue
) => ) =>
{ {
store_as_string = true; store_as_string = true;
@ -151,7 +144,7 @@ pub(super) fn parse_events<'a>(
// lookup signal idx // lookup signal idx
let (f, l )= (file!(), line!()); let (f, l )= (file!(), line!());
let Signal_Idx(ref signal_idx) = signal_map.get(word).ok_or( let SignalIdx(ref signal_idx) = signal_map.get(word).ok_or(
format!("Error near {f}:{l}. Failed to lookup signal {word} at {cursor:?}"))?; format!("Error near {f}:{l}. Failed to lookup signal {word} at {cursor:?}"))?;
// account for fact that signal idx could be an alias, so there // account for fact that signal idx could be an alias, so there
@ -161,8 +154,8 @@ pub(super) fn parse_events<'a>(
let signal = vcd.all_signals.get(*signal_idx).unwrap(); let signal = vcd.all_signals.get(*signal_idx).unwrap();
match signal { match signal {
Signal::Data {..} => {*signal_idx} Signal::Data {..} => {*signal_idx}
Signal::Alias {name, signal_alias} => { Signal::Alias {signal_alias, ..} => {
let Signal_Idx(ref signal_idx) = signal_alias; let SignalIdx(ref signal_idx) = signal_alias;
signal_idx.clone() signal_idx.clone()
} }
@ -174,7 +167,7 @@ pub(super) fn parse_events<'a>(
let signal = vcd.all_signals.get_mut(signal_idx).unwrap(); let signal = vcd.all_signals.get_mut(signal_idx).unwrap();
match signal { match signal {
Signal::Data {name, sig_type, ref mut signal_error, num_bits, Signal::Data {name, sig_type, ref mut signal_error, num_bits,
self_idx, u8_timeline, u8_timeline_markers, string_timeline, u8_timeline, u8_timeline_markers, string_timeline,
string_timeline_markers, ..} => { string_timeline_markers, ..} => {
if signal_error.is_some() {continue;} if signal_error.is_some() {continue;}
@ -210,7 +203,7 @@ pub(super) fn parse_events<'a>(
let (f, l )= (file!(), line!()); let (f, l )= (file!(), line!());
let timeline_idx = u32::try_from(vcd.timeline.len()).map_err( let timeline_idx = u32::try_from(vcd.timeline.len()).map_err(
|e| format!("Error near {f}:{l}. Failed to convert from usize to u32."))?; |_| format!("Error near {f}:{l}. Failed to convert from usize to u32."))?;
let timeline_idx = TimelineIdx(timeline_idx); let timeline_idx = TimelineIdx(timeline_idx);
if store_as_string { if store_as_string {
@ -264,7 +257,7 @@ pub(super) fn parse_events<'a>(
// lookup signal idx // lookup signal idx
let hash = &word[1..]; let hash = &word[1..];
let (f, l )= (file!(), line!()); let (f, l )= (file!(), line!());
let Signal_Idx(ref signal_idx) = signal_map.get(hash).ok_or( let SignalIdx(ref signal_idx) = signal_map.get(hash).ok_or(
format!("Error near {f}:{l}. Failed to lookup signal {hash} at {cursor:?}"))?; format!("Error near {f}:{l}. Failed to lookup signal {hash} at {cursor:?}"))?;
// account for fact that signal idx could be an alias, so there // account for fact that signal idx could be an alias, so there
@ -274,8 +267,8 @@ pub(super) fn parse_events<'a>(
let signal = vcd.all_signals.get(*signal_idx).unwrap(); let signal = vcd.all_signals.get(*signal_idx).unwrap();
match signal { match signal {
Signal::Data {..} => {*signal_idx} Signal::Data {..} => {*signal_idx}
Signal::Alias {name, signal_alias} => { Signal::Alias {signal_alias, ..} => {
let Signal_Idx(ref signal_idx) = signal_alias; let SignalIdx(ref signal_idx) = signal_alias;
signal_idx.clone() signal_idx.clone()
} }
@ -287,7 +280,7 @@ pub(super) fn parse_events<'a>(
let signal = vcd.all_signals.get_mut(signal_idx).unwrap(); let signal = vcd.all_signals.get_mut(signal_idx).unwrap();
match signal { match signal {
Signal::Data {name, sig_type, ref mut signal_error, num_bits, Signal::Data {name, sig_type, ref mut signal_error, num_bits,
self_idx, u8_timeline, u8_timeline_markers, ..} => { u8_timeline, u8_timeline_markers, ..} => {
// if this is a bad signal, go ahead and skip it // if this is a bad signal, go ahead and skip it
if signal_error.is_some() {continue;} if signal_error.is_some() {continue;}
@ -321,7 +314,7 @@ pub(super) fn parse_events<'a>(
let (f, l )= (file!(), line!()); let (f, l )= (file!(), line!());
let timeline_idx = u32::try_from(vcd.timeline.len()).map_err( let timeline_idx = u32::try_from(vcd.timeline.len()).map_err(
|e| format!("Error near {f}:{l}. Failed to convert from usize to u32."))?; |_| format!("Error near {f}:{l}. Failed to convert from usize to u32."))?;
let timeline_idx = TimelineIdx(timeline_idx); let timeline_idx = TimelineIdx(timeline_idx);
u8_timeline_markers.push(timeline_idx); u8_timeline_markers.push(timeline_idx);
@ -342,7 +335,7 @@ pub(super) fn parse_events<'a>(
// lokup signal idx // lokup signal idx
let hash = &word[1..]; let hash = &word[1..];
let (f, l )= (file!(), line!()); let (f, l )= (file!(), line!());
let Signal_Idx(ref signal_idx) = signal_map.get(hash).ok_or( let SignalIdx(ref signal_idx) = signal_map.get(hash).ok_or(
format!("Error near {f}:{l}. Failed to lookup signal {hash} at {cursor:?}"))?; format!("Error near {f}:{l}. Failed to lookup signal {hash} at {cursor:?}"))?;
// account for fact that signal idx could be an alias, so there // account for fact that signal idx could be an alias, so there
@ -352,8 +345,8 @@ pub(super) fn parse_events<'a>(
let signal = vcd.all_signals.get(*signal_idx).unwrap(); let signal = vcd.all_signals.get(*signal_idx).unwrap();
match signal { match signal {
Signal::Data {..} => {*signal_idx} Signal::Data {..} => {*signal_idx}
Signal::Alias {name, signal_alias} => { Signal::Alias {signal_alias, ..} => {
let Signal_Idx(ref signal_idx) = signal_alias; let SignalIdx(ref signal_idx) = signal_alias;
signal_idx.clone() signal_idx.clone()
} }
@ -365,7 +358,7 @@ pub(super) fn parse_events<'a>(
let signal = vcd.all_signals.get_mut(signal_idx).unwrap(); let signal = vcd.all_signals.get_mut(signal_idx).unwrap();
match signal { match signal {
Signal::Data {name, sig_type, ref mut signal_error, num_bits, Signal::Data {name, sig_type, ref mut signal_error, num_bits,
self_idx, u8_timeline, u8_timeline_markers, scope_parent, ..} => { u8_timeline, u8_timeline_markers, ..} => {
// if this is a bad signal, go ahead and skip it // if this is a bad signal, go ahead and skip it
if signal_error.is_some() {continue;} if signal_error.is_some() {continue;}
@ -399,7 +392,7 @@ pub(super) fn parse_events<'a>(
let (f, l )= (file!(), line!()); let (f, l )= (file!(), line!());
let timeline_idx = u32::try_from(vcd.timeline.len()).map_err( let timeline_idx = u32::try_from(vcd.timeline.len()).map_err(
|e| format!("Error near {f}:{l}. Failed to convert from usize to u32."))?; |_| format!("Error near {f}:{l}. Failed to convert from usize to u32."))?;
let timeline_idx = TimelineIdx(timeline_idx); let timeline_idx = TimelineIdx(timeline_idx);
u8_timeline_markers.push(timeline_idx); u8_timeline_markers.push(timeline_idx);
@ -421,7 +414,7 @@ pub(super) fn parse_events<'a>(
// lokup signal idx // lokup signal idx
let hash = &word[1..]; let hash = &word[1..];
let (f, l )= (file!(), line!()); let (f, l )= (file!(), line!());
let Signal_Idx(ref signal_idx) = signal_map.get(hash).ok_or( let SignalIdx(ref signal_idx) = signal_map.get(hash).ok_or(
format!("Error near {f}:{l}. Failed to lookup signal {hash} at {cursor:?}"))?; format!("Error near {f}:{l}. Failed to lookup signal {hash} at {cursor:?}"))?;
// account for fact that signal idx could be an alias, so there // account for fact that signal idx could be an alias, so there
@ -431,8 +424,8 @@ pub(super) fn parse_events<'a>(
let signal = vcd.all_signals.get(*signal_idx).unwrap(); let signal = vcd.all_signals.get(*signal_idx).unwrap();
match signal { match signal {
Signal::Data {..} => {*signal_idx} Signal::Data {..} => {*signal_idx}
Signal::Alias {name, signal_alias} => { Signal::Alias {signal_alias, ..} => {
let Signal_Idx(ref signal_idx) = signal_alias; let SignalIdx(ref signal_idx) = signal_alias;
signal_idx.clone() signal_idx.clone()
} }
@ -444,8 +437,7 @@ pub(super) fn parse_events<'a>(
let signal = vcd.all_signals.get_mut(signal_idx).unwrap(); let signal = vcd.all_signals.get_mut(signal_idx).unwrap();
match signal { match signal {
Signal::Data {name, sig_type, ref mut signal_error, num_bits, Signal::Data {name, sig_type, ref mut signal_error, num_bits,
self_idx, u8_timeline, u8_timeline_markers, string_timeline, string_timeline, string_timeline_markers, ..} => {
string_timeline_markers, ..} => {
// if this is a bad signal, go ahead and skip it // if this is a bad signal, go ahead and skip it
if signal_error.is_some() {continue;} if signal_error.is_some() {continue;}
@ -479,7 +471,7 @@ pub(super) fn parse_events<'a>(
let (f, l )= (file!(), line!()); let (f, l )= (file!(), line!());
let timeline_idx = u32::try_from(vcd.timeline.len()).map_err( let timeline_idx = u32::try_from(vcd.timeline.len()).map_err(
|e| format!("Error near {f}:{l}. Failed to convert from usize to u32."))?; |_| format!("Error near {f}:{l}. Failed to convert from usize to u32."))?;
let timeline_idx = TimelineIdx(timeline_idx); let timeline_idx = TimelineIdx(timeline_idx);
string_timeline_markers.push(timeline_idx); string_timeline_markers.push(timeline_idx);

View file

@ -1,6 +1,5 @@
use chrono::prelude::*; use chrono::prelude::*;
use itertools::Itertools; use itertools::Itertools;
use std::fs::File;
use function_name::named; use function_name::named;
use super::*; use super::*;
@ -120,7 +119,7 @@ pub(super) fn parse_date(
let year = { let year = {
// check for another word in the file // check for another word in the file
let (word, cursor) = word_and_ctx5; let (word, _) = word_and_ctx5;
word.to_string() word.to_string()
}; };
@ -148,7 +147,7 @@ pub(super) fn parse_version(word_reader : &mut WordReader) -> Result<Version, St
return Err(format!("reached end of file without parser leaving {}", function_name!())) return Err(format!("reached end of file without parser leaving {}", function_name!()))
} }
let (word, cursor) = word.unwrap(); let (word, _) = word.unwrap();
if word == "$end" { if word == "$end" {
// truncate trailing whitespace // truncate trailing whitespace
@ -170,7 +169,7 @@ pub(super) fn parse_timescale(word_reader : &mut WordReader) -> Result<(Option<u
// we might see `scalarunit $end` or `scalar unit $end` // we might see `scalarunit $end` or `scalar unit $end`
// first get timescale // first get timescale
let (word, cursor) = word_reader.next_word().ok_or(&err_msg)?; let (word, _) = word_reader.next_word().ok_or(&err_msg)?;
let word = word.to_string(); let word = word.to_string();
let ParseResult{matched, residual} = take_while(word.as_str(), digit); let ParseResult{matched, residual} = take_while(word.as_str(), digit);
let scalar = matched; let scalar = matched;
@ -180,14 +179,14 @@ pub(super) fn parse_timescale(word_reader : &mut WordReader) -> Result<(Option<u
let timescale = { let timescale = {
if residual == "" { if residual == "" {
let (word, cursor) = word_reader.next_word().ok_or(&err_msg)?; let (word, _) = word_reader.next_word().ok_or(&err_msg)?;
let unit = match word { let unit = match word {
"fs" => {Ok(Timescale::fs)} "fs" => {Ok(Timescale::Fs)}
"ps" => {Ok(Timescale::ps)} "ps" => {Ok(Timescale::Ps)}
"ns" => {Ok(Timescale::ns)} "ns" => {Ok(Timescale::Ns)}
"us" => {Ok(Timescale::us)} "us" => {Ok(Timescale::Us)}
"ms" => {Ok(Timescale::ms)} "ms" => {Ok(Timescale::Ms)}
"s" => {Ok(Timescale::s)} "s" => {Ok(Timescale::S)}
_ => {Err(err_msg.to_string())} _ => {Err(err_msg.to_string())}
}.unwrap(); }.unwrap();
@ -195,11 +194,11 @@ pub(super) fn parse_timescale(word_reader : &mut WordReader) -> Result<(Option<u
} }
else { else {
let unit = match residual { let unit = match residual {
"ps" => {Ok(Timescale::ps)} "ps" => {Ok(Timescale::Ps)}
"ns" => {Ok(Timescale::ns)} "ns" => {Ok(Timescale::Ns)}
"us" => {Ok(Timescale::us)} "us" => {Ok(Timescale::Us)}
"ms" => {Ok(Timescale::ms)} "ms" => {Ok(Timescale::Ms)}
"s" => {Ok(Timescale::s)} "s" => {Ok(Timescale::S)}
_ => {Err(err_msg.to_string())} _ => {Err(err_msg.to_string())}
}.unwrap(); }.unwrap();
@ -208,12 +207,11 @@ pub(super) fn parse_timescale(word_reader : &mut WordReader) -> Result<(Option<u
}; };
// then check for the `$end` keyword // then check for the `$end` keyword
let (end, cursor) = word_reader.next_word().ok_or(&err_msg)?; let (end, _) = word_reader.next_word().ok_or(&err_msg)?;
tag(end, "$end").assert_match()?; tag(end, "$end").assert_match()?;
return Ok(timescale); return Ok(timescale);
Err("".to_string())
} }
#[named] #[named]
@ -223,12 +221,12 @@ pub(super) fn parse_metadata(word_reader : &mut WordReader) -> Result<Metadata,
let mut metadata = Metadata { let mut metadata = Metadata {
date : None, date : None,
version : None, version : None,
timescale : (None, Timescale::unit) timescale : (None, Timescale::Unit)
}; };
loop { loop {
// check for another word in the file // check for another word in the file
let (word, cursor) = word_reader.next_word().ok_or(&err_msg)?; let (word, _) = word_reader.next_word().ok_or(&err_msg)?;
let ParseResult{matched, residual} = tag(word, "$"); let ParseResult{matched, residual} = tag(word, "$");
match matched { match matched {
@ -254,7 +252,7 @@ pub(super) fn parse_metadata(word_reader : &mut WordReader) -> Result<Metadata,
let mut found_end = false; let mut found_end = false;
let mut lookahead_5_words : Vec<(String, Cursor)> = Vec::new(); let mut lookahead_5_words : Vec<(String, Cursor)> = Vec::new();
for word in 0..5 { for _ in 0..5 {
let (word, cursor) = word_reader.next_word().expect(err_msg.as_str()); let (word, cursor) = word_reader.next_word().expect(err_msg.as_str());
let word = word.to_string(); let word = word.to_string();
match word.as_str() { match word.as_str() {

View file

@ -7,9 +7,9 @@ use super::*;
#[named] #[named]
pub(super) fn parse_var<'a>( pub(super) fn parse_var<'a>(
word_reader : &mut WordReader, word_reader : &mut WordReader,
parent_scope_idx : Scope_Idx, parent_scope_idx : ScopeIdx,
vcd : &'a mut VCD, vcd : &'a mut VCD,
signal_map : &mut HashMap<String, Signal_Idx> signal_map : &mut HashMap<String, SignalIdx>
) -> Result<(), String> { ) -> Result<(), String> {
let err = format!("reached end of file without parser leaving {}", function_name!()); let err = format!("reached end of file without parser leaving {}", function_name!());
let (word, cursor) = word_reader.next_word().ok_or(&err)?; let (word, cursor) = word_reader.next_word().ok_or(&err)?;
@ -18,14 +18,14 @@ pub(super) fn parse_var<'a>(
// $var parameter 3 a IDLE $end // $var parameter 3 a IDLE $end
// ^^^^^^^^^ - var_type // ^^^^^^^^^ - var_type
let var_type = match word { let var_type = match word {
"integer" => {Ok(Sig_Type::Integer)} "integer" => {Ok(SigType::Integer)}
"parameter" => {Ok(Sig_Type::Parameter)} "parameter" => {Ok(SigType::Parameter)}
"real" => {Ok(Sig_Type::Real)} "real" => {Ok(SigType::Real)}
"reg" => {Ok(Sig_Type::Reg)} "reg" => {Ok(SigType::Reg)}
"string" => {Ok(Sig_Type::Str)} "string" => {Ok(SigType::Str)}
"wire" => {Ok(Sig_Type::Wire)} "wire" => {Ok(SigType::Wire)}
"tri1" => {Ok(Sig_Type::Tri1)} "tri1" => {Ok(SigType::Tri1)}
"time" => {Ok(Sig_Type::Time)} "time" => {Ok(SigType::Time)}
_ => { _ => {
let err = format!("found keyword `{word}` but expected one of {expected_types:?} on {cursor:?}"); let err = format!("found keyword `{word}` but expected one of {expected_types:?} on {cursor:?}");
Err(err) Err(err)
@ -38,10 +38,10 @@ pub(super) fn parse_var<'a>(
// $var parameter 3 a IDLE $end // $var parameter 3 a IDLE $end
// ^ - no_bits // ^ - no_bits
let no_bits = match var_type { let no_bits = match var_type {
Sig_Type::Integer | Sig_Type::Parameter | SigType::Integer | SigType::Parameter |
Sig_Type::Real | Sig_Type::Reg | SigType::Real | SigType::Reg |
Sig_Type::Wire | Sig_Type::Tri1 | SigType::Wire | SigType::Tri1 |
Sig_Type::Time => { SigType::Time => {
let no_bits = word.parse::<usize>().expect(parse_err.as_str()); let no_bits = word.parse::<usize>().expect(parse_err.as_str());
Some(no_bits) Some(no_bits)
} }
@ -51,7 +51,7 @@ pub(super) fn parse_var<'a>(
// $var parameter 3 a IDLE $end // $var parameter 3 a IDLE $end
// ^ - signal_alias // ^ - signal_alias
let (word, cursor) = word_reader.next_word().ok_or(&err)?; let (word, _) = word_reader.next_word().ok_or(&err)?;
let signal_alias = word.to_string(); let signal_alias = word.to_string();
// dbg!(&signal_alias); // dbg!(&signal_alias);
@ -59,7 +59,7 @@ pub(super) fn parse_var<'a>(
// ^^^^ - full_signal_name(can extend until $end) // ^^^^ - full_signal_name(can extend until $end)
let mut full_signal_name = Vec::<String>::new(); let mut full_signal_name = Vec::<String>::new();
loop { loop {
let (word, cursor) = word_reader.next_word().ok_or(&err)?; let (word, _) = word_reader.next_word().ok_or(&err)?;
match word { match word {
"$end" => {break} "$end" => {break}
_ => {full_signal_name.push(word.to_string())} _ => {full_signal_name.push(word.to_string())}
@ -72,14 +72,14 @@ pub(super) fn parse_var<'a>(
// map // map
let (signal, signal_idx) = match signal_map.get(&signal_alias) { let (signal, signal_idx) = match signal_map.get(&signal_alias) {
Some(ref_signal_idx) => { Some(ref_signal_idx) => {
let signal_idx = Signal_Idx(vcd.all_signals.len()); let signal_idx = SignalIdx(vcd.all_signals.len());
let signal = Signal::Alias{ let signal = Signal::Alias{
name: full_signal_name, name: full_signal_name,
signal_alias: *ref_signal_idx}; signal_alias: *ref_signal_idx};
(signal, signal_idx) (signal, signal_idx)
} }
None => { None => {
let signal_idx = Signal_Idx(vcd.all_signals.len()); let signal_idx = SignalIdx(vcd.all_signals.len());
signal_map.insert(signal_alias.to_string(), signal_idx); signal_map.insert(signal_alias.to_string(), signal_idx);
let signal = Signal::Data{ let signal = Signal::Data{
name: full_signal_name, name: full_signal_name,
@ -97,7 +97,7 @@ pub(super) fn parse_var<'a>(
}; };
vcd.all_signals.push(signal); vcd.all_signals.push(signal);
let Scope_Idx(parent_scope_idx_usize) = parent_scope_idx; let ScopeIdx(parent_scope_idx_usize) = parent_scope_idx;
let parent_scope = vcd.all_scopes.get_mut(parent_scope_idx_usize).unwrap(); let parent_scope = vcd.all_scopes.get_mut(parent_scope_idx_usize).unwrap();
parent_scope.child_signals.push(signal_idx); parent_scope.child_signals.push(signal_idx);
@ -109,7 +109,7 @@ pub(super) fn parse_var<'a>(
fn parse_orphaned_vars<'a>( fn parse_orphaned_vars<'a>(
word_reader : &mut WordReader, word_reader : &mut WordReader,
vcd : &'a mut VCD, vcd : &'a mut VCD,
signal_map : &mut HashMap<String, Signal_Idx> signal_map : &mut HashMap<String, SignalIdx>
) -> Result<(), String> { ) -> Result<(), String> {
// create scope for unscoped signals if such a scope does not // create scope for unscoped signals if such a scope does not
// yet exist // yet exist
@ -118,7 +118,7 @@ fn parse_orphaned_vars<'a>(
// set default scope_idx to the count of existing scope as we // set default scope_idx to the count of existing scope as we
// generally set scope.self_idx to the number of existing scopes // generally set scope.self_idx to the number of existing scopes
// when that particular scope was inserted // when that particular scope was inserted
let mut scope_idx = Scope_Idx(vcd.all_scopes.len()); let mut scope_idx = ScopeIdx(vcd.all_scopes.len());
// Override scope_idx if we find a scope named "Orphaned Signals" // Override scope_idx if we find a scope named "Orphaned Signals"
// already exists // already exists
@ -181,9 +181,9 @@ fn parse_orphaned_vars<'a>(
#[named] #[named]
pub(super) fn parse_signal_tree<'a>( pub(super) fn parse_signal_tree<'a>(
word_reader : &mut WordReader, word_reader : &mut WordReader,
parent_scope_idx : Option<Scope_Idx>, parent_scope_idx : Option<ScopeIdx>,
vcd : &'a mut VCD, vcd : &'a mut VCD,
signal_map : &mut HashMap<String, Signal_Idx> signal_map : &mut HashMap<String, SignalIdx>
) -> Result<(), String> { ) -> Result<(), String> {
// $scope module reg_mag_i $end // $scope module reg_mag_i $end
@ -203,13 +203,13 @@ pub(super) fn parse_signal_tree<'a>(
// ^^^^^^^^^ - scope name // ^^^^^^^^^ - scope name
let (scope_name, _) = word_reader.next_word().ok_or(&err)?; let (scope_name, _) = word_reader.next_word().ok_or(&err)?;
let curr_scope_idx = Scope_Idx(vcd.all_scopes.len()); let curr_scope_idx = ScopeIdx(vcd.all_scopes.len());
// register this scope as a child of the current parent scope // register this scope as a child of the current parent scope
// if there is a parent scope, or else we register this scope as // if there is a parent scope, or else we register this scope as
// root scope // root scope
match parent_scope_idx { match parent_scope_idx {
Some(Scope_Idx(parent_scope_idx)) => { Some(ScopeIdx(parent_scope_idx)) => {
let parent_scope = vcd.all_scopes.get_mut(parent_scope_idx).unwrap(); let parent_scope = vcd.all_scopes.get_mut(parent_scope_idx).unwrap();
parent_scope.child_scopes.push(curr_scope_idx); parent_scope.child_scopes.push(curr_scope_idx);
} }
@ -277,14 +277,13 @@ pub(super) fn parse_signal_tree<'a>(
#[named] #[named]
pub(super) fn parse_scopes<'a>( pub(super) fn parse_scopes<'a>(
word_reader : &mut WordReader, word_reader : &mut WordReader,
parent_scope_idx : Option<Scope_Idx>,
vcd : &'a mut VCD, vcd : &'a mut VCD,
signal_map : &mut HashMap<String, Signal_Idx> signal_map : &mut HashMap<String, SignalIdx>
) -> Result<(), String> { ) -> Result<(), String> {
// get the current word // get the current word
let (f, l ) = (file!(), line!()); let (f, l ) = (file!(), line!());
let msg = format!("Error near {f}:{l}. Current word empty!"); let msg = format!("Error near {f}:{l}. Current word empty!");
let (word, cursor) = word_reader.curr_word().ok_or(msg)?; let (word, _) = word_reader.curr_word().ok_or(msg)?;
// we may have orphaned vars that occur before the first scope // we may have orphaned vars that occur before the first scope
if word == "$var" { if word == "$var" {

View file

@ -12,17 +12,9 @@ pub(super) struct Word(pub(super) usize);
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub(super) struct Cursor(pub(super) Line, pub(super) Word); pub(super) struct Cursor(pub(super) Line, pub(super) Word);
impl Cursor {
pub(super) fn error(&self, word : &str) -> Result<(), String> {
let Cursor(Line(line_no), Word(word_no)) = self;
Err(format!("Error on word '{word}' {word_no} words into line {line_no}!"))
}
}
pub struct WordReader { pub struct WordReader {
reader : io::BufReader<File>, reader : io::BufReader<File>,
EOF : bool, eof : bool,
buffers : Vec<String>, buffers : Vec<String>,
curr_line : usize, curr_line : usize,
str_slices : VecDeque<(*const u8, usize, Cursor)>, str_slices : VecDeque<(*const u8, usize, Cursor)>,
@ -31,10 +23,10 @@ pub struct WordReader {
impl WordReader { impl WordReader {
pub(super) fn new(file : File) -> WordReader { pub(super) fn new(file : File) -> WordReader {
let mut reader = io::BufReader::new(file); let reader = io::BufReader::new(file);
WordReader { WordReader {
reader : reader, reader : reader,
EOF : false, eof : false,
buffers : vec![], buffers : vec![],
curr_line : 0, curr_line : 0,
str_slices : VecDeque::new(), str_slices : VecDeque::new(),
@ -48,7 +40,7 @@ impl WordReader {
if self.str_slices.is_empty() { if self.str_slices.is_empty() {
self.buffers.clear(); self.buffers.clear();
if self.EOF {return None} if self.eof {return None}
let num_buffers = 10; let num_buffers = 10;
@ -60,11 +52,11 @@ impl WordReader {
// if we've reached the end of the file on the first attempt to read // if we've reached the end of the file on the first attempt to read
// a line in this for loop, no further attempts are necessary and we // a line in this for loop, no further attempts are necessary and we
if bytes_read == 0 { if bytes_read == 0 {
self.EOF = true; self.eof = true;
break; break;
} }
let mut words = self.buffers[buf_idx].split_ascii_whitespace(); let words = self.buffers[buf_idx].split_ascii_whitespace();
for word in words.enumerate() { for word in words.enumerate() {
let (word_idx, word) = word; let (word_idx, word) = word;

View file

@ -4,7 +4,7 @@ use chrono::prelude::*;
pub(super) struct Version(pub String); pub(super) struct Version(pub String);
#[derive(Debug)] #[derive(Debug)]
pub(super) enum Timescale {fs, ps, ns, us, ms, s, unit} pub(super) enum Timescale {Fs, Ps, Ns, Us, Ms, S, Unit}
#[derive(Debug)] #[derive(Debug)]
pub(super) struct Metadata { pub(super) struct Metadata {
@ -13,10 +13,10 @@ pub(super) struct Metadata {
pub(super) timescale : (Option<u32>, Timescale)} pub(super) timescale : (Option<u32>, Timescale)}
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
pub(super) struct Scope_Idx(pub(super) usize); pub(super) struct ScopeIdx(pub(super) usize);
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
pub(super) struct Signal_Idx(pub(super) usize); pub(super) struct SignalIdx(pub(super) usize);
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
pub(super) struct TimelineIdx(pub(super) u32); pub(super) struct TimelineIdx(pub(super) u32);
@ -25,13 +25,13 @@ pub(super) struct TimelineIdx(pub(super) u32);
pub struct StartIdx(pub(super) u32); pub struct StartIdx(pub(super) u32);
#[derive(Debug)] #[derive(Debug)]
pub(super) enum Sig_Type {Integer, Parameter, Real, Reg, Str, Wire, Tri1, Time} pub(super) enum SigType {Integer, Parameter, Real, Reg, Str, Wire, Tri1, Time}
#[derive(Debug)] #[derive(Debug)]
pub(super) enum Signal{ pub(super) enum Signal{
Data{ Data{
name : String, name : String,
sig_type : Sig_Type, sig_type : SigType,
// I've seen a 0 bit signal parameter in a xilinx // I've seen a 0 bit signal parameter in a xilinx
// simulation before that gets assigned 1 bit values. // simulation before that gets assigned 1 bit values.
// I consider this to be bad behavior. We capture such // I consider this to be bad behavior. We capture such
@ -39,28 +39,28 @@ pub(super) enum Signal{
signal_error : Option<String>, signal_error : Option<String>,
num_bits : Option<usize>, num_bits : Option<usize>,
// TODO : may be able to remove self_idx // TODO : may be able to remove self_idx
self_idx : Signal_Idx, self_idx : SignalIdx,
// we could encounter a mix of pure values and strings // we could encounter a mix of pure values and strings
// for the same signal timeline // for the same signal timeline
u8_timeline : Vec<u8>, u8_timeline : Vec<u8>,
u8_timeline_markers : Vec<(TimelineIdx)>, u8_timeline_markers : Vec<TimelineIdx>,
string_timeline : Vec<String>, string_timeline : Vec<String>,
string_timeline_markers : Vec<(TimelineIdx)>, string_timeline_markers : Vec<TimelineIdx>,
scope_parent : Scope_Idx}, scope_parent : ScopeIdx},
Alias{ Alias{
name : String, name : String,
signal_alias : Signal_Idx} signal_alias : SignalIdx}
} }
#[derive(Debug)] #[derive(Debug)]
pub(super) struct Scope { pub(super) struct Scope {
pub(super) name : String, pub(super) name : String,
pub(super) parent_idx : Option<Scope_Idx>, pub(super) parent_idx : Option<ScopeIdx>,
pub(super) self_idx : Scope_Idx, pub(super) self_idx : ScopeIdx,
pub(super) child_signals : Vec<Signal_Idx>, pub(super) child_signals : Vec<SignalIdx>,
pub(super) child_scopes : Vec<Scope_Idx>} pub(super) child_scopes : Vec<ScopeIdx>}
// TODO: document how timeline is represented // TODO: document how timeline is represented
@ -71,27 +71,27 @@ pub struct VCD {
pub timeline_markers : Vec<StartIdx>, pub timeline_markers : Vec<StartIdx>,
pub(super) all_signals : Vec<Signal>, pub(super) all_signals : Vec<Signal>,
pub(super) all_scopes : Vec<Scope>, pub(super) all_scopes : Vec<Scope>,
pub(super) scope_roots : Vec<Scope_Idx>} pub(super) scope_roots : Vec<ScopeIdx>}
impl VCD { impl VCD {
// TODO : make this a generic traversal function that applies specified // TODO : make this a generic traversal function that applies specified
// functions upon encountering scopes and signals // functions upon encountering scopes and signals
fn print_scope_tree( fn print_scope_tree(
&self, &self,
root_scope_idx : Scope_Idx, root_scope_idx : ScopeIdx,
depth : usize) depth : usize)
{ {
let all_scopes = &self.all_scopes; let all_scopes = &self.all_scopes;
let all_signals = &self.all_signals; let all_signals = &self.all_signals;
let indent = " ".repeat(depth * 4); let indent = " ".repeat(depth * 4);
let Scope_Idx(root_scope_idx) = root_scope_idx; let ScopeIdx(root_scope_idx) = root_scope_idx;
let root_scope = &all_scopes[root_scope_idx]; let root_scope = &all_scopes[root_scope_idx];
let root_scope_name = &root_scope.name; let root_scope_name = &root_scope.name;
println!("{indent}scope: {root_scope_name}"); println!("{indent}scope: {root_scope_name}");
for Signal_Idx(ref signal_idx) in &root_scope.child_signals { for SignalIdx(ref signal_idx) in &root_scope.child_signals {
let child_signal = &all_signals[*signal_idx]; let child_signal = &all_signals[*signal_idx];
let name = match child_signal { let name = match child_signal {
Signal::Data{name, ..} => {name} Signal::Data{name, ..} => {name}
@ -123,14 +123,12 @@ impl VCD {
Signal::Alias {..} => {} Signal::Alias {..} => {}
Signal::Data { Signal::Data {
name, name,
sig_type,
num_bits,
self_idx, self_idx,
u8_timeline, u8_timeline,
.. } => { .. } => {
if u8_timeline.len() > max_len { if u8_timeline.len() > max_len {
max_len = u8_timeline.len(); max_len = u8_timeline.len();
let Signal_Idx(idx_usize) = self_idx; let SignalIdx(idx_usize) = self_idx;
idx = *idx_usize; idx = *idx_usize;
signal_name = name.clone(); signal_name = name.clone();
} }