dvgrammar

package
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

********************************************************************** MicroCore Copyright 2020 - 2022 by Danyil Dobryvechir (dobrivecher@yahoo.com ddobryvechir@gmail.com) ***********************************************************************

********************************************************************** MicroCore Copyright 2020 - 2020 by Danyil Dobryvechir (dobrivecher@yahoo.com ddobryvechir@gmail.com) ***********************************************************************

********************************************************************** MicroCore Copyright 2017 - 2022 by Danyil Dobryvechir (dobrivecher@yahoo.com ddobryvechir@gmail.com) ***********************************************************************

********************************************************************** MicroCore Copyright 2020 - 2020 by Danyil Dobryvechir (dobrivecher@yahoo.com ddobryvechir@gmail.com) ***********************************************************************

********************************************************************** MicroCore Copyright 2020 - 2020 by Danyil Dobryvechir (dobrivecher@yahoo.com ddobryvechir@gmail.com) ***********************************************************************

********************************************************************** MicroCore Copyright 2020 - 2020 by Danyil Dobryvechir (dobrivecher@yahoo.com ddobryvechir@gmail.com) ***********************************************************************

********************************************************************** MicroCore Copyright 2020 - 2022 by Danyil Dobryvechir (dobrivecher@yahoo.com ddobryvechir@gmail.com) ***********************************************************************

********************************************************************** MicroCore Copyright 2020 - 2020 by Danyil Dobryvechir (dobrivecher@yahoo.com ddobryvechir@gmail.com) ***********************************************************************

********************************************************************** MicroCore Copyright 2020 - 2020 by Danyil Dobryvechir (dobrivecher@yahoo.com ddobryvechir@gmail.com) ***********************************************************************

********************************************************************** MicroCore Copyright 2020 - 2020 by Danyil Dobryvechir (dobrivecher@yahoo.com ddobryvechir@gmail.com) ***********************************************************************

********************************************************************** MicroCore Copyright 2020 - 2022 by Danyil Dobryvechir (dobrivecher@yahoo.com ddobryvechir@gmail.com) ***********************************************************************

********************************************************************** MicroCore Copyright 2017 - 2022 by Danyil Dobryvechir (dobrivecher@yahoo.com ddobryvechir@gmail.com) ***********************************************************************

Index

Constants

View Source
const (
	NON_LATIN_LETTERS_ONLY_IN_STRINGS = 1 << iota
	SINGLE_QUOTE_HAS_TYPE_CHAR
	EXCLUDE_RECOGNIZER_BYTE_LETTER
	EXCLUDE_RECOGNIZER_BYTE_DIGIT
	EXCLUDE_RECOGNIZER_BYTE_QUOTATION
	EXCLUDE_RECOGNIZER_BYTE_CONTROL
	EXCLUDE_RECOGNIZER_BYTE_LIST_CONTROL
)

bitwise values of EvaluateOptions

View Source
const (
	FEATURE_ROUND_BRACKET          = 1 << iota
	FEATURE_CURLY_BRACKETS         = 1 << iota
	FEATURE_CURLY_BRACKETS_OR_CODE = 1 << iota
	FEATURE_FINISH                 = 1 << iota
	FEATURE_FINISH_OR_ELSE         = 1 << iota
)
View Source
const (
	TYPE_CONTROL    = iota
	TYPE_OPERATOR   = iota
	TYPE_STRING     = iota
	TYPE_CHAR       = iota
	TYPE_NUMBER     = iota
	TYPE_NUMBER_INT = iota
	TYPE_BOOLEAN    = iota
	TYPE_NULL       = iota
	TYPE_UNDEFINED  = iota
	TYPE_NAN        = iota
	TYPE_FUNCTION   = iota
	TYPE_DATA       = iota
	TYPE_OBJECT     = iota
	TYPE_ERROR      = iota
	TYPE_MASK       = 0x7ff
)
View Source
const (
	FLOW_NORMAL = iota
	FLOW_RETURN
	FLOW_BREAK
	FLOW_CONTINUE
)
View Source
const (
	EVALUATE_OPTION_UNDEFINED = 1 << (iota + 8)
	EVALUATE_OPTION_PARENT    = 1 << (iota + 8)
	EVALUATE_OPTION_NAME      = 1 << (iota + 8)
)
View Source
const (
	BYTE_ONLY_INSIDE_STRING = iota
	BYTE_CONTROL
	BYTE_DIGIT
	BYTE_LETTER
	BYTE_LIST_CLOSING
	BYTE_LIST_SEPARATOR
	BYTE_OPERATOR
	BYTE_QUOTATION
	BYTE_SPACE
)
View Source
const (
	VISITOR_OPTION_CASHED = 1 << iota
)

bitwise values of VisitorOptions

Variables

View Source
var CommonGrammarBaseDefinition = &GrammarBaseDefinition{
	Operators: map[string]*InterOperator{
		";": &InterOperator{
			Precedence: 0,
			Multi:      true,
		},
		"in": &InterOperator{
			Precedence: 1,
		},
		"of": &InterOperator{
			Precedence: 1,
		},
		"else": &InterOperator{
			Precedence: 1,
			Multi:      true,
		},
		",": &InterOperator{
			Precedence: 1,
			Multi:      true,
		},
		"=": &InterOperator{
			Precedence:         3,
			Multi:              true,
			RightToLeft:        true,
			Assignment:         true,
			LazyLoadedOperands: true,
		},
		"+=": &InterOperator{
			Precedence:  3,
			Multi:       true,
			RightToLeft: true,
			Assignment:  true,
		},
		"-=": &InterOperator{
			Precedence:  3,
			Multi:       true,
			RightToLeft: true,
			Assignment:  true,
		},
		"**=": &InterOperator{
			Precedence:  3,
			Multi:       true,
			RightToLeft: true,
			Assignment:  true,
		},
		"*=": &InterOperator{
			Precedence:  3,
			Multi:       true,
			RightToLeft: true,
			Assignment:  true,
		},
		"/=": &InterOperator{
			Precedence:  3,
			Multi:       true,
			RightToLeft: true,
			Assignment:  true,
		},
		"%=": &InterOperator{
			Precedence:  3,
			Multi:       true,
			RightToLeft: true,
			Assignment:  true,
		},
		"<<=": &InterOperator{
			Precedence:  3,
			Multi:       true,
			RightToLeft: true,
			Assignment:  true,
		},
		">>=": &InterOperator{
			Precedence:  3,
			Multi:       true,
			RightToLeft: true,
			Assignment:  true,
		},
		">>>=": &InterOperator{
			Precedence:  3,
			Multi:       true,
			RightToLeft: true,
			Assignment:  true,
		},
		"&=": &InterOperator{
			Precedence:  3,
			Multi:       true,
			RightToLeft: true,
			Assignment:  true,
		},
		"^=": &InterOperator{
			Precedence:  3,
			Multi:       true,
			RightToLeft: true,
			Assignment:  true,
		},
		"|=": &InterOperator{
			Precedence:  3,
			Multi:       true,
			RightToLeft: true,
			Assignment:  true,
		},
		"&&=": &InterOperator{
			Precedence:  3,
			Multi:       true,
			RightToLeft: true,
			Assignment:  true,
		},
		"||=": &InterOperator{
			Precedence:  3,
			Multi:       true,
			RightToLeft: true,
			Assignment:  true,
		},
		"??=": &InterOperator{
			Precedence:  3,
			Multi:       true,
			RightToLeft: true,
			Assignment:  true,
		},
		"?": &InterOperator{
			Precedence:         4,
			Multi:              false,
			RightToLeft:        true,
			LazyLoadedOperands: true,
		},
		":": &InterOperator{
			Precedence:         4,
			Multi:              false,
			RightToLeft:        true,
			LazyLoadedOperands: true,
		},
		"||": &InterOperator{
			Precedence:         5,
			Multi:              true,
			LazyLoadedOperands: true,
		},
		"&&": &InterOperator{
			Precedence:         6,
			Multi:              true,
			LazyLoadedOperands: true,
		},
		"??": &InterOperator{
			Precedence:         6,
			Multi:              true,
			LazyLoadedOperands: true,
		},
		"|": &InterOperator{
			Precedence: 7,
			Multi:      true,
		},
		"^": &InterOperator{
			Precedence: 8,
			Multi:      true,
		},
		"&": &InterOperator{
			Precedence: 9,
			Multi:      true,
		},
		"!==": &InterOperator{
			Precedence: 10,
		},
		"===": &InterOperator{
			Precedence: 10,
		},
		"==": &InterOperator{
			Precedence: 10,
		},
		"!=": &InterOperator{
			Precedence: 10,
		},
		"<": &InterOperator{
			Precedence: 11,
		},
		">": &InterOperator{
			Precedence: 11,
		},
		"<=": &InterOperator{
			Precedence: 11,
		},
		">=": &InterOperator{
			Precedence: 11,
		},
		"<<": &InterOperator{
			Precedence: 12,
		},
		">>": &InterOperator{
			Precedence: 12,
		},
		">>>": &InterOperator{
			Precedence: 12,
		},
		"+": &InterOperator{
			Precedence: 13,
			Multi:      true,
		},
		"-": &InterOperator{
			Precedence: 13,
			Multi:      true,
		},
		"*": &InterOperator{
			Precedence: 14,
		},
		"/": &InterOperator{
			Precedence: 14,
		},
		"%": &InterOperator{
			Precedence: 14,
		},
		"**": &InterOperator{
			Precedence: 15,
		},
		"IN": &InterOperator{
			Precedence: 16,
		},
		"NOT IN": &InterOperator{
			Precedence: 16,
		},
		"=>": &InterOperator{
			Precedence: 18,
			Multi:      true,
		},
		"(": &InterOperator{
			Precedence: 19,
			Closing:    ")",
		},
		")": &InterOperator{
			Precedence: 19,
			Opening:    "(",
		},
		"[": &InterOperator{
			Precedence: 19,
			Closing:    "]",
		},
		"]": &InterOperator{
			Precedence: 19,
			Opening:    "[",
		},
		".": &InterOperator{
			Precedence: 19,
			Multi:      true,
		},
	},
	UnaryOperators: map[string]*UnaryOperator{
		"!": &UnaryOperator{
			Post: false,
			Pre:  true,
		},
		"~": &UnaryOperator{
			Post: false,
			Pre:  true,
		},
		"+": &UnaryOperator{
			Post: false,
			Pre:  true,
		},
		"-": &UnaryOperator{
			Post: false,
			Pre:  true,
		},
		"++": &UnaryOperator{
			Post: true,
			Pre:  true,
		},
		"--": &UnaryOperator{
			Post: true,
			Pre:  true,
		},
		"...": &UnaryOperator{
			Post: true,
			Pre:  false,
		},
	},
	VoidOperators: map[string]int{
		"var":        1,
		"const":      1,
		"let":        1,
		"try":        1,
		"catch":      1,
		"public":     1,
		"private":    1,
		"protected":  1,
		"await":      1,
		"class":      1,
		"debugger":   1,
		"enum":       1,
		"export":     1,
		"extends":    1,
		"finally":    1,
		"implements": 1,
		"import":     1,
		"instanceof": 1,
		"interface":  1,
		"new":        1,
		"package":    1,
		"super":      1,
		"static":     1,
		"void":       1,
		"with":       1,
		"yield":      1,
	},
	Language: map[string]*LanguageOperator{
		"return": {
			AlwaysFirst:      true,
			CanHaveArgument:  true,
			MustHaveArgument: false,
		},
		"for": {
			AlwaysFirst:                  true,
			CanHaveArgument:              false,
			MustHaveArgument:             false,
			ParenthesesFollow:            true,
			CurlyBracesFollowParentheses: true,
			FeatureOptions:               FEATURE_ROUND_BRACKET | FEATURE_CURLY_BRACKETS_OR_CODE | FEATURE_FINISH,
		},
		"if": {
			AlwaysFirst:                  true,
			CanHaveArgument:              false,
			MustHaveArgument:             false,
			ParenthesesFollow:            true,
			CurlyBracesFollowParentheses: true,
			FeatureOptions:               FEATURE_ROUND_BRACKET | FEATURE_CURLY_BRACKETS_OR_CODE | FEATURE_FINISH_OR_ELSE,
		},
		"else": {
			AlwaysFirst:      false,
			CanHaveArgument:  true,
			MustHaveArgument: false,
		},
		"break": {
			AlwaysFirst:      true,
			CanHaveArgument:  true,
			MustHaveArgument: false,
		},
		"continue": {
			AlwaysFirst:      true,
			CanHaveArgument:  true,
			MustHaveArgument: false,
		},
		"switch": {
			AlwaysFirst:                  true,
			CanHaveArgument:              false,
			MustHaveArgument:             false,
			ParenthesesFollow:            true,
			CurlyBracesFollowParentheses: true,
		},
		"case": {
			AlwaysFirst:      true,
			CanHaveArgument:  true,
			MustHaveArgument: true,
		},
		"default": {
			AlwaysFirst:      true,
			CanHaveArgument:  false,
			MustHaveArgument: false,
		},
		"function": {
			AlwaysFirst:                  false,
			CanHaveArgument:              true,
			MustHaveArgument:             false,
			ParenthesesFollow:            true,
			CurlyBracesFollowParentheses: true,
		},
		"while": {
			AlwaysFirst:                  true,
			CanHaveArgument:              false,
			MustHaveArgument:             false,
			ParenthesesFollow:            true,
			CurlyBracesFollowParentheses: true,
		},
		"typeof": {
			AlwaysFirst:      true,
			CanHaveArgument:  true,
			MustHaveArgument: true,
		},
		"do": {
			AlwaysFirst:      true,
			CanHaveArgument:  false,
			MustHaveArgument: false,
		},
		"throw": {
			AlwaysFirst:      true,
			CanHaveArgument:  true,
			MustHaveArgument: true,
		},
		"delete": {
			AlwaysFirst:      true,
			CanHaveArgument:  true,
			MustHaveArgument: true,
		},
	},
	DefaultOperator: "",
}
View Source
var ErrorExpressionValue = &ExpressionValue{DataType: TYPE_ERROR}
View Source
var LogicalGrammarBaseDefinition = &GrammarBaseDefinition{
	Operators: map[string]*InterOperator{
		"||": &InterOperator{
			Precedence:         0,
			Multi:              true,
			LazyLoadedOperands: true,
		},
		"&&": &InterOperator{
			Precedence:         1,
			Multi:              true,
			LazyLoadedOperands: true,
		},
	},
	UnaryOperators: map[string]*UnaryOperator{
		"!": &UnaryOperator{
			Post: false,
			Pre:  true,
		},
	},
	DefaultOperator: "&&",
}

Functions

func CheckCreateGrammarTable

func CheckCreateGrammarTable(rules *GrammarRuleDefinitions)

func EnrichErrorStr

func EnrichErrorStr(err error, info string) error

func ErrorMessageForNode

func ErrorMessageForNode(message string, node *BuildNode, context *ExpressionContext) error

func ExecuteBracketExpression added in v1.0.2

func ExecuteBracketExpression(parent *ExpressionValue, hasNoParent bool, nodes []*BuildNode, context *ExpressionContext) (*ExpressionValue, *ExpressionValue, error)

func GetEscapedString

func GetEscapedString(data []byte) string

Types

type BracketOperatorVisitor added in v1.0.2

type BuildNode

type BuildNode struct {
	Children       []*BuildNode
	Parent         *BuildNode
	Operator       string
	Value          *Token
	PreAttributes  []string
	PostAttributes []string
	Group          int
	// contains filtered or unexported fields
}

func Compile

func Compile(data []byte, context *ExpressionContext) ([]*BuildNode, error)

func CompileOrCache

func CompileOrCache(data []byte, context *ExpressionContext, cache bool) ([]*BuildNode, error)

func (*BuildNode) CloneFrom added in v1.0.2

func (b *BuildNode) CloneFrom(other *BuildNode)

func (*BuildNode) ExecuteExpression

func (tree *BuildNode) ExecuteExpression(context *ExpressionContext) (int, *ExpressionValue, error)

func (*BuildNode) GetChildrenExpressionValue

func (tree *BuildNode) GetChildrenExpressionValue(childNo int, context *ExpressionContext) (*ExpressionValue, error)

func (*BuildNode) GetChildrenNumber

func (tree *BuildNode) GetChildrenNumber() int

type ExpressionContext

type ExpressionContext struct {
	Scope          ScopeInterface
	Reference      *SourceReference
	Rules          *GrammarRuleDefinitions
	VisitorOptions int
}

type ExpressionValue

type ExpressionValue struct {
	DataType int
	Value    interface{}
	Name     string
	Parent   *ExpressionValue
}

func BuildNodeExecution added in v1.0.2

func BuildNodeExecution(nodes []*BuildNode, context *ExpressionContext) (flow int, value *ExpressionValue, err error)

func FastEvaluation

func FastEvaluation(data []byte, context *ExpressionContext) (*ExpressionValue, error)

type GrammarBaseDefinition

type GrammarBaseDefinition struct {
	Operators       map[string]*InterOperator
	UnaryOperators  map[string]*UnaryOperator
	VoidOperators   map[string]int
	Language        map[string]*LanguageOperator
	DefaultOperator string
}

type GrammarRuleDefinitions

type GrammarRuleDefinitions struct {
	BaseGrammar       *GrammarBaseDefinition
	GrammarAliases    map[string]string
	Recognizers       []TypeRecognizer
	EvaluateOptions   int
	Visitors          map[string]InterOperatorVisitor
	BracketVisitor    map[string]BracketOperatorVisitor
	LanguageOperator  map[string]LanguageOperatorVisitor
	UnaryPostVisitors map[string]UnaryVisitor
	UnaryPreVisitors  map[string]UnaryVisitor
	DataGetter        func(*Token, *ExpressionContext) (*ExpressionValue, error)

	Grammar *GrammarTable
	// contains filtered or unexported fields
}

type GrammarTable

type GrammarTable struct {
	TokenMap              TokenMapType
	IsSingleQuoteTypeChar bool
}

type InterOperator

type InterOperator struct {
	Precedence         int
	Multi              bool
	RightToLeft        bool
	LazyLoadedOperands bool
	Assignment         bool
	Opening            string
	Closing            string
}

type LanguageOperator added in v1.0.2

type LanguageOperator struct {
	AlwaysFirst                  bool
	CanHaveArgument              bool
	MustHaveArgument             bool
	ParenthesesFollow            bool
	CurlyBracesFollowParentheses bool
	FeatureOptions               int
}

type LanguageOperatorVisitor added in v1.0.2

type LanguageOperatorVisitor func(*BuildNode, *ExpressionContext) (int, *ExpressionValue, error)

type ScopeInterface

type ScopeInterface interface {
	Get(key string) (interface{}, bool)
	Set(key string, value interface{})
	StackPush(option int)
	StackPop()
	SetDeep(key string, value interface{})
}

type SourceReference

type SourceReference struct {
	Row    int
	Column int
	Place  string
}

type Token

type Token struct {
	DataType int
	Row      int
	Column   int
	Place    string
	Value    string
}

func RecognizerByteControl

func RecognizerByteControl(grammar *GrammarTable, data []byte, pos int, n int) (t *Token, nextPos int, err error)

func RecognizerByteDigit

func RecognizerByteDigit(grammar *GrammarTable, data []byte, pos int, n int) (t *Token, nextPos int, err error)

func RecognizerByteLetter

func RecognizerByteLetter(grammar *GrammarTable, data []byte, pos int, n int) (t *Token, nextPos int, err error)

func RecognizerByteListControl

func RecognizerByteListControl(grammar *GrammarTable, data []byte, pos int, n int) (t *Token, nextPos int, err error)

func RecognizerByteQuotation

func RecognizerByteQuotation(grammar *GrammarTable, data []byte, pos int, n int) (t *Token, nextPos int, err error)

func Tokenize

func Tokenize(src *SourceReference, data []byte, grammar *GrammarTable) ([]Token, error)

type TokenBlock

type TokenBlock struct {
	Kind        int
	IsCommon    bool
	Controls    []map[string]string
	MaxWords    int
	MaxLength   int
	Recognizers []TypeRecognizer
}

type TokenMapType

type TokenMapType [256]*TokenBlock

type TypeRecognizer

type TypeRecognizer struct {
	StartByteTypes []int
	Processor      func(grammar *GrammarTable, data []byte, pos int, n int) (*Token, int, error)
	// contains filtered or unexported fields
}

type UnaryOperator

type UnaryOperator struct {
	Post bool
	Pre  bool
}

Jump to

Keyboard shortcuts

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