Documentation
¶
Overview ¶
Package lexer implements a hand-written scanner for SysML v2 and KerML.
The lexer tokenizes source text into a stream of tokens with full position tracking. It handles the ~200 SysML keywords, multi-character operators, string/number/infinity literals, comments (both line and block), and whitespace (tracked as leading/trailing trivia).
Key types:
- Token: a single lexical unit (kind, text, span, trivia)
- TokenKind: categorizes tokens (Keyword, Identifier, Number, String, Operator, etc.)
- Lexer: stateful scanner with Next() method
The lexer supports error recovery: invalid characters produce ErrorToken but scanning continues. All keywords are case-sensitive and pre-registered in keywords.go.
Package lexer is a hand-written pull-based scanner for SysML v2 / KerML.
Index ¶
- func CommentBody(raw string) string
- func ContextualWords(kind source.Kind) []string
- func InvalidEscapes(tok source.Span, raw string) []source.Span
- func IsDecimalValue(s string) bool
- func IsEscapeChar(c byte) bool
- func IsIdentifier(s string) bool
- func IsKeyword(s string) bool
- func IsKeywordIn(s string, kind source.Kind) bool
- func IsRealValue(s string) 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 StringText(value string) string
- func StringValue(raw string) string
- func UnrestrictedNameText(value string) string
- type Kind
- type Lexer
- type Token
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CommentBody ¶ added in v0.6.0
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 ContextualWords ¶ added in v0.2.0
ContextualWords returns the contextual words of a language, in name order. KindUnknown gets the union, so a surface with no file name still offers them.
func InvalidEscapes ¶ added in v0.8.0
InvalidEscapes locates the backslash escapes in the quoted token text raw (spanning tok) that its terminal does not admit, each span covering the backslash and the character after it.
func IsDecimalValue ¶ added in v0.5.0
IsDecimalValue reports whether s is exactly one DECIMAL_VALUE token (KerML §8.2.2): digits alone, so no sign, point or exponent.
func IsEscapeChar ¶ added in v0.8.0
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 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 ¶ added in v0.2.0
IsKeywordIn reports whether s is reserved by one language's grammar.
func IsRealValue ¶ added in v0.5.0
IsRealValue reports whether s is exactly one REAL_VALUE token (KerML §8.2.2): an unsigned number with a fractional part or an exponent, as `1.5`, `.5`, `1e3`.
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 ¶ added in v0.8.0
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 ¶ added in v0.8.0
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 StringText ¶ added in v0.5.0
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 ¶ added in v0.4.3
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 ¶ added in v0.5.0
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 enumerates token categories.
const ( Invalid Kind = iota EOF // Literals & names Identifier // ID: [A-Za-z_][A-Za-z_0-9]* UnrestrictedName // '...' String // "..." Decimal // [0-9]+ Real // 1.5, .5, 1e3, 1.5e-2 // Trivia Whitespace // WS (hidden) SLNote // // ... (hidden) MLNote // //* */ (hidden) RegularComment // /* */ (NOT hidden) // Punctuation / operators Question // ? QuestionQ // ?? Pipe // | Amp // & EqEq // == NotEq // != EqEqEq // === NotEqEq // !== At // @ AtAt // @@ Lt // < Gt // > Le // <= Ge // >= DotDot // .. Plus // + Minus // - Star // * Slash // / Percent // % StarStar // ** Caret // ^ Tilde // ~ Dot // . Hash // # LParen // ( RParen // ) LBracket // [ RBracket // ] Arrow // -> DotQuestion // .? Comma // , ColonColon // :: Dollar // $ Eq // = LBrace // { RBrace // } Semicolon // ; Colon // : ColonEq // := ColonGt // :> ColonGtGt // :>> ColonColonGt // ::> EqGt // => Keyword // generic keyword marker; specific keyword identity via Token.KeywordID Error // illegal char / unterminated literal )
type Lexer ¶
type Lexer struct {
// contains filtered or unexported fields
}
Lexer is a hand-written, pull-based scanner. Call Next() repeatedly.
type Token ¶
type Token struct {
Kind Kind
Span source.Span
KeywordID string // populated for Kind==Keyword
// Unterminated marks a comment or note the scanner ran to the end of the
// file without finding its closing "*/", so everything after the opener —
// possibly the rest of the document — is inside it.
Unterminated bool
// BadEscape marks a string or unrestricted name spelling a backslash escape
// outside the set its terminal admits; InvalidEscapes locates them.
BadEscape bool
}
Token is a single lexeme: a Kind plus its source span. KeywordID is set only when Kind == Keyword, giving the specific keyword text.