libs

package
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2026 License: Apache-2.0 Imports: 30 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package libs loads SysML/KerML standard-library files (bundled via embed.FS or overridden on disk) and maintains a persistent cache of their indexed symbols. See spec section 10.

Index

Constants

View Source
const LibraryPathEnvVar = "OPENSYSML_LIBRARY_PATH"

LibraryPathEnvVar names the directory to load the standard library from instead of the embedded copy. The legacy SYSML_LIBRARY_PATH name remains accepted; the OPENSYSML_ name wins when both are set.

Variables

View Source
var ErrSnapshotStale = errors.New("libs: library snapshot does not match the library files")

ErrSnapshotStale reports a snapshot built from different library files, or in a different format, than the ones in hand.

Functions

func BuildSnapshot added in v0.4.3

func BuildSnapshot(src Source) ([]byte, error)

BuildSnapshot loads src into a fresh index the way a process without a snapshot would, freezes it, and returns the snapshot bytes for it. The result is a function of the files alone: the same files give the same bytes.

func DecodeSnapshot added in v0.4.3

func DecodeSnapshot(data []byte, digest string) (*symbols.Index, error)

DecodeSnapshot rebuilds the frozen index a snapshot holds, provided it was built from files with the given set digest (see Loader.setDigest) in the current format; otherwise it reports ErrSnapshotStale.

func LoadInto added in v0.1.2

func LoadInto(idx *symbols.Index)

LoadInto loads every standard library file into idx, using the symbol cache when it is available. A failure is non-fatal: it is logged, and the index stays usable for a model that does not depend on the file that failed.

func NewModelIndex added in v0.2.0

func NewModelIndex() *symbols.Index

NewModelIndex returns an index for one model to add its documents to: an overlay over the shared standard library, which the model reads but cannot write to.

func SharedBase added in v0.2.0

func SharedBase() *symbols.Index

SharedBase returns the frozen index holding the standard library, built on first use and shared by every model afterwards: the library is the same for every model and immutable once loaded, so one copy serves them all.

func SnapshotIndex added in v0.4.3

func SnapshotIndex() (*symbols.Index, error)

SnapshotIndex returns the frozen library index decoded from the embedded snapshot, when the snapshot was built from exactly the files DefaultSource yields. It fails with ErrSnapshotStale when it was not — an edited bundled file, a LibraryPathEnvVar override with other content — and the caller loads the files instead.

func Text added in v0.6.0

func Text(src Source) source.Lookup

Text answers spans of the library files src serves, by the names an index built from src holds them under; nil for a nil src. Each file is read once.

func TierOf added in v0.5.0

func TierOf(name string) symbols.LibraryTier

TierOf classifies a library file by the directory of the bundle it is under, as a Source names it; a file outside the bundle's layout is of no stated tier.

func Version added in v0.8.0

func Version() string

Version identifies the standard library the host loads: `sha256:` and the set digest of every file of DefaultSource, so two hosts embedding the same files report the same version and an edited or overridden library reports another.

Types

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

Cache persists reduced library IndexRecords to disk keyed by content hash and format version. A hit lets the loader skip lexing/parsing entirely. The same mechanism serves git dependencies (Plan 5c) since it keys purely on content.

func NewCache

func NewCache() (*Cache, error)

NewCache resolves the cache directory ($XDG_CACHE_HOME or os.UserCacheDir), appends "sysml-ls/libs", creates it, and returns a ready Cache.

func (*Cache) Load

func (c *Cache) Load(key string) (*IndexRecord, bool)

Load returns the cached record for key, or (nil, false) on any miss (absent file, read error, or decode error — all treated as a benign miss).

func (*Cache) Prune

func (c *Cache) Prune()

Prune removes records that no load has hit for maxIdleAge, and the temp files a crashed store left behind. Cache entries are otherwise never invalidated explicitly — a key that no longer matches is simply missed — so without this the directory keeps every record any library version ever produced.

func (*Cache) Store

func (c *Cache) Store(key string, rec *IndexRecord) error

Store gob-encodes rec and writes it to <dir>/<key>.idx atomically: it writes a temp file of its own then renames it into place, so a concurrent Load or a crash never observes a partially written file, and two writers of one key cannot truncate or publish each other's temp. A failed encode/write removes the temp and leaves any existing final file untouched.

type IndexRecord

type IndexRecord struct {
	Name  string
	Facts []factRecord
}

IndexRecord is the derived analysis persisted for one library document.

type Loader

type Loader struct {

	// RequireResolved makes LoadAll cache no document with an unresolved
	// specialization target, since a key does not describe the index a record was
	// built in.
	RequireResolved bool
	// contains filtered or unexported fields
}

Loader loads library files from a Source and registers them into a target index, using a Cache to skip the derivation a load would otherwise repeat. Every file is parsed and indexed on every load path, so a library contributes its declarations whether or not the cache held anything for it; a record carries only the derived facts whose derivation dominates a cold load.

func NewLoader

func NewLoader(src Source, cache *Cache) *Loader

NewLoader returns a Loader over src, using cache for persistence.

func (*Loader) Hits added in v0.2.0

func (l *Loader) Hits() int

Hits reports how many documents of the last LoadAll had their facts installed from a cache record instead of derived, which is how a caller tells a warm load from a cold one.

func (*Loader) LoadAll added in v0.2.0

func (l *Loader) LoadAll(idx *symbols.Index) error

LoadAll registers every file of the source into idx as library content and installs the derived facts of each. It loads the whole set at once because a record holds supertype names a sibling file may declare; an incomplete library is reported and not cached.

Hashing and parsing are per file and pure, so the files are hashed and parsed concurrently; the index is then written in source order, so the result is the one a serial load produces.

type Source

type Source interface {
	// List returns the relative paths of available library files, sorted.
	List() []string
	// Read returns the bytes of the named library file, or an error.
	Read(name string) ([]byte, error)
}

Source yields standard-library file contents by relative path (e.g. "Kernel Libraries/Kernel Data Type Library/ScalarValues.kerml").

func DefaultSource

func DefaultSource() Source

DefaultSource returns a dirSource rooted at LibraryPathEnvVar when that environment variable is set and non-empty, otherwise the embedded source.

func EmbeddedSource added in v0.4.3

func EmbeddedSource() Source

EmbeddedSource returns the bundled copy of the standard library, whatever LibraryPathEnvVar says: it is what the embedded snapshot is generated from.

func FrozenLibrary added in v0.4.3

func FrozenLibrary() (*symbols.Index, Source)

FrozenLibrary loads a frozen index holding the standard library, together with a source serving exactly the bytes it was built from and nothing else (see SharedLibrary). Unlike SharedLibrary it loads on every call.

func NewDirSource

func NewDirSource(dir string) Source

NewDirSource returns a Source that reads .kerml/.sysml files from dir.

func SharedLibrary added in v0.5.0

func SharedLibrary() (*symbols.Index, Source)

SharedLibrary returns the shared frozen index (see SharedBase) together with the source serving the bytes it was built from. The source answers only for the files the index holds, and with the text they had when it was built, so a span the index carries always addresses the text the source serves — even when a LibraryPathEnvVar override is edited while the process runs.

Directories

Path Synopsis
Command gensnapshot writes the bundled library's snapshot, the derived artifact package libs embeds; run through `go generate ./internal/core/libs`.
Command gensnapshot writes the bundled library's snapshot, the derived artifact package libs embeds; run through `go generate ./internal/core/libs`.

Jump to

Keyboard shortcuts

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