sexp

package
v1.2.23 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Copyright Consensys Software Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

SPDX-License-Identifier: Apache-2.0

Copyright Consensys Software Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

SPDX-License-Identifier: Apache-2.0

Copyright Consensys Software Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

SPDX-License-Identifier: Apache-2.0

Copyright Consensys Software Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

SPDX-License-Identifier: Apache-2.0

Copyright Consensys Software Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

SPDX-License-Identifier: Apache-2.0

Copyright Consensys Software Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

SPDX-License-Identifier: Apache-2.0

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Array

type Array struct {
	Elements []SExp
}

Array represents a list of zero or more S-Expressions.

func NewArray

func NewArray(elements []SExp) *Array

NewArray creates a new Array from a given array of S-Expressions.

func (*Array) AsArray

func (a *Array) AsArray() *Array

AsArray returns the given array.

func (*Array) AsList

func (a *Array) AsList() *List

AsList returns nil for a Array.

func (*Array) AsSet

func (a *Array) AsSet() *Set

AsSet returns the given Array.

func (*Array) AsSymbol

func (a *Array) AsSymbol() *Symbol

AsSymbol returns nil for a Array.

func (*Array) Get

func (a *Array) Get(i int) SExp

Get the ith element of this Array

func (*Array) Len

func (a *Array) Len() int

Len gets the number of elements in this Array.

func (*Array) String

func (a *Array) String(quote bool) string

type ArrayRule

type ArrayRule[T comparable] func(*Array) (T, []source.SyntaxError)

ArrayRule is an array translator which is responsible converting an array with a given sequence of zero or more arguments into an expression type T. Observe that the arguments are already translated into the correct form.

type BinaryRule

type BinaryRule[T comparable] func(string, string) (T, error)

BinaryRule is a binary translator is a wrapper for translating lists which must have exactly two symbol arguments. The wrapper takes care of ensuring sufficient arguments are given, etc.

type FormattedText

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

FormattedText provides encapsulates the notion of formatted chunk of text.

func (*FormattedText) Indent

func (p *FormattedText) Indent(delta int)

Indent increases or decreases the current indent level.

func (*FormattedText) LineWidth

func (p *FormattedText) LineWidth() uint

LineWidth returns the width of the current line.

func (*FormattedText) MaxWidth

func (p *FormattedText) MaxWidth() uint

MaxWidth returns the maximum width of any line in this formatted text block.

func (*FormattedText) NewLine

func (p *FormattedText) NewLine()

NewLine starts a new line

func (*FormattedText) String

func (p *FormattedText) String() string

func (*FormattedText) WriteString

func (p *FormattedText) WriteString(str string)

WriteString writes a string into the current line of this formatted text block.

type Formatter

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

Formatter encapsulates and applies a given set of rules.

func NewFormatter

func NewFormatter(width uint, quote bool) *Formatter

NewFormatter constructs a new formatter which aims to fit its output within a given width.

func (*Formatter) Add

func (p *Formatter) Add(rule FormattingRule)

Add a new formatting rule to this formatter.

func (*Formatter) Format

func (p *Formatter) Format(sexp SExp) string

Format a given S-Expression using the rules embedded within this formatter.

type FormattingChunk

type FormattingChunk struct {
	Priority uint
	Indent   uint
	Contents SExp
}

FormattingChunk represents a chunk of a lisp expression which is to be indented at a given priority level.

type FormattingRule

type FormattingRule interface {
	Split(*List) ([]FormattingChunk, uint)
}

FormattingRule provides a generic mechanism for writing custom formatting rules. Whenever a list is encountered during formatting, the formatting rules will be given the opportunity to direct formatting of the list. That is, whether to start a new line and indent the list as whole and/or any of its children. A formatting rule should return nil for the formatting chunks when it doesn't handle the given list.

type IFormatter

type IFormatter struct {
	// Head symbol to match
	Head string
	// Priority to give for matching.
	Priority uint
}

IFormatter is your basic formatting rule.

func (*IFormatter) Split

func (p *IFormatter) Split(list *List) ([]FormattingChunk, uint)

Split a list using the LFormatter where the list matches.

type LFormatter

type LFormatter struct {
	// Head symbol to match
	Head string
	// Priority to give for matching.
	Priority uint
}

LFormatter is a simple formatter which indents a list like so:

(head
  child1
  child2
  ...
  childn)

That is, the head starts on a newly indented line, and each child is indented one more position.

func (*LFormatter) Split

func (p *LFormatter) Split(list *List) ([]FormattingChunk, uint)

Split a list using the LFormatter where the list matches.

type List

type List struct {
	Elements []SExp
}

List represents a list of zero or more S-Expressions.

func EmptyList

func EmptyList() *List

EmptyList creates an empty list.

func NewList

func NewList(elements []SExp) *List

NewList creates a new list from a given array of S-Expressions.

func (*List) Append

func (l *List) Append(element SExp)

Append a new element onto this list.

func (*List) AsArray

func (l *List) AsArray() *Array

AsArray returns the given array.

func (*List) AsList

func (l *List) AsList() *List

AsList returns the given list.

func (*List) AsSet

func (l *List) AsSet() *Set

AsSet returns nil for a list.

func (*List) AsSymbol

func (l *List) AsSymbol() *Symbol

AsSymbol returns nil for a list.

func (*List) Get

func (l *List) Get(i int) SExp

Get the ith element of this list

func (*List) Len

func (l *List) Len() int

Len gets the number of elements in this list.

func (*List) MatchSymbols

func (l *List) MatchSymbols(n int, symbols ...string) bool

MatchSymbols matches a list which starts with at least n symbols, of which the first m match the given strings.

func (*List) String

func (l *List) String(quote bool) string

type ListRule

type ListRule[T comparable] func(*List) (T, []source.SyntaxError)

ListRule is a list translator is responsible converting a list with a given sequence of zero or more arguments into an expression type T. Observe that the arguments are already translated into the correct form.

type Number

type Number struct {
	Value big.Int
}

Number represents a terminating number

func NewNumber

func NewNumber(value big.Int) *Number

NewNumber creates a new number from a given integer.

func (*Number) AsArray

func (s *Number) AsArray() *Array

AsArray returns the given array.

func (*Number) AsList

func (s *Number) AsList() *List

AsList returns nil for a symbol.

func (*Number) AsSet

func (s *Number) AsSet() *Set

AsSet returns nil for a symbol.

func (*Number) AsSymbol

func (s *Number) AsSymbol() *Symbol

AsSymbol returns the given symbol

func (*Number) String

func (s *Number) String(quote bool) string

type Parser

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

Parser represents a parser in the process of parsing a given string into one or more S-expressions.

func NewParser

func NewParser(srcfile *source.File) *Parser

NewParser constructs a new instance of Parser

func (*Parser) Lookahead

func (p *Parser) Lookahead(i int) *rune

Lookahead and see what punctuation is next.

func (*Parser) Next

func (p *Parser) Next() []rune

Next extracts the next token from a given string.

func (*Parser) Parse

func (p *Parser) Parse() (SExp, *source.SyntaxError)

Parse a given string into an S-Expression, or produce an error.

func (*Parser) SkipWhiteSpace

func (p *Parser) SkipWhiteSpace()

SkipWhiteSpace skips over any whitespace, including comments.

func (*Parser) SourceMap

func (p *Parser) SourceMap() *source.Map[SExp]

SourceMap returns the internal source map constructing during parsing. Using this one can determine, for each SExp, where in the original text it originated. This is helpful, for example, when reporting syntax errors.

func (*Parser) Text

func (p *Parser) Text() []rune

Text returns the underlying text for this parser.

type RecursiveRule

type RecursiveRule[T comparable] func(string, []T) (T, error)

RecursiveRule is a recursive translator is a wrapper for translating lists whose elements can be built by recursively reusing the enclosing translator.

type SExp

type SExp interface {
	// AsList checks whether this S-Expression is a list and, if
	// so, returns it.  Otherwise, it returns nil.
	AsList() *List
	// AsSet checks whether this S-Expression is a set and, if
	// so, returns it.  Otherwise, it returns nil.
	AsSet() *Set
	// AsArray checks whether this S-Expression is an array and, if so, returns
	// it.  Otherwise, it returns nil.
	AsArray() *Array
	// AsSymbol checks whether this S-Expression is a symbol and,
	// if so, returns it.  Otherwise, it returns nil.
	AsSymbol() *Symbol
	// String generates a string representation which may (may not) be quoted.
	// Quoting is used to manage symbol names which contain whitespace
	// characters and braces, etc.
	String(quote bool) string
}

SExp is an S-Expression is either a List of zero or more S-Expressions, or a Symbol.

func Parse

func Parse(s *source.File) (SExp, *source.Map[SExp], *source.SyntaxError)

Parse a given string into an S-expression, or return an error if the string is malformed. A source map is also returned for debugging purposes.

func ParseAll

func ParseAll(s *source.File) ([]SExp, *source.Map[SExp], *source.SyntaxError)

ParseAll converts a given string into zero or more S-expressions, or returns an error if the string is malformed. A source map is also returned for debugging purposes. The key distinction from Parse is that this function continues parsing after the first S-expression is encountered.

type SFormatter

type SFormatter struct {
	// Head symbol to match
	Head string
	// Priority to give for matching.
	Priority uint
}

SFormatter is a variation on the LFormatter which does not indent the first child, thusly:

(head child1
  child2
  ...
  childn)

That is, the head starts on a newly indented line, and each child is indented one more position.

func (*SFormatter) Split

func (p *SFormatter) Split(list *List) ([]FormattingChunk, uint)

Split a list using the LFormatter where the list matches.

type Set

type Set struct {
	Elements []SExp
}

Set represents a list of zero or more S-Expressions.

func NewSet

func NewSet(elements []SExp) *Set

NewSet creates a new set from a given array of S-Expressions.

func (*Set) AsArray

func (l *Set) AsArray() *Array

AsArray returns the given array.

func (*Set) AsList

func (l *Set) AsList() *List

AsList returns nil for a set.

func (*Set) AsSet

func (l *Set) AsSet() *Set

AsSet returns the given set.

func (*Set) AsSymbol

func (l *Set) AsSymbol() *Symbol

AsSymbol returns nil for a set.

func (*Set) Get

func (l *Set) Get(i int) SExp

Get the ith element of this set

func (*Set) Len

func (l *Set) Len() int

Len gets the number of elements in this set.

func (*Set) String

func (l *Set) String(quote bool) string

type Symbol

type Symbol struct {
	Value string
}

Symbol represents a terminating symbol.

func NewSymbol

func NewSymbol(value string) *Symbol

NewSymbol creates a new symbol from a given string.

func (*Symbol) AsArray

func (s *Symbol) AsArray() *Array

AsArray returns the given array.

func (*Symbol) AsList

func (s *Symbol) AsList() *List

AsList returns nil for a symbol.

func (*Symbol) AsSet

func (s *Symbol) AsSet() *Set

AsSet returns nil for a symbol.

func (*Symbol) AsSymbol

func (s *Symbol) AsSymbol() *Symbol

AsSymbol returns the given symbol

func (*Symbol) String

func (s *Symbol) String(quote bool) string

type SymbolRule

type SymbolRule[T comparable] func(string) (T, bool, error)

SymbolRule is a symbol generator is responsible for converting a terminating expression (i.e. a symbol) into an expression type T. For example, a number or a column access.

type Translator

type Translator[T comparable] struct {
	// contains filtered or unexported fields
}

Translator is a generic mechanism for translating S-Expressions into a structured form.

func NewTranslator

func NewTranslator[T comparable](srcfile *source.File, srcmap *source.Map[SExp]) *Translator[T]

NewTranslator constructs a new Translator instance.

func (*Translator[T]) AddBinaryRule

func (p *Translator[T]) AddBinaryRule(name string, t BinaryRule[T])

AddBinaryRule .

func (*Translator[T]) AddDefaultListRule

func (p *Translator[T]) AddDefaultListRule(rule ListRule[T])

AddDefaultListRule adds a default rule to be applied when no other recursive rules apply.

func (*Translator[T]) AddDefaultRecursiveArrayRule

func (p *Translator[T]) AddDefaultRecursiveArrayRule(t RecursiveRule[T])

AddDefaultRecursiveArrayRule adds a default recursive rule to be applied when no other recursive rules apply.

func (*Translator[T]) AddListRule

func (p *Translator[T]) AddListRule(name string, rule ListRule[T])

AddListRule adds a raw list rule to this expression translator.

func (*Translator[T]) AddRecursiveListRule

func (p *Translator[T]) AddRecursiveListRule(name string, t RecursiveRule[T])

AddRecursiveListRule adds a new list translator to this expression translator.

func (*Translator[T]) AddSymbolRule

func (p *Translator[T]) AddSymbolRule(t SymbolRule[T])

AddSymbolRule adds a new symbol translator to this expression translator.

func (*Translator[T]) SourceMap

func (p *Translator[T]) SourceMap() *source.Map[T]

SourceMap returns the source map maintained for terms constructed by this translator.

func (*Translator[T]) SpanOf

func (p *Translator[T]) SpanOf(sexp SExp) source.Span

SpanOf gets the span associated with a given S-Expression in the original source file.

func (*Translator[T]) SyntaxError

func (p *Translator[T]) SyntaxError(s SExp, msg string) *source.SyntaxError

SyntaxError constructs a suitable syntax error for a given S-Expression.

func (*Translator[T]) SyntaxErrors

func (p *Translator[T]) SyntaxErrors(s SExp, msg string) []source.SyntaxError

SyntaxErrors constructs a suitable syntax error for a given S-Expression.

func (*Translator[T]) Translate

func (p *Translator[T]) Translate(sexp SExp) (T, []source.SyntaxError)

Translate a given string into a given structured representation T using an appropriately configured.

Jump to

Keyboard shortcuts

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