utils

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	BigIntZero      = big.NewInt(0)
	BigIntOne       = big.NewInt(1)
	BigIntTwo       = big.NewInt(2)
	BigIntMaxInt64  = big.NewInt(math.MaxInt64)
	BigIntMaxUint64 = new(big.Int).Add(new(big.Int).Mul(BigIntMaxInt64, BigIntTwo), BigIntOne)
)

Functions

func ApplyVariableDefinition

func ApplyVariableDefinition(gen StatementGenerator, definitions map[string]StatementGenerator) error

ApplyVariableDefinition applies the given map of variable definitions to the statement generator. This modifies the statement generator, rather than returning a copy.

func GenerateRandomInts

func GenerateRandomInts(count int64, max *big.Int) (randInts []*big.Int, err error)

GenerateRandomInts generates a slice of random integers, with each integer ranging from [0, max). The returned slice will be sorted from smallest to largest. If count <= 0 or max <= 0, then they will be set to 1. If count >= max, then the returned slice will contain all incrementing integers [0, max).

func GetPercentages

func GetPercentages(numbers []*big.Int, max *big.Int) []float64

GetPercentages converts the slice of numbers to percentages. The max defines the number that would equal 100%. All floats will be between [0.0, 100.0], unless the number is not between [0, max].

func PrintSectionMarker

func PrintSectionMarker(centeredText string, fillerCharacter rune, totalLength int)

PrintSectionMarker is a convenience function that prints the result of SectionMarker with a newline appended.

func SectionMarker

func SectionMarker(centeredText string, fillerCharacter rune, totalLength int) string

SectionMarker returns a marker that may be used to denote sections.

For example, SectionMarker("abc", '-', 21) would return:

-------- abc --------

func UnsetVariables

func UnsetVariables(gen StatementGenerator) ([]string, error)

UnsetVariables returns the name of all variables that do not have a definition. Sorted in ascending order.

Types

type CollectionGen

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

CollectionGen is a generator that contains multiple child generators, and will print all of its children.

func Collection

func Collection(children ...StatementGenerator) *CollectionGen

Collection creates a new StatementGenerator representing a CollectionGen.

func (*CollectionGen) AddChildren

func (c *CollectionGen) AddChildren(children ...StatementGenerator) error

AddChildren implements the interface StatementGenerator.

func (*CollectionGen) Consume

func (c *CollectionGen) Consume() bool

Consume implements the interface StatementGenerator.

func (*CollectionGen) Copy

Copy implements the interface StatementGenerator.

func (*CollectionGen) Permutations

func (c *CollectionGen) Permutations() *big.Int

Permutations implements the interface StatementGenerator.

func (*CollectionGen) PermutationsUint64

func (c *CollectionGen) PermutationsUint64() (uint64, bool)

PermutationsUint64 implements the interface StatementGenerator.

func (*CollectionGen) Reset

func (c *CollectionGen) Reset()

Reset implements the interface StatementGenerator.

func (*CollectionGen) SetConsumeIterations

func (c *CollectionGen) SetConsumeIterations(count *big.Int)

SetConsumeIterations implements the interface StatementGenerator.

func (*CollectionGen) SetConsumeIterationsFast

func (c *CollectionGen) SetConsumeIterationsFast(count uint64)

SetConsumeIterationsFast implements the interface StatementGenerator.

func (*CollectionGen) SourceString

func (c *CollectionGen) SourceString() string

SourceString implements the interface StatementGenerator.

func (*CollectionGen) String

func (c *CollectionGen) String() string

String implements the interface StatementGenerator.

type OptionalGen

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

OptionalGen is a generator that will toggle between displaying its children and not displaying its children.

func Optional

func Optional(children ...StatementGenerator) *OptionalGen

Optional creates a new StatementGenerator representing an OptionalGen.

func (*OptionalGen) AddChildren

func (o *OptionalGen) AddChildren(children ...StatementGenerator) error

AddChildren implements the interface StatementGenerator.

func (*OptionalGen) Consume

func (o *OptionalGen) Consume() bool

Consume implements the interface StatementGenerator.

func (*OptionalGen) Copy

func (o *OptionalGen) Copy() StatementGenerator

Copy implements the interface StatementGenerator.

func (*OptionalGen) Permutations

func (o *OptionalGen) Permutations() *big.Int

Permutations implements the interface StatementGenerator.

func (*OptionalGen) PermutationsUint64

func (o *OptionalGen) PermutationsUint64() (uint64, bool)

PermutationsUint64 implements the interface StatementGenerator.

func (*OptionalGen) Reset

func (o *OptionalGen) Reset()

Reset implements the interface StatementGenerator.

func (*OptionalGen) SetConsumeIterations

func (o *OptionalGen) SetConsumeIterations(count *big.Int)

SetConsumeIterations implements the interface StatementGenerator.

func (*OptionalGen) SetConsumeIterationsFast

func (o *OptionalGen) SetConsumeIterationsFast(count uint64)

SetConsumeIterationsFast implements the interface StatementGenerator.

func (*OptionalGen) SourceString

func (o *OptionalGen) SourceString() string

SourceString implements the interface StatementGenerator.

func (*OptionalGen) String

func (o *OptionalGen) String() string

String implements the interface StatementGenerator.

type OrGen

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

OrGen is a generator that contains multiple child generators, and will print only one at a time. Consuming will cycle to the next child.

func Or

func Or(children ...StatementGenerator) *OrGen

Or creates a new StatementGenerator representing an OrGen.

func (*OrGen) AddChildren

func (o *OrGen) AddChildren(children ...StatementGenerator) error

AddChildren implements the interface StatementGenerator.

func (*OrGen) Consume

func (o *OrGen) Consume() bool

Consume implements the interface StatementGenerator.

func (*OrGen) Copy

func (o *OrGen) Copy() StatementGenerator

Copy implements the interface StatementGenerator.

func (*OrGen) Permutations

func (o *OrGen) Permutations() *big.Int

Permutations implements the interface StatementGenerator.

func (*OrGen) PermutationsUint64

func (o *OrGen) PermutationsUint64() (uint64, bool)

PermutationsUint64 implements the interface StatementGenerator.

func (*OrGen) Reset

func (o *OrGen) Reset()

Reset implements the interface StatementGenerator.

func (*OrGen) SetConsumeIterations

func (o *OrGen) SetConsumeIterations(count *big.Int)

SetConsumeIterations implements the interface StatementGenerator.

func (*OrGen) SetConsumeIterationsFast

func (o *OrGen) SetConsumeIterationsFast(count uint64)

SetConsumeIterationsFast implements the interface StatementGenerator.

func (*OrGen) SourceString

func (o *OrGen) SourceString() string

SourceString implements the interface StatementGenerator.

func (*OrGen) String

func (o *OrGen) String() string

String implements the interface StatementGenerator.

type RootFolderLocation

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

RootFolderLocation is the location of this project's root folder.

func GetRootFolder

func GetRootFolder() (RootFolderLocation, error)

GetRootFolder returns the location of this project's root folder. This is useful to locate relative directories, which will often be read from and written to for generation. It is assumed that this will always be called from within an IDE.

func (RootFolderLocation) GetAbsolutePath

func (root RootFolderLocation) GetAbsolutePath(relativePath string) string

GetAbsolutePath returns the absolute path of the given path, which should be relative to the project's root folder.

func (RootFolderLocation) MoveRoot

func (root RootFolderLocation) MoveRoot(relativePath string) RootFolderLocation

MoveRoot returns a new RootFolderLocation that defines the root at the new path. The parameter should be relative to the current root.

func (RootFolderLocation) ReadDir

func (root RootFolderLocation) ReadDir(relativePath string) ([]os.DirEntry, error)

ReadDir is equivalent to os.ReadDir, except that it uses the root path and the given relative path.

func (RootFolderLocation) ReadFile

func (root RootFolderLocation) ReadFile(relativePath string) ([]byte, error)

ReadFile is equivalent to os.ReadFile, except that it uses the root path and the given relative path.

func (RootFolderLocation) ReadFileFromDirectory

func (root RootFolderLocation) ReadFileFromDirectory(relativeDirectoryPath string, fileName string) ([]byte, error)

ReadFileFromDirectory is similar to ReadFile, except that it takes in a relative path for the directory containing the file, along with the file name to be read. This is purely for convenience, as the same behavior can be accomplished using ReadFile.

func (RootFolderLocation) WriteFile

func (root RootFolderLocation) WriteFile(relativePath string, data []byte, perm os.FileMode) error

WriteFile is equivalent to os.WriteFile, except that it uses the root path and the given relative path.

func (RootFolderLocation) WriteFileToDirectory

func (root RootFolderLocation) WriteFileToDirectory(relativeDirectoryPath string, fileName string, data []byte, perm os.FileMode) error

WriteFileToDirectory is similar to WriteFile, except that it takes in a relative path for the directory containing the file, along with the file name to be written. This is purely for convenience, as the same behavior can be accomplished using WriteFile.

type StatementGenerator

type StatementGenerator interface {
	// AddChildren adds the given children to the generator. Not all generators accept all children, so this may error.
	AddChildren(child ...StatementGenerator) error
	// Consume returns true when the generator is able to produce a unique mutation, and false if it is not. Only one
	// generator should mutate per call, meaning a parent generator should only mutate when its children return false.
	// If the top-level generator returns false, then all permutations have been created.
	Consume() bool
	// SetConsumeIterations is equivalent to calling Copy then Consume the given number of times, without allocating a
	// new StatementGenerator. This allows you to generate a specific statement efficiently, rather than calling Consume
	// the given number of times. If the count is <= 0, then the statement will be in its original state (the same state
	// as a StatementGenerator copy).
	SetConsumeIterations(count *big.Int)
	// SetConsumeIterationsFast is the same as SetConsumeIterations, except far more efficient due to using uint64,
	// however it only works for iteration counts <= MAX_SIZE(uint64).
	SetConsumeIterationsFast(count uint64)
	// String returns a string based on the current permutation.
	String() string
	// Copy returns a copy of the given generator (along with all of its children) in its original setting. This means
	// that the copy is in the same state that the target would be in if it had never called Consume.
	Copy() StatementGenerator
	// Reset sets the StatementGenerator back to its original state, which would be as though Consume was never called.
	// This is equivalent to calling SetConsumeIterations(0), albeit slightly more efficient.
	Reset()
	// SourceString returns a string that may be used to recreate the StatementGenerator in a Go source file.
	SourceString() string
	// Permutations returns the number of unique permutations that the generator can return.
	Permutations() *big.Int
	// PermutationsUint64 returns the number of unique permutations that the generator can return. Returns true if the
	// number fits within an uint64, false if it's larger than an uint64.
	PermutationsUint64() (uint64, bool)
}

StatementGenerator represents a statement, and is able to produce all valid variations of the statement.

func ParseTokens

func ParseTokens(tokens []Token, includeRepetition bool) (StatementGenerator, error)

ParseTokens parses the given tokens into a StatementGenerator.

type StatementGeneratorStack

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

StatementGeneratorStack handles the creation of a StatementGenerator by contextually applying operations based on the current state of the internal stack whenever calls are made.

func NewStatementGeneratorStack

func NewStatementGeneratorStack() *StatementGeneratorStack

NewStatementGeneratorStack returns a new *StatementGeneratorStack.

func (*StatementGeneratorStack) AddText

func (sgs *StatementGeneratorStack) AddText(text string)

AddText creates a new TextGen at the current depth.

func (*StatementGeneratorStack) AddVariable

func (sgs *StatementGeneratorStack) AddVariable(name string)

AddVariable creates a new VariableGen at the current depth.

func (*StatementGeneratorStack) ExitOptionalScope

func (sgs *StatementGeneratorStack) ExitOptionalScope() error

ExitOptionalScope decrements the depth, writing all elements from the current depth (and all sub depths) to the preceding depth. This will fail if the root of the current depth is not an OptionalGen.

func (*StatementGeneratorStack) ExitParenScope

func (sgs *StatementGeneratorStack) ExitParenScope() error

ExitParenScope decrements the depth, writing all elements from the current depth (and all sub depths) to the preceding depth, while appending an ending Text(")").

func (*StatementGeneratorStack) ExitScope

func (sgs *StatementGeneratorStack) ExitScope() error

ExitScope decrements the depth, writing all elements from the current depth (and all sub depths) to the preceding depth.

func (*StatementGeneratorStack) Finish

Finish returns a StatementGenerator containing all of the generators that have been added. This returns an error if the stack is not at the root depth, as that indicates a missing scope exit, or too many scope entries.

func (*StatementGeneratorStack) NewOptionalScope

func (sgs *StatementGeneratorStack) NewOptionalScope()

NewOptionalScope increases the depth, creating an OptionalGen at the depth root, and adding elements to the sub depth.

func (*StatementGeneratorStack) NewParenScope

func (sgs *StatementGeneratorStack) NewParenScope()

NewParenScope increases the depth, creating a CollectionGen at the depth root, adding Text("(") to the CollectionGen, and adding any new elements to the sub depth.

func (*StatementGeneratorStack) NewScope

func (sgs *StatementGeneratorStack) NewScope()

NewScope increases the depth.

func (*StatementGeneratorStack) OptionalRepeat

func (sgs *StatementGeneratorStack) OptionalRepeat(prefix string) error

OptionalRepeat will add the last StatementGenerator at the current depth to an OptionalGen. If a prefix is given, then it will be added as a TextGen before the repeated generator.

func (*StatementGeneratorStack) Or

func (sgs *StatementGeneratorStack) Or() error

Or will take all items from the current depth and add them to a parent OrGen. Either the previous depth is an OrGen, or the stack will increment the sub depth to insert an OrGen.

func (*StatementGeneratorStack) Repeat

func (sgs *StatementGeneratorStack) Repeat() error

Repeat will add the last StatementGenerator at the current depth to an OptionalGen.

type StatementGeneratorStackElement

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

StatementGeneratorStackElement represents an element within the StatementGeneratorStack.

func NewStatementGeneratorStackElement

func NewStatementGeneratorStackElement(depth int, gens ...StatementGenerator) *StatementGeneratorStackElement

NewStatementGeneratorStackElement returns a new *StatementGeneratorStackElement.

func (*StatementGeneratorStackElement) Append

Append adds the given child to this element's collection.

func (*StatementGeneratorStackElement) LastGenerator

func (sgse *StatementGeneratorStackElement) LastGenerator() StatementGenerator

LastGenerator returns the last StatementGenerator contained within its slice.

type TextGen

type TextGen string

TextGen is a generator that returns a simple string.

func Text

func Text(str string) *TextGen

Text creates a new StatementGenerator representing a simple string.

func (*TextGen) AddChildren

func (t *TextGen) AddChildren(children ...StatementGenerator) error

AddChildren implements the interface StatementGenerator.

func (*TextGen) Consume

func (t *TextGen) Consume() bool

Consume implements the interface StatementGenerator.

func (*TextGen) Copy

func (t *TextGen) Copy() StatementGenerator

Copy implements the interface StatementGenerator.

func (*TextGen) Permutations

func (t *TextGen) Permutations() *big.Int

Permutations implements the interface StatementGenerator.

func (*TextGen) PermutationsUint64

func (t *TextGen) PermutationsUint64() (uint64, bool)

PermutationsUint64 implements the interface StatementGenerator.

func (*TextGen) Reset

func (t *TextGen) Reset()

Reset implements the interface StatementGenerator.

func (*TextGen) SetConsumeIterations

func (t *TextGen) SetConsumeIterations(count *big.Int)

SetConsumeIterations implements the interface StatementGenerator.

func (*TextGen) SetConsumeIterationsFast

func (t *TextGen) SetConsumeIterationsFast(count uint64)

SetConsumeIterationsFast implements the interface StatementGenerator.

func (*TextGen) SourceString

func (t *TextGen) SourceString() string

SourceString implements the interface StatementGenerator.

func (*TextGen) String

func (t *TextGen) String() string

String implements the interface StatementGenerator.

type Token

type Token struct {
	Type    TokenType
	Literal string
}

Token represents a token in the synopsis. Unlike with traditional lexers, whitespace is tokenized, with multiple token types for differing lengths, as the whitespace has some significance to the definition.

func (Token) CreatesNewScope

func (t Token) CreatesNewScope() bool

CreatesNewScope returns whether the token creates a new scope.

func (Token) ExitsScope

func (t Token) ExitsScope() bool

ExitsScope returns whether the token exits the current scope.

func (Token) IsNewStatement

func (t Token) IsNewStatement() bool

IsNewStatement returns whether the token represents one a long space, which will end a statement

func (Token) IsSpace

func (t Token) IsSpace() bool

IsSpace returns whether the token represents one of the space types.

func (Token) IsStandardSpace

func (t Token) IsStandardSpace() bool

IsStandardSpace returns whether the token represents a short or medium space, which will not end a statement.

func (Token) String

func (t Token) String() string

String returns a string representation of the token.

type TokenReader

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

TokenReader may be used to easily read through a collection of tokens generated from a synopsis. Of note, a TokenReader will skip all short and medium spaces, as clients of a reader will only care about statement boundaries.

func NewTokenReader

func NewTokenReader(tokens []Token) *TokenReader

NewTokenReader returns a new *TokenReader created from the given tokens.

func (*TokenReader) Advance

func (reader *TokenReader) Advance()

Advance is equivalent to calling AdvanceBy(1).

func (*TokenReader) AdvanceBy

func (reader *TokenReader) AdvanceBy(n int)

AdvanceBy advances the reader by the given amount. If the amount is greater than the number of remaining tokens, then it advances to the end. Cannot advance backwards.

func (*TokenReader) Next

func (reader *TokenReader) Next() (Token, bool)

Next returns the next token while advancing the reader. Returns false if there are no more tokens.

func (*TokenReader) Peek

func (reader *TokenReader) Peek() (Token, bool)

Peek returns the next token. Does not advance the reader. Returns false if there are no more tokens.

func (*TokenReader) PeekBy

func (reader *TokenReader) PeekBy(n int) (Token, bool)

PeekBy returns the next token that is n positions from the current token. Does not advance the reader. Returns false if we are peeking beyond the slice.

type TokenType

type TokenType uint8

TokenType is the type of the token.

const (
	TokenType_Text TokenType = iota
	TokenType_Variable
	TokenType_VariableDefinition
	TokenType_Or
	TokenType_Repeat
	TokenType_OptionalRepeat
	TokenType_ShortSpace
	TokenType_MediumSpace
	TokenType_LongSpace
	TokenType_ParenOpen
	TokenType_ParenClose
	TokenType_OptionalOpen
	TokenType_OptionalClose
	TokenType_OneOfOpen
	TokenType_OneOfClose
	TokenType_EOF
)

type VariableGen

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

VariableGen represents a variable in the synopsis. Its values are user-configurable if they cannot be deduced from the synopsis.

func Variable

func Variable(name string, child StatementGenerator) *VariableGen

Variable creates a new StatementGenerator representing a VariableGen.

func (*VariableGen) AddChildren

func (v *VariableGen) AddChildren(children ...StatementGenerator) error

AddChildren implements the interface StatementGenerator.

func (*VariableGen) Consume

func (v *VariableGen) Consume() bool

Consume implements the interface StatementGenerator.

func (*VariableGen) Copy

func (v *VariableGen) Copy() StatementGenerator

Copy implements the interface StatementGenerator.

func (*VariableGen) Permutations

func (v *VariableGen) Permutations() *big.Int

Permutations implements the interface StatementGenerator.

func (*VariableGen) PermutationsUint64

func (v *VariableGen) PermutationsUint64() (uint64, bool)

PermutationsUint64 implements the interface StatementGenerator.

func (*VariableGen) Reset

func (v *VariableGen) Reset()

Reset implements the interface StatementGenerator.

func (*VariableGen) SetConsumeIterations

func (v *VariableGen) SetConsumeIterations(count *big.Int)

SetConsumeIterations implements the interface StatementGenerator.

func (*VariableGen) SetConsumeIterationsFast

func (v *VariableGen) SetConsumeIterationsFast(count uint64)

SetConsumeIterationsFast implements the interface StatementGenerator.

func (*VariableGen) SourceString

func (v *VariableGen) SourceString() string

SourceString implements the interface StatementGenerator.

func (*VariableGen) String

func (v *VariableGen) String() string

String implements the interface StatementGenerator.

Jump to

Keyboard shortcuts

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