Slang: building Solidity Compiler APIs
from a declarative language specification
Solidity Summit - Istanbul - November 2023
Nomic Foundation
A non-profit dedicated to Ethereum developers. We provide open-source software to empower developers today and ensure Ethereum's continued success in the future.
Hardhat
Slang
EDR
Build, Test, Deploy
Analyze
Execute
Agenda
Evolution of Solidity over the years
Evolution of Solidity over the years
Which of these are valid in a top-level position in a source file?
contract
library
interface
struct
enum
function
constant
error
type
using
event
Evolution of Solidity over the years
Which of these are valid in a top-level position in a source file?
contract
library
interface
struct
enum
function
constant
error
type
using
event
Evolution of Solidity over the years
Which of these are valid in a top-level position in a source file?
contract
library
interface
struct
enum
function
constant
error
type
using
event
Evolution of Solidity over the years
Which of these are valid in a top-level position in a source file?
contract
library
interface
struct
enum
function
constant
error
type
using
event
Evolution of Solidity over the years
Which of these are valid in a top-level position in a source file?
contract
library
interface
struct
enum
function
constant
error
type
using
event
Evolution of Solidity over the years
Which of these are valid in a top-level position in a source file?
contract
library
interface
struct
enum
function
constant
error
type
using
event
Evolution of Solidity over the years
Which of these are valid in a top-level position in a source file?
contract
library
interface
struct
enum
function
constant
error
type
using
event
Evolution of Solidity over the years
Which of these are valid in a top-level position in a source file?
contract
library
interface
struct
enum
function
constant
error
type
using
event
Evolution of Solidity over the years
Which of these are valid in a top-level position in a source file?
contract
library
interface
struct
enum
function
constant
error
type
using
event
Evolution of Solidity over the years
What is the result of calling a function on another contract?
someContract.call("f");
Evolution of Solidity over the years
What is the result of calling a function on another contract?
someContract.call("f");
bool success = someContract.call("f");
(bool success, bytes memory data) = someContract.call("f");
Given the following function:
function two_power(uint8 exponent) returns (uint256 result) {
return 2 ** exponent;
}
What is the result of calling two_power(16)?
Evolution of Solidity over the years
Given the following function:
function two_power(uint8 exponent) returns (uint256 result) {
return 2 ** exponent;
}
What is the result of calling two_power(16)?
Evolution of Solidity over the years
Impact on the Ecosystem
Introducing: Slang’s Solidity Language Specification
Slang’s Solidity Language Specification
Slang’s Solidity Language Specification
Introducing: Slang’s Modular Solidity Compiler APIs
Slang’s Modular Solidity Compiler APIs
Building on top of the specification:
Our Alpha release is out:
Slang’s Modular Solidity Compiler APIs
Currently working on our Beta release:
And last but not least, working with language tooling authors to integrate Slang, and build our roadmap based on user feedback.
Slang’s Modular Solidity Compiler APIs
Using Slang: Analyzing Solidity Contracts
1 import assert from "assert";
2 import {Language} from "@nomicfoundation/slang/language";
3 import {ProductionKind, RuleKind, TokenKind} from "@nomicfoundation/slang/kinds";
4
5 const source = "contract Foo {}";
6 const language = new Language("0.8.0");
7
8 const output = language.parse(ProductionKind.ContractDefinition, source);
9 assert(output.isValid);
10 assert(output.errors().length == 0);
Using Slang: Analyzing Solidity Contracts
12 // const source = "contract Foo {}";
13 const tree = output.tree();
14 assert(tree.kind == RuleKind.ContractDefinition);
15
16 const children = tree.children();
17 assert(children[0]!.kind == TokenKind.ContractKeyword); // "contract"
18 assert(children[1]!.kind == RuleKind.LeadingTrivia); // " "
19 assert(children[2]!.kind == TokenKind.Identifier); // "Foo"
20 assert(children[3]!.kind == RuleKind.LeadingTrivia); // " "
21 assert(children[4]!.kind == TokenKind.OpenBrace); // "{"
22 assert(children[5]!.kind == TokenKind.CloseBrace); // "}"
Using Slang: Analyzing Solidity Contracts
24 // const source = "contract Foo {}";
25 const cursor = output.createTreeCursor();
26
27 cursor.goToNthChild(2);
28 assert(cursor.node().kind == TokenKind.Identifier);
29
30 cursor.goToParent();
31 assert(cursor.node().kind == RuleKind.ContractDefinition);
32
33 const identifier = cursor.findTokenWithKind([TokenKind.Identifier]);
34 assert(identifier?.text == "Foo");
Useful Links
Please come chat with us after the talk. We would love to hear from you!
The Slang team at Nomic Foundation is currently three people, and we are hiring:
Questions?
Omar Tawfik
Igor Matuszewski
Antony Blakey
Questions?
📘