A Solidity parser for JS built on top of a robust ANTLR4 grammar

Federico Bond ca5da9b0d1 Fix lint errors 1 year ago
scripts 86be7279bb Update antlr command name in antlr4 script 1 year ago
solidity-antlr4 @ 88c2be659a 5e14b90d22 Improve parsing of type name expressions 1 year ago
src ca5da9b0d1 Fix lint errors 1 year ago
test 9581f68d39 Add support for NatSpec comments 1 year ago
.babelrc 615dc0a635 Add ES5 support using Babel 2 years ago
.editorconfig 816d59b5af Add .editorconfig file 3 years ago
.eslintignore 6fa0974c4a Make it pass both prettier and eslint 1 year ago
.eslintrc 9581f68d39 Add support for NatSpec comments 1 year ago
.gitignore 615dc0a635 Add ES5 support using Babel 2 years ago
.gitmodules caba0d7fdf add grammar as git submodule 2 years ago
.npmignore e2c5a058f2 Ignore more files for npm package 1 year ago
.travis.yml f6f37f99f3 Add build status badge 3 years ago
CHANGES.md 5e14b90d22 Improve parsing of type name expressions 1 year ago
LICENSE 6c5346985d Add explicit license file 2 years ago
README.md 8e2bf32bce Add note about line and column numbers. Closes #78 1 year ago
index.d.ts db37b6a886 add typings for unary expression. (#91) 1 year ago
package-lock.json 51f7698d98 Update dependencies 1 year ago
package.json 51f7698d98 Update dependencies 1 year ago
tslint.json c067a50f8c Remove ImportDeclaration type 1 year ago
yarn.lock c067a50f8c Remove ImportDeclaration type 1 year ago

README.md

solidity-parser-antlr

npm Build Status

A Solidity parser built on top of a robust ANTLR4 grammar.

Usage

import parser from 'solidity-parser-antlr';

var input = `
    contract test {
        uint256 a;
        function f() {}
    }
`
try {
    parser.parse(input)
} catch (e) {
    if (e instanceof parser.ParserError) {
        console.log(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)
  }
})

Author

Federico Bond (@federicobond)

License

MIT