Giter VIP home page Giter VIP logo

solidity-parser's Introduction

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

npm install solidity-parser

Then, in your code:

var SolidityParser = require("solidity-parser");

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

// Or, parse a file:
var 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:

var SolidityParser = require("solidity-parser");

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

console.log(result);
// [
//   "SomeFile.sol",
//   "AnotherFile.sol"
// ]

Command Line (for convenience)

$ solidity-parser ./path/to/file.js

Results

Consider this solidity code as input:

import "Foo.sol";

contract MyContract {
  mapping (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.

{
  "type": "Program",
  "body": [
    {
      "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:

$ npm test

License

MIT

solidity-parser's People

Contributors

federicobond avatar tcoulter avatar cgewecke avatar frangio avatar raineorshine avatar marekkirejczyk avatar sogoiii avatar walkerq avatar

Watchers

James Cloos avatar Raghav avatar  avatar

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.