lexer

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2026 License: Apache-2.0 Imports: 3 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 added in v0.2.0

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 IsIdentifier

func IsIdentifier(s string) bool

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

func IsKeyword(s string) bool

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

func IsKeywordIn(s string, kind source.Kind) bool

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

func NameText(name string) string

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

func QualifiedNameText(fqn string) string

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
)

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
}

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