Merge pull request #35 from ThePerfectComputer/scopefix

Add workaround for files produced by verilator which have annonymous scopes
This commit is contained in:
Frans Skarman 2023-12-05 13:29:26 +00:00 committed by GitHub
commit 3851c4e06c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -289,87 +289,154 @@ fn parse_scopes_inner<R: std::io::Read>(
// $scope module reg_mag_i $end // $scope module reg_mag_i $end
// ^^^^^^^^^ - scope name // ^^^^^^^^^ - scope name
let (scope_name, _) = next_word!(word_reader)?; let (scope_name, _) = next_word!(word_reader)?;
// In some cases there are VCD files which have scopes without names.
// since these occur in the wild, we'll tolerate them even if it is unclear
// if it is supported or not by the spec.
if scope_name != "$end" {
let mut path = path.clone();
path.push(scope_name.to_string());
let mut path = path.clone(); let curr_scope_idx = ScopeIdx(vcd.all_scopes.len());
path.push(scope_name.to_string());
let curr_scope_idx = ScopeIdx(vcd.all_scopes.len()); // register this scope as a child of the current parent scope
// if there is a parent scope, or else we register this scope as
// register this scope as a child of the current parent scope // root scope
// if there is a parent scope, or else we register this scope as match parent_scope_idx {
// root scope Some(ScopeIdx(parent_scope_idx)) => {
match parent_scope_idx { let parent_scope = vcd.all_scopes.get_mut(parent_scope_idx).unwrap();
Some(ScopeIdx(parent_scope_idx)) => { parent_scope.child_scopes.push(curr_scope_idx);
let parent_scope = vcd.all_scopes.get_mut(parent_scope_idx).unwrap(); }
parent_scope.child_scopes.push(curr_scope_idx); None => vcd.root_scopes.push(curr_scope_idx),
} }
None => vcd.root_scopes.push(curr_scope_idx),
}
// add this scope to list of existing scopes // add this scope to list of existing scopes
vcd.all_scopes.push(Scope { vcd.all_scopes.push(Scope {
name: scope_name.to_string(), name: scope_name.to_string(),
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) = next_word!(word_reader)?; 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 `$`
"$" => { "$" => {
match residual { match residual {
"scope" => { "scope" => {
// recursive - parse inside of current scope tree // recursive - parse inside of current scope tree
parse_scopes_inner( parse_scopes_inner(
word_reader, word_reader,
Some(curr_scope_idx), Some(curr_scope_idx),
vcd, vcd,
signal_map, signal_map,
&path, &path,
)?; )?;
} }
"var" => { "var" => {
parse_var(word_reader, curr_scope_idx, vcd, signal_map, &path)?; parse_var(word_reader, curr_scope_idx, vcd, signal_map, &path)?;
} }
"upscope" => { "upscope" => {
ident(word_reader, "$end")?; ident(word_reader, "$end")?;
break;
}
// we ignore comments
"comment" => loop {
if ident(word_reader, "$end").is_ok() {
break; break;
} }
}, // we ignore comments
_ => { "comment" => loop {
let err = format!( if ident(word_reader, "$end").is_ok() {
"Error near {}:{}. \ break;
found keyword `{residual}` but expected \ }
`$scope`, `$var`, `$comment`, or `$upscope` \ },
on {cursor:?}", _ => {
file!(), let err = format!(
line!() "Error near {}:{}. \
); found keyword `{residual}` but expected \
return Err(err); `$scope`, `$var`, `$comment`, or `$upscope` \
on {cursor:?}",
file!(),
line!()
);
return Err(err);
}
} }
} }
_ => {
let err = format!(
"Error near {}:{}. \
found keyword `{matched}` but \
expected `$` on {cursor:?}",
file!(),
line!()
);
return Err(err);
}
} }
_ => { }
let err = format!( } else {
"Error near {}:{}. \ // We'll be conservative and only allow new scopes in this case, and make the nameless
found keyword `{matched}` but \ // scope completely transparent. I.e.
expected `$` on {cursor:?}", // $scope module a $end
file!(), // $scope module $end
line!() // $scope module b $end
); // ...
return Err(err); // $upscope
// $upscope
// $upscope
// will create `a.b`
loop {
let (word, cursor) = next_word!(word_reader)?;
let ParseResult { matched, residual } = tag(word, "$");
match matched {
// we hope that this word starts with a `$`
"$" => {
match residual {
"scope" => {
// recursive - parse inside of current scope tree
parse_scopes_inner(
word_reader,
parent_scope_idx,
vcd,
signal_map,
&path,
)?;
}
"upscope" => {
ident(word_reader, "$end")?;
break;
}
// we ignore comments
"comment" => loop {
if ident(word_reader, "$end").is_ok() {
break;
}
},
_ => {
let err = format!(
"Error near {}:{}. \
found keyword `{residual}` in annonyoums scope but expected \
`$scope`, `$comment`, or `$upscope` \
on {cursor:?}",
file!(),
line!()
);
return Err(err);
}
}
}
_ => {
let err = format!(
"Error near {}:{}. \
found keyword `{matched}` but \
expected `$` on {cursor:?}",
file!(),
line!()
);
return Err(err);
}
} }
} }
} }