0.4.11 • Published 5 years ago
solidity-parser-antlr v0.4.11
solidity-parser-antlr
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
0.4.11
5 years ago
0.4.10
5 years ago
0.4.9
5 years ago
0.4.8
5 years ago
0.4.7
5 years ago
0.4.6
5 years ago
0.4.5
5 years ago
0.4.4
5 years ago
0.4.3
5 years ago
0.4.2
5 years ago
0.4.1
5 years ago
0.4.0
5 years ago
0.3.3
5 years ago
0.3.2
6 years ago
0.3.1
6 years ago
0.3.0
6 years ago
0.2.15
6 years ago
0.2.14
6 years ago
0.2.13
6 years ago
0.2.12
6 years ago
0.2.11
6 years ago
0.2.10
6 years ago
0.2.9
6 years ago
0.2.8
6 years ago
0.2.7
6 years ago
0.2.6
6 years ago
0.2.5
6 years ago
0.2.4
7 years ago
0.2.3
7 years ago
0.2.2
7 years ago
0.2.1
7 years ago
0.2.0
7 years ago
0.1.0
7 years ago
0.0.5
7 years ago
0.0.4
7 years ago
0.0.3
7 years ago
0.0.2
7 years ago
0.0.1
7 years ago