lexgo

package module
v0.0.0-...-166568f Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2024 License: MIT Imports: 9 Imported by: 0

README

Package lexgo provides a simple lexer.

Usage

type Token uint8

const (
	Invalid Token = iota
	Error
	EOF
	Unsigned
	String
	Identifier

	Add
	Sub
	Mul
	Div
)

config := lexgo.Config{
	Invalid: Invalid,
	Error: Error,
	EOF: EOF,
	Unsigned: Unsigned,
	String: String,
	Identifier: Identifier,
	All: map[Token]string{
		Add: "+",
		Sub: "-",
		Mul: "*",
		Div: "/",
	},
}

r := strings.NewReader("1 + 2")
lex := lexgo.New[Token](r, config)
switch lex.Next() {
	case Unsigned:
case Add:
...
}

Documentation

Overview

Package lexgo implements a simple lexer.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnterminatedString = errors.New("unterminated string")
	ErrUnterminatedChar   = errors.New("unterminated char")
)

Functions

This section is empty.

Types

type Config

type Config[T constraints.Unsigned] struct {
	Invalid    T
	Error      T
	EOF        T
	Unsigned   T
	String     T
	Identifier T
	All        map[T]string
}

type Lexer

type Lexer[T constraints.Unsigned] struct {
	Error       error
	Location    Location
	String      string // identifier, comment, string literal
	ValueFormat ValueFormat
	Unsigned    uint64
	// contains filtered or unexported fields
}

func New

func New[T constraints.Unsigned](r io.Reader, config Config[T]) *Lexer[T]

func (*Lexer[T]) Init

func (lex *Lexer[T]) Init(r io.Reader, config Config[T])

func (*Lexer[T]) Newline

func (lex *Lexer[T]) Newline() bool

Newline returns whether a newline was encountered in the last Peek() or Next() call. NB. whitespace is always skipped at the start when calling Peek() and Next().

func (*Lexer[T]) Next

func (lex *Lexer[T]) Next() T

func (*Lexer[T]) NextLine

func (lex *Lexer[T]) NextLine() string

func (*Lexer[T]) Peek

func (lex *Lexer[T]) Peek() T

func (*Lexer[T]) Reset

func (lex *Lexer[T]) Reset(src io.Reader)

type Location

type Location struct {
	File   string
	Offset uint32
}

func (Location) String

func (loc Location) String() string

type ValueFormat

type ValueFormat uint8
const (
	Decimal     ValueFormat = iota // 123
	Hexadecimal                    // 0x
	Binary                         // 0b
	Character                      // ' '
)

func (ValueFormat) Format

func (f ValueFormat) Format(v uint64) string

Jump to

Keyboard shortcuts

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