Documentation ¶
Overview ¶
Package apc provides a minimalist parser combinator library.
Index ¶
- Constants
- Variables
- func IsMustReturnParseErr(err error) bool
- func Parse[CT, T any](ctx Context[CT], parser Parser[CT, T], parseConfig ParseConfig) (T, error)
- type ByteReaderWithOrigin
- type Context
- type EOFError
- type MapDetailedFunc
- type MapFunc
- type MaybeValue
- type Origin
- type OriginRange
- type ParseConfig
- type ParseError
- type ParseErrorConsumed
- type ParseReader
- type Parser
- func Any[CT, T any](parsers ...Parser[CT, T]) Parser[CT, T]
- func Bind[CT, T, U any](parser Parser[CT, T], node U) Parser[CT, U]
- func BindToToken[CT, T any](parser Parser[CT, T], tokenType TokenType) Parser[CT, Token]
- func CastTo[CT, T, U any](parser Parser[CT, T]) Parser[CT, U]
- func CastToAny[CT, T any](parser Parser[CT, T]) Parser[CT, any]
- func Exact[CT any](value CT) Parser[CT, CT]
- func ExactSlice[CT any](value []CT) Parser[CT, []CT]
- func ExactStr(value string) Parser[rune, string]
- func ExactTokenType(tokenType TokenType) Parser[Token, Token]
- func ExactTokenValue(tokenType TokenType, value any) Parser[Token, Token]
- func Look[CT, T any](parser Parser[CT, T]) Parser[CT, T]
- func Map[CT, T, U any](parser Parser[CT, T], mapFunc MapFunc[T, U]) Parser[CT, U]
- func MapDetailed[CT, T, U any](parser Parser[CT, T], mapFunc MapDetailedFunc[T, U]) Parser[CT, U]
- func MapTokenToValue[CT, T any](parser Parser[CT, Token]) Parser[CT, T]
- func Maybe[CT, T any](parser Parser[CT, T]) Parser[CT, MaybeValue[T]]
- func Named[CT, T any](name string, parser Parser[CT, T]) Parser[CT, T]
- func OneOrMore[CT, T any](parser Parser[CT, T]) Parser[CT, []T]
- func OneOrMoreSeparated[CT, T, U any](parser Parser[CT, T], sepParser Parser[CT, U]) Parser[CT, []T]
- func Range[CT, T any](min int, max int, parser Parser[CT, T]) Parser[CT, []T]
- func Ref[CT, T any](parserPtr *Parser[CT, T]) Parser[CT, T]
- func Regex(pattern string) Parser[rune, string]
- func Seq[CT, T any](parsers ...Parser[CT, T]) Parser[CT, []T]
- func Seq2[CT, T1, T2 any](parser1 Parser[CT, T1], parser2 Parser[CT, T2]) Parser[CT, *Seq2Node[T1, T2]]
- func Seq3[CT, T1, T2, T3 any](parser1 Parser[CT, T1], parser2 Parser[CT, T2], parser3 Parser[CT, T3]) Parser[CT, *Seq3Node[T1, T2, T3]]
- func Seq4[CT, T1, T2, T3, T4 any](parser1 Parser[CT, T1], parser2 Parser[CT, T2], parser3 Parser[CT, T3], ...) Parser[CT, *Seq4Node[T1, T2, T3, T4]]
- func Seq5[CT, T1, T2, T3, T4, T5 any](parser1 Parser[CT, T1], parser2 Parser[CT, T2], parser3 Parser[CT, T3], ...) Parser[CT, *Seq5Node[T1, T2, T3, T4, T5]]
- func Seq6[CT, T1, T2, T3, T4, T5, T6 any](parser1 Parser[CT, T1], parser2 Parser[CT, T2], parser3 Parser[CT, T3], ...) Parser[CT, *Seq6Node[T1, T2, T3, T4, T5, T6]]
- func Seq7[CT, T1, T2, T3, T4, T5, T6, T7 any](parser1 Parser[CT, T1], parser2 Parser[CT, T2], parser3 Parser[CT, T3], ...) Parser[CT, *Seq7Node[T1, T2, T3, T4, T5, T6, T7]]
- func Seq8[CT, T1, T2, T3, T4, T5, T6, T7, T8 any](parser1 Parser[CT, T1], parser2 Parser[CT, T2], parser3 Parser[CT, T3], ...) Parser[CT, *Seq8Node[T1, T2, T3, T4, T5, T6, T7, T8]]
- func Skip[CT, T any](skipParser Parser[CT, any], parser Parser[CT, T]) Parser[CT, T]
- func Unskip[CT, T any](skipParser Parser[CT, any], parser Parser[CT, T]) Parser[CT, T]
- func ZeroOrMore[CT, T any](parser Parser[CT, T]) Parser[CT, []T]
- func ZeroOrMoreSeparated[CT, T, U any](parser Parser[CT, T], sepParser Parser[CT, U]) Parser[CT, []T]
- type ReaderContext
- func NewBinaryFileContext(file *os.File) *ReaderContext[byte]
- func NewByteReaderContext(originName string, reader io.Reader) *ReaderContext[byte]
- func NewFileContext(file *os.File) *ReaderContext[rune]
- func NewReaderContext[CT any](reader ReaderWithOrigin[CT]) *ReaderContext[CT]
- func NewRuneReaderContext(originName string, reader io.RuneReader) *ReaderContext[rune]
- func NewStringContext(originName string, data string) *ReaderContext[rune]
- func (ctx *ReaderContext[CT]) AddSkipParser(parser Parser[CT, any])
- func (ctx *ReaderContext[CT]) Consume(num int) ([]CT, error)
- func (ctx *ReaderContext[CT]) DebugEnd(format string, formatArgs ...interface{})
- func (ctx *ReaderContext[CT]) DebugPrint(format string, formatArgs ...interface{})
- func (ctx *ReaderContext[CT]) DebugStart(format string, formatArgs ...interface{})
- func (ctx *ReaderContext[CT]) GetCurOrigin() Origin
- func (ctx *ReaderContext[CT]) GetCurParserName() string
- func (ctx *ReaderContext[CT]) GetLookOffset() int
- func (ctx *ReaderContext[CT]) GetUserData() any
- func (ctx *ReaderContext[CT]) Peek(offset int, num int) ([]CT, error)
- func (ctx *ReaderContext[CT]) RemoveSkipParser(parser Parser[CT, any])
- func (ctx *ReaderContext[CT]) RunSkipParsers() error
- func (ctx *ReaderContext[CT]) SetCurParserName(name string)
- func (ctx *ReaderContext[CT]) SetLookOffset(val int)
- func (ctx *ReaderContext[CT]) SetUserData(data any)
- type ReaderWithOrigin
- type RuneContextPeekingRuneReader
- type RuneReaderWithOrigin
- type Seq2Node
- type Seq3Node
- type Seq4Node
- type Seq5Node
- type Seq6Node
- type Seq7Node
- type Seq8Node
- type Token
- type TokenType
Constants ¶
const InvalidLookOffset int = -1
Value of an invalid look offset.
Variables ¶
var ( // Instance of EOFError to compare. ErrEOF = &EOFError{} // Instance of ParseError to compare. ErrParseErr = &ParseError{} // Instance of ParseErrorConsumed to compare. ErrParseErrConsumed = &ParseErrorConsumed{} )
var BoolParser = Named("boolean", Any(Bind(ExactStr("true"), true), Bind(ExactStr("false"), false)))
Parses "true" and "false" literals into a boolean and returns the boolean result.
var (
DebugPrintReaderContext = false
)
Enable various debug prints
var DefaultParseConfig = ParseConfig{ MustParseToEOF: true, }
A sane default for ParseConfig.
var DoubleQuotedStringParser = Named("double-quoted string", Map( Regex(`"(?:[^"\\]|\\.)*"`), func(node string) string { return node[1 : len(node)-1] }))
Parses a string wrapped in (") characters. The result is a string excluding the start and end ("). The result may contain raw/unescaped (\") and other escape markers.
var FloatParser = Named("float", Map( Regex("[+\\-]?\\d+(\\.\\d+)?"), func(node string) float64 { val, err := strconv.ParseFloat(node, 64) if err != nil { panic(err) } return val }))
Parses floating point numbers and returns a float64 result. May be preceded with '+' or '-'.
var IdentifierParser = Named("identifier", Regex("[a-zA-Z_][a-zA-Z_0-9]*"))
Parses a C-style identifier and returns the string result.
var IntOrFloatParser = Named("int or float", Map( Regex("[+\\-]?\\d+(\\.\\d+)?"), func(node string) any { if strings.ContainsAny(node, ".") { val, err := strconv.ParseFloat(node, 64) if err != nil { panic(err) } return val } val, err := strconv.ParseInt(node, 10, 64) if err != nil { panic(err) } return val }))
Parses int and floating point numbers and returns a int64 or float64 result. May be preceded with '+' or '-'.
var IntParser = Named("integer", Map( Regex("[+\\-]?\\d+"), func(node string) int64 { val, err := strconv.ParseInt(node, 10, 64) if err != nil { panic(err) } return val }))
Parses integer numbers and returns an int64 result. May be preceded with '+' or '-'.
var SingleQuotedStringParser = Named("single-quoted string", Map( Regex(`'(?:[^'\\]|\\.)*'`), func(node string) string { return node[1 : len(node)-1] }))
Parses a string wrapped in (') characters. The result is a string excluding the start and end ('). The result may contain raw/unescaped (\') and other escape markers.
var WhitespaceParser = Named("whitespace", Regex("\\s+"))
Parses one or more whitespace characters and returns the string result.
Functions ¶
func IsMustReturnParseErr ¶
Returns true if err is anything but nil or a ParseError.
Types ¶
type ByteReaderWithOrigin ¶
type ByteReaderWithOrigin struct {
// contains filtered or unexported fields
}
Implements ReaderWithOrigin[byte] by calling reader.Read.
func NewByteReaderWithOrigin ¶
func NewByteReaderWithOrigin(originName string, reader io.Reader) *ByteReaderWithOrigin
Returns a *ByteReaderWithOrigin with the provided origin name and reader.
type Context ¶
type Context[CT any] interface { // Returns a []CT of num elements beginning at offset without consuming // the elements. // The offset is a non-negative value relative to the next unconsumed // element in the input stream. // // If the end of input is reached, an EOFError is returned along // with any peeked elements (which may be less than num elements in length // if end of input has been reached). Peek(offset int, num int) ([]CT, error) // Advances the input stream by num elements, returning the consumed // elements. // // If the end of input is reached, an EOFError is returned along // with any consumed elements (which may be less than num elements in length // if end of input has been reached). Consume(num int) ([]CT, error) // Returns an Origin representing the next unconsumed element in the // input stream. GetCurOrigin() Origin // Adds the parser to the list of parsers that attempt to run when // RunSkipParsers is called. If the parser matches, its result will // be discarded. Duplicate parsers cannot be added. AddSkipParser(parser Parser[CT, any]) // Removes the parser from the list of parsers that attempt to run // when RunSkipParsers is called. If the parser has not been added, // the function panics. RemoveSkipParser(parser Parser[CT, any]) // Attempts to run any added skip parsers as long as one of the parsers // successfully matches. The results of any matched parsers is discarded. // Should only return nil or non-ParseError errors. RunSkipParsers() error // Sets the name of all subsequent parsers. SetCurParserName(name string) // Gets the current name of parsers. GetCurParserName() string // Sets the look offset value. SetLookOffset(val int) // Gets the look offset value. GetLookOffset() int // TODO: document DebugStart(format string, formatArgs ...interface{}) DebugPrint(format string, formatArgs ...interface{}) DebugEnd(format string, formatArgs ...interface{}) SetUserData(data any) GetUserData() any }
Context[CT] holds the current state of some input parsing stream of type CT, and provides methods to peek the input stream, consume it, get the current Origin of the input stream, etc.
Also allows for parsers to be added/removed that will skip matched input.
type EOFError ¶
type EOFError struct{}
EOFError represents that the end of a file or input has been reached.
type MapDetailedFunc ¶
type MapDetailedFunc[T, U any] func(node T, originRange OriginRange) (U, error)
MapDetailedFunc is a function that maps some type T to some type U while allowing for error reporting and knowledge of the Origin.
type MapFunc ¶
type MapFunc[T, U any] func(node T) U
MapFunc is a function that maps some type T to some type U.
type MaybeValue ¶
type MaybeValue[T any] struct { // contains filtered or unexported fields }
Represents any value along with if the value is nil. Useful when working with non-pointer types.
func NewMaybeValue ¶
func NewMaybeValue[T any](value T) MaybeValue[T]
Returns a MaybeValue that returns the provide value for Value().
func NewNilMaybeValue ¶
func NewNilMaybeValue[T any]() MaybeValue[T]
Returns a MaybeValue that returns true for IsNil().
func (MaybeValue[T]) IsNil ¶
func (val MaybeValue[T]) IsNil() bool
Returns true if the value represented is nil.
func (MaybeValue[T]) Value ¶
func (val MaybeValue[T]) Value() T
Returns the value represented. Panics if IsNil().
type Origin ¶
type Origin struct { // The name of the origin (usually filename). Name string // The line number location. LineNum int // The column number location. ColNum int }
Origin represents a line and column number location in some source.
type OriginRange ¶
Holds a start and end Origin.
type ParseConfig ¶
type ParseConfig struct { // If true, parsing will fail if there is remaining input in the Context after parsing. MustParseToEOF bool }
ParseConfig contains settings that can be passed to the Parse function.
type ParseError ¶
type ParseError struct { // The optional error to wrap. Err error // The error message. Message string // The Origin of the error. Origin Origin }
ParseError represents a parser that could not match input and has NOT consumed any input.
func ParseErrExpectedButGot ¶
func ParseErrExpectedButGot[CT any](ctx Context[CT], expected interface{}, got interface{}, wrapErr error) *ParseError
Returns a ParseError with an error message in the format of "expected but got".
func ParseErrExpectedButGotNext ¶
func ParseErrExpectedButGotNext[CT any](ctx Context[CT], expected interface{}, wrapErr error) *ParseError
Returns a ParseError with an error message in the format of "expected but got" where got is the next N input runes (truncated).
func (*ParseError) Is ¶
func (err *ParseError) Is(target error) bool
Returns true if target is also a ParseError.
type ParseErrorConsumed ¶
type ParseErrorConsumed struct { // The optional error to wrap. Err error // The error message. Message string // The Origin of the error. Origin Origin }
ParseErrorConsumed represents a parser that could not match input and HAS consumed some input.
func ParseErrConsumedExpectedButGot ¶
func ParseErrConsumedExpectedButGot[CT any](ctx Context[CT], expected interface{}, got interface{}, wrapErr error) *ParseErrorConsumed
func ParseErrConsumedExpectedButGotNext ¶
func ParseErrConsumedExpectedButGotNext[CT any](ctx Context[CT], expected interface{}, wrapErr error) *ParseErrorConsumed
Returns a ParseError with an error message in the format of "expected but got" where got is the next N input runes (truncated).
func (*ParseErrorConsumed) Is ¶
func (err *ParseErrorConsumed) Is(target error) bool
Returns true if target is also a ParseErrorConsumed.
func (*ParseErrorConsumed) Unwrap ¶
func (err *ParseErrorConsumed) Unwrap() error
Unwraps this error.
type ParseReader ¶
type ParseReader[CT, T any] struct { // contains filtered or unexported fields }
Implements ReaderWithOrigin[T] by calling the provided parser with the provided ctx each time Read is called.
func NewParseReader ¶
func NewParseReader[CT, T any](ctx Context[CT], parser Parser[CT, T]) *ParseReader[CT, T]
Returns a *ParseReader[CT, T] with the provided ctx and parser.
func (*ParseReader[CT, T]) Read ¶
func (r *ParseReader[CT, T]) Read() (T, Origin, error)
Calls the parser with the corresponding ctx, returning the result and Origin of the result. If an error occurs or if no element is available, an error is returned.
type Parser ¶
Parser[CT, T] represents a parser that takes a Context[CT] and returns a result of type T or an error.
Should return a nil error if the result was parsed and consumed. Should return a ParseError error if parsing failed, and no input was consumed. Should return a ParseErrorConsumed error if parsing failed, but some input was consumed. Any other error type may be returned, and is treated like ParseErrorConsumed.
Any terminal parser (such as Exact or Regex) should call ctx.RunSkipParsers first.
func Any ¶
Returns a parser that attempts to parse, in order, the provided parsers. Returns the result of the first successful parser.
func Bind ¶
Returns a parser that maps a Parser[CT, T] into a Parser[CT, U] by always returning node.
func BindToToken ¶
Returns a parser that maps a Parser[CT, T] into a Parser[CT, Token] by returning a new Token with Type tokenType and Value being the result of parser.
func CastTo ¶
Returns a parser that maps a Parser[CT, T] into a Parser[CT, U] by casting the result of parser to type U.
func Exact ¶
Returns a parser that succeeds if peeking 1 element from the Context equals value, returning value as the result.
func ExactSlice ¶
Returns a parser that succeeds if peeking elements from the Context equals value, returning value as the result.
func ExactStr ¶
Equivalent to ExactSlice but for a Context[rune]. Implicitly converts value to []rune.
func ExactTokenType ¶
Returns a parser that succeeds if the next peeked token from the Context[Token] has a Type that is tokenType.
func ExactTokenValue ¶
Returns a parser that succeeds if the next peeked token from the Context[Token] has a Type that is tokenType and a Value that is value.
func Look ¶
Provides backtracking support for the provided parser. If an error occurs, any consumptions made are reverted to the state of the context when this Look parser is called. If no error occurs, any consumptions made are committed to the current Look frame.
func Map ¶
Returns a parser that maps a Parser[CT, T] into a Parser[CT, U] by running the result of parser through mapFunc.
func MapDetailed ¶
func MapDetailed[CT, T, U any](parser Parser[CT, T], mapFunc MapDetailedFunc[T, U]) Parser[CT, U]
Returns a parser that maps a Parser[CT, T] into a Parser[CT, U] by running the result of parser through mapFunc. This version of Map provides the ability to report errors and have knowledge of the Origin.
func MapTokenToValue ¶
Returns a parser that maps a Parser[CT, Token] into a Parser[CT, T] by returning the Value of the parser result.
func Maybe ¶
func Maybe[CT, T any](parser Parser[CT, T]) Parser[CT, MaybeValue[T]]
Same as Range(0, 1, parser), but with the resulting slice mapped to a single value, or default T if 0 matches occurred.
func OneOrMoreSeparated ¶
func OneOrMoreSeparated[CT, T, U any](parser Parser[CT, T], sepParser Parser[CT, U]) Parser[CT, []T]
Same as OneOrMore(parser), but ensures that each subsequent match is separated by a successful parse by sepParser. The results of sepParser are not returned.
func Range ¶
Returns a parser that runs parser at least min, but at most max, times. Returns each parser result in order as a slice.
The min must be >= 0, and max must be > 0. Unless max == -1, in which case no maximum is set.
func Ref ¶
Creates a Parser[CT, T] from a *Parser[CT, T].
Useful for avoiding circular variable dependencies. For example:
var value = Any(CastToAny(ExactStr("hello")), CastToAny(hashValue)) var hashValue = Seq(CastToAny(ExactStr("#")), value)
Is invalid, however this can be remedied by:
var value Parser[rune, any] var valueRef = Ref(&value) var hashValue = Seq(CastToAny(ExactStr("#")), valueRef) // At runtime, in some initialization function: value = Any(CastToAny(ExactStr("hello")), hashValue)
func Regex ¶
Returns a parser that succeeds if peeking elements from the Context[rune] matches the provided regex pattern, returning the match as the result.
Note that the regex is always normalized to contain '^' as the starting symbol, to always match the left-most character in the input stream.
func Seq ¶
Returns a parser that parses all provided parsers in order. Returns each parser result in order as a slice.
The number of parsers provided must be at least 2.
func Seq2 ¶
func Seq2[CT, T1, T2 any](parser1 Parser[CT, T1], parser2 Parser[CT, T2]) Parser[CT, *Seq2Node[T1, T2]]
Returns a parser that parses all provided parsers in order. This is the same as Seq, but is optimized for N parsers of different types. Returns each parser result in the corresponding typed result field.
func Seq3 ¶
func Seq3[CT, T1, T2, T3 any](parser1 Parser[CT, T1], parser2 Parser[CT, T2], parser3 Parser[CT, T3]) Parser[CT, *Seq3Node[T1, T2, T3]]
Returns a parser that parses all provided parsers in order. This is the same as Seq, but is optimized for N parsers of different types. Returns each parser result in the corresponding typed result field.
func Seq4 ¶
func Seq4[CT, T1, T2, T3, T4 any](parser1 Parser[CT, T1], parser2 Parser[CT, T2], parser3 Parser[CT, T3], parser4 Parser[CT, T4]) Parser[CT, *Seq4Node[T1, T2, T3, T4]]
Returns a parser that parses all provided parsers in order. This is the same as Seq, but is optimized for N parsers of different types. Returns each parser result in the corresponding typed result field.
func Seq5 ¶
func Seq5[CT, T1, T2, T3, T4, T5 any](parser1 Parser[CT, T1], parser2 Parser[CT, T2], parser3 Parser[CT, T3], parser4 Parser[CT, T4], parser5 Parser[CT, T5]) Parser[CT, *Seq5Node[T1, T2, T3, T4, T5]]
Returns a parser that parses all provided parsers in order. This is the same as Seq, but is optimized for N parsers of different types. Returns each parser result in the corresponding typed result field.
func Seq6 ¶
func Seq6[CT, T1, T2, T3, T4, T5, T6 any](parser1 Parser[CT, T1], parser2 Parser[CT, T2], parser3 Parser[CT, T3], parser4 Parser[CT, T4], parser5 Parser[CT, T5], parser6 Parser[CT, T6]) Parser[CT, *Seq6Node[T1, T2, T3, T4, T5, T6]]
Returns a parser that parses all provided parsers in order. This is the same as Seq, but is optimized for N parsers of different types. Returns each parser result in the corresponding typed result field.
func Seq7 ¶
func Seq7[CT, T1, T2, T3, T4, T5, T6, T7 any](parser1 Parser[CT, T1], parser2 Parser[CT, T2], parser3 Parser[CT, T3], parser4 Parser[CT, T4], parser5 Parser[CT, T5], parser6 Parser[CT, T6], parser7 Parser[CT, T7]) Parser[CT, *Seq7Node[T1, T2, T3, T4, T5, T6, T7]]
Returns a parser that parses all provided parsers in order. This is the same as Seq, but is optimized for N parsers of different types. Returns each parser result in the corresponding typed result field.
func Seq8 ¶
func Seq8[CT, T1, T2, T3, T4, T5, T6, T7, T8 any](parser1 Parser[CT, T1], parser2 Parser[CT, T2], parser3 Parser[CT, T3], parser4 Parser[CT, T4], parser5 Parser[CT, T5], parser6 Parser[CT, T6], parser7 Parser[CT, T7], parser8 Parser[CT, T8]) Parser[CT, *Seq8Node[T1, T2, T3, T4, T5, T6, T7, T8]]
Returns a parser that parses all provided parsers in order. This is the same as Seq, but is optimized for N parsers of different types. Returns each parser result in the corresponding typed result field.
func Skip ¶
Returns a parser that temporarily adds the skipParser to the Context while parsing with parser.
func Unskip ¶
Returns a parser that temporarily removes the skipParser from the Context while parsing with parser.
func ZeroOrMore ¶
Same as Range(0, -1, parser).
type ReaderContext ¶
type ReaderContext[CT any] struct { // Whether or not to enable debugging. DebugParsers bool // contains filtered or unexported fields }
ReaderContext[CT] implements Context[CT] by operating with a ReaderWithOrigin[CT].
func NewBinaryFileContext ¶
func NewBinaryFileContext(file *os.File) *ReaderContext[byte]
Returns a *ReaderContext[byte] from a file.
func NewByteReaderContext ¶
func NewByteReaderContext(originName string, reader io.Reader) *ReaderContext[byte]
Returns a *ReaderContext[byte] from an io.Reader.
func NewFileContext ¶
func NewFileContext(file *os.File) *ReaderContext[rune]
Returns a *ReaderContext[rune] from a file.
func NewReaderContext ¶
func NewReaderContext[CT any](reader ReaderWithOrigin[CT]) *ReaderContext[CT]
Returns a *ReaderContext[CT] with the given reader.
func NewRuneReaderContext ¶
func NewRuneReaderContext(originName string, reader io.RuneReader) *ReaderContext[rune]
Returns a *ReaderContext[rune] from an io.RuneReader.
func NewStringContext ¶
func NewStringContext(originName string, data string) *ReaderContext[rune]
Returns a *ReaderContext[rune] from a string.
func (*ReaderContext[CT]) AddSkipParser ¶
func (ctx *ReaderContext[CT]) AddSkipParser(parser Parser[CT, any])
Adds the parser to the list of parsers that attempt to run when RunSkipParsers is called. If the parser matches, its result will be discarded. Duplicate parsers cannot be added.
func (*ReaderContext[CT]) Consume ¶
func (ctx *ReaderContext[CT]) Consume(num int) ([]CT, error)
Advances the input stream by num elements, returning the consumed elements.
If the end of input is reached, an EOFError is returned along with any consumed elements (which may be less than num elements in length if end of input has been reached).
func (*ReaderContext[CT]) DebugEnd ¶
func (ctx *ReaderContext[CT]) DebugEnd(format string, formatArgs ...interface{})
TODO: document
func (*ReaderContext[CT]) DebugPrint ¶
func (ctx *ReaderContext[CT]) DebugPrint(format string, formatArgs ...interface{})
TODO: document
func (*ReaderContext[CT]) DebugStart ¶
func (ctx *ReaderContext[CT]) DebugStart(format string, formatArgs ...interface{})
TODO: document
func (*ReaderContext[CT]) GetCurOrigin ¶
func (ctx *ReaderContext[CT]) GetCurOrigin() Origin
Returns an Origin representing the next unconsumed element in the input stream.
func (*ReaderContext[CT]) GetCurParserName ¶
func (ctx *ReaderContext[CT]) GetCurParserName() string
Gets the current name of parsers.
func (*ReaderContext[CT]) GetLookOffset ¶
func (ctx *ReaderContext[CT]) GetLookOffset() int
Gets the look value.
func (*ReaderContext[CT]) GetUserData ¶
func (ctx *ReaderContext[CT]) GetUserData() any
func (*ReaderContext[CT]) Peek ¶
func (ctx *ReaderContext[CT]) Peek(offset int, num int) ([]CT, error)
Returns a []CT of num elements beginning at offset without consuming the elements. The offset is a non-negative value relative to the next unconsumed element in the input stream.
If the end of input is reached, an EOFError is returned along with any peeked elements (which may be less than num elements in length if end of input has been reached).
func (*ReaderContext[CT]) RemoveSkipParser ¶
func (ctx *ReaderContext[CT]) RemoveSkipParser(parser Parser[CT, any])
Removes the parser from the list of parsers that attempt to run when RunSkipParsers is called. If the parser has not been added, the function panics.
func (*ReaderContext[CT]) RunSkipParsers ¶
func (ctx *ReaderContext[CT]) RunSkipParsers() error
Attempts to run any added skip parsers as long as one of the parsers successfully matches. The results of any matched parsers is discarded. Should only return nil or non-ParseError errors.
func (*ReaderContext[CT]) SetCurParserName ¶
func (ctx *ReaderContext[CT]) SetCurParserName(name string)
Sets the name of all subsequent parsers.
func (*ReaderContext[CT]) SetLookOffset ¶
func (ctx *ReaderContext[CT]) SetLookOffset(val int)
Sets the look value.
func (*ReaderContext[CT]) SetUserData ¶
func (ctx *ReaderContext[CT]) SetUserData(data any)
type ReaderWithOrigin ¶
type ReaderWithOrigin[T any] interface { // Returns the next element in a stream of elements of type T, // along with the Origin associated with the element. // If an error occurs or if no element is available, an error is returned. Read() (T, Origin, error) }
A reader that provides an Origin per read element of type T.
type RuneContextPeekingRuneReader ¶
type RuneContextPeekingRuneReader struct { Context Context[rune] // contains filtered or unexported fields }
RuneContextPeekingRuneReader implements io.RuneReader using the given Context[rune] by peeking one rune at a time, starting at offset 0.
type RuneReaderWithOrigin ¶
type RuneReaderWithOrigin struct {
// contains filtered or unexported fields
}
Implements ReaderWithOrigin[rune] by calling reader.ReadRune.
func NewRuneReaderWithOrigin ¶
func NewRuneReaderWithOrigin(originName string, reader io.RuneReader) *RuneReaderWithOrigin
Returns a *RuneReaderWithOrigin with the provided origin name and reader.
type Seq2Node ¶
type Seq2Node[T1, T2 any] struct { Result1 T1 Result2 T2 }
Seq2Node holds 2 generically-typed results.
type Seq3Node ¶
type Seq3Node[T1, T2, T3 any] struct { Result1 T1 Result2 T2 Result3 T3 }
Seq3Node holds 3 generically-typed results.
type Seq4Node ¶
type Seq4Node[T1, T2, T3, T4 any] struct { Result1 T1 Result2 T2 Result3 T3 Result4 T4 }
Seq4Node holds 4 generically-typed results.
type Seq5Node ¶
type Seq5Node[T1, T2, T3, T4, T5 any] struct { Result1 T1 Result2 T2 Result3 T3 Result4 T4 Result5 T5 }
Seq5Node holds 5 generically-typed results.
type Seq6Node ¶
type Seq6Node[T1, T2, T3, T4, T5, T6 any] struct { Result1 T1 Result2 T2 Result3 T3 Result4 T4 Result5 T5 Result6 T6 }
Seq6Node holds 6 generically-typed results.
type Seq7Node ¶
type Seq7Node[T1, T2, T3, T4, T5, T6, T7 any] struct { Result1 T1 Result2 T2 Result3 T3 Result4 T4 Result5 T5 Result6 T6 Result7 T7 }
Seq7Node holds 7 generically-typed results.
type Seq8Node ¶
type Seq8Node[T1, T2, T3, T4, T5, T6, T7, T8 any] struct { Result1 T1 Result2 T2 Result3 T3 Result4 T4 Result5 T5 Result6 T6 Result7 T7 Result8 T8 }
Seq8Node holds 8 generically-typed results.