Giter VIP home page Giter VIP logo

syparse's Introduction

syparse - the Solidity parser written in Erlang

Build Status Coverage Status

syparse is a production-ready Solidity parser written in pure Erlang. syparse is closely aligned to the Solidity project and in future will be adapted on a regular basis as the Solidity project evolves. Solidity is a high-level language whose syntax is similar to that of JavaScript and it is designed to compile to code for the Ethereum Virtual Machine. And, with the grammar.txt file the project provides the basis for the definition of the LALR grammar.

1. Usage

Example code:

pragma solidity ^0.4.0;

contract C {
  struct s { uint a; uint b; }
  uint x;
  mapping(uint => mapping(uint => s)) data;
}

Parsing the example code:

1> {ok, {ParseTree, Tokens}} = syparse:source_to_pt("pragma solidity ^0.4.0;

                                                     contract C {
                                                       struct s { uint a; uint b; }
                                                       uint x;
                                                       mapping(uint => mapping(uint => s)) data;
                                                     }").

{ok,{{sourceUnit,
         [{pragmaDirective,{identifier,"solidity"},"^0.4.0;"},
          {contractDefinition,"contract",
              {identifier,"C"},
              [],
              [{contractPart,
                   {structDefinition,
                       {identifier,"s"},
                       [{variableDeclaration,
                            {typeName,{elementaryTypeName,"uint"}},
                            [],
                            {identifier,"a"}},
                        {variableDeclaration,
                            {typeName,{elementaryTypeName,"uint"}},
                            [],
                            {identifier,"b"}}]}},
               {contractPart,
                   {stateVariableDeclaration,
                       {typeName,{elementaryTypeName,"uint"}},
                       [],
                       {identifier,"x"},
                       []}},
               {contractPart,
                   {stateVariableDeclaration,
                       {typeName,
                           {mapping,
                               {elementaryTypeName,"uint"},
                               {typeName,
                                   {mapping,{elementaryTypeName,...},{...}}}}},
                       [],
                       {identifier,"data"},
                       []}}]}]},
     [{'PRAGMA',1},
      {'IDENTIFIER',8,"solidity"},
      {'PRAGMA_DIRECTIVE',1,"^0.4.0;"},
      {'CONTRACT',3},
      {'IDENTIFIER',1,"C"},
      {'{',3},
      {'STRUCT',4},
      {'IDENTIFIER',1,"s"},
      {'{',4},
      {'UINT',4,"uint"},
      {'IDENTIFIER',1,"a"},
      {';',4},
      {'UINT',4,"uint"},
      {'IDENTIFIER',1,"b"},
      {';',4},
      {'}',4},
      {'UINT',5,"uint"},
      {'IDENTIFIER',1,"x"},
      {';',5},
      {'MAPPING',6},
      {'(',6},
      {'UINT',6,[...]},
      {'=>',6},
      {'MAPPING',...},
      {...}|...]}}

Access the parse tree of the example code:

2> ParseTree.

{sourceUnit,
    [{pragmaDirective,{identifier,"solidity"},"^0.4.0;"},
     {contractDefinition,"contract",
         {identifier,"C"},
         [],
         [{contractPart,
              {structDefinition,
                  {identifier,"s"},
                  [{variableDeclaration,
                       {typeName,{elementaryTypeName,"uint"}},
                       [],
                       {identifier,"a"}},
                   {variableDeclaration,
                       {typeName,{elementaryTypeName,"uint"}},
                       [],
                       {identifier,"b"}}]}},
          {contractPart,
              {stateVariableDeclaration,
                  {typeName,{elementaryTypeName,"uint"}},
                  [],
                  {identifier,"x"},
                  []}},
          {contractPart,
              {stateVariableDeclaration,
                  {typeName,
                      {mapping,
                          {elementaryTypeName,"uint"},
                          {typeName,
                              {mapping,
                                  {elementaryTypeName,"uint"},
                                  {typeName,{userDefinedTypeName,...}}}}}},
                  [],
                  {identifier,"data"},
                  []}}]}]}

Access the token list of the example code:

3> Tokens.

[{'PRAGMA',1},
 {'IDENTIFIER',8,"solidity"},
 {'PRAGMA_DIRECTIVE',1,"^0.4.0;"},
 {'CONTRACT',3},
 {'IDENTIFIER',1,"C"},
 {'{',3},
 {'STRUCT',4},
 {'IDENTIFIER',1,"s"},
 {'{',4},
 {'UINT',4,"uint"},
 {'IDENTIFIER',1,"a"},
 {';',4},
 {'UINT',4,"uint"},
 {'IDENTIFIER',1,"b"},
 {';',4},
 {'}',4},
 {'UINT',5,"uint"},
 {'IDENTIFIER',1,"x"},
 {';',5},
 {'MAPPING',6},
 {'(',6},
 {'UINT',6,"uint"},
 {'=>',6},
 {'MAPPING',6},
 {'(',6},
 {'UINT',6,[...]},
 {'=>',6},
 {'IDENTIFIER',...},
 {...}|...]

Compile the code from a parse tree:

4> syparse:pt_to_source_td(ParseTree).

<<"pragma solidity ^0.4.0; contract C{struct s{uint a;uint b;} uint x; mapping(uint=>mapping(uint=>s)) data;}">>

Complete parse tree:

The output of the parse tree in the Erlang shell is shortened (cause not known). The complete parse tree of the example code looks as follows:

{sourceUnit,
 [{pragmaDirective,{identifier,"solidity"},"^0.4.0;"},
  {contractDefinition,"contract",
   {identifier,"C"},
   [],
   [{contractPart,
     {structDefinition,
      {identifier,"s"},
      [{variableDeclaration,
        {typeName,{elementaryTypeName,"uint"}},
        [],
        {identifier,"a"}},
       {variableDeclaration,
        {typeName,{elementaryTypeName,"uint"}},
        [],
        {identifier,"b"}}]}},
    {contractPart,
     {stateVariableDeclaration,
      {typeName,{elementaryTypeName,"uint"}},
      [],
      {identifier,"x"},
      []}},
    {contractPart,
     {stateVariableDeclaration,
      {typeName,
       {mapping,
        {elementaryTypeName,"uint"},
        {typeName,
         {mapping,
          {elementaryTypeName,"uint"},
          {typeName,{userDefinedTypeName,[{identifier,"s"}]}}}}}},
      [],
      {identifier,"data"},
      []}}]}]}

2. Documentation

The documentation for syparse is available here: Wiki.

3. Known issues

Due to deficiencies in the grammar definition of the rules PrimaryExpression and TypeName, the usability of the parser is still very limited.

ElementaryTypeNameExpression

Due to a reduce / reduce conflict between

ElementaryTypeNameExpression = ElementaryTypeName

and

TypeName = ElementaryTypeName
         | UserDefinedTypeName
         | Mapping
         | ArrayTypeName
         | FunctionTypeName

the grammar rule of ElementaryTypeNameExpression has precedence over TypeName.

Identifier versus Keywords

Keywords such as 'abstract', 'address', 'after', etc. must not be used as identifiers.

NumberLiteral

NumberLiteral = '0x'? [0-9]+ (' ' NumberUnit)?

The ' ' can not be enforced with the parser tools leex and yecc.

ParameterList

Due to a reduce / reduce conflict between

ParameterList =        '(' ( TypeName            Identifier? (',' TypeName            Identifier?)* )? ')'

and

TypeNameList =         '(' ( TypeName (',' TypeName )* )? ')'

the grammar rule of ParameterList has precedence over TypeNameList.

PrimaryExpression

Due to a reduce / reduce conflict between

PrimaryExpression = BooleanLiteral
                  | NumberLiteral
                  | HexLiteral
                  | StringLiteral
                  | TupleExpression
                  | Identifier
                  | ElementaryTypeNameExpression

and

UserDefinedTypeName = Identifier ( '.' Identifier )*

the grammar rule of PrimaryExpression has precedence over UserDefinedTypeName.

TypeNameList

Due to a reduce / reduce conflict between

ParameterList =        '(' ( TypeName            Identifier? (',' TypeName            Identifier?)* )? ')'

and

TypeNameList =         '(' ( TypeName (',' TypeName )* )? ')'

the grammar rule of ParameterList has precedence over TypeNameList.

4. Acknowledgement

This project was inspired by the sqlparse project of the company K2 Informatics GmbH.

syparse's People

Contributors

walter-weinmann avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

kwanghoon

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.