reader

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 31, 2023 License: EUPL-1.2 Imports: 8 Imported by: 1

Documentation

Index

Constants

View Source
const DefaultListLimit = 10000

DefaultListLimit specifies the default valur for `WithListLimit`.

View Source
const DefaultNestingLimit = 1000

DefaultNestingLimit specifies the default value for `WithNestingLimit`.

Variables

View Source
var ErrEOF = errors.New("unexpected EOF while reading")

ErrEOF is returned when reader ends prematurely to indicate that more data is needed to complete the current s-expression.

View Source
var ErrListTooLong = errors.New("list too long")

ErrListTooLong is returned, if the reader should read a list that is too long.

View Source
var ErrNumberFormat = errors.New("invalid number format")

ErrNumberFormat is returned when a reader macro encounters a invalid number.

View Source
var ErrPairFormat = errors.New("invalid pair format")

ErrPairFormat signals an invalid pair.

View Source
var ErrSkip = errors.New("skip s-expression")

ErrSkip signals a caller that the read value should be skipped. Typically, the read method is called again. Mostly used to implement comments at the lexical level.

View Source
var ErrTooDeeplyNested = errors.New("too deeply nested")

ErrTooDeeplyNested is returned, if the reader should read an object that is too deeply nested.

Functions

func ReadComment

func ReadComment(rd *Reader, _ rune) (sxpf.Object, error)

ReadComment is a reader macro that ignores everything until EOL.

func UnmatchedDelimiter

func UnmatchedDelimiter(rd *Reader, firstCh rune) (sxpf.Object, error)

UnmatchedDelimiter is a reader macro that signals the error of an unmatched delimiter, e.g. a closing parenthesis.

func WithDefaultSymbolFactory

func WithDefaultSymbolFactory(rd *Reader)

WithDefaultSymbolFactory sets a new symbol factory. Use this option, if following options need a defined symbol factory.

Types

type Error

type Error struct {
	Cause error
	Begin Position
	End   Position
}

Error is returned when reading fails due to some issue. Use errors.Is() with Cause to check for specific underlying errors.

func (Error) Error

func (e Error) Error() string

func (Error) Format

func (e Error) Format(s fmt.State, _ rune)

func (Error) Is

func (e Error) Is(other error) bool

Is returns true if the other error is same as the cause of this error.

func (Error) Unwrap

func (e Error) Unwrap() error

Unwrap returns the underlying cause of the error.

type Macro

type Macro func(*Reader, rune) (sxpf.Object, error)

Macro is a function that reads accoring to its on syntax.

type MacroMap

type MacroMap map[rune]Macro

MacroMap maps rune to read macros.

type Option

type Option func(*Reader)

Option is a function to modify the default reader when it is made.

func WithListLimit

func WithListLimit(length uint) Option

WithListLimit sets the maximum length of a list.

func WithNestingLimit

func WithNestingLimit(depth uint) Option

WithNestingLimit sets the maximum nesting for a object.

func WithSymbolFactory

func WithSymbolFactory(sf sxpf.SymbolFactory) Option

WithSymbolFactory sets the symbol factory to a defined value.

type Position

type Position struct {
	Name string
	Line int
	Col  int
}

Position stores the positional information about a value within the reader.

func (*Position) String

func (rp *Position) String() string

type Reader

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

Reader consumes characters from a stream and parses them into s-expressions.

func MakeReader

func MakeReader(r io.Reader, opts ...Option) *Reader

MakeReader creates a new reader.

func (*Reader) AnnotateError

func (rd *Reader) AnnotateError(err error, begin Position) error

AnnotateError adds error information (reader.Name, position) to the given error.

func (*Reader) IsSpace

func (*Reader) IsSpace(ch rune) bool

IsSpace returns true, if the rune is a space character.

func (*Reader) IsTerminal

func (rd *Reader) IsTerminal(ch rune) bool

IsTerminal returns true, if rune terminates the current token, according to the given read macro map.

func (*Reader) Name

func (rd *Reader) Name() string

Name return the name of the underlying reader.

func (*Reader) NextRune

func (rd *Reader) NextRune() (rune, error)

NextRune returns the next rune from the reader and advances the reader.

func (*Reader) Position

func (rd *Reader) Position() Position

Position returns information about the current position of the reader.

func (*Reader) Read

func (rd *Reader) Read() (sxpf.Object, error)

Read one s-expression and return it.

func (*Reader) ReadAll

func (rd *Reader) ReadAll() (objs []sxpf.Object, _ error)

ReadAll s-expressions until EOF.

func (*Reader) ReadToken

func (rd *Reader) ReadToken(firstCh rune, isTerminal func(rune) bool) (string, error)

ReadToken reads a sequence of non-terminal runes from the reader. if initCh > ' ', it is included as the first char.

func (*Reader) SetMacro

func (rd *Reader) SetMacro(initCh rune, macro Macro)

SetMacro sets the given read macro for the given initial character. If macro is nil, the initial character will not start a read macro.

func (*Reader) SkipSpace

func (rd *Reader) SkipSpace() (rune, error)

SkipSpace skips all space rune from the reader and return the first non-space rune.

func (*Reader) SymbolFactory

func (rd *Reader) SymbolFactory() sxpf.SymbolFactory

SymbolFactory returns the symbol factory that is used in the reader.

func (*Reader) Unread

func (rd *Reader) Unread(chs ...rune)

Unread returns runes consumed from the reader back to it.

Jump to

Keyboard shortcuts

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