Documentation ¶
Index ¶
- func FilterErrorLeaves[O any](h *CurrentEval[O]) bool
- func FilterIncompleteLeaves[O any](h *CurrentEval[O]) bool
- func FilterIncompleteTokens[O any](h []*CurrentEval[O]) bool
- func HelperWeightFunc[O any](h []*CurrentEval[O]) (float64, bool)
- type CurrentEval
- type ErrAllMatchesFailed
- type ErrEmptyInput
- type ErrInvalidElement
- type ErrNilRoot
- type EvalStatus
- type FilterBranchesFunc
- type MatchResulter
- type Matcher
- type TreeEvaluator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FilterErrorLeaves ¶
func FilterErrorLeaves[O any](h *CurrentEval[O]) bool
FilterErrorLeaves is a filter that filters out leaves that are in error.
Parameters:
- leaf: The leaf to filter.
Returns:
- bool: True if the leaf is in error, false otherwise.
func FilterIncompleteLeaves ¶
func FilterIncompleteLeaves[O any](h *CurrentEval[O]) bool
FilterIncompleteLeaves is a filter that filters out incomplete leaves.
Parameters:
- leaf: The leaf to filter.
Returns:
- bool: True if the leaf is incomplete, false otherwise.
func FilterIncompleteTokens ¶
func FilterIncompleteTokens[O any](h []*CurrentEval[O]) bool
FilterIncompleteTokens is a filter that filters out incomplete tokens.
Parameters:
- h: The helper tokens to filter.
Returns:
- bool: True if the helper tokens are incomplete, false otherwise.
func HelperWeightFunc ¶
func HelperWeightFunc[O any](h []*CurrentEval[O]) (float64, bool)
HelperWeightFunc is a weight function that returns the length of the helper tokens.
Parameters:
- h: The helper tokens to weigh.
Returns:
- float64: The weight of the helper tokens.
- bool: True if the weight is valid, false otherwise.
Types ¶
type CurrentEval ¶
type CurrentEval[T any] struct { // Status is the status of the current evaluation. Status EvalStatus // Elem is the element of the current evaluation. Elem T }
CurrentEval is a struct that holds the current evaluation of the TreeExplorer.
func NewCurrentEval ¶
func NewCurrentEval[T any](elem T) *CurrentEval[T]
NewCurrentEval creates a new CurrentEval with the given element.
Parameters:
- elem: The element of the CurrentEval.
Returns:
- *CurrentEval: The new CurrentEval.
func (*CurrentEval[T]) GetElem ¶
func (ce *CurrentEval[T]) GetElem() T
GetElem returns the element of the CurrentEval.
Returns:
- T: The element of the CurrentEval.
func (*CurrentEval[T]) GetStatus ¶
func (ce *CurrentEval[T]) GetStatus() EvalStatus
GetStatus returns the status of the CurrentEval.
Returns:
- EvalStatus: The status of the CurrentEval.
func (*CurrentEval[T]) SetStatus ¶
func (ce *CurrentEval[T]) SetStatus(status EvalStatus)
SetStatus sets the status of the CurrentEval.
Parameters:
- status: The status to set.
func (*CurrentEval[T]) String ¶
func (ce *CurrentEval[T]) String() string
String is a method of fmt.Stringer that returns the string representation of the CurrentEval.
Returns:
- string: The string representation of the CurrentEval.
type ErrAllMatchesFailed ¶
type ErrAllMatchesFailed struct{}
ErrAllMatchesFailed is an error that is returned when all matches fail.
func NewErrAllMatchesFailed ¶
func NewErrAllMatchesFailed() *ErrAllMatchesFailed
NewErrAllMatchesFailed creates a new error of type *ErrAllMatchesFailed.
Returns:
- *ErrAllMatchesFailed: The new error.
func (*ErrAllMatchesFailed) Error ¶
func (e *ErrAllMatchesFailed) Error() string
Error returns the error message: "all matches failed".
Returns:
- string: The error message.
type ErrEmptyInput ¶
type ErrEmptyInput struct{}
ErrEmptyInput is an error that is returned when the input is empty.
func NewErrEmptyInput ¶
func NewErrEmptyInput() *ErrEmptyInput
NewErrEmptyInput creates a new error of type *ErrEmptyInput.
Returns:
- *ErrEmptyInput: The new error.
func (*ErrEmptyInput) Error ¶
func (e *ErrEmptyInput) Error() string
Error returns the error message: "empty input".
Returns:
- string: The error message.
type ErrInvalidElement ¶
type ErrInvalidElement struct{}
ErrInvalidElement is an error that is returned when an invalid element is found.
func NewErrInvalidElement ¶
func NewErrInvalidElement() *ErrInvalidElement
NewErrInvalidElement creates a new error of type *ErrInvalidElement.
Returns:
- *ErrInvalidElement: The new error.
func (*ErrInvalidElement) Error ¶
func (e *ErrInvalidElement) Error() string
Error returns the error message: "invalid element".
Returns:
- string: The error message.
type ErrNilRoot ¶
type ErrNilRoot struct{}
ErrNilRoot is an error that is returned when the root is nil.
func NewErrNilRoot ¶
func NewErrNilRoot() *ErrNilRoot
NewErrNilRoot creates a new error of type *ErrNilRoot.
Returns:
- *ErrNilRoot: The new error.
func (*ErrNilRoot) Error ¶
func (e *ErrNilRoot) Error() string
Error returns the error message: "root is nil".
Returns:
- string: The error message.
type EvalStatus ¶
type EvalStatus int8
EvalStatus represents the status of an evaluation.
const ( // EvalComplete represents a completed evaluation. EvalComplete EvalStatus = iota // EvalIncomplete represents an incomplete evaluation. EvalIncomplete // EvalError represents an evaluation that has an error. EvalError )
func (EvalStatus) String ¶
func (s EvalStatus) String() string
String is a method of fmt.Stringer that returns the string representation of the EvalStatus.
Returns:
- string: The string representation of the EvalStatus.
type FilterBranchesFunc ¶
type FilterBranchesFunc[O any] func(branches [][]*CurrentEval[O]) ([][]*CurrentEval[O], error)
FilterBranchesFunc is a function that filters branches.
Parameters:
- branches: The branches to filter.
Returns:
- [][]*CurrentEval: The filtered branches.
- error: An error if the branches are invalid.
type MatchResulter ¶
type MatchResulter[O any] interface { // GetMatch returns the match. // // Returns: // - O: The match. GetMatch() O }
MatchResult is an interface that represents a match result.
type Matcher ¶
type Matcher[R MatchResulter[O], O any] interface { // IsDone is a function that checks if the matcher is done. // // Parameters: // - from: The starting position of the match. // // Returns: // - bool: True if the matcher is done, false otherwise. IsDone(from int) bool // Match is a function that matches the element. // // Parameters: // - from: The starting position of the match. // // Returns: // - []R: The list of matched results. // - error: An error if the matchers cannot be created. Match(from int) ([]R, error) // SelectBestMatches selects the best matches from the list of matches. // Usually, the best matches' euristic is the longest match. // // Parameters: // - matches: The list of matches. // // Returns: // - []T: The best matches. SelectBestMatches(matches []R) []R // GetNext is a function that returns the next position of an element. // // Parameters: // - elem: The element to get the next position of. // // Returns: // - int: The next position of the element. GetNext(elem O) int }
Matcher is an interface that represents a matcher.
type TreeEvaluator ¶
type TreeEvaluator[R MatchResulter[O], M Matcher[R, O], O any] struct { // contains filtered or unexported fields }
TreeEvaluator is a tree evaluator that uses a grammar to tokenize a string.
func NewTreeEvaluator ¶
func NewTreeEvaluator[R MatchResulter[O], M Matcher[R, O], O any](filters ...FilterBranchesFunc[O]) *TreeEvaluator[R, M, O]
NewTreeEvaluator creates a new tree evaluator.
Parameters:
- matcher: The matcher that the tree evaluator will use.
Returns:
- *TreeEvaluator: A pointer to the new tree evaluator.
func (*TreeEvaluator[R, M, O]) Evaluate ¶
func (te *TreeEvaluator[R, M, O]) Evaluate(matcher M, root O) error
Evaluate is the main function of the tree evaluator.
Parameters:
- source: The source to evaluate.
- root: The root of the tree evaluator.
Returns:
- error: An error if lexing fails.
Errors:
- *ErrEmptyInput: The source is empty.
- *ers.ErrAt: An error occurred at a specific index.
- *ErrAllMatchesFailed: All matches failed.
func (*TreeEvaluator[R, M, O]) GetBranches ¶
func (te *TreeEvaluator[R, M, O]) GetBranches() ([][]*CurrentEval[O], error)
GetBranches returns the tokens that have been lexed.
Remember to use Lexer.RemoveToSkipTokens() to remove tokens that are not needed for the parser (i.e., marked as to skip in the grammar).
Returns:
- result: The tokens that have been lexed.
- reason: An error if the tree evaluator has not been run yet.