parser

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2024 License: GPL-3.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TokenRoleNone = TokenRole(iota)
	TokenRoleOperator
	TokenRoleKeyword
	TokenRoleNumber
	TokenRoleString
	TokenRoleComment
)
View Source
const (
	TokenRoleCustom1 = TokenRole((1 << 16) + iota)
	TokenRoleCustom2
	TokenRoleCustom3
	TokenRoleCustom4
	TokenRoleCustom5
	TokenRoleCustom6
	TokenRoleCustom7
	TokenRoleCustom8
	TokenRoleCustom9
	TokenRoleCustom10
	TokenRoleCustom11
	TokenRoleCustom12
	TokenRoleCustom13
	TokenRoleCustom14
	TokenRoleCustom15
	TokenRoleCustom16
)

Variables

View Source
var FailedResult = Result{}

FailedResult represents a failed parse.

Functions

This section is empty.

Types

type ComputedToken added in v0.3.0

type ComputedToken struct {
	// Offset is the token's start position,
	// defined relative to the computation's start position.
	Offset uint64
	Length uint64
	Role   TokenRole
}

ComputedToken is a token recognized by a computation.

type Edit

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

Edit represents a change to a document.

func NewDeleteEdit added in v0.3.0

func NewDeleteEdit(pos, numDeleted uint64) Edit

func NewInsertEdit added in v0.3.0

func NewInsertEdit(pos, numInserted uint64) Edit

type EmptyState added in v0.3.0

type EmptyState struct{}

EmptyState represents an empty state. An empty state is considered equal to every other empty state.

func (EmptyState) Equals added in v0.3.0

func (s EmptyState) Equals(other State) bool

type Func added in v0.3.0

type Func func(TrackingRuneIter, State) Result

Func incrementally parses a document into tokens.

It returns the number of tokens consumed and a slice of tokens. The output MUST be deterministic based solely on the input args.

Each invocation of the function is cached and may be reused when reparsing the document after an edit.

The returned tokens must be sequential, non-overlapping, have non-zero length, and have positions within the range of consumed characters.

Every successful parse must consume at least one rune.

The state parameter allows the parse func to track state across invocations. The initial state is always EmptyState. The parse func must return a non-nil state, which will be passed back to the parse func on the next invocation.

func (Func) Map added in v0.3.0

func (f Func) Map(mapFn MapFn) Func

Map maps a successful parse result to another parse result using mapFn.

func (Func) MapWithInput added in v0.3.0

func (f Func) MapWithInput(mapFn MapWithInputFn) Func

MapWithInput maps a successful parse to another parse result according to mapFn. The input iterator will output only runes consumed by the result before returning EOF.

func (Func) MaybeBefore added in v0.5.0

func (f Func) MaybeBefore(nextFn Func) Func

MaybeBefore produces a parse func that attempts `f` then `nextFn`, falling back to `nextFn` if `f` fails.

func (Func) Or added in v0.3.0

func (f Func) Or(nextFn Func) Func

Or produces a parse func that returns the result of `f` if it succeeds, or the result of `nextFn` if `f` fails.

func (Func) Then added in v0.3.0

func (f Func) Then(nextFn Func) Func

Then produces a parse func that succeeds if both `f` and `nextFn` succeed.

func (Func) ThenMaybe added in v0.3.0

func (f Func) ThenMaybe(nextFn Func) Func

ThenMaybe produces a parse func that succeeds if `f` succeeds, optionally followed by a successful result from `nextFn`.

func (Func) ThenNot added in v0.3.0

func (f Func) ThenNot(nextFn Func) Func

ThenNot produces a parse func that succeeds if `f` succeeds, followed by an unsuccessful parse from `nextFn`.

type MapFn added in v0.3.0

type MapFn func(Result) Result

MapFn maps a successful parse result to another parse result.

type MapWithInputFn added in v0.3.0

type MapWithInputFn func(Result, TrackingRuneIter, State) Result

MapWithInputFn maps a successful parse result to another parse result, using the original input (iter + state) that produced the first parse result.

type P added in v0.3.0

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

P parses a document into tokens. It caches the results from the last parse so it can efficiently reparse a document after an edit (insertion/deletion).

func New added in v0.3.0

func New(f Func) *P

New constructs a new parser for the language recognized by parseFunc.

func (*P) ParseAll added in v0.3.0

func (p *P) ParseAll(tree *text.Tree)

ParseAll parses the entire document.

func (*P) ReparseAfterEdit added in v0.3.0

func (p *P) ReparseAfterEdit(tree *text.Tree, edit Edit)

ReparseAfterEdit parses a document after an edit (insertion/deletion), re-using cached results from previous computations when possible. This should be called *after* at least one invocation of ParseAll(). It must be called for *every* edit to the document, otherwise the tokens may not match the current state of the document.

func (*P) TokenAtPosition added in v0.7.0

func (p *P) TokenAtPosition(pos uint64) Token

TokenAtPosition returns the token containing a position. If no such token exists, it returns the Token zero value.

func (*P) TokensIntersectingRange added in v0.3.0

func (p *P) TokensIntersectingRange(startPos, endPos uint64) []Token

TokensIntersectingRange returns tokens that overlap the interval [startPos, endPos)

type Result added in v0.3.0

type Result struct {
	NumConsumed    uint64
	ComputedTokens []ComputedToken
	NextState      State
}

Result represents the result of a single execution of a parse function.

func (Result) IsFailure added in v0.3.0

func (r Result) IsFailure() bool

IsFailure returns whether the parse failed.

func (Result) IsSuccess added in v0.3.0

func (r Result) IsSuccess() bool

IsSuccess returns whether the parse succeeded.

func (Result) ShiftForward added in v0.3.0

func (r Result) ShiftForward(n uint64) Result

ShiftForward shifts the result offsets forward by the specified number of positions.

type State added in v0.3.0

type State interface {
	// Equals returns whether two states are equal.
	Equals(other State) bool
}

State represents an arbitrary state of the parser.

type Token

type Token struct {
	Role     TokenRole
	StartPos uint64
	EndPos   uint64
}

Token represents a distinct element in a document.

type TokenRole

type TokenRole int

TokenRole represents the role a token plays in a document, as interpreted in a particular syntax language.

type TrackingRuneIter added in v0.3.0

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

TrackingReader tracks the number of runes read by a reader iter and all its clones. It updates a shared counter, so clones of this iter should NOT be used in other threads. Copying the struct produces a new, independent iterator.

func NewTrackingRuneIter added in v0.3.0

func NewTrackingRuneIter(reader text.Reader) TrackingRuneIter

NewTrackingRuneIter starts tracking an existing rune iter.

func (*TrackingRuneIter) Limit added in v0.6.0

func (iter *TrackingRuneIter) Limit(n uint64)

Limit sets the maximum number of runes this reader can produce.

func (*TrackingRuneIter) MaxRead added in v0.3.0

func (iter *TrackingRuneIter) MaxRead() uint64

MaxRead returns the maximum number of runes read by this iter and all its clones.

func (*TrackingRuneIter) NextRune added in v0.3.0

func (iter *TrackingRuneIter) NextRune() (rune, error)

NextRune returns the next rune from the underlying reader and advances the iterator.

func (*TrackingRuneIter) Skip added in v0.3.0

func (iter *TrackingRuneIter) Skip(n uint64) uint64

Skip advances the iterator by the specified number of positions or the end of the file, whichever comes first.

Jump to

Keyboard shortcuts

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