Documentation ¶
Index ¶
- Variables
- type ErrNoMatch
- type LavenshteinTable
- type MatchRule
- type Matched
- type Matcher
- func (m *Matcher[T]) AddToMatch(symbol T, word string) error
- func (m *Matcher[T]) AddToSkipRule(words ...string) error
- func (m Matcher[T]) GetMatches() []Matched[T]
- func (m Matcher[T]) GetRuleNames() []string
- func (m Matcher[T]) GetWords() []string
- func (m Matcher[T]) HasSkipped() bool
- func (m Matcher[T]) IsEmpty() bool
- func (m *Matcher[T]) Match(scanner io.RuneScanner) (bool, error)
- type RuleTyper
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoClosestWordFound is an error when no closest word is found. ErrNoClosestWordFound error )
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.
type Matched ¶
type Matched[T RuleTyper] struct { // contains filtered or unexported fields }
Matched is the matched result.
func (Matched[T]) GetChars ¶
GetChars returns the matched characters.
Returns:
- []rune: The matched characters.
func (Matched[T]) GetMatch ¶
GetMatch returns the matched symbol and the matched characters.
Returns:
- T: The matched symbol.
- string: The matched characters.
func (Matched[T]) IsShouldSkip ¶
IsShouldSkip returns true if the rule should be skipped.
Returns:
- bool: True if the rule should be skipped, false otherwise.
func (Matched[T]) IsValidMatch ¶
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 ¶
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 ¶
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 ¶
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 ¶
GetRuleNames returns the names of the rules of the matcher.
Returns:
- []string: The names of the rules of the matcher.
func (Matcher[T]) GetWords ¶
GetWords returns the words of the matcher.
Returns:
- []string: The words of the matcher.
func (Matcher[T]) HasSkipped ¶
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 ¶
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.