source

package
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2026 License: Apache-2.0 Imports: 7 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 source provides source file management and text indexing for the SysML v2 parser, and the notation's text forms that every layer above it reads and writes.

This package defines SourceFile, which represents a single input file (either .sysml or .kerml), along with Span and Position types for tracking locations in source text. LineIndex provides efficient line/column lookups for error reporting and IDE features.

It also holds the KerML and SysML keyword sets and the helpers that read and write notation text without a parse: names (NameText, QualifiedNameText), string values (StringValue, StringText) and comment bodies (CommentBody).

All source locations are represented as byte offsets; LineIndex converts between offsets and user-facing line:column positions on demand.

Package source owns file content and byte-offset span types.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CommentBody

func CommentBody(raw string) string

CommentBody is the body a REGULAR_COMMENT gives its Comment, Documentation or TextualRepresentation (KerML 1.1 §8.2.3.3.2 Note 1): the delimiters come off, then the white space after `/*` up to and including the first line terminator; each later line loses its initial white space, then a `*`, then one space. All other text, line breaks and white space stay as entered. A `\r\n` or `\r` line terminator reads as `\n`.

func CommentProse added in v0.9.2

func CommentProse(raw string) string

CommentProse reads a comment or note token as display prose: the `/* */`, `/** */`, `//* */` or `//` delimiters come off, as does each line's indentation and the `*` margin a block comment runs down its left edge. Lines keep their breaks.

func CommentText added in v0.9.2

func CommentText(body, indent string) (string, bool)

CommentText writes a REGULAR_COMMENT whose CommentBody is body, its later lines opening with the ` * ` margin under indent. False for a body no comment carries: `*/` would close it, and `\r` is not a line break it reads back.

func Dir added in v0.9.2

func Dir(name string) string

Dir is the directory of a source file's name when IsFile, else "".

func FileNamed added in v0.9.2

func FileNamed(doc string, _ Span) string

FileNamed is the Locate for documents named after the file they hold: the name itself when IsFile, else "".

func IsEscapeChar

func IsEscapeChar(c byte) bool

IsEscapeChar reports whether c may follow a backslash in a STRING_VALUE or UNRESTRICTED_NAME (KerMLExpressions.xtext:562-566): b t n f r " ' \.

func IsFile added in v0.9.2

func IsFile(name string) bool

IsFile reports whether a source file's name is a path on disk rather than empty, a `<placeholder>` such as standard input's, or a URI.

func IsIdentCont

func IsIdentCont(c byte) bool

IsIdentCont reports whether c may continue a basic name: a letter, digit or `_`.

func IsIdentStart

func IsIdentStart(c byte) bool

IsIdentStart reports whether c may open a basic name: a letter or `_`.

func IsIdentifier

func IsIdentifier(s string) bool

IsIdentifier reports whether s can be written as a basic name (KerML §8.2.2); a name that cannot needs the quotes of an unrestricted name.

func IsKeyword

func IsKeyword(s string) bool

IsKeyword reports whether s is a KerML or SysML keyword, and so cannot be used where a plain identifier is expected in either language.

func IsKeywordIn

func IsKeywordIn(s string, kind Kind) bool

IsKeywordIn reports whether s is reserved by one language's grammar.

func Keywords

func Keywords() []string

Keywords returns a copy of the KerML+SysML keyword list, for tooling (e.g. LSP completion). The returned slice is safe to mutate.

func MemberPathOf added in v0.9.2

func MemberPathOf(names []string) string

MemberPathOf writes the segments of a member path, outermost first, joined by '.', each quoted on its own like a qualified name segment.

func MemberPathSegments added in v0.9.2

func MemberPathSegments(text string) ([]string, bool)

MemberPathSegments reads a member path — `.`-joined names, each a basic or a 'quoted name' — back into its names, quotes dropped and escapes kept; false for malformed text. A bare name is a one-segment path.

func MetadataPathSegments added in v0.9.2

func MetadataPathSegments(path string) (definition []string, feature string, ok bool)

MetadataPathSegments splits a metadata path into the metadata definition's segments and the feature name.

func NameText

func NameText(name string) string

NameText writes a name as the notation spells it: a name that is not a basic name — one holding a space or punctuation, one spelling a keyword, an empty one — gets the quotes of an unrestricted name back (KerML §8.2.2). The text is quoted as it stands, since a name parsed from the notation still carries the escapes it was written with, so quoting is the exact inverse of parsing.

func QualifiedNameOf

func QualifiedNameOf(names []string) string

QualifiedNameOf writes names, outermost first, as one qualified name, each quoted on its own; a name may hold `::`, so the text is read back exactly.

func QualifiedNameSegments

func QualifiedNameSegments(text string) ([]string, bool)

QualifiedNameSegments reads a qualified name back into its names, `'x::y'` one and `x::y` two, quotes dropped and escapes kept; false for malformed text.

func QualifiedNameText

func QualifiedNameText(fqn string) string

QualifiedNameText writes a qualified name segment by segment, since each segment is a name of its own and is quoted on its own. An empty name is written as it stands.

func ReferenceEndNames

func ReferenceEndNames(text string) string

ReferenceEndNames rewrites a typing's references — `, ` apart, each a qualified name or feature chain, `$::` led or `~` conjugated — by the name each ends in, `~` kept; text that does not read as such is returned as it is.

func ReferenceQualifiedNames

func ReferenceQualifiedNames(text string) [][]string

ReferenceQualifiedNames reads a typing's references — `, ` apart, `$::` led or `~` conjugated — into each one's names; a feature chain, or text that does not read as references, contributes none.

func ReplaceFile

func ReplaceFile(source, target string) error

ReplaceFile renames source over target, atomically where the platform allows. Where the platform refuses a rename over what is at target, it retries after removing that; any other failure — the source missing, another device — leaves the target as it was. A target that is a link is replaced, never followed.

func StringText

func StringText(value string) string

StringText writes a value as the double-quoted STRING_VALUE token that StringValue reads back to it, escaping the quote, the backslash and the control characters KerML §8.2.2 names; everything else stands as it is.

func StringValue

func StringValue(raw string) string

StringValue reads the text a STRING_VALUE token spells: the quotes come off and the backslash escapes KerML §8.2.2 defines stand for the characters they name. A backslash before anything else (rejected by the scanner) stands for that character itself.

func Unescape added in v0.9.2

func Unescape(raw string) string

Unescape reads the backslash escapes KerML §8.2.2 defines as the characters they name; a backslash before anything else stands for that character itself, and text with no backslash stands as it is.

func UnrestrictedNameText

func UnrestrictedNameText(value string) string

UnrestrictedNameText writes raw text as the quoted unrestricted name whose value is that text, escaping the quote, the backslash and the control characters.

Types

type Kind

type Kind int

Kind is the language a file is written in. The two languages share a lexer but not a grammar: KerML notation in a SysML file is diagnosed.

const (
	// KindUnknown is a name with neither model extension.
	KindUnknown Kind = iota
	// KindSysML is a `.sysml` file, whose grammar is SysML.xtext.
	KindSysML
	// KindKerML is a `.kerml` file, whose grammar is KerML.xtext.
	KindKerML
)

func KindOf

func KindOf(name string) Kind

KindOf reports the language of a file name by its extension.

func (Kind) String

func (k Kind) String() string

String returns the language's name.

type LineIndex

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

LineIndex maps byte offsets to 1-based line/column positions. lineStarts[i] is the byte offset where line (i+1) begins.

func NewLineIndex

func NewLineIndex(content []byte) *LineIndex

NewLineIndex builds the index of content's line starts.

func (*LineIndex) OffsetAt

func (li *LineIndex) OffsetAt(p Pos) int

OffsetAt returns the byte offset for a 1-based line/col.

func (*LineIndex) PosAt

func (li *LineIndex) PosAt(offset int) Pos

PosAt returns the 1-based line/col for a byte offset. Col counts bytes from the line start + 1.

type Locate added in v0.9.2

type Locate func(doc string, span Span) string

Locate answers the file on disk a span of the named document was read from, or "" for text that came from no file: a buffer, standard input, a library.

type Lookup

type Lookup func(doc string, span Span) string

Lookup answers the text a span of the named document covers, or "" for a document it does not hold.

func TextOf

func TextOf(files map[string]*SourceFile, next Lookup) Lookup

TextOf answers spans from files by document name, shadowing next; a name not among files goes to next, "" when next is nil.

type Pos

type Pos struct {
	Line int
	Col  int
}

Pos is a 1-based line/column location.

type SourceFile

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

SourceFile owns the raw bytes of one source file.

func New

func New(name string, content []byte) *SourceFile

New creates a SourceFile from a name and its raw bytes.

func NewWithKind

func NewWithKind(name string, content []byte, kind Kind) *SourceFile

NewWithKind creates a source file whose language is explicit rather than inferred from its name. This is used for inline KerML content.

func (*SourceFile) Bytes

func (sf *SourceFile) Bytes() []byte

Bytes returns the raw content (do not mutate).

func (*SourceFile) Kind

func (sf *SourceFile) Kind() Kind

Kind reports the language of this file.

func (*SourceFile) Len

func (sf *SourceFile) Len() int

Len returns the byte length of the content.

func (*SourceFile) Lines

func (sf *SourceFile) Lines() *LineIndex

Lines returns the cached line index for this file, building it on first use. Col is a byte column (1-based). LSP requires UTF-16 code-unit columns; that conversion is an LSP-layer concern (Plan 06), not the source package.

func (*SourceFile) Name

func (sf *SourceFile) Name() string

Name returns the file name.

func (*SourceFile) Text

func (sf *SourceFile) Text(sp Span) string

Text returns the substring covered by the span. Spans are taken from one cached copy of the content, so a span costs no allocation of its own.

type Span

type Span struct {
	Offset int
	Len    int
}

Span is a byte range within a SourceFile: [Offset, Offset+Len).

func (Span) Contains

func (s Span) Contains(other Span) bool

Contains reports whether other lies within s.

func (Span) End

func (s Span) End() int

End returns the exclusive end offset.

Jump to

Keyboard shortcuts

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