btparser

package
v1.3.13 Latest Latest
Warning

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

Go to latest
Published: May 12, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NoMatchErr = fmt.Errorf("no match")

Functions

func BoolErrFn

func BoolErrFn[T any](fn func(T) bool) func(T) error

func BytesSnippet

func BytesSnippet(src []byte, mp MPos, pad int) string

func FatalError

func FatalError(err error) error

func FatalError2

func FatalError2(tag string, err error) error

func IsFatalError

func IsFatalError(err error) bool

IsFatalError is in the parser hot path, so keep it as a shallow check and preserve fatal wrapping at API boundaries.

func IterateStructFields

func IterateStructFields(tag string, v any, byPtr bool) iter.Seq2[string, any]

func ReverseString

func ReverseString(s string) string

func UserDataPtrFn

func UserDataPtrFn[T any](key string) func(*ParserState) *T

Types

type MFn

type MFn func(*ParserState, Pos) (MPos, error)

func AppendFn

func AppendFn[T any](dst func(*ParserState) *[]T, fn VFn[T]) MFn

func AppendLocal

func AppendLocal[T any](w *[]T, fn VFn[T]) MFn

func Assign2Fn

func Assign2Fn[T any](dst func(*ParserState) **T, fn VFn[T]) MFn

func Assign2Local

func Assign2Local[T any](v **T, fn VFn[T]) MFn

func AssignFn

func AssignFn[T any](dst func(*ParserState) *T, fn VFn[T]) MFn

func AssignLocal

func AssignLocal[T any](v *T, fn VFn[T]) MFn

func Debug2

func Debug2(fn func()) MFn

type MPos

type MPos struct {
	Start Pos
	End   Pos
}

MPos (match position) stores the start position passed to a rule and the end position returned by it; rules may move forward or backward, so End can be lower than Start.

func (MPos) Bounds

func (mp MPos) Bounds() (Pos, Pos)

type ParserState

type ParserState struct {
	UserData map[string]any

	// Ignore is used by Token to skip input before matching; set it before parsing and do not modify it while a parse is running.
	Ignore MFn
	// contains filtered or unexported fields
}

func NewParserStateFromBytes

func NewParserStateFromBytes(src []byte) *ParserState

func NewParserStateFromString

func NewParserStateFromString(s string) *ParserState

func (*ParserState) Snippet

func (ps *ParserState) Snippet(mp MPos) string

func (*ParserState) Source

func (ps *ParserState) Source(mp MPos) []byte

func (*ParserState) SourceStr

func (ps *ParserState) SourceStr(mp MPos) string

type Pos

type Pos int

type Rules

type Rules struct{}

Rules is the public API for building and running parser rules. ParserState holds the source, ignore rule, and runtime state for a single parse. Built rules are MFn values and can be reused with different ParserState values. When building parsers, keep construction helpers separate from grammar rules: helpers may read/write parse state, create values from MPos, or wrap a provided rule with an action, but should not hide the grammar shape they receive. Prefer the pattern "helpers first, separator, grammar block after": define helpers such as data(ps), newArg(ps,mp), appendArg(fn), or assignPath(fn), then after //---------- define visible rules such as quotedString, spaces, arg, part, parts, and the final parse rule. Example: appendField(field) belongs above the separator because it only stores a matched value, while field := g.Loop1(g.Or(g.Escape(esc), g.QuotedString2(...), itemRune)) belongs below the separator because it describes the grammar.

func NewRules

func NewRules() Rules

func (Rules) And

func (g Rules) And(fns ...MFn) MFn

func (Rules) AnyExceptNewline

func (g Rules) AnyExceptNewline() MFn

func (Rules) AnyRune

func (g Rules) AnyRune() MFn

func (Rules) AsciiLetter

func (g Rules) AsciiLetter() MFn

func (Rules) Bool

func (g Rules) Bool() MFn

func (Rules) BruteCoverPos

func (g Rules) BruteCoverPos(backward, forward MFn) MFn

BruteCoverPos uses backward func + brute forward attempts until covering pos.

func (Rules) BruteCoverPosEnd

func (g Rules) BruteCoverPosEnd(backward, forward MFn) MFn

BruteCoverPosEnd is like BruteCoverPos, but also matches when forward ends exactly at pos.

func (Rules) Byte

func (g Rules) Byte(b byte) MFn

func (Rules) ByteFn

func (g Rules) ByteFn(fn func(byte) bool) MFn

func (Rules) Debug

func (g Rules) Debug(on bool, prefix string, fn MFn) MFn

func (Rules) DebugAnd

func (g Rules) DebugAnd(on bool, prefix string, fns ...MFn) MFn

func (Rules) DebugOr

func (g Rules) DebugOr(on bool, prefix string, fns ...MFn) MFn

func (Rules) Digit

func (g Rules) Digit() MFn

func (Rules) DigitNotZero

func (g Rules) DigitNotZero() MFn

func (Rules) Digits

func (g Rules) Digits() MFn

func (Rules) EmptyLinesExceptNewline

func (g Rules) EmptyLinesExceptNewline(ignore MFn) MFn

func (Rules) Eof

func (g Rules) Eof() MFn

func (Rules) EofAbs

func (g Rules) EofAbs() MFn

EofAbs matches only at the absolute end of the source.

func (Rules) Escape

func (g Rules) Escape(esc rune) MFn

func (Rules) Fail

func (g Rules) Fail() MFn

func (Rules) FatalOnError

func (g Rules) FatalOnError(tag string, fn MFn) MFn

func (Rules) Float

func (g Rules) Float() MFn

func (Rules) Float2

func (g Rules) Float2(sep rune) MFn

func (Rules) HexBytes

func (g Rules) HexBytes() MFn

func (Rules) Identifier

func (g Rules) Identifier() MFn

func (Rules) Integer

func (g Rules) Integer() MFn

func (Rules) IsTrue

func (g Rules) IsTrue(v bool) MFn

func (Rules) LastAnyRune

func (g Rules) LastAnyRune() MFn

func (Rules) Letter

func (g Rules) Letter() MFn

func (Rules) LineComment1

func (g Rules) LineComment1(open string) MFn

func (Rules) Loop1

func (g Rules) Loop1(fn MFn) MFn

func (Rules) LoopConsumeUntil

func (g Rules) LoopConsumeUntil(consumeFn, untilFn MFn) MFn

func (Rules) LoopSep

func (g Rules) LoopSep(optLastSep bool, fn, sepFn MFn) MFn

func (Rules) LoopSepAllowEmpty

func (g Rules) LoopSepAllowEmpty(fn, sepFn MFn) MFn

func (Rules) LoopToNLOrEof

func (g Rules) LoopToNLOrEof(esc rune, includeNL bool) MFn

func (Rules) MaxNRunes

func (g Rules) MaxNRunes(n int) MFn

func (Rules) NRunes

func (g Rules) NRunes(n int) MFn

func (Rules) Newline

func (g Rules) Newline() MFn

func (Rules) NoOp

func (g Rules) NoOp() MFn

func (Rules) NoOp2

func (g Rules) NoOp2(fn func() error) MFn

func (Rules) Not

func (g Rules) Not(fn MFn) MFn

func (Rules) Optional

func (g Rules) Optional(fn MFn) MFn

func (Rules) Or

func (g Rules) Or(fns ...MFn) MFn

func (Rules) Parse

func (g Rules) Parse(ps *ParserState, fn MFn) (Pos, error)

func (Rules) ParseAt

func (g Rules) ParseAt(ps *ParserState, pos Pos, fn MFn) (Pos, error)

func (Rules) Peek

func (g Rules) Peek(fn MFn) MFn

func (Rules) PeekBackN

func (g Rules) PeekBackN(n int, fn MFn) MFn

func (Rules) QuotedSection

func (g Rules) QuotedSection(quote string, esc rune, consume MFn) MFn

func (Rules) QuotedString1

func (g Rules) QuotedString1() MFn

func (Rules) QuotedString2

func (g Rules) QuotedString2(esc rune, maxLen1, maxLen2 int) MFn

func (Rules) ReverseSource

func (g Rules) ReverseSource(fn MFn) MFn

func (Rules) Rune

func (g Rules) Rune(ru rune) MFn

func (Rules) RuneAnyOf

func (g Rules) RuneAnyOf(rus ...rune) MFn

func (Rules) RuneAnyOfString

func (g Rules) RuneAnyOfString(s string) MFn

func (Rules) RuneFn

func (g Rules) RuneFn(fn func(rune) bool) MFn

func (Rules) RuneNotAnyOf

func (g Rules) RuneNotAnyOf(rus ...rune) MFn

func (Rules) Section

func (g Rules) Section(open, close string, esc rune, newlineCloses, eofCloses bool, consume MFn) MFn

func (Rules) Seq

func (g Rules) Seq(s string) MFn

func (Rules) SeqOrMid

func (g Rules) SeqOrMid(s string) MFn

func (Rules) Sign

func (g Rules) Sign() MFn

func (Rules) Sof

func (g Rules) Sof() MFn

Sof matches only at the start of the current source bounds.

func (Rules) SofAbs

func (g Rules) SofAbs() MFn

SofAbs matches only at the absolute start of the source.

func (Rules) Sop

func (g Rules) Sop() MFn

Sop matches only at the start position of the current parse.

func (Rules) Space

func (g Rules) Space() MFn

func (Rules) Spaces

func (g Rules) Spaces() MFn

func (Rules) SpacesExceptNewline

func (g Rules) SpacesExceptNewline() MFn

func (Rules) ToIndexByte

func (g Rules) ToIndexByte(b byte) MFn

func (Rules) ToIndexByteOrEnd

func (g Rules) ToIndexByteOrEnd(b byte) MFn

func (Rules) ToLastIndexByte

func (g Rules) ToLastIndexByte(b byte) MFn

func (Rules) ToLastIndexByteOrStart

func (g Rules) ToLastIndexByteOrStart(b byte) MFn

func (Rules) Token

func (g Rules) Token(fn MFn) MFn

func (Rules) UnicodeLetter

func (g Rules) UnicodeLetter() MFn

func (Rules) VBool

func (g Rules) VBool() VFn[bool]

func (Rules) VByte

func (g Rules) VByte() VFn[byte]

func (Rules) VBytes

func (g Rules) VBytes(fn MFn) VFn[[]byte]

func (Rules) VFloat

func (g Rules) VFloat() VFn[float64]

func (Rules) VIdentifier

func (g Rules) VIdentifier() VFn[string]

func (Rules) VInteger

func (g Rules) VInteger() VFn[int]

func (Rules) VLastRune

func (g Rules) VLastRune() VFn[rune]

func (Rules) VQuotedString1

func (g Rules) VQuotedString1() VFn[string]

func (Rules) VRune

func (g Rules) VRune() VFn[rune]

func (Rules) VString

func (g Rules) VString(fn MFn) VFn[string]

func (Rules) VTime

func (g Rules) VTime(fmt string) VFn[time.Time]

func (Rules) WithBounds

func (g Rules) WithBounds(back, forward int, fn MFn) MFn

func (Rules) WithBoundsAbs

func (g Rules) WithBoundsAbs(start, end Pos, fn MFn) MFn

func (Rules) WithLineBounds

func (g Rules) WithLineBounds(back, forward int, fn MFn) MFn

type VFn

type VFn[T any] func(*ParserState, Pos) (T, MPos, error)

func VAny

func VAny[T any](fn VFn[T]) VFn[any]

func VAppend

func VAppend[T any](fn VFn[T]) VFn[[]T]

func VCast

func VCast[T, U any](fn VFn[U]) VFn[T]

func VConst

func VConst[T any](fn MFn, v T) VFn[T]

func VFromMPos

func VFromMPos[T any](fn MFn, makeFn func(*ParserState, MPos) T) VFn[T]

func VOr

func VOr[T any](fns ...VFn[T]) VFn[T]

func VToken

func VToken[T any](fn VFn[T]) VFn[T]

type VHandler

type VHandler[T any] func(T) error

type VMaker

type VMaker[T any] func(MPos) (T, error)

Jump to

Keyboard shortcuts

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