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 ¶
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 ¶
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 ¶
IsDecimalValue reports whether s is exactly one DECIMAL_VALUE token (KerML §8.2.2): digits alone, so no sign, point or exponent.
func IsRealValue ¶
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`.
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 )
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; and a string or
// unrestricted name whose closing quote the file never has, cut at the end
// of the line it opens on.
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.