Help us shape the future of Go! Take the 2026 Go Developer Survey

lex

package
v25.5.1 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const EOF = -1

EOF indicates the end of the an input.

Variables

This section is empty.

Functions

func HasUChars

func HasUChars(r rune, l *Lexer) bool

HasUChars returns whether the lexer is at the beginning of a escaped Unicode character. UCHAR ::= '\u' HEX HEX HEX HEX | '\U' HEX HEX HEX HEX HEX HEX HEX HEX

func HasXChars

func HasXChars(r rune, l *Lexer) bool

HasXChars returns whether the lexer is at the start of a escaped hexadecimal byte (e.g \xFE) XCHAR ::= '\x' HEX HEX

func IRIRef

func IRIRef(l *Lexer, styp ItemType) error

IRIRef emits an IRIREF or returns an error if the input is invalid.

func IsEndOfLine

func IsEndOfLine(r rune) bool

IsEndOfLine returns true if the rune is a Linefeed or a Carriage return.

Types

type CheckRune

type CheckRune func(r rune) bool

CheckRune is predicate signature for accepting valid runes on input.

type CheckRuneRec

type CheckRuneRec func(r rune, l *Lexer) bool

CheckRuneRec is like CheckRune with Lexer as extra argument. This can be used to recursively call other CheckRune(s).

type Item

type Item struct {
	Typ ItemType
	Val string
	// contains filtered or unexported fields
}

Item represents a unit emitted by the lexer.

func (Item) Errorf

func (i Item) Errorf(format string, args ...interface{}) error

Errorf returns an error message that includes the line and column where the error occurred.

func (Item) String

func (i Item) String() string

type ItemIterator

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

ItemIterator iterates over the items emitted by a lexer.

func (*ItemIterator) Errorf

func (p *ItemIterator) Errorf(format string, args ...interface{}) error

Errorf returns an error message using the location of the next item in the iterator.

func (*ItemIterator) Item

func (p *ItemIterator) Item() Item

Item returns the current item.

func (*ItemIterator) Next

func (p *ItemIterator) Next() bool

Next advances the iterator by one.

func (*ItemIterator) Peek

func (p *ItemIterator) Peek(num int) ([]Item, error)

Peek returns the next n items without consuming them.

func (*ItemIterator) PeekOne

func (p *ItemIterator) PeekOne() (Item, bool)

PeekOne returns the next 1 item without consuming it.

func (*ItemIterator) Prev

func (p *ItemIterator) Prev() bool

Prev moves the index back by one.

func (*ItemIterator) Restore

func (p *ItemIterator) Restore(pos int)

Restore restores the iterator to position specified.

func (*ItemIterator) Save

func (p *ItemIterator) Save() int

Save returns the current position of the iterator which we can use for restoring later.

type ItemType

type ItemType int

ItemType is used to set the type of a token. These constants can be defined in the file containing state functions. Note that their value should be >= 5.

const (
	// ItemEOF is emitted when the end of the input is reached.
	ItemEOF ItemType = iota
	// ItemError is emitted when there was an error lexing the input.
	ItemError
)

type Lexer

type Lexer struct {
	// NOTE: Using a text scanner wouldn't work because it's designed for parsing
	// Golang. It won't keep track of Start Position, or allow us to retrieve
	// slice from [Start:Pos]. Better to just use normal string.
	Input string // string being scanned.
	Start int    // Start Position of this item.
	Pos   int    // current Position of this item.
	Width int    // Width of last rune read from input.

	Depth      int     // nesting of {}
	BlockDepth int     // nesting of blocks (e.g. mutation block inside upsert block)
	ArgDepth   int     // nesting of ()
	Mode       StateFn // Default state to go back to after reading a token.
	Line       int     // the current line number corresponding to Start
	Column     int     // the current column number corresponding to Start
	// contains filtered or unexported fields
}

Lexer converts a raw input into tokens.

func (*Lexer) AcceptRun

func (l *Lexer) AcceptRun(c CheckRune) (lastr rune, validr bool)

AcceptRun accepts tokens based on CheckRune until it returns false or EOF is reached. Returns last rune accepted and valid flag for rune.

func (*Lexer) AcceptRunRec

func (l *Lexer) AcceptRunRec(c CheckRuneRec)

AcceptRunRec accepts tokens based on CheckRuneRec until it returns false or EOF is reached.

func (*Lexer) AcceptRunTimes

func (l *Lexer) AcceptRunTimes(c CheckRune, times int) int

AcceptRunTimes accepts tokens with CheckRune given number of times. returns number of times it was successful.

func (*Lexer) AcceptUntil

func (l *Lexer) AcceptUntil(c CheckRune)

AcceptUntil accepts tokens based on CheckRune till it returns false or EOF is reached.

func (*Lexer) Backup

func (l *Lexer) Backup()

Backup moves the lexer back to its previous position.

func (*Lexer) Emit

func (l *Lexer) Emit(t ItemType)

Emit emits the item with it's type information.

func (*Lexer) Errorf

func (l *Lexer) Errorf(format string, args ...interface{}) StateFn

Errorf returns the error state function.

func (*Lexer) Ignore

func (l *Lexer) Ignore()

Ignore skips the current token. Meant to be used for tokens that do not have any syntactical meaning (e.g comments).

func (*Lexer) IgnoreRun

func (l *Lexer) IgnoreRun(c CheckRune)

IgnoreRun ignores all the runes accepted by the given CheckRune.

func (*Lexer) IsEscChar

func (l *Lexer) IsEscChar(r rune) bool

IsEscChar returns true if the run is an escape character (ECHAR ::= '\' [uvtbnrf"'\])

func (*Lexer) LexQuotedString

func (l *Lexer) LexQuotedString() error

LexQuotedString properly processes a quoted string (by taking care of escaped characters).

func (*Lexer) NewIterator

func (l *Lexer) NewIterator() *ItemIterator

NewIterator returns a new ItemIterator instance that uses the lexer.

func (*Lexer) Next

func (l *Lexer) Next() (result rune)

Next reads the next rune from the Input, sets the Width and advances Pos.

func (*Lexer) Peek

func (l *Lexer) Peek() rune

Peek returns the next rune without advancing the lexer.

func (*Lexer) PeekTwo

func (l *Lexer) PeekTwo() []rune

Peek returns the next two rune without advancing the lexer.

func (*Lexer) Reset

func (l *Lexer) Reset(input string)

Reset resets Lexer fields. It reuses already allocated buffers.

func (*Lexer) Run

func (l *Lexer) Run(f StateFn) *Lexer

Run executes the given StateFn on the lexer and returns the lexer.

func (*Lexer) ValidateResult

func (l *Lexer) ValidateResult() error

ValidateResult verifies whether the entire input can be lexed without errors.

type RuneWidth

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

A RuneWidth represents a consecutive string of runes with the same width and the number of runes is stored in count. The reason we maintain this information is to properly backup when multiple look-aheads happen. For example, if the following sequence of events happen 1. Lexer.Next() consumes 1 byte 2. Lexer.Next() consumes 1 byte 3. Lexer.Next() consumes 3 bytes we would create two RunWidthTrackers, the 1st having width 1 and count 2, while the 2nd having width 3 and count 1, then the following backups can be done properly: 4. Lexer.Backup() should decrement the pos by 3 5. Lexer.Backup() should decrement the pos by 1 6. Lexer.Backup() should decrement the pos by 1

type StateFn

type StateFn func(*Lexer) StateFn

StateFn represents the state of the scanner as a function that returns the next state.

Jump to

Keyboard shortcuts

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