Skip to content

ConsenSysMesh/solidity-parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

74f153c · Jul 21, 2018
Jun 30, 2017
Dec 21, 2016
Dec 21, 2016
Apr 8, 2017
Apr 8, 2017
Dec 4, 2016
Jun 12, 2017
Jul 21, 2018
Dec 22, 2016
May 30, 2017
Nov 22, 2016
Jul 24, 2017
Aug 1, 2017

Repository files navigation

npm npm Build Status

⚠️ DEPRECATION NOTICE ⚠️

This library is not being maintained anymore. For an up-to-date Solidity parser, check out solidity-parser-antlr.

Solidity Parser

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

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

About

Solidity Parser in Javascript

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published