summaryrefslogtreecommitdiff
path: root/graph
diff options
context:
space:
mode:
authorJSDurand <mmemmew@gmail.com>2023-02-03 10:52:35 +0800
committerJSDurand <mmemmew@gmail.com>2023-02-03 10:52:35 +0800
commit265ff8f87dc7392fdf701f811eb2bf54d7bc6678 (patch)
tree35538c8ac7524e0d9f2acff8be21b72994728bc4 /graph
parentf28155105134b90fd86049c65478d307e0d8dbbc (diff)
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.
Diffstat (limited to 'graph')
-rw-r--r--graph/src/labelled/binary.rs47
-rw-r--r--graph/src/lib.rs6
2 files changed, 49 insertions, 4 deletions
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<T: GraphLabel> Graph for PLGraph<T> {
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()