idl

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2022 License: LGPL-3.0 Imports: 7 Imported by: 2

README

IDL

This repository implements a lexer and parser for YARP's Interface Description Language (IDL). Contents of this repository are used as a library by yarpc to parse definitions and build an AST that allows the compiler to generate sources in other languages.

Documentation

Index

Constants

View Source
const (
	// RepeatedAnnotation contains a constant representing the name of
	// @repeated annotations.
	RepeatedAnnotation = "repeated"

	// OptionalAnnotation contains a constant representing the name of
	// @optional annotations.
	OptionalAnnotation = "optional"

	// DeprecatedAnnotation contains a constant representing the name of
	// @deprecated annotations. (RFU)
	DeprecatedAnnotation = "deprecated"
)

Variables

This section is empty.

Functions

func SplitComponents

func SplitComponents(n string) (pkgName, messageName string)

SplitComponents splits a given name into a package and message name. In case the provided name does not include a package, pkgName is returned as an empty string.

Types

type AnnotationCollection

type AnnotationCollection []AnnotationValue

AnnotationCollection represents a list of Annotation values.

func (AnnotationCollection) FindByName

func (a AnnotationCollection) FindByName(name string) (*AnnotationValue, bool)

FindByName takes a name and returns an Annotation value, and a boolean indicating whether the current AnnotationCollection contains the provided name.

type AnnotationValue

type AnnotationValue struct {
	Offset Offset
	Name   string
	Value  []string
}

AnnotationValue represents a single @annotation value present in a source file.

type Array

type Array struct {
	Of Type
}

func (Array) Type

func (Array) Type() TypeType

type Element

type Element int

Element represents a single Token element kind in a source file

const (
	InvalidElement Element = iota
	Identifier             // [a-z][0-9a-z_]
	OpenCurly              // {
	CloseCurly             // }
	OpenParen              // (
	CloseParen             // )
	OpenAngled             // <
	CloseAngled            // >
	Comma                  // ,
	Dot                    // .
	LineBreak              // \n
	Equal                  // =
	Number                 // 0-9+
	Arrow                  // ->
	Semi                   // ;
	Comment                // Anything from # onwards
	Annotation             // Anything from @ until next space
	StringElement          // Anything between "
	EOF
)

func (Element) String

func (i Element) String() string

type Field

type Field struct {
	Offset      Offset
	Name        string
	Comments    []string
	Annotations AnnotationCollection
	Type        Type
	Index       int
}

Field represents a Message's field

type File

type File struct {
	// Tree contains a list of Package, Import, Message, and Service objects
	// representing structures defined in a source file.
	Tree []any

	// Package represents the package name defined by the source file.
	Package string

	// DeclaredMessages contains the names of all messages declared by the
	// source file.
	DeclaredMessages []string

	// DeclaredService contains the names of all services declared by the
	// source file.
	DeclaredServices []string

	// ImportedFiles contains a list of paths provided to `import` directives.
	ImportedFiles []string
	// contains filtered or unexported fields
}

File represents a single YARP source file.

func Parse

func Parse(tokens []Token) (*File, error)

Parse takes a list of Token and returns either a File, or an error.

func (File) MessageByName

func (f File) MessageByName(name string) (*Message, bool)

MessageByName takes a name and returns a Message, along with a boolean indicating whether the provided message exists in the current File.

func (File) ServiceByName

func (f File) ServiceByName(name string) (*Service, bool)

ServiceByName takes a name and returns a Service, along with a boolean indicating whether the provided service exists in the current File.

type FileSet

type FileSet struct {
	Messages []*Message
	Services []*Service
	// contains filtered or unexported fields
}

FileSet represents structures provided by a set of source files.

func NewFileSet

func NewFileSet() *FileSet

NewFileSet creates a new FileSet structure

func (*FileSet) FindMessage

func (f *FileSet) FindMessage(name string) (*Message, bool)

FindMessage takes a message name (e.g. SomethingRequest) or FQN (e.g. package.SomethingRequest) and returns a Message along with a boolean indicating whether the provided name could be resolved to a message.

func (FileSet) FromSamePackage

func (f FileSet) FromSamePackage(name string) bool

FromSamePackage takes a name and returns whether it is declared by the package declared in the loaded source files.

func (*FileSet) Load

func (f *FileSet) Load(path string) error

Load attempts to load a given file under the provided path and add its contents to the current FileSet. In case the file cannot be loaded, an error is returned.

func (FileSet) Package

func (f FileSet) Package() string

Package returns the package declared by loaded source files.

type Import

type Import struct {
	Offset Offset
	Path   string
}

Import represents a `import` statement, which includes a path to be loaded.

type ImportFileNotFoundError

type ImportFileNotFoundError struct{ Source, Path string }

ImportFileNotFoundError indicates that a file imported by a given source could not be found.

func (ImportFileNotFoundError) Error

func (i ImportFileNotFoundError) Error() string

type Map

type Map struct {
	Key   PrimitiveType
	Value Type
}

func (Map) Type

func (Map) Type() TypeType

type Message

type Message struct {
	Offset      Offset
	Name        string
	Comments    []string
	Annotations AnnotationCollection
	Fields      []any
}

Message represents a single `message` declared in a source file.

type Method

type Method struct {
	Offset          Offset
	Name            string
	Comments        []string
	Annotations     AnnotationCollection
	ArgumentType    string
	ReturnType      string
	ReturnStreaming bool
}

Method represents a Service's method

type MixedPackagesError

type MixedPackagesError struct{ Path, Package1, Package2 string }

MixedPackagesError indicates that source files provides different packages. Only a single package can be compiled at a time.

func (MixedPackagesError) Error

func (m MixedPackagesError) Error() string

type Offset

type Offset struct {
	StartsAt Position
	EndsAt   Position
}

Offset represents the offset in which a given structure appears in the source file. It includes Position for both the point in which it starts, and the point in which it ends.

type OneOfField

type OneOfField struct {
	Offset      Offset
	Comments    []string
	Annotations AnnotationCollection
	Index       int
	Items       []any
}

OneOfField represents an oneof field present in a Message

type Package

type Package struct {
	Offset Offset
	Name   string
}

Package represents a `package` declaration in a source file.

type ParseError

type ParseError struct {
	Token   Token
	Message string
}

ParseError indicates that one or more productions from the scanner does not define a valid IDL file.

func (ParseError) Error

func (p ParseError) Error() string

type Position

type Position struct {
	Line   int
	Column int
}

Position represents a given Line/Column position within a source file.

type Primitive

type Primitive struct {
	Kind PrimitiveType
}

func (Primitive) Type

func (Primitive) Type() TypeType

type PrimitiveType

type PrimitiveType int

PrimitiveType represents a single type recognized by YARP

const (
	Invalid PrimitiveType = iota
	Uint8
	Uint16
	Uint32
	Uint64
	Int8
	Int16
	Int32
	Int64
	Float32
	Float64
	Struct
	OneOf
	Bool
	String
)

func (PrimitiveType) String

func (i PrimitiveType) String() string

type Scanner

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

Scanner implements mechanisms responsible for reading an IDL file into a list of tokens.

func NewScanner

func NewScanner(r io.Reader) (*Scanner, error)

NewScanner creates a new Scanner bound to a given io.Reader. The scanner does not close the provided reader. See also: Scan

func (*Scanner) Run

func (s *Scanner) Run() ([]Token, error)

Run executes the scan process into the provided reader. Returns either a list of Token, or an error.

type Service

type Service struct {
	Offset      Offset
	Name        string
	Comments    []string
	Annotations AnnotationCollection
	Methods     []Method
}

Service represents a single `service` declared in a source file.

type SourceFileNotFoundError

type SourceFileNotFoundError struct{ Path string }

SourceFileNotFoundError indicates that the process could not find an input file provided by the user.

func (SourceFileNotFoundError) Error

func (s SourceFileNotFoundError) Error() string

type SourceIsDirectoryError

type SourceIsDirectoryError struct{ Path string }

SourceIsDirectoryError indicates that a given source file is, in fact, a directory, and therefore cannot be read.

func (SourceIsDirectoryError) Error

func (s SourceIsDirectoryError) Error() string

type SyntaxError

type SyntaxError struct {
	Message string
	Line    int
	Column  int
}

SyntaxError indicates that a provided file does not contain a valid YARP Interface Description File.

func (SyntaxError) Error

func (s SyntaxError) Error() string

type Token

type Token struct {
	Type   Element
	Value  string
	Line   int
	Column int
}

Token represents a single token present in a source file

func Scan

func Scan(r io.Reader) ([]Token, error)

Scan takes an io.Reader and returns a list of Token from it, or an error, in case the file is invalid. This is a convenience function that creates a new Scanner, reads the provided io.Reader into it, and returns the resulting value. Scan does not close the provided io.Reader.

func (Token) String

func (t Token) String() string

type Type

type Type interface {
	Type() TypeType
}

type TypeType

type TypeType int

TypeType represents the concrete type of a Field.

const (
	TypeInvalid TypeType = iota
	TypePrimitive
	TypeArray
	TypeMap
	TypeUnresolved
)

type Unresolved

type Unresolved struct {
	Name string
}

func (Unresolved) Type

func (Unresolved) Type() TypeType

Jump to

Keyboard shortcuts

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