ast

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2023 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrUnknownType = errors.New("unknown type")

Functions

func DetermineArithmeticOperationResultType added in v0.7.0

func DetermineArithmeticOperationResultType(operand1 *Expression, operand2 *Expression) (ExpressionType, *TypeReference, error)

DetermineArithmeticOperationResultType determines the result type of an expression that involves two operands in an arithmetic operation. For example, when adding integers with floats, the resulting type should be float, and how to handle operations that mix unsigned/signed types, or types of different bit length.

func GoPackageAlias

func GoPackageAlias(packageName string) string

Types

type Array

type Array struct {
	IsPacked bool
	Length   *Expression
}

type BaseScope

type BaseScope struct {
	Parent Scope
}

func (*BaseScope) GoArrayTraits

func (s *BaseScope) GoArrayTraits(t *TypeReference) (string, error)

func (*BaseScope) GoType

func (s *BaseScope) GoType(t *TypeReference) (string, error)

GoType looks up a name in the scope, and provides the Go type name for it. If the type is not found ErrUnkownType is returned.

func (*BaseScope) HasType

func (s *BaseScope) HasType(t string) bool

HasType checks if a type is defined *in this scope*. It does not check parent scopes.

func (*BaseScope) OriginalType added in v0.7.0

func (s *BaseScope) OriginalType(t *TypeReference) (*OriginalTypeReference, error)

type BitmaskType

type BitmaskType struct {
	Name    string
	Comment string
	Type    *TypeReference
	Values  []*BitmaskValue
}

func (*BitmaskType) Evaluate

func (bitmask *BitmaskType) Evaluate(p *Package) error

type BitmaskValue

type BitmaskValue struct {
	Name string
	*Expression
}

type Choice

type Choice struct {
	Name               string
	Comment            string
	TemplateParameters []string
	TypeParameters     []*Parameter
	SelectorExpression *Expression
	Cases              []*ChoiceCase
	DefaultCase        *ChoiceCase
	Functions          []*Function
}

func (*Choice) BuildScope

func (choice *Choice) BuildScope(p *Package) error

func (*Choice) Evaluate

func (choice *Choice) Evaluate(p *Package) error

type ChoiceCase

type ChoiceCase struct {
	Conditions []*ChoiceCaseExpression
	Field      *Field
	Comment    string
}

type ChoiceCaseExpression

type ChoiceCaseExpression struct {
	Condition *Expression
	Comment   string
}

type CompoundScope

type CompoundScope struct {
	Symbol any
	Scope  map[string]any
}

type Const

type Const struct {
	Name            string
	Comment         string
	Type            *TypeReference
	ValueExpression *Expression
}

Const is a zserio constant

func (*Const) Evaluate added in v0.2.6

func (c *Const) Evaluate(scope *Package) error

Evaluate evalutes the value expression of a const type

type Enum

type Enum struct {
	Name    string
	Comment string
	Items   []*EnumItem
	Type    *TypeReference
}

func (*Enum) AddItem added in v0.4.0

func (e *Enum) AddItem(i *EnumItem) *EnumItem

func (*Enum) Evaluate

func (e *Enum) Evaluate(scope *Package) error

type EnumItem

type EnumItem struct {
	Name    string
	Comment string

	*Expression
	// contains filtered or unexported fields
}

func (EnumItem) GoName added in v0.4.0

func (e EnumItem) GoName() string

type EvaluationState

type EvaluationState int

EvaluationState is the type of the expression evaluation state

const (
	EvaluationStateNotStarted EvaluationState = iota
	EvaluationStateInProgress
	EvaluationStateComplete
)

type Expression

type Expression struct {
	// Type is the type of an expression, as defined in the ZserioParser.
	Type int

	// Text is the text of the expression.
	Text string

	// Operand1 is the first operand of the expression (if used).
	Operand1 *Expression

	// Operand2 is the second operand of the expression (if used).
	Operand2 *Expression

	// Operand3 is the third operand of the expression (if used).
	Operand3 *Expression

	// ResultType is the type of the evaluated expression, e.g. ExpressionTypeInteger.
	ResultType ExpressionType

	// EvaluationState is the evaluation state of the expression.
	EvaluationState EvaluationState

	// ResultIntValue is the value of the expression if ResultType is
	// ExpressionTypeInteger or ExpressionTypeBitmask.
	ResultIntValue int64

	// ResultFloatValue is the value of the expression if ResutlType is
	// ExpressionTypeFloat.
	ResultFloatValue float64

	// ResultStringValue is the value of the expression if ResultType is
	// ExpressionTypeString.
	ResultStringValue string

	// ResultBoolValue is the value of the expression if ResultType is
	// ExpressionTypeBool
	ResultBoolValue bool

	// NativeZserioType stores the exact zserio type, in case ResultType is
	// an integer or float type. This is needed to make type casts in the
	// generated Go code when mixing different types - for example
	// mixing varint with uint32, or mixing float types with integer types.
	NativeZserioType *TypeReference

	// ResultSymbol points to the symbol if ResultType is ExpressionTypeEnum or
	// ExpressionTypeStruct.
	ResultSymbol *SymbolReference

	// FullyResolved specifies if the expression is fully resolved (no variables)
	FullyResolved bool
}

Expression satisfies IExpressionContext

func (*Expression) Evaluate

func (expr *Expression) Evaluate(scope *Package) error

Evaluate evalues the value of the expression, by evaluating all child expressions. If successful, expr will have valid ExpressionType and Values set.

type ExpressionType

type ExpressionType string

ExpressionType is the type of an expression

const (
	ExpressionTypeInteger  ExpressionType = "int"
	ExpressionTypeFloat    ExpressionType = "float"
	ExpressionTypeString   ExpressionType = "string"
	ExpressionTypeBool     ExpressionType = "bool"
	ExpressionTypeBitmask  ExpressionType = "bitmask"
	ExpressionTypeEnum     ExpressionType = "enum"
	ExpressionTypeCompound ExpressionType = "compound"
)

type Field

type Field struct {
	Name string

	// ZserioName is the original name of the field, as it appears in the
	// zserio file.
	ZserioName string

	// Comment is the comment of the field in the zserio file.
	Comment string

	// Optional fields are always serialised as a boolean that indicates
	// if the value field is set. If the field is not set no value is
	// serialised.
	// http://zserio.org/doc/ZserioLanguageOverview.html#optional-members
	IsOptional     bool
	Alignment      uint8
	Type           *TypeReference
	Initializer    *Expression
	Constraint     *Expression
	OptionalClause *Expression
	// Array stored the array properties if the field is an array
	Array *Array
}

func (*Field) Evaluate

func (f *Field) Evaluate(p *Package) error

type Function

type Function struct {
	Name       string
	Comment    string
	Result     *Expression
	ReturnType *TypeReference
}

type Import

type Import struct {
	Package string
	Type    string
}

type InstantiateType

type InstantiateType struct {
	Name    string
	Comment string
	Type    *TypeReference
}

InstantiateType is an instantiation of a template.

type MissingEnumValue

type MissingEnumValue struct {
	Name string
}

func (MissingEnumValue) Error

func (e MissingEnumValue) Error() string

type OriginalTypeReference added in v0.7.0

type OriginalTypeReference struct {
	Type         *TypeReference
	RequiresCast bool
	IsMarshaler  bool
}

OriginalTypeReference is a reference to the original zserio type, for example fundamental types, but also enums/structs, that were aliased by a subtype.

type Package

type Package struct {
	BaseScope

	Name    string
	Comment string

	Imports           []*Import
	Consts            map[string]*Const
	Subtypes          map[string]*Subtype
	InstantiatedTypes map[string]*InstantiateType
	Enums             map[string]*Enum
	Unions            map[string]*Union
	Bitmasks          map[string]*BitmaskType
	Structs           map[string]*Struct
	Choices           map[string]*Choice

	ImportedPackages []*Package
	LocalSymbols     *SymbolScope
}

func NewPackage

func NewPackage(name string) *Package

func (*Package) CollectSymbols

func (p *Package) CollectSymbols() error

CollectSymbols collects all symbols from a package, and organizes them in the scope

func (*Package) GetCompoundType

func (p *Package) GetCompoundType(compoundSymbolName, name string) (*SymbolReference, error)

func (*Package) GetCurrentCompoundType

func (p *Package) GetCurrentCompoundType(name string) (*SymbolReference, error)

func (*Package) GetImportedScope

func (p *Package) GetImportedScope(name string) (*Package, error)

GetImportedScope returns a scope, if it can be found within the imported scope

func (*Package) GetOriginalSymbol added in v0.7.0

func (p *Package) GetOriginalSymbol(symbolReference *SymbolReference) (*SymbolReference, error)

func (*Package) GetOriginalType added in v0.7.0

func (p *Package) GetOriginalType(typeRef *TypeReference) (*OriginalTypeReference, error)

func (*Package) GetOtherType

func (p *Package) GetOtherType(name string) (*SymbolReference, error)

func (*Package) GetSymbol

func (p *Package) GetSymbol(name string) (*SymbolReference, error)

GetSymbol searches for a symbol not only in the current scope, but also in all imported scopes.

func (*Package) GetType

func (p *Package) GetType(name string) (*SymbolReference, error)

func (*Package) GetTypeParameter

func (p *Package) GetTypeParameter(typeRef *TypeReference) ([]*Parameter, error)

GetTypeParameter returns the parameters of a referenced type

func (*Package) GoArrayTraits

func (p *Package) GoArrayTraits(t *TypeReference) (string, error)

GoArrayTraits returns the array traits object for non-basic zserio types, such as enums, subtypes, or structures.

func (*Package) GoType

func (p *Package) GoType(t *TypeReference) (string, error)

GoType looks up a name in the scope, and provides the Go type name for it. If the type is not found ErrUnkownType is returned.

func (*Package) HasType

func (p *Package) HasType(name string) bool

HasType checks if a type is defined *in this scope*. It does not check parent scopes.

func (*Package) IsDeltaPackable added in v0.2.6

func (p *Package) IsDeltaPackable(t *TypeReference) (bool, error)

IsDeltaPackable indicates if a non-basic zserio type (struct, enum, subtype, ...) can be delta-packed.

func (*Package) OriginalType added in v0.7.0

func (p *Package) OriginalType(t *TypeReference) (*OriginalTypeReference, error)

GoType looks up a name in the scope, and provides the Go type name for it. If the type is not found ErrUnkownType is returned.

type Parameter

type Parameter struct {
	Name string
	Type *TypeReference
}

type RootScope

type RootScope struct{}

RootScope is the root zserio scope, which contains all built-in types

func (*RootScope) GoArrayTraits

func (s *RootScope) GoArrayTraits(t *TypeReference) (string, error)

GoArrayTraits returns the array traits for a zserio basic type (int, float, ...)

func (*RootScope) GoType

func (s *RootScope) GoType(t *TypeReference) (string, error)

func (*RootScope) HasType

func (s *RootScope) HasType(t string) bool

HasType checks if a type is defined *in this scope*. It does not check parent scopes.

func (*RootScope) IsDeltaPackable added in v0.2.6

func (s *RootScope) IsDeltaPackable(t *TypeReference) (bool, error)

IsDeltaPackable reutrns if a zserio basic type is delta-packabe. If the type is not a basic type, the function will return an error.

func (*RootScope) OriginalType added in v0.7.0

func (s *RootScope) OriginalType(t *TypeReference) (*OriginalTypeReference, error)

type Scope

type Scope interface {
	// GoType looks up a name in the scope, and provides the Go type name for it.
	// If the type is not found ErrUnkownType is returned.
	GoType(t *TypeReference) (string, error)

	// Returns the original zserio type, removing all indirections caused by
	// subtyping.
	OriginalType(T *TypeReference) (*OriginalTypeReference, error)

	// GoArrayTraits looks up a name in the scope, and provides the array traits
	// for it.
	GoArrayTraits(t *TypeReference) (string, error)

	// IsDeltaPackable returns if a data type can be delta-packed: when stored in an
	// array, a delta compression can be applied, which only stored the delta to
	// the previous value, not the entire value. Only works for integer types.
	IsDeltaPackable(t *TypeReference) (bool, error)

	// HasType checks if a type is defined *in this scope*. It does not check parent
	// scopes.
	HasType(t string) bool
}

type Struct

type Struct struct {
	Name               string
	Comment            string
	TemplateParameters []string
	TypeParameters     []*Parameter
	Fields             []*Field
	Functions          []*Function
}

func (*Struct) BuildScope

func (s *Struct) BuildScope(p *Package) error

func (*Struct) Evaluate

func (s *Struct) Evaluate(p *Package) error

type Subtype

type Subtype struct {
	Name    string
	Comment string
	Type    *TypeReference
}

type SymbolReference

type SymbolReference struct {
	Symbol       any
	Name         string
	Package      string
	CompoundName string
}

type SymbolScope

type SymbolScope struct {
	// TypeScope contains all types that are currently in the scope
	TypeScope map[string]any

	// OtherScope contains all symbols that are not Types (e.g. enum values)
	OtherScope map[string]any

	// CompoundScope contains all symbols that are restricted to the current
	// evaluation scope (e.g. the struct parameters when evaluating a struct)
	CompoundScopes map[string]*CompoundScope

	CurrentCompoundScope *string
}

SymbolScope keeps track of all symbols that are available in this scope

func (*SymbolScope) GetCompoundType

func (v *SymbolScope) GetCompoundType(compoundType, name string) (*SymbolReference, error)

func (*SymbolScope) GetOtherType

func (v *SymbolScope) GetOtherType(name string) (*SymbolReference, error)

func (*SymbolScope) GetType

func (v *SymbolScope) GetType(name string) (*SymbolReference, error)

type TypeReference

type TypeReference struct {
	// IsBuiltin is set to true for standard zserio types
	IsBuiltin         bool
	Package           string
	Name              string
	Bits              int
	TemplateArguments []*TypeReference

	// TypeArguments are the type arguments if the underlying type supports
	// parameters.
	TypeArguments []*Expression

	// LengthExpression is an optional expression, specifying the length of a bit
	// mask, for example int<Expression>. If the expression evaluates to 18, the
	// resulting type will be int<18>
	LengthExpression *Expression
}

func (*TypeReference) Evaluate

func (tr *TypeReference) Evaluate(p *Package) error

type Union

type Union struct {
	Name               string
	Comment            string
	TemplateParameters []string
	TypeParameters     []*Parameter
	Fields             []*Field
	Functions          []*Function
}

func (*Union) BuildScope

func (u *Union) BuildScope(p *Package) error

func (*Union) Evaluate

func (u *Union) Evaluate(p *Package) error

Jump to

Keyboard shortcuts

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