presumably using macros everywhere now

This commit is contained in:
Yehowshua Immanuel 2022-08-11 17:35:40 -04:00
parent 0946d13e6e
commit c53c9684e6
8 changed files with 384 additions and 308 deletions

View file

@ -20,7 +20,8 @@ fn main() -> std::io::Result<()> {
let now = Instant::now(); let now = Instant::now();
let file = File::open(&args.path)?; let file = File::open(&args.path)?;
parse_vcd(file).unwrap(); let vcd = parse_vcd(file).unwrap();
// vcd.
let elapsed = now.elapsed(); let elapsed = now.elapsed();
println!("Elapsed: {:.2?}", elapsed); println!("Elapsed: {:.2?}", elapsed);

View file

@ -1,5 +1,5 @@
use super::reader::{next_word, WordReader};
use super::types::ParseResult; use super::types::ParseResult;
use super::reader::WordReader;
pub(super) fn digit(chr: u8) -> bool { pub(super) fn digit(chr: u8) -> bool {
let zero = b'0' as u8; let zero = b'0' as u8;
@ -7,7 +7,7 @@ pub(super) fn digit(chr : u8) -> bool {
let between_zero_and_nine = (chr >= zero) && (nine >= chr); let between_zero_and_nine = (chr >= zero) && (nine >= chr);
return between_zero_and_nine return between_zero_and_nine;
} }
pub(super) fn take_until<'a>(word: &'a str, pattern: u8) -> ParseResult<'a> { pub(super) fn take_until<'a>(word: &'a str, pattern: u8) -> ParseResult<'a> {
@ -15,19 +15,16 @@ pub(super) fn take_until<'a>(word : &'a str, pattern : u8) -> ParseResult<'a> {
for chr in word.as_bytes() { for chr in word.as_bytes() {
if *chr == pattern { if *chr == pattern {
break break;
} } else {
else {
new_start += 1; new_start += 1;
} }
} }
return return ParseResult {
ParseResult{
matched: &word[0..new_start], matched: &word[0..new_start],
residual : &word[new_start..] residual: &word[new_start..],
}; };
} }
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> {
@ -36,18 +33,15 @@ pub(super) fn take_while<'a>(word : &'a str, cond : fn(u8) -> bool) -> ParseResu
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 { break;
break
} }
} }
return return ParseResult {
ParseResult{
matched: &word[0..new_start], matched: &word[0..new_start],
residual : &word[new_start..] residual: &word[new_start..],
}; };
} }
pub(super) fn tag<'a>(word: &'a str, pattern: &'a str) -> ParseResult<'a> { pub(super) fn tag<'a>(word: &'a str, pattern: &'a str) -> ParseResult<'a> {
@ -59,30 +53,26 @@ pub(super) fn tag<'a>(word : &'a str, pattern : &'a str) -> ParseResult<'a> {
let mut res = true; let mut res = true;
for (c_lhs, c_rhs) in iter { for (c_lhs, c_rhs) in iter {
res = res && (c_lhs == c_rhs); res = res && (c_lhs == c_rhs);
if !res {break} if !res {
break;
}
new_start += 1; new_start += 1;
} }
return return ParseResult {
ParseResult{
matched: &word[0..new_start], matched: &word[0..new_start],
residual : &word[new_start..] residual: &word[new_start..],
}; };
} }
pub(super) fn ident( pub(super) fn ident(word_reader: &mut WordReader, keyword: &str) -> Result<(), String> {
word_reader : &mut WordReader,
keyword : &str,
) -> Result<(), String> {
// let keyword = "module"; // let keyword = "module";
let (word, cursor) = next_word!(word_reader)?;
let (word, cursor) = word_reader.next_word()?;
if word == keyword { if word == keyword {
return Ok(()) return Ok(());
} } else {
else {
let err = format!("found keyword `{word}` but expected `{keyword}` on {cursor:?}"); let err = format!("found keyword `{word}` but expected `{keyword}` on {cursor:?}");
return Err(err) return Err(err);
} }
} }

View file

@ -10,10 +10,11 @@ pub(super) fn parse_events<'a>(
loop { loop {
let next_word = word_reader.next_word(); let next_word = word_reader.next_word();
// The following is the only case where eof is not an error. // The following is the only case where eof is not an error.
// If we've reached the end of the file, then there is obviously // If we've reached the end of the file, then there is obviously
// nothing left to do... // nothing left to do...
if next_word.is_err() { if next_word.is_none() {
break; break;
}; };
@ -77,7 +78,7 @@ pub(super) fn parse_events<'a>(
} }
// this word should be the signal alias // this word should be the signal alias
let (word, cursor) = word_reader.next_word().unwrap(); let (word, cursor) = next_word!(word_reader)?;
// lookup signal idx // lookup signal idx
let SignalIdx(ref signal_idx) = signal_map.get(word).ok_or(()).map_err(|_| { let SignalIdx(ref signal_idx) = signal_map.get(word).ok_or(()).map_err(|_| {

View file

@ -10,7 +10,6 @@ pub(super) fn parse_date(
word_and_ctx4: (&str, &Cursor), word_and_ctx4: (&str, &Cursor),
word_and_ctx5: (&str, &Cursor), word_and_ctx5: (&str, &Cursor),
) -> Result<DateTime<Utc>, String> { ) -> Result<DateTime<Utc>, String> {
let day = { let day = {
// check for another word in the file // check for another word in the file
let (word, cursor) = word_and_ctx1; let (word, cursor) = word_and_ctx1;
@ -20,7 +19,7 @@ pub(super) fn parse_date(
let msg = format!("Error near {}:{}.", file!(), line!()); let msg = format!("Error near {}:{}.", file!(), line!());
let msg2 = format!("{word} is not a valid weekday : expected one of {days:?}\n"); let msg2 = format!("{word} is not a valid weekday : expected one of {days:?}\n");
let msg3 = format!("failure location: {cursor:?}"); let msg3 = format!("failure location: {cursor:?}");
return Err(format!("{}{}{}", msg, msg2, msg3)) return Err(format!("{}{}{}", msg, msg2, msg3));
} }
word.to_string() word.to_string()
@ -31,16 +30,14 @@ pub(super) fn parse_date(
let (word, cursor) = word_and_ctx2; let (word, cursor) = word_and_ctx2;
let months = [ let months = [
"Jan", "Feb", "Mar", "Apr", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec",
"May", "Jun", "Jul", "Aug",
"Sept", "Oct", "Nov", "Dec",
]; ];
if !months.contains(&word) { if !months.contains(&word) {
let msg = format!("Error near {}:{}.", file!(), line!()); let msg = format!("Error near {}:{}.", file!(), line!());
let msg2 = format!("{word} is not a valid month : expected one of {months:?}\n"); let msg2 = format!("{word} is not a valid month : expected one of {months:?}\n");
let msg3 = format!("failure location: {cursor:?}"); let msg3 = format!("failure location: {cursor:?}");
return Err(format!("{}{}{}", msg, msg2, msg3)) return Err(format!("{}{}{}", msg, msg2, msg3));
} }
word.to_string() word.to_string()
@ -52,15 +49,14 @@ pub(super) fn parse_date(
let date: u8 = match word.to_string().parse() { let date: u8 = match word.to_string().parse() {
Ok(date) => date, Ok(date) => date,
Err(e) => {return Err(format!("Error near {}:{}. {e}", file!(), line!()))} Err(e) => return Err(format!("Error near {}:{}. {e}", file!(), line!())),
}; };
if date > 31 { if date > 31 {
let msg = format!("Error near {}:{}.", file!(), line!()); let msg = format!("Error near {}:{}.", file!(), line!());
let msg2 = format!("{word} is not a valid date : must be between 0 and 31\n"); let msg2 = format!("{word} is not a valid date : must be between 0 and 31\n");
let msg3 = format!("failure location: {cursor:?}"); let msg3 = format!("failure location: {cursor:?}");
return Err(format!("{}{}{}", msg, msg2, msg3)) return Err(format!("{}{}{}", msg, msg2, msg3));
} }
date.to_string() date.to_string()
@ -72,7 +68,9 @@ pub(super) fn parse_date(
let res = take_until(word, b':'); let res = take_until(word, b':');
res.assert_match()?; res.assert_match()?;
let hh : u8 = res.matched.to_string() let hh: u8 = res
.matched
.to_string()
.parse() .parse()
.map_err(|e| format!("Error near {}:{}. {e}", file!(), line!()))?; .map_err(|e| format!("Error near {}:{}. {e}", file!(), line!()))?;
@ -80,14 +78,16 @@ pub(super) fn parse_date(
let msg = format!("Error near {}:{}.", file!(), line!()); let msg = format!("Error near {}:{}.", file!(), line!());
let msg2 = format!("{hh} is not a valid hour : must be between 0 and 23\n"); let msg2 = format!("{hh} is not a valid hour : must be between 0 and 23\n");
let msg3 = format!("failure location: {cursor:?}"); let msg3 = format!("failure location: {cursor:?}");
return Err(format!("{}{}{}", msg, msg2, msg3)) return Err(format!("{}{}{}", msg, msg2, msg3));
} }
// get minute // get minute
let word = &res.residual[1..]; // chop off colon which is at index 0 let word = &res.residual[1..]; // chop off colon which is at index 0
let res = take_until(word, b':'); let res = take_until(word, b':');
res.assert_match()?; res.assert_match()?;
let mm : u8 = res.matched.to_string() let mm: u8 = res
.matched
.to_string()
.parse() .parse()
.map_err(|e| format!("Error near {}:{}. {e}", file!(), line!()))?; .map_err(|e| format!("Error near {}:{}. {e}", file!(), line!()))?;
@ -95,14 +95,15 @@ pub(super) fn parse_date(
let msg = format!("Error near {}:{}.", file!(), line!()); let msg = format!("Error near {}:{}.", file!(), line!());
let msg2 = format!("{mm} is not a valid minute : must be between 0 and 60\n"); let msg2 = format!("{mm} is not a valid minute : must be between 0 and 60\n");
let msg3 = format!("failure location: {cursor:?}"); let msg3 = format!("failure location: {cursor:?}");
return Err(format!("{}{}{}", msg, msg2, msg3)) return Err(format!("{}{}{}", msg, msg2, msg3));
} }
// get second // get second
// let ss : u8 = remainder.to_string().parse().unwrap(); // let ss : u8 = remainder.to_string().parse().unwrap();
res.assert_residual()?; res.assert_residual()?;
let residual = &res.residual[1..]; // chop of colon which is at index 0 let residual = &res.residual[1..]; // chop of colon which is at index 0
let ss : u8 = residual.to_string() let ss: u8 = residual
.to_string()
.parse() .parse()
.map_err(|e| format!("Error near {}:{}. {e}", file!(), line!()))?; .map_err(|e| format!("Error near {}:{}. {e}", file!(), line!()))?;
@ -110,7 +111,7 @@ pub(super) fn parse_date(
let msg = format!("Error near {}:{}.", file!(), line!()); let msg = format!("Error near {}:{}.", file!(), line!());
let msg2 = format!("{ss} is not a valid second : must be between 0 and 60\n"); let msg2 = format!("{ss} is not a valid second : must be between 0 and 60\n");
let msg3 = format!("failure location: {cursor:?}"); let msg3 = format!("failure location: {cursor:?}");
return Err(format!("{}{}{}", msg, msg2, msg3)) return Err(format!("{}{}{}", msg, msg2, msg3));
} }
(hh.to_string(), mm.to_string(), ss.to_string()) (hh.to_string(), mm.to_string(), ss.to_string())
}; };
@ -126,93 +127,109 @@ pub(super) fn parse_date(
let full_date = format!("{day} {month} {date} {hh}:{mm}:{ss} {year}"); let full_date = format!("{day} {month} {date} {hh}:{mm}:{ss} {year}");
let full_date = Utc.datetime_from_str(full_date.as_str(), "%a %b %e %T %Y"); let full_date = Utc.datetime_from_str(full_date.as_str(), "%a %b %e %T %Y");
if full_date.is_ok() { if full_date.is_ok() {
return Ok(full_date.unwrap()) return Ok(full_date.unwrap());
} }
Err(format!("Error near {}:{}. Failed to parse date.", file!(), line!())) Err(format!(
"Error near {}:{}. Failed to parse date.",
file!(),
line!()
))
} }
pub(super) fn parse_version(word_reader: &mut WordReader) -> Result<Version, String> { pub(super) fn parse_version(word_reader: &mut WordReader) -> Result<Version, String> {
let mut version = String::new(); let mut version = String::new();
loop { loop {
let (word, _) = word_reader.next_word()?; let (word, _) = next_word!(word_reader)?;
if word == "$end" { if word == "$end" {
// truncate trailing whitespace // truncate trailing whitespace
let version = version[0..(version.len() - 1)].to_string(); let version = version[0..(version.len() - 1)].to_string();
return Ok(Version(version)) return Ok(Version(version));
} else {
}
else {
version.push_str(word); version.push_str(word);
version.push_str(" "); version.push_str(" ");
} }
} }
} }
pub(super) fn parse_timescale(word_reader : &mut WordReader) -> Result<(Option<u32>, Timescale), String> { pub(super) fn parse_timescale(
word_reader: &mut WordReader,
) -> Result<(Option<u32>, Timescale), String> {
// we might see `1ps $end` or `1 ps $end` // we might see `1ps $end` or `1 ps $end`
// first get timescale // first get timescale
let (word, _) = word_reader.next_word()?; let (word, _) = next_word!(word_reader)?;
let word = word.to_string(); let ParseResult { matched, residual } = take_while(word, digit);
let ParseResult{matched, residual} = take_while(word.as_str(), digit);
let scalar = matched; let scalar = matched;
let scalar : u32 = scalar.to_string().parse() let scalar: u32 = scalar
.to_string()
.parse()
.map_err(|e| format!("Error near {}:{}. {e}", file!(), line!()))?; .map_err(|e| format!("Error near {}:{}. {e}", file!(), line!()))?;
let timescale = { let timescale = {
if residual == "" { if residual == "" {
let (word, _) = word_reader.next_word()?; let (word, _) = next_word!(word_reader)?;
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(format!("Error near {}:{}. Unknown unit {word}.", file!(), line!()))} _ => Err(format!(
}.unwrap(); "Error near {}:{}. Unknown unit {word}.",
file!(),
line!()
)),
}
.unwrap();
(Some(scalar), unit) (Some(scalar), unit)
} } else {
else {
let unit = match residual { let unit = match residual {
"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(format!("Error near {}:{}. Unknown unit {residual}.", file!(), line!()))} _ => Err(format!(
}.unwrap(); "Error near {}:{}. Unknown unit {residual}.",
file!(),
line!()
)),
}
.unwrap();
(Some(scalar), unit) (Some(scalar), unit)
} }
}; };
// then check for the `$end` keyword // then check for the `$end` keyword
let (end, _) = word_reader.next_word()?; let (word, _) = next_word!(word_reader)?;
tag(end, "$end").assert_match()?; tag(word, "$end").assert_match()?;
return Ok(timescale); return Ok(timescale);
} }
pub(super) fn parse_metadata(word_reader: &mut WordReader) -> Result<Metadata, String> { pub(super) fn parse_metadata(word_reader: &mut WordReader) -> Result<Metadata, String> {
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, _) = word_reader.next_word()?; let (word, _) = word_reader.next_word().ok_or(()).map_err(|_| {
format!(
"Error near {}:{}. Did not expect to reach end of file here.",
file!(),
line!()
)
})?;
let ParseResult { matched, residual } = tag(word, "$"); let ParseResult { matched, residual } = tag(word, "$");
match matched { match matched {
@ -238,7 +255,7 @@ pub(super) fn parse_metadata(word_reader : &mut WordReader) -> Result<Metadata,
let mut lookahead_5_words: Vec<(String, Cursor)> = Vec::new(); let mut lookahead_5_words: Vec<(String, Cursor)> = Vec::new();
for _ in 0..5 { for _ in 0..5 {
let (word, cursor) = word_reader.next_word()?; let (word, cursor) = next_word!(word_reader)?;
let word = word.to_string(); let word = word.to_string();
match word.as_str() { match word.as_str() {
"$end" => { "$end" => {
@ -253,7 +270,9 @@ pub(super) fn parse_metadata(word_reader : &mut WordReader) -> Result<Metadata,
// we no longer attempt to parse date if we weren't able to lookahead 5 // we no longer attempt to parse date if we weren't able to lookahead 5
// words // words
if found_end {continue} if found_end {
continue;
}
let permutations = lookahead_5_words let permutations = lookahead_5_words
.iter() .iter()
@ -281,9 +300,8 @@ pub(super) fn parse_metadata(word_reader : &mut WordReader) -> Result<Metadata,
// store date and exit loop if a match is found // store date and exit loop if a match is found
if parsed_date.is_ok() { if parsed_date.is_ok() {
metadata.date = Some(parsed_date.unwrap()); metadata.date = Some(parsed_date.unwrap());
break break;
} }
} }
} }
"version" => { "version" => {
@ -298,8 +316,8 @@ pub(super) fn parse_metadata(word_reader : &mut WordReader) -> Result<Metadata,
metadata.timescale = timescale.unwrap(); metadata.timescale = timescale.unwrap();
} }
} }
"scope" => {break} "scope" => break,
"var" => {break} "var" => break,
// we keep searching for words until we've found one of the following // we keep searching for words until we've found one of the following
// keywords, ["version", "timescale", "scope", "var"] // keywords, ["version", "timescale", "scope", "var"]
_ => {} _ => {}
@ -308,7 +326,6 @@ pub(super) fn parse_metadata(word_reader : &mut WordReader) -> Result<Metadata,
// if word does not start with `$`, then we keep looping // if word does not start with `$`, then we keep looping
_ => {} _ => {}
} }
} }
return Ok(metadata) return Ok(metadata);
} }

View file

@ -6,60 +6,77 @@ pub(super) fn parse_var<'a>(
word_reader: &mut WordReader, word_reader: &mut WordReader,
parent_scope_idx: ScopeIdx, parent_scope_idx: ScopeIdx,
vcd: &'a mut VCD, vcd: &'a mut VCD,
signal_map : &mut HashMap<String, SignalIdx> signal_map: &mut HashMap<String, SignalIdx>,
) -> Result<(), String> { ) -> Result<(), String> {
let (word, cursor) = word_reader.next_word()?; let (word, cursor) = next_word!(word_reader)?;
let expected_types = ["integer", "parameter", "real", "reg", "string", "wire", "tri1", "time"]; let expected_types = [
"integer",
"parameter",
"real",
"reg",
"string",
"wire",
"tri1",
"time",
];
// $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(SigType::Integer)} "integer" => Ok(SigType::Integer),
"parameter" => {Ok(SigType::Parameter)} "parameter" => Ok(SigType::Parameter),
"real" => {Ok(SigType::Real)} "real" => Ok(SigType::Real),
"reg" => {Ok(SigType::Reg)} "reg" => Ok(SigType::Reg),
"string" => {Ok(SigType::Str)} "string" => Ok(SigType::Str),
"wire" => {Ok(SigType::Wire)} "wire" => Ok(SigType::Wire),
"tri1" => {Ok(SigType::Tri1)} "tri1" => Ok(SigType::Tri1),
"time" => {Ok(SigType::Time)} "time" => Ok(SigType::Time),
_ => { _ => {
let err = format!("Error near {}:{} \ let err = format!(
"Error near {}:{} \
found keyword `{word}` but expected one of \ found keyword `{word}` but expected one of \
{expected_types:?} on {cursor:?}", file!(), line!()); {expected_types:?} on {cursor:?}",
file!(),
line!()
);
Err(err) Err(err)
} }
}?; }?;
let (word, cursor) = word_reader.next_word()?; let (word, cursor) = next_word!(word_reader)?;
let parse_err = format!("failed to parse as usize on {cursor:?}"); let parse_err = format!("failed to parse as usize on {cursor:?}");
// $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 {
SigType::Integer | SigType::Parameter | SigType::Integer
SigType::Real | SigType::Reg | | SigType::Parameter
SigType::Wire | SigType::Tri1 | | SigType::Real
SigType::Time => { | SigType::Reg
| SigType::Wire
| SigType::Tri1
| 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)
} }
// for strings, we don't really care what the number of bits is // for strings, we don't really care what the number of bits is
_ => {None} _ => None,
}; };
// $var parameter 3 a IDLE $end // $var parameter 3 a IDLE $end
// ^ - signal_alias // ^ - signal_alias
let (word, _) = word_reader.next_word()?; let (word, _) = next_word!(word_reader)?;
let signal_alias = word.to_string(); let signal_alias = word.to_string();
// $var parameter 3 a IDLE $end // $var parameter 3 a IDLE $end
// ^^^^ - 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, _) = word_reader.next_word()?; let (word, _) = next_word!(word_reader)?;
match word { match word {
"$end" => {break} "$end" => break,
_ => {full_signal_name.push(word.to_string())} _ => full_signal_name.push(word.to_string()),
} }
} }
let full_signal_name = full_signal_name.join(" "); let full_signal_name = full_signal_name.join(" ");
@ -72,7 +89,8 @@ pub(super) fn parse_var<'a>(
let signal_idx = SignalIdx(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 => {
@ -88,7 +106,8 @@ pub(super) fn parse_var<'a>(
u8_timeline_markers: vec![], u8_timeline_markers: vec![],
string_timeline: vec![], string_timeline: vec![],
string_timeline_markers: vec![], string_timeline_markers: vec![],
scope_parent: parent_scope_idx }; scope_parent: parent_scope_idx,
};
(signal, signal_idx) (signal, signal_idx)
} }
}; };
@ -106,7 +125,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, SignalIdx> 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
@ -124,20 +143,18 @@ fn parse_orphaned_vars<'a>(
if scope.name == scope_name { if scope.name == scope_name {
scope_idx = scope.self_idx; scope_idx = scope.self_idx;
scope_already_exists = true; scope_already_exists = true;
break break;
} }
} }
if !scope_already_exists { if !scope_already_exists {
vcd.all_scopes.push( vcd.all_scopes.push(Scope {
Scope {
name: scope_name.to_string(), name: scope_name.to_string(),
parent_idx: None, parent_idx: None,
self_idx: scope_idx, self_idx: scope_idx,
child_signals: vec![], child_signals: vec![],
child_scopes: vec![] child_scopes: vec![],
} });
);
vcd.scope_roots.push(scope_idx); vcd.scope_roots.push(scope_idx);
} }
@ -146,17 +163,21 @@ fn parse_orphaned_vars<'a>(
parse_var(word_reader, scope_idx, vcd, signal_map)?; parse_var(word_reader, scope_idx, vcd, signal_map)?;
loop { loop {
let (word, cursor) = word_reader.next_word()?; let (word, cursor) = next_word!(word_reader)?;
match word { match word {
"$var" => { "$var" => {
parse_var(word_reader, scope_idx, vcd, signal_map)?; parse_var(word_reader, scope_idx, vcd, signal_map)?;
} }
"$scope" => {break} "$scope" => break,
_ => { _ => {
let msg = format!("Error near {}:{}.\ let msg = format!(
"Error near {}:{}.\
Expected $scope or $var, found \ Expected $scope or $var, found \
{word} at {cursor:?}", file!(), line!()); {word} at {cursor:?}",
file!(),
line!()
);
Err(msg)?; Err(msg)?;
} }
}; };
@ -165,30 +186,33 @@ fn parse_orphaned_vars<'a>(
Ok(()) Ok(())
} }
pub(super) fn parse_signal_tree<'a>( fn parse_scopes_inner<'a>(
word_reader: &mut WordReader, word_reader: &mut WordReader,
parent_scope_idx: Option<ScopeIdx>, parent_scope_idx: Option<ScopeIdx>,
vcd: &'a mut VCD, vcd: &'a mut VCD,
signal_map : &mut HashMap<String, SignalIdx> signal_map: &mut HashMap<String, SignalIdx>,
) -> Result<(), String> { ) -> Result<(), String> {
// $scope module reg_mag_i $end // $scope module reg_mag_i $end
// ^^^^^^ - module keyword // ^^^^^^ - module keyword
let (keyword, cursor) = word_reader.next_word()?; let (keyword, cursor) = next_word!(word_reader)?;
let expected = ["module", "begin", "task", "function"]; let expected = ["module", "begin", "task", "function"];
if expected.contains(&keyword) { if expected.contains(&keyword) {
Ok(()) Ok(())
} else { } else {
let err = format!("Error near {}:{}. \ let err = format!(
"Error near {}:{}. \
found keyword `{keyword}` but expected one of \ found keyword `{keyword}` but expected one of \
{expected:?} on {cursor:?}", file!(), line!()); {expected:?} on {cursor:?}",
file!(),
line!()
);
Err(err) Err(err)
}?; }?;
// $scope module reg_mag_i $end // $scope module reg_mag_i $end
// ^^^^^^^^^ - scope name // ^^^^^^^^^ - scope name
let (scope_name, _) = word_reader.next_word()?; let (scope_name, _) = next_word!(word_reader)?;
let curr_scope_idx = ScopeIdx(vcd.all_scopes.len()); let curr_scope_idx = ScopeIdx(vcd.all_scopes.len());
@ -200,28 +224,24 @@ pub(super) fn parse_signal_tree<'a>(
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);
} }
None => { None => vcd.scope_roots.push(curr_scope_idx),
vcd.scope_roots.push(curr_scope_idx)
}
} }
// add this scope to list of existing scopes // add this scope to list of existing scopes
vcd.all_scopes.push( vcd.all_scopes.push(Scope {
Scope {
name: scope_name.to_string(), name: scope_name.to_string(),
parent_idx: parent_scope_idx, parent_idx: parent_scope_idx,
self_idx: curr_scope_idx, self_idx: curr_scope_idx,
child_signals: vec![], child_signals: vec![],
child_scopes: vec![] child_scopes: vec![],
} });
);
// $scope module reg_mag_i $end // $scope module reg_mag_i $end
// ^^^^ - end keyword // ^^^^ - end keyword
ident(word_reader, "$end")?; ident(word_reader, "$end")?;
loop { loop {
let (word, cursor) = word_reader.next_word()?; let (word, cursor) = next_word!(word_reader)?;
let ParseResult { matched, residual } = tag(word, "$"); let ParseResult { matched, residual } = tag(word, "$");
match matched { match matched {
// we hope that this word starts with a `$` // we hope that this word starts with a `$`
@ -229,35 +249,43 @@ pub(super) fn parse_signal_tree<'a>(
match residual { match residual {
"scope" => { "scope" => {
// recursive - parse inside of current scope tree // recursive - parse inside of current scope tree
parse_signal_tree(word_reader, Some(curr_scope_idx), vcd, signal_map)?; parse_scopes_inner(word_reader, Some(curr_scope_idx), vcd, signal_map)?;
} }
"var" => { "var" => {
parse_var(word_reader, curr_scope_idx, vcd, signal_map)?; parse_var(word_reader, curr_scope_idx, vcd, signal_map)?;
} }
"upscope" => { "upscope" => {
ident(word_reader, "$end")?; ident(word_reader, "$end")?;
break break;
} }
// we ignore comments // we ignore comments
"comment" => { "comment" => loop {
loop { if ident(word_reader, "$end").is_ok() {
if ident(word_reader, "$end").is_ok() {break} break;
}
} }
},
_ => { _ => {
let err = format!("Error near {}:{}. \ let err = format!(
"Error near {}:{}. \
found keyword `{residual}` but expected \ found keyword `{residual}` but expected \
`$scope`, `$var`, `$comment`, or `$upscope` \ `$scope`, `$var`, `$comment`, or `$upscope` \
on {cursor:?}", file!(), line!()); on {cursor:?}",
return Err(err) file!(),
line!()
);
return Err(err);
} }
} }
} }
_ => { _ => {
let err = format!("Error near {}:{}. \ let err = format!(
"Error near {}:{}. \
found keyword `{matched}` but \ found keyword `{matched}` but \
expected `$` on {cursor:?}", file!(), line!()); expected `$` on {cursor:?}",
return Err(err) file!(),
line!()
);
return Err(err);
} }
} }
} }
@ -268,10 +296,10 @@ pub(super) fn parse_signal_tree<'a>(
pub(super) fn parse_scopes<'a>( pub(super) fn parse_scopes<'a>(
word_reader: &mut WordReader, word_reader: &mut WordReader,
vcd: &'a mut VCD, vcd: &'a mut VCD,
signal_map : &mut HashMap<String, SignalIdx> signal_map: &mut HashMap<String, SignalIdx>,
) -> Result<(), String> { ) -> Result<(), String> {
// get the current word // get the current word
let (word, _) = word_reader.curr_word()?; let (word, cursor) = curr_word!(word_reader)?;
// 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" {
@ -279,52 +307,62 @@ pub(super) fn parse_scopes<'a>(
} }
// get the current word // get the current word
let (word, cursor) = word_reader.curr_word()?; let (word, cursor) = curr_word!(word_reader)?;
// the current word should be "scope", as `parse_orphaned_vars`(if it // the current word should be "scope", as `parse_orphaned_vars`(if it
// was called), should have terminated upon encountering "$scope". // was called), should have terminated upon encountering "$scope".
// If `parse_orphaned_vars` was not called, `parse_scopes` should still // If `parse_orphaned_vars` was not called, `parse_scopes` should still
// have only been called if the caller encountered the word "$scope" // have only been called if the caller encountered the word "$scope"
if word != "$scope" { if word != "$scope" {
let msg = format!("Error near {}:{}.\ let msg = format!(
"Error near {}:{}.\
Expected $scope or $var, found \ Expected $scope or $var, found \
{word} at {cursor:?}", file!(), line!()); {word} at {cursor:?}",
return Err(msg) file!(),
line!()
);
return Err(msg);
} }
// now for the interesting part // now for the interesting part
parse_signal_tree(word_reader, None, vcd, signal_map)?; parse_scopes_inner(word_reader, None, vcd, signal_map)?;
// 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 expected_keywords = ["$scope", "$enddefinitions"]; let expected_keywords = ["$scope", "$enddefinitions"];
// there could be multiple signal trees, and unfortunately, we // there could be multiple signal trees, and unfortunately, we
// can't merge the earlier call to `parse_signal_tree` into this loop // can't merge the earlier call to `parse_scopes_inner` into this loop
// because this loop gets a word from `next_word` instead of // because this loop gets a word from `next_word` instead of
// `curr_word()`. // `curr_word()`.
loop { loop {
let (word, cursor) = word_reader.next_word()?; let (word, cursor) = next_word!(word_reader)?;
match word { match word {
"$scope" => { "$scope" => {
parse_signal_tree(word_reader, None, vcd, signal_map)?; parse_scopes_inner(word_reader, None, vcd, signal_map)?;
} }
"$enddefinitions" => { "$enddefinitions" => {
ident(word_reader, "$end")?; ident(word_reader, "$end")?;
break break;
} }
"comment" => { "comment" => {
// although we don't store comments, we still need to advance the // although we don't store comments, we still need to advance the
// word_reader cursor to the end of the comment // word_reader cursor to the end of the comment
loop { loop {
if ident(word_reader, "$end").is_ok() {break} if ident(word_reader, "$end").is_ok() {
break;
}
} }
} }
_ => { _ => {
let err = format!("Error near {}:{} \ let err = format!(
"Error near {}:{} \
found keyword `{word}` but expected one of \ found keyword `{word}` but expected one of \
{expected_keywords:?} on {cursor:?}", file!(), line!()); {expected_keywords:?} on {cursor:?}",
return Err(err) file!(),
line!()
);
return Err(err);
} }
} }
} }

View file

@ -13,12 +13,8 @@ pub(super) struct Line(pub(super) usize);
pub(super) struct Word(pub(super) usize); 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);
#[derive(Debug)]
pub(super) enum FileStatus {
Eof,
}
pub struct WordReader { pub(super) struct WordReader {
reader: io::BufReader<File>, reader: io::BufReader<File>,
eof: bool, eof: bool,
buffers: Vec<String>, buffers: Vec<String>,
@ -40,7 +36,7 @@ impl WordReader {
} }
} }
pub(super) fn next_word(&mut self) -> Result<(&str, Cursor), FileStatus> { pub(super) fn next_word(&mut self) -> Option<(&str, Cursor)> {
// although reaching the eof is not technically an error, in most cases, // although reaching the eof is not technically an error, in most cases,
// we treat it like one in the rest of the codebase. // we treat it like one in the rest of the codebase.
@ -50,7 +46,7 @@ impl WordReader {
self.buffers.clear(); self.buffers.clear();
if self.eof { if self.eof {
return Err(FileStatus::Eof); return None;
} }
let num_buffers = 10; let num_buffers = 10;
@ -81,7 +77,7 @@ impl WordReader {
// if after we've attempted to read in more content from the file, // if after we've attempted to read in more content from the file,
// there are still no words... // there are still no words...
if self.str_slices.is_empty() { if self.str_slices.is_empty() {
return Err(FileStatus::Eof); return None;
} }
// if we make it here, we return the next word // if we make it here, we return the next word
@ -89,57 +85,84 @@ impl WordReader {
let (ptr, len, position) = self.str_slices.pop_front().unwrap(); let (ptr, len, position) = self.str_slices.pop_front().unwrap();
let slice = slice::from_raw_parts(ptr, len); let slice = slice::from_raw_parts(ptr, len);
self.curr_slice = Some((ptr, len, position.clone())); self.curr_slice = Some((ptr, len, position.clone()));
return Ok((str::from_utf8(slice).unwrap(), position)); return Some((str::from_utf8(slice).unwrap(), position));
}; };
} }
pub(super) fn curr_word(&mut self) -> Result<(&str, Cursor), FileStatus> { pub(super) fn curr_word(&mut self) -> Option<(&str, Cursor)> {
match &self.curr_slice { match &self.curr_slice {
Some(slice) => unsafe { Some(slice) => unsafe {
let (ptr, len, position) = slice.clone(); let (ptr, len, position) = slice.clone();
let slice = slice::from_raw_parts(ptr, len); let slice = slice::from_raw_parts(ptr, len);
Ok((str::from_utf8(slice).unwrap(), position)) return Some((str::from_utf8(slice).unwrap(), position));
}, },
None => Err(FileStatus::Eof), None => None,
} }
} }
} }
fn previous_symbol(level: u32) -> Option<BacktraceSymbol> { macro_rules! next_word {
let (trace, curr_file, curr_line) = (Backtrace::new(), file!(), line!()); ($word_reader:ident) => {
let frames = trace.frames(); $word_reader.next_word().ok_or(()).map_err(|_| {
frames format!(
.iter() "Error near {}:{}. Did not expect to reach end of file here.",
.flat_map(BacktraceFrame::symbols) file!(),
.skip_while(|s| { line!()
s.filename() )
.map(|p| !p.ends_with(curr_file))
.unwrap_or(true)
|| s.lineno() != Some(curr_line)
}) })
.nth(1 + level as usize) };
.cloned()
} }
impl From<FileStatus> for String { macro_rules! curr_word {
fn from(f: FileStatus) -> String { ($word_reader:ident) => {
let sym = previous_symbol(1); $word_reader.curr_word().ok_or(()).map_err(|_| {
let filename = sym format!(
.as_ref() "Error near {}:{}. A call to curr_word! shouldn't fail unless next_word has not yet been invoked.",
.and_then(BacktraceSymbol::filename) file!(),
.map_or(None, |path| path.to_str()) line!()
.unwrap_or("(Couldn't determine filename)"); )
let lineno = sym })
.as_ref() };
.and_then(BacktraceSymbol::lineno) }
.map_or(None, |path| Some(path.to_string()))
.unwrap_or("(Couldn't determine line number)".to_string());
match f { pub(super) use curr_word;
FileStatus::Eof => format!( pub(super) use next_word;
"Error near {filename}:{lineno} \
No more words left in vcd file." // fn previous_symbol(level: u32) -> Option<BacktraceSymbol> {
), // let (trace, curr_file, curr_line) = (Backtrace::new(), file!(), line!());
} // let frames = trace.frames();
} // frames
} // .iter()
// .flat_map(BacktraceFrame::symbols)
// .skip_while(|s| {
// s.filename()
// .map(|p| !p.ends_with(curr_file))
// .unwrap_or(true)
// || s.lineno() != Some(curr_line)
// })
// .nth(1 + level as usize)
// .cloned()
// }
// impl From<FileStatus> for String {
// fn from(f: FileStatus) -> String {
// let sym = previous_symbol(1);
// let filename = sym
// .as_ref()
// .and_then(BacktraceSymbol::filename)
// .map_or(None, |path| path.to_str())
// .unwrap_or("(Couldn't determine filename)");
// let lineno = sym
// .as_ref()
// .and_then(BacktraceSymbol::lineno)
// .map_or(None, |path| Some(path.to_string()))
// .unwrap_or("(Couldn't determine line number)".to_string());
// match f {
// FileStatus::Eof => format!(
// "Error near {filename}:{lineno} \
// No more words left in vcd file."
// ),
// }
// }
// }

View file

@ -87,7 +87,13 @@ pub(super) struct Scope {
#[derive(Debug)] #[derive(Debug)]
pub struct VCD { pub struct VCD {
pub(super) metadata: Metadata, pub(super) metadata: Metadata,
// since we only need to store values when there is an actual change
// in the timeline, we keep a vector that stores the time at which an
// event occurs. Time t is always stored as the minimum length sequence
// of u8.
pub timeline: Vec<u8>, pub timeline: Vec<u8>,
// we need to keep track of where a given time t sequence of u8 begins
// and ends in the timeline vector.
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>,

View file

@ -12,7 +12,7 @@ pub(super) enum BinaryParserErrTypes {
} }
// We build a quick and not so dirty bit string parser. // We build a quick and not so dirty bit string parser.
pub(super) fn base2_str_to_byte(word: &[u8]) -> Result<u8, BinaryParserErrTypes> { fn base2_str_to_byte(word: &[u8]) -> Result<u8, BinaryParserErrTypes> {
let mut val = 0u8; let mut val = 0u8;
// shouldn't have more than 8 chars in str // shouldn't have more than 8 chars in str