fix primitive bug | add gen primitive bin to copy sh

This commit is contained in:
light-ly 2024-11-12 16:55:11 +08:00
parent 935a852ba0
commit 103baacb8f
2 changed files with 36 additions and 14 deletions

Binary file not shown.

View File

@ -23,15 +23,15 @@ pub struct PrimitiveXml {
pub name_to_template: HashMap<String, Template> pub name_to_template: HashMap<String, Template>
} }
pub fn init_parse_primitive_files() -> Result<PrimitiveXml, Error> { #[allow(unused)]
const XML_DIR: &str = "../../primitive_files"; pub fn init_parse_primitive_files(dir: &str) -> Result<PrimitiveXml, Error> {
let mut primitive_xml = PrimitiveXml::default(); let mut primitive_xml = PrimitiveXml::default();
for entry in fs::read_dir(XML_DIR)? { for entry in fs::read_dir(dir)? {
let entry = entry?; let entry = entry?;
let path = entry.path(); let path = entry.path();
if let Some(ext) = path.extension() { if let Some(ext) = path.extension() {
if ext == "xml" { if ext == "xml" {
println!("parse xml file {}", path.to_str().unwrap());
if let Ok(primitive) = xml_parser(path.to_str().unwrap()) { if let Ok(primitive) = xml_parser(path.to_str().unwrap()) {
primitive_xml.name_to_template.extend( primitive_xml.name_to_template.extend(
primitive.name_to_template primitive.name_to_template
@ -40,7 +40,6 @@ pub fn init_parse_primitive_files() -> Result<PrimitiveXml, Error> {
} }
} }
} }
Ok(primitive_xml) Ok(primitive_xml)
} }
@ -56,10 +55,11 @@ fn xml_parser(path: &str) -> Result<PrimitiveXml, Error> {
match parser.next() { match parser.next() {
Ok(XmlEvent::StartElement { name, .. }) => { Ok(XmlEvent::StartElement { name, .. }) => {
if name.local_name == "Template" { if name.local_name == "Template" {
let (inst_name , template) = xml_parse_template(&mut parser); if let Some((inst_name , template)) = xml_parse_template(&mut parser) {
primitive_xml.name_to_template.insert(inst_name, template); primitive_xml.name_to_template.insert(inst_name, template);
} }
} }
}
Ok(XmlEvent::EndElement { name }) => { Ok(XmlEvent::EndElement { name }) => {
if name.local_name == "RootFolder" { break; } if name.local_name == "RootFolder" { break; }
} }
@ -70,17 +70,19 @@ fn xml_parser(path: &str) -> Result<PrimitiveXml, Error> {
Ok((primitive_xml)) Ok((primitive_xml))
} }
fn xml_parse_template(parser: &mut EventReader<BufReader<File>>) -> (String, Template) { fn xml_parse_template(parser: &mut EventReader<BufReader<File>>) -> Option<(String, Template)> {
loop { loop {
match parser.next() { match parser.next() {
Ok(XmlEvent::Characters(text)) => { Ok(XmlEvent::Characters(text)) => {
if text.contains("Cut code below this line") { if text.contains("Cut code below this line") {
if let Some((name, text, fast)) = xml_parse_text(&text) { if let Some((name, text, fast)) = xml_parse_text(&text) {
return (name, Template { text, fast }); return Some((name, Template { text, fast }));
} else {
return None
} }
} }
} }
_ => () _ => return None
} }
} }
} }
@ -201,15 +203,35 @@ fn xml_parse_text(text: &str) -> Option<(String, String, FastHdlparam)> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::{fs, path::Path}; use std::{fs::{self, File}, io::Write, path::Path};
use super::xml_parser; use super::{init_parse_primitive_files, xml_parser};
const TESTFILE: &str = "D:\\work\\playground\\digital-lsp-server\\primitive_files\\verilog1.xml"; const TESTFILE: &str = "D:\\work\\playground\\digital-lsp-server\\primitive_files\\verilog2.xml";
const TESTDIR: &str = "/home/dide/project/Digital-Test/Digital-IDE-test/user/factory/xilinx"; const TESTDIR: &str = "/home/dide/project/Digital-Test/Digital-IDE-test/user/factory/xilinx";
#[test]
fn gen_primitive_bin() {
if let Ok(primitive_info) = init_parse_primitive_files("primitive_files") {
let serialized_data = bincode::serialize(&primitive_info).unwrap();
let path = "target/primitive.bin";
let mut file = File::create(path).unwrap();
let _ = file.write_all(&serialized_data);
} else {
println!("parse primitive error");
}
}
#[test] #[test]
fn test_xml() { fn test_xml() {
let _ = xml_parser(TESTFILE); let res = xml_parser(TESTFILE);
match res {
Ok(r) => {
println!("res len {:#?}", r.name_to_template.len())
}
Err(e) => {
println!("error {:#?}", e)
}
}
} }
#[test] #[test]