peruse

package
v0.0.0-...-9900226 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2023 License: AGPL-3.0-or-later Imports: 3 Imported by: 0

README

peruse

Simple lexer and parser. The lexer is designed after the go templating lexer, the parser is a simple Pratt parser by way of Robert Nystrom[^fn1] and Thorsten Ball

[^fn1]: There's also his blog post

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func UnexpectedAt

func UnexpectedAt(where string, unexpected error) error

Types

type Grammar

type Grammar[T any] struct {
	// contains filtered or unexported fields
}

func NewGrammar

func NewGrammar[T any]() Grammar[T]

func (Grammar[T]) Infix

func (g Grammar[T]) Infix(p Precedence, i InflixParselet[T], ts ...TokenType)

func (Grammar[T]) Parse

func (g Grammar[T]) Parse(t TokenType, p Parselet[T])

func (Grammar[T]) Precedence

func (g Grammar[T]) Precedence(t TokenType) Precedence

type InflixParselet

type InflixParselet[T any] func(*Parser[T], T, Precedence) (T, error)

type InvalidToken

type InvalidToken struct {
	Have   Token
	Wanted TokenType
}

func (InvalidToken) Error

func (e InvalidToken) Error() string

type LexFn

type LexFn func(*StringLexer, any) LexFn

type Parselet

type Parselet[T any] func(*Parser[T]) (T, error)

type Parser

type Parser[T any] struct {
	Cur, Next Token
	// contains filtered or unexported fields
}

func NewParser

func NewParser[T any](g Grammar[T], l *StringLexer) *Parser[T]

func (*Parser[T]) Consume

func (p *Parser[T]) Consume()

func (*Parser[T]) Expect

func (p *Parser[T]) Expect(n TokenType) bool

func (*Parser[T]) ExpectOrError

func (p *Parser[T]) ExpectOrError(n TokenType) error

func (*Parser[T]) HasMore

func (p *Parser[T]) HasMore() bool

func (*Parser[T]) NextPrecedence

func (p *Parser[T]) NextPrecedence() Precedence

func (*Parser[T]) Parse

func (p *Parser[T]) Parse() (T, error)

func (*Parser[T]) ParseAt

func (p *Parser[T]) ParseAt(prd Precedence) (T, error)

type Pos

type Pos int

type Precedence

type Precedence uint
const (
	INVALID_PRECEDENCE Precedence = iota
	LOWEST
)

type StringLexer

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

func NewLexer

func NewLexer(input string, initial LexFn, state any) *StringLexer

func (*StringLexer) AcceptManyOf

func (l *StringLexer) AcceptManyOf(chars string)

func (*StringLexer) AcceptOneOf

func (l *StringLexer) AcceptOneOf(chars string) bool

func (*StringLexer) AcceptWhile

func (l *StringLexer) AcceptWhile(a func(rune) bool)

func (*StringLexer) Discard

func (l *StringLexer) Discard()

func (*StringLexer) Emit

func (l *StringLexer) Emit(typ TokenType) LexFn

func (*StringLexer) Error

func (l *StringLexer) Error(format string, args ...any) LexFn

func (*StringLexer) Init

func (l *StringLexer) Init(input string, initial LexFn, state any)

func (*StringLexer) Next

func (l *StringLexer) Next() rune

func (*StringLexer) NextToken

func (l *StringLexer) NextToken() Token

func (*StringLexer) Peek

func (l *StringLexer) Peek() rune

func (*StringLexer) Prev

func (l *StringLexer) Prev()

func (*StringLexer) Word

func (l *StringLexer) Word() string

type Token

type Token struct {
	Type    TokenType
	Pos     Pos
	Literal string
}

func (Token) Is

func (t Token) Is(o TokenType) bool

func (Token) String

func (t Token) String() string

type TokenType

type TokenType int64
const (
	EOF TokenType = -1
	ERR TokenType = -2
)

type UnexpectedToken

type UnexpectedToken struct {
	Have Token
}

func (UnexpectedToken) Error

func (u UnexpectedToken) Error() string

Jump to

Keyboard shortcuts

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