Documentation
¶
Overview ¶
Package ast provides Go abstract syntax tree (AST) parsing and function extraction capabilities for the similarity detection tool.
This package implements high-performance AST analysis using Go's standard library with thread-safe operations and comprehensive function metadata extraction.
Key Components:
- Parser: Parses Go source files and extracts function declarations
- Function: Thread-safe function representation with metadata and AST data
- Normalization: AST structure normalization for accurate comparison
The Parser efficiently processes Go files using the go/ast and go/parser packages, extracting detailed function information including line numbers, signatures, and complete AST representations.
Thread Safety: All operations are designed to be thread-safe using sync.RWMutex for concurrent access patterns, preventing data races during parallel processing.
Example Usage:
parser := NewParser()
result := parser.ParseFile("example.go")
if result.IsOk() {
functions := result.Unwrap()
for _, fn := range functions {
fmt.Printf("Function: %s (lines %d-%d)\n",
fn.Name, fn.StartLine, fn.EndLine)
}
}
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FileMetadata ¶
type FileMetadata struct {
TotalFiles int // Total number of files processed
SuccessfulFiles int // Number of files parsed successfully
FailedFiles int // Number of files that failed to parse
}
FileMetadata contains metadata about the parsing operation.
type Function ¶
type Function struct {
Name string // Function name
File string // Source file path
StartLine int // Starting line number
EndLine int // Ending line number
AST *ast.FuncDecl // Original AST node
Normalized *ast.FuncDecl // Normalized AST for comparison
LineCount int // Number of lines in the function
// contains filtered or unexported fields
}
Function represents a Go function with its metadata and AST representation.
func (*Function) DeepCopy ¶ added in v0.2.0
DeepCopy creates a deep copy of the Function with an independent AST.
func (*Function) GetSignature ¶
GetSignature returns the function signature as a string. The signature is cached after first computation.
type ParseResult ¶
type ParseResult struct {
Functions []*Function // Successfully parsed functions
Errors []error // Errors encountered during parsing
Metadata FileMetadata // Additional metadata about the parsing operation
}
ParseResult contains the results of parsing one or more Go files.
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser handles parsing Go source files and extracting function information.
func (*Parser) ParseFile ¶
func (p *Parser) ParseFile(filename string) types.Result[*ParseResult]
ParseFile parses a single Go source file and extracts function information.
func (*Parser) ParseFiles ¶
func (p *Parser) ParseFiles(filenames []string) types.Result[*ParseResult]
ParseFiles parses multiple Go source files and returns combined results. This method continues processing even if some files fail, collecting all errors.