uniast

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const Version = "v0.1.3"

Variables

This section is empty.

Functions

func Append

func Append[T comparable](ids []T, id T) []T

func ModPathName

func ModPathName(mod ModPath) string

Types

type Dependency

type Dependency struct {
	Identity
	FileLine `json:",omitempty"`
}

func InsertDependency

func InsertDependency(ids []Dependency, id Dependency) []Dependency

func NewDependency

func NewDependency(id Identity, fl FileLine) Dependency

func (Dependency) Id

func (d Dependency) Id() Identity

type File

type File struct {
	Path    string
	Imports []Import `json:",omitempty"`
	Package PkgPath  `json:",omitempty"`
}

func NewFile

func NewFile(path string) *File

type FileLine

type FileLine struct {
	File string

	// NOTICE: start line. line number start from 1
	Line int

	// start offset in file
	StartOffset int

	// end offset in file
	EndOffset int
}

FileLine represents a filename and line number

type Function

type Function struct {
	Exported bool

	IsMethod          bool // If the function is a method
	IsInterfaceMethod bool // If is a empty interface method stub
	Identity               // unique identity in a repo
	FileLine
	Content string // Content of the function, including functiion signature and body

	Signature string       `json:",omitempty"`
	Receiver  *Receiver    `json:",omitempty"` // Method receiver
	Params    []Dependency `json:",omitempty"` // function parameters, key is the parameter name
	Results   []Dependency `json:",omitempty"` // function results, key is the result name or type name

	// call to in-the-project functions, key is {{pkgAlias.funcName}} or {{funcName}}
	FunctionCalls []Dependency `json:",omitempty"`

	// call to internal methods,
	// NOTICE: method name may be duplicated, so we collect according to the SEQUENCE of APPEARANCE
	MethodCalls []Dependency `json:",omitempty"`

	Types      []Dependency `json:",omitempty"` // types used in the function
	GlobalVars []Dependency `json:",omitempty"` // global vars used in the function

	// func llm compress result
	CompressData *string `json:"compress_data,omitempty"`
}

Function holds the information about a function

type Identity

type Identity struct {
	// module id, must be unique within a repo
	ModPath `json:"ModPath" jsonschema:"description=the compiling module of the ast node, the format is {ModName} or {ModName}@{Version}"`

	// path id, must be unique within a module
	PkgPath `json:"PkgPath" jsonschema:"description=the namespace of the ast node"`

	// symbol id , must be unique within a package
	Name string `` /* 180-byte string literal not displayed */
}

Identity is the universal-unique for an ast node.

func NewIdentity

func NewIdentity(mod, pkg, name string) Identity

func NewIdentityFromString

func NewIdentityFromString(str string) (ret Identity)

func (Identity) CallName

func (i Identity) CallName() string

return packagename.name

func (Identity) Full

func (i Identity) Full() string

func (Identity) String

func (i Identity) String() string

return full packagepath.name

type Import

type Import struct {
	Alias *string `json:",omitempty"`
	Path  string  // raw path
}

func InserImport

func InserImport(ids []Import, id Import) []Import

func NewImport

func NewImport(alias *string, path string) Import

func (Import) Equals

func (i Import) Equals(other Import) bool

func (*Import) UnmarshalJSON

func (i *Import) UnmarshalJSON(data []byte) error

type Language

type Language string
const (
	Golang  Language = "go"
	Rust    Language = "rust"
	Cxx     Language = "cxx"
	Python  Language = "python"
	Unknown Language = ""
)

func NewLanguage

func NewLanguage(lang string) (l Language)

func (Language) String

func (l Language) String() string

type ModPath

type ModPath = string

type Module

type Module struct {
	Language     Language
	Version      string
	Name         string               // go module name
	Dir          string               // relative path to repo
	Packages     map[PkgPath]*Package // pkage import path => Package
	Dependencies map[string]string    `json:",omitempty"`              // module name => module_path@version
	Files        map[string]*File     `json:",omitempty"`              // relative path => file info
	CompressData *string              `json:"compress_data,omitempty"` // module compress info
}

func NewModule

func NewModule(name string, dir string, language Language) *Module

func (Module) GetFile

func (m Module) GetFile(path string) *File

func (Module) GetPkgFiles

func (m Module) GetPkgFiles(pkg PkgPath) []*File

func (Module) IsExternal

func (m Module) IsExternal() bool

func (Module) SetFile

func (m Module) SetFile(path string, file *File)

type Node

type Node struct {
	// unique identity of the node
	Identity
	// Node Type, must be one of FUNC, TYPE, VAR
	Type NodeType
	// other nodes that  depends on this node
	Dependencies []Relation `json:",omitempty"`
	// other nodes that references this node
	References []Relation `json:",omitempty"`
	// other nodes this node implements
	Implements []Relation `json:",omitempty"`
	// other nodes this node inherits
	Inherits []Relation `json:",omitempty"`
	// other nodes in the same definition group
	Groups []Relation `json:",omitempty"`
	// the repo that this node belongs to
	Repo *Repository `json:"-"`
}

an Entity in a language

func NewNode

func NewNode(id Identity, typ NodeType, repo *Repository) *Node

func (Node) CompressData

func (n Node) CompressData() *string

func (Node) Content

func (n Node) Content() string

func (Node) FileLine

func (n Node) FileLine() FileLine

func (Node) GetDependency

func (n Node) GetDependency(id Identity) *Relation

func (Node) IsExported

func (n Node) IsExported() bool

func (Node) Module

func (n Node) Module() *Module

func (Node) SetCompressData

func (n Node) SetCompressData(data string) bool

func (Node) SetContent

func (n Node) SetContent(content string) bool

func (Node) SetFileLine

func (n Node) SetFileLine(file FileLine)

func (Node) SetIsExported

func (n Node) SetIsExported(isExported bool)

func (Node) SetIsMethod

func (n Node) SetIsMethod(isMethod bool)

func (Node) Signature

func (n Node) Signature() string

Signature returns the signature of the node:

  • for function, return the function signature
  • for var, return the var full content
  • for type, return the type full content

type NodeGraph

type NodeGraph map[string]*Node

Node ID (string) => Node Node ID comes from Identity.Full()

type NodeType

type NodeType int

Node Type

const (
	UNKNOWN NodeType = iota
	// top Function、 methods
	FUNC
	// Struct、TypeAlias、Enum...
	TYPE
	// Global Varable or Global Const
	VAR
)

func NewNodeType

func NewNodeType(typ string) NodeType

func (NodeType) MarshalJSON

func (t NodeType) MarshalJSON() ([]byte, error)

func (NodeType) String

func (t NodeType) String() string

func (*NodeType) UnmarshalJSON

func (t *NodeType) UnmarshalJSON(b []byte) error

type Package

type Package struct {
	IsMain bool
	IsTest bool
	PkgPath
	Functions    map[string]*Function // Function name (may be {{func}} or {{struct.method}}) => Function
	Types        map[string]*Type     // type name => type define
	Vars         map[string]*Var      // var name => var define
	CompressData *string              `json:"compress_data,omitempty"` // package compress info
}

Package

func NewPackage

func NewPackage(pkgPath PkgPath) *Package

type Parser

type Parser interface {
	ParseRepo() (Repository, error)
	ParseNode(pkgPath string, name string) (Repository, error)
	ParsePackage(pkgPath PkgPath) (Repository, error)
}

type PkgPath

type PkgPath = string

PkgPath is the import path of a package, it is either absolute path or url

type Receiver

type Receiver struct {
	IsPointer bool
	Type      Identity
}

type Relation

type Relation struct {
	// Kind of the relation
	Kind RelationKind
	// target node
	Identity
	// start line-offset of the target token related to the current node's codes
	Line int `json:",omitempty"`
	// information about this relation
	Desc *string `json:",omitempty"`
	// related codes representing this relation, comming from current node's codes
	Codes *string `json:",omitempty"`
}

Relation between two nodes

func InsertRelation

func InsertRelation(ids []Relation, id Relation) []Relation

type RelationKind

type RelationKind string

RelationKind

const (
	// DEPENDENCY: the target node is a dependency of the current node
	DEPENDENCY RelationKind = "Dependency"
	// IMPLEMENT: the target node is implemented by  the current node
	IMPLEMENT RelationKind = "Implement"
	// INHERIT: the target node is inherited by the current node
	INHERIT RelationKind = "Inherit"
	// GROUPT: the target is in same definition group of nodes, like `const(a=1,b=2)`
	GROUP RelationKind = "Group"
)

type Repository

type Repository struct {
	ASTVersion string
	Name       string             `json:"id"` // module name
	Path       string             // repo path
	Modules    map[string]*Module // module name => module
	Graph      NodeGraph          // node id => node
}

Repository

func LoadRepo

func LoadRepo(path string) (*Repository, error)

func NewRepository

func NewRepository(name string) Repository

NOTICE: Repository.Path is set as name by default, if th name isn't a path, set path somewhere

func (*Repository) AddRelation

func (r *Repository) AddRelation(node *Node, dep Identity, depFl FileLine, kinds ...RelationKind)

func (*Repository) AllNodesSetRepo

func (r *Repository) AllNodesSetRepo()

func (*Repository) BuildGraph

func (r *Repository) BuildGraph() error

func (Repository) GetFile

func (p Repository) GetFile(fp string) (*File, *Module)

func (Repository) GetFileNodes

func (r Repository) GetFileNodes(path string) []*Node

func (Repository) GetFunction

func (p Repository) GetFunction(id Identity) *Function

GetFunction the function identified by id. if id indicates a method, it will try traceinto inlined sub structs to get the named method

func (*Repository) GetModule

func (r *Repository) GetModule(mod ModPath) *Module

func (*Repository) GetNode

func (r *Repository) GetNode(id Identity) *Node

func (*Repository) GetPackage

func (r *Repository) GetPackage(mod ModPath, pkg PkgPath) *Package

func (Repository) GetType

func (p Repository) GetType(id Identity) *Type

func (*Repository) GetVar

func (p *Repository) GetVar(id Identity) *Var

func (Repository) ID

func (r Repository) ID() string

func (Repository) InternalModules

func (r Repository) InternalModules() []*Module

func (*Repository) SetFunction

func (p *Repository) SetFunction(id Identity, f *Function) *Function

func (*Repository) SetModule

func (p *Repository) SetModule(path string, mod *Module)

func (*Repository) SetNode

func (r *Repository) SetNode(id Identity, typ NodeType) *Node

NOTICE: if entity not exist, only set the node on graph

func (*Repository) SetType

func (p *Repository) SetType(id Identity, f *Type) *Type

func (*Repository) SetVar

func (p *Repository) SetVar(id Identity, v *Var) *Var

type Type

type Type struct {
	Exported bool // if the struct is exported

	TypeKind TypeKind // type Kind: Struct / Interface / Typedef

	Identity // unique id in a repo
	FileLine
	Content string // struct declaration content

	// field type, type name => type id
	SubStruct []Dependency `json:",omitempty"`

	// inherit field type
	InlineStruct []Dependency `json:",omitempty"`

	// methods defined on the Struct, not including inlined type's method
	Methods map[string]Identity `json:",omitempty"`

	// Implemented interfaces
	Implements []Identity `json:",omitempty"`

	CompressData *string `json:"compress_data,omitempty"` // struct llm compress result
}

Type holds the information about a struct

type TypeKind

type TypeKind string
const (
	TypeKindStruct    TypeKind = "struct"
	TypeKindInterface TypeKind = "interface"
	TypeKindTypedef   TypeKind = "typedef"
	TypeKindEnum      TypeKind = "enum"
)

func (*TypeKind) UnmarshalJSON

func (t *TypeKind) UnmarshalJSON(data []byte) error

type Var

type Var struct {
	IsExported bool

	IsConst   bool
	IsPointer bool // if its Type is a pointer type
	Identity
	FileLine
	Type         *Identity `json:",omitempty"`
	Content      string
	Dependencies []Dependency `json:",omitempty"`
	// Groups means the var is a group of vars, like Enum in Go
	Groups []Identity `json:",omitempty"`

	CompressData *string `json:"compress_data,omitempty"`
}

type Writer

type Writer interface {
	// write a module onto Options.OutDir.
	WriteModule(repo *Repository, modPath string, outDir string) error

	// CreateFile will create a file bytes with the given file info.
	CreateFile(fi *File, mod *Module) ([]byte, error)

	// SplitImportsAndCodes will split the imports and codes from the src.
	// the src has only codes, just return the src.
	SplitImportsAndCodes(src string) (codes string, imports []Import, err error)

	// IdToImport converts the identity to import.
	IdToImport(id Identity) (Import, error)

	// PatchImports patches the imports into file content
	PatchImports(impts []Import, file []byte) ([]byte, error)
}

Jump to

Keyboard shortcuts

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