parser

package
v0.0.0-...-1b5e7ed Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2021 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefinitionName            = "parser"
	DefinitionNameContext     = "global-context"
	DefinitionNameRootScope   = "parser-global-scope"
	DefinitionNameCommandTree = "parser-command-tree"
)
View Source
const (
	ExpressionTypeCmd     = "expression-cmd"
	ExpressionTypeCmdList = "expression-cmd-list"
	ExpressionTypeMath    = "expression-math"
	ExpressionTypeVar     = "expression-var"
	ExpressionTypeStd     = "expression-std"
)
View Source
const (
	OperatorEqual          = OperatorTypeCompare
	OperatorNotEqual       = OperatorTypeCompare + 1
	OperatorGreater        = OperatorTypeCompare + 2
	OperatorGreaterOrEqual = OperatorTypeCompare + 3
	OperatorLess           = OperatorTypeCompare + 4
	OperatorLessOrEqual    = OperatorTypeCompare + 5
	OperatorConcatenate    = OperatorTypeConcatenate
	OperatorPlus           = OperatorTypeAddition
	OperatorMinus          = OperatorTypeAddition + 1
	OperatorMultiply       = OperatorTypeMultiply
	OperatorDivide         = OperatorTypeMultiply + 1
	OperatorNot            = OperatorTypeUnary
)

Variables

View Source
var (
	Definition = di.Def{
		Name: DefinitionName,
		Build: func(ctn di.Container) (interface{}, error) {
			return newParser(ctn), nil
		},
	}
	DefinitionScope = di.Def{
		Name: DefinitionNameRootScope,
		Build: func(ctn di.Container) (interface{}, error) {
			return newRootContext(ctn)
		},
	}
	DefinitionContext = di.Def{
		Name: DefinitionNameContext,
		Build: func(ctn di.Container) (interface{}, error) {
			return context.Background(), nil
		},
	}
	DefinitionCommandTree = di.Def{
		Name: DefinitionNameCommandTree,
		Build: func(ctn di.Container) (interface{}, error) {
			return List{}, nil
		},
	}
)
View Source
var (
	ErrWrongRune    = errors.New("wrong rune")
	ErrWrongPayload = errors.New("wrong payload")
	ErrPanic        = errors.New("panic")
	ErrNotFinished  = errors.New("not finished")
)
View Source
var (
	ErrWrongType       = errors.New("wrong type")
	ErrWrongOperator   = errors.New("wrong operator")
	ErrNoMandatoryFlag = errors.New("no mandatory flag")
)
View Source
var (
	NullValue = NewStringValue("")
)

Functions

func NewMathTree

func NewMathTree() *mathTree

func NewNode

func NewNode() *commandNode

func NewVariableNode

func NewVariableNode() *variableNode

Types

type CloseResponse

type CloseResponse struct {
	UnclosedBrackets models.Rune
	Error            error
}

type Command

type Command struct {
	Type           CommandType
	Path           []string
	Name           string
	ExecFunc       ExecFunc
	SystemExecFunc SystemExecFunc
	OutFunc        OutFunc
	Flags          Flags
	Options        Options
	MandatoryFlags []string
	// contains filtered or unexported fields
}

func (*Command) Copy

func (c *Command) Copy() *Command

func (*Command) Exec

func (c *Command) Exec(ctx SystemContext, flags Flags) (Value, error)

func (*Command) Out

func (c *Command) Out(ctx SystemContext, inFlags Flags)

func (*Command) UnnamedFlag

func (c *Command) UnnamedFlag(number uint) *Flag

type CommandTree

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

func BuildCommandTree

func BuildCommandTree(items map[string]*Item) *CommandTree

func NewCommandTree

func NewCommandTree() *CommandTree

func (*CommandTree) Add

func (c *CommandTree) Add(key string, payload *Payload)

func (*CommandTree) Copy

func (c *CommandTree) Copy() *CommandTree

func (*CommandTree) GetIterator

func (c *CommandTree) GetIterator() *commandIterator

func (*CommandTree) Use

func (c *CommandTree) Use(key string)

type CommandType

type CommandType string
const (
	CommandTypeUser   CommandType = "user"
	CommandTypeSystem CommandType = "system"
)

type CompleteOption

type CompleteOption struct {
	Level  LevelType
	Option string
}

type CompleteResponse

type CompleteResponse struct {
	Options []*CompleteOption
	Merged  string
}

type Context

type Context interface {
	context.Context
	Buffer() terminal.Buffer
}

type ContextType

type ContextType int
const (
	ContextTypeNone ContextType = iota
	ContextTypeCopied
	// TODO: Rename it
	ContextTypeNew
)

type ExecFunc

type ExecFunc func(Context, FlagValues, Options) (Value, error)

type ExecResponse

type ExecResponse struct {
	UnclosedBrackets models.Rune
	Error            error
	Value            Value
}

type Expression

type Expression interface {
	Valuer
	Type() ExpressionType
	Add(ctx SystemContext, r models.Rune) *Response
	Complete(ctx SystemContext) *CompleteResponse
	Close(ctx SystemContext) *CloseResponse
}

func NewCommandExpression

func NewCommandExpression(ctx SystemContext) Expression

func NewCommandList

func NewCommandList(rootMode, isCurly bool) Expression

func NewMathExpression

func NewMathExpression() Expression

func NewStdExpression

func NewStdExpression(strictMode bool) Expression

func NewVariable

func NewVariable(functionMode bool) Expression

type ExpressionStack

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

func (*ExpressionStack) Pop

func (*ExpressionStack) Push

func (es *ExpressionStack) Push(ctx SystemContext, exp Expression)

func (*ExpressionStack) Size

func (es *ExpressionStack) Size() int

type ExpressionType

type ExpressionType string

type Flag

type Flag struct {
	Name      string
	Mandatory bool
	Number    uint
	ValueType
	// contains filtered or unexported fields
}

func (*Flag) Copy

func (f *Flag) Copy() *Flag

func (*Flag) Expression

func (f *Flag) Expression() Expression

func (*Flag) Set

func (f *Flag) Set(e Expression)

func (*Flag) Value

func (f *Flag) Value(ctx SystemContext) Value

type FlagValues

type FlagValues map[string]Value

func (FlagValues) Get

func (fv FlagValues) Get(name string) (Value, bool)

func (FlagValues) Set

func (fv FlagValues) Set(name string, value Value)

type Flags

type Flags map[string]*Flag

func (Flags) Get

func (f Flags) Get(name string) *Flag

func (Flags) Set

func (f Flags) Set(flag *Flag)

type Item

type Item struct {
	Level    LevelType
	Payload  interface{}
	Children map[string]*Item
}

type LevelType

type LevelType int
const (
	LevelTypeNone     LevelType = 0 << 1
	LevelTypePath     LevelType = 1 << 1
	LevelTypeCommand  LevelType = 2 << 1
	LevelTypeFlag     LevelType = 3 << 1
	LevelTypeOption   LevelType = 4 << 1
	LevelTypeVariable LevelType = 5 << 1
)

type List

type List struct {
	Commands []*Command
}

func (*List) Items

func (l *List) Items() (map[string]*Item, error)

type MathState

type MathState int
const (
	StateMathNone MathState = iota
	StateMathOperatorAfterNone
	StateMathOperatorNotAfterExpression
	StateMathOperatorNotAfterFinished
	StateMathOperatorFinished
	StateMathOperatorNotFinished
	StateMathExpression
)

type MockExpression

type MockExpression struct {
	mock.Mock
}

MockExpression is an autogenerated mock type for the Expression type

func (*MockExpression) Add

func (_m *MockExpression) Add(ctx SystemContext, r models.Rune) *Response

Add provides a mock function with given fields: ctx, r

func (*MockExpression) Close

func (_m *MockExpression) Close(ctx SystemContext) *CloseResponse

Close provides a mock function with given fields: ctx

func (*MockExpression) Complete

func (_m *MockExpression) Complete(ctx SystemContext) *CompleteResponse

Complete provides a mock function with given fields: ctx

func (*MockExpression) Type

func (_m *MockExpression) Type() ExpressionType

Type provides a mock function with given fields:

func (*MockExpression) Value

func (_m *MockExpression) Value(ctx SystemContext) Value

Value provides a mock function with given fields: ctx

type MockTestExec

type MockTestExec struct {
	mock.Mock
}

MockTestExec is an autogenerated mock type for the TestExec type

func (*MockTestExec) Exec

func (_m *MockTestExec) Exec(ctx Context, flags FlagValues, options Options) (Value, error)

Exec provides a mock function with given fields: ctx, flags, options

type NextOption

type NextOption struct {
	Name  string
	Level LevelType
}

type NextOptions

type NextOptions struct {
	AggregatedLevel LevelType
	Options         []*NextOption
	Merged          string
}

type Object

type Object int
const (
	ObjectNone Object = iota
	ObjectError
	ObjectPath
	ObjectCommand
	ObjectMandatoryFlag
	ObjectOptionalFlag
	ObjectUnknown
	ObjectValue
	ObjectOption
	ObjectVariableName
	ObjectVariableWrongName
	ObjectQuotedString
	ObjectComment

	// symbols
	ObjectSpace
	ObjectEqualSymbol
	ObjectVariableSymbol
	ObjectQuotedSymbol
	ObjectOperator
	ObjectSquareBrackets
	ObjectRoundBrackets
	ObjectCurlyBrackets
)

func (Object) IsSingle

func (o Object) IsSingle() bool

type Operator

type Operator int
const (
	OperatorTypeCompare     Operator = 1 << 3
	OperatorTypeConcatenate Operator = 1 << 4
	OperatorTypeAddition    Operator = 1 << 5
	OperatorTypeMultiply    Operator = 1 << 6
	OperatorTypeUnary       Operator = 1 << 7
	OperatorTypeMask        Operator = 0xf8 // 1111 1000
)

func (Operator) Equals

func (o Operator) Equals(op Operator) bool

func (Operator) IsType

func (o Operator) IsType(t Operator) bool

func (Operator) LessOrEqualThan

func (o Operator) LessOrEqualThan(op Operator) bool

func (Operator) LessThan

func (o Operator) LessThan(op Operator) bool

type Option

type Option struct {
	Name string
}

func (*Option) Copy

func (o *Option) Copy() *Option

type Options

type Options map[string]bool

func (Options) Get

func (o Options) Get(name string) bool

func (Options) Set

func (o Options) Set(name string)

type OutFunc

type OutFunc func(SystemContext, Flags, Options)

type ParseRuneResponse

type ParseRuneResponse struct {
	Object Object
}

type ParseStringResponse

type ParseStringResponse struct {
	Objects []*ParsedObject
	Error   error
}

type ParsedObject

type ParsedObject struct {
	Object Object
	Length int
}

type Parser

type Parser interface {
	Flush()
	IsFlushed() bool
	Add(r models.Rune) (*ParseRuneResponse, error)
	ParseString(s string) *ParseStringResponse
	Exec() (*ExecResponse, error)
	Continue() *CompleteResponse
}

type Payload

type Payload struct {
	Level    LevelType
	Value    string
	NextTree *CommandTree
	Payload  interface{}
}

type Response

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

func NewResponse

func NewResponse() *Response

func (*Response) Action

func (r *Response) Action() ResponseAction

func (*Response) ContextType

func (r *Response) ContextType() ContextType

func (*Response) Err

func (r *Response) Err() error

func (*Response) Expression

func (r *Response) Expression() Expression

func (*Response) WithAction

func (r *Response) WithAction(action ResponseAction) *Response

func (*Response) WithContextType

func (r *Response) WithContextType(ctxType ContextType) *Response

func (*Response) WithError

func (r *Response) WithError(err error) *Response

func (*Response) WithExpression

func (r *Response) WithExpression(exp Expression) *Response

func (*Response) WithObject

func (r *Response) WithObject(object Object) *Response

type ResponseAction

type ResponseAction int
const (
	ResponseGoNext ResponseAction = iota
	ResponseGoOut
	ResponseRepeat
)

type ResponseType

type ResponseType string

type Result

type Result struct {
	Message string
}

type StateCommand

type StateCommand int
const (
	StateCommandStart StateCommand = iota
	StateCommandPath
	StateCommandCommand
	StateCommandArgument
	StateCommandFlag
	StateFlagEqual
	StateFlagValue
	StateCommandOption
)

type SystemContext

type SystemContext interface {
	Context
	CommandTree() *CommandTree
	SetCommandTree(commandTree *CommandTree)
	CommandRoot() *CommandTree
	SetCommandRoot(commandRoot *CommandTree)
	VariableTree() *VariableTree
	GetVariablePayload(name string) interface{}
	VariableExists(name string) bool
	GetVariable(name string) Value
	SetGlobalVariable(name string, value interface{})
	SetLocalVariable(name string, value interface{})
	Ctx() context.Context
	WithContext(ctx context.Context) SystemContext
	New() SystemContext
	Copy() SystemContext
	Logger() logger.Logger
}

type SystemExecFunc

type SystemExecFunc func(SystemContext, Flags, Options) (Value, error)

type TestExec

type TestExec interface {
	Exec(ctx Context, flags FlagValues, options Options) (Value, error)
}

type Value

type Value interface {
	Bool() bool
	Number() int
	String() string

	IsBool() bool
	IsString() bool
	IsNumber() bool

	Equal(v Value) bool
	Less(v Value) bool
	Greater(v Value) bool
}

func NewBoolValue

func NewBoolValue(value bool) Value

func NewNumberValue

func NewNumberValue(v int) Value

func NewStringValue

func NewStringValue(v string) Value

type ValueType

type ValueType int
const (
	ValueTypeString ValueType = iota
	ValueTypeNumber
	ValueTypeBool
)

type Valuer

type Valuer interface {
	Value(ctx SystemContext) Value
}

type VariableState

type VariableState int
const (
	VariableStateStart VariableState = iota
	VariableStateName
	VariableStateArgument
	VariableStateFlagName
	VariableStateValue
)

type VariableTree

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

func NewVariableTree

func NewVariableTree() *VariableTree

func (*VariableTree) AddGlobal

func (t *VariableTree) AddGlobal(name string, value interface{})

func (*VariableTree) AddLocal

func (t *VariableTree) AddLocal(name string, value interface{})

func (*VariableTree) Copy

func (t *VariableTree) Copy() *VariableTree

func (*VariableTree) Get

func (t *VariableTree) Get(name string) interface{}

func (*VariableTree) GetIterator

func (t *VariableTree) GetIterator() *variableIterator

Jump to

Keyboard shortcuts

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