grammar

package
v0.0.2-alpha.3 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	JSONDictionary = iota
	CSVDictionary
)

Variables

This section is empty.

Functions

func Dictionary

func Dictionary[K prim.Hashable, V ~[]S, S Symbol](entries ...str.Entry[K, V]) str.Dictionary[K, V]

Dictionary creates a new empty dictionary of symbols

func GetSymbolValue

func GetSymbolValue[T any](symbol Symbol) (T, error)

GetSymbolValue takes a symbol and attempts to convert it to a value of type K.

Returns: - The value of type K if the conversion was successful. - An error if the conversion failed.

func HasConsecutiveSymbol

func HasConsecutiveSymbol(pattern, symbol Symbol) bool

HasConsecutiveSymbol checks if a symbol has a consecutive occurrence in a pattern.

If the symbol has a consecutive occurrence in the pattern, it returns true. If the symbol doesn't have a consecutive occurrence in the pattern, it returns false.

func HasSymbolAt

func HasSymbolAt(pattern, symbol Symbol, idx int) bool

HasSymbolAt checks if a symbol is present at a given index in a pattern.

If the symbol is present at the given index, it returns true. If the symbol isn't present at the given index, it returns false.

func HasSymbolAtAll

func HasSymbolAtAll(pattern, symbol Symbol, idx ...int) bool

HasSymbolAtAll checks if a symbol is present at all the given indices in a pattern.

If the symbol is present at any of the given indices, it returns true. If the symbol isn't present at any of the given indices, it returns false.

func HasSymbolAtAny

func HasSymbolAtAny(pattern, symbol Symbol, idx ...int) bool

HasSymbolAtAny checks if a symbol is present at any of the given indices in a pattern.

If the symbol is present at any of the given indices, it returns true. If the symbol is not present at any of the given indices, it returns false.

func SymbolEntry

func SymbolEntry[K prim.Hashable, V ~[]S, S Symbol](key K, values ...S) str.Entry[K, V]

SymbolEntry creates a new Entry for a SymbolDictionary.

The key of the Entry is of type K, and the value is of type ByteSymbol. The function takes two parameters, the key and the value, and returns a new Entry.

The Entry is created using the symbolEntry struct, which contains the key and value.

Types

type ByteSymbol

type ByteSymbol []byte

ByteSymbol is a sequence of bytes that represents a pattern.

It is used to identify specific patterns in general expressions.

func (ByteSymbol) After

func (s ByteSymbol) After(i int) Symbol

After returns a new ByteSymbol which is a subset of the receiver, starting at index `i`.

This is a convenience method for building symbols.

func (ByteSymbol) Append

func (s ByteSymbol) Append(other Symbol) Symbol

Append returns a new ByteSymbol which is the concatenation of the receiver and the other ByteSymbol.

This is a convenience method for building symbols.

func (ByteSymbol) Before

func (s ByteSymbol) Before(i int) Symbol

Before returns a new ByteSymbol which is a subset of the receiver, up to index `i`.

This is a convenience method for building symbols.

func (ByteSymbol) Between

func (s ByteSymbol) Between(i, j int) Symbol

Between returns a substring of the symbol, starting at index `i` and ending at `j`. If `j` is negative, it is treated as the index from the end of the string.

func (ByteSymbol) Bytes

func (s ByteSymbol) Bytes() []byte

Bytes return the symbol as a slice of bytes.

This is a convenience method for working with the symbol.

func (ByteSymbol) CharSet

func (s ByteSymbol) CharSet() map[rune]struct{}

CharSet returns a set of all characters in the symbol.

This is a convenience method for working with the symbol.

func (ByteSymbol) Contains

func (s ByteSymbol) Contains(other Symbol) bool

Contains returns true if the receiver contains the other symbol.

This is a convenience method for working with the symbol.

func (ByteSymbol) CutAll

func (s ByteSymbol) CutAll(other Symbol) Symbol

CutAll removes all occurrences of the given symbol from the receiver.

Returns the modified symbol.

func (ByteSymbol) CutFirst

func (s ByteSymbol) CutFirst(symbol Symbol) Symbol

CutFirst removes the first occurrence of the given symbol from the receiver.

Returns the modified symbol.

If the given symbol is not found, the receiver is returned unchanged.

func (ByteSymbol) CutLast

func (s ByteSymbol) CutLast(symbol Symbol) Symbol

CutLast removes the last occurrence of the given symbol from the receiver.

symbol - the symbol to be removed. Returns the modified symbol. If the given symbol isn't found, the receiver is returned unchanged.

func (ByteSymbol) Empty

func (s ByteSymbol) Empty() Symbol

func (ByteSymbol) EndsWith

func (s ByteSymbol) EndsWith(other Symbol) bool

EndsWith returns true if the receiver ends with the given symbol.

This is a convenience method for working with the symbol.

func (ByteSymbol) Equal

func (s ByteSymbol) Equal(other Symbol) bool

Equal returns true if the symbol is equal to the other symbol.

This is a convenience method for working with the symbol.

func (ByteSymbol) ExtractSymbol

func (s ByteSymbol) ExtractSymbol(split Symbol) (symbol, after Symbol)

ExtractSymbol splits the given string into two parts, the first part being the given symbol and the second part being the rest of the string.

It returns the symbol and the rest of the string. If the symbol is longer than the string, it returns an empty string for the symbol and the full string as the rest.

The split is done starting from the beginning of the string, and the symbol is matched against the start of the string.

If the symbol is longer than the string, it can't match, so the function returns an empty string for the symbol and the full string as the rest.

func (ByteSymbol) Format

func (s ByteSymbol) Format(format Symbol, placeholder Symbol) Symbol

Format returns a new symbol by replacing all occurrences of the placeholder symbol with the receiver in the given format symbol.

The format symbol is the symbol to be formatted. The placeholder symbol is the symbol to be replaced in the format symbol. It returns the formatted symbol.

func (ByteSymbol) IndexOf

func (s ByteSymbol) IndexOf(other Symbol) (int, bool)

IndexOf returns the index of the first occurrence of the given symbol in the receiver. It returns the index and a boolean indicating whether the symbol was found. If the symbol is not found, it returns -1 and false.

func (ByteSymbol) IsEmpty

func (s ByteSymbol) IsEmpty() bool

IsEmpty returns true if the symbol is empty.

This is a convenience method for checking if the symbol is empty.

func (ByteSymbol) IsValid

func (s ByteSymbol) IsValid(rgx string) bool

IsValid checks if the symbol matches the given regular expression. It returns true if the symbol is valid according to the regular expression, otherwise false.

The regular expression is compiled and the symbol is matched against it.

func (ByteSymbol) LastIndexOf

func (s ByteSymbol) LastIndexOf(other Symbol) (int, bool)

LastIndexOf returns the index of the last occurrence of the given symbol in the current symbol. If the symbol is not found, it returns -1 and false.

This is a convenience method for working with the symbol.

func (ByteSymbol) Len

func (s ByteSymbol) Len() int

Len returns the length of the symbol in bytes.

This is a convenience method for working with the symbol.

func (ByteSymbol) Map

Map applies the given function to each byte of the symbol and returns a new symbol.

The function takes a byte and returns a new byte. The function is applied to each byte of the symbol. The resulting bytes are concatenated to form the new symbol.

func (ByteSymbol) Remove

func (s ByteSymbol) Remove(i, j int) Symbol

func (ByteSymbol) ReplaceAll

func (s ByteSymbol) ReplaceAll(target Symbol, replacement Symbol) (Symbol, bool)

ReplaceAll replaces all occurrences of a given target symbol with a given replacement symbol within this symbol.

It returns a new symbol, which is the result of replacing all occurrences of the target symbol with the replacement symbol. If the target symbol is not found in this symbol, it returns the original symbol.

func (ByteSymbol) ReplaceEach

func (s ByteSymbol) ReplaceEach(target Symbol, replacements ...Symbol) (Symbol, bool)

ReplaceEach replaces all occurrences of a given target symbol with a given replacement symbol within this symbol. This is similar to ReplaceAll, but it takes a slice of replacement symbols, and each replacement symbol is used in order. If there are more occurrences of the target symbol than there are replacement symbols, the remaining occurrences aren't replaced.

It returns a new symbol, which is the result of replacing all occurrences of the target symbol with the replacement symbols. If the target symbol is not found in this symbol, it returns the original symbol. If there are not enough replacement symbols, it returns the original symbol.

func (ByteSymbol) ReplaceFirst

func (s ByteSymbol) ReplaceFirst(target Symbol, replacement Symbol) (Symbol, bool)

ReplaceFirst replaces the first occurrence of the target symbol with the replacement symbol. If the target symbol is not found in this symbol, it returns the original symbol and false. If the target symbol is found, it returns the new symbol and true.

func (ByteSymbol) ReplaceLast

func (s ByteSymbol) ReplaceLast(target Symbol, replacement Symbol) (Symbol, bool)

ReplaceLast replaces the last occurrence of the target symbol with the replacement symbol. If the target symbol is not found in this symbol, it returns the original symbol and false. If the target symbol is found, it returns the new symbol and true.

func (ByteSymbol) ReplaceMany

func (s ByteSymbol) ReplaceMany(replacement Symbol, targets ...Symbol) (Symbol, bool)

ReplaceMany replaces all occurrences of the target symbols in the given symbol with the given replacement symbol.

It returns the new symbol and a boolean indicating if any replacements were made.

If no replacements were made, the original symbol is returned.

func (ByteSymbol) StartsWith

func (s ByteSymbol) StartsWith(other Symbol) bool

StartsWith returns true if the given symbol starts with the given other symbol.

This is a convenience method for working with the symbol.

func (ByteSymbol) String

func (s ByteSymbol) String() string

String returns the symbol as a string.

This is a convenience method for working with the symbol.

func (ByteSymbol) Validate

func (s ByteSymbol) Validate(rgx string) error

Validate checks if the symbol matches the given regular expression. If it doesn't match, it returns an error.

type DictionaryFileType

type DictionaryFileType int

type PatternTrieDictionary

type PatternTrieDictionary SymbolDictionary[string, []Symbol, Symbol]

type Symbol

type Symbol interface {
	fmt.Stringer
	// After returns a new symbol that is the sub symbol of the current symbol
	// from the given index to the end of the symbol.
	After(i int) Symbol
	// Append returns a new symbol that is the result of appending the given
	// other symbol to the end of the current symbol.
	Append(other Symbol) Symbol
	// Before returns a new symbol that is the sub symbol of the current symbol
	// from the beginning of the symbol to the given index.
	Before(i int) Symbol
	// Between returns a new symbol that is the sub symbol of the current symbol
	// from the given i index to the given j index.
	Between(i, j int) Symbol
	// Bytes returns the byte representation of the symbol.
	Bytes() []byte
	// CharSet returns a set of characters used in the symbol.
	// It handles string symbols, numbers, and any other type.
	// For numbers, it converts them to their string representation and treats digits as characters.
	// For other types, it ignores them.
	// It also handles composed sequences of characters as a single character.
	// For example, if the symbol is "foo-bar", it will treat it as a single character.
	CharSet() map[rune]struct{}
	// Contains returns true if the given other symbol is a sub symbol of the current symbol.
	// It returns false otherwise.
	Contains(other Symbol) bool
	// CutAll removes all occurrences of the given other symbol from the current symbol.
	// It returns a new symbol with all occurrences of the other symbol removed.
	//
	// For example, if the current symbol is "test-test2" and the given other symbol
	// is "test", it will return a symbol with only "2".
	CutAll(other Symbol) Symbol
	// CutFirst removes the first occurrence of the given other symbol from the current symbol.
	// It returns a new symbol with the first occurrence of the other symbol removed.
	//
	// For example, if the current symbol is "test-test2" and the given other symbol
	CutFirst(symbol Symbol) Symbol
	// CutLast removes the last occurrence of the given other symbol from the current symbol.
	// It returns a new symbol with the last occurrence of the other symbol removed.
	//
	// For example, if the current symbol is "test-test2" and the given other symbol
	// is "test", it will return a symbol with only "test2".
	CutLast(other Symbol) Symbol
	// EndsWith returns true if the given other symbol is a suffix of the current symbol.
	// It returns false otherwise.
	//
	// For example, if the current symbol is "test" and the given other symbol is "t",
	EndsWith(other Symbol) bool
	// Equal returns true if the given other symbol is equal to the current symbol.
	// It returns false otherwise.
	Equal(other Symbol) bool
	// Empty returns a new empty symbol.
	Empty() Symbol
	// ExtractSymbol splits the given string into two parts, the first part being the
	// given symbol and the second part being the rest of the string.
	//
	// It returns the symbol and the rest of the string. If the symbol is longer
	// than the string, it returns an empty string for the symbol and the full
	// string as the rest.
	//
	// If the split symbol is not found, it returns an empty symbol and the entire current symbol.
	ExtractSymbol(split Symbol) (symbol, after Symbol)
	// Format formats the given placeholder symbol with the given format symbol.
	// It replaces any occurrence of the placeholder symbol in the format symbol with the given placeholder symbol.
	// For example, if the format symbol is "hello %s" and the placeholder symbol is "world", it will return a symbol with the value "hello world".
	Format(format Symbol, placeholder Symbol) Symbol
	// IsEmpty returns true if the symbol is empty, false otherwise.
	IsEmpty() bool
	// IndexOf returns the index of the first occurrence of the given other symbol in the current symbol.
	// It returns -1 if the symbol is not found.
	// It takes a Symbol as an argument and returns an int and a boolean.
	IndexOf(other Symbol) (int, bool)
	// LastIndexOf returns the index of the last occurrence of the given other symbol in the current symbol.
	// It returns -1 if the symbol is not found.
	// It takes a Symbol as an argument and returns an int and a boolean.
	LastIndexOf(other Symbol) (int, bool)
	// Len returns the length of the symbol.
	Len() int
	// Map applies a given BiFunction to each character in the symbol and returns a new symbol.
	// It takes a BiFunction that takes two parameters: the index of the character in the symbol and the character itself.
	// It returns a new symbol with the transformed characters.
	// For example, if the symbol is "test-test2" and the BiFunction is a lambda that takes the index and the character and returns the character incremented by 1,
	// it will return a symbol with the value "uvft-uvfu3".
	Map(fn functions.BiFunction[int, byte, byte]) Symbol
	// Remove returns a new symbol that is the result of removing the sub symbol
	// from the given i index to the given j index from the current symbol.
	Remove(i, j int) Symbol
	// ReplaceAll replaces all occurrences of the given target symbol in the current symbol
	// with the given replacement symbol and returns the new symbol.
	// It returns a boolean indicating whether the replacement was performed or not.
	ReplaceAll(target Symbol, replacement Symbol) (Symbol, bool)
	// ReplaceEach replaces all occurrences of a given target symbol with a given replacement symbol
	// within this symbol. This is similar to ReplaceAll, but it takes a slice of replacement symbols,
	// and each replacement symbol is used in order. If there are more occurrences of the target symbol
	ReplaceEach(target Symbol, replacements ...Symbol) (Symbol, bool)
	// ReplaceFirst replaces the first occurrence of the given target symbol in the current symbol
	// with the given replacement symbol and returns the new symbol.
	// It returns a boolean indicating whether the replacement was performed or not.
	ReplaceFirst(target Symbol, replacement Symbol) (Symbol, bool)
	// ReplaceLast replaces the last occurrence of the given target symbol in the current symbol
	// with the given replacement symbol and returns the new symbol.
	// It returns a boolean indicating whether the replacement was performed or not.
	ReplaceLast(target Symbol, replacement Symbol) (Symbol, bool)
	// ReplaceMany replaces all occurrences of a given target symbol with a given replacement symbol
	// within this symbol. This is similar to ReplaceAll, but it takes a slice of target symbols,
	// and each target symbol is used in order. If there are more occurrences of the target symbols
	// than the number of replacement symbols, the replacement symbols are cycled over.
	// It returns a boolean indicating whether the replacement was performed or not.
	ReplaceMany(replacement Symbol, targets ...Symbol) (Symbol, bool)
	// StartsWith returns true if the current symbol starts with the given other symbol.
	// It returns false otherwise.
	StartsWith(other Symbol) bool
}

func NewSymbol

func NewSymbol(s any) (Symbol, error)

NewSymbol takes an interface{} and attempts to convert it to a ByteSymbol.

Symbols are simply sequences of bytes used to identify patterns.

If the input is a string, it is converted to a []byte. If the input is already a []byte, it is returned as is. If the input is a []byte, it is returned as is. If the input is a ByteSymbol, it is returned as is. Otherwise, the function tries to marshal the input to JSON and return the resulting []byte. If all else fails, an error is returned.

type SymbolDictionary

type SymbolDictionary[K prim.Hashable, V ~[]S, S Symbol] map[K]V

func (*SymbolDictionary[K, V, S]) CharSet

func (d *SymbolDictionary[K, V, S]) CharSet() map[rune]struct{}

CharSet returns a set of all characters used in the values of the dictionary.

The returned map is a set of runes, where each rune is a character used in at least one of the values of the dictionary. The set is represented as a map of runes to empty structs, as this is a common idiom in Go for representing a set.

This function is useful when you want to know all the characters used in the values of the dictionary. For example, you can use it to pre-allocate a buffer for parsing a string, or to check if a string contains any characters that are not in the dictionary.

func (*SymbolDictionary[K, V, S]) Entries

func (d *SymbolDictionary[K, V, S]) Entries() []str.Entry[K, V]

Entries returns a slice of Entry objects representing the key-value pairs in the dictionary.

func (*SymbolDictionary[K, V, S]) EntrySet

func (d *SymbolDictionary[K, V, S]) EntrySet() map[K]V

func (*SymbolDictionary[K, V, S]) Get

func (d *SymbolDictionary[K, V, S]) Get(key K) (V, bool)

Get returns the value associated with the given key. If the key does not exist, it returns the zero value for the ByteSymbol type and false.

func (*SymbolDictionary[K, V, S]) IsEmpty

func (d *SymbolDictionary[K, V, S]) IsEmpty() bool

IsEmpty returns true if the dictionary is empty, false otherwise.

func (*SymbolDictionary[K, V, S]) Keys

func (d *SymbolDictionary[K, V, S]) Keys() []K

Keys returns a slice of keys in the dictionary.

func (*SymbolDictionary[K, V, S]) LoadFromFile

func (d *SymbolDictionary[K, V, S]) LoadFromFile(filePath string, fileType DictionaryFileType) error

LoadFromFile reads a file and populates the SymbolDictionary with symbols

func (*SymbolDictionary[K, V, S]) Put

func (d *SymbolDictionary[K, V, S]) Put(key K, value V)

Put adds a new key-value pair to the dictionary. If the key already exists, the old value is replaced by the new value.

func (*SymbolDictionary[K, V, S]) Size

func (d *SymbolDictionary[K, V, S]) Size() int

Size returns the number of key-value pairs in the dictionary.

func (*SymbolDictionary[K, V, S]) Values

func (d *SymbolDictionary[K, V, S]) Values() []V

Values returns a slice of values in the dictionary.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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