parser

package
v3.13.1+incompatible Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2021 License: Apache-2.0 Imports: 14 Imported by: 39

Documentation

Index

Constants

View Source
const (
	// VendorAnnotation is used on namespace definitions to indicate to any
	// consumers of the IDL where the generated code is vendored so that
	// consumers can generate code that points to it. This cannot be used with
	// "*" namespaces since it is language-dependent. Consumers then use the
	// "vendor" annotation on includes they wish to vendor. The value provided
	// on the include-side "vendor" annotation, if any, is ignored.
	//
	// When an include is annotated with "vendor", Frugal will skip generating
	// the include if -use-vendor is set since this flag indicates intention to
	// use the vendored code as advertised by the "vendor" annotation.
	//
	// If no location is specified by the "vendor" annotation, the behavior is
	// defined by the language generator.
	VendorAnnotation = "vendor"

	// DeprecatedAnnotation is the annotation to mark a service method as deprecated.
	DeprecatedAnnotation = "deprecated"
)

Supported generator annotations.

View Source
const (
	StructTypeStruct = iota
	StructTypeException
	StructTypeUnion
)

Valid StructTypes.

Variables

This section is empty.

Functions

func LowercaseFirstLetter

func LowercaseFirstLetter(s string) string

LowercaseFirstLetter of the string.

func Parse

func Parse(filename string, b []byte, opts ...Option) (interface{}, error)

Parse parses the data from b using filename as information in the error messages.

func ParseFile

func ParseFile(filename string, opts ...Option) (interface{}, error)

ParseFile parses the file identified by filename.

func ParseReader

func ParseReader(filename string, r io.Reader, opts ...Option) (interface{}, error)

ParseReader parses the data from r using filename as information in the error messages.

Types

type Annotation

type Annotation struct {
	Name  string
	Value string
}

Annotation is key-value metadata attached to an IDL definition.

type Annotations

type Annotations []*Annotation

Annotations is the collection of Annotations present on an IDL definition.

func (Annotations) Deprecated

func (a Annotations) Deprecated() (string, bool)

Deprecated returns true if the "deprecated" annotation is present and its associated value, if any.

func (Annotations) DeprecationValue

func (a Annotations) DeprecationValue() string

DeprecationValue returns the value of the "deprecated" annotation.

func (Annotations) Get

func (a Annotations) Get(name string) (string, bool)

Get returns true if the given annotation name is present and its associated value, if any.

func (Annotations) IsDeprecated

func (a Annotations) IsDeprecated() bool

IsDeprecated returns true if the "deprecated" annotation is present.

func (Annotations) Vendor

func (a Annotations) Vendor() (string, bool)

Vendor returns true if the "vendor" annotation is present and its associated value, if any.

type Auditor

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

Auditor provides an interface for auditing one frugal file against another for breaking API changes.

func NewAuditor

func NewAuditor() *Auditor

NewAuditor constructs an auditor that logs warnings and errors to standard output

func NewAuditorWithLogger

func NewAuditorWithLogger(logger ValidationLogger) *Auditor

NewAuditorWithLogger constructs an auditor which uses the given logger to log warnings and errors

func (*Auditor) Audit

func (a *Auditor) Audit(oldFile, newFile string) error

Compare checks the contents of newFile for breaking changes with respect to oldFile

type Constant

type Constant struct {
	Comment     []string
	Name        string
	Type        *Type
	Value       interface{}
	Annotations Annotations
}

Constant represents an IDL constant.

type Enum

type Enum struct {
	Comment     []string
	Name        string
	Values      []*EnumValue
	Annotations Annotations
}

Enum represents an IDL enum.

type EnumValue

type EnumValue struct {
	Comment     []string
	Name        string
	Value       int
	Annotations Annotations
}

EnumValue represents an IDL enum value.

type Field

type Field struct {
	Comment     []string
	ID          int
	Name        string
	Modifier    FieldModifier
	Type        *Type
	Default     interface{}
	Annotations Annotations
}

Field represents an IDL field on a struct or method.

func FieldFromType

func FieldFromType(t *Type, name string) *Field

FieldFromType returns a new Field from the given Type and name.

type FieldModifier

type FieldModifier int

FieldModifier represents a Frugal IDL field modifier (required, optional default).

const (
	// Required indicates always written for the writer and must read or error
	// for the reader.
	Required FieldModifier = iota

	// Optional indicates written if set for the writer and read if present for
	// the reader.
	Optional

	// Default indicates always written for the writer and read if present for
	// the reader.
	Default
)

func (*FieldModifier) String

func (f *FieldModifier) String() string

type Frugal

type Frugal struct {
	Name           string
	File           string
	Dir            string
	Path           string
	ParsedIncludes map[string]*Frugal

	Includes   []*Include
	Typedefs   []*TypeDef
	Namespaces []*Namespace
	Constants  []*Constant
	Enums      []*Enum
	Structs    []*Struct
	Exceptions []*Struct
	Unions     []*Struct
	Services   []*Service
	Scopes     []*Scope
	// contains filtered or unexported fields
}

Frugal contains the complete IDL parse tree.

func ParseFrugal

func ParseFrugal(filePath string) (*Frugal, error)

ParseFrugal parses the given Frugal file into its semantic representation.

func (*Frugal) ConstantFromField

func (f *Frugal) ConstantFromField(field *Field, value interface{}) *Constant

ConstantFromField returns a new Constant from the given Field and value.

func (*Frugal) ContainsFrugalDefinitions

func (f *Frugal) ContainsFrugalDefinitions() bool

ContainsFrugalDefinitions indicates if the parse tree contains any scope or service definitions.

func (*Frugal) ContextFromIdentifier

func (f *Frugal) ContextFromIdentifier(identifier Identifier) *IdentifierContext

ContextFromIdentifier returns a IdentifierContext for the given Identifier.

func (*Frugal) DataStructures

func (f *Frugal) DataStructures() []*Struct

DataStructures returns a slice containing all structs, exceptions, and unions.

func (*Frugal) FindStruct

func (f *Frugal) FindStruct(typ *Type) *Struct

func (*Frugal) Include

func (f *Frugal) Include(name string) *Include

Include returns the Include with the given name.

func (*Frugal) IsEnum

func (f *Frugal) IsEnum(t *Type) bool

IsEnum indicates if the underlying Type is an enum.

func (*Frugal) IsStruct

func (f *Frugal) IsStruct(t *Type) bool

IsStruct indicates if the underlying Type is a struct.

func (*Frugal) IsUnion

func (f *Frugal) IsUnion(t *Type) bool

IsUnion indicates if the underlying types is a union.

func (*Frugal) Namespace

func (f *Frugal) Namespace(scope string) *Namespace

Namespace returns namespace value for the given scope.

func (*Frugal) NamespaceForInclude

func (f *Frugal) NamespaceForInclude(include, lang string) *Namespace

NamespaceForInclude returns the Namespace for the given include name and language.

func (*Frugal) OrderedIncludes

func (f *Frugal) OrderedIncludes() []Include

OrderedIncludes returns the Includes in order, sorted by the include name.

func (*Frugal) ReferencedIncludes

func (f *Frugal) ReferencedIncludes() ([]*Include, error)

ReferencedIncludes returns a slice containing the referenced includes which will need to be imported in generated code.

func (*Frugal) ReferencedInternals

func (f *Frugal) ReferencedInternals() []string

ReferencedInternals returns a slice containing the referenced internals which will need to be imported in generated code.

func (*Frugal) ReferencedScopeIncludes

func (f *Frugal) ReferencedScopeIncludes() ([]*Include, error)

ReferencedScopeIncludes returns a slice containing the referenced includes which will need to be imported in generated code for scopes.

func (*Frugal) ReferencedServiceIncludes

func (f *Frugal) ReferencedServiceIncludes() ([]*Include, error)

ReferencedServiceIncludes returns a slice containing the referenced includes which will need to be imported in generated code for services.

func (*Frugal) UnderlyingType

func (f *Frugal) UnderlyingType(t *Type) *Type

UnderlyingType follows any typedefs to get the base IDL type.

type Identifier

type Identifier string

Identifier represents an IDL identifier.

type IdentifierContext

type IdentifierContext struct {
	Type      IdentifierType
	Constant  *Constant
	Enum      *Enum
	EnumValue *EnumValue
	Include   *Frugal
}

IdentifierContext gives information about the identifier for a given contextual reference

type IdentifierType

type IdentifierType uint8

IdentifierType indicates if a Identifier is a local/include constant/enum

const (
	NonIdentifier IdentifierType = iota
	LocalConstant
	LocalEnum
	IncludeConstant
	IncludeEnum
)

type Include

type Include struct {
	Name        string
	Value       string
	Annotations Annotations
}

Include represents an IDL file include.

type KeyValue

type KeyValue struct {
	Key, Value interface{}
}

KeyValue is a key-value pair.

func (*KeyValue) KeyToString

func (k *KeyValue) KeyToString() string

type Method

type Method struct {
	Comment     []string
	Name        string
	Oneway      bool
	ReturnType  *Type
	Arguments   []*Field
	Exceptions  []*Field
	Annotations Annotations
}

Method represents an IDL service method.

type Namespace

type Namespace struct {
	Scope       string
	Value       string
	Annotations Annotations
}

Namespace represents an IDL namespace.

func (*Namespace) Wildcard

func (n *Namespace) Wildcard() bool

Wildcard indicates if this Namespace is a wildcard (*).

type Operation

type Operation struct {
	Comment     []string
	Name        string
	Type        *Type
	Annotations Annotations
	Scope       *Scope // Pointer back to containing Scope
}

Operation is a pub/sub scope operation. Corresponding publish and subscribe methods are generated from this for publishers and subscribers, respectively.

type Option

type Option func(*parser) Option

Option is a function that can set an option on the parser. It returns the previous setting as an Option.

func Debug

func Debug(b bool) Option

Debug creates an Option to set the debug flag to b. When set to true, debugging information is printed to stdout while parsing.

The default is false.

func Memoize

func Memoize(b bool) Option

Memoize creates an Option to set the memoize flag to b. When set to true, the parser will cache all results so each expression is evaluated only once. This guarantees linear parsing time even for pathological cases, at the expense of more memory and slower times for typical cases.

The default is false.

func Recover

func Recover(b bool) Option

Recover creates an Option to set the recover flag to b. When set to true, this causes the parser to recover from panics and convert it to an error. Setting it to false can be useful while debugging to access the full stack trace.

The default is true.

type Scope

type Scope struct {
	Comment     []string
	Name        string
	Prefix      *ScopePrefix
	Operations  []*Operation
	Annotations Annotations
	Frugal      *Frugal // Pointer back to containing Frugal
}

Scope is a pub/sub namespace.

func (*Scope) ReferencedIncludes

func (s *Scope) ReferencedIncludes() ([]*Include, error)

ReferencedIncludes returns a slice containing the referenced includes which will need to be imported in generated code for this Scope.

type ScopePrefix

type ScopePrefix struct {
	String    string
	Variables []string
}

ScopePrefix is the string prefix prepended to a pub/sub topic. The string can contain variables of the form {foo}, e.g. "foo.{bar}.baz" where "bar" is supplied at publish/subscribe time.

func (*ScopePrefix) Template

func (n *ScopePrefix) Template(s string) string

Template returns the prefix where variables are replaced with the given string.

type Service

type Service struct {
	Comment     []string
	Name        string
	Extends     string
	Methods     []*Method
	Annotations Annotations
	Frugal      *Frugal // Pointer back to containing Frugal
}

Service represents an IDL service.

func (*Service) ExtendsInclude

func (s *Service) ExtendsInclude() string

ExtendsInclude returns the name of the include this service extends from, if applicable, or an empty string if not.

func (*Service) ExtendsService

func (s *Service) ExtendsService() string

ExtendsService returns the name of the service this service extends, if applicable, or an empty string if not.

func (*Service) ReferencedIncludes

func (s *Service) ReferencedIncludes() ([]*Include, error)

ReferencedIncludes returns a slice containing the referenced includes which will need to be imported in generated code for this Service.

func (*Service) ReferencedInternals

func (s *Service) ReferencedInternals() []string

ReferencedInternals returns a slice containing the referenced internals which will need to be imported in generated code for this Service. TODO: Clean this mess up

func (*Service) TwowayMethods

func (s *Service) TwowayMethods() []*Method

TwowayMethods returns a slice of the non-oneway methods defined in this Service.

type Struct

type Struct struct {
	Comment     []string
	Name        string
	Fields      []*Field
	Type        StructType
	Annotations Annotations
}

Struct represents an IDL struct (or exception or union).

type StructType

type StructType int

StructType represents what "type" a struct is (struct, exception, or union).

func (StructType) String

func (s StructType) String() string

String returns a human-readable version of the StructType.

type Type

type Type struct {
	Name        string
	KeyType     *Type // If map
	ValueType   *Type // If map, list, or set
	Annotations Annotations
}

Type represents an IDL data type.

func TypeFromStruct

func TypeFromStruct(s *Struct) *Type

TypeFromStruct returns a new Type from the given Struct.

func (*Type) IncludeName

func (t *Type) IncludeName() string

IncludeName returns the base include name of the type, if any.

func (*Type) IsContainer

func (t *Type) IsContainer() bool

IsContainer indicates if the type is a Frugal container type (list, set, or map).

func (*Type) IsCustom

func (t *Type) IsCustom() bool

IsCustom indicates if the type is not a container or primitive type.

func (*Type) IsPrimitive

func (t *Type) IsPrimitive() bool

IsPrimitive indicates if the type is a Frugal primitive type.

func (*Type) ParamName

func (t *Type) ParamName() string

ParamName returns the base type name with any include prefix removed.

func (*Type) String

func (t *Type) String() string

String returns a human-readable version of the Type.

type TypeDef

type TypeDef struct {
	Comment     []string
	Name        string
	Type        *Type
	Annotations Annotations
}

TypeDef represents an IDL typedef.

type ValidationLogger

type ValidationLogger interface {
	// LogWarning should log a warning message
	LogWarning(...string)
	// LogError should log an error message
	LogError(...string)
	// ErrorsLogged should return true if any errors have been logged,
	// and false otherwise
	ErrorsLogged() bool
}

ValidationLogger provides an interface to output validation results.

Jump to

Keyboard shortcuts

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