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 ¶
- Variables
- type Error
- type Macro
- type Option
- type Position
- type Reader
- func (rd *Reader) All() ([]core.Any, error)
- func (rd Reader) Container(end rune, formType string, f func(core.Any) error) error
- func (rd *Reader) IsTerminal(r rune) bool
- func (rd *Reader) NextRune() (rune, error)
- func (rd *Reader) One() (core.Any, error)
- func (rd Reader) Position() Position
- func (rd *Reader) SetMacro(init rune, isDispatch bool, macro Macro)
- func (rd *Reader) SkipSpaces() error
- func (rd *Reader) Token(init rune) (string, error)
- func (rd *Reader) Unread(runes ...rune)
Constants ¶
This section is empty.
Variables ¶
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 ¶
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.
type Macro ¶
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 ¶
WithBuiltinSymbolReader configures the default symbol reader with given symbol table.
func WithNumReader ¶
WithNumReader sets the number reader macro to be used by the Reader. Uses the default number reader if nil.
func WithSymbolReader ¶
WithSymbolReader sets the symbol reader macro to be used by the Reader. Builds a slurp.Symbol if nil.
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 ¶
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 ¶
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 ¶
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 ¶
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) One ¶
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 ¶
Position returns information about the stream including file name and the position of the reader.
func (*Reader) SetMacro ¶
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 ¶
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.