libs

package
v0.10.1 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: Apache-2.0 Imports: 34 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.

View Source
const RecordCacheEnvVar = "OPENSYSML_RECORD_CACHE"

RecordCacheEnvVar switches the interface-record cache off when set to 0 (or false/off/no): every document is then parsed and held loaded, and none of their records is written.

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.

View Source
var ErrUnrecordable = errors.New("libs: document interface cannot be recorded")

ErrUnrecordable reports a document whose interface cannot be written without its tree: a fact a reader needs has no name to restore it by. The document is held loaded instead.

Functions

func BuildSnapshot

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

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

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

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 RecordCacheFromEnv added in v0.9.2

func RecordCacheFromEnv() bool

RecordCacheFromEnv reports whether the environment leaves the record cache on, which it does unless RecordCacheEnvVar switches it off.

func SharedBase

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

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

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

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

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 NewCacheIn added in v0.9.2

func NewCacheIn(base string) (*Cache, error)

NewCacheIn is NewCache under the given base directory instead of the user's.

func OpenRecordCache added in v0.9.2

func OpenRecordCache(noCache bool) (*Cache, error)

OpenRecordCache is the record cache a command holds its closed documents through when the flag and the environment leave it on: the shared cache directory, nil when either switches it off or the directory cannot be made (the reason returned), in which case every document is held loaded.

func (*Cache) InterfaceKey added in v0.9.2

func (c *Cache) InterfaceKey(name string, content []byte, libraryDigest string, mode diag.ConformanceMode) string

InterfaceKey derives the cache key of a document's interface record from its name and content, the digest of the library it was analyzed against, the conformance mode, the build and the record format version: a record written under any other is never found. The name is part of the key because the record names its document (two files of equal content are two documents).

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) LoadInterface added in v0.9.2

func (c *Cache) LoadInterface(key string) (*InterfaceRecord, bool)

LoadInterface returns the interface record stored under key, or (nil, false) on any 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.

func (*Cache) StoreInterface added in v0.9.2

func (c *Cache) StoreInterface(key string, rec *InterfaceRecord) error

StoreInterface writes rec under key, atomically (see Store).

type IndexRecord

type IndexRecord struct {
	Name  string
	Facts []factRecord
}

IndexRecord is the derived analysis persisted for one library document.

type InterfaceRecord added in v0.9.2

type InterfaceRecord struct {
	Name        string
	Kind        source.Kind
	Digest      string // of the content the record was written from, as the workspace fingerprints it
	Scope       *symbols.DocumentRecord
	Diagnostics []diag.Diagnostic
	Mode        diag.ConformanceMode // the diagnostics answer this mode's question
	// Provenance is where the analysis got its answers (see Provenance); the
	// record holds only where it is valid.
	Provenance *Provenance
	// Members are the document's top-level members as written, which a session
	// lists of a file it loaded.
	Members []TopMember
}

InterfaceRecord is a user document held as its interface: the scope tree another document can observe through the language, with facts in place of declarations, and the diagnostics its analysis reported. See docs/internals/interface-records.md.

func WriteInterface added in v0.9.2

func WriteInterface(name string, kind source.Kind, idx *symbols.Index, r *resolve.Resolver,
	model *semantics.Model, gathered *symbols.GatheredRelationships, diags []diag.Diagnostic) (*InterfaceRecord, error)

WriteInterface writes the interface record of the named document from the analysis that just ran over it: its scope tree in idx, the resolver and model that analysis read through, the relationships the workspace-wide audits gathered from its body, and the diagnostics it reported. It fails with ErrUnrecordable when a fact another document can observe has no fully-qualified name to restore it by.

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

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

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 Provenance added in v0.9.2

type Provenance struct {
	Reads resolve.Reads
	// Answerers maps each read that a workspace document took part in to those
	// documents, sorted; reads answered by the library alone are absent.
	Answerers map[string][]string
	// Digests are of every document named in Answerers.
	Digests map[string]string
}

Provenance is where the analysis an interface record was written from got its answers: what it read of the index (Reads) and, per read, the workspace documents that took part in the answer, with their content digests. A record holds only where those documents are as they were; anywhere else its diagnostics and evaluated facts may not follow from the documents around it, and the document is parsed instead (see Valid). Library documents are left out of it: the record's key already names the library as a whole.

func Attribute added in v0.9.2

func Attribute(reads resolve.Reads, src Sources) (*Provenance, error)

Attribute records where the reads were answered from, as src stands now, which is as it stood for the analysis that made them. Documents src has no digest for — the library's, and the versions of its files standing in for them — are not attributed: the record's key names the library as a whole. It fails when a read names shared state nothing of src's answers for: such a record could not tell where it holds.

func (*Provenance) Clone added in v0.9.2

func (p *Provenance) Clone() *Provenance

Clone is a deep copy of p, sharing nothing with it; nil clones to nil.

func (*Provenance) Valid added in v0.9.2

func (p *Provenance) Valid(src Sources) bool

Valid reports whether src answers every read as the analysis saw it: the same documents take part in each answer, and each has the content it had. A nil provenance holds nowhere.

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 BundledSource

func BundledSource() Source

BundledSource returns the standard library a process loads when no directory overrides it: the published text with the corrections declared in the errata package beside it applied on read. The embedded snapshot is generated from it. A registry that fails to load makes every read fail rather than serve the published text uncorrected.

func DefaultSource

func DefaultSource() Source

DefaultSource returns a dirSource rooted at LibraryPathEnvVar when that environment variable is set and non-empty, otherwise BundledSource. A directory override is read as it stands: its text is the caller's to correct.

func EmbeddedSource

func EmbeddedSource() Source

EmbeddedSource returns the standard library exactly as OMG published it, whatever LibraryPathEnvVar says. It is the text the declared errata are verified against; BundledSource is what a process loads.

func FrozenLibrary

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 FrozenLibraryFrom

func FrozenLibraryFrom(published Source) (*symbols.Index, Source)

FrozenLibraryFrom is FrozenLibrary over the given source, for a consumer judging a model against a library other than the one DefaultSource names, such as the published text without its declared errata (EmbeddedSource).

func NewDirSource

func NewDirSource(dir string) Source

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

func SharedLibrary

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.

type Sources added in v0.9.2

type Sources struct {
	Index *symbols.Index
	// Contributors names the workspace documents the shared state called name
	// is built from, false when no shared state answers for name (see
	// passes.Contributors).
	Contributors func(name string) ([]string, bool)
	// Digest reports the content digest of a workspace document, false when
	// the workspace holds none of the name.
	Digest func(doc string) (string, bool)
	// Library names the library document a workspace document stands in for,
	// "" for one that stands in for none (see model.Workspace.StandsInFor).
	Library func(doc string) string
	// contains filtered or unexported fields
}

Sources is what a Provenance is attributed against or checked against: the index, the shared state its readers consulted, and the digest of each workspace document.

func NewSources added in v0.9.2

func NewSources(index *symbols.Index, contributors func(string) ([]string, bool), digest func(string) (string, bool), library func(string) string) Sources

NewSources is a Sources over index, contributors, digest and library as they stand now, memoizing the index's answers: use one for every record checked or attributed against the same state, and a fresh one once the index changes. A nil library is one no document stands in for.

type TopMember added in v0.9.2

type TopMember struct {
	Kind      string
	Name      string
	ShortName string
	// Target is what an import names, wildcards included.
	Target string
	Span   source.Span
}

TopMember is what a document's top-level member is, as its notation states it, which is what a session lists of a file it loaded whether the file is held loaded or as its record. Kind is the keyword the member is written with ("package", "part def", "import", "comment"), empty for a member the notation has no keyword of its own for.

func TopMemberOf added in v0.9.2

func TopMemberOf(m ast.Node) TopMember

TopMemberOf is TopMembers over one top-level member.

func TopMembers added in v0.9.2

func TopMembers(root *ast.RootNamespace) []TopMember

TopMembers reports root's top-level members in declaration order; nil for a nil root.

Directories

Path Synopsis
Package errata is the declared overlay of defects in OMG-published material, and the entries for the bundled standard library; published bytes are never rewritten.
Package errata is the declared overlay of defects in OMG-published material, and the entries for the bundled standard library; published bytes are never rewritten.

Jump to

Keyboard shortcuts

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