core.codeparser #
CodeParser Module
The codeparser module provides a comprehensive indexing and analysis system for V codebases. It walks directory trees, parses all V files, and allows efficient searching, filtering, and analysis of code structures.
Features
- Directory Scanning: Automatically walks directory trees and finds all V files
- Batch Parsing: Parses multiple files efficiently
- Indexing: Indexes code by module, structs, functions, interfaces, constants
- Search: Find specific items by name
- Filtering: Use predicates to filter code items
- Statistics: Get module statistics (file count, struct count, etc.)
- Export: Export complete codebase structure as JSON
- Error Handling: Gracefully handles parse errors
Basic Usage
import incubaid.herolib.core.codeparser
// Create a parser for a directory
mut parser := codeparser.new('/path/to/herolib')!
// List all modules
modules := parser.list_modules()
for mod in modules {
println('Module: ${mod}')
}
// Find a specific struct
struct_ := parser.find_struct('User', 'mymodule')!
println('Struct: ${struct_.name}')
// List all public functions
pub_fns := parser.filter_public_functions()
// Get methods on a struct
methods := parser.list_methods_on_struct('User')
// Export to JSON
json_str := parser.to_json()!
API Reference
Factory
new(root_dir: string) !CodeParser- Create parser for a directory
Listers
list_modules() []string- All moduleslist_files() []string- All fileslist_files_in_module(module: string) []string- Files in modulelist_structs(module: string = '') []Struct- All structslist_functions(module: string = '') []Function- All functionslist_interfaces(module: string = '') []Interface- All interfaceslist_methods_on_struct(struct: string, module: string = '') []Function- Methodslist_imports(module: string = '') []Import- All importslist_constants(module: string = '') []Const- All constants
Finders
find_struct(name: string, module: string = '') !Structfind_function(name: string, module: string = '') !Functionfind_interface(name: string, module: string = '') !Interfacefind_method(struct: string, method: string, module: string = '') !Functionfind_module(name: string) !ParsedModulefind_file(path: string) !ParsedFilefind_structs_with_method(method: string, module: string = '') []stringfind_callers(function: string, module: string = '') []Function
Filters
filter_structs(predicate: fn(Struct) bool, module: string = '') []Structfilter_functions(predicate: fn(Function) bool, module: string = '') []Functionfilter_public_structs(module: string = '') []Structfilter_public_functions(module: string = '') []Functionfilter_functions_with_receiver(module: string = '') []Functionfilter_functions_returning_error(module: string = '') []Functionfilter_structs_with_field(type: string, module: string = '') []Structfilter_structs_by_name(pattern: string, module: string = '') []Structfilter_functions_by_name(pattern: string, module: string = '') []Function
Export
to_json(module: string = '') !string- Export to JSONto_json_pretty(module: string = '') !string- Pretty-printed JSON
Error Handling
has_errors() bool- Check if parsing errors occurrederror_count() int- Get number of errorsprint_errors()- Print all errors
Example: Analyzing a Module
import incubaid.herolib.core.codeparser
mut parser := codeparser.new(os.home_dir() + '/code/github/incubaid/herolib/lib/core')!
// Get all public functions in the 'pathlib' module
pub_fns := parser.filter_public_functions('incubaid.herolib.core.pathlib')
for fn in pub_fns {
println('${fn.name}() -> ${fn.result.typ.symbol()}')
}
// Find all structs with a specific method
structs := parser.find_structs_with_method('read')
// Export pathlib module to JSON
json_str := parser.to_json('incubaid.herolib.core.pathlib')!
println(json_str)
Implementation Notes
- Lazy Parsing: Files are parsed only when needed
- Error Recovery: Parsing errors don't stop the indexing process
- Memory Efficient: Maintains index in memory but doesn't duplicate code
- Module Agnostic: Works with any V module structure
- Cross-Module Search: Can search across entire codebase or single module
fn new #
fn new(args ParserOptions) !CodeParser
new creates a CodeParser and scans the given root directory
struct CodeParser #
struct CodeParser {
pub mut:
root_dir string
options ParserOptions
parsed_files map[string]ParsedFile
modules map[string][]string
parse_errors []ParseError
}
fn (CodeParser) all_consts #
fn (p CodeParser) all_consts() []code.Const
all_consts returns all constants from all parsed files
fn (CodeParser) all_enums #
fn (p CodeParser) all_enums() []code.Enum
all_enums returns all enums from all parsed files
fn (CodeParser) all_functions #
fn (p CodeParser) all_functions() []code.Function
all_functions returns all functions from all parsed files
fn (CodeParser) all_imports #
fn (p CodeParser) all_imports() map[string]bool
all_imports returns a map of all unique imports
fn (CodeParser) all_structs #
fn (p CodeParser) all_structs() []code.Struct
all_structs returns all structs from all parsed files
fn (CodeParser) error_count #
fn (parser CodeParser) error_count() int
error_count returns the number of parsing errors
fn (CodeParser) filter_methods #
fn (parser CodeParser) filter_methods(module_name string) []code.Function
filter_methods returns all functions with receivers (methods)
fn (CodeParser) filter_public_functions #
fn (parser CodeParser) filter_public_functions(module_name string) []code.Function
filter_public_functions returns all public functions
fn (CodeParser) filter_public_structs #
fn (parser CodeParser) filter_public_structs(module_name string) []code.Struct
filter_public_structs returns all public structs
fn (CodeParser) find_callers #
fn (parser CodeParser) find_callers(args FinderOptions) []code.Function
find_callers finds all functions that call a specific function (basic text matching)
fn (CodeParser) find_file #
fn (parser CodeParser) find_file(path string) !ParsedFile
find_file retrieves parsed file information
fn (CodeParser) find_function #
fn (parser CodeParser) find_function(args FinderOptions) !code.Function
find_function searches for a function by name
fn (CodeParser) find_interface #
fn (parser CodeParser) find_interface(args FinderOptions) !code.Interface
find_interface searches for an interface by name
fn (CodeParser) find_method #
fn (parser CodeParser) find_method(args FinderOptions) !code.Function
find_method searches for a method on a struct
fn (CodeParser) find_module #
fn (parser CodeParser) find_module(module_name string) !ParsedModule
find_module searches for a module by name
fn (CodeParser) find_struct #
fn (parser CodeParser) find_struct(args FinderOptions) !code.Struct
find_struct searches for a struct by name
fn (CodeParser) find_structs_with_method #
fn (parser CodeParser) find_structs_with_method(args FinderOptions) []string
find_structs_with_method finds all structs that have a specific method
fn (CodeParser) functions #
fn (parser CodeParser) functions(options FilterOptions) []code.Function
functions returns a filtered list of all functions found in the parsed files
fn (CodeParser) get_module_stats #
fn (parser CodeParser) get_module_stats(module_name string) ModuleStats
get_module_stats calculates statistics for a module
fn (CodeParser) get_parsed_file #
fn (parser CodeParser) get_parsed_file(file_path string) ?ParsedFile
get_parsed_file returns the parsed file for a given path
fn (CodeParser) has_errors #
fn (parser CodeParser) has_errors() bool
has_errors returns true if any parsing errors occurred
fn (CodeParser) list_constants #
fn (parser CodeParser) list_constants(module_name string) []code.Const
list_constants returns all constants in the codebase (optionally filtered by module)
fn (CodeParser) list_files #
fn (parser CodeParser) list_files() []string
fn (CodeParser) list_files_in_module #
fn (parser CodeParser) list_files_in_module(module_name string) []string
list_files_in_module returns all file paths in a specific module
fn (CodeParser) list_functions #
fn (parser CodeParser) list_functions(module_name string) []code.Function
list_functions returns all functions in the codebase (optionally filtered by module)
fn (CodeParser) list_imports #
fn (parser CodeParser) list_imports(module_name string) []code.Import
list_imports returns all unique imports used in the codebase (optionally filtered by module)
fn (CodeParser) list_interfaces #
fn (parser CodeParser) list_interfaces(module_name string) []code.Interface
list_interfaces returns all interfaces in the codebase (optionally filtered by module)
fn (CodeParser) list_methods_on_struct #
fn (parser CodeParser) list_methods_on_struct(struct_name string, module_name string) []code.Function
list_methods_on_struct returns all methods (receiver functions) for a struct
fn (CodeParser) list_modules #
fn (parser CodeParser) list_modules() []string
list_modules returns a list of all parsed module names
fn (CodeParser) list_structs #
fn (parser CodeParser) list_structs(module_name string) []code.Struct
list_structs returns all structs in the codebase (optionally filtered by module)
fn (CodeParser) parse #
fn (mut parser CodeParser) parse() !
parse processes all V files that were scanned and parses them
fn (CodeParser) parse_file #
fn (mut parser CodeParser) parse_file(file_path string) !
parse_file parses a single V file and adds it to the index
fn (CodeParser) structs #
fn (parser CodeParser) structs(options FilterOptions) []code.Struct
structs returns a filtered list of all structs found in the parsed files
fn (CodeParser) to_json #
fn (parser CodeParser) to_json(module_name string) !string
to_json exports the complete code structure to JSON
Args: module_name - optional module filter (if empty, exports all modules) Returns: JSON string representation
struct CodeParserJSON #
struct CodeParserJSON {
pub mut:
root_dir string
modules map[string]ModuleJSON
summary SummaryJSON
}
JSON export structures
struct ConstJSON #
struct ConstJSON {
pub:
name string
value string
}
struct EnumJSON #
struct EnumJSON {
pub:
name string
is_pub bool
value_count int
description string
}
struct FileJSON #
struct FileJSON {
pub:
path string
module_name string
items_count int
structs []StructJSON
functions []FunctionJSON
interfaces []InterfaceJSON
enums []EnumJSON
constants []ConstJSON
}
struct FilterOptions #
struct FilterOptions {
pub:
module_name string
name_filter string // just partial match
is_public bool
has_receiver bool
}
struct FinderOptions #
struct FinderOptions {
pub:
name string @[required]
struct_name string // only useful for methods on structs
module_name string
}
struct FunctionJSON #
struct FunctionJSON {
pub:
name string
is_pub bool
has_return bool
params int
receiver string
}
struct InterfaceJSON #
struct InterfaceJSON {
pub:
name string
is_pub bool
description string
}
struct ModuleJSON #
struct ModuleJSON {
pub mut:
name string
files map[string]FileJSON
stats ModuleStats
imports []string
}
struct ModuleStats #
struct ModuleStats {
pub mut:
file_count int
struct_count int
function_count int
interface_count int
const_count int
}
struct ParseError #
struct ParseError {
pub:
file_path string
error string
}
ParseError represents an error that occurred while parsing a file
struct ParsedFile #
struct ParsedFile {
pub:
path string
module_name string
vfile code.VFile
}
ParsedFile represents a successfully parsed V file
struct ParsedModule #
struct ParsedModule {
pub:
name string
file_paths []string
stats ModuleStats
}
struct ParserOptions #
struct ParserOptions {
pub:
path string @[required]
recursive bool = true
exclude_patterns []string
include_patterns []string = ['*.v']
}
import incubaid.herolib.core.pathlib import incubaid.herolib.core.code
struct StructJSON #
struct StructJSON {
pub:
name string
is_pub bool
field_count int
description string
}
struct SummaryJSON #
struct SummaryJSON {
pub mut:
total_files int
total_modules int
total_structs int
total_functions int
total_interfaces int
total_enums int
}
- README
- fn new
- struct CodeParser
- fn all_consts
- fn all_enums
- fn all_functions
- fn all_imports
- fn all_structs
- fn error_count
- fn filter_methods
- fn filter_public_functions
- fn filter_public_structs
- fn find_callers
- fn find_file
- fn find_function
- fn find_interface
- fn find_method
- fn find_module
- fn find_struct
- fn find_structs_with_method
- fn functions
- fn get_module_stats
- fn get_parsed_file
- fn has_errors
- fn list_constants
- fn list_files
- fn list_files_in_module
- fn list_functions
- fn list_imports
- fn list_interfaces
- fn list_methods_on_struct
- fn list_modules
- fn list_structs
- fn parse
- fn parse_file
- fn structs
- fn to_json
- struct CodeParserJSON
- struct ConstJSON
- struct EnumJSON
- struct FileJSON
- struct FilterOptions
- struct FinderOptions
- struct FunctionJSON
- struct InterfaceJSON
- struct ModuleJSON
- struct ModuleStats
- struct ParseError
- struct ParsedFile
- struct ParsedModule
- struct ParserOptions
- struct StructJSON
- struct SummaryJSON