feel

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2023 License: MIT Imports: 16 Imported by: 0

README

FEEL.go

The interpreter of the FEEL language(Friendly Enough Expression Language) in go, FEEL is broadly used in DMN and BPMN to provide rule engine and script support, the FEEL.go module can be imported into other go projects or used as command line executable as FEEL interpreter.

Build

  • run make build to build feel interpreter bin/feel
  • run make test to run testing

Examples


% bin/feel -c '"hello " + "world"'
"hello world"

% bin/feel -c '(function(a, b) a + b)(5, 8)'
13

# dump AST tree instead of evaluating the script
% bin/feel -c 'bind("a", 5); if a > 3 then "larger" else "smaller"' -ast
(explist (call bind ["a", 5]) (if (> a 3) "larger"  "smaller"))

% bin/feel -c 'some x in [3, 4, 8, 9] satisfies x % 2 = 0'
4

% bin/feel -c 'every x in [3, 4, 8, 9] satisfies x % 2 = 0'
[
  4,
  8
]

for more examples please refer to testing

Use in golang codes

import (
  feel "github.com/superisaac/FEEL.go"
)

res, err := feel.EvalString(input)

Documentation

Index

Constants

View Source
const (
	TokenEOF               = "eof"
	TokenSpace             = "space"
	TokenCommentSingleLine = "comment/singleline"
	TokenCommentMultiline  = "comment/multiline"

	TokenName = "name"
	//TokenFuncall  = "funcall"
	TokenTemporal = "temporal"
	TokenString   = "string"

	TokenKeyword = "keyword"
	TokenNumber  = "number"
)
View Source
const (
	Prec = 34 * 8
)

Variables

View Source
var (
	ErrParseNumber = errors.New("fail to parse number")
)
View Source
var ErrParseTemporal = errors.New("fail to parse temporal value")
View Source
var Null = &NullValue{}
View Source
var Zero = N(0)

Functions

func CompareValues added in v0.1.0

func CompareValues(leftVal, rightVal any) int

func EvalString

func EvalString(input string) (any, error)

func EvalStringWithScope

func EvalStringWithScope(input string, scope Scope) (any, error)

func ParseTemporalValue added in v0.1.0

func ParseTemporalValue(temporalStr string) (interface{}, error)

Types

type ArgSizeError

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

ArgSizeError

func (ArgSizeError) Error

func (self ArgSizeError) Error() string

type ArgTypeError

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

arg type error

func (ArgTypeError) Error

func (self ArgTypeError) Error() string

type ArrayNode

type ArrayNode struct {
	Elements []Node
	// contains filtered or unexported fields
}

array

func (ArrayNode) Eval

func (self ArrayNode) Eval(intp *Interpreter) (any, error)

func (ArrayNode) Repr

func (self ArrayNode) Repr() string

func (ArrayNode) TextRange added in v0.1.0

func (self ArrayNode) TextRange() TextRange

type Binop

type Binop struct {
	Op    string
	Left  Node
	Right Node
	// contains filtered or unexported fields
}

binary operator

func (Binop) Eval

func (self Binop) Eval(intp *Interpreter) (any, error)

func (Binop) Repr

func (self Binop) Repr() string

func (Binop) TextRange added in v0.1.0

func (self Binop) TextRange() TextRange

type BoolNode

type BoolNode struct {
	Value bool
	// contains filtered or unexported fields
}

bool

func (BoolNode) Eval

func (self BoolNode) Eval(intp *Interpreter) (any, error)

Evaluate bool node

func (BoolNode) Repr

func (self BoolNode) Repr() string

func (BoolNode) TextRange added in v0.1.0

func (self BoolNode) TextRange() TextRange

type DotOp

type DotOp struct {
	Left Node
	Attr string
	// contains filtered or unexported fields
}

function call

func (DotOp) Eval

func (self DotOp) Eval(intp *Interpreter) (any, error)

func (DotOp) Repr

func (self DotOp) Repr() string

func (DotOp) TextRange added in v0.1.0

func (self DotOp) TextRange() TextRange

type EvalError

type EvalError struct {
	Code    int
	Short   string
	Message string
}

func NewErrBadOp added in v0.1.0

func NewErrBadOp(leftType, op, rightType string) *EvalError

func NewErrIndex added in v0.1.0

func NewErrIndex(msg string) *EvalError

func NewErrKeyNotFound added in v0.1.0

func NewErrKeyNotFound(keyName string) *EvalError

func NewErrKeywordArgument added in v0.1.0

func NewErrKeywordArgument(argName string) *EvalError

argument errors

func NewErrTooFewArguments added in v0.1.0

func NewErrTooFewArguments(required []string) *EvalError

func NewErrTooManyArguments added in v0.1.0

func NewErrTooManyArguments() *EvalError

func NewErrTypeMismatch added in v0.1.0

func NewErrTypeMismatch(expectType string) *EvalError

func NewErrValue added in v0.1.0

func NewErrValue(msg string) *EvalError

func NewEvalError

func NewEvalError(code int, short string, msgs ...string) *EvalError

func (EvalError) Error

func (self EvalError) Error() string

type EveryExpr

type EveryExpr struct {
	Varname    string
	ListExpr   Node
	FilterExpr Node
	// contains filtered or unexported fields
}

Every expression

func (EveryExpr) Eval

func (self EveryExpr) Eval(intp *Interpreter) (any, error)

func (EveryExpr) Repr

func (self EveryExpr) Repr() string

func (EveryExpr) TextRange added in v0.1.0

func (self EveryExpr) TextRange() TextRange

type ExprList

type ExprList struct {
	Elements []Node
	// contains filtered or unexported fields
}

ExpressList

func (ExprList) Eval

func (self ExprList) Eval(intp *Interpreter) (any, error)

func (ExprList) Repr

func (self ExprList) Repr() string

func (ExprList) TextRange added in v0.1.0

func (self ExprList) TextRange() TextRange

type FEELDate added in v0.1.0

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

Date

func ParseDate added in v0.1.0

func ParseDate(timeStr string) (*FEELDate, error)

func (FEELDate) Date added in v0.1.0

func (self FEELDate) Date() time.Time

func (FEELDate) GetAttr added in v0.1.0

func (self FEELDate) GetAttr(name string) (interface{}, bool)

func (FEELDate) MarshalJSON added in v0.1.0

func (self FEELDate) MarshalJSON() ([]byte, error)

func (FEELDate) String added in v0.1.0

func (self FEELDate) String() string

type FEELDatetime added in v0.1.0

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

Date and Time

func MustParseDatetime added in v0.1.0

func MustParseDatetime(temporalStr string) *FEELDatetime

func ParseDatetime added in v0.1.0

func ParseDatetime(temporalStr string) (*FEELDatetime, error)

func (*FEELDatetime) Add added in v0.1.0

func (self *FEELDatetime) Add(dur *FEELDuration) *FEELDatetime

func (FEELDatetime) Compare added in v0.1.0

func (self FEELDatetime) Compare(other FEELDatetime) int

func (FEELDatetime) Date added in v0.1.0

func (self FEELDatetime) Date() time.Time

func (FEELDatetime) Equal added in v0.1.0

func (self FEELDatetime) Equal(other FEELDatetime) bool

func (FEELDatetime) GetAttr added in v0.1.0

func (self FEELDatetime) GetAttr(name string) (interface{}, bool)

func (FEELDatetime) MarshalJSON added in v0.1.0

func (self FEELDatetime) MarshalJSON() ([]byte, error)

func (FEELDatetime) String added in v0.1.0

func (self FEELDatetime) String() string

func (*FEELDatetime) Sub added in v0.1.0

func (self *FEELDatetime) Sub(v HasTime) *FEELDuration

func (FEELDatetime) Time added in v0.1.0

func (self FEELDatetime) Time() time.Time

type FEELDuration added in v0.1.0

type FEELDuration struct {
	Neg     bool
	Years   int
	Months  int
	Days    int
	Hours   int
	Minutes int
	Seconds int
}

func NewFEELDuration added in v0.1.0

func NewFEELDuration(dur time.Duration) *FEELDuration

func ParseDuration added in v0.1.0

func ParseDuration(temporalStr string) (*FEELDuration, error)

func (FEELDuration) Duration added in v0.1.0

func (self FEELDuration) Duration() time.Duration

func (FEELDuration) GetAttr added in v0.1.0

func (self FEELDuration) GetAttr(name string) (interface{}, bool)

func (FEELDuration) MarshalJSON added in v0.1.0

func (self FEELDuration) MarshalJSON() ([]byte, error)

func (*FEELDuration) Negative added in v0.1.0

func (self *FEELDuration) Negative() *FEELDuration

func (FEELDuration) String added in v0.1.0

func (self FEELDuration) String() string

type FEELTime added in v0.1.0

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

time

func ParseTime added in v0.1.0

func ParseTime(temporalStr string) (*FEELTime, error)

func (FEELTime) GetAttr added in v0.1.0

func (self FEELTime) GetAttr(name string) (interface{}, bool)

func (FEELTime) MarshalJSON added in v0.1.0

func (self FEELTime) MarshalJSON() ([]byte, error)

func (FEELTime) String added in v0.1.0

func (self FEELTime) String() string

func (FEELTime) Time added in v0.1.0

func (self FEELTime) Time() time.Time

type ForExpr

type ForExpr struct {
	Varname    string
	ListExpr   Node
	ReturnExpr Node
	// contains filtered or unexported fields
}

For expression

func (ForExpr) Eval

func (self ForExpr) Eval(intp *Interpreter) (any, error)

func (ForExpr) Repr

func (self ForExpr) Repr() string

func (ForExpr) TextRange added in v0.1.0

func (self ForExpr) TextRange() TextRange

type FunCall

type FunCall struct {
	FunRef Node
	Args   []funcallArg
	// contains filtered or unexported fields
}

func (FunCall) Eval

func (self FunCall) Eval(intp *Interpreter) (any, error)

func (FunCall) EvalFunDef

func (self FunCall) EvalFunDef(intp *Interpreter, funDef *FunDef) (any, error)

func (FunCall) EvalMacro added in v0.1.0

func (self FunCall) EvalMacro(intp *Interpreter, macro *Macro) (any, error)

func (FunCall) EvalNativeFun

func (self FunCall) EvalNativeFun(intp *Interpreter, funDef *NativeFun) (any, error)

func (FunCall) Repr

func (self FunCall) Repr() string

func (FunCall) TextRange added in v0.1.0

func (self FunCall) TextRange() TextRange

type FunDef

type FunDef struct {
	Args []string
	Body Node
	// contains filtered or unexported fields
}

function definition

func (FunDef) Eval

func (self FunDef) Eval(intp *Interpreter) (any, error)

func (FunDef) EvalCall added in v0.1.0

func (self FunDef) EvalCall(intp *Interpreter, args []any) (any, error)

func (FunDef) Repr

func (self FunDef) Repr() string

func (FunDef) TextRange added in v0.1.0

func (self FunDef) TextRange() TextRange

type HasAttrs added in v0.1.0

type HasAttrs interface {
	GetAttr(name string) (interface{}, bool)
}

type HasDate added in v0.1.0

type HasDate interface {
	Date() time.Time
}

type HasTime added in v0.1.0

type HasTime interface {
	Time() time.Time
}

type IfExpr

type IfExpr struct {
	Cond       Node
	ThenBranch Node
	ElseBranch Node
	// contains filtered or unexported fields
}

if expression

func (IfExpr) Eval

func (self IfExpr) Eval(intp *Interpreter) (any, error)

func (IfExpr) Repr

func (self IfExpr) Repr() string

func (IfExpr) TextRange added in v0.1.0

func (self IfExpr) TextRange() TextRange

type Interpreter

type Interpreter struct {
	ScopeStack []Scope
}

func NewIntepreter

func NewIntepreter() *Interpreter

intepreter

func (*Interpreter) Bind

func (self *Interpreter) Bind(name string, value any)

bind the value to the name of current scope

func (Interpreter) Len

func (self Interpreter) Len() int

func (*Interpreter) Pop

func (self *Interpreter) Pop() Scope

func (*Interpreter) Push

func (self *Interpreter) Push(scp Scope)

func (*Interpreter) PushEmpty

func (self *Interpreter) PushEmpty()

func (Interpreter) Resolve

func (self Interpreter) Resolve(name string) (any, bool)

resolve a name from the top of scopestack to bottom

func (Interpreter) Set added in v0.1.0

func (self Interpreter) Set(name string, value any) bool

resolve the name and set to new value

func (Interpreter) String

func (self Interpreter) String() string

type Macro added in v0.1.0

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

func NewMacro added in v0.1.0

func NewMacro(fn MacroDef) *Macro

func (*Macro) Help added in v0.1.0

func (self *Macro) Help(help string) *Macro

func (*Macro) Optional added in v0.1.0

func (self *Macro) Optional(argNames ...string) *Macro

func (*Macro) Required added in v0.1.0

func (self *Macro) Required(argNames ...string) *Macro

func (*Macro) Vararg added in v0.1.0

func (self *Macro) Vararg(argName string) *Macro

type MacroDef added in v0.1.0

type MacroDef func(intp *Interpreter, args map[string]Node, varArgs []Node) (interface{}, error)

macro

type MapNode

type MapNode struct {
	Values []mapItem
	// contains filtered or unexported fields
}

func (MapNode) Eval

func (self MapNode) Eval(intp *Interpreter) (any, error)

func (MapNode) Repr

func (self MapNode) Repr() string

func (MapNode) TextRange added in v0.1.0

func (self MapNode) TextRange() TextRange

type MultiTests

type MultiTests struct {
	Elements []Node
	// contains filtered or unexported fields
}

MultiTests

func (MultiTests) Eval

func (self MultiTests) Eval(intp *Interpreter) (any, error)

func (MultiTests) Repr

func (self MultiTests) Repr() string

func (MultiTests) TextRange added in v0.1.0

func (self MultiTests) TextRange() TextRange

type NativeFun

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

func NewNativeFunc

func NewNativeFunc(fn NativeFunDef) *NativeFun

func (NativeFun) ArgNameAt added in v0.1.0

func (self NativeFun) ArgNameAt(at int) (string, bool)

func (*NativeFun) Call

func (self *NativeFun) Call(intp *Interpreter, args map[string]interface{}) (interface{}, error)

func (*NativeFun) Help added in v0.1.0

func (self *NativeFun) Help(help string) *NativeFun

func (*NativeFun) Optional added in v0.1.0

func (self *NativeFun) Optional(argNames ...string) *NativeFun

func (*NativeFun) Required added in v0.1.0

func (self *NativeFun) Required(argNames ...string) *NativeFun

func (*NativeFun) Vararg added in v0.1.0

func (self *NativeFun) Vararg(argName string) *NativeFun

type NativeFunDef

type NativeFunDef func(args map[string]interface{}) (interface{}, error)

native function

type Node added in v0.1.0

type Node interface {
	Repr() string
	Eval(*Interpreter) (interface{}, error)
	TextRange() TextRange
}

func ParseString

func ParseString(input string) (Node, error)

type NullNode

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

null

func (NullNode) Eval

func (self NullNode) Eval(intp *Interpreter) (any, error)

func (NullNode) Repr

func (self NullNode) Repr() string

func (NullNode) TextRange added in v0.1.0

func (self NullNode) TextRange() TextRange

type NullValue

type NullValue struct {
}

func (NullValue) Equal

func (self NullValue) Equal(other NullValue) bool

func (NullValue) MarshalJSON added in v0.1.0

func (self NullValue) MarshalJSON() ([]byte, error)

type Number

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

func N added in v0.1.0

func N(v interface{}) *Number

func NewNumber

func NewNumber(strn string) *Number

func NewNumberFromFloat

func NewNumberFromFloat(input float64) *Number

func NewNumberFromInt64

func NewNumberFromInt64(input int64) *Number

func ParseNumberWithErr added in v0.1.0

func ParseNumberWithErr(v interface{}) (*Number, error)

func (*Number) Add

func (self *Number) Add(other *Number) *Number

func (*Number) Cmp

func (self *Number) Cmp(other *Number) int

func (Number) Compare

func (self Number) Compare(other Number) int

func (Number) Equal

func (self Number) Equal(other Number) bool

func (Number) Float64 added in v0.1.0

func (self Number) Float64() float64

func (*Number) FloatDiv added in v0.1.0

func (self *Number) FloatDiv(other *Number) *Number

func (Number) Int added in v0.1.0

func (self Number) Int() int

func (Number) Int64

func (self Number) Int64() int64

func (*Number) IntDiv

func (self *Number) IntDiv(other *Number) *Number

func (*Number) IntMod

func (self *Number) IntMod(other *Number) *Number

func (Number) MarshalJSON

func (self Number) MarshalJSON() ([]byte, error)

func (*Number) Mul

func (self *Number) Mul(other *Number) *Number

func (Number) String

func (self Number) String() string

func (*Number) Sub

func (self *Number) Sub(other *Number) *Number

type NumberNode

type NumberNode struct {
	Value string
	// contains filtered or unexported fields
}

number

func (NumberNode) Eval

func (self NumberNode) Eval(intp *Interpreter) (any, error)

Evaluate Number node

func (NumberNode) Repr

func (self NumberNode) Repr() string

func (NumberNode) TextRange added in v0.1.0

func (self NumberNode) TextRange() TextRange

type Parser

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

func NewParser

func NewParser(scanner *Scanner) *Parser

func (Parser) CurrentToken

func (self Parser) CurrentToken() ScannerToken

func (*Parser) Parse

func (self *Parser) Parse() (Node, error)

func (Parser) Unexpected

func (self Parser) Unexpected(expects ...string) *UnexpectedToken

type Prelude

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

Prelude

func GetPrelude

func GetPrelude() *Prelude

func (*Prelude) Bind

func (self *Prelude) Bind(name string, value interface{}) *Prelude

func (*Prelude) Load

func (self *Prelude) Load()

func (*Prelude) Resolve

func (self *Prelude) Resolve(name string) (interface{}, bool)

type RangeNode

type RangeNode struct {
	StartOpen bool
	Start     Node

	EndOpen bool
	End     Node
	// contains filtered or unexported fields
}

range

func (RangeNode) Eval

func (self RangeNode) Eval(intp *Interpreter) (any, error)

func (RangeNode) Repr

func (self RangeNode) Repr() string

func (RangeNode) TextRange added in v0.1.0

func (self RangeNode) TextRange() TextRange

type RangeValue

type RangeValue struct {
	StartOpen bool
	Start     any

	EndOpen bool
	End     any
}

func (RangeValue) AfterPoint added in v0.1.0

func (self RangeValue) AfterPoint(p any) (bool, error)

func (RangeValue) AfterRange added in v0.1.0

func (self RangeValue) AfterRange(other RangeValue) (bool, error)

func (RangeValue) BeforePoint added in v0.1.0

func (self RangeValue) BeforePoint(p any) (bool, error)

func (RangeValue) BeforeRange added in v0.1.0

func (self RangeValue) BeforeRange(other RangeValue) (bool, error)

func (RangeValue) Contains

func (self RangeValue) Contains(p any) bool

func (RangeValue) Includes added in v0.1.0

func (self RangeValue) Includes(other RangeValue) (bool, error)

func (RangeValue) Position added in v0.1.0

func (self RangeValue) Position(p any) (int, error)

type ScanPosition

type ScanPosition struct {
	Row    int
	Column int
}

type Scanner

type Scanner struct {
	Pos   ScanPosition
	Eaten int
	// contains filtered or unexported fields
}

func NewScanner

func NewScanner(input string) *Scanner

func (Scanner) Current

func (scanner Scanner) Current() ScannerToken

func (*Scanner) Next

func (scanner *Scanner) Next() error

func (*Scanner) Tokens

func (scanner *Scanner) Tokens() ([]ScannerToken, error)

* * Find all tokens

type ScannerToken

type ScannerToken struct {
	Kind  string
	Value string
	Pos   ScanPosition
}

func (ScannerToken) Expect

func (token ScannerToken) Expect(tokenKinds ...string) bool

func (ScannerToken) ExpectKeywords

func (token ScannerToken) ExpectKeywords(words ...string) bool

func (ScannerToken) IsOp

func (token ScannerToken) IsOp() bool

type Scope

type Scope map[string]interface{}

type SomeExpr

type SomeExpr struct {
	Varname    string
	ListExpr   Node
	FilterExpr Node
	// contains filtered or unexported fields
}

Some expression

func (SomeExpr) Eval

func (self SomeExpr) Eval(intp *Interpreter) (any, error)

func (SomeExpr) Repr

func (self SomeExpr) Repr() string

func (SomeExpr) TextRange added in v0.1.0

func (self SomeExpr) TextRange() TextRange

type StringNode

type StringNode struct {
	Value string
	// contains filtered or unexported fields
}

string

func (StringNode) Content

func (self StringNode) Content() string

func (StringNode) Eval

func (self StringNode) Eval(intp *Interpreter) (any, error)

func (StringNode) Repr

func (self StringNode) Repr() string

func (StringNode) TextRange added in v0.1.0

func (self StringNode) TextRange() TextRange

type TemporalNode added in v0.1.0

type TemporalNode struct {
	Value string
	// contains filtered or unexported fields
}

temporal

func (TemporalNode) Content added in v0.1.0

func (self TemporalNode) Content() string

func (TemporalNode) Eval added in v0.1.0

func (self TemporalNode) Eval(intp *Interpreter) (any, error)

func (TemporalNode) Repr added in v0.1.0

func (self TemporalNode) Repr() string

func (TemporalNode) TextRange added in v0.1.0

func (self TemporalNode) TextRange() TextRange

type TextRange added in v0.1.0

type TextRange struct {
	Start ScanPosition
	End   ScanPosition
}

type UnexpectedToken

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

func NewUnexpectedToken

func NewUnexpectedToken(token ScannerToken, callers []string, expects []string) *UnexpectedToken

func (UnexpectedToken) Error

func (self UnexpectedToken) Error() string

type Var

type Var struct {
	Name string
	// contains filtered or unexported fields
}

variable

func (Var) Eval

func (self Var) Eval(intp *Interpreter) (any, error)

func (Var) Repr

func (self Var) Repr() string

func (Var) TextRange added in v0.1.0

func (self Var) TextRange() TextRange

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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