38 releases

new 0.8.12 Nov 12, 2024
0.8.10 Oct 28, 2024
0.7.7 Jul 8, 2024
0.7.0 Mar 30, 2024
0.3.1 Jul 30, 2023

#707 in Magic Beans

Download history 54140/week @ 2024-07-30 67882/week @ 2024-08-06 70373/week @ 2024-08-13 60432/week @ 2024-08-20 73062/week @ 2024-08-27 83473/week @ 2024-09-03 71746/week @ 2024-09-10 70931/week @ 2024-09-17 77854/week @ 2024-09-24 85989/week @ 2024-10-01 104774/week @ 2024-10-08 97634/week @ 2024-10-15 98441/week @ 2024-10-22 88502/week @ 2024-10-29 96830/week @ 2024-11-05 86339/week @ 2024-11-12

393,253 downloads per month
Used in 209 crates (4 directly)

MIT/Apache

315KB
9K SLoC

syn-solidity

syn-powered parser for Solidity-like TokenStreams.

The parsed root element is the File, which contains a list of Items. Items also support outer attributes, as shown below.

Design

This parser is specifically designed for Rust procedural macros. It aims to mimic the behavior of the official Solidity compiler (Solc) when it comes to parsing valid Solidity code. This means that all valid Solidity code, as recognized by Solc v0.5.*[^1] and above, will also be recognized and parsed correctly by syn-solidity.

However, syn-solidity is more permissive and lenient compared to the official Solidity compiler and grammar specifications. Some examples of code patterns that are valid in syn-solidity but not in the official compiler include:

  • identifiers are Rust identifiers (syn::Ident), and as such cannot contain the dollar sign ($), but can contain unicode characters
  • trailing punctuation, like commas (,) in function arguments or enums definitions
  • certain variable and function attributes in certain contexts, like internal functions or functions with implementations ({ ... }) in interfaces
  • parameter storage locations in item definitions, like uint256[] memory in a struct or error definition
  • the tuple type (T, U, ..) is allowed wherever a type is expected, and can optionally be preceded by the tuple keyword. This is the same as ethers.js's Human-Readable ABI

This lenient behavior is intentionally designed to facilitate usage within procedural macros, and to reduce general code complexity in the parser and AST.

[^1]: Older versions may still parse successfully, but this is not guaranteed.

Known limitations

This parser is limited to only valid Rust tokens, meaning that certain Solidity constructs are not supported. Some examples include, but are not limited to:

  • dollar signs ($) inside of identifiers
  • single quote strings
  • hex and unicode string literal prefixes. Literal prefixes are reserved in Rust edition 2021 and above.
  • "\uXXXX" unicode escapes. Rust uses "\u{XXXX}" for unicode codepoints
  • invalid nested block comments. For example, /*/*/ does not parse.

For the most part, you can copy-paste Solidity code and expect it to parse correctly most of the time. You can see a few examples of Solidity code that parses correctly (after some very light patching) in the tests directory.

Examples

Basic usage:

use quote::quote;
use syn_solidity::{Expr, File, Item, Lit, Stmt};

// Create a Solidity `TokenStream`
let tokens = quote! {
    /// @name HelloWorld
    /// @notice A hello world example in Solidity.
    contract HelloWorld {
        /// @notice Returns the string "Hello, World!".
        function helloWorld() external pure returns (string memory) {
            return "Hello, World!";
        }
    }
};

// Parse the tokens into a `File`
let ast: File = syn_solidity::parse2(tokens)?;

let items: &[Item] = &ast.items;
let Some(Item::Contract(contract)) = items.first() else {
    unreachable!()
};
assert_eq!(contract.name, "HelloWorld");
assert_eq!(contract.attrs.len(), 2); // doc comments

let body: &[Item] = &contract.body;
let Some(Item::Function(function)) = body.first() else {
    unreachable!()
};
assert_eq!(function.attrs.len(), 1); // doc comment
assert_eq!(function.name.as_ref().unwrap(), "helloWorld");
assert!(function.parameters.is_empty()); // ()
assert_eq!(function.attributes.len(), 2); // external pure
assert!(function.returns.is_some());

let Some([Stmt::Return(ret)]) = function.body() else {
    unreachable!()
};
let Some(Expr::Lit(Lit::Str(s))) = &ret.expr else {
    unreachable!()
};
assert_eq!(s.value(), "Hello, World!");
# syn::Result::Ok(())

Dependencies

~215–660KB
~16K SLoC