ast

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 19, 2025 License: MIT Imports: 11 Imported by: 0

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

func (f *Function) DeepCopy() *Function

DeepCopy creates a deep copy of the Function with an independent AST.

func (*Function) GetSignature

func (f *Function) GetSignature() string

GetSignature returns the function signature as a string. The signature is cached after first computation.

func (*Function) GetSource

func (f *Function) GetSource() (string, error)

GetSource returns the complete source code of the function.

func (*Function) Hash

func (f *Function) Hash() string

Hash returns a structural hash of the function for quick comparison.

func (*Function) IsValid

func (f *Function) IsValid(minLines int) bool

IsValid checks if the function meets the minimum requirements for analysis. It must have a non-nil body and meet the minimum line count.

func (*Function) Normalize

func (f *Function) Normalize() *Function

Normalize returns a normalized version of the function for comparison. Normalization removes variable names, literal values, and other non-structural elements while preserving the essential structure for similarity comparison.

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 NewParser

func NewParser() *Parser

NewParser creates a new Parser instance.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL