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 CommentProse(raw string) string
- func CommentText(body, indent string) (string, bool)
- func Dir(name string) string
- func FileNamed(doc string, _ Span) string
- func IsEscapeChar(c byte) bool
- func IsFile(name string) 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 MemberPathOf(names []string) string
- func MemberPathSegments(text string) ([]string, bool)
- func MetadataPathSegments(path string) (definition []string, feature string, ok bool)
- 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 Unescape(raw string) string
- func UnrestrictedNameText(value string) string
- type Kind
- type LineIndex
- type Locate
- type Lookup
- type Pos
- type SourceFile
- type Span
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CommentBody ¶
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
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
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 FileNamed ¶ added in v0.9.2
FileNamed is the Locate for documents named after the file they hold: the name itself when IsFile, else "".
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 IsFile ¶ added in v0.9.2
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 ¶
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 MemberPathOf ¶ added in v0.9.2
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
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
MetadataPathSegments splits a metadata path into the metadata definition's segments and the feature name.
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 Unescape ¶ added in v0.9.2
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 ¶
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 source is parsed as. 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 Locate ¶ added in v0.9.2
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 ¶
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.