0.18.0 • Published 2 months ago

@solidity-parser/parser v0.18.0

Weekly downloads
68,025
License
MIT
Repository
github
Last release
2 months ago

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:

KeyTypeDefaultDescription
tolerantBooleanfalseWhen 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.
locBooleanfalseWhen 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.
rangeBooleanfalseWhen 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.umd.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

solhint-portablesolidity-doppelganger-demo@rari-capital/solidity-coverage@infinitebrahmanuniverse/nolb-_sol@everything-registry/sub-chunk-851multi-sol-verifier@witnet/truffle-flattener-single-experimental@xircus-web3/stardust@zemse/solidity-flatteneraboptioadhara-solidity-coverage@wuwe1/solkitamethysta@yuetlooo/hardhat-aragon@zalastax/nolb-_sol@andstor/smart-contract-slicer@cartesi/solidity-coverageblockapps-restcontractshark-solidity-coverage@bretth18/solhint@bowdo/hardhat-celo@calystral/prettier-plugin-solidityquibusdamquodquisquamautprettier-plugin-solidityprettier-plugin-solidity-explicit-typesoto-buidlertranspiler-solidityweb3mtruffle-flattenertruffle-flattener-1truffle-flattener-newtronboxsol2umlsuryasolidifiersolidity-workspacesolidity-parsesolidity-interfacersolidity-auditsolidity-audit-reportersolidity-code-metricssolidity-code-metrics-mlsolidity-coveragesolidity-coverage-forkedsolidity-coveragessolidity-doppelgangersolspsolivetest-hardhat-node18@deth-profiler/profilersolengine-jssolgraphsolgrepsolhintsolhint-cisolhint-communitysolhint-plugin-mudsolhint-nvimcelo-hardhat@crestproject/coverage@dolomite-exchange/hardhat0xweb@antchain/mysolidity@aragon/hardhat-aragon@aragon/buidler-aragon@enzymefinance/coverage@g4g/eth-gas-reporter@gzeoneth/hardhat@hexpayday/solidity-code-metrics@hexpayday/surya@1hive/hardhat-aragon@aiwozhe/hardhat@0xgabi/hardhat-aragon@134dd3v/prettier-plugin-solidity@ironblocks/clidequanto@dzejkop/hardhat@float-capital/solidity-coverage@devdavi/sumofeather-eip712@layerzerolabs/prettier-plugin-solidity@latticexyz/common@l2beat/discovery@komet/hardhat-kometevenietquiethhintgemforgeforkhintgas-saver@matthewethantam/surya@morenabarboni/sumo@nexeth/solidity-docgen@node-guardians/ng-quests-helpers@phated/hardhat@nomiclabs/buidlereth-gas-reportereth-gas-reporter-forked@octane-security/solgrep-ts@opengsn/truffle-flattener
0.18.0

2 months ago

0.17.0

3 months ago

0.16.1

9 months ago

0.16.2

5 months ago

0.16.0

1 year ago

0.15.0

1 year ago

0.14.5

1 year ago

0.14.4

1 year ago

0.14.2

2 years ago

0.14.3

2 years ago

0.14.2-beta.0

2 years ago

0.14.2-beta.1

2 years ago

0.14.1

2 years ago

0.14.0

2 years ago

0.13.3-rc.0

3 years ago

0.13.3-rc.1

3 years ago

0.13.2

3 years ago

0.13.1

3 years ago

0.13.0-rc.8

3 years ago

0.13.0

3 years ago

0.13.0-rc.7

3 years ago

0.13.0-rc.6

3 years ago

0.13.0-rc.5

3 years ago

0.13.0-rc.4

3 years ago

0.13.0-rc.3

3 years ago

0.13.0-rc.2

3 years ago

0.12.2

3 years ago

0.13.0-rc.1

3 years ago

0.13.0-rc.0

3 years ago

0.12.1

3 years ago

0.12.0

3 years ago

0.11.1

3 years ago

0.11.0

3 years ago

0.10.2

3 years ago

0.10.1

3 years ago

0.10.0

3 years ago

0.10.1-rc.1

3 years ago

0.9.1

3 years ago

0.9.0

3 years ago

0.8.2

3 years ago

0.8.1

4 years ago

0.8.0

4 years ago

0.7.1

4 years ago

0.7.0

4 years ago

0.6.2

4 years ago

0.6.1

4 years ago

0.6.0

4 years ago

0.5.2

4 years ago

0.5.1

4 years ago

0.4.18

4 years ago