parse

package
v0.1.16 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2021 License: BSD-3-Clause Imports: 9 Imported by: 16

Documentation

Overview

Package parse implements the VDL parser, converting source files into a parse tree. The ParseFile function is the main entry point.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractExprPackagePaths

func ExtractExprPackagePaths(expr ConstExpr) []string

ExtractExprPackagePaths returns any package paths that appear in named constants in expr. i.e. "a/b/c".Foo => "a/b/c".

func ExtractTypePackagePaths

func ExtractTypePackagePaths(typ Type) []string

func InferPackageName

func InferPackageName(files []*File, errs *vdlutil.Errors) (pkgName string)

InferPackageName returns the package name from a group of files. Every file must specify the same package name, otherwise an error is reported in errs.

func QuoteStripDoc

func QuoteStripDoc(doc string) string

QuoteStripDoc takes a Doc string, which includes comment markers /**/ and double-slash, and returns a raw-quoted string.

TODO(toddw): This should remove comment markers. This is non-trivial, since we should handle removing leading whitespace "rectangles", and might want to retain inline /**/ or adjacent /**/ on the same line. For now we just leave them in the output.

Types

type Config

type Config struct {
	FileName  string      // Config file name, e.g. "a/b/foo.config"
	Doc       string      // Top-level config file documentation
	ConfigDef NamePos     // Name, position and docs of the "config" clause
	Imports   []*Import   // Imports listed in this file.
	Config    ConstExpr   // Const expression exported from this config.
	ConstDefs []*ConstDef // Consts defined in this file.
}

Config represents a parsed config file. Config files use a similar syntax as vdl files, with similar concepts.

func ParseConfig

func ParseConfig(fileName string, src io.Reader, opts Opts, errs *vdlutil.Errors) *Config

ParseConfig takes a file name, the contents of the config file src, and the accumulated errors, and parses the config into a parse.Config containing the parse tree. Returns nil if any errors are encountered, with errs containing more information. Otherwise returns the parsed Config.

func (*Config) AddImports

func (c *Config) AddImports(path ...string)

AddImports adds the path imports that don't already exist to c.

func (*Config) HasImport

func (c *Config) HasImport(path string) bool

HasImport returns true iff path exists in c.Imports.

type ConstBinaryOp

type ConstBinaryOp struct {
	Op    string
	Lexpr ConstExpr
	Rexpr ConstExpr
	P     Pos
}

ConstBinaryOp represents all binary operations.

func (*ConstBinaryOp) Pos

func (c *ConstBinaryOp) Pos() Pos

func (*ConstBinaryOp) String

func (c *ConstBinaryOp) String() string

type ConstCompositeLit

type ConstCompositeLit struct {
	Type   Type
	KVList []KVLit
	P      Pos
}

ConstCompositeLit represents composite literals in const expressions.

func (*ConstCompositeLit) Pos

func (c *ConstCompositeLit) Pos() Pos

func (*ConstCompositeLit) String

func (c *ConstCompositeLit) String() string

type ConstDef

type ConstDef struct {
	NamePos
	Expr ConstExpr
}

ConstDef represents a user-defined named const.

func (*ConstDef) String

func (c *ConstDef) String() string

type ConstExpr

type ConstExpr interface {
	String() string
	Pos() Pos
}

ConstExpr is the interface for all nodes in an expression.

func ParseExprs

func ParseExprs(data string, errs *vdlutil.Errors) []ConstExpr

ParseExprs parses data into a slice of parsed const expressions. The input data is specified in VDL syntax, with commas separating multiple expressions. There must be at least one expression specified in data. Errors are returned in errs.

type ConstIndexed

type ConstIndexed struct {
	Expr      *ConstNamed
	IndexExpr ConstExpr
	P         Pos
}

ConstIndexed represents an index operation on a composite type.

func (*ConstIndexed) Pos

func (c *ConstIndexed) Pos() Pos

func (*ConstIndexed) String

func (c *ConstIndexed) String() string

type ConstLit

type ConstLit struct {
	Lit interface{}
	P   Pos
}

ConstLit represents scalar literals in const expressions. The supported types for Lit are:

string     - Represents all string constants.
*big.Int   - Represents all integer constants.
*big.Rat   - Represents all rational constants.

func (*ConstLit) Pos

func (c *ConstLit) Pos() Pos

func (*ConstLit) String

func (c *ConstLit) String() string

type ConstNamed

type ConstNamed struct {
	Name string
	P    Pos
}

ConstNamed represents named references to other consts.

func (*ConstNamed) Pos

func (c *ConstNamed) Pos() Pos

func (*ConstNamed) String

func (c *ConstNamed) String() string

type ConstTypeConv

type ConstTypeConv struct {
	Type Type
	Expr ConstExpr
	P    Pos
}

ConstTypeConv represents explicit type conversions.

func (*ConstTypeConv) Pos

func (c *ConstTypeConv) Pos() Pos

func (*ConstTypeConv) String

func (c *ConstTypeConv) String() string

type ConstTypeObject

type ConstTypeObject struct {
	Type Type
	P    Pos
}

ConstTypeObject represents typeobject; a type used as a value.

func (*ConstTypeObject) Pos

func (c *ConstTypeObject) Pos() Pos

func (*ConstTypeObject) String

func (c *ConstTypeObject) String() string

type ConstUnaryOp

type ConstUnaryOp struct {
	Op   string
	Expr ConstExpr
	P    Pos
}

ConstUnaryOp represents all unary operations.

func (*ConstUnaryOp) Pos

func (c *ConstUnaryOp) Pos() Pos

func (*ConstUnaryOp) String

func (c *ConstUnaryOp) String() string

type ErrorDef

type ErrorDef struct {
	NamePos             // error name, pos and doc
	Params  []*Field    // list of positional parameters
	Actions []StringPos // list of action code identifiers
	Formats []LangFmt   // list of language / format pairs
}

ErrorDef represents an error definition.

func (*ErrorDef) String

func (x *ErrorDef) String() string

type Field

type Field struct {
	NamePos      // field name, pos and doc
	Type    Type // field type, never nil
}

Field represents fields in structs as well as method arguments.

func (*Field) String

func (x *Field) String() string

type File

type File struct {
	BaseName   string       // Base name of the vdl file, e.g. "foo.vdl"
	Doc        string       // Top-level file documentation
	PackageDef NamePos      // Name, position and docs of the "package" clause
	Imports    []*Import    // Imports listed in this file
	ErrorDefs  []*ErrorDef  // Errors defined in this file
	TypeDefs   []*TypeDef   // Types defined in this file
	ConstDefs  []*ConstDef  // Consts defined in this file
	Interfaces []*Interface // Interfaces defined in this file
}

File represents a parsed vdl file.

func ParseFile

func ParseFile(fileName string, src io.Reader, opts Opts, errs *vdlutil.Errors) *File

ParseFile takes a file name, the contents of the vdl file src, and the accumulated errors, and parses the vdl into a parse.File containing the parse tree. Returns nil if any errors are encountered, with errs containing more information. Otherwise returns the parsed File.

func (*File) String

func (x *File) String() string

type Import

type Import struct {
	NamePos        // e.g. foo (from above), or typically empty
	Path    string // e.g. "some/package/path" (from above)
}

Import represents an import definition, which is used to import other packages into an vdl file. An example of the syntax in the vdl file:

import foo "some/package/path"

func (*Import) LocalName

func (i *Import) LocalName() string

LocalName returns the name used locally within the File to refer to the imported package.

func (*Import) String

func (i *Import) String() string

type Interface

type Interface struct {
	NamePos            // interface name, pos and doc
	Embeds  []*NamePos // names of embedded interfaces
	Methods []*Method  // list of methods
}

Interface represents a set of embedded interfaces and methods.

func (*Interface) String

func (x *Interface) String() string

type KVLit

type KVLit struct {
	Key   ConstExpr
	Value ConstExpr
}

KVLit represents a key/value literal in composite literals.

type LangFmt

type LangFmt struct {
	Lang StringPos // IETF language tag
	Fmt  StringPos // i18n format string in the given language
}

LangFmt represents a language / format string pair.

func (LangFmt) Pos

func (x LangFmt) Pos() Pos

Pos returns the position of the LangFmt.

type Method

type Method struct {
	NamePos               // method name, pos and doc
	InArgs    []*Field    // list of positional in-args
	OutArgs   []*Field    // list of positional out-args
	InStream  Type        // in-stream type, may be nil
	OutStream Type        // out-stream type, may be nil
	Tags      []ConstExpr // list of method tags
}

Method represents a method in an interface.

func (*Method) String

func (x *Method) String() string

type NamePos

type NamePos struct {
	Name      string
	Pos       Pos    // position of first character in name
	Doc       string // docs that occur before the item
	DocSuffix string // docs that occur on the same line after the item
}

NamePos represents a name, its associated position and documentation.

func (*NamePos) String

func (x *NamePos) String() string

type Opts

type Opts struct {
	ImportsOnly bool // Only parse imports; skip everything else.
}

Opts specifies vdl parsing options.

type Pos

type Pos struct {
	Line int // Line number, starting at 1
	Col  int // Column number (character count), starting at 1
}

Pos captures positional information during parsing.

func (Pos) IsValid

func (p Pos) IsValid() bool

Returns true iff this Pos has been initialized. The zero Pos is invalid.

func (Pos) String

func (p Pos) String() string

type StringPos

type StringPos struct {
	String string
	Pos    Pos
}

StringPos holds a string and a Pos.

type Type

type Type interface {
	// String returns a human-readable description of the type.
	String() string
	// Kind returns a short human-readable string describing the kind of type.
	Kind() string
	// Pos returns the position of the first character in the type.
	Pos() Pos
}

Type is an interface representing symbolic occurrences of types in VDL files.

type TypeArray

type TypeArray struct {
	Len  int
	Elem Type
	P    Pos
}

TypeArray represents array types.

func (*TypeArray) Kind

func (t *TypeArray) Kind() string

func (*TypeArray) Pos

func (t *TypeArray) Pos() Pos

func (*TypeArray) String

func (t *TypeArray) String() string

type TypeDef

type TypeDef struct {
	NamePos      // name assigned by the user, pos and doc
	Type    Type // the underlying type of the type definition.
}

TypeDef represents a user-defined named type.

func (*TypeDef) String

func (t *TypeDef) String() string

type TypeEnum

type TypeEnum struct {
	Labels []NamePos
	P      Pos
}

TypeEnum represents enum types.

func (*TypeEnum) Kind

func (t *TypeEnum) Kind() string

func (*TypeEnum) Pos

func (t *TypeEnum) Pos() Pos

func (*TypeEnum) String

func (t *TypeEnum) String() string

type TypeList

type TypeList struct {
	Elem Type
	P    Pos
}

TypeList represents list types.

func (*TypeList) Kind

func (t *TypeList) Kind() string

func (*TypeList) Pos

func (t *TypeList) Pos() Pos

func (*TypeList) String

func (t *TypeList) String() string

type TypeMap

type TypeMap struct {
	Key  Type
	Elem Type
	P    Pos
}

TypeMap represents map types.

func (*TypeMap) Kind

func (t *TypeMap) Kind() string

func (*TypeMap) Pos

func (t *TypeMap) Pos() Pos

func (*TypeMap) String

func (t *TypeMap) String() string

type TypeNamed

type TypeNamed struct {
	Name string
	P    Pos
}

TypeNamed captures named references to other types. Both built-in primitives and user-defined named types use this representation.

func (*TypeNamed) Kind

func (t *TypeNamed) Kind() string

func (*TypeNamed) Pos

func (t *TypeNamed) Pos() Pos

func (*TypeNamed) String

func (t *TypeNamed) String() string

type TypeOptional

type TypeOptional struct {
	Base Type
	P    Pos
}

TypeOptional represents optional types.

func (*TypeOptional) Kind

func (t *TypeOptional) Kind() string

func (*TypeOptional) Pos

func (t *TypeOptional) Pos() Pos

func (*TypeOptional) String

func (t *TypeOptional) String() string

type TypeSet

type TypeSet struct {
	Key Type
	P   Pos
}

TypeSet represents set types.

func (*TypeSet) Kind

func (t *TypeSet) Kind() string

func (*TypeSet) Pos

func (t *TypeSet) Pos() Pos

func (*TypeSet) String

func (t *TypeSet) String() string

type TypeStruct

type TypeStruct struct {
	Fields []*Field
	P      Pos
}

TypeStruct represents struct types.

func (*TypeStruct) Kind

func (t *TypeStruct) Kind() string

func (*TypeStruct) Pos

func (t *TypeStruct) Pos() Pos

func (*TypeStruct) String

func (t *TypeStruct) String() string

type TypeUnion

type TypeUnion struct {
	Fields []*Field
	P      Pos
}

TypeUnion represents union types.

func (*TypeUnion) Kind

func (t *TypeUnion) Kind() string

func (*TypeUnion) Pos

func (t *TypeUnion) Pos() Pos

func (*TypeUnion) String

func (t *TypeUnion) String() string

Jump to

Keyboard shortcuts

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