Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 157 additions & 0 deletions fpga_arch_viewer/src/error_report.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
use fpga_arch_parser::FPGAArchParseError;
use std::io::{BufRead, BufReader};

fn get_file_line(file_path: &std::path::Path, line_num: u64) -> Option<String> {
let file = std::fs::File::open(file_path).ok()?;
let reader = BufReader::new(file);
let target = line_num.saturating_sub(1) as usize;
reader.lines().nth(target).and_then(Result::ok)
}

fn format_context_line(line: &str, column: u64) -> String {
let column = column as usize;
let mut result = format!(" {}\n", line);
let offset = column.saturating_sub(1).min(line.len());
result.push_str(&format!(" {}{}", " ".repeat(offset), "^"));
result
}

pub(crate) fn format_parse_error(
error: &FPGAArchParseError,
file_path: Option<&std::path::Path>,
) -> String {
match error {
FPGAArchParseError::ArchFileOpenError(msg) => {
format!("Failed to open architecture file:\n{}", msg)
}
FPGAArchParseError::MissingRequiredTag(tag) => {
format!("Missing required XML tag: {}", tag)
}
FPGAArchParseError::MissingRequiredAttribute(attr, pos) => {
let mut msg = format!(
"Missing required attribute '{}' at line {}, column {}",
attr,
pos.row + 1,
pos.column + 1
);
if let Some(path) = file_path
&& let Some(line) = get_file_line(path, pos.row + 1)
{
msg.push_str("\n\n");
msg.push_str(&format_context_line(&line, pos.column + 1));
}
msg
}
FPGAArchParseError::InvalidTag(tag, pos) => {
let mut msg = format!(
"Invalid or unexpected tag '{}' at line {}, column {}",
tag,
pos.row + 1,
pos.column + 1
);
if let Some(path) = file_path
&& let Some(line) = get_file_line(path, pos.row + 1)
{
msg.push_str("\n\n");
msg.push_str(&format_context_line(&line, pos.column + 1));
}
msg
}
FPGAArchParseError::XMLParseError(msg_text, pos) => {
let mut msg = format!(
"XML parsing error at line {}, column {}:\n{}",
pos.row + 1,
pos.column + 1,
msg_text
);
if let Some(path) = file_path
&& let Some(line) = get_file_line(path, pos.row + 1)
{
msg.push_str("\n\n");
msg.push_str(&format_context_line(&line, pos.column + 1));
}
msg
}
FPGAArchParseError::UnknownAttribute(attr, pos) => {
let mut msg = format!(
"Unknown attribute '{}' at line {}, column {}",
attr,
pos.row + 1,
pos.column + 1
);
if let Some(path) = file_path
&& let Some(line) = get_file_line(path, pos.row + 1)
{
msg.push_str("\n\n");
msg.push_str(&format_context_line(&line, pos.column + 1));
}
msg
}
FPGAArchParseError::DuplicateTag(tag, pos) => {
let mut msg = format!(
"Duplicate tag '{}' at line {}, column {}",
tag,
pos.row + 1,
pos.column + 1
);
if let Some(path) = file_path
&& let Some(line) = get_file_line(path, pos.row + 1)
{
msg.push_str("\n\n");
msg.push_str(&format_context_line(&line, pos.column + 1));
}
msg
}
FPGAArchParseError::DuplicateAttribute(attr, pos) => {
let mut msg = format!(
"Duplicate attribute '{}' at line {}, column {}",
attr,
pos.row + 1,
pos.column + 1
);
if let Some(path) = file_path
&& let Some(line) = get_file_line(path, pos.row + 1)
{
msg.push_str("\n\n");
msg.push_str(&format_context_line(&line, pos.column + 1));
}
msg
}
FPGAArchParseError::UnexpectedEndTag(tag, pos) => {
let mut msg = format!(
"Unexpected end tag '</{}>' at line {}, column {}",
tag,
pos.row + 1,
pos.column + 1
);
if let Some(path) = file_path
&& let Some(line) = get_file_line(path, pos.row + 1)
{
msg.push_str("\n\n");
msg.push_str(&format_context_line(&line, pos.column + 1));
}
msg
}
FPGAArchParseError::AttributeParseError(msg_text, pos) => {
let mut msg = format!(
"Failed to parse attribute at line {}, column {}:\n{}",
pos.row + 1,
pos.column + 1,
msg_text
);
if let Some(path) = file_path
&& let Some(line) = get_file_line(path, pos.row + 1)
{
msg.push_str("\n\n");
msg.push_str(&format_context_line(&line, pos.column + 1));
}
msg
}
FPGAArchParseError::UnexpectedEndOfDocument(msg) => {
format!("Unexpected end of document:\n{}", msg)
}
FPGAArchParseError::PinParsingError(msg) => {
format!("Pin parsing error:\n{}", msg)
}
}
}
45 changes: 43 additions & 2 deletions fpga_arch_viewer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod common_ui;
mod complex_block_view;
mod crr_sb_view;
mod crr_view;
mod error_report;
mod grid;
mod grid_renderer;
mod grid_view;
Expand All @@ -27,6 +28,46 @@ fn main() -> Result<(), eframe::Error> {
// Log to stderr (if you run with `RUST_LOG=debug`).
env_logger::init();

let args: Vec<String> = std::env::args().collect();

if let Some(unknown) = args
.iter()
.skip(1)
.find(|a| a.starts_with('-') && *a != "--parse-only")
{
eprintln!("error: unknown argument: {unknown}");
std::process::exit(1);
}

let parse_only = args.iter().any(|a| a == "--parse-only");
let initial_file = args
.iter()
.skip(1)
.find(|a| !a.starts_with('-'))
.map(std::path::PathBuf::from);

// --parse-only: parse the architecture file and report errors without opening the GUI.
if parse_only {
let Some(file_path) = initial_file.as_deref() else {
eprintln!("error: --parse-only requires a file path");
std::process::exit(1);
};
match fpga_arch_parser::parse(file_path) {
Ok(_) => {
println!("Successfully parsed: {}", file_path.display());
return Ok(());
}
Err(e) => {
eprintln!(
"Parse error in {}:\n{}",
file_path.display(),
error_report::format_parse_error(&e, Some(file_path))
);
std::process::exit(1);
}
}
}

// Load the icon data.
let icon_data =
eframe::icon_data::from_png_bytes(&include_bytes!("../assets/icon-256.png")[..]);
Expand All @@ -41,7 +82,7 @@ fn main() -> Result<(), eframe::Error> {
eframe::run_native(
"FPGA Architecture Visualizer",
options,
Box::new(|_cc| Ok(Box::new(viewer::FpgaViewer::new()))),
Box::new(|_cc| Ok(Box::new(viewer::FpgaViewer::new(initial_file)))),
)
}

Expand Down Expand Up @@ -70,7 +111,7 @@ fn main() {
.start(
canvas,
web_options,
Box::new(|_cc| Ok(Box::new(viewer::FpgaViewer::new()))),
Box::new(|_cc| Ok(Box::new(viewer::FpgaViewer::new(None)))),
)
.await;

Expand Down
Loading
Loading