token

package
v1.8.1 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package token provides a token stream implementation for parsing with backtracking support.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Stream

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

Stream wraps a slice of tokens with position tracking and backtracking support.

func New

func New(normalizedText string) *Stream

New creates a new token stream from the given normalized text.

func (*Stream) Advance

func (s *Stream) Advance() bool

Advance moves to the next token. Returns false if at the end. Clears any partial token before advancing.

func (*Stream) AtEnd

func (s *Stream) AtEnd() bool

AtEnd returns true if the stream is at or past the end.

func (*Stream) ConsumeChars

func (s *Stream) ConsumeChars(n int) (string, bool)

ConsumeChars consumes n characters from the current token and sets the rest as partial. Returns the consumed text and whether the operation succeeded.

func (*Stream) Current

func (s *Stream) Current() (Token, bool)

Current returns the current token and whether it exists. If there's a partial token buffered, returns that instead.

func (*Stream) Peek

func (s *Stream) Peek(offset int) (Token, bool)

Peek returns the token at the given offset from the current position. Offset 0 returns the current token, 1 returns the next, etc.

func (*Stream) Remaining

func (s *Stream) Remaining() []Token

Remaining returns all tokens from the current position to the end.

func (*Stream) RemainingText

func (s *Stream) RemainingText() string

RemainingText returns the text of all remaining tokens joined by spaces.

func (*Stream) RestorePosition

func (s *Stream) RestorePosition(pos int)

RestorePosition restores a previously saved position.

func (*Stream) SavePosition

func (s *Stream) SavePosition() int

SavePosition returns the current position for later restoration.

func (*Stream) SetPartialToken

func (s *Stream) SetPartialToken(remaining string)

SetPartialToken sets the remaining text from a partially consumed token. This allows consuming only part of a token (e.g., "13015" -> "130" consumed, "15" remains).

func (*Stream) Skip

func (s *Stream) Skip(predicate func(string) bool)

Skip advances past tokens that match the given predicate.

func (*Stream) Text

func (s *Stream) Text() string

Text returns the text at the current position, or empty string if at end.

type Token

type Token struct {
	Text     string // The actual text (normalized)
	Position int    // Position in the token stream
}

Token represents a parsed word from the input text.

Jump to

Keyboard shortcuts

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