parser

package
v2.2.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2019 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Comment

type Comment struct {
	// Raw includes a comment syntax like // and /* */.
	Raw string
	// Meta is the meta information.
	Meta meta.Meta
}

Comment is a comment in either C/C++-style // and /* ... */ syntax.

func (*Comment) Accept

func (c *Comment) Accept(v Visitor)

Accept dispatches the call to the visitor.

func (*Comment) IsCStyle

func (c *Comment) IsCStyle() bool

IsCStyle refers to /* ... */.

func (*Comment) Lines

func (c *Comment) Lines() []string

Lines formats comment text lines without prefixes //, /* or suffix */.

type ConfigOption

type ConfigOption func(*Parser)

ConfigOption is an option for Parser.

func WithBodyIncludingComments added in v1.1.0

func WithBodyIncludingComments(bodyIncludingComments bool) ConfigOption

WithBodyIncludingComments is an option to allow to include comments into each element's body. The comments are remaining of other elements'Comments and InlineComment.

func WithPermissive

func WithPermissive(permissive bool) ConfigOption

WithPermissive is an option to allow the permissive parsing rather than the just documented spec.

type EmptyStatement

type EmptyStatement struct {
	// InlineComment is the optional one placed at the ending.
	InlineComment *Comment
}

EmptyStatement represents ";".

func (*EmptyStatement) Accept

func (e *EmptyStatement) Accept(v Visitor)

Accept dispatches the call to the visitor.

func (*EmptyStatement) SetInlineComment

func (e *EmptyStatement) SetInlineComment(comment *Comment)

SetInlineComment implements the HasInlineCommentSetter interface.

type Enum

type Enum struct {
	EnumName string
	// EnumBody can have options and enum fields.
	// The element of this is the union of an option, enumField, reserved, and emptyStatement.
	EnumBody []Visitee

	// Comments are the optional ones placed at the beginning.
	Comments []*Comment
	// InlineComment is the optional one placed at the ending.
	InlineComment *Comment
	// InlineCommentBehindLeftCurly is the optional one placed behind a left curly.
	InlineCommentBehindLeftCurly *Comment
	// Meta is the meta information.
	Meta meta.Meta
}

Enum consists of a name and an enum body.

func (*Enum) Accept

func (e *Enum) Accept(v Visitor)

Accept dispatches the call to the visitor.

func (*Enum) SetInlineComment

func (e *Enum) SetInlineComment(comment *Comment)

SetInlineComment implements the HasInlineCommentSetter interface.

type EnumField

type EnumField struct {
	Ident            string
	Number           string
	EnumValueOptions []*EnumValueOption

	// Comments are the optional ones placed at the beginning.
	Comments []*Comment
	// InlineComment is the optional one placed at the ending.
	InlineComment *Comment
	// Meta is the meta information.
	Meta meta.Meta
}

EnumField is a field of enum.

func (*EnumField) Accept

func (f *EnumField) Accept(v Visitor)

Accept dispatches the call to the visitor.

func (*EnumField) SetInlineComment

func (f *EnumField) SetInlineComment(comment *Comment)

SetInlineComment implements the HasInlineCommentSetter interface.

type EnumValueOption

type EnumValueOption struct {
	OptionName string
	Constant   string
}

EnumValueOption is an option of a enumField.

type Extend

type Extend struct {
	MessageType string
	// ExtendBody can have fields and emptyStatements
	ExtendBody []Visitee

	// Comments are the optional ones placed at the beginning.
	Comments []*Comment
	// InlineComment is the optional one placed at the ending.
	InlineComment *Comment
	// InlineCommentBehindLeftCurly is the optional one placed behind a left curly.
	InlineCommentBehindLeftCurly *Comment
	// Meta is the meta information.
	Meta meta.Meta
}

Extend consists of a messageType and an extend body.

func (*Extend) Accept

func (m *Extend) Accept(v Visitor)

Accept dispatches the call to the visitor.

func (*Extend) SetInlineComment

func (m *Extend) SetInlineComment(comment *Comment)

SetInlineComment implements the HasInlineCommentSetter interface.

type Extensions

type Extensions struct {
	Ranges []*Range

	// Comments are the optional ones placed at the beginning.
	Comments []*Comment
	// InlineComment is the optional one placed at the ending.
	InlineComment *Comment
	// Meta is the meta information.
	Meta meta.Meta
}

Extensions declare that a range of field numbers in a message are available for third-party extensions.

func (*Extensions) Accept

func (e *Extensions) Accept(v Visitor)

Accept dispatches the call to the visitor.

func (*Extensions) SetInlineComment

func (e *Extensions) SetInlineComment(comment *Comment)

SetInlineComment implements the HasInlineCommentSetter interface.

type Field

type Field struct {
	IsRepeated   bool
	IsRequired   bool // proto2 only
	IsOptional   bool // proto2 only
	Type         string
	FieldName    string
	FieldNumber  string
	FieldOptions []*FieldOption

	// Comments are the optional ones placed at the beginning.
	Comments []*Comment
	// InlineComment is the optional one placed at the ending.
	InlineComment *Comment
	// Meta is the meta information.
	Meta meta.Meta
}

Field is a normal field that is the basic element of a protocol buffer message.

func (*Field) Accept

func (f *Field) Accept(v Visitor)

Accept dispatches the call to the visitor.

func (*Field) SetInlineComment

func (f *Field) SetInlineComment(comment *Comment)

SetInlineComment implements the HasInlineCommentSetter interface.

type FieldOption

type FieldOption struct {
	OptionName string
	Constant   string
}

FieldOption is an option for the field.

type GroupField

type GroupField struct {
	IsRepeated bool
	IsRequired bool
	IsOptional bool
	// GroupName must begin with capital letter.
	GroupName string
	// MessageBody can have fields, nested enum definitions, nested message definitions,
	// options, oneofs, map fields, extends, reserved, and extensions statements.
	MessageBody []Visitee
	FieldNumber string

	// Comments are the optional ones placed at the beginning.
	Comments []*Comment
	// InlineComment is the optional one placed at the ending.
	InlineComment *Comment
	// InlineCommentBehindLeftCurly is the optional one placed behind a left curly.
	InlineCommentBehindLeftCurly *Comment
	// Meta is the meta information.
	Meta meta.Meta
}

GroupField is one way to nest information in message definitions. proto2 only.

func (*GroupField) Accept

func (f *GroupField) Accept(v Visitor)

Accept dispatches the call to the visitor.

func (*GroupField) SetInlineComment

func (f *GroupField) SetInlineComment(comment *Comment)

SetInlineComment implements the HasInlineCommentSetter interface.

type HasInlineCommentSetter

type HasInlineCommentSetter interface {
	SetInlineComment(comment *Comment)
}

HasInlineCommentSetter requires to have a setter for an InlineComment field.

type Import

type Import struct {
	Modifier ImportModifier
	Location string

	// Comments are the optional ones placed at the beginning.
	Comments []*Comment
	// InlineComment is the optional one placed at the ending.
	InlineComment *Comment
	// Meta is the meta information.
	Meta meta.Meta
}

Import is used to import another .proto's definitions.

func (*Import) Accept

func (i *Import) Accept(v Visitor)

Accept dispatches the call to the visitor.

func (*Import) SetInlineComment

func (i *Import) SetInlineComment(comment *Comment)

SetInlineComment implements the HasInlineCommentSetter interface.

type ImportModifier

type ImportModifier uint

ImportModifier is a modifier enum type for import behavior.

const (
	ImportModifierNone ImportModifier = iota
	ImportModifierPublic
	ImportModifierWeak
)

Optional import modifier value to change a default behavior.

type MapField

type MapField struct {
	KeyType      string
	Type         string
	MapName      string
	FieldNumber  string
	FieldOptions []*FieldOption

	// Comments are the optional ones placed at the beginning.
	Comments []*Comment
	// InlineComment is the optional one placed at the ending.
	InlineComment *Comment
	// Meta is the meta information.
	Meta meta.Meta
}

MapField is an associative map.

func (*MapField) Accept

func (m *MapField) Accept(v Visitor)

Accept dispatches the call to the visitor.

func (*MapField) SetInlineComment

func (m *MapField) SetInlineComment(comment *Comment)

SetInlineComment implements the HasInlineCommentSetter interface.

type Message

type Message struct {
	MessageName string
	// MessageBody can have fields, nested enum definitions, nested message definitions,
	// options, oneofs, map fields, group fields(proto2 only), extends, reserved, and extensions(proto2 only) statements.
	MessageBody []Visitee

	// Comments are the optional ones placed at the beginning.
	Comments []*Comment
	// InlineComment is the optional one placed at the ending.
	InlineComment *Comment
	// InlineCommentBehindLeftCurly is the optional one placed behind a left curly.
	InlineCommentBehindLeftCurly *Comment
	// Meta is the meta information.
	Meta meta.Meta
}

Message consists of a message name and a message body.

func (*Message) Accept

func (m *Message) Accept(v Visitor)

Accept dispatches the call to the visitor.

func (*Message) SetInlineComment

func (m *Message) SetInlineComment(comment *Comment)

SetInlineComment implements the HasInlineCommentSetter interface.

type Oneof

type Oneof struct {
	OneofFields []*OneofField
	OneofName   string

	// Comments are the optional ones placed at the beginning.
	Comments []*Comment
	// InlineComment is the optional one placed at the ending.
	InlineComment *Comment
	// InlineCommentBehindLeftCurly is the optional one placed behind a left curly.
	InlineCommentBehindLeftCurly *Comment
	// Meta is the meta information.
	Meta meta.Meta
}

Oneof consists of oneof fields and a oneof name.

func (*Oneof) Accept

func (o *Oneof) Accept(v Visitor)

Accept dispatches the call to the visitor.

func (*Oneof) SetInlineComment

func (o *Oneof) SetInlineComment(comment *Comment)

SetInlineComment implements the HasInlineCommentSetter interface.

type OneofField

type OneofField struct {
	Type         string
	FieldName    string
	FieldNumber  string
	FieldOptions []*FieldOption

	// Comments are the optional ones placed at the beginning.
	Comments []*Comment
	// InlineComment is the optional one placed at the ending.
	InlineComment *Comment
	// Meta is the meta information.
	Meta meta.Meta
}

OneofField is a constituent field of oneof.

func (*OneofField) Accept

func (f *OneofField) Accept(v Visitor)

Accept dispatches the call to the visitor.

func (*OneofField) SetInlineComment

func (f *OneofField) SetInlineComment(comment *Comment)

SetInlineComment implements the HasInlineCommentSetter interface.

type Option

type Option struct {
	OptionName string
	Constant   string

	// Comments are the optional ones placed at the beginning.
	Comments []*Comment
	// InlineComment is the optional one placed at the ending.
	InlineComment *Comment
	// Meta is the meta information.
	Meta meta.Meta
}

Option can be used in proto files, messages, enums and services.

func (*Option) Accept

func (o *Option) Accept(v Visitor)

Accept dispatches the call to the visitor.

func (*Option) SetInlineComment

func (o *Option) SetInlineComment(comment *Comment)

SetInlineComment implements the HasInlineCommentSetter interface.

type Package

type Package struct {
	Name string

	// Comments are the optional ones placed at the beginning.
	Comments []*Comment
	// InlineComment is the optional one placed at the ending.
	InlineComment *Comment
	// Meta is the meta information.
	Meta meta.Meta
}

Package can be used to prevent name clashes between protocol message types.

func (*Package) Accept

func (p *Package) Accept(v Visitor)

Accept dispatches the call to the visitor.

func (*Package) SetInlineComment

func (p *Package) SetInlineComment(comment *Comment)

SetInlineComment implements the HasInlineCommentSetter interface.

type Parser

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

Parser is a parser.

func NewParser

func NewParser(lex *lexer.Lexer, opts ...ConfigOption) *Parser

NewParser creates a new Parser.

func (*Parser) IsEOF

func (p *Parser) IsEOF() bool

IsEOF checks whether the lex's read buffer is empty.

func (*Parser) MaybeScanInlineComment

func (p *Parser) MaybeScanInlineComment(
	hasSetter HasInlineCommentSetter,
)

MaybeScanInlineComment tries to scan a comment on the current line. If present then set it with setter.

func (*Parser) ParseComments

func (p *Parser) ParseComments() []*Comment

ParseComments parsers a sequence of comments.

comments = { comment }

See https://developers.google.com/protocol-buffers/docs/proto3#adding-comments

func (*Parser) ParseEnum

func (p *Parser) ParseEnum() (*Enum, error)

ParseEnum parses the enum.

enum = "enum" enumName enumBody

See https://developers.google.com/protocol-buffers/docs/reference/proto3-spec#enum_definition

func (*Parser) ParseExtend

func (p *Parser) ParseExtend() (*Extend, error)

ParseExtend parses the extend. Note that group is not supported.

extend = "extend" messageType "{" {field | group | emptyStatement} "}"

See https://developers.google.com/protocol-buffers/docs/reference/proto2-spec#extend

func (*Parser) ParseExtensions

func (p *Parser) ParseExtensions() (*Extensions, error)

ParseExtensions parses the extensions.

extensions = "extensions" ranges ";"

See https://developers.google.com/protocol-buffers/docs/reference/proto2-spec#extensions

func (*Parser) ParseField

func (p *Parser) ParseField() (*Field, error)

ParseField parses the field.

field = [ "repeated" ] type fieldName "=" fieldNumber [ "[" fieldOptions "]" ] ";"
field = [ "required" | "optional" | "repeated" ] type fieldName "=" fieldNumber [ "[" fieldOptions "]" ] ";"

See

https://developers.google.com/protocol-buffers/docs/reference/proto3-spec#normal_field
https://developers.google.com/protocol-buffers/docs/reference/proto2-spec#normal_field

func (*Parser) ParseGroupField

func (p *Parser) ParseGroupField() (*GroupField, error)

ParseGroupField parses the group.

group = label "group" groupName "=" fieldNumber messageBody

See https://developers.google.com/protocol-buffers/docs/reference/proto2-spec#group_field

func (*Parser) ParseImport

func (p *Parser) ParseImport() (*Import, error)

ParseImport parses the import.

import = "import" [ "weak" | "public" ] strLit ";"

See https://developers.google.com/protocol-buffers/docs/reference/proto3-spec#import_statement

func (*Parser) ParseMapField

func (p *Parser) ParseMapField() (*MapField, error)

ParseMapField parses the mapField.

mapField = "map" "<" keyType "," type ">" mapName "=" fieldNumber [ "[" fieldOptions "]" ] ";"

See https://developers.google.com/protocol-buffers/docs/reference/proto3-spec#map_field

func (*Parser) ParseMessage

func (p *Parser) ParseMessage() (*Message, error)

ParseMessage parses the message.

message = "message" messageName messageBody

See https://developers.google.com/protocol-buffers/docs/reference/proto3-spec#message_definition

func (*Parser) ParseOneof

func (p *Parser) ParseOneof() (*Oneof, error)

ParseOneof parses the oneof.

oneof = "oneof" oneofName "{" { oneofField | emptyStatement } "}"

See https://developers.google.com/protocol-buffers/docs/reference/proto3-spec#oneof_and_oneof_field

func (*Parser) ParseOption

func (p *Parser) ParseOption() (*Option, error)

ParseOption parses the option.

option = "option" optionName  "=" constant ";"

See https://developers.google.com/protocol-buffers/docs/reference/proto3-spec#option

func (*Parser) ParsePackage

func (p *Parser) ParsePackage() (*Package, error)

ParsePackage parses the package.

package = "package" fullIdent ";"

See https://developers.google.com/protocol-buffers/docs/reference/proto3-spec#package

func (*Parser) ParseProto

func (p *Parser) ParseProto() (*Proto, error)

ParseProto parses the proto.

proto = syntax { import | package | option | topLevelDef | emptyStatement }

See https://developers.google.com/protocol-buffers/docs/reference/proto3-spec#proto_file

func (*Parser) ParseReserved

func (p *Parser) ParseReserved() (*Reserved, error)

ParseReserved parses the reserved.

reserved = "reserved" ( ranges | fieldNames ) ";"

See https://developers.google.com/protocol-buffers/docs/reference/proto3-spec#reserved

func (*Parser) ParseService

func (p *Parser) ParseService() (*Service, error)

ParseService parses the service.

service = "service" serviceName "{" { option | rpc | emptyStatement } "}"

See https://developers.google.com/protocol-buffers/docs/reference/proto3-spec#service_definition

func (*Parser) ParseSyntax

func (p *Parser) ParseSyntax() (*Syntax, error)

ParseSyntax parses the syntax.

syntax = "syntax" "=" quote "proto3" quote ";"
syntax = "syntax" "=" quote "proto2" quote ";"

See https://developers.google.com/protocol-buffers/docs/reference/proto3-spec#syntax

type Proto

type Proto struct {
	Syntax *Syntax
	// ProtoBody is a slice of sum type consisted of *Import, *Package, *Option, *Message, *Enum, *Service, *Extend and *EmptyStatement.
	ProtoBody []Visitee
	Meta      *ProtoMeta
}

Proto represents a protocol buffer definition.

func (*Proto) Accept

func (p *Proto) Accept(v Visitor)

Accept dispatches the call to the visitor.

type ProtoMeta

type ProtoMeta struct {
	// Filename is a name of file, if any.
	Filename string
}

ProtoMeta represents a meta information about the Proto.

type RPC

type RPC struct {
	RPCName     string
	RPCRequest  *RPCRequest
	RPCResponse *RPCResponse
	Options     []*Option

	// Comments are the optional ones placed at the beginning.
	Comments []*Comment
	// InlineComment is the optional one placed at the ending.
	InlineComment *Comment
	// Meta is the meta information.
	Meta meta.Meta
}

RPC is a Remote Procedure Call.

func (*RPC) Accept

func (r *RPC) Accept(v Visitor)

Accept dispatches the call to the visitor.

func (*RPC) SetInlineComment

func (r *RPC) SetInlineComment(comment *Comment)

SetInlineComment implements the HasInlineCommentSetter interface.

type RPCRequest

type RPCRequest struct {
	IsStream    bool
	MessageType string

	// Meta is the meta information.
	Meta meta.Meta
}

RPCRequest is a request of RPC.

type RPCResponse

type RPCResponse struct {
	IsStream    bool
	MessageType string

	// Meta is the meta information.
	Meta meta.Meta
}

RPCResponse is a response of RPC.

type Range

type Range struct {
	Begin string
	End   string
}

Range is a range of field numbers. End is an optional value.

type Reserved

type Reserved struct {
	Ranges     []*Range
	FieldNames []string

	// Comments are the optional ones placed at the beginning.
	Comments []*Comment
	// InlineComment is the optional one placed at the ending.
	InlineComment *Comment
	// Meta is the meta information.
	Meta meta.Meta
}

Reserved declares a range of field numbers or field names that cannot be used in this message. These component Ranges and FieldNames are mutually exclusive.

func (*Reserved) Accept

func (r *Reserved) Accept(v Visitor)

Accept dispatches the call to the visitor.

func (*Reserved) SetInlineComment

func (r *Reserved) SetInlineComment(comment *Comment)

SetInlineComment implements the HasInlineCommentSetter interface.

type Service

type Service struct {
	ServiceName string
	// ServiceBody can have options and rpcs.
	ServiceBody []Visitee

	// Comments are the optional ones placed at the beginning.
	Comments []*Comment
	// InlineComment is the optional one placed at the ending.
	InlineComment *Comment
	// InlineCommentBehindLeftCurly is the optional one placed behind a left curly.
	InlineCommentBehindLeftCurly *Comment
	// Meta is the meta information.
	Meta meta.Meta
}

Service consists of RPCs.

func (*Service) Accept

func (s *Service) Accept(v Visitor)

Accept dispatches the call to the visitor.

func (*Service) SetInlineComment

func (s *Service) SetInlineComment(comment *Comment)

SetInlineComment implements the HasInlineCommentSetter interface.

type Syntax

type Syntax struct {
	ProtobufVersion string

	// Comments are the optional ones placed at the beginning.
	Comments []*Comment
	// InlineComment is the optional one placed at the ending.
	InlineComment *Comment
	// Meta is the meta information.
	Meta meta.Meta
}

Syntax is used to define the protobuf version.

func (*Syntax) Accept

func (s *Syntax) Accept(v Visitor)

Accept dispatches the call to the visitor.

func (*Syntax) SetInlineComment

func (s *Syntax) SetInlineComment(comment *Comment)

SetInlineComment implements the HasInlineCommentSetter interface.

func (*Syntax) Version

func (s *Syntax) Version() int

Version returns the version number.

type Visitee

type Visitee interface {
	Accept(v Visitor)
}

Visitee is implemented by all Protocol Buffer elements.

type Visitor

type Visitor interface {
	VisitComment(*Comment)
	VisitEmptyStatement(*EmptyStatement) (next bool)
	VisitEnum(*Enum) (next bool)
	VisitEnumField(*EnumField) (next bool)
	VisitExtend(*Extend) (next bool)
	VisitExtensions(*Extensions) (next bool)
	VisitField(*Field) (next bool)
	VisitGroupField(*GroupField) (next bool)
	VisitImport(*Import) (next bool)
	VisitMapField(*MapField) (next bool)
	VisitMessage(*Message) (next bool)
	VisitOneof(*Oneof) (next bool)
	VisitOneofField(*OneofField) (next bool)
	VisitOption(*Option) (next bool)
	VisitPackage(*Package) (next bool)
	VisitReserved(*Reserved) (next bool)
	VisitRPC(*RPC) (next bool)
	VisitService(*Service) (next bool)
	VisitSyntax(*Syntax) (next bool)
}

Visitor is for dispatching Protocol Buffer elements.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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