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 ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 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 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 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.
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
}
Token is a single lexeme: a Kind plus its source span. KeywordID is set only when Kind == Keyword, giving the specific keyword text.