solc-ast provides rust bindings for the solidity AST and visitors. The visitors were built to be 1-1 compatible with the visitors from solc.
Note: The AST structs are almost entirely from camden-smallwood's solidity-rs. The main deviation is in the Visitor implementation.
Note: AST for inline assembly in incomplete.
|
#[derive(Default, Debug)] |
|
struct FunctionDefinitionCollector { |
|
names: Vec<String>, |
|
} |
|
|
|
impl ASTConstVisitor for FunctionDefinitionCollector { |
|
fn end_visit_function_definition(&mut self, node: &FunctionDefinition) -> Result<()> { |
|
self.names.push(node.name.clone()); |
|
Ok(()) |
|
} |
|
} |
|
|
|
#[test] |
|
fn functions() -> Result<()> { |
|
let source_unit = read_counter()?; |
|
let mut function_definition_collector = FunctionDefinitionCollector::default(); |
|
source_unit.accept(&mut function_definition_collector)?; |
|
assert_eq!( |
|
function_definition_collector.names, |
|
vec![String::from("setNumber"), String::from("increment")] |
|
); |
|
Ok(()) |
|
} |