Expand description
Slang is intended to be a modular Solidity compiler, specifically targeting code analysis and developer tooling. This means servicing tools with domain-specific APIs and, in general, facilitating working with and analyzing the Solidity source code. If you’re in the editor writing Solidity or performing linting or additional validation, there’s a chance that you are, or could be, running Slang!
§Parsing Solidity Source Code
use slang_solidity::parser::Parser;
use slang_solidity::utils::LanguageFacts;
let source = "contract Foo {}";
let parser = Parser::create(LanguageFacts::LATEST_VERSION).unwrap();
let parse_output = parser.parse_file_contents(&source);§Using the Cursor: Listing Contract Names
Cursors are the simplest way to navigate a CST. The below example shows how you might
use cursors to list all of the contract names in a source file.
// Step 1 (not shown): get a parse tree, for instance with the code above.
// Step 2: Get a cursor
let mut cursor = parse_output.create_tree_cursor();
// Step 3: Use the cursor to navigate to all `ContractDefinition`'s and print out their names
while cursor.go_to_next_nonterminal_with_kind(NonterminalKind::ContractDefinition) {
println!("Contract: {}", cursor.node().unparse());
}Modules§
- bindings
- This module contains APIs used to resolve symbol bindings in source code. These APIs can be used for code analysis features such as “Go to Definition” or “Find References”.
- compilation
- The
compilationmodule provides some basic utilities that users can use to combine multiple source files into a single compilation unit. The compilation unit can then be used to get a completeBindingGraphthat can resolve references and defintions across all the added files. - cst
- This module defines the types that represent a concrete syntax tree (CST), such as
NodeandEdge. It also defines utilities that are used for navigating and searching a CST,CursorandQuery. - parser
- Source code parsing utilities. See
parser::Parserfor more info on parsing source code. - utils
- Various Slang language utilities.