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 ¶
- type Array
- type ArrayRule
- type BinaryRule
- type FormattedText
- type Formatter
- type FormattingChunk
- type FormattingRule
- type IFormatter
- type LFormatter
- type List
- func (l *List) Append(element SExp)
- func (l *List) AsArray() *Array
- func (l *List) AsList() *List
- func (l *List) AsSet() *Set
- func (l *List) AsSymbol() *Symbol
- func (l *List) Get(i int) SExp
- func (l *List) Len() int
- func (l *List) MatchSymbols(n int, symbols ...string) bool
- func (l *List) String(quote bool) string
- type ListRule
- type Number
- type Parser
- type RecursiveRule
- type SExp
- type SFormatter
- type Set
- type Symbol
- type SymbolRule
- type Translator
- func (p *Translator[T]) AddBinaryRule(name string, t BinaryRule[T])
- func (p *Translator[T]) AddDefaultListRule(rule ListRule[T])
- func (p *Translator[T]) AddDefaultRecursiveArrayRule(t RecursiveRule[T])
- func (p *Translator[T]) AddListRule(name string, rule ListRule[T])
- func (p *Translator[T]) AddRecursiveListRule(name string, t RecursiveRule[T])
- func (p *Translator[T]) AddSymbolRule(t SymbolRule[T])
- func (p *Translator[T]) SourceMap() *source.Map[T]
- func (p *Translator[T]) SpanOf(sexp SExp) source.Span
- func (p *Translator[T]) SyntaxError(s SExp, msg string) *source.SyntaxError
- func (p *Translator[T]) SyntaxErrors(s SExp, msg string) []source.SyntaxError
- func (p *Translator[T]) Translate(sexp SExp) (T, []source.SyntaxError)
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.
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) 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 ¶
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.
type FormattingChunk ¶
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 (*List) MatchSymbols ¶
MatchSymbols matches a list which starts with at least n symbols, of which the first m match the given strings.
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 ¶
Number represents a terminating number
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 (*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.
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 ¶
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.
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.
type Symbol ¶
type Symbol struct {
Value string
}
Symbol represents a terminating symbol.
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.