forked from NomicFoundation/slang
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: extract bindings and graph renderers into modules
- Loading branch information
Showing
4 changed files
with
184 additions
and
175 deletions.
There are no files selected for viewing
94 changes: 94 additions & 0 deletions
94
crates/solidity/outputs/cargo/tests/src/bindings_output/graph.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
use std::fmt; | ||
|
||
use anyhow::Result; | ||
use metaslang_bindings::builder::{self, ROOT_NODE_VAR}; | ||
use metaslang_graph_builder::ast::File; | ||
use metaslang_graph_builder::graph::{Graph, Value}; | ||
use metaslang_graph_builder::{ExecutionConfig, NoCancellation, Variables}; | ||
use semver::Version; | ||
use slang_solidity::bindings; | ||
use slang_solidity::cst::KindTypes; | ||
use slang_solidity::parse_output::ParseOutput; | ||
|
||
const VARIABLE_DEBUG_ATTR: &str = "__variable"; | ||
const LOCATION_DEBUG_ATTR: &str = "__location"; | ||
const MATCH_DEBUG_ATTR: &str = "__match"; | ||
|
||
pub(crate) fn render_graph(version: &Version, parse_output: &ParseOutput) -> Result<String> { | ||
let graph_builder = File::from_str(bindings::get_binding_rules())?; | ||
|
||
let tree = parse_output.create_tree_cursor(); | ||
let mut graph = Graph::new(); | ||
let root_node = graph.add_graph_node(); | ||
graph[root_node] | ||
.attributes | ||
.add(VARIABLE_DEBUG_ATTR.into(), ROOT_NODE_VAR.to_string()) | ||
.unwrap(); | ||
|
||
let functions = builder::default_functions(version.clone()); | ||
let mut variables = Variables::new(); | ||
variables.add(ROOT_NODE_VAR.into(), root_node.into())?; | ||
let execution_config = ExecutionConfig::new(&functions, &variables).debug_attributes( | ||
LOCATION_DEBUG_ATTR.into(), | ||
VARIABLE_DEBUG_ATTR.into(), | ||
MATCH_DEBUG_ATTR.into(), | ||
); | ||
|
||
graph_builder.execute_into(&mut graph, &tree, &execution_config, &NoCancellation)?; | ||
|
||
let note = if parse_output.is_valid() { | ||
"" | ||
} else { | ||
"%% WARNING: Parsing failed, graph may be incomplete\n" | ||
}; | ||
Ok(format!( | ||
"{note}{graph}", | ||
graph = print_graph_as_mermaid(&graph) | ||
)) | ||
} | ||
|
||
fn print_graph_as_mermaid(graph: &Graph<KindTypes>) -> impl fmt::Display + '_ { | ||
struct DisplayGraph<'a>(&'a Graph<KindTypes>); | ||
|
||
impl<'a> fmt::Display for DisplayGraph<'a> { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
let graph = self.0; | ||
writeln!(f, "graph TD")?; | ||
for node in graph.iter_nodes() { | ||
let gn = &graph[node]; | ||
let node_label = if let Some(symbol) = gn.attributes.get("symbol") { | ||
symbol.to_string() | ||
} else { | ||
format!("{}", node.index()) | ||
}; | ||
let source = gn | ||
.attributes | ||
.get(MATCH_DEBUG_ATTR) | ||
.and_then(|source| source.as_syntax_node_ref().ok()); | ||
let location = gn.attributes.get(LOCATION_DEBUG_ATTR); | ||
|
||
let node_label = format!( | ||
"\"`**{node_label}** @{source}\n{variable}\n{location}`\"", | ||
source = source.map(|s| s.location()).unwrap_or_default(), | ||
variable = gn | ||
.attributes | ||
.get(VARIABLE_DEBUG_ATTR) | ||
.unwrap_or(&Value::Null), | ||
location = location.unwrap_or(&Value::Null), | ||
); | ||
let node_type = gn.attributes.get("type").and_then(|x| x.as_str().ok()); | ||
match node_type { | ||
Some("push_symbol") => writeln!(f, "\tN{}[/{}\\]", node.index(), node_label)?, | ||
Some("pop_symbol") => writeln!(f, "\tN{}[\\{}/]", node.index(), node_label)?, | ||
_ => writeln!(f, "\tN{}[{}]", node.index(), node_label)?, | ||
} | ||
for (sink, _edge) in gn.iter_edges() { | ||
writeln!(f, "\tN{} --> N{}", node.index(), sink.index())?; | ||
} | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
DisplayGraph(graph) | ||
} |
2 changes: 2 additions & 0 deletions
2
crates/solidity/outputs/cargo/tests/src/bindings_output/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
mod generated; | ||
mod graph; | ||
mod renderer; | ||
mod runner; |
84 changes: 84 additions & 0 deletions
84
crates/solidity/outputs/cargo/tests/src/bindings_output/renderer.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use std::ops::Range; | ||
use std::path::Path; | ||
|
||
use anyhow::Result; | ||
use ariadne::{Color, Config, Label, Report, ReportBuilder, ReportKind, Source}; | ||
use infra_utils::paths::PathExtensions; | ||
use semver::Version; | ||
use slang_solidity::bindings::{self, Handle}; | ||
use slang_solidity::parse_output::ParseOutput; | ||
|
||
pub(crate) fn render_bindings( | ||
version: &Version, | ||
parse_output: &ParseOutput, | ||
source: &str, | ||
source_path: &Path, | ||
) -> Result<String> { | ||
let mut bindings = bindings::create(version.clone()); | ||
bindings.add_file( | ||
source_path.to_str().unwrap(), | ||
parse_output.create_tree_cursor(), | ||
); | ||
|
||
let source_id = source_path.strip_repo_root()?.unwrap_str(); | ||
let mut builder: ReportBuilder<'_, (&str, Range<usize>)> = Report::build( | ||
ReportKind::Custom("References and definitions", Color::Unset), | ||
source_id, | ||
0, | ||
) | ||
.with_config(Config::default().with_color(false)); | ||
|
||
if !parse_output.is_valid() { | ||
builder = builder.with_note("WARNING: Parsing failed. Results may be incomplete."); | ||
} | ||
|
||
let mut definitions: Vec<Handle<'_>> = Vec::new(); | ||
|
||
for definition in bindings.all_definitions() { | ||
let Some(cursor) = definition.get_cursor() else { | ||
continue; | ||
}; | ||
|
||
let range = { | ||
let range = cursor.text_range(); | ||
let start = source[..range.start.utf8].chars().count(); | ||
let end = source[..range.end.utf8].chars().count(); | ||
start..end | ||
}; | ||
|
||
definitions.push(definition); | ||
let message = format!("def: {}", definitions.len()); | ||
builder = builder.with_label(Label::new((source_id, range)).with_message(message)); | ||
} | ||
|
||
for reference in bindings.all_references() { | ||
let Some(cursor) = reference.get_cursor() else { | ||
continue; | ||
}; | ||
|
||
let range = { | ||
let range = cursor.text_range(); | ||
let start = source[..range.start.utf8].chars().count(); | ||
let end = source[..range.end.utf8].chars().count(); | ||
start..end | ||
}; | ||
|
||
let definition = reference.jump_to_definition(); | ||
let message = match definition { | ||
None => "unresolved".to_string(), | ||
Some(definition) => { | ||
let def_id = definitions.iter().position(|d| *d == definition).unwrap(); | ||
format!("ref: {}", def_id + 1) | ||
} | ||
}; | ||
|
||
builder = builder.with_label(Label::new((source_id, range)).with_message(message)); | ||
} | ||
|
||
let report = builder.finish(); | ||
let mut buffer = Vec::new(); | ||
report.write((source_id, Source::from(source)), &mut buffer)?; | ||
|
||
let result = String::from_utf8(buffer)?; | ||
Ok(result) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters