|
| 1 | +use crate::completeness::Completeness; |
| 2 | +use crate::{Scope, ScopeGraph}; |
| 3 | +use dot::LabelText::LabelStr; |
| 4 | +use dot::{Edges, Id, LabelText, Nodes}; |
| 5 | +use std::borrow::Cow; |
| 6 | +use std::fmt::Debug; |
| 7 | +use std::fs::File; |
| 8 | +use std::io; |
| 9 | +use std::io::Write; |
| 10 | +use std::path::Path; |
| 11 | + |
| 12 | +pub trait RenderScopeData { |
| 13 | + /// Renders a scope (or probably rather, the data in a scope). |
| 14 | + /// Can return None if there's no data to render. |
| 15 | + fn render(&self) -> Option<String>; |
| 16 | + |
| 17 | + /// Returns whether this scope is a definition of some variable |
| 18 | + /// |
| 19 | + /// Defaults to whether the outcome of [`render`](RenderScopeData::render) is Some, |
| 20 | + /// because often non-definition scopes have no data associated with them. |
| 21 | + fn definition(&self) -> bool { |
| 22 | + self.render().is_some() |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +impl<LABEL: Clone + Debug, DATA: RenderScopeData + Clone, CMPL: Completeness<LABEL, DATA>> |
| 27 | + ScopeGraph<'_, LABEL, DATA, CMPL> |
| 28 | +{ |
| 29 | + pub fn render<W: Write>(&self, output: &mut W, name: &str) -> io::Result<()> { |
| 30 | + dot::render(&(self, name), output) |
| 31 | + } |
| 32 | + |
| 33 | + pub fn render_to(&self, path: impl AsRef<Path>) -> io::Result<()> { |
| 34 | + let path = path.as_ref(); |
| 35 | + let mut w = File::create(path)?; |
| 36 | + let name = path |
| 37 | + .file_stem() |
| 38 | + .expect("path must have filename for File::create to work") |
| 39 | + .to_string_lossy(); |
| 40 | + self.render(&mut w, &name) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +impl<'a, LABEL: Clone + Debug, DATA: Clone + RenderScopeData, CMPL: Completeness<LABEL, DATA>> |
| 45 | + dot::Labeller<'a, Scope, (Scope, LABEL, Scope)> for (&ScopeGraph<'_, LABEL, DATA, CMPL>, &str) |
| 46 | +{ |
| 47 | + fn graph_id(&'a self) -> Id<'a> { |
| 48 | + Id::new(self.1).unwrap() |
| 49 | + } |
| 50 | + |
| 51 | + fn node_id(&'a self, n: &Scope) -> Id<'a> { |
| 52 | + Id::new(format!("s{}", n.0)).unwrap() |
| 53 | + } |
| 54 | + |
| 55 | + fn node_label(&self, n: &Scope) -> LabelText { |
| 56 | + let data = self.0.get_data(*n); |
| 57 | + LabelText::label(data.render().unwrap_or_else(|| format!("scope {}", n.0))) |
| 58 | + } |
| 59 | + fn edge_label(&self, edge: &(Scope, LABEL, Scope)) -> LabelText { |
| 60 | + LabelText::label(format!("{:?}", edge.1)) |
| 61 | + } |
| 62 | + |
| 63 | + fn node_shape(&'a self, node: &Scope) -> Option<LabelText<'a>> { |
| 64 | + let data = self.0.get_data(*node); |
| 65 | + if data.definition() { |
| 66 | + Some(LabelStr(Cow::Borrowed("box"))) |
| 67 | + } else { |
| 68 | + Some(LabelStr(Cow::Borrowed("circle"))) |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +impl<'a, LABEL: Clone, DATA: Clone, CMPL> dot::GraphWalk<'a, Scope, (Scope, LABEL, Scope)> |
| 74 | + for (&ScopeGraph<'_, LABEL, DATA, CMPL>, &str) |
| 75 | +{ |
| 76 | + fn nodes(&'a self) -> Nodes<'a, Scope> { |
| 77 | + (0..self.0.inner_scope_graph.data.borrow().len()) |
| 78 | + .map(Scope) |
| 79 | + .collect() |
| 80 | + } |
| 81 | + |
| 82 | + fn edges(&'a self) -> Edges<'a, (Scope, LABEL, Scope)> { |
| 83 | + self.0 |
| 84 | + .inner_scope_graph |
| 85 | + .edges |
| 86 | + .borrow() |
| 87 | + .iter() |
| 88 | + .enumerate() |
| 89 | + .flat_map(|(scope, edges)| { |
| 90 | + (*edges) |
| 91 | + .borrow() |
| 92 | + .iter() |
| 93 | + .flat_map(|(lbl, edges_with_lbl)| { |
| 94 | + edges_with_lbl |
| 95 | + .iter() |
| 96 | + .map(|edge| (Scope(scope), lbl.clone(), *edge)) |
| 97 | + .collect::<Vec<_>>() |
| 98 | + }) |
| 99 | + .collect::<Vec<_>>() |
| 100 | + }) |
| 101 | + .collect() |
| 102 | + } |
| 103 | + |
| 104 | + fn source(&'a self, edge: &(Scope, LABEL, Scope)) -> Scope { |
| 105 | + edge.0 |
| 106 | + } |
| 107 | + |
| 108 | + fn target(&'a self, edge: &(Scope, LABEL, Scope)) -> Scope { |
| 109 | + edge.2 |
| 110 | + } |
| 111 | +} |
0 commit comments