fix unwrap none bug in sv-parser

This commit is contained in:
light-ly 2024-09-23 20:01:00 +08:00
parent 8ee9a361f7
commit 15bf62bc5b
4 changed files with 49 additions and 13 deletions

View File

@ -2,6 +2,7 @@ use std::fs::File;
use std::io::BufRead; use std::io::BufRead;
use std::{collections::HashMap, io::BufReader}; use std::{collections::HashMap, io::BufReader};
use std::path::PathBuf; use std::path::PathBuf;
use anyhow::Error;
use sv_parser::{parse_sv, unwrap_node, ConstantMintypmaxExpression, GateInstantiation, ListOfParameterAssignments, ListOfPortConnections, Locate, PackedDimensionRange, RefNode, SyntaxTree}; use sv_parser::{parse_sv, unwrap_node, ConstantMintypmaxExpression, GateInstantiation, ListOfParameterAssignments, ListOfPortConnections, Locate, PackedDimensionRange, RefNode, SyntaxTree};
use super::fast_hdlparam::{FastHdlparam, Macro}; use super::fast_hdlparam::{FastHdlparam, Macro};
@ -16,14 +17,15 @@ pub fn sv_parser(path: &str) -> Option<FastHdlparam> {
let result = parse_sv(&path, &defines, &includes, false, true); let result = parse_sv(&path, &defines, &includes, false, true);
if let Ok((syntax_tree, _)) = result { if let Ok((syntax_tree, _)) = result {
let hdlparam = make_fast_from_syntaxtree(&syntax_tree, &path); if let Ok(hdlparam) = make_fast_from_syntaxtree(&syntax_tree, &path) {
return Some(hdlparam); return Some(hdlparam);
}
} }
None None
} }
pub fn make_fast_from_syntaxtree(syntax_tree: &SyntaxTree, path: &PathBuf) -> FastHdlparam { pub fn make_fast_from_syntaxtree(syntax_tree: &SyntaxTree, path: &PathBuf) -> Result<FastHdlparam, Error> {
let mut hdlparam = FastHdlparam { let mut hdlparam = FastHdlparam {
fast_macro: Macro { fast_macro: Macro {
defines: Vec::new(), defines: Vec::new(),
@ -96,8 +98,13 @@ pub fn make_fast_from_syntaxtree(syntax_tree: &SyntaxTree, path: &PathBuf) -> Fa
let (mut end_line, mut end_character) = (id.line, start_character + id.len as u32); let (mut end_line, mut end_character) = (id.line, start_character + id.len as u32);
let net_type = match unwrap_node!(param_node, DataType) { let net_type = match unwrap_node!(param_node, DataType) {
Some(RefNode::DataType(data_type)) => { Some(RefNode::DataType(data_type)) => {
let id = get_identifier(unwrap_node!(data_type, SimpleIdentifier, Keyword).unwrap()).unwrap(); let id = unwrap_node!(data_type, SimpleIdentifier, Keyword);
syntax_tree.get_str(&id).unwrap() if id == None {
"wire"
} else {
let id = get_identifier(id.unwrap()).unwrap();
syntax_tree.get_str(&id).unwrap()
}
} }
_ => "wire" _ => "wire"
}; };
@ -129,8 +136,12 @@ pub fn make_fast_from_syntaxtree(syntax_tree: &SyntaxTree, path: &PathBuf) -> Fa
let net_type = match unwrap_node!(x, DataType, ImplicitDataType) { let net_type = match unwrap_node!(x, DataType, ImplicitDataType) {
Some(RefNode::DataType(x)) => { Some(RefNode::DataType(x)) => {
let id = unwrap_node!(x, Keyword).unwrap(); let id = unwrap_node!(x, Keyword);
syntax_tree.get_str(&get_identifier(id).unwrap()).unwrap() if id != None {
syntax_tree.get_str(&get_identifier(id.unwrap()).unwrap()).unwrap()
} else {
"unknown"
}
}, },
Some(RefNode::ImplicitDataType(_)) => "wire", Some(RefNode::ImplicitDataType(_)) => "wire",
_ => "unknown" _ => "unknown"
@ -180,8 +191,12 @@ pub fn make_fast_from_syntaxtree(syntax_tree: &SyntaxTree, path: &PathBuf) -> Fa
} else { } else {
match unwrap_node!(x, DataType, ImplicitDataType) { match unwrap_node!(x, DataType, ImplicitDataType) {
Some(RefNode::DataType(x)) => { Some(RefNode::DataType(x)) => {
let id = unwrap_node!(x, Keyword).unwrap(); let id = unwrap_node!(x, Keyword);
syntax_tree.get_str(&get_identifier(id).unwrap()).unwrap() if id != None {
syntax_tree.get_str(&get_identifier(id.unwrap()).unwrap()).unwrap()
} else {
"unknown"
}
}, },
Some(RefNode::ImplicitDataType(_)) => "wire", Some(RefNode::ImplicitDataType(_)) => "wire",
_ => "unknown" _ => "unknown"
@ -276,7 +291,7 @@ pub fn make_fast_from_syntaxtree(syntax_tree: &SyntaxTree, path: &PathBuf) -> Fa
} }
} }
hdlparam Ok(hdlparam)
} }
fn get_inst_param_last_locate(x: &ListOfParameterAssignments) -> Option<(Locate, Locate)> { fn get_inst_param_last_locate(x: &ListOfParameterAssignments) -> Option<(Locate, Locate)> {

View File

@ -114,8 +114,9 @@ pub fn do_fast(path: String) -> Result<FastHdlparam> {
); );
if let Some(syntax_tree) = parse_result { if let Some(syntax_tree) = parse_result {
let hdlparam = make_fast_from_syntaxtree(&syntax_tree, &path_buf); if let Ok(hdlparam) = make_fast_from_syntaxtree(&syntax_tree, &path_buf) {
return Ok(hdlparam); return Ok(hdlparam);
}
} }
let api_error = tower_lsp::jsonrpc::Error { let api_error = tower_lsp::jsonrpc::Error {

View File

@ -239,7 +239,19 @@ impl Sources {
// 计算 fast // 计算 fast
if let Some(syntax_tree) = &syntax_tree { if let Some(syntax_tree) = &syntax_tree {
let path = PathBuf::from(uri.path().to_string()); let path = PathBuf::from(uri.path().to_string());
let fast = make_fast_from_syntaxtree(&syntax_tree, &path); let fast = if let Ok(fast) = make_fast_from_syntaxtree(&syntax_tree, &path) {
fast
} else {
crate::core::fast_hdlparam::FastHdlparam {
fast_macro: crate::core::fast_hdlparam::Macro {
defines: Vec::new(),
errors: Vec::new(),
includes: Vec::new(),
invalid: Vec::new()
},
content: Vec::new()
}
};
update_fast_to_client(backend.clone(), fast); update_fast_to_client(backend.clone(), fast);
} }

View File

@ -0,0 +1,8 @@
module ibex_alu #(
parameter ibex_pkg::rv32b_e RV32B = ibex_pkg::RV32BNone
) (
input ibex_pkg::alu_op_e operator_i,
input logic[31:0] operand_a_i,
input logic[31:0] operand_b_i
);
endmodule