npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@d1ll0n/solidity-parser

v0.0.3

Published

A Solidity parser built from a robust ANTLR 4 grammar

Downloads

10

Readme

Solidity Parser for JavaScript

npm version

A JavaScript package for parsing Solidity code using ANTLR (ANother Tool for Language Recognition) grammar.

This is a fork of @federicobond's original repo, with some extra features taken from Consensys Diligence's alternative fork.

Installation

The following installation options assume Node.js has already been installed.

Using Node Package Manager (npm).

npm install @solidity-parser/parser

Using yarn

yarn add @solidity-parser/parser

Usage

const parser = require('@solidity-parser/parser');

const input = `
    contract test {
        uint256 a;
        function f() {}
    }
`
try {
    const ast = parser.parse(input)
    console.log(ast)
} catch (e) {
    if (e instanceof parser.ParserError) {
        console.error(e.errors)
    }
}

The parse method also accepts a second argument which lets you specify the following options, in a style similar to the esprima API:

| Key | Type | Default | Description | |----------|---------|---------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | tolerant | Boolean | false | When set to true it will collect syntax errors and place them in a list under the key errors inside the root node of the returned AST. Otherwise, it will raise a parser.ParserError. | | loc | Boolean | false | When set to true, it will add location information to each node, with start and stop keys that contain the corresponding line and column numbers. Column numbers start from 0, lines start from 1. | | range | Boolean | false | When set to true, it will add range information to each node, which consists of a two-element array with start and stop character indexes in the input. |

Example with location information

parser.parse('contract test { uint a; }', { loc: true })

// { type: 'SourceUnit',
//   children:
//    [ { type: 'ContractDefinition',
//        name: 'test',
//        baseContracts: [],
//        subNodes: [Array],
//        kind: 'contract',
//        loc: [Object] } ],
//   loc: { start: { line: 1, column: 0 }, end: { line: 1, column: 24 } } }

Example using a visitor to walk over the AST

var ast = parser.parse('contract test { uint a; }')

// output the path of each import found
parser.visit(ast, {
  ImportDirective: function(node) {
    console.log(node.path)
  }
})

Usage in the browser

A browser-friendly version is available in dist/index.iife.js (along with its sourcemaps file) in the published version.

If you are using webpack, keep in mind that minimizing your bundle will mangle function names, breaking the parser. To fix this you can just set optimization.minimize to false.

Contribution

This project is dependant on the @solidity-parser/antlr repository via a git submodule. To clone this repository and the submodule, run

git clone --recursive

If you have already cloned this repo, you can load the submodule with

git submodule update --init

This project can be linked to a forked @solidity-parser/antlr project by editing the url in the .gitmodules file to point to the forked repo and running

git submodule sync

The Solidity ANTLR file Solidity.g4 can be built with the following. This will also download the ANTLR Java Archive (jar) file to antlr/antlr4.jar if it doesn't already exist. The generated ANTLR tokens and JavaScript files are copied the src folder.

yarn run antlr

The files to be distributed with the npm package are in the dist folder and built by running

yarn run build

The mocha tests under the test folder can be run with the following. This includes parsing the test.sol Solidity file.

yarn run test

Used by

License

MIT