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 ¶
- func CommentBody(raw string) string
- func IsEscapeChar(c byte) bool
- func IsIdentCont(c byte) bool
- func IsIdentStart(c byte) bool
- func IsIdentifier(s string) bool
- func IsKeyword(s string) bool
- func IsKeywordIn(s string, kind Kind) bool
- func Keywords() []string
- func NameText(name string) string
- func QualifiedNameOf(names []string) string
- func QualifiedNameSegments(text string) ([]string, bool)
- func QualifiedNameText(fqn string) string
- func ReferenceEndNames(text string) string
- func ReferenceQualifiedNames(text string) [][]string
- func ReplaceFile(source, target string) error
- func StringText(value string) string
- func StringValue(raw string) string
- func UnrestrictedNameText(value string) string
- type Kind
- type LineIndex
- type Lookup
- type Pos
- type SourceFile
- type Span
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CommentBody ¶
CommentBody reads the prose a comment token carries: 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 IsEscapeChar ¶
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 IsIdentCont ¶
IsIdentCont reports whether c may continue a basic name: a letter, digit or `_`.
func IsIdentStart ¶
IsIdentStart reports whether c may open a basic name: a letter or `_`.
func IsIdentifier ¶
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 ¶
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 ¶
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 NameText ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 UnrestrictedNameText ¶
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.
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 ¶
NewLineIndex builds the index of content's line starts.
type Lookup ¶
Lookup answers the text a span of the named document covers, or "" for a document it does not hold.
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) 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) 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.