#substrate #smart-contracts #syntax-tree #polkadot #blockchain #ink #intermediate-representation

ink-analyzer-ir

Intermediate representations (IRs) and abstractions of ink! smart contract code for ink! analyzer

26 releases (12 breaking)

0.13.0 Mar 16, 2024
0.12.0 Jan 25, 2024
0.11.2 Dec 24, 2023
0.10.4 Nov 2, 2023
0.7.1 Jul 8, 2023

#27 in #ink

Download history 15/week @ 2023-12-23 21/week @ 2023-12-30 23/week @ 2024-01-13 13/week @ 2024-01-20 1/week @ 2024-01-27 5/week @ 2024-02-03 129/week @ 2024-02-17 64/week @ 2024-02-24 4/week @ 2024-03-02 13/week @ 2024-03-09 173/week @ 2024-03-16 7/week @ 2024-03-23 57/week @ 2024-03-30 11/week @ 2024-04-06

253 downloads per month
Used in 2 crates (via ink-analyzer)

MIT/Apache

315KB
7K SLoC

ink! Analyzer IR

ink! intermediate representations (IRs) and abstractions for ink! analyzer.

This library implements types and abstractions for all ink! entities (e.g contracts, storage, events, topics, impls, constructors, messages, selectors, tests, trait definitions, chain extensions, storage items e.t.c).

It uses rust-analyzer's ra_ap_syntax crate for generating the syntax tree of the ink! smart contract code that it then converts into ink! entity intermediate representations and abstractions.

It uses ra_ap_syntax instead of other Rust parsing and syntax tree libraries because ink! analyzer has similar design goals to rust-analyzer. The most important being that parsing should be:

  • resilient (even if the input is invalid, parser tries to see as much syntax tree fragments in the input as it can).
  • lossless (even if the input is invalid, the tree produced by the parser represents it exactly).

It's the main dependency for the semantic analyzer crate.

Installation

Run the following Cargo command in your project directory

cargo add ink-analyzer-ir

Usage

Example:

Generate an IR of ink! smart contract code.

use ink_analyzer_ir::InkFile;

fn generate_ir() {
    let file = InkFile::parse(r#"
        #[ink::contract]
        mod my_contract {

            #[ink(storage)]
            pub struct MyContract {
                value: bool,
            }

            // --snip--
        }
    "#);
    dbg!(&file);

    let contracts = file.contracts();
    dbg!(&contracts);

    if let Some(contract) = contracts.first() {
        let storage = contract.storage();
        dbg!(&storage);
    }
}

Documentation

https://docs.rs/ink-analyzer-ir/latest/ink_analyzer_ir/

Or you can access documentation locally by running the following command from the project root

cargo doc -p ink-analyzer-ir --open

Testing

You can run unit tests for all the core functionality by running the following command from the project root

cargo test -p ink-analyzer-ir

License

Licensed under either MIT or Apache-2.0 license at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~5–15MB
~173K SLoC