lexer

package
v0.10.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 11, 2026 License: Apache-2.0 Imports: 2 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

func ContextualWords(kind source.Kind) []string

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

func InvalidEscapes(tok source.Span, raw string) []source.Span

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

func IsDecimalValue(s string) bool

IsDecimalValue reports whether s is exactly one DECIMAL_VALUE token (KerML §8.2.2): digits alone, so no sign, point or exponent.

func IsRealValue

func IsRealValue(s string) bool

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 / unterminated literal
)

func (Kind) String

func (k Kind) String() string

String returns a human-readable name for the kind.

type Lexer

type Lexer struct {
	// contains filtered or unexported fields
}

Lexer is a hand-written, pull-based scanner. Call Next() repeatedly.

func New

func New(sf *source.SourceFile) *Lexer

New creates a Lexer over a source file.

func (*Lexer) Next

func (lx *Lexer) Next() Token

Next returns the next token, including trivia tokens. At end of input it returns EOF repeatedly (idempotent).

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.

func (Token) IsTrivia

func (t Token) IsTrivia() bool

IsTrivia reports whether the token is hidden trivia (skipped by the parser). WS, SLNote, MLNote are hidden. RegularComment is NOT hidden.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL