don't use #function in errors

This commit is contained in:
Yehowshua Immanuel 2022-08-04 11:36:26 -04:00
parent 21f0682500
commit fa25bad391
3 changed files with 25 additions and 37 deletions

View file

@ -9,5 +9,4 @@ edition = "2021"
num = "0.4" num = "0.4"
clap = { version = "3.1.8", features = ["derive"] } clap = { version = "3.1.8", features = ["derive"] }
chrono = "0.4" chrono = "0.4"
function_name = "0.3.0"
itertools = "0.10.3" itertools = "0.10.3"

View file

@ -1,10 +1,8 @@
use chrono::prelude::*; use chrono::prelude::*;
use itertools::Itertools; use itertools::Itertools;
use function_name::named;
use super::*; use super::*;
#[named]
pub(super) fn parse_date( pub(super) fn parse_date(
word_and_ctx1 : (&str, &Cursor), word_and_ctx1 : (&str, &Cursor),
word_and_ctx2 : (&str, &Cursor), word_and_ctx2 : (&str, &Cursor),
@ -19,7 +17,8 @@ pub(super) fn parse_date(
let days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]; let days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
if !days.contains(&word) { if !days.contains(&word) {
let msg = format!("reached end of file without parser leaving {}\n", function_name!()); let msg = format!("Error near {}:{}. Reached end of file without \
terminating parser", 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))
@ -39,7 +38,8 @@ pub(super) fn parse_date(
]; ];
if !months.contains(&word) { if !months.contains(&word) {
let msg = format!("reached end of file without parser leaving {}\n", function_name!()); let msg = format!("Error near {}:{}. Reached end of file without \
terminating parser", 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))
@ -58,7 +58,8 @@ pub(super) fn parse_date(
}; };
if date > 31 { if date > 31 {
let msg = format!("reached end of file without parser leaving {}\n", function_name!()); let msg = format!("Error near {}:{}. Reached end of file without \
terminating parser", 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))
@ -79,7 +80,7 @@ pub(super) fn parse_date(
.map_err(|_| "failed to parse".to_string())?; .map_err(|_| "failed to parse".to_string())?;
if hh > 23 { if hh > 23 {
let msg = format!("reached end of file without parser leaving {}\n", function_name!()); 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))
@ -94,7 +95,7 @@ pub(super) fn parse_date(
.map_err(|_| "failed to parse".to_string())?; .map_err(|_| "failed to parse".to_string())?;
if mm > 60 { if mm > 60 {
let msg = format!("reached end of file without parser leaving {}\n", function_name!()); 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))
@ -109,7 +110,7 @@ pub(super) fn parse_date(
.map_err(|_| "failed to parse".to_string())?; .map_err(|_| "failed to parse".to_string())?;
if ss > 60 { if ss > 60 {
let msg = format!("reached end of file without parser leaving {}\n", function_name!()); 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))
@ -135,7 +136,6 @@ pub(super) fn parse_date(
} }
#[named]
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();
@ -144,7 +144,9 @@ pub(super) fn parse_version(word_reader : &mut WordReader) -> Result<Version, St
// if there isn't another word left in the file, then we exit // if there isn't another word left in the file, then we exit
if word.is_none() { if word.is_none() {
return Err(format!("reached end of file without parser leaving {}", function_name!())) let msg = format!("Error near {}:{}. Reached end of file without \
terminating parser", file!(), line!());
return Err(msg)
} }
let (word, _) = word.unwrap(); let (word, _) = word.unwrap();
@ -162,9 +164,9 @@ pub(super) fn parse_version(word_reader : &mut WordReader) -> Result<Version, St
} }
} }
#[named]
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> {
let err_msg = format!("failed in {}", function_name!()); let err_msg = format!("Error near {}:{}. No more words left in vcd file.",
file!(), line!());
// we might see `scalarunit $end` or `scalar unit $end` // we might see `scalarunit $end` or `scalar unit $end`
@ -214,9 +216,9 @@ pub(super) fn parse_timescale(word_reader : &mut WordReader) -> Result<(Option<u
} }
#[named]
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 err_msg = format!("reached end of file without parser leaving {}", function_name!()); let err_msg = format!("Error near {}:{}. No more words left in vcd file.",
file!(), line!());
let mut metadata = Metadata { let mut metadata = Metadata {
date : None, date : None,
@ -234,7 +236,6 @@ pub(super) fn parse_metadata(word_reader : &mut WordReader) -> Result<Metadata,
"$" => { "$" => {
match residual { match residual {
"date" => { "date" => {
let err_msg = format!("reached end of file without parser leaving {}", function_name!());
// a date is typically composed of the 5 following words which can // a date is typically composed of the 5 following words which can
// occur in any order: // occur in any order:
// {Day, Month, Date(number in month), hh:mm:ss, year}. // {Day, Month, Date(number in month), hh:mm:ss, year}.

View file

@ -1,17 +1,14 @@
//! 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::*;
#[named]
pub(super) fn parse_var<'a>( 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 err = format!("reached end of file without parser leaving {}", function_name!()); let err = format!("Error near {}:{}. No more words left in vcd file.", file!(), line!());
let (word, cursor) = word_reader.next_word().ok_or(&err)?; let (word, cursor) = word_reader.next_word().ok_or(&err)?;
let expected_types = ["integer", "parameter", "real", "reg", "string", "wire", "tri1", "time"]; let expected_types = ["integer", "parameter", "real", "reg", "string", "wire", "tri1", "time"];
@ -53,7 +50,6 @@ pub(super) fn parse_var<'a>(
// ^ - signal_alias // ^ - signal_alias
let (word, _) = 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);
// $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)
@ -153,10 +149,8 @@ fn parse_orphaned_vars<'a>(
// we shouldn't reach the end of the file here... // we shouldn't reach the end of the file here...
if next_word.is_none() { if next_word.is_none() {
let (f, l )= (file!(), line!()); let err = format!("Error near {}:{}. No more words left in vcd file.", file!(), line!());
let msg = format!("Error near {f}:{l}.\ Err(err)?;
Reached end of file without terminating parser");
Err(msg)?;
}; };
let (word, cursor) = next_word.unwrap(); let (word, cursor) = next_word.unwrap();
@ -178,7 +172,6 @@ fn parse_orphaned_vars<'a>(
Ok(()) Ok(())
} }
#[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<ScopeIdx>, parent_scope_idx : Option<ScopeIdx>,
@ -188,7 +181,7 @@ pub(super) fn parse_signal_tree<'a>(
// $scope module reg_mag_i $end // $scope module reg_mag_i $end
// ^^^^^^ - module keyword // ^^^^^^ - module keyword
let err = format!("reached end of file without parser leaving {}", function_name!()); let err = format!("Error near {}:{}. No more words left in vcd file.", file!(), line!());
let (keyword, cursor) = word_reader.next_word().ok_or(&err)?; let (keyword, cursor) = word_reader.next_word().ok_or(&err)?;
let expected = ["module", "begin", "task", "function"]; let expected = ["module", "begin", "task", "function"];
@ -233,7 +226,6 @@ pub(super) fn parse_signal_tree<'a>(
// ^^^^ - end keyword // ^^^^ - end keyword
ident(word_reader, "$end")?; ident(word_reader, "$end")?;
let err = format!("reached end of file without parser leaving {}", function_name!());
loop { loop {
let (word, cursor) = word_reader.next_word().ok_or(&err)?; let (word, cursor) = word_reader.next_word().ok_or(&err)?;
let ParseResult{matched, residual} = tag(word, "$"); let ParseResult{matched, residual} = tag(word, "$");
@ -274,16 +266,14 @@ pub(super) fn parse_signal_tree<'a>(
Ok(()) Ok(())
} }
#[named]
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 (f, l ) = (file!(), line!()); let err = format!("Error near {}:{}. No more words left in vcd file.", file!(), line!());
let msg = format!("Error near {f}:{l}. Current word empty!"); let (word, _) = word_reader.curr_word().ok_or(&err)?;
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" {
@ -291,9 +281,7 @@ pub(super) fn parse_scopes<'a>(
} }
// get the current word // get the current word
let (f, l ) = (file!(), line!()); let (word, cursor) = word_reader.curr_word().ok_or(&err)?;
let msg = format!("Error near {f}:{l}. Current word empty!");
let (word, cursor) = word_reader.curr_word().ok_or(msg)?;
// 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".
@ -302,14 +290,14 @@ pub(super) fn parse_scopes<'a>(
if word != "$scope" { if word != "$scope" {
let (f, l )= (file!(), line!()); let (f, l )= (file!(), line!());
let msg = format!("Error near {f}:{l}.\ let msg = format!("Error near {f}:{l}.\
Expected $scope or $var, found {word} at {cursor:?}"); Expected $scope or $var, found {word} at {cursor:?}");
return Err(msg) return Err(msg)
} }
// now for the interesting part // now for the interesting part
parse_signal_tree(word_reader, None, vcd, signal_map)?; parse_signal_tree(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