module

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package module 提供模块加载和管理功能

模块系统支持:

  • 模块加载和缓存
  • 路径解析(相对路径和绝对路径)
  • Import/Export 处理

示例:

loader := module.NewLoader(parser)
mod, err := loader.Load("./mymodule.dsl")
if err != nil {
    log.Fatal(err)
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildExportMap

func BuildExportMap(program *ast.Program, modulePath string) (map[string]*Rule, error)

BuildExportMap 构建导出映射

func FilterExports

func FilterExports(exports map[string]*Rule, predicate func(*Rule) bool) map[string]*Rule

FilterExports 过滤导出的规则

func GetAllExportedNames

func GetAllExportedNames(exports map[string]*Rule) []string

GetAllExportedNames 获取所有导出的名称

func GetImportedRules

func GetImportedRules(importedMod *ImportedModule) map[string]*Rule

GetImportedRules 获取所有导入的规则

func HasExport

func HasExport(name string, exports map[string]*Rule) bool

HasExport 检查是否导出了指定名称的规则

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 模块缓存接口

func NewCache

func NewCache() Cache

NewCache 创建新的模块缓存

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 导出处理器接口

func NewExportProcessor

func NewExportProcessor() ExportProcessor

NewExportProcessor 创建新的导出处理器

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 导入处理器接口

func NewImportProcessor

func NewImportProcessor() ImportProcessor

NewImportProcessor 创建新的导入处理器

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 模块加载器接口

func NewLoader

func NewLoader(p parser.Parser, baseDir string) Loader

NewLoader 创建新的模块加载器

type Module

type Module struct {
	Path    string            // 模块路径
	Program *ast.Program      // 解析后的 AST
	Exports map[string]*Rule  // 导出的规则
	Imports []*ImportedModule // 导入的模块
}

Module 表示一个已加载的模块

type Resolver

type Resolver interface {
	// Resolve 解析模块路径
	// basePath: 当前模块的路径
	// importPath: 要导入的路径
	// 返回: 解析后的绝对路径
	Resolve(basePath, importPath string) (string, error)
}

Resolver 路径解析器接口

func NewResolver

func NewResolver(baseDir string) Resolver

NewResolver 创建新的路径解析器 baseDir: 基础目录,用于解析相对路径

type Rule

type Rule struct {
	Name       string        // 规则名称
	Decl       *ast.RuleDecl // 规则声明
	ModulePath string        // 所属模块路径
}

Rule 表示一个规则定义

Jump to

Keyboard shortcuts

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