Documentation
¶
Overview ¶
Package contracts provides a set of utilities and listeners for working with Solidity contracts. It includes a ContractListener, which is a listener for the Solidity parser that extracts information about contracts, such as the contract name, implemented interfaces, imported contracts, pragmas, and comments. The ContractListener is designed to be used in conjunction with the Solidity parser to provide a convenient interface for working with Solidity contracts.
The ContractListener extracts information from Solidity contracts by traversing the Solidity parse tree and capturing relevant information during the parsing process. It identifies pragma directives, import directives, contract definitions, inheritance specifiers, using directives, and comments within the contract source code. Additionally, it can detect SPDX license identifiers if present.
The extracted contract information can be accessed using the provided getter methods, which return various aspects of the contract, such as the contract name, implemented interfaces, imported contracts, pragmas, comments, and SPDX license. The ContractListener also provides a convenience method, ToStruct, that returns a struct containing all the extracted information.
Index ¶
- type ContractListener
- func (l *ContractListener) EnterContractDefinition(ctx *parser.ContractDefinitionContext)
- func (l *ContractListener) EnterEveryRule(ctx antlr.ParserRuleContext)
- func (l *ContractListener) EnterImportDirective(ctx *parser.ImportDirectiveContext)
- func (l *ContractListener) EnterInheritanceSpecifier(ctx *parser.InheritanceSpecifierContext)
- func (l *ContractListener) EnterPragmaDirective(ctx *parser.PragmaDirectiveContext)
- func (l *ContractListener) EnterUsingDirective(ctx *parser.UsingDirectiveContext)
- func (l *ContractListener) GetComments() []string
- func (l *ContractListener) GetImplements() []string
- func (l *ContractListener) GetImports() []string
- func (l *ContractListener) GetLicense() string
- func (l *ContractListener) GetName() string
- func (l *ContractListener) GetPragmas() []string
- func (l *ContractListener) ToStruct() common.ContractInfo
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ContractListener ¶
type ContractListener struct {
*parser.BaseSolidityParserListener // BaseSolidityParserListener is the base listener from the Solidity parser.
// contains filtered or unexported fields
}
ContractListener is a listener for the Solidity parser that extracts information about contracts, including the contract name, implemented interfaces, imported contracts, pragmas, and comments. It also extracts the SPDX license identifier if present. This listener is designed to be used in conjunction with the Solidity parser to provide a convenient interface for working with Solidity contracts.
func NewContractListener ¶
func NewContractListener(parser *parser.SolidityParser) *ContractListener
NewContractListener creates a new ContractListener. It takes a SolidityParser as an argument.
func (*ContractListener) EnterContractDefinition ¶
func (l *ContractListener) EnterContractDefinition(ctx *parser.ContractDefinitionContext)
EnterContractDefinition is called when the parser enters a contract definition. It extracts the contract name from the context and sets it to the contractName field.
func (*ContractListener) EnterEveryRule ¶
func (l *ContractListener) EnterEveryRule(ctx antlr.ParserRuleContext)
EnterEveryRule is called when the parser enters any rule in the grammar. It is used to search for license and any comments that code has. ANTLR parser by default have comments disabled to be parsed as tokens, so we need to search for them manually using the CommonTokenStream.
func (*ContractListener) EnterImportDirective ¶
func (l *ContractListener) EnterImportDirective(ctx *parser.ImportDirectiveContext)
EnterImportDirective is called when the parser enters an import directive. It extracts the import path from the context and adds it to the imports slice.
func (*ContractListener) EnterInheritanceSpecifier ¶
func (l *ContractListener) EnterInheritanceSpecifier(ctx *parser.InheritanceSpecifierContext)
EnterInheritanceSpecifier is called when the parser enters an inheritance specifier. It extracts the name of the inherited contract/interface and adds it to the implements slice.
func (*ContractListener) EnterPragmaDirective ¶
func (l *ContractListener) EnterPragmaDirective(ctx *parser.PragmaDirectiveContext)
EnterPragmaDirective is called when the parser enters a pragma directive. It extracts all pragma tokens from the context and adds them to the pragmas slice.
func (*ContractListener) EnterUsingDirective ¶
func (l *ContractListener) EnterUsingDirective(ctx *parser.UsingDirectiveContext)
EnterUsingDirective is called when the parser enters a using directive. It extracts the name of the library and adds it to the libraries slice.
func (*ContractListener) GetComments ¶
func (l *ContractListener) GetComments() []string
GetComments returns a slice of all comments in the contract.
func (*ContractListener) GetImplements ¶
func (l *ContractListener) GetImplements() []string
GetImplements returns a slice of all interfaces that the contract implements.
func (*ContractListener) GetImports ¶
func (l *ContractListener) GetImports() []string
GetImports returns a slice of all contracts that the contract imports.
func (*ContractListener) GetLicense ¶
func (l *ContractListener) GetLicense() string
GetLicense returns the SPDX license identifier, if present.
func (*ContractListener) GetName ¶
func (l *ContractListener) GetName() string
GetName returns the name of the contract.
func (*ContractListener) GetPragmas ¶
func (l *ContractListener) GetPragmas() []string
GetPragmas returns a slice of all pragma directives in the contract.
func (*ContractListener) ToStruct ¶
func (l *ContractListener) ToStruct() common.ContractInfo
GetInfoForTests returns a map of all information extracted from the contract. This is used for testing purposes only