2022-07-14 20:46:11 +00:00
|
|
|
use std::{fs::File};
|
2022-07-19 17:48:22 +00:00
|
|
|
use std::collections::HashMap;
|
2022-07-26 01:16:15 +00:00
|
|
|
use chrono::format::format;
|
2022-07-19 17:48:22 +00:00
|
|
|
use num::BigInt;
|
|
|
|
use num::bigint::ToBigInt;
|
2022-06-04 01:06:46 +00:00
|
|
|
|
2022-06-19 13:44:57 +00:00
|
|
|
use super::*;
|
2022-06-13 02:52:24 +00:00
|
|
|
|
2022-06-19 13:44:57 +00:00
|
|
|
mod combinator_atoms;
|
|
|
|
use combinator_atoms::*;
|
2022-06-04 01:06:46 +00:00
|
|
|
|
2022-06-19 13:44:57 +00:00
|
|
|
mod types;
|
|
|
|
use types::*;
|
2022-06-17 22:16:51 +00:00
|
|
|
|
2022-06-22 01:06:51 +00:00
|
|
|
mod metadata;
|
|
|
|
use metadata::*;
|
2022-06-04 01:06:46 +00:00
|
|
|
|
2022-07-18 16:53:44 +00:00
|
|
|
mod scopes;
|
|
|
|
use scopes::*;
|
2022-07-16 00:48:02 +00:00
|
|
|
|
2022-07-26 01:16:15 +00:00
|
|
|
use std::num::{IntErrorKind, ParseIntError};
|
|
|
|
|
2022-07-19 17:48:22 +00:00
|
|
|
use function_name::named;
|
|
|
|
|
2022-07-26 01:16:15 +00:00
|
|
|
/// Sometimes, variables can be listed outside of scopes.
|
|
|
|
/// We call these floating vars.
|
|
|
|
pub(super) fn parse_orphaned_vars<'a>(
|
|
|
|
word_reader : &mut WordReader,
|
|
|
|
vcd : &'a mut VCD,
|
|
|
|
signal_map : &mut HashMap<String, Signal_Idx>
|
|
|
|
) -> Result<(), String> {
|
|
|
|
// create scope for unscoped signals if such a scope does not
|
|
|
|
// yet exist
|
|
|
|
let scope_name = "Orphaned Signals";
|
|
|
|
|
|
|
|
// set default scope_idx to the count of existing scope as we
|
|
|
|
// generally set scope.self_idx to the number of existing scopes
|
|
|
|
// when that particular scope was inserted
|
|
|
|
let mut scope_idx = Scope_Idx(vcd.all_scopes.len());
|
|
|
|
|
|
|
|
// Override scope_idx if we find a scope named "Orphaned Signals"
|
|
|
|
// already exists
|
|
|
|
let mut scope_already_exists = false;
|
|
|
|
for scope in &vcd.all_scopes {
|
|
|
|
if scope.name == scope_name {
|
|
|
|
scope_idx = scope.self_idx;
|
|
|
|
scope_already_exists = true;
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !scope_already_exists {
|
|
|
|
vcd.all_scopes.push(
|
|
|
|
Scope {
|
|
|
|
name: scope_name.to_string(),
|
|
|
|
parent_idx: None,
|
|
|
|
self_idx: scope_idx,
|
|
|
|
child_signals: vec![],
|
|
|
|
child_scopes: vec![]
|
|
|
|
}
|
|
|
|
);
|
|
|
|
vcd.scope_roots.push(scope_idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
// we can go ahead and parse the current var as we've already encountered
|
|
|
|
// "$var" before now.
|
|
|
|
parse_var(word_reader, scope_idx, vcd, signal_map)?;
|
|
|
|
|
|
|
|
loop {
|
|
|
|
let next_word = word_reader.next_word();
|
|
|
|
|
|
|
|
// we shouldn't reach the end of the file here...
|
|
|
|
if next_word.is_none() {
|
|
|
|
let (f, l )= (file!(), line!());
|
|
|
|
let msg = format!("Error near {f}:{l}.\
|
|
|
|
Reached end of file without terminating parser");
|
|
|
|
Err(msg)?;
|
|
|
|
};
|
|
|
|
|
|
|
|
let (word, cursor) = next_word.unwrap();
|
|
|
|
|
|
|
|
match word {
|
|
|
|
"$var" => {
|
|
|
|
parse_var(word_reader, scope_idx, vcd, signal_map)?;
|
|
|
|
}
|
|
|
|
"$scope" => {break}
|
|
|
|
_ => {
|
|
|
|
let (f, l )= (file!(), line!());
|
|
|
|
let msg = format!("Error near {f}:{l}.\
|
|
|
|
Expected $scope or $var, found {word} at {cursor:?}");
|
|
|
|
Err(msg)?;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-07-19 17:48:22 +00:00
|
|
|
#[named]
|
|
|
|
fn parse_events<'a>(
|
|
|
|
word_reader : &mut WordReader,
|
|
|
|
vcd : &'a mut VCD,
|
|
|
|
signal_map : &mut HashMap<String, Signal_Idx>
|
|
|
|
) -> Result<(), String> {
|
|
|
|
|
|
|
|
loop {
|
|
|
|
let next_word = word_reader.next_word();
|
|
|
|
// if we've reached the end of the file, then there is obviously
|
|
|
|
// nothing left to do...
|
|
|
|
if next_word.is_none() {break};
|
|
|
|
|
2022-07-20 02:05:00 +00:00
|
|
|
|
2022-07-19 17:48:22 +00:00
|
|
|
let (word, cursor) = next_word.unwrap();
|
2022-07-20 14:38:56 +00:00
|
|
|
let Cursor(Line(_), Word(word_in_line_idx)) = cursor;
|
|
|
|
// we only want to match on the first word in a line
|
|
|
|
if word_in_line_idx != 1 {continue}
|
2022-07-19 17:48:22 +00:00
|
|
|
match &word[0..1] {
|
2022-07-20 02:05:00 +00:00
|
|
|
"$" => {}
|
|
|
|
"#" => {
|
|
|
|
let value = &word[1..];
|
2022-07-26 01:16:15 +00:00
|
|
|
// we try to parse the timestamp into the Value unsigned
|
|
|
|
// variant used to hold the previous timestamp. Doing this
|
|
|
|
// may fail with PosOverflow, which we would store in parse_ok,
|
|
|
|
// and later try to remedy with bigger unsigned variants of Value.
|
|
|
|
let parse_ok =
|
|
|
|
if let Value::u8(_) = vcd.cursor {
|
|
|
|
let value = value.parse::<u8>();
|
|
|
|
match value {
|
|
|
|
Ok(value) => {
|
|
|
|
vcd.cursor = Value::u8(value);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
Err(e) => Err(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if let Value::u16(_) = vcd.cursor {
|
|
|
|
let value = value.parse::<u16>();
|
|
|
|
match value {
|
|
|
|
Ok(value) => {
|
|
|
|
vcd.cursor = Value::u16(value);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
Err(e) => Err(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if let Value::u32(_) = vcd.cursor {
|
|
|
|
let value = value.parse::<u32>();
|
|
|
|
match value {
|
|
|
|
Ok(value) => {
|
|
|
|
vcd.cursor = Value::u32(value);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
Err(e) => Err(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if let Value::u64(_) = vcd.cursor {
|
|
|
|
let value = value.parse::<u64>();
|
|
|
|
match value {
|
|
|
|
Ok(value) => {
|
|
|
|
vcd.cursor = Value::u64(value);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
Err(e) => Err(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
let value = BigInt::parse_bytes(value.as_bytes(), 10).ok_or(
|
|
|
|
format!("failed to parse {value} as BigInt at {cursor:?}").as_str())?;
|
|
|
|
vcd.cursor = Value::BigInt(value);
|
|
|
|
Ok(())
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// If there was no parse error, we don't evaluate any more logic
|
|
|
|
// in this match arm and simply continue to the next iteration of
|
|
|
|
// the outer loop to evaluate the next word.
|
|
|
|
if parse_ok.is_ok() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try parsing value as u16 since there was a previous
|
|
|
|
// PosOverflow error, and record if this parse attempt
|
|
|
|
// was Ok or Err in parse_ok.
|
|
|
|
let parse_ok =
|
|
|
|
{
|
|
|
|
let e = parse_ok.unwrap_err();
|
|
|
|
// There could have been other parse errors...
|
|
|
|
// Return Err below if there were.
|
|
|
|
if e.kind() != &IntErrorKind::PosOverflow {
|
|
|
|
Err(format!("{e:?}"))?;
|
|
|
|
}
|
|
|
|
|
|
|
|
match value.parse::<u16>() {
|
|
|
|
Ok(value) => {
|
|
|
|
vcd.cursor = Value::u16(value);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
Err(e) => Err(e)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// If there was no parse error, we don't evaluate any more logic
|
|
|
|
// in this match arm and simply continue to the next iteration of
|
|
|
|
// the outer loop to evaluate the next word.
|
|
|
|
if parse_ok.is_ok() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try parsing value as u32 since there was a previous
|
|
|
|
// PosOverflow error, and record if this parse attempt
|
|
|
|
// was Ok or Err in parse_ok.
|
|
|
|
let parse_ok =
|
|
|
|
{
|
|
|
|
let e = parse_ok.unwrap_err();
|
|
|
|
// There could have been other parse errors...
|
|
|
|
// Return Err below if there were.
|
|
|
|
if e.kind() != &IntErrorKind::PosOverflow {
|
|
|
|
Err(format!("{e:?}"))?;
|
|
|
|
}
|
|
|
|
|
|
|
|
match value.parse::<u32>() {
|
|
|
|
Ok(value) => {
|
|
|
|
vcd.cursor = Value::u32(value);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
Err(e) => Err(e)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// If there was no parse error, we don't evaluate any more logic
|
|
|
|
// in this match arm and simply continue to the next iteration of
|
|
|
|
// the outer loop to evaluate the next word.
|
|
|
|
if parse_ok.is_ok() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try parsing value as u64 since there was a previous
|
|
|
|
// PosOverflow error, and record if this parse attempt
|
|
|
|
// was Ok or Err in parse_ok.
|
|
|
|
let parse_ok =
|
|
|
|
{
|
|
|
|
let e = parse_ok.unwrap_err();
|
|
|
|
// There could have been other parse errors...
|
|
|
|
// Return Err below if there were.
|
|
|
|
if e.kind() != &IntErrorKind::PosOverflow {
|
|
|
|
Err(format!("{e:?}"))?;
|
|
|
|
}
|
|
|
|
|
|
|
|
match value.parse::<u64>() {
|
|
|
|
Ok(value) => {
|
|
|
|
vcd.cursor = Value::u64(value);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
Err(e) => Err(e)
|
|
|
|
}
|
|
|
|
};
|
2022-07-20 02:05:00 +00:00
|
|
|
}
|
|
|
|
"0" => {
|
2022-07-20 14:38:56 +00:00
|
|
|
// lokup signal idx
|
|
|
|
let hash = &word[1..].to_string();
|
|
|
|
let Signal_Idx(ref signal_idx) = signal_map.get(hash).ok_or(
|
|
|
|
format!("failed to lookup signal {hash} at {cursor:?}").as_str())?;
|
|
|
|
|
|
|
|
// account for fact that signal idx could be an alias, so there
|
|
|
|
// could be one step of indirection
|
|
|
|
let signal_idx =
|
|
|
|
{
|
|
|
|
let signal = vcd.all_signals.get(*signal_idx).unwrap();
|
|
|
|
match signal {
|
|
|
|
Signal::Data {..} => {signal_idx.clone()}
|
|
|
|
Signal::Alias {name, signal_alias} => {
|
|
|
|
let Signal_Idx(ref signal_idx) = signal_alias;
|
|
|
|
signal_idx.clone()
|
|
|
|
|
2022-07-20 02:05:00 +00:00
|
|
|
}
|
2022-07-20 14:38:56 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// after handling potential indirection, go ahead and update the timeline
|
|
|
|
// of the signal signal_idx references
|
2022-07-26 01:16:15 +00:00
|
|
|
let signal = vcd.all_signals.get_mut(0usize).unwrap();
|
|
|
|
// let signal = vcd.all_signals.get_mut(signal_idx).unwrap();
|
2022-07-20 14:38:56 +00:00
|
|
|
match signal {
|
|
|
|
Signal::Data {name, sig_type, num_bits,
|
|
|
|
self_idx, timeline, scope_parent} => {
|
|
|
|
let value = 0.to_bigint().unwrap();
|
2022-07-26 01:16:15 +00:00
|
|
|
let pair = (vcd.cursor.clone(), Value::u8(0));
|
2022-07-20 14:38:56 +00:00
|
|
|
timeline.push(pair);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
Signal::Alias {..} => {
|
|
|
|
let (f, l )= (file!(), line!());
|
|
|
|
let msg = format!(
|
|
|
|
"Error near {f}:{l}, a signal alias should not point to a signal alias.\n\
|
|
|
|
This error occurred while parsing vcd file at {cursor:?}");
|
|
|
|
Err(msg)
|
|
|
|
}
|
|
|
|
}?;
|
|
|
|
}
|
2022-07-26 01:16:15 +00:00
|
|
|
// "1" => {
|
|
|
|
// // lokup signal idx
|
|
|
|
// let hash = &word[1..].to_string();
|
|
|
|
// let Signal_Idx(ref signal_idx) = signal_map.get(hash).ok_or(
|
|
|
|
// format!("failed to lookup signal {hash} at {cursor:?}").as_str())?;
|
2022-07-20 14:38:56 +00:00
|
|
|
|
2022-07-26 01:16:15 +00:00
|
|
|
// // account for fact that signal idx could be an alias, so there
|
|
|
|
// // could be one step of indirection
|
|
|
|
// let signal_idx =
|
|
|
|
// {
|
|
|
|
// let signal = vcd.all_signals.get(*signal_idx).unwrap();
|
|
|
|
// match signal {
|
|
|
|
// Signal::Data {..} => {signal_idx.clone()}
|
|
|
|
// Signal::Alias {name, signal_alias} => {
|
|
|
|
// let Signal_Idx(ref signal_idx) = signal_alias;
|
|
|
|
// signal_idx.clone()
|
2022-07-20 02:05:00 +00:00
|
|
|
|
2022-07-26 01:16:15 +00:00
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// };
|
2022-07-20 14:38:56 +00:00
|
|
|
|
2022-07-26 01:16:15 +00:00
|
|
|
// // after handling potential indirection, go ahead and update the timeline
|
|
|
|
// // of the signal signal_idx references
|
|
|
|
// let signal = vcd.all_signals.get_mut(signal_idx).unwrap();
|
|
|
|
// match signal {
|
|
|
|
// Signal::Data {name, sig_type, num_bits,
|
|
|
|
// self_idx, timeline, scope_parent} => {
|
|
|
|
// let value = 1.to_bigint().unwrap();
|
|
|
|
// let pair = (TimeStamp(vcd.cursor.clone()), Sig_Value::Numeric(value));
|
|
|
|
// timeline.push(pair);
|
|
|
|
// Ok(())
|
|
|
|
// }
|
|
|
|
// Signal::Alias {..} => {
|
|
|
|
// let (f, l )= (file!(), line!());
|
|
|
|
// let msg = format!(
|
|
|
|
// "Error near {f}:{l}, a signal alias should not point to a signal alias.\n\
|
|
|
|
// This error occurred while parsing vcd file at {cursor:?}");
|
|
|
|
// Err(msg)
|
|
|
|
// }
|
|
|
|
// }?;
|
|
|
|
// }
|
2022-07-19 17:48:22 +00:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-07-16 00:48:02 +00:00
|
|
|
pub fn parse_vcd(file : File) -> Result<VCD, String> {
|
2022-06-04 01:06:46 +00:00
|
|
|
let mut word_gen = WordReader::new(file);
|
|
|
|
|
2022-07-14 20:46:11 +00:00
|
|
|
let header = parse_metadata(&mut word_gen)?;
|
2022-07-13 00:02:45 +00:00
|
|
|
|
2022-07-26 01:16:15 +00:00
|
|
|
// later, we'll need to map parsed ascii symbols to their
|
|
|
|
// respective signal indexes
|
2022-07-14 20:46:11 +00:00
|
|
|
let mut signal_map = std::collections::HashMap::new();
|
2022-07-13 00:02:45 +00:00
|
|
|
|
2022-07-26 01:16:15 +00:00
|
|
|
// after we parse metadata, we form VCD object
|
2022-07-13 00:02:45 +00:00
|
|
|
let mut vcd = VCD{
|
2022-07-14 22:52:12 +00:00
|
|
|
metadata : header,
|
2022-07-26 01:16:15 +00:00
|
|
|
cursor : Value::u8(0),
|
2022-07-13 00:02:45 +00:00
|
|
|
all_signals: vec![],
|
2022-07-14 22:52:12 +00:00
|
|
|
all_scopes : vec![],
|
|
|
|
scope_roots: vec![],
|
2022-07-13 00:02:45 +00:00
|
|
|
};
|
2022-07-14 20:46:11 +00:00
|
|
|
|
2022-07-26 01:16:15 +00:00
|
|
|
// The last word parse_metadata saw determines how we proceed.
|
|
|
|
// There may be some orphan vars we must parse first before
|
|
|
|
// parsing scoped vars.
|
|
|
|
let (f, l ) = (file!(), line!());
|
|
|
|
let msg = format!("Error near {f}:{l}. Current word empty!");
|
|
|
|
let (word, cursor) = word_gen.curr_word().expect(msg.as_str());
|
|
|
|
match word {
|
|
|
|
"$scope" => {
|
|
|
|
parse_scopes(&mut word_gen, None, &mut vcd, &mut signal_map)
|
|
|
|
}
|
|
|
|
"$var" => {
|
|
|
|
parse_orphaned_vars(&mut word_gen, &mut vcd, &mut signal_map)?;
|
|
|
|
parse_scopes(&mut word_gen, None, &mut vcd, &mut signal_map)
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
let (f, l )= (file!(), line!());
|
|
|
|
let msg = format!("Error near {f}:{l}.\
|
|
|
|
Expected $scope or $var, found {word} at {cursor:?}");
|
|
|
|
Err(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
}?;
|
2022-07-20 02:05:00 +00:00
|
|
|
parse_events(&mut word_gen, &mut vcd, &mut signal_map)?;
|
|
|
|
dbg!(&vcd.cursor);
|
2022-07-16 00:48:02 +00:00
|
|
|
|
|
|
|
Ok(vcd)
|
2022-06-18 05:00:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2022-06-24 00:54:27 +00:00
|
|
|
use crate::test;
|
2022-06-18 05:00:01 +00:00
|
|
|
use std::fs::File;
|
|
|
|
#[test]
|
|
|
|
fn headers() {
|
2022-06-25 02:22:55 +00:00
|
|
|
// TODO: eventually, once all dates pass, merge the following
|
|
|
|
// two loops
|
|
|
|
// testing dates
|
|
|
|
for file in test::good_date_files {
|
2022-06-18 05:00:01 +00:00
|
|
|
let metadata = parse_metadata(
|
|
|
|
&mut WordReader::new(
|
|
|
|
File::open(file)
|
|
|
|
.unwrap()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
assert!(metadata.is_ok());
|
|
|
|
assert!(metadata.unwrap().date.is_some());
|
|
|
|
}
|
|
|
|
|
2022-06-25 02:22:55 +00:00
|
|
|
for file in test::files {
|
|
|
|
let metadata = parse_metadata(
|
|
|
|
&mut WordReader::new(
|
|
|
|
File::open(file)
|
|
|
|
.unwrap()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
assert!(metadata.is_ok());
|
|
|
|
|
|
|
|
let (scalar, timescale) = metadata.unwrap().timescale;
|
|
|
|
assert!(scalar.is_some());
|
|
|
|
}
|
|
|
|
|
2022-06-18 05:00:01 +00:00
|
|
|
}
|
2022-07-16 00:48:02 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn scopes() {
|
2022-07-18 16:53:44 +00:00
|
|
|
// see if we can parse all signal trees successfully
|
2022-07-16 00:48:02 +00:00
|
|
|
for file_name in test::files {
|
|
|
|
let file = File::open(file_name).unwrap();
|
|
|
|
let vcd = parse_vcd(file);
|
|
|
|
|
|
|
|
if !vcd.is_ok() {
|
|
|
|
dbg!(file_name);
|
|
|
|
vcd.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
// assert!(vcd.is_ok());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-06-04 01:06:46 +00:00
|
|
|
}
|