Documentation
¶
Overview ¶
Package module 提供模块加载和管理功能
模块系统支持:
- 模块加载和缓存
- 路径解析(相对路径和绝对路径)
- Import/Export 处理
示例:
loader := module.NewLoader(parser)
mod, err := loader.Load("./mymodule.dsl")
if err != nil {
log.Fatal(err)
}
Index ¶
- func BuildExportMap(program *ast.Program, modulePath string) (map[string]*Rule, error)
- func FilterExports(exports map[string]*Rule, predicate func(*Rule) bool) map[string]*Rule
- func GetAllExportedNames(exports map[string]*Rule) []string
- func GetImportedRules(importedMod *ImportedModule) map[string]*Rule
- func HasExport(name string, exports map[string]*Rule) bool
- func MergeImports(imports []*ImportedModule) (map[string]*Rule, error)
- type Cache
- type ExportProcessor
- type ImportProcessor
- type ImportedModule
- type Loader
- type Module
- type Resolver
- type Rule
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildExportMap ¶
BuildExportMap 构建导出映射
func FilterExports ¶
FilterExports 过滤导出的规则
func GetAllExportedNames ¶
GetAllExportedNames 获取所有导出的名称
func GetImportedRules ¶
func GetImportedRules(importedMod *ImportedModule) map[string]*Rule
GetImportedRules 获取所有导入的规则
func MergeImports ¶
func MergeImports(imports []*ImportedModule) (map[string]*Rule, error)
MergeImports 合并多个导入模块的规则
Types ¶
type Cache ¶
type Cache interface {
// Get 获取缓存的模块
Get(path string) (*Module, bool)
// Set 设置缓存的模块
Set(path string, module *Module)
// Clear 清空缓存
Clear()
// Remove 移除指定模块
Remove(path string)
}
Cache 模块缓存接口
type ExportProcessor ¶
type ExportProcessor interface {
// ProcessExport 处理导出声明
ProcessExport(exportDecl *ast.ExportDecl, modulePath string) (*Rule, error)
// ValidateExport 验证导出声明
ValidateExport(exportDecl *ast.ExportDecl) error
// GetExportedRule 获取导出的规则
GetExportedRule(name string, exports map[string]*Rule) (*Rule, error)
}
ExportProcessor 导出处理器接口
type ImportProcessor ¶
type ImportProcessor interface {
// ProcessImport 处理导入声明
ProcessImport(basePath string, importDecl *ast.ImportDecl, loader Loader) (*ImportedModule, error)
// ValidateImport 验证导入声明
ValidateImport(importDecl *ast.ImportDecl) error
// ResolveImportedRule 解析导入的规则
ResolveImportedRule(localName string, importedMod *ImportedModule) (*Rule, error)
}
ImportProcessor 导入处理器接口
type ImportedModule ¶
type ImportedModule struct {
Source string // 源路径
Module *Module // 导入的模块
Specifiers map[string]string // 导入项映射 (local -> imported)
}
ImportedModule 表示一个导入的模块
type Loader ¶
type Loader interface {
// Load 加载模块
Load(path string) (*Module, error)
// LoadFromSource 从源代码加载模块
LoadFromSource(path, source string) (*Module, error)
// GetCache 获取缓存
GetCache() Cache
// ClearCache 清空缓存
ClearCache()
}
Loader 模块加载器接口
type Module ¶
type Module struct {
Path string // 模块路径
Program *ast.Program // 解析后的 AST
Exports map[string]*Rule // 导出的规则
Imports []*ImportedModule // 导入的模块
}
Module 表示一个已加载的模块
Click to show internal directories.
Click to hide internal directories.