# solidity-parser

> PEG.js Solidity parser for Javascript

Latest version **0.4.0** (published 2017-07-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install solidity-parser
pnpm add solidity-parser
yarn add solidity-parser
bun add solidity-parser
```

Provides the command `solidity-parser`.

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.4.0 |
| Published | 2017-07-24 |
| First published | 2016-05-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | tcoulter, gnidan |

## Links

- npm: https://www.npmjs.com/package/solidity-parser
- npm.io page: https://npm.io/package/solidity-parser

## Dependencies (3)

- [mocha](https://npm.io/package/mocha.md) ^2.4.5
- [pegjs](https://npm.io/package/pegjs.md) ^0.10.0
- [yargs](https://npm.io/package/yargs.md) ^4.6.0

## Recent versions

- 0.4.0 (latest) — 2017-07-24
- 0.3.0 — 2017-02-16
- 0.2.0 — 2016-12-16
- 0.1.1 — 2016-10-12
- 0.1.0 — 2016-08-24
- 0.0.9 — 2016-06-29
- 0.0.8 — 2016-05-09
- 0.0.7 — 2016-05-09
- 0.0.6 — 2016-05-09
- 0.0.5 — 2016-05-06
- 0.0.4 — 2016-05-06
- 0.0.3 — 2016-05-06
- 0.0.2 — 2016-05-06
- 0.0.1 — 2016-05-04

## README

[![npm](https://img.shields.io/npm/v/solidity-parser.svg)]()
[![npm](https://img.shields.io/npm/dm/solidity-parser.svg)]()
[![Build Status](https://travis-ci.org/ConsenSys/solidity-parser.svg?branch=master)](https://travis-ci.org/ConsenSys/solidity-parser)

# 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:

```javascript
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:

```javascript

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.

```json
{
  "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

---
_Source: https://npm.io/package/solidity-parser · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
