Commit 5e505895 authored by David Declerck's avatar David Declerck
Browse files

Update ocaml-solidity-parser-with-menhir.md

parent 2f528d6b
......@@ -4,64 +4,94 @@ date=2020-05-19
category=Blockchains
tags=
<a href="https://www.ocamlpro.com/wp-content/uploads/2020/05/solidity-cover.png" title="A Solidity parser in OCaml with Menhir"><a href="https://www.ocamlpro.com/2020/05/19/ocaml-solidity-parser-with-menhir/" title="A Solidity parser in OCaml with Menhir"><img width="420" height="420" src="https://www.ocamlpro.com/wp-content/uploads/2020/05/solidity-cover.png" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" srcset="https://www.ocamlpro.com/wp-content/uploads/2020/05/solidity-cover.png 420w, https://www.ocamlpro.com/wp-content/uploads/2020/05/solidity-cover-300x300.png 300w, https://www.ocamlpro.com/wp-content/uploads/2020/05/solidity-cover-150x150.png 150w" sizes="(max-width: 420px) 100vw, 420px" /></a></a>
<!-- size 420x420 -->
<blockquote class="wp-block-quote"><p id="11a3" class="ht ih cu bk hv b hw hx ii hy hz ij ia ib ik ic id il ie if im ig fh" data-selectable-paragraph=""><em class="in">This article is cross-posted on Origin Labs’ Dune Network <a href="https://medium.com/dune-network/a-solidity-parser-in-ocaml-with-menhir-e1064f94e76b">blog</a>.</em></p></blockquote>
[![Solidity Logo](https://www.ocamlpro.com/wp-content/uploads/2020/05/solidity-cover.png "A Solidity parser in OCaml with Menhir")](https://www.ocamlpro.com/2020/05/19/ocaml-solidity-parser-with-menhir/)
<p>We are happy to announce the first release of <a href="https://gitlab.com/o-labs/solidity-parser-ocaml" target="_blank" rel="noopener nofollow noreferrer">our Solidity parser</a>, written in OCaml using <a href="http://gallium.inria.fr/~fpottier/menhir/" target="_blank" rel="noopener nofollow noreferrer">Menhir.</a> This is a joint effort with <a href="https://www.origin-labs.com/">Origin Labs</a>, the company dedicated to blockchain challenges, to implement a full interpreter for the <a href="https://solidity.readthedocs.io/en/v0.6.8/" target="_blank" rel="noopener nofollow noreferrer">Solidity language</a> directly in a blockchain.</p>
<figure class="wp-block-image"><img src="https://miro.medium.com/max/1500/1*USQMP1HCMH3BW5KQ5DuOkQ.png" alt="Solidity"/></figure>
> This article is cross-posted on Origin Labs’ Dune Network [blog](https://medium.com/dune-network/a-solidity-parser-in-ocaml-with-menhir-e1064f94e76b)
<p>Solidity is probably the most popular language for smart-contracts, small pieces of code triggered when accounts receive transactions on a blockchain.Solidity is an object-oriented strongly-typed language with a Javascript-like syntax.</p>
We are happy to announce the first release of [our Solidity parser](https://gitlab.com/o-labs/solidity-parser-ocaml), written in OCaml using [Menhir](http://gallium.inria.fr/~fpottier/menhir/). This is a joint effort with [Origin Labs](https://www.origin-labs.com/), the company dedicated to blockchain challenges, to implement a full interpreter for the [Solidity language](https://solidity.readthedocs.io/en/v0.6.8/) directly in a blockchain.
<figure class="wp-block-image"><img src="https://miro.medium.com/max/1400/1*X-eldSjyZO6kJvHL_VYeCQ.png" alt=""/></figure>
![Solidity Logo](https://miro.medium.com/max/1500/1*USQMP1HCMH3BW5KQ5DuOkQ.png")
<p>Solidity was first implemented for the <a href="https://ethereum.org/" target="_blank" rel="noopener nofollow noreferrer">Ethereum</a> blockchain, with a compiler to the EVM, the Ethereum Virtual Machine.</p>
Solidity is probably the most popular language for smart-contracts, small pieces of code triggered when accounts receive transactions on a blockchain.Solidity is an object-oriented strongly-typed language with a Javascript-like syntax.
<figure class="wp-block-image"><img src="https://miro.medium.com/max/1400/1*pt7nACsxaiY4gCk8vjbeOQ.png" alt=""/></figure>
![Ethereum Logo](https://miro.medium.com/max/1400/1*X-eldSjyZO6kJvHL_VYeCQ.png)
<p>Dune Network takes a different approach, as Solidity smart-contracts will be executed natively, after type-checking. Solidity will be the third native language on Dune Network, with <a href="https://dune.network/docs/dune-node-mainnet/whitedoc/michelson.html" target="_blank" rel="noopener nofollow noreferrer">Michelson</a>, a low-level strongly-typed language inherited from Tezos, and <a href="https://dune.network/docs/dune-node-mainnet/love-doc/introduction.html" target="_blank" rel="noopener nofollow noreferrer">Love</a>, an higher-level strongly-typed language, also implemented jointly by OCamlPro and Origin Labs.</p>
Solidity was first implemented for the [Ethereum](https://ethereum.org/) blockchain, with a compiler to the EVM, the Ethereum Virtual Machine.
<p>A first step has been accomplished, with the completion of the Solidity parser and printer, written in OCaml with Menhir.</p>
![Dune Network Logo](https://miro.medium.com/max/1400/1*pt7nACsxaiY4gCk8vjbeOQ.png)
<p>This parser (and its printer companion) is now available as a standalone library under the LGPLv3 license with Linking Exception, allowing its integration in all projects. The source code is available at <a href="https://gitlab.com/o-labs/solidity-parser-ocaml" target="_blank" rel="noopener nofollow noreferrer">https://gitlab.com/o-labs/solidity-parser-ocaml</a>.</p>
Dune Network takes a different approach, as Solidity smart-contracts will be executed natively, after type-checking. Solidity will be the third native language on Dune Network, with [Michelson](https://dune.network/docs/dune-node-mainnet/whitedoc/michelson.html), a low-level strongly-typed language inherited from Tezos, and [Love](https://dune.network/docs/dune-node-mainnet/love-doc/introduction.html), an higher-level strongly-typed language, also implemented jointly by OCamlPro and Origin Labs.
<p>Our parser should support all of Solidity 0.6, with the notable exception of inline assembly (may be added in a future release).</p>
A first step has been accomplished, with the completion of the Solidity parser and printer, written in OCaml with Menhir.
<h1 id="4d19">Example contract</h1>
This parser (and its printer companion) is now available as a standalone library under the LGPLv3 license with Linking Exception, allowing its integration in all projects. The source code is available at [https://gitlab.com/o-labs/solidity-parser-ocaml].
<p>Here is an example of a very simple contract that stores an integer value and allows the contract’s owner to add an arbitrary value to this value, and any other contract to read this value:</p>
Our parser should support all of Solidity 0.6, with the notable exception of inline assembly (may be added in a future release).
<pre>pragma solidity >=0.6.0 <0.7.0;contract C { address owner; int x; constructor() public { owner = msg.sender; x = 0; } function add(int d) public { require(msg.sender == owner); x += d; } function read_x() public view returns(int) { return x; }}</pre>
# Example contract
<h1 class="jr js cu bk bj jt fq ju fs jv jw jx jy jz ka kb kc" id="d42c">Parser Usage</h1>
Here is an example of a very simple contract that stores an integer value and allows the contract’s owner to add an arbitrary value to this value, and any other contract to read this value:
<h2 class="kk js cu bk bj jt ku kv kw kx ky kz la lb lc ld le" id="d47c">Executable</h2>
```solidity
pragma solidity >=0.6.0 <0.7.0;
<p class="ht ih cu bk hv b hw kd ii hy ke ij ia kf ik ic kg il ie kh im ig fh">Our parser comes with a small executable that demonstrates the library usage. Simply run:</p>
contract C {
address owner;
int x;
<pre class="wp-block-preformatted">./solp contract.sol</pre>
constructor() public {
owner = msg.sender;
x = 0;
}
<p class="ht ih cu bk hv b hw hx ii hy hz ij ia ib ik ic id il ie if im ig fh">This will parse the file <code class="jh lf lg lh kl b">contract.sol</code> and reprint it on the terminal.</p>
function add(int d) public {
require(msg.sender == owner);
x += d;
}
<h2 class="kk js cu bk bj jt ku kv kw kx ky kz la lb lc ld le" id="e6b5">Library</h2>
function read_x() public view returns(int) {
return x;
}
}
```
<p class="ht ih cu bk hv b hw kd ii hy ke ij ia kf ik ic kg il ie kh im ig fh">To use our parser as a library, add it to your program’s dependencies and use the following function:</p>
# Parser Usage
<pre class="wp-block-preformatted">Solidity_parser.parse_contract_file : string -&gt; Solidity_parser.Solidity_types.module_</pre>
## Executable
<p class="ht ih cu bk hv b hw hx ii hy hz ij ia ib ik ic id il ie if im ig fh">It takes a filename and returns a Solidity AST.</p>
Our parser comes with a small executable that demonstrates the library usage. Simply run:
<p class="ht ih cu bk hv b hw hx ii hy hz ij ia ib ik ic id il ie if im ig fh">If you wish to print this AST, you may turn it into its string representation by sending it to the following function:</p>
```bash
./solp contract.sol
```
<pre class="wp-block-preformatted">Solidity_parser.Printer.string_of_code : Solidity_parser.Solidity_types.module_ -&gt; string</pre>
This will parse the file `contract.sol` and reprint it on the terminal.
<h1 class="jr js cu bk bj jt fq ju fs jv jw jx jy jz ka kb kc" id="9eee">Conclusion</h1>
## Library
<p class="ht ih cu bk hv b hw kd ii hy ke ij ia kf ik ic kg il ie kh im ig fh">Of course, all of this is Work In Progress, but we are quite happy to share it with the OCaml community. We think there is a tremendous work to be done around blockchains for experts in formal methods. Do not hesitate to contact us if you want to use this library!</p>
To use our parser as a library, add it to your program’s dependencies and use the following function:
<blockquote class="wp-block-quote"><p><strong class="hv li">About OCamlPro</strong></p><p>OCamlPro is a company founded in 2011, with the mission to help industrial users benefit from state-of-the art programming languages like OCaml and Rust. We design, create and implement custom ad-hoc software for our clients. We also have a long experience in developing and maintaining open-source tooling for OCaml, such as Opam, TryOCaml, ocp-indent etc. and we contribute to the core-development of OCaml, notably with our work on the Flambda optimizer branch with Jane Street. Another area of expertise is that of Formal Methods, with tools such as our SMT Solver Alt-Ergo (check our Alt-Ergo Users’ Club). We provide consulting and vocational trainings in OCaml and Rust, and we can build courses on formal methods on-demand. Do not hesitate to reach out by email: <a class="ch dk io ip iq ir" rel="noopener nofollow noreferrer" href="mailto:contact@ocamlpro.com" target="_blank">contact@ocamlpro.com</a>.</p></blockquote>
```ocaml
Solidity_parser.parse_contract_file : string -> Solidity_parser.Solidity_types.module_
```
<blockquote class="wp-block-quote"><p><strong class="hv li">About Origin Labs</strong></p><p>Origin Labs is a company founded in 2019 by the former blockchain team at OCamlPro. At Origin Labs, they have been developing Dune Network, a fork of the Tezos blockchain, its ecosystem, and applications over the Dune Network platform. At OCamlPro, they developed TzScan, the most popular block explorer at the time, Liquidity, a smart contract language, and were involved in the development of the core protocol and node.Do not hesitate to reach out by email: <a class="ch dk io ip iq ir" rel="noopener nofollow noreferrer" href="mailto:contact@ocamlpro.com" target="_blank">contact@origin-labs.com</a>.</p></blockquote>
It takes a filename and returns a Solidity AST.
If you wish to print this AST, you may turn it into its string representation by sending it to the following function:
```ocaml
Solidity_parser.Printer.string_of_code : Solidity_parser.Solidity_types.module_ -> string
```
# Conclusion
Of course, all of this is Work In Progress, but we are quite happy to share it with the OCaml community. We think there is a tremendous work to be done around blockchains for experts in formal methods. Do not hesitate to contact us if you want to use this library!
> #About OCamlPro
> OCamlPro is a company founded in 2011, with the mission to help industrial users benefit from state-of-the art programming languages like OCaml and Rust. We design, create and implement custom ad-hoc software for our clients. We also have a long experience in developing and maintaining open-source tooling for OCaml, such as Opam, TryOCaml, ocp-indent etc. and we contribute to the core-development of OCaml, notably with our work on the Flambda optimizer branch with Jane Street. Another area of expertise is that of Formal Methods, with tools such as our SMT Solver Alt-Ergo (check our Alt-Ergo Users’ Club). We provide consulting and vocational trainings in OCaml and Rust, and we can build courses on formal methods on-demand. Do not hesitate to reach out by email: [contact@ocamlpro.com]
> #About Origin Labs
> Origin Labs is a company founded in 2019 by the former blockchain team at OCamlPro. At Origin Labs, they have been developing Dune Network, a fork of the Tezos blockchain, its ecosystem, and applications over the Dune Network platform. At OCamlPro, they developed TzScan, the most popular block explorer at the time, Liquidity, a smart contract language, and were involved in the development of the core protocol and node.Do not hesitate to reach out by email: [contact@origin-labs.com]
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment