sexpr

package
v0.0.16 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2021 License: Apache-2.0, BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Overview

Package sexpr parses s-expressions in a manner similar to the Common Lisp reader. It supports typical s-expression syntax and customization.

The elements of S-Expressions are called "forms" by this package and represented with the Form interface in the "form" subpackage. Form provides a Value() method for obtaining the underlying value. There are specific interfaces like `String`, `Number`, `Comment`, and `SymbolForm` corresponding to typical s-expression types. Type switches and assertions may be used to cast a Form to a suitable type.

Example

Example shows how ReadForm() parses a list s-expression.

r := NewFileReader("hello-world.sexpr", `("hello-world" 123)`)

f, err := r.ReadForm()
if err != nil {
	fmt.Printf("got error: %s", err.Error())
}
switch ff := f.(type) {
case StringForm:
	fmt.Printf("got string: %q", ff.StringValue())
case ListForm:
	fmt.Printf("got list of %d elements\n", ff.Len())
}
fmt.Printf("first element: %q", f.(ListForm).Nth(0).(StringForm).StringValue())
Output:

got list of 2 elements
first element: "hello-world"

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CommentForm

type CommentForm = form.Comment

type Form

type Form = form.Form

type FormFactory

type FormFactory interface {
	// NewList returns a new form.List for the provided subforms.
	//
	// The passed forms slice contains whitespace and comment forms that should
	// not be part of the value used to implement the form.List interface.
	NewList(forms []form.Form, span SourceSpan) (form.List, error)

	// NewNumberForm returns a new NumberForm for the provided literal
	// representation.
	//
	// value is guaranteed to be one of the numeric values defined in the
	// constant package.
	NewNumberForm(value constant.Value, span SourceSpan) (form.Number, error)

	// NewSymbolForm returns a new Form for the provided symbol literal representation.
	NewSymbolForm(literal string, span SourceSpan) (form.Symbol, error)

	// NewStringForm returns a new StringForm for the provided literal representation.
	NewStringForm(value string, span SourceSpan) (StringForm, error)

	// NewCommentForm returns a new CommentForm for the provided literal representation.
	NewCommentForm(value string, span SourceSpan) (CommentForm, error)

	// NewWhitespaceForm returns a new WhitespaceForm for the provided literal representation.
	NewWhitespaceForm(value string, span SourceSpan) (WhitespaceForm, error)
}

FormFactory is used by FormReader to construct Form values as it processes the input stream. This allows customization of the concrete types used to implement the Form interface.

A default FormFactory is provided by DefaultFormFactory().

func DefaultFormFactory

func DefaultFormFactory(r *FormReader) FormFactory

DefaultFormFactory returns a FormConstructor that creates Form objects using unexported types within the sexpr package.

type FormReader

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

FormReader reads a stream of S-Expressions.

func NewFileReader

func NewFileReader(fileName, contents string, opts ...Option) *FormReader

NewFileReader returns an object for reading Forms from a source file, which is provided as a string.

The filename value is used to print error messages and will not be accessed by the reader, so it does not need to be a real file at all.

func (*FormReader) ReadForm

func (fr *FormReader) ReadForm() (Form, error)

ReadForm reads the next form in the input stream.

If the end of the file is encountered, the second value will be io.EOF.

type ListForm

type ListForm = form.List

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option is used to configure a FormReader.

func CustomFormFactory

func CustomFormFactory(factoryProvider func(*FormReader) FormFactory) Option

CustomFormFactory returns an Option that may be passed to NewFileReader that uses a non-default FormFactory for constructing Forms.

The passed in function will be called with a newly created *FormReader as an argument, allowing the factory to depend on the reader to implement its functionality.

type ReaderMacroResult

type ReaderMacroResult interface {
	// Skip returns true if the reader macro declined to read from the stream
	// and returned the cursor back to the original location. If true is
	// rturned, the Form() function will not be called.
	Skip() bool

	// The form read by the reader macro.
	Form() form.Form
}

ReaderMacroResult is returned by custom reader macro functions.

type SourceSpan

type SourceSpan = form.SourcePosition

type StringForm

type StringForm = form.String

type ValuelessForm

type ValuelessForm = form.Valueless

type WhitespaceForm

type WhitespaceForm = form.Whitespace

Directories

Path Synopsis
Package form is an API for working with S-Expression objects.
Package form is an API for working with S-Expression objects.

Jump to

Keyboard shortcuts

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