https://github.com/duaraghav8/solidityparser
ANTLR4-generated Go library to parse Solidity
https://github.com/duaraghav8/solidityparser
antlr4 golang parser solidity
Last synced: about 1 month ago
JSON representation
ANTLR4-generated Go library to parse Solidity
- Host: GitHub
- URL: https://github.com/duaraghav8/solidityparser
- Owner: duaraghav8
- License: mit
- Created: 2020-05-17T11:23:02.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-05-22T09:58:00.000Z (about 6 years ago)
- Last Synced: 2025-01-22T12:26:56.026Z (over 1 year ago)
- Topics: antlr4, golang, parser, solidity
- Language: Go
- Size: 185 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Solidity Parser
This is a Go module to parse [Solidity](https://solidity.readthedocs.io/en/latest/), based on [Federico](https://github.com/federicobond) 's [ANTLR grammar](https://github.com/solidityj/solidity-antlr4) for the language.
## Usage
```go
package main
import (
"fmt"
"github.com/antlr/antlr4/runtime/Go/antlr"
"github.com/duaraghav8/solidityparser"
)
var code = `
contract Foo {
function bar(uint x) {}
}
contract Bar {
constructor(){}
}
`
// EverythingListener listens for all rules, i.e., it visits
// all AST nodes.
type EverythingListener struct {
*solidityparser.BaseSolidityListener
}
func NewEverythingListener() *EverythingListener {
return new(EverythingListener)
}
func (l *EverythingListener) EnterEveryRule(ctx antlr.ParserRuleContext) {
fmt.Println("Entered a rule")
fmt.Println(ctx.GetRuleIndex())
fmt.Printf("Text: %s\nStart line: %d\n", ctx.GetText(), ctx.GetStart().GetLine())
fmt.Println("--------------")
}
func main() {
code := antlr.NewInputStream(code)
lexer := solidityparser.NewSolidityLexer(code)
stream := antlr.NewCommonTokenStream(lexer, 0)
parser := solidityparser.NewSolidityParser(stream)
parser.AddErrorListener(antlr.NewDiagnosticErrorListener(true))
parser.BuildParseTrees = true
tree := parser.SourceUnit()
antlr.ParseTreeWalkerDefault.Walk(NewEverythingListener(), tree)
}
```
## Building
Install the ANTLR4 tool and the Go runtime as described [here](https://github.com/antlr/antlr4/blob/master/doc/getting-started.md) & [here](https://github.com/antlr/antlr4/blob/master/doc/go-target.md).
To generate this Parser:
1. Update the `solidity-antlr4` [submodule](https://git-scm.com/book/en/v2/Git-Tools-Submodules) to the appropriate commit.
2. Run `scripts/generate-solidity-parser.sh` in the root directory of this repository.
3. Run `go mod tidy && go mod vendor`
4. Commit and push.