Documentation
¶
Overview ¶
Package ast provides an Abstract Syntax Tree (AST) representation for Solidity contracts. The ast package offers a set of data structures and functions to parse Solidity source code and construct an AST that represents the structure and semantics of the contracts. The package supports the creation of nodes for various Solidity constructs such as contracts, functions, modifiers, variables, statements, expressions, events, errors, enums, structs, and more. It also includes utilities for traversing and inspecting the AST, as well as generating code from the AST. By utilizing the ast package, developers can programmatically analyze, manipulate, and generate Solidity code. It serves as a foundation for building tools, analyzers, compilers, and other applications that require deep understanding and processing of Solidity contracts.
Note: The ast package is designed to be used in conjunction with a Solidity parser, provided by ANTLR, to generate the initial AST from Solidity source code. It then enriches the AST with additional information and functionality specific to the Solidity language.
Index ¶
- type ASTBuilder
- func (b *ASTBuilder) CreateFunction(ctx *parser.FunctionDefinitionContext) *FunctionNode
- func (b *ASTBuilder) EnterConstructorDefinition(ctx *parser.ConstructorDefinitionContext)
- func (b *ASTBuilder) EnterContractDefinition(ctx *parser.ContractDefinitionContext)
- func (b *ASTBuilder) EnterEnumDefinition(ctx *parser.EnumDefinitionContext)
- func (b *ASTBuilder) EnterErrorDefinition(ctx *parser.ErrorDefinitionContext)
- func (b *ASTBuilder) EnterEventDefinition(ctx *parser.EventDefinitionContext)
- func (b *ASTBuilder) EnterFallbackFunctionDefinition(ctx *parser.FallbackFunctionDefinitionContext)
- func (b *ASTBuilder) EnterFunctionDefinition(ctx *parser.FunctionDefinitionContext)
- func (b *ASTBuilder) EnterInterfaceDefinition(ctx *parser.InterfaceDefinitionContext)
- func (b *ASTBuilder) EnterLibraryDefinition(ctx *parser.LibraryDefinitionContext)
- func (b *ASTBuilder) EnterReceiveFunctionDefinition(ctx *parser.ReceiveFunctionDefinitionContext)
- func (b *ASTBuilder) EnterSourceUnit(ctx *parser.SourceUnitContext)
- func (b *ASTBuilder) EnterStateVariableDeclaration(ctx *parser.StateVariableDeclarationContext)
- func (b *ASTBuilder) EnterStructDefinition(ctx *parser.StructDefinitionContext)
- func (b *ASTBuilder) EnterUsingDirective(ctx *parser.UsingDirectiveContext)
- func (b *ASTBuilder) ExitContractDefinition(ctx *parser.ContractDefinitionContext)
- func (b *ASTBuilder) GetErrors() []error
- func (b *ASTBuilder) GetParseTime() time.Duration
- func (b *ASTBuilder) GetPragmas() [][]string
- func (b *ASTBuilder) GetTree() Node
- func (b *ASTBuilder) ToJSON() (string, error)
- type ConstructorNode
- type ContractNode
- type EnumMemberNode
- type EnumNode
- type ErrorNode
- type ErrorValueNode
- type EventNode
- type EventParameterNode
- type FunctionNode
- type InterfaceNode
- type ModifierNode
- type MutabilityNode
- type Node
- type RootNode
- type StateVariableNode
- type StatementNode
- type StructMemberNode
- type StructNode
- type UsingDirectiveNode
- type VariableNode
- type VisibilityNode
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ASTBuilder ¶
type ASTBuilder struct {
*parser.BaseSolidityParserListener
// contains filtered or unexported fields
}
func NewAstBuilder ¶
func NewAstBuilder() *ASTBuilder
func (*ASTBuilder) CreateFunction ¶
func (b *ASTBuilder) CreateFunction(ctx *parser.FunctionDefinitionContext) *FunctionNode
func (*ASTBuilder) EnterConstructorDefinition ¶
func (b *ASTBuilder) EnterConstructorDefinition(ctx *parser.ConstructorDefinitionContext)
func (*ASTBuilder) EnterContractDefinition ¶
func (b *ASTBuilder) EnterContractDefinition(ctx *parser.ContractDefinitionContext)
func (*ASTBuilder) EnterEnumDefinition ¶
func (b *ASTBuilder) EnterEnumDefinition(ctx *parser.EnumDefinitionContext)
func (*ASTBuilder) EnterErrorDefinition ¶
func (b *ASTBuilder) EnterErrorDefinition(ctx *parser.ErrorDefinitionContext)
func (*ASTBuilder) EnterEventDefinition ¶
func (b *ASTBuilder) EnterEventDefinition(ctx *parser.EventDefinitionContext)
func (*ASTBuilder) EnterFallbackFunctionDefinition ¶
func (b *ASTBuilder) EnterFallbackFunctionDefinition(ctx *parser.FallbackFunctionDefinitionContext)
func (*ASTBuilder) EnterFunctionDefinition ¶
func (b *ASTBuilder) EnterFunctionDefinition(ctx *parser.FunctionDefinitionContext)
func (*ASTBuilder) EnterInterfaceDefinition ¶
func (b *ASTBuilder) EnterInterfaceDefinition(ctx *parser.InterfaceDefinitionContext)
func (*ASTBuilder) EnterLibraryDefinition ¶ added in v0.1.5
func (b *ASTBuilder) EnterLibraryDefinition(ctx *parser.LibraryDefinitionContext)
func (*ASTBuilder) EnterReceiveFunctionDefinition ¶
func (b *ASTBuilder) EnterReceiveFunctionDefinition(ctx *parser.ReceiveFunctionDefinitionContext)
func (*ASTBuilder) EnterSourceUnit ¶
func (b *ASTBuilder) EnterSourceUnit(ctx *parser.SourceUnitContext)
func (*ASTBuilder) EnterStateVariableDeclaration ¶
func (b *ASTBuilder) EnterStateVariableDeclaration(ctx *parser.StateVariableDeclarationContext)
func (*ASTBuilder) EnterStructDefinition ¶
func (b *ASTBuilder) EnterStructDefinition(ctx *parser.StructDefinitionContext)
func (*ASTBuilder) EnterUsingDirective ¶
func (b *ASTBuilder) EnterUsingDirective(ctx *parser.UsingDirectiveContext)
func (*ASTBuilder) ExitContractDefinition ¶
func (b *ASTBuilder) ExitContractDefinition(ctx *parser.ContractDefinitionContext)
func (*ASTBuilder) GetErrors ¶
func (b *ASTBuilder) GetErrors() []error
func (*ASTBuilder) GetParseTime ¶
func (b *ASTBuilder) GetParseTime() time.Duration
func (*ASTBuilder) GetPragmas ¶
func (b *ASTBuilder) GetPragmas() [][]string
func (*ASTBuilder) GetTree ¶
func (b *ASTBuilder) GetTree() Node
func (*ASTBuilder) ToJSON ¶
func (b *ASTBuilder) ToJSON() (string, error)
ToJSON converts the ABI object into a JSON string.
type ConstructorNode ¶
type ConstructorNode struct {
Parameters []*VariableNode `json:"parameters"`
Body []*StatementNode `json:"body"`
}
ConstructorNode represents a constructor definition in Solidity.
func (*ConstructorNode) Children ¶
func (c *ConstructorNode) Children() []Node
type ContractNode ¶
type ContractNode struct {
Name string `json:"name"`
StateVariables []*StateVariableNode `json:"variables"`
Enums []*EnumNode `json:"enums"`
Structs []*StructNode `json:"structs"`
Events []*EventNode `json:"events"`
Errors []*ErrorNode `json:"errors"`
Constructor *ConstructorNode `json:"constructor"`
Functions []*FunctionNode `json:"functions"`
Kind string `json:"kind"`
Inherits []string `json:"inherits"`
Using []*UsingDirectiveNode `json:"using"`
}
ContractNode represents a contract definition in Solidity.
func (*ContractNode) Children ¶
func (c *ContractNode) Children() []Node
type EnumMemberNode ¶
type EnumMemberNode struct {
Name string `json:"name"`
}
func (*EnumMemberNode) Children ¶
func (e *EnumMemberNode) Children() []Node
type EnumNode ¶
type EnumNode struct {
Name string `json:"name"` // Name of the enum
MemberValues []*EnumMemberNode `json:"memberValues"` // Values of the enum members
}
type ErrorNode ¶
type ErrorNode struct {
Name string `json:"name"`
Values []*ErrorValueNode `json:"values"`
}
type ErrorValueNode ¶
type ErrorValueNode struct {
Name string `json:"name"`
Type string `json:"type"`
Code int `json:"code"`
}
func (*ErrorValueNode) Children ¶
func (ev *ErrorValueNode) Children() []Node
type EventNode ¶
type EventNode struct {
Name string `json:"name"`
Anonymous bool `json:"anonymous"`
Parameters []*EventParameterNode `json:"parameters"`
}
type EventParameterNode ¶
type EventParameterNode struct {
Name string `json:"name"`
Type string `json:"type"`
Indexed bool `json:"indexed"`
}
func (*EventParameterNode) Children ¶
func (e *EventParameterNode) Children() []Node
type FunctionNode ¶
type FunctionNode struct {
Name string `json:"name"`
Parameters []*VariableNode `json:"parameters"`
ReturnParameters []*VariableNode `json:"return_parameters"`
Body []*StatementNode `json:"body"`
Visibility []*VisibilityNode `json:"visibility"`
Mutability []*MutabilityNode `json:"mutability"`
Modifiers []*ModifierNode `json:"modifiers"`
IsVirtual bool `json:"is_virtual"`
IsReceive bool `json:"is_receive"`
IsFallback bool `json:"is_fallback"`
Overrides bool `json:"overrides"`
}
func (*FunctionNode) Children ¶
func (f *FunctionNode) Children() []Node
type InterfaceNode ¶
type InterfaceNode struct {
Name string `json:"name"`
Functions []*FunctionNode `json:"functions"`
}
InterfaceNode represents an interface definition in Solidity.
func (*InterfaceNode) Children ¶
func (i *InterfaceNode) Children() []Node
type ModifierNode ¶
type ModifierNode struct {
Modifier string `json:"value"`
}
func (*ModifierNode) Children ¶
func (m *ModifierNode) Children() []Node
type MutabilityNode ¶
type MutabilityNode struct {
Mutability string `json:"value"`
}
func (*MutabilityNode) Children ¶
func (m *MutabilityNode) Children() []Node
type Node ¶
type Node interface {
// Children returns the child nodes of this node.
Children() []Node
}
Node is the interface that all AST nodes implement.
type RootNode ¶
type RootNode struct {
Contracts []*ContractNode `json:"contracts"`
}
RootNode represents the root of the AST and can contain multiple contracts.
type StateVariableNode ¶
type StateVariableNode struct {
Name string `json:"name"`
Type string `json:"type"`
Visibility string `json:"visibility"`
IsConstant bool `json:"is_constant"`
IsImmutable bool `json:"is_immutable"`
InitialValue string `json:"initial_value"`
}
StateVariableNode represents a state variable definition in Solidity.
func (*StateVariableNode) Children ¶
func (v *StateVariableNode) Children() []Node
Children returns an empty slice of nodes.
type StatementNode ¶
StatementNode represents a statement in Solidity.
func (*StatementNode) Children ¶
func (s *StatementNode) Children() []Node
type StructMemberNode ¶
StructMemberNode represents a member of a struct.
func (*StructMemberNode) Children ¶
func (s *StructMemberNode) Children() []Node
Children returns an empty slice of nodes.
type StructNode ¶
type StructNode struct {
Name string
Members []*StructMemberNode
}
StructNode represents a struct definition in Solidity.
func (*StructNode) AddMember ¶
func (s *StructNode) AddMember(member *StructMemberNode)
AddMember adds a member to the struct.
func (*StructNode) Children ¶
func (s *StructNode) Children() []Node
Children returns an empty slice of nodes.
type UsingDirectiveNode ¶
type UsingDirectiveNode struct {
Alias string `json:"alias"`
Type string `json:"type"`
IsWildcard bool `json:"is_wildcard"`
IsGlobal bool `json:"is_global"`
IsUserDef bool `json:"is_user_defined"`
}
UsingDirectiveNode represents a using directive in the AST.
func (*UsingDirectiveNode) Children ¶
func (n *UsingDirectiveNode) Children() []Node
Children returns an empty slice of nodes.
type VariableNode ¶
VariableNode represents a variable definition in Solidity.
func (*VariableNode) Children ¶
func (v *VariableNode) Children() []Node
Children returns an empty slice of nodes.
type VisibilityNode ¶
type VisibilityNode struct {
Visibility string `json:"value"`
}
func (*VisibilityNode) Children ¶
func (v *VisibilityNode) Children() []Node