From 265ff8f87dc7392fdf701f811eb2bf54d7bc6678 Mon Sep 17 00:00:00 2001 From: JSDurand Date: Fri, 3 Feb 2023 10:52:35 +0800 Subject: Finally produced the first correct forest Finally the prototype parser has produced the first correct forest. It is my first time to generate a correct forest, in fact, ever since the beginning of this project. --- graph/src/labelled/binary.rs | 47 ++++++++++++++++++++++++++++++++++++++++++-- graph/src/lib.rs | 6 ++++-- 2 files changed, 49 insertions(+), 4 deletions(-) (limited to 'graph') diff --git a/graph/src/labelled/binary.rs b/graph/src/labelled/binary.rs index 201dda2..4ec7378 100644 --- a/graph/src/labelled/binary.rs +++ b/graph/src/labelled/binary.rs @@ -203,8 +203,51 @@ impl Graph for PLGraph { unimplemented!("use a `PLGBuilderMut` instead") } - fn print_viz(&self, _filename: &str) -> Result<(), std::io::Error> { - todo!() + fn print_viz(&self, filename: &str) -> Result<(), std::io::Error> { + let filename = format!("output/{filename}"); + + let preamble = "digraph nfa { + fontname=\"Helvetica,Arial,sans-serif\" + node [fontname=\"Helvetica,Arial,sans-serif\"] + edge [fontname=\"Helvetica,Arial,sans-serif\"] + rankdir=LR;\n"; + + let mut post = String::new(); + + for node in self.nodes() { + post.push_str(&format!( + " {node} [label = \"{}\"]\n", + match self.vertex_label(node) { + Ok(Some(label)) => { + format!("{label}") + } + _ => { + " ε ".to_owned() + } + } + )); + } + + for (source, target) in self.edges() { + post.push_str(&format!(" {source} -> {target}\n")); + } + + post.push_str("}\n"); + + let result = format!("{preamble}{post}"); + + if std::fs::metadata(&filename).is_ok() { + std::fs::remove_file(&filename)?; + } + + let mut file = std::fs::File::options() + .write(true) + .create(true) + .open(&filename)?; + + use std::io::Write; + + file.write_all(result.as_bytes()) } } diff --git a/graph/src/lib.rs b/graph/src/lib.rs index 2a0c50d..87f39c0 100644 --- a/graph/src/lib.rs +++ b/graph/src/lib.rs @@ -129,8 +129,10 @@ pub trait Graph: Default { let result = format!("{preamble}{post}"); - if std::fs::metadata(filename).is_ok() { - std::fs::remove_file(filename)?; + let filename = format!("output/{filename}"); + + if std::fs::metadata(&filename).is_ok() { + std::fs::remove_file(&filename)?; } let mut file = std::fs::File::options() -- cgit v1.2.3-18-g5258