ConsenSys/solidity-parser

Name: solidity-parser

Owner: ConsenSys

Description: Solidity Parser in Javascript

Created: 2016-05-04 16:16:45.0

Updated: 2018-01-03 07:15:31.0

Pushed: 2017-08-01 20:44:37.0

Homepage: null

Size: 547

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

npm npm Build Status

Solidity Parser

A Solidity parser in Javascript. So we can evaluate and alter Solidity code without resorting to cruddy preprocessing.

?? WARNING ??

This is pre-alpha software. The goal of it is to take Solidity code as input and return an object as output that can be used to correctly describe that Solidity code. The structure of the resultant object is highly likely to change as the parser's features get filled out. This parser is set to ignore Solidity constructs it's not yet able to handle. Or, it might just error. So watch out.

Usage

Library

install solidity-parser

Then, in your code:

SolidityParser = require("solidity-parser");

arse Solidity code as a string:
result = SolidityParser.parse("contract { ... }");

r, parse a file:
result = SolidityParser.parseFile("./path/to/file.sol");

You can also parse a file specifically for its imports. This won't return an abstract syntax tree, but will instead return a list of files required by the parsed file:

SolidityParser = require("solidity-parser");

result = SolidityParser.parseFile("./path/to/file.sol", "imports");

ole.log(result);

 "SomeFile.sol",
 "AnotherFile.sol"

Command Line (for convenience)

lidity-parser ./path/to/file.js
Results

Consider this solidity code as input:

rt "Foo.sol";

ract MyContract {
pping (uint => address) public addresses;

You'll receiving the following (or something very similar) as output. Note that the structure of mappings could be made more clear, and this will likely be changed in the future.


ype": "Program",
ody": [
{
  "type": "ImportStatement",
  "value": "Foo.sol"
},
{
  "type": "ContractStatement",
  "name": "MyContract",
  "is": [],
  "body": [
    {
      "type": "ExpressionStatement",
      "expression": {
        "type": "DeclarativeExpression",
        "name": "addresses",
        "literal": {
          "type": "Type",
          "literal": {
            "type": "MappingExpression",
            "from": {
              "type": "Type",
              "literal": "uint",
              "members": [],
              "array_parts": []
            },
            "to": {
              "type": "Type",
              "literal": "address",
              "members": [],
              "array_parts": []
            }
          },
          "members": [],
          "array_parts": []
        },
        "is_constant": false,
        "is_public": true
      }
    }
  ]
}


Test

In a checkout of the project, run:

m test
License

MIT


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.