matcher

package
v0.1.22 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoClosestWordFound is an error when no closest word is found.
	ErrNoClosestWordFound error
)
View Source
var (
	// NoMatch is the error that occurs when the matcher does not match any rule. Readers
	// must return this error as is and not wrap it as callers are expected to check for
	// this error using ==.
	NoMatch error
)

Functions

This section is empty.

Types

type ErrNoMatch

type ErrNoMatch struct {
	// Reason is the reason of the error.
	Reason error
}

ErrNoMatch is the error that occurs when the matcher does not match any rule.

func NewErrNoMatch

func NewErrNoMatch(reason error) *ErrNoMatch

NewErrNoMatch creates a new error that occurs when the matcher does not match any rule.

Parameters:

  • reason: The reason of the error.

Returns:

  • *ErrNoMatch: The new error. Never returns nil.

func (ErrNoMatch) Error

func (e ErrNoMatch) Error() string

Error implements the error interface.

Message: "no match: <reason>"

func (ErrNoMatch) Unwrap

func (e ErrNoMatch) Unwrap() error

Unwrap returns the reason of the error.

Returns:

  • error: The reason of the error.

type LavenshteinTable

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

LevenshteinTable is a table of words for the Levenshtein distance.

func (*LavenshteinTable) AddWord

func (lt *LavenshteinTable) AddWord(word string) error

AddWord adds a word to the table.

Parameters:

  • word: The word to add.

Returns:

  • error: An error of type *ErrInvalidUTF8Encoding if the word is not valid UTF-8.

func (*LavenshteinTable) AddWords

func (lt *LavenshteinTable) AddWords(words []string) error

AddWords adds words to the table.

Parameters:

  • words: The words to add.

Returns:

  • error: An error of type *ints.ErrAt if the word is not valid UTF-8.

func (LavenshteinTable) Closest

func (lt LavenshteinTable) Closest(target []rune, limit int) (string, error)

Closest gets the closest word to a target.

Parameters:

  • target: The target.
  • limit: The max distance a word can have to be considered a match. Non-positive limit will cause all words to be ignored.

Returns:

  • string: The closest word.
  • error: The error if any occurs.

Errors:

  • *common.ErrInvalidParameter: If the target is empty.
  • *ErrNoClosestWordFound: If no closest word is found.

type MatchRule

type MatchRule[T RuleTyper] struct {
	// contains filtered or unexported fields
}

MatchRule is a rule to match.

func (MatchRule[T]) CharAt

func (r MatchRule[T]) CharAt(at int) (rune, bool)

CharAt returns the character at the given index.

Returns:

  • rune: The character at the given index.
  • bool: True if the index is valid, false otherwise.

type Matched

type Matched[T RuleTyper] struct {
	// contains filtered or unexported fields
}

Matched is the matched result.

func (Matched[T]) GetChars

func (m Matched[T]) GetChars() []rune

GetChars returns the matched characters.

Returns:

  • []rune: The matched characters.

func (Matched[T]) GetMatch

func (m Matched[T]) GetMatch() (T, string)

GetMatch returns the matched symbol and the matched characters.

Returns:

  • T: The matched symbol.
  • string: The matched characters.

func (Matched[T]) IsShouldSkip

func (m Matched[T]) IsShouldSkip() bool

IsShouldSkip returns true if the rule should be skipped.

Returns:

  • bool: True if the rule should be skipped, false otherwise.

func (Matched[T]) IsValidMatch

func (m Matched[T]) IsValidMatch() bool

IsValidMatch returns true if the matched symbol is not nil.

Returns:

  • bool: True if the matched symbol is not nil, false otherwise.

type Matcher

type Matcher[T RuleTyper] struct {
	// contains filtered or unexported fields
}

Matcher is the matcher of the grammar.

func (*Matcher[T]) AddToMatch

func (m *Matcher[T]) AddToMatch(symbol T, word string) error

AddToMatch adds a rule to match.

Parameters:

  • symbol: The symbol to match.
  • word: The word to match.

Returns:

  • error: An error if the rule to match is invalid.

func (*Matcher[T]) AddToSkipRule

func (m *Matcher[T]) AddToSkipRule(words ...string) error

AddToSkipRule adds a rule to skip.

Parameters:

  • words: The words to skip.

Returns:

  • error: An error if the rule to skip is invalid.

func (Matcher[T]) GetMatches

func (m Matcher[T]) GetMatches() []Matched[T]

GetMatches returns the matches of the matcher.

Returns:

  • []Matched[T]: The matches of the matcher. Nil if no matches were found.

func (Matcher[T]) GetRuleNames

func (m Matcher[T]) GetRuleNames() []string

GetRuleNames returns the names of the rules of the matcher.

Returns:

  • []string: The names of the rules of the matcher.

func (Matcher[T]) GetWords

func (m Matcher[T]) GetWords() []string

GetWords returns the words of the matcher.

Returns:

  • []string: The words of the matcher.

func (Matcher[T]) HasSkipped

func (m Matcher[T]) HasSkipped() bool

HasSkipped checks whether the matcher has skipped any characters.

Returns:

  • bool: True if the matcher has skipped any characters, false otherwise.

func (Matcher[T]) IsEmpty

func (m Matcher[T]) IsEmpty() bool

IsEmpty checks whether the matcher has at least one rule.

Returns:

  • bool: True if matcher is empty, false otherwise.

func (*Matcher[T]) Match

func (m *Matcher[T]) Match(scanner io.RuneScanner) (bool, error)

Match matches the next characters of the matcher.

Parameters:

  • scanner: The scanner to match.

Returns:

  • bool: True if the error is not critical, false otherwise.
  • error: An error if the next characters do not match.

A non-critical error is an error that occurs when the matcher cannot match a word due to it not being in the dictionary. Because of that, they can be ignored.

However, critical errors are errors that are external to the dictionary and prevent the matching to continue.

type RuleTyper

type RuleTyper interface {
	~int

	// String returns the name of the rule type.
	//
	// Returns:
	//   - string: The name of the rule type.
	String() string
}

RuleTyper is a rule type.

Jump to

Keyboard shortcuts

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