reader

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2022 License: MIT Imports: 14 Imported by: 3

Documentation

Overview

Package reader implements a highly customizable and generic reader, reader-macros for primitive runtime types and reader-macro factories for collection types like vector, map, set.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrSkip can be returned by reader macro to indicate a no-op form which
	// should be discarded (e.g., Comments).
	ErrSkip = errors.New("skip expr")

	// ErrEOF is returned by reader when stream ends prematurely to indicate
	// that more data is needed to complete the current form.
	ErrEOF = errors.New("unexpected EOF while parsing")

	// ErrNumberFormat is returned when a reader macro encounters a illegally
	// formatted numerical form.
	ErrNumberFormat = errors.New("invalid number format")
)

Functions

This section is empty.

Types

type Error

type Error struct {
	Cause      error
	Begin, End Position
}

Error is returned by the error when reading from a stream 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 added in v0.3.0

func (e Error) Format(s fmt.State, verb 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(rd *Reader, init rune) (core.Any, error)

Macro implementations can be plugged into the Reader to extend, override or customize behavior of the reader.

func UnmatchedDelimiter

func UnmatchedDelimiter() Macro

UnmatchedDelimiter implements a reader macro that can be used to capture unmatched delimiters such as closing parenthesis etc.

type Option

type Option func(*Reader)

Option values can be used with New() to configure the reader during init.

func WithBuiltinSymbolReader

func WithBuiltinSymbolReader(symTable map[string]core.Any) Option

WithBuiltinSymbolReader configures the default symbol reader with given symbol table.

func WithNumReader

func WithNumReader(m Macro) Option

WithNumReader sets the number reader macro to be used by the Reader. Uses the default number reader if nil.

func WithSymbolReader

func WithSymbolReader(m Macro) Option

WithSymbolReader sets the symbol reader macro to be used by the Reader. Builds a slurp.Symbol if nil.

type Position

type Position struct {
	File string
	Ln   int
	Col  int
}

Position represents the positional information about a value read by reader.

func (Position) String

func (p Position) String() string

type Reader

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

Reader consumes characters from a stream and parses them into symbolic expressions or forms. Reader is customizable through Macro implementations which can be set as handler for specific trigger runes.

func New

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

New returns a lisp reader instance which can read forms from r. Returned instance supports only primitive data types from value package by default. Support for custom forms can be added using SetMacro(). File name is inferred from the value & type information of 'r' OR can be set manually on the Reader instance returned.

func (*Reader) All

func (rd *Reader) All() ([]core.Any, error)

All consumes characters from stream until EOF and returns a list of all the forms parsed. Any no-op forms (e.g., comment) will not be included in the result.

func (Reader) Container

func (rd Reader) Container(end rune, formType string, f func(core.Any) error) error

Container reads multiple forms until 'end' rune is reached. Should be used to read collection types like List etc. formType is only used to annotate errors.

func (*Reader) IsTerminal

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

IsTerminal returns true if the rune should terminate a form. Macro trigger runes defined in the read table and all whitespace characters are considered terminal. "," is also considered a whitespace character and hence a terminal.

func (*Reader) NextRune

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

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

func (*Reader) One

func (rd *Reader) One() (core.Any, error)

One consumes characters from underlying stream until a complete form is parsed and returns the form while ignoring the no-op forms like comments. Except EOF, all other errors will be wrapped with reader Error type along with the positional information obtained using Position().

func (Reader) Position

func (rd Reader) Position() Position

Position returns information about the stream including file name and the position of the reader.

func (*Reader) SetMacro

func (rd *Reader) SetMacro(init rune, isDispatch bool, macro Macro)

SetMacro sets the given reader macro as the handler for init rune in the read table. Overwrites if a macro is already present. If the macro value given is nil, entry for the init rune will be removed from the read table. isDispatch decides if the macro is a dispatch macro and takes effect only after a '#' sign.

func (*Reader) SkipSpaces

func (rd *Reader) SkipSpaces() error

SkipSpaces consumes and discards runes from stream repeatedly until a character that is not a whitespace is identified. Along with standard unicode whitespace characters, "," is also considered a whitespace and discarded.

func (*Reader) Token

func (rd *Reader) Token(init rune) (string, error)

Token reads one token from the reader and returns. If init is not -1, it is included as first character in the token.

func (*Reader) Unread

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

Unread returns runes consumed from the stream back to the stream. Un-reading more runes than read is guaranteed to work but will cause inconsistency in positional information of the Reader.

Jump to

Keyboard shortcuts

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