ast

package
v0.0.0-...-4f6f732 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2022 License: Apache-2.0 Imports: 4 Imported by: 1

Documentation

Overview

Package ast defines types for modeling the AST (Abstract Syntax Tree) for the protocol buffers source language.

All nodes of the tree implement the Node interface. Leaf nodes in the tree implement TerminalNode and all others implement CompositeNode. The root of the tree for a proto source file is a *FileNode.

Position information is tracked using a *FileInfo, callings its various Add* methods as the file is tokenized by the lexer. This allows AST nodes to have a compact representation. To extract detailed position information, you must use the nodeInfo method, available on either the *FileInfo which produced the node's tokens or the *FileNode root of the tree that contains the node.

Comments are not represented as nodes in the tree. Instead, they are attributed to terminal nodes in the tree. So, when lexing, comments are accumulated until the next non-comment token is found. The AST model in this package thus provides access to all comments in the file, regardless of location (unlike the SourceCodeInfo present in descriptor protos, which is lossy). The comments associated with a a non-leaf/non-token node (i.e. a CompositeNode) come from the first and last nodes in its sub-tree, for leading and trailing comments respectively.

Creation of AST nodes should use the factory functions in this package instead of struct literals. Some factory functions accept optional arguments, which means the arguments can be nil. If nil values are provided for other (non-optional) arguments, the resulting node may be invalid and cause panics later in the program.

This package defines numerous interfaces. However, user code should not attempt to implement any of them. Most consumers of an AST will not work correctly if they encounter concrete implementations other than the ones defined in this package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AsInt32

func AsInt32(n IntValueNode, min, max int32) (int32, bool)

AsInt32 range checks the given int value and returns its value is in the range or 0, false if it is outside the range.

func Visit

func Visit(n Node, v Visitor) error

Visit implements the double-dispatch idiom and visits the given node by calling the appropriate method of the given visitor.

func VisitChildren

func VisitChildren(n CompositeNode, v Visitor) error

VisitChildren visits all direct children of the given node using the given visitor. If visiting a child returns an error, that error is immediately returned, and other children will not be visited.

func Walk

func Walk(root Node, v Visitor, opts ...WalkOption) error

Walk conducts a walk of the AST rooted at the given root using the given visitor. It performs a "pre-order traversal", visiting a given AST node before it visits that node's descendants.

If a visitor returns an error while walking the tree, the entire operation is aborted and that error is returned.

Types

type AncestorTracker

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

AncestorTracker is used to track the path of nodes during a walk operation. By passing AsWalkOptions to a call to Walk, a visitor can inspect the path to the node being visited using this tracker.

func (*AncestorTracker) AsWalkOptions

func (t *AncestorTracker) AsWalkOptions() []WalkOption

AsWalkOptions returns WalkOption values that will cause this ancestor tracker to track the path through the AST during the walk operation.

func (*AncestorTracker) Parent

func (t *AncestorTracker) Parent() Node

Parent returns the parent node of the currently visited node. If the node currently being visited is the root supplied to Walk then nil is returned.

func (*AncestorTracker) Path

func (t *AncestorTracker) Path() []Node

Path returns a slice of nodes that represents the path from the root of the walk operaiton to the currently visited node. The first element in the path is the root supplied to Walk. The last element in the path is the currently visited node.

The returned slice is not a defensive copy; so callers should NOT mutate it.

type ArrayLiteralNode

type ArrayLiteralNode struct {
	OpenBracket *RuneNode
	Elements    []ValueNode
	// Commas represent the separating ',' characters between elements. The
	// length of this slice must be exactly len(Elements)-1, with each item
	// in Elements having a corresponding item in this slice *except the last*
	// (since a trailing comma is not allowed).
	Commas       []*RuneNode
	CloseBracket *RuneNode
	// contains filtered or unexported fields
}

ArrayLiteralNode represents an array literal, which is only allowed inside of a MessageLiteralNode, to indicate values for a repeated field. Example:

["foo", "bar", "baz"]

func NewArrayLiteralNode

func NewArrayLiteralNode(openBracket *RuneNode, vals []ValueNode, commas []*RuneNode, closeBracket *RuneNode) *ArrayLiteralNode

NewArrayLiteralNode creates a new *ArrayLiteralNode. The openBracket and closeBracket args must be non-nil and represent the "[" and "]" runes that surround the array values. The given commas arg must have a length that is one less than the length of the vals arg. However, vals may be empty, in which case commas must also be empty.

func (*ArrayLiteralNode) Children

func (n *ArrayLiteralNode) Children() []Node

func (*ArrayLiteralNode) End

func (n *ArrayLiteralNode) End() Token

func (*ArrayLiteralNode) Start

func (n *ArrayLiteralNode) Start() Token

func (*ArrayLiteralNode) Value

func (n *ArrayLiteralNode) Value() interface{}

type BoolLiteralNode

type BoolLiteralNode struct {
	*KeywordNode
	Val bool
}

BoolLiteralNode represents a boolean literal.

func NewBoolLiteralNode

func NewBoolLiteralNode(name *KeywordNode) *BoolLiteralNode

NewBoolLiteralNode returns a new *BoolLiteralNode for the given keyword, which must be "true" or "false".

func (*BoolLiteralNode) Value

func (n *BoolLiteralNode) Value() interface{}

type Comment

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

Comment represents a single comment in a source file. It indicates the position of the comment and its contents.

func (Comment) End

func (c Comment) End() SourcePos

func (Comment) LeadingWhitespace

func (c Comment) LeadingWhitespace() string

func (Comment) RawText

func (c Comment) RawText() string

func (Comment) Start

func (c Comment) Start() SourcePos

type Comments

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

Comments represents a range of sequential comments in a source file (e.g. no interleaving tokens or AST nodes).

func (Comments) Index

func (c Comments) Index(i int) Comment

func (Comments) Len

func (c Comments) Len() int

type CompactOptionsNode

type CompactOptionsNode struct {
	OpenBracket *RuneNode
	Options     []*OptionNode
	// Commas represent the separating ',' characters between options. The
	// length of this slice must be exactly len(Options)-1, with each item
	// in Options having a corresponding item in this slice *except the last*
	// (since a trailing comma is not allowed).
	Commas       []*RuneNode
	CloseBracket *RuneNode
	// contains filtered or unexported fields
}

CompactOptionsNode represents a compact options declaration, as used with fields, enum values, and extension ranges. Example:

[deprecated = true, json_name = "foo_bar"]

func NewCompactOptionsNode

func NewCompactOptionsNode(openBracket *RuneNode, opts []*OptionNode, commas []*RuneNode, closeBracket *RuneNode) *CompactOptionsNode

NewCompactOptionsNode creates a *CompactOptionsNode. All args must be non-nil. The commas arg must have a length that is one less than the length of opts. The opts arg must not be empty.

func (*CompactOptionsNode) Children

func (n *CompactOptionsNode) Children() []Node

func (*CompactOptionsNode) End

func (n *CompactOptionsNode) End() Token

func (*CompactOptionsNode) GetElements

func (e *CompactOptionsNode) GetElements() []*OptionNode

func (*CompactOptionsNode) Start

func (n *CompactOptionsNode) Start() Token

type CompositeNode

type CompositeNode interface {
	Node
	// All AST nodes that are immediate children of this one.
	Children() []Node
}

CompositeNode represents any non-terminal node in the tree. These are interior or root nodes and have child nodes.

type CompoundIdentNode

type CompoundIdentNode struct {

	// Optional leading dot, indicating that the identifier is fully qualified.
	LeadingDot *RuneNode
	Components []*IdentNode
	// Dots[0] is the dot after Components[0]. The length of Dots is always
	// one less than the length of Components.
	Dots []*RuneNode
	// The text value of the identifier, with all components and dots
	// concatenated.
	Val string
	// contains filtered or unexported fields
}

CompoundIdentNode represents a qualified identifier. A qualified identifier has at least one dot and possibly multiple identifier names (all separated by dots). If the identifier has a leading dot, then it is a *fully* qualified identifier. Example:

.com.foobar.Baz

func NewCompoundIdentNode

func NewCompoundIdentNode(leadingDot *RuneNode, components []*IdentNode, dots []*RuneNode) *CompoundIdentNode

NewCompoundIdentNode creates a *CompoundIdentNode. The leadingDot may be nil. The dots arg must have a length that is one less than the length of components. The components arg must not be empty.

func (*CompoundIdentNode) AsIdentifier

func (n *CompoundIdentNode) AsIdentifier() Identifier

func (*CompoundIdentNode) Children

func (n *CompoundIdentNode) Children() []Node

func (*CompoundIdentNode) End

func (n *CompoundIdentNode) End() Token

func (*CompoundIdentNode) Start

func (n *CompoundIdentNode) Start() Token

func (*CompoundIdentNode) Value

func (n *CompoundIdentNode) Value() interface{}

type CompoundStringLiteralNode

type CompoundStringLiteralNode struct {
	Val string
	// contains filtered or unexported fields
}

CompoundStringLiteralNode represents a compound string literal, which is the concatenaton of adjacent string literals. Example:

"this "  "is"   " all one "   "string"

func NewCompoundLiteralStringNode

func NewCompoundLiteralStringNode(components ...*StringLiteralNode) *CompoundStringLiteralNode

NewCompoundLiteralStringNode creates a new *CompoundStringLiteralNode that consists of the given string components. The components argument may not be empty.

func (*CompoundStringLiteralNode) AsString

func (n *CompoundStringLiteralNode) AsString() string

func (*CompoundStringLiteralNode) Children

func (n *CompoundStringLiteralNode) Children() []Node

func (*CompoundStringLiteralNode) End

func (n *CompoundStringLiteralNode) End() Token

func (*CompoundStringLiteralNode) Start

func (n *CompoundStringLiteralNode) Start() Token

func (*CompoundStringLiteralNode) Value

func (n *CompoundStringLiteralNode) Value() interface{}

type EmptyDeclNode

type EmptyDeclNode struct {
	Semicolon *RuneNode
	// contains filtered or unexported fields
}

EmptyDeclNode represents an empty declaration in protobuf source. These amount to extra semicolons, with no actual content preceding the semicolon.

func NewEmptyDeclNode

func NewEmptyDeclNode(semicolon *RuneNode) *EmptyDeclNode

NewEmptyDeclNode creates a new *EmptyDeclNode. The one argument must be non-nil.

func (*EmptyDeclNode) Children

func (n *EmptyDeclNode) Children() []Node

func (*EmptyDeclNode) End

func (n *EmptyDeclNode) End() Token

func (*EmptyDeclNode) Start

func (n *EmptyDeclNode) Start() Token

type EnumElement

type EnumElement interface {
	Node
	// contains filtered or unexported methods
}

EnumElement is an interface implemented by all AST nodes that can appear in the body of an enum declaration.

type EnumNode

type EnumNode struct {
	Keyword    *KeywordNode
	Name       *IdentNode
	OpenBrace  *RuneNode
	Decls      []EnumElement
	CloseBrace *RuneNode
	// contains filtered or unexported fields
}

EnumNode represents an enum declaration. Example:

enum Foo { BAR = 0; BAZ = 1 }

func NewEnumNode

func NewEnumNode(keyword *KeywordNode, name *IdentNode, openBrace *RuneNode, decls []EnumElement, closeBrace *RuneNode) *EnumNode

NewEnumNode creates a new *EnumNode. All arguments must be non-nil. While it is technically allowed for decls to be nil or empty, the resulting node will not be a valid enum, which must have at least one value.

  • keyword: The token corresponding to the "enum" keyword.
  • name: The token corresponding to the enum's name.
  • openBrace: The token corresponding to the "{" rune that starts the body.
  • decls: All declarations inside the enum body.
  • closeBrace: The token corresponding to the "}" rune that ends the body.

func (*EnumNode) Children

func (n *EnumNode) Children() []Node

func (*EnumNode) End

func (n *EnumNode) End() Token

func (*EnumNode) Start

func (n *EnumNode) Start() Token

type EnumValueDeclNode

type EnumValueDeclNode interface {
	Node
	GetName() Node
	GetNumber() Node
}

EnumValueDeclNode is a placeholder interface for AST nodes that represent enum values. This allows NoSourceNode to be used in place of *EnumValueNode for some usages.

type EnumValueNode

type EnumValueNode struct {
	Name      *IdentNode
	Equals    *RuneNode
	Number    IntValueNode
	Options   *CompactOptionsNode
	Semicolon *RuneNode
	// contains filtered or unexported fields
}

EnumNode represents an enum declaration. Example:

UNSET = 0 [deprecated = true];

func NewEnumValueNode

func NewEnumValueNode(name *IdentNode, equals *RuneNode, number IntValueNode, opts *CompactOptionsNode, semicolon *RuneNode) *EnumValueNode

NewEnumValueNode creates a new *EnumValueNode. All arguments must be non-nil except opts which is only non-nil if the declaration included options.

  • name: The token corresponding to the enum value's name.
  • equals: The token corresponding to the '=' rune after the name.
  • number: The token corresponding to the enum value's number.
  • opts: Optional set of enum value options.
  • semicolon: The token corresponding to the ";" rune that ends the declaration.

func (*EnumValueNode) Children

func (n *EnumValueNode) Children() []Node

func (*EnumValueNode) End

func (n *EnumValueNode) End() Token

func (*EnumValueNode) GetName

func (e *EnumValueNode) GetName() Node

func (*EnumValueNode) GetNumber

func (e *EnumValueNode) GetNumber() Node

func (*EnumValueNode) Start

func (n *EnumValueNode) Start() Token

type ExtendElement

type ExtendElement interface {
	Node
	// contains filtered or unexported methods
}

ExtendElement is an interface implemented by all AST nodes that can appear in the body of an extends declaration.

type ExtendNode

type ExtendNode struct {
	Keyword    *KeywordNode
	Extendee   IdentValueNode
	OpenBrace  *RuneNode
	Decls      []ExtendElement
	CloseBrace *RuneNode
	// contains filtered or unexported fields
}

ExtendNode represents a declaration of extension fields. Example:

extend google.protobuf.FieldOptions {
  bool redacted = 33333;
}

func NewExtendNode

func NewExtendNode(keyword *KeywordNode, extendee IdentValueNode, openBrace *RuneNode, decls []ExtendElement, closeBrace *RuneNode) *ExtendNode

NewExtendNode creates a new *ExtendNode. All arguments must be non-nil.

  • keyword: The token corresponding to the "extend" keyword.
  • extendee: The token corresponding to the name of the extended message.
  • openBrace: The token corresponding to the "{" rune that starts the body.
  • decls: All declarations inside the message body.
  • closeBrace: The token corresponding to the "}" rune that ends the body.

func (*ExtendNode) Children

func (n *ExtendNode) Children() []Node

func (*ExtendNode) End

func (n *ExtendNode) End() Token

func (*ExtendNode) Start

func (n *ExtendNode) Start() Token

type ExtensionRangeNode

type ExtensionRangeNode struct {
	Keyword *KeywordNode
	Ranges  []*RangeNode
	// Commas represent the separating ',' characters between ranges. The
	// length of this slice must be exactly len(Ranges)-1, each item in Ranges
	// having a corresponding item in this slice *except the last* (since a
	// trailing comma is not allowed).
	Commas    []*RuneNode
	Options   *CompactOptionsNode
	Semicolon *RuneNode
	// contains filtered or unexported fields
}

ExtensionRangeNode represents an extension range declaration in an extendable message. Example:

extensions 100 to max;

func NewExtensionRangeNode

func NewExtensionRangeNode(keyword *KeywordNode, ranges []*RangeNode, commas []*RuneNode, opts *CompactOptionsNode, semicolon *RuneNode) *ExtensionRangeNode

NewExtensionRangeNode creates a new *ExtensionRangeNode. All args must be non-nil except opts, which may be nil.

  • keyword: The token corresponding to the "extends" keyword.
  • ranges: One or more range expressions.
  • commas: Tokens that represent the "," runes that delimit the range expressions. The length of commas must be one less than the length of ranges.
  • opts: The node corresponding to options that apply to each of the ranges.
  • semicolon The token corresponding to the ";" rune that ends the declaration.

func (*ExtensionRangeNode) Children

func (n *ExtensionRangeNode) Children() []Node

func (*ExtensionRangeNode) End

func (n *ExtensionRangeNode) End() Token

func (*ExtensionRangeNode) Start

func (n *ExtensionRangeNode) Start() Token

type FieldDeclNode

type FieldDeclNode interface {
	Node
	FieldLabel() Node
	FieldName() Node
	FieldType() Node
	FieldTag() Node
	FieldExtendee() Node
	GetGroupKeyword() Node
	GetOptions() *CompactOptionsNode
}

FieldDeclNode is a node in the AST that defines a field. This includes normal message fields as well as extensions. There are multiple types of AST nodes that declare fields:

  • *FieldNode
  • *GroupNode
  • *MapFieldNode

This also allows NoSourceNode and SyntheticMapField to be used in place of one of the above for some usages.

type FieldLabel

type FieldLabel struct {
	*KeywordNode
	Repeated bool
	Required bool
}

FieldLabel represents the label of a field, which indicates its cardinality (i.e. whether it is optional, required, or repeated).

func (*FieldLabel) IsPresent

func (f *FieldLabel) IsPresent() bool

IsPresent returns true if a label keyword was present in the declaration and false if it was absent.

type FieldNode

type FieldNode struct {
	Label     FieldLabel
	FldType   IdentValueNode
	Name      *IdentNode
	Equals    *RuneNode
	Tag       *UintLiteralNode
	Options   *CompactOptionsNode
	Semicolon *RuneNode

	// This is an up-link to the containing *ExtendNode for fields
	// that are defined inside of "extend" blocks.
	Extendee *ExtendNode
	// contains filtered or unexported fields
}

FieldNode represents a normal field declaration (not groups or maps). It can represent extension fields as well as non-extension fields (both inside of messages and inside of one-ofs). Example:

optional string foo = 1;

func NewFieldNode

func NewFieldNode(label *KeywordNode, fieldType IdentValueNode, name *IdentNode, equals *RuneNode, tag *UintLiteralNode, opts *CompactOptionsNode, semicolon *RuneNode) *FieldNode

NewFieldNode creates a new *FieldNode. The label and options arguments may be nil but the others must be non-nil.

  • label: The token corresponding to the label keyword if present ("optional", "required", or "repeated").
  • fieldType: The token corresponding to the field's type.
  • name: The token corresponding to the field's name.
  • equals: The token corresponding to the '=' rune after the name.
  • tag: The token corresponding to the field's tag number.
  • opts: Optional set of field options.
  • semicolon: The token corresponding to the ";" rune that ends the declaration.

func (*FieldNode) Children

func (n *FieldNode) Children() []Node

func (*FieldNode) End

func (n *FieldNode) End() Token

func (*FieldNode) FieldExtendee

func (n *FieldNode) FieldExtendee() Node

func (*FieldNode) FieldLabel

func (n *FieldNode) FieldLabel() Node

func (*FieldNode) FieldName

func (n *FieldNode) FieldName() Node

func (*FieldNode) FieldTag

func (n *FieldNode) FieldTag() Node

func (*FieldNode) FieldType

func (n *FieldNode) FieldType() Node

func (*FieldNode) GetGroupKeyword

func (n *FieldNode) GetGroupKeyword() Node

func (*FieldNode) GetOptions

func (n *FieldNode) GetOptions() *CompactOptionsNode

func (*FieldNode) Start

func (n *FieldNode) Start() Token

type FieldReferenceNode

type FieldReferenceNode struct {
	Open  *RuneNode // only present for extension names
	Name  IdentValueNode
	Close *RuneNode // only present for extension names
	// contains filtered or unexported fields
}

FieldReferenceNode is a reference to a field name. It can indicate a regular field (simple unqualified name) or an extension field (possibly-qualified name that is enclosed either in brackets or parentheses).

This is used in options to indicate the names of custom options (which are actually extensions), in which case the name is enclosed in parentheses "(" and ")". It is also used in message literals to set extension fields, in which case the name is enclosed in square brackets "[" and "]".

Example:

(foo.bar)

func NewExtensionFieldReferenceNode

func NewExtensionFieldReferenceNode(openSym *RuneNode, name IdentValueNode, closeSym *RuneNode) *FieldReferenceNode

NewExtensionFieldReferenceNode creates a new *FieldReferenceNode for an extension field. All args must be non-nil. The openSym and closeSym runes should be "(" and ")" or "[" and "]".

func NewFieldReferenceNode

func NewFieldReferenceNode(name *IdentNode) *FieldReferenceNode

NewFieldReferenceNode creates a new *FieldReferenceNode for a regular field. The name arg must not be nil.

func (*FieldReferenceNode) Children

func (n *FieldReferenceNode) Children() []Node

func (*FieldReferenceNode) End

func (n *FieldReferenceNode) End() Token

func (*FieldReferenceNode) IsExtension

func (a *FieldReferenceNode) IsExtension() bool

IsExtension reports if this is an extension name or not (e.g. enclosed in punctuation, such as parentheses or brackets).

func (*FieldReferenceNode) Start

func (n *FieldReferenceNode) Start() Token

func (*FieldReferenceNode) Value

func (a *FieldReferenceNode) Value() string

type FileDeclNode

type FileDeclNode interface {
	Node
	Name() string
	GetSyntax() Node
	NodeInfo(n Node) NodeInfo
}

FileDeclNode is a placeholder interface for AST nodes that represent files. This allows NoSourceNode to be used in place of *FileNode for some usages.

type FileElement

type FileElement interface {
	Node
	// contains filtered or unexported methods
}

FileElement is an interface implemented by all AST nodes that are allowed as top-level declarations in the file.

type FileInfo

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

FileInfo contains information about the contents of a source file, including details about comments and tokens. A lexer accumulates these details as it scans the file contents. This allows efficient representation of things like source positions.

func NewFileInfo

func NewFileInfo(filename string, contents []byte) *FileInfo

NewFileInfo creates a new instance for the given file.

func (*FileInfo) AddComment

func (f *FileInfo) AddComment(comment, attributedTo Token) Comment

AddComment adds info about a comment to this file. Comments must first be added as tokens via f.AddToken(). The given comment argument is the TokenInfo from that step. The given attributedTo argument indicates another token in the file with which the comment is associated. If comment's offset is before that of attributedTo, then this is a leading comment. Otherwise, it is a trailing comment.

func (*FileInfo) AddLine

func (f *FileInfo) AddLine(offset int)

AddLine adds the offset representing the beginning of the "next" line in the file. The first line always starts at offset 0, the second line starts at offset-of-newline-char+1.

func (*FileInfo) AddToken

func (f *FileInfo) AddToken(offset, length int) Token

AddToken adds info about a token at the given location to this file. It returns a value that allows access to all of the token's details.

func (*FileInfo) Name

func (f *FileInfo) Name() string

func (*FileInfo) NodeInfo

func (f *FileInfo) NodeInfo(n Node) NodeInfo

func (*FileInfo) SourcePos

func (f *FileInfo) SourcePos(offset int) SourcePos

func (*FileInfo) TokenInfo

func (f *FileInfo) TokenInfo(t Token) NodeInfo

type FileNode

type FileNode struct {
	Syntax *SyntaxNode // nil if file has no syntax declaration
	Decls  []FileElement

	// This synthetic node allows access to final comments and whitespace
	EOF *RuneNode
	// contains filtered or unexported fields
}

FileNode is the root of the AST hierarchy. It represents an entire protobuf source file.

func NewEmptyFileNode

func NewEmptyFileNode(filename string) *FileNode

func NewFileNode

func NewFileNode(info *FileInfo, syntax *SyntaxNode, decls []FileElement, eof Token) *FileNode

NewFileElement creates a new *FileNode. The syntax parameter is optional. If it is absent, it means the file had no syntax declaration.

This function panics if the concrete type of any element of decls is not from this package.

func (*FileNode) Children

func (n *FileNode) Children() []Node

func (*FileNode) End

func (n *FileNode) End() Token

func (*FileNode) GetSyntax

func (f *FileNode) GetSyntax() Node

func (*FileNode) Name

func (f *FileNode) Name() string

func (*FileNode) NodeInfo

func (f *FileNode) NodeInfo(n Node) NodeInfo

func (*FileNode) Start

func (n *FileNode) Start() Token

func (*FileNode) TokenInfo

func (f *FileNode) TokenInfo(t Token) NodeInfo

type FloatLiteralNode

type FloatLiteralNode struct {

	// Val is the numeric value indicated by the literal
	Val float64
	// contains filtered or unexported fields
}

FloatLiteralNode represents a floating point numeric literal.

func NewFloatLiteralNode

func NewFloatLiteralNode(val float64, tok Token) *FloatLiteralNode

NewFloatLiteralNode creates a new *FloatLiteralNode with the given val.

func (*FloatLiteralNode) AsFloat

func (n *FloatLiteralNode) AsFloat() float64

func (FloatLiteralNode) End

func (n FloatLiteralNode) End() Token

func (FloatLiteralNode) Start

func (n FloatLiteralNode) Start() Token

func (FloatLiteralNode) Token

func (n FloatLiteralNode) Token() Token

func (*FloatLiteralNode) Value

func (n *FloatLiteralNode) Value() interface{}

type FloatValueNode

type FloatValueNode interface {
	ValueNode
	AsFloat() float64
}

FloatValueNode is an AST node that represents a numeric literal with a floating point, in scientific notation, or too large to fit in an int64 or uint64.

type GroupNode

type GroupNode struct {
	Label   FieldLabel
	Keyword *KeywordNode
	Name    *IdentNode
	Equals  *RuneNode
	Tag     *UintLiteralNode
	Options *CompactOptionsNode
	MessageBody

	// This is an up-link to the containing *ExtendNode for groups
	// that are defined inside of "extend" blocks.
	Extendee *ExtendNode
	// contains filtered or unexported fields
}

GroupNode represents a group declaration, which doubles as a field and inline message declaration. It can represent extension fields as well as non-extension fields (both inside of messages and inside of one-ofs). Example:

optional group Key = 4 {
  optional uint64 id = 1;
  optional string name = 2;
}

func NewGroupNode

func NewGroupNode(label *KeywordNode, keyword *KeywordNode, name *IdentNode, equals *RuneNode, tag *UintLiteralNode, opts *CompactOptionsNode, openBrace *RuneNode, decls []MessageElement, closeBrace *RuneNode) *GroupNode

NewGroupNode creates a new *GroupNode. The label and options arguments may be nil but the others must be non-nil.

  • label: The token corresponding to the label keyword if present ("optional", "required", or "repeated").
  • keyword: The token corresponding to the "group" keyword.
  • name: The token corresponding to the field's name.
  • equals: The token corresponding to the '=' rune after the name.
  • tag: The token corresponding to the field's tag number.
  • opts: Optional set of field options.
  • openBrace: The token corresponding to the "{" rune that starts the body.
  • decls: All declarations inside the group body.
  • closeBrace: The token corresponding to the "}" rune that ends the body.

func (*GroupNode) Children

func (n *GroupNode) Children() []Node

func (*GroupNode) End

func (n *GroupNode) End() Token

func (*GroupNode) FieldExtendee

func (n *GroupNode) FieldExtendee() Node

func (*GroupNode) FieldLabel

func (n *GroupNode) FieldLabel() Node

func (*GroupNode) FieldName

func (n *GroupNode) FieldName() Node

func (*GroupNode) FieldTag

func (n *GroupNode) FieldTag() Node

func (*GroupNode) FieldType

func (n *GroupNode) FieldType() Node

func (*GroupNode) GetGroupKeyword

func (n *GroupNode) GetGroupKeyword() Node

func (*GroupNode) GetOptions

func (n *GroupNode) GetOptions() *CompactOptionsNode

func (*GroupNode) MessageName

func (n *GroupNode) MessageName() Node

func (*GroupNode) Start

func (n *GroupNode) Start() Token

type IdentNode

type IdentNode struct {
	Val string
	// contains filtered or unexported fields
}

IdentNode represents a simple, unqualified identifier. These are used to name elements declared in a protobuf file or to refer to elements. Example:

foobar

func NewIdentNode

func NewIdentNode(val string, tok Token) *IdentNode

NewIdentNode creates a new *IdentNode. The given val is the identifier text.

func (*IdentNode) AsIdentifier

func (n *IdentNode) AsIdentifier() Identifier

func (IdentNode) End

func (n IdentNode) End() Token

func (IdentNode) Start

func (n IdentNode) Start() Token

func (*IdentNode) ToKeyword

func (n *IdentNode) ToKeyword() *KeywordNode

ToKeyword is used to convert identifiers to keywords. Since keywords are not reserved in the protobuf language, they are initially lexed as identifiers and then converted to keywords based on context.

func (IdentNode) Token

func (n IdentNode) Token() Token

func (*IdentNode) Value

func (n *IdentNode) Value() interface{}

type IdentValueNode

type IdentValueNode interface {
	ValueNode
	AsIdentifier() Identifier
}

IdentValueNode is an AST node that represents an identifier.

type Identifier

type Identifier string

Identifier is a possibly-qualified name. This is used to distinguish ValueNode values that are references/identifiers vs. those that are string literals.

type ImportNode

type ImportNode struct {
	Keyword *KeywordNode
	// Optional; if present indicates this is a public import
	Public *KeywordNode
	// Optional; if present indicates this is a weak import
	Weak      *KeywordNode
	Name      StringValueNode
	Semicolon *RuneNode
	// contains filtered or unexported fields
}

ImportNode represents an import statement. Example:

import "google/protobuf/empty.proto";

func NewImportNode

func NewImportNode(keyword *KeywordNode, public *KeywordNode, weak *KeywordNode, name StringValueNode, semicolon *RuneNode) *ImportNode

NewImportNode creates a new *ImportNode. The public and weak arguments are optional and only one or the other (or neither) may be specified, not both. When public is non-nil, it indicates the "public" keyword in the import statement and means this is a public import. When weak is non-nil, it indicates the "weak" keyword in the import statement and means this is a weak import. When both are nil, this is a normal import. The other arguments must be non-nil:

  • keyword: The token corresponding to the "import" keyword.
  • public: The token corresponding to the optional "public" keyword.
  • weak: The token corresponding to the optional "weak" keyword.
  • name: The actual imported file name.
  • semicolon: The token corresponding to the ";" rune that ends the declaration.

func (*ImportNode) Children

func (n *ImportNode) Children() []Node

func (*ImportNode) End

func (n *ImportNode) End() Token

func (*ImportNode) Start

func (n *ImportNode) Start() Token

type IntValueNode

type IntValueNode interface {
	ValueNode
	AsInt64() (int64, bool)
	AsUint64() (uint64, bool)
}

IntValueNode is an AST node that represents an integer literal. If an integer literal is too large for an int64 (or uint64 for positive literals), it is represented instead by a FloatValueNode.

type KeywordNode

type KeywordNode IdentNode

KeywordNode is an AST node that represents a keyword. Keywords are like identifiers, but they have special meaning in particular contexts. Example:

message

func NewKeywordNode

func NewKeywordNode(val string, tok Token) *KeywordNode

NewKeywordNode creates a new *KeywordNode. The given val is the keyword.

type MapFieldNode

type MapFieldNode struct {
	MapType   *MapTypeNode
	Name      *IdentNode
	Equals    *RuneNode
	Tag       *UintLiteralNode
	Options   *CompactOptionsNode
	Semicolon *RuneNode
	// contains filtered or unexported fields
}

MapFieldNode represents a map field declaration. Example:

map<string,string> replacements = 3 [deprecated = true];

func NewMapFieldNode

func NewMapFieldNode(mapType *MapTypeNode, name *IdentNode, equals *RuneNode, tag *UintLiteralNode, opts *CompactOptionsNode, semicolon *RuneNode) *MapFieldNode

NewMapFieldNode creates a new *MapFieldNode. All arguments must be non-nil except opts, which may be nil.

  • mapType: The token corresponding to the map type.
  • name: The token corresponding to the field's name.
  • equals: The token corresponding to the '=' rune after the name.
  • tag: The token corresponding to the field's tag number.
  • opts: Optional set of field options.
  • semicolon: The token corresponding to the ";" rune that ends the declaration.

func (*MapFieldNode) Children

func (n *MapFieldNode) Children() []Node

func (*MapFieldNode) End

func (n *MapFieldNode) End() Token

func (*MapFieldNode) FieldExtendee

func (n *MapFieldNode) FieldExtendee() Node

func (*MapFieldNode) FieldLabel

func (n *MapFieldNode) FieldLabel() Node

func (*MapFieldNode) FieldName

func (n *MapFieldNode) FieldName() Node

func (*MapFieldNode) FieldTag

func (n *MapFieldNode) FieldTag() Node

func (*MapFieldNode) FieldType

func (n *MapFieldNode) FieldType() Node

func (*MapFieldNode) GetGroupKeyword

func (n *MapFieldNode) GetGroupKeyword() Node

func (*MapFieldNode) GetOptions

func (n *MapFieldNode) GetOptions() *CompactOptionsNode

func (*MapFieldNode) KeyField

func (n *MapFieldNode) KeyField() *SyntheticMapField

func (*MapFieldNode) MessageName

func (n *MapFieldNode) MessageName() Node

func (*MapFieldNode) Start

func (n *MapFieldNode) Start() Token

func (*MapFieldNode) ValueField

func (n *MapFieldNode) ValueField() *SyntheticMapField

type MapTypeNode

type MapTypeNode struct {
	Keyword    *KeywordNode
	OpenAngle  *RuneNode
	KeyType    *IdentNode
	Comma      *RuneNode
	ValueType  IdentValueNode
	CloseAngle *RuneNode
	// contains filtered or unexported fields
}

MapTypeNode represents the type declaration for a map field. It defines both the key and value types for the map. Example:

map<string, Values>

func NewMapTypeNode

func NewMapTypeNode(keyword *KeywordNode, openAngle *RuneNode, keyType *IdentNode, comma *RuneNode, valType IdentValueNode, closeAngle *RuneNode) *MapTypeNode

NewMapTypeNode creates a new *MapTypeNode. All arguments must be non-nil.

  • keyword: The token corresponding to the "map" keyword.
  • openAngle: The token corresponding to the "<" rune after the keyword.
  • keyType: The token corresponding to the key type for the map.
  • comma: The token corresponding to the "," rune between key and value types.
  • valType: The token corresponding to the value type for the map.
  • closeAngle: The token corresponding to the ">" rune that ends the declaration.

func (*MapTypeNode) Children

func (n *MapTypeNode) Children() []Node

func (*MapTypeNode) End

func (n *MapTypeNode) End() Token

func (*MapTypeNode) Start

func (n *MapTypeNode) Start() Token

type MessageBody

type MessageBody struct {
	OpenBrace  *RuneNode
	Decls      []MessageElement
	CloseBrace *RuneNode
}

MessageBody represents the body of a message. It is used by both MessageNodes and GroupNodes.

type MessageDeclNode

type MessageDeclNode interface {
	Node
	MessageName() Node
}

MessageDeclNode is a node in the AST that defines a message type. This includes normal message fields as well as implicit messages:

  • *MessageNode
  • *GroupNode (the group is a field and inline message type)
  • *MapFieldNode (map fields implicitly define a MapEntry message type)

This also allows NoSourceNode to be used in place of one of the above for some usages.

type MessageElement

type MessageElement interface {
	Node
	// contains filtered or unexported methods
}

MessageElement is an interface implemented by all AST nodes that can appear in a message body.

type MessageFieldNode

type MessageFieldNode struct {
	Name *FieldReferenceNode
	// Sep represents the ':' separator between the name and value. If
	// the value is a message literal (and thus starts with '<' or '{'),
	// then the separator is optional, and thus may be nil.
	Sep *RuneNode
	Val ValueNode
	// contains filtered or unexported fields
}

MessageFieldNode represents a single field (name and value) inside of a message literal. Example:

foo:"bar"

func NewMessageFieldNode

func NewMessageFieldNode(name *FieldReferenceNode, sep *RuneNode, val ValueNode) *MessageFieldNode

NewMessageFieldNode creates a new *MessageFieldNode. All args except sep must be non-nil.

func (*MessageFieldNode) Children

func (n *MessageFieldNode) Children() []Node

func (*MessageFieldNode) End

func (n *MessageFieldNode) End() Token

func (*MessageFieldNode) Start

func (n *MessageFieldNode) Start() Token

type MessageLiteralNode

type MessageLiteralNode struct {
	Open     *RuneNode // should be '{' or '<'
	Elements []*MessageFieldNode
	// Separator characters between elements, which can be either ','
	// or ';' if present. This slice must be exactly len(Elements) in
	// length, with each item in Elements having one corresponding item
	// in Seps. Separators in message literals are optional, so a given
	// item in this slice may be nil to indicate absence of a separator.
	Seps  []*RuneNode
	Close *RuneNode // should be '}' or '>', depending on Open
	// contains filtered or unexported fields
}

MessageLiteralNode represents a message literal, which is compatible with the protobuf text format and can be used for custom options with message types. Example:

{ foo:1 foo:2 foo:3 bar:<name:"abc" id:123> }

func NewMessageLiteralNode

func NewMessageLiteralNode(openSym *RuneNode, vals []*MessageFieldNode, seps []*RuneNode, closeSym *RuneNode) *MessageLiteralNode

NewMessageLiteralNode creates a new *MessageLiteralNode. The openSym and closeSym runes must not be nil and should be "{" and "}" or "<" and ">".

Unlike separators (dots and commas) used for other AST nodes that represent a list of elements, the seps arg must be the SAME length as vals, and it may contain nil values to indicate absence of a separator (in fact, it could be all nils).

func (*MessageLiteralNode) Children

func (n *MessageLiteralNode) Children() []Node

func (*MessageLiteralNode) End

func (n *MessageLiteralNode) End() Token

func (*MessageLiteralNode) Start

func (n *MessageLiteralNode) Start() Token

func (*MessageLiteralNode) Value

func (n *MessageLiteralNode) Value() interface{}

type MessageNode

type MessageNode struct {
	Keyword *KeywordNode
	Name    *IdentNode
	MessageBody
	// contains filtered or unexported fields
}

MessageNode represents a message declaration. Example:

message Foo {
  string name = 1;
  repeated string labels = 2;
  bytes extra = 3;
}

func NewMessageNode

func NewMessageNode(keyword *KeywordNode, name *IdentNode, openBrace *RuneNode, decls []MessageElement, closeBrace *RuneNode) *MessageNode

NewMessageNode creates a new *MessageNode. All arguments must be non-nil.

  • keyword: The token corresponding to the "message" keyword.
  • name: The token corresponding to the field's name.
  • openBrace: The token corresponding to the "{" rune that starts the body.
  • decls: All declarations inside the message body.
  • closeBrace: The token corresponding to the "}" rune that ends the body.

func (*MessageNode) Children

func (n *MessageNode) Children() []Node

func (*MessageNode) End

func (n *MessageNode) End() Token

func (*MessageNode) MessageName

func (n *MessageNode) MessageName() Node

func (*MessageNode) Start

func (n *MessageNode) Start() Token

type NegativeIntLiteralNode

type NegativeIntLiteralNode struct {
	Minus *RuneNode
	Uint  *UintLiteralNode
	Val   int64
	// contains filtered or unexported fields
}

NegativeIntLiteralNode represents an integer literal with a negative (-) sign.

func NewNegativeIntLiteralNode

func NewNegativeIntLiteralNode(sign *RuneNode, i *UintLiteralNode) *NegativeIntLiteralNode

NewNegativeIntLiteralNode creates a new *NegativeIntLiteralNode. Both arguments must be non-nil.

func (*NegativeIntLiteralNode) AsInt64

func (n *NegativeIntLiteralNode) AsInt64() (int64, bool)

func (*NegativeIntLiteralNode) AsUint64

func (n *NegativeIntLiteralNode) AsUint64() (uint64, bool)

func (*NegativeIntLiteralNode) Children

func (n *NegativeIntLiteralNode) Children() []Node

func (*NegativeIntLiteralNode) End

func (n *NegativeIntLiteralNode) End() Token

func (*NegativeIntLiteralNode) Start

func (n *NegativeIntLiteralNode) Start() Token

func (*NegativeIntLiteralNode) Value

func (n *NegativeIntLiteralNode) Value() interface{}

type NoOpVisitor

type NoOpVisitor struct{}

NoOpVisitor is a visitor implementation that does nothing. All methods unconditionally return nil. This can be embedded into a struct to make that struct implement the Visitor interface, and only the relevant visit methods then need to be implemented on the struct.

func (NoOpVisitor) VisitArrayLiteralNode

func (n NoOpVisitor) VisitArrayLiteralNode(_ *ArrayLiteralNode) error

func (NoOpVisitor) VisitBoolLiteralNode

func (n NoOpVisitor) VisitBoolLiteralNode(_ *BoolLiteralNode) error

func (NoOpVisitor) VisitCompactOptionsNode

func (n NoOpVisitor) VisitCompactOptionsNode(_ *CompactOptionsNode) error

func (NoOpVisitor) VisitCompoundIdentNode

func (n NoOpVisitor) VisitCompoundIdentNode(_ *CompoundIdentNode) error

func (NoOpVisitor) VisitCompoundStringLiteralNode

func (n NoOpVisitor) VisitCompoundStringLiteralNode(_ *CompoundStringLiteralNode) error

func (NoOpVisitor) VisitEmptyDeclNode

func (n NoOpVisitor) VisitEmptyDeclNode(_ *EmptyDeclNode) error

func (NoOpVisitor) VisitEnumNode

func (n NoOpVisitor) VisitEnumNode(_ *EnumNode) error

func (NoOpVisitor) VisitEnumValueNode

func (n NoOpVisitor) VisitEnumValueNode(_ *EnumValueNode) error

func (NoOpVisitor) VisitExtendNode

func (n NoOpVisitor) VisitExtendNode(_ *ExtendNode) error

func (NoOpVisitor) VisitExtensionRangeNode

func (n NoOpVisitor) VisitExtensionRangeNode(_ *ExtensionRangeNode) error

func (NoOpVisitor) VisitFieldNode

func (n NoOpVisitor) VisitFieldNode(_ *FieldNode) error

func (NoOpVisitor) VisitFieldReferenceNode

func (n NoOpVisitor) VisitFieldReferenceNode(_ *FieldReferenceNode) error

func (NoOpVisitor) VisitFileNode

func (n NoOpVisitor) VisitFileNode(_ *FileNode) error

func (NoOpVisitor) VisitFloatLiteralNode

func (n NoOpVisitor) VisitFloatLiteralNode(_ *FloatLiteralNode) error

func (NoOpVisitor) VisitGroupNode

func (n NoOpVisitor) VisitGroupNode(_ *GroupNode) error

func (NoOpVisitor) VisitIdentNode

func (n NoOpVisitor) VisitIdentNode(_ *IdentNode) error

func (NoOpVisitor) VisitImportNode

func (n NoOpVisitor) VisitImportNode(_ *ImportNode) error

func (NoOpVisitor) VisitKeywordNode

func (n NoOpVisitor) VisitKeywordNode(_ *KeywordNode) error

func (NoOpVisitor) VisitMapFieldNode

func (n NoOpVisitor) VisitMapFieldNode(_ *MapFieldNode) error

func (NoOpVisitor) VisitMapTypeNode

func (n NoOpVisitor) VisitMapTypeNode(_ *MapTypeNode) error

func (NoOpVisitor) VisitMessageFieldNode

func (n NoOpVisitor) VisitMessageFieldNode(_ *MessageFieldNode) error

func (NoOpVisitor) VisitMessageLiteralNode

func (n NoOpVisitor) VisitMessageLiteralNode(_ *MessageLiteralNode) error

func (NoOpVisitor) VisitMessageNode

func (n NoOpVisitor) VisitMessageNode(_ *MessageNode) error

func (NoOpVisitor) VisitNegativeIntLiteralNode

func (n NoOpVisitor) VisitNegativeIntLiteralNode(_ *NegativeIntLiteralNode) error

func (NoOpVisitor) VisitOneOfNode

func (n NoOpVisitor) VisitOneOfNode(_ *OneOfNode) error

func (NoOpVisitor) VisitOptionNameNode

func (n NoOpVisitor) VisitOptionNameNode(_ *OptionNameNode) error

func (NoOpVisitor) VisitOptionNode

func (n NoOpVisitor) VisitOptionNode(_ *OptionNode) error

func (NoOpVisitor) VisitPackageNode

func (n NoOpVisitor) VisitPackageNode(_ *PackageNode) error

func (NoOpVisitor) VisitPositiveUintLiteralNode

func (n NoOpVisitor) VisitPositiveUintLiteralNode(_ *PositiveUintLiteralNode) error

func (NoOpVisitor) VisitRPCNode

func (n NoOpVisitor) VisitRPCNode(_ *RPCNode) error

func (NoOpVisitor) VisitRPCTypeNode

func (n NoOpVisitor) VisitRPCTypeNode(_ *RPCTypeNode) error

func (NoOpVisitor) VisitRangeNode

func (n NoOpVisitor) VisitRangeNode(_ *RangeNode) error

func (NoOpVisitor) VisitReservedNode

func (n NoOpVisitor) VisitReservedNode(_ *ReservedNode) error

func (NoOpVisitor) VisitRuneNode

func (n NoOpVisitor) VisitRuneNode(_ *RuneNode) error

func (NoOpVisitor) VisitServiceNode

func (n NoOpVisitor) VisitServiceNode(_ *ServiceNode) error

func (NoOpVisitor) VisitSignedFloatLiteralNode

func (n NoOpVisitor) VisitSignedFloatLiteralNode(_ *SignedFloatLiteralNode) error

func (NoOpVisitor) VisitSpecialFloatLiteralNode

func (n NoOpVisitor) VisitSpecialFloatLiteralNode(_ *SpecialFloatLiteralNode) error

func (NoOpVisitor) VisitStringLiteralNode

func (n NoOpVisitor) VisitStringLiteralNode(_ *StringLiteralNode) error

func (NoOpVisitor) VisitSyntaxNode

func (n NoOpVisitor) VisitSyntaxNode(_ *SyntaxNode) error

func (NoOpVisitor) VisitUintLiteralNode

func (n NoOpVisitor) VisitUintLiteralNode(_ *UintLiteralNode) error

type NoSourceNode

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

NoSourceNode is a placeholder AST node that implements numerous interfaces in this package. It can be used to represent an AST element for a file whose source is not available.

func NewNoSourceNode

func NewNoSourceNode(filename string) NoSourceNode

NewNoSourceNode creates a new NoSourceNode for the given filename.

func (NoSourceNode) End

func (n NoSourceNode) End() Token

func (NoSourceNode) FieldExtendee

func (n NoSourceNode) FieldExtendee() Node

func (NoSourceNode) FieldLabel

func (n NoSourceNode) FieldLabel() Node

func (NoSourceNode) FieldName

func (n NoSourceNode) FieldName() Node

func (NoSourceNode) FieldTag

func (n NoSourceNode) FieldTag() Node

func (NoSourceNode) FieldType

func (n NoSourceNode) FieldType() Node

func (NoSourceNode) GetGroupKeyword

func (n NoSourceNode) GetGroupKeyword() Node

func (NoSourceNode) GetInputType

func (n NoSourceNode) GetInputType() Node

func (NoSourceNode) GetName

func (n NoSourceNode) GetName() Node

func (NoSourceNode) GetNumber

func (n NoSourceNode) GetNumber() Node

func (NoSourceNode) GetOptions

func (n NoSourceNode) GetOptions() *CompactOptionsNode

func (NoSourceNode) GetOutputType

func (n NoSourceNode) GetOutputType() Node

func (NoSourceNode) GetSyntax

func (n NoSourceNode) GetSyntax() Node

func (NoSourceNode) GetValue

func (n NoSourceNode) GetValue() ValueNode

func (NoSourceNode) MessageName

func (n NoSourceNode) MessageName() Node

func (NoSourceNode) Name

func (n NoSourceNode) Name() string

func (NoSourceNode) NodeInfo

func (n NoSourceNode) NodeInfo(Node) NodeInfo

func (NoSourceNode) RangeEnd

func (n NoSourceNode) RangeEnd() Node

func (NoSourceNode) RangeStart

func (n NoSourceNode) RangeStart() Node

func (NoSourceNode) Start

func (n NoSourceNode) Start() Token

func (NoSourceNode) Value

func (n NoSourceNode) Value() interface{}

type Node

type Node interface {
	Start() Token
	End() Token
}

Node is the interface implemented by all nodes in the AST. It provides information about the span of this AST node in terms of location in the source file. It also provides information about all prior comments (attached as leading comments) and optional subsequent comments (attached as trailing comments).

type NodeInfo

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

NodeInfo represents the details for a node in the source file's AST.

func (NodeInfo) End

func (n NodeInfo) End() SourcePos

func (NodeInfo) LeadingComments

func (n NodeInfo) LeadingComments() Comments

func (NodeInfo) LeadingWhitespace

func (n NodeInfo) LeadingWhitespace() string

func (NodeInfo) RawText

func (n NodeInfo) RawText() string

func (NodeInfo) Start

func (n NodeInfo) Start() SourcePos

func (NodeInfo) TrailingComments

func (n NodeInfo) TrailingComments() Comments

type OneOfElement

type OneOfElement interface {
	Node
	// contains filtered or unexported methods
}

OneOfElement is an interface implemented by all AST nodes that can appear in the body of a oneof declaration.

type OneOfNode

type OneOfNode struct {
	Keyword    *KeywordNode
	Name       *IdentNode
	OpenBrace  *RuneNode
	Decls      []OneOfElement
	CloseBrace *RuneNode
	// contains filtered or unexported fields
}

OneOfNode represents a one-of declaration. Example:

oneof query {
  string by_name = 2;
  Type by_type = 3;
  Address by_address = 4;
  Labels by_label = 5;
}

func NewOneOfNode

func NewOneOfNode(keyword *KeywordNode, name *IdentNode, openBrace *RuneNode, decls []OneOfElement, closeBrace *RuneNode) *OneOfNode

NewOneOfNode creates a new *OneOfNode. All arguments must be non-nil. While it is technically allowed for decls to be nil or empty, the resulting node will not be a valid oneof, which must have at least one field.

  • keyword: The token corresponding to the "oneof" keyword.
  • name: The token corresponding to the oneof's name.
  • openBrace: The token corresponding to the "{" rune that starts the body.
  • decls: All declarations inside the oneof body.
  • closeBrace: The token corresponding to the "}" rune that ends the body.

func (*OneOfNode) Children

func (n *OneOfNode) Children() []Node

func (*OneOfNode) End

func (n *OneOfNode) End() Token

func (*OneOfNode) Start

func (n *OneOfNode) Start() Token

type OptionDeclNode

type OptionDeclNode interface {
	Node
	GetName() Node
	GetValue() ValueNode
}

OptionDeclNode is a placeholder interface for AST nodes that represent options. This allows NoSourceNode to be used in place of *OptionNode for some usages.

type OptionNameNode

type OptionNameNode struct {
	Parts []*FieldReferenceNode
	// Dots represent the separating '.' characters between name parts. The
	// length of this slice must be exactly len(Parts)-1, each item in Parts
	// having a corresponding item in this slice *except the last* (since a
	// trailing dot is not allowed).
	//
	// These do *not* include dots that are inside of an extension name. For
	// example: (foo.bar).baz.(bob) has three parts:
	//    1. (foo.bar)  - an extension name
	//    2. baz        - a regular field in foo.bar
	//    3. (bob)      - an extension field in baz
	// Note that the dot in foo.bar will thus not be present in Dots but is
	// instead in Parts[0].
	Dots []*RuneNode
	// contains filtered or unexported fields
}

OptionNameNode represents an option name or even a traversal through message types to name a nested option field. Example:

(foo.bar).baz.(bob)

func NewOptionNameNode

func NewOptionNameNode(parts []*FieldReferenceNode, dots []*RuneNode) *OptionNameNode

NewOptionNameNode creates a new *OptionNameNode. The dots arg must have a length that is one less than the length of parts. The parts arg must not be empty.

func (*OptionNameNode) Children

func (n *OptionNameNode) Children() []Node

func (*OptionNameNode) End

func (n *OptionNameNode) End() Token

func (*OptionNameNode) Start

func (n *OptionNameNode) Start() Token

type OptionNode

type OptionNode struct {
	Keyword   *KeywordNode // absent for compact options
	Name      *OptionNameNode
	Equals    *RuneNode
	Val       ValueNode
	Semicolon *RuneNode // absent for compact options
	// contains filtered or unexported fields
}

OptionNode represents the declaration of a single option for an element. It is used both for normal option declarations (start with "option" keyword and end with semicolon) and for compact options found in fields, enum values, and extension ranges. Example:

option (custom.option) = "foo";

func NewCompactOptionNode

func NewCompactOptionNode(name *OptionNameNode, equals *RuneNode, val ValueNode) *OptionNode

NewCompactOptionNode creates a new *OptionNode for a full compact declaration (as used in fields, enum values, and extension ranges). All arguments must be non-nil.

  • name: The token corresponding to the name of the option.
  • equals: The token corresponding to the "=" rune after the name.
  • val: The token corresponding to the option value.

func NewOptionNode

func NewOptionNode(keyword *KeywordNode, name *OptionNameNode, equals *RuneNode, val ValueNode, semicolon *RuneNode) *OptionNode

NewOptionNode creates a new *OptionNode for a full option declaration (as used in files, messages, oneofs, enums, services, and methods). All arguments must be non-nil. (Also see NewCompactOptionNode.)

  • keyword: The token corresponding to the "option" keyword.
  • name: The token corresponding to the name of the option.
  • equals: The token corresponding to the "=" rune after the name.
  • val: The token corresponding to the option value.
  • semicolon: The token corresponding to the ";" rune that ends the declaration.

func (*OptionNode) Children

func (n *OptionNode) Children() []Node

func (*OptionNode) End

func (n *OptionNode) End() Token

func (*OptionNode) GetName

func (n *OptionNode) GetName() Node

func (*OptionNode) GetValue

func (n *OptionNode) GetValue() ValueNode

func (*OptionNode) Start

func (n *OptionNode) Start() Token

type PackageNode

type PackageNode struct {
	Keyword   *KeywordNode
	Name      IdentValueNode
	Semicolon *RuneNode
	// contains filtered or unexported fields
}

PackageNode represents a package declaration. Example:

package foobar.com;

func NewPackageNode

func NewPackageNode(keyword *KeywordNode, name IdentValueNode, semicolon *RuneNode) *PackageNode

NewPackageNode creates a new *PackageNode. All three arguments must be non-nil:

  • keyword: The token corresponding to the "package" keyword.
  • name: The package name declared for the file.
  • semicolon: The token corresponding to the ";" rune that ends the declaration.

func (*PackageNode) Children

func (n *PackageNode) Children() []Node

func (*PackageNode) End

func (n *PackageNode) End() Token

func (*PackageNode) Start

func (n *PackageNode) Start() Token

type PositiveUintLiteralNode

type PositiveUintLiteralNode struct {
	Plus *RuneNode
	Uint *UintLiteralNode
	Val  uint64
	// contains filtered or unexported fields
}

PositiveUintLiteralNode represents an integer literal with a positive (+) sign.

func NewPositiveUintLiteralNode

func NewPositiveUintLiteralNode(sign *RuneNode, i *UintLiteralNode) *PositiveUintLiteralNode

NewPositiveUintLiteralNode creates a new *PositiveUintLiteralNode. Both arguments must be non-nil.

func (*PositiveUintLiteralNode) AsInt64

func (n *PositiveUintLiteralNode) AsInt64() (int64, bool)

func (*PositiveUintLiteralNode) AsUint64

func (n *PositiveUintLiteralNode) AsUint64() (uint64, bool)

func (*PositiveUintLiteralNode) Children

func (n *PositiveUintLiteralNode) Children() []Node

func (*PositiveUintLiteralNode) End

func (n *PositiveUintLiteralNode) End() Token

func (*PositiveUintLiteralNode) Start

func (n *PositiveUintLiteralNode) Start() Token

func (*PositiveUintLiteralNode) Value

func (n *PositiveUintLiteralNode) Value() interface{}

type RPCDeclNode

type RPCDeclNode interface {
	Node
	GetName() Node
	GetInputType() Node
	GetOutputType() Node
}

RPCDeclNode is a placeholder interface for AST nodes that represent RPC declarations. This allows NoSourceNode to be used in place of *RPCNode for some usages.

type RPCElement

type RPCElement interface {
	Node
	// contains filtered or unexported methods
}

RPCElement is an interface implemented by all AST nodes that can appear in the body of an rpc declaration (aka method).

type RPCNode

type RPCNode struct {
	Keyword    *KeywordNode
	Name       *IdentNode
	Input      *RPCTypeNode
	Returns    *KeywordNode
	Output     *RPCTypeNode
	Semicolon  *RuneNode
	OpenBrace  *RuneNode
	Decls      []RPCElement
	CloseBrace *RuneNode
	// contains filtered or unexported fields
}

RPCNode represents an RPC declaration. Example:

rpc Foo (Bar) returns (Baz);

func NewRPCNode

func NewRPCNode(keyword *KeywordNode, name *IdentNode, input *RPCTypeNode, returns *KeywordNode, output *RPCTypeNode, semicolon *RuneNode) *RPCNode

NewRPCNode creates a new *RPCNode with no body. All arguments must be non-nil.

  • keyword: The token corresponding to the "rpc" keyword.
  • name: The token corresponding to the RPC's name.
  • input: The token corresponding to the RPC input message type.
  • returns: The token corresponding to the "returns" keyword that precedes the output type.
  • output: The token corresponding to the RPC output message type.
  • semicolon: The token corresponding to the ";" rune that ends the declaration.

func NewRPCNodeWithBody

func NewRPCNodeWithBody(keyword *KeywordNode, name *IdentNode, input *RPCTypeNode, returns *KeywordNode, output *RPCTypeNode, openBrace *RuneNode, decls []RPCElement, closeBrace *RuneNode) *RPCNode

NewRPCNodeWithBody creates a new *RPCNode that includes a body (and possibly options). All arguments must be non-nil.

  • keyword: The token corresponding to the "rpc" keyword.
  • name: The token corresponding to the RPC's name.
  • input: The token corresponding to the RPC input message type.
  • returns: The token corresponding to the "returns" keyword that precedes the output type.
  • output: The token corresponding to the RPC output message type.
  • openBrace: The token corresponding to the "{" rune that starts the body.
  • decls: All declarations inside the RPC body.
  • closeBrace: The token corresponding to the "}" rune that ends the body.

func (*RPCNode) Children

func (n *RPCNode) Children() []Node

func (*RPCNode) End

func (n *RPCNode) End() Token

func (*RPCNode) GetInputType

func (n *RPCNode) GetInputType() Node

func (*RPCNode) GetName

func (n *RPCNode) GetName() Node

func (*RPCNode) GetOutputType

func (n *RPCNode) GetOutputType() Node

func (*RPCNode) Start

func (n *RPCNode) Start() Token

type RPCTypeNode

type RPCTypeNode struct {
	OpenParen   *RuneNode
	Stream      *KeywordNode
	MessageType IdentValueNode
	CloseParen  *RuneNode
	// contains filtered or unexported fields
}

RPCTypeNode represents the declaration of a request or response type for an RPC. Example:

(stream foo.Bar)

func NewRPCTypeNode

func NewRPCTypeNode(openParen *RuneNode, stream *KeywordNode, msgType IdentValueNode, closeParen *RuneNode) *RPCTypeNode

NewRPCTypeNode creates a new *RPCTypeNode. All arguments must be non-nil except stream, which may be nil.

  • openParen: The token corresponding to the "(" rune that starts the declaration.
  • stream: The token corresponding to the "stream" keyword or nil if not present.
  • msgType: The token corresponding to the message type's name.
  • closeParen: The token corresponding to the ")" rune that ends the declaration.

func (*RPCTypeNode) Children

func (n *RPCTypeNode) Children() []Node

func (*RPCTypeNode) End

func (n *RPCTypeNode) End() Token

func (*RPCTypeNode) Start

func (n *RPCTypeNode) Start() Token

type RangeDeclNode

type RangeDeclNode interface {
	Node
	RangeStart() Node
	RangeEnd() Node
}

RangeDeclNode is a placeholder interface for AST nodes that represent numeric values. This allows NoSourceNode to be used in place of *RangeNode for some usages.

type RangeNode

type RangeNode struct {
	StartVal IntValueNode
	// if To is non-nil, then exactly one of EndVal or Max must also be non-nil
	To *KeywordNode
	// EndVal and Max are mutually exclusive
	EndVal IntValueNode
	Max    *KeywordNode
	// contains filtered or unexported fields
}

RangeNode represents a range expression, used in both extension ranges and reserved ranges. Example:

1000 to max

func NewRangeNode

func NewRangeNode(start IntValueNode, to *KeywordNode, end IntValueNode, max *KeywordNode) *RangeNode

NewRangeNode creates a new *RangeNode. The start argument must be non-nil. The to argument represents the "to" keyword. If present (i.e. if it is non-nil), then so must be exactly one of end or max. If max is non-nil, it indicates a "100 to max" style range. But if end is non-nil, the end of the range is a literal, such as "100 to 200".

func (*RangeNode) Children

func (n *RangeNode) Children() []Node

func (*RangeNode) End

func (n *RangeNode) End() Token

func (*RangeNode) EndValue

func (n *RangeNode) EndValue() interface{}

func (*RangeNode) EndValueAsInt32

func (n *RangeNode) EndValueAsInt32(min, max int32) (int32, bool)

func (*RangeNode) RangeEnd

func (n *RangeNode) RangeEnd() Node

func (*RangeNode) RangeStart

func (n *RangeNode) RangeStart() Node

func (*RangeNode) Start

func (n *RangeNode) Start() Token

func (*RangeNode) StartValue

func (n *RangeNode) StartValue() interface{}

func (*RangeNode) StartValueAsInt32

func (n *RangeNode) StartValueAsInt32(min, max int32) (int32, bool)

type ReservedNode

type ReservedNode struct {
	Keyword *KeywordNode
	// If non-empty, this node represents reserved ranges and Names will be empty.
	Ranges []*RangeNode
	// If non-empty, this node represents reserved names and Ranges will be empty.
	Names []StringValueNode
	// Commas represent the separating ',' characters between options. The
	// length of this slice must be exactly len(Ranges)-1 or len(Names)-1, depending
	// on whether this node represents reserved ranges or reserved names. Each item
	// in Ranges or Names has a corresponding item in this slice *except the last*
	// (since a trailing comma is not allowed).
	Commas    []*RuneNode
	Semicolon *RuneNode
	// contains filtered or unexported fields
}

ReservedNode represents reserved declaration, which can be used to reserve either names or numbers. Examples:

reserved 1, 10-12, 15;
reserved "foo", "bar", "baz";

func NewReservedNamesNode

func NewReservedNamesNode(keyword *KeywordNode, names []StringValueNode, commas []*RuneNode, semicolon *RuneNode) *ReservedNode

NewReservedNamesNode creates a new *ReservedNode that represents reserved names. All args must be non-nil.

  • keyword: The token corresponding to the "reserved" keyword.
  • names: One or more names.
  • commas: Tokens that represent the "," runes that delimit the names. The length of commas must be one less than the length of names.
  • semicolon The token corresponding to the ";" rune that ends the declaration.

func NewReservedRangesNode

func NewReservedRangesNode(keyword *KeywordNode, ranges []*RangeNode, commas []*RuneNode, semicolon *RuneNode) *ReservedNode

NewReservedRangesNode creates a new *ReservedNode that represents reserved numeric ranges. All args must be non-nil.

  • keyword: The token corresponding to the "reserved" keyword.
  • ranges: One or more range expressions.
  • commas: Tokens that represent the "," runes that delimit the range expressions. The length of commas must be one less than the length of ranges.
  • semicolon The token corresponding to the ";" rune that ends the declaration.

func (*ReservedNode) Children

func (n *ReservedNode) Children() []Node

func (*ReservedNode) End

func (n *ReservedNode) End() Token

func (*ReservedNode) Start

func (n *ReservedNode) Start() Token

type RuneNode

type RuneNode struct {
	Rune rune
	// contains filtered or unexported fields
}

RuneNode represents a single rune in protobuf source. Runes are typically collected into tokens, but some runes stand on their own, such as punctuation/symbols like commas, semicolons, equals signs, open and close symbols (braces, brackets, angles, and parentheses), and periods/dots. TODO: make this more compact; if runes don't have attributed comments

then we don't need a Token to represent them and only need an offset
into the file's contents

func NewRuneNode

func NewRuneNode(r rune, tok Token) *RuneNode

NewRuneNode creates a new *RuneNode with the given properties.

func (RuneNode) End

func (n RuneNode) End() Token

func (RuneNode) Start

func (n RuneNode) Start() Token

func (RuneNode) Token

func (n RuneNode) Token() Token

type ServiceElement

type ServiceElement interface {
	Node
	// contains filtered or unexported methods
}

ServiceElement is an interface implemented by all AST nodes that can appear in the body of a service declaration.

type ServiceNode

type ServiceNode struct {
	Keyword    *KeywordNode
	Name       *IdentNode
	OpenBrace  *RuneNode
	Decls      []ServiceElement
	CloseBrace *RuneNode
	// contains filtered or unexported fields
}

ServiceNode represents a service declaration. Example:

service Foo {
  rpc Bar (Baz) returns (Bob);
  rpc Frobnitz (stream Parts) returns (Gyzmeaux);
}

func NewServiceNode

func NewServiceNode(keyword *KeywordNode, name *IdentNode, openBrace *RuneNode, decls []ServiceElement, closeBrace *RuneNode) *ServiceNode

NewServiceNode creates a new *ServiceNode. All arguments must be non-nil.

  • keyword: The token corresponding to the "service" keyword.
  • name: The token corresponding to the service's name.
  • openBrace: The token corresponding to the "{" rune that starts the body.
  • decls: All declarations inside the service body.
  • closeBrace: The token corresponding to the "}" rune that ends the body.

func (*ServiceNode) Children

func (n *ServiceNode) Children() []Node

func (*ServiceNode) End

func (n *ServiceNode) End() Token

func (*ServiceNode) Start

func (n *ServiceNode) Start() Token

type SignedFloatLiteralNode

type SignedFloatLiteralNode struct {
	Sign  *RuneNode
	Float FloatValueNode
	Val   float64
	// contains filtered or unexported fields
}

SignedFloatLiteralNode represents a signed floating point number.

func NewSignedFloatLiteralNode

func NewSignedFloatLiteralNode(sign *RuneNode, f FloatValueNode) *SignedFloatLiteralNode

NewSignedFloatLiteralNode creates a new *SignedFloatLiteralNode. Both arguments must be non-nil.

func (*SignedFloatLiteralNode) AsFloat

func (n *SignedFloatLiteralNode) AsFloat() float64

func (*SignedFloatLiteralNode) Children

func (n *SignedFloatLiteralNode) Children() []Node

func (*SignedFloatLiteralNode) End

func (n *SignedFloatLiteralNode) End() Token

func (*SignedFloatLiteralNode) Start

func (n *SignedFloatLiteralNode) Start() Token

func (*SignedFloatLiteralNode) Value

func (n *SignedFloatLiteralNode) Value() interface{}

type SimpleVisitor

type SimpleVisitor struct {
	DoVisitFileNode                  func(*FileNode) error
	DoVisitSyntaxNode                func(*SyntaxNode) error
	DoVisitPackageNode               func(*PackageNode) error
	DoVisitImportNode                func(*ImportNode) error
	DoVisitOptionNode                func(*OptionNode) error
	DoVisitOptionNameNode            func(*OptionNameNode) error
	DoVisitFieldReferenceNode        func(*FieldReferenceNode) error
	DoVisitCompactOptionsNode        func(*CompactOptionsNode) error
	DoVisitMessageNode               func(*MessageNode) error
	DoVisitExtendNode                func(*ExtendNode) error
	DoVisitExtensionRangeNode        func(*ExtensionRangeNode) error
	DoVisitReservedNode              func(*ReservedNode) error
	DoVisitRangeNode                 func(*RangeNode) error
	DoVisitFieldNode                 func(*FieldNode) error
	DoVisitGroupNode                 func(*GroupNode) error
	DoVisitMapFieldNode              func(*MapFieldNode) error
	DoVisitMapTypeNode               func(*MapTypeNode) error
	DoVisitOneOfNode                 func(*OneOfNode) error
	DoVisitEnumNode                  func(*EnumNode) error
	DoVisitEnumValueNode             func(*EnumValueNode) error
	DoVisitServiceNode               func(*ServiceNode) error
	DoVisitRPCNode                   func(*RPCNode) error
	DoVisitRPCTypeNode               func(*RPCTypeNode) error
	DoVisitIdentNode                 func(*IdentNode) error
	DoVisitCompoundIdentNode         func(*CompoundIdentNode) error
	DoVisitStringLiteralNode         func(*StringLiteralNode) error
	DoVisitCompoundStringLiteralNode func(*CompoundStringLiteralNode) error
	DoVisitUintLiteralNode           func(*UintLiteralNode) error
	DoVisitPositiveUintLiteralNode   func(*PositiveUintLiteralNode) error
	DoVisitNegativeIntLiteralNode    func(*NegativeIntLiteralNode) error
	DoVisitFloatLiteralNode          func(*FloatLiteralNode) error
	DoVisitSpecialFloatLiteralNode   func(*SpecialFloatLiteralNode) error
	DoVisitSignedFloatLiteralNode    func(*SignedFloatLiteralNode) error
	DoVisitBoolLiteralNode           func(*BoolLiteralNode) error
	DoVisitArrayLiteralNode          func(*ArrayLiteralNode) error
	DoVisitMessageLiteralNode        func(*MessageLiteralNode) error
	DoVisitMessageFieldNode          func(*MessageFieldNode) error
	DoVisitKeywordNode               func(*KeywordNode) error
	DoVisitRuneNode                  func(*RuneNode) error
	DoVisitEmptyDeclNode             func(*EmptyDeclNode) error

	DoVisitFieldDeclNode   func(FieldDeclNode) error
	DoVisitMessageDeclNode func(MessageDeclNode) error

	DoVisitIdentValueNode  func(IdentValueNode) error
	DoVisitStringValueNode func(StringValueNode) error
	DoVisitIntValueNode    func(IntValueNode) error
	DoVisitFloatValueNode  func(FloatValueNode) error
	DoVisitValueNode       func(ValueNode) error

	DoVisitTerminalNode  func(TerminalNode) error
	DoVisitCompositeNode func(CompositeNode) error
	DoVisitNode          func(Node) error
}

SimpleVisitor is a visitor implementation that uses numerous function fields. If a relevant function field is not nil, then it will be invoked when a node is visited.

In addition to a function for each concrete node type (and thus for each Visit* method of the Visitor interface), it also has function fields that accept interface types. So a visitor can, for example, easily treat all ValueNodes uniformly by providing a non-nil value for DoVisitValueNode instead of having to supply values for the various Do*Node methods corresponding to all types that implement ValueNode.

The most specific function provided that matches a given node is the one that will be invoked. For example, DoVisitStringValueNode will be called if present and applicable before DoVisitValueNode. Similarly, DoVisitValueNode would be called before DoVisitTerminalNode or DoVisitCompositeNode. The DoVisitNode is the most generic function and is called only if no more specific function is present for a given node type.

The *UintLiteralNode type implements both IntValueNode and FloatValueNode. In this case, the DoVisitIntValueNode function is considered more specific than DoVisitFloatValueNode, so will be preferred if present.

Similarly, *MapFieldNode implements both FieldDeclNode and MessageDeclNode. In this case, the DoVisitFieldDeclNode function is considered more specific than DoVisitMessageDeclNode, so will be preferred if present.

func (*SimpleVisitor) VisitArrayLiteralNode

func (b *SimpleVisitor) VisitArrayLiteralNode(node *ArrayLiteralNode) error

func (*SimpleVisitor) VisitBoolLiteralNode

func (b *SimpleVisitor) VisitBoolLiteralNode(node *BoolLiteralNode) error

func (*SimpleVisitor) VisitCompactOptionsNode

func (b *SimpleVisitor) VisitCompactOptionsNode(node *CompactOptionsNode) error

func (*SimpleVisitor) VisitCompoundIdentNode

func (b *SimpleVisitor) VisitCompoundIdentNode(node *CompoundIdentNode) error

func (*SimpleVisitor) VisitCompoundStringLiteralNode

func (b *SimpleVisitor) VisitCompoundStringLiteralNode(node *CompoundStringLiteralNode) error

func (*SimpleVisitor) VisitEmptyDeclNode

func (b *SimpleVisitor) VisitEmptyDeclNode(node *EmptyDeclNode) error

func (*SimpleVisitor) VisitEnumNode

func (b *SimpleVisitor) VisitEnumNode(node *EnumNode) error

func (*SimpleVisitor) VisitEnumValueNode

func (b *SimpleVisitor) VisitEnumValueNode(node *EnumValueNode) error

func (*SimpleVisitor) VisitExtendNode

func (b *SimpleVisitor) VisitExtendNode(node *ExtendNode) error

func (*SimpleVisitor) VisitExtensionRangeNode

func (b *SimpleVisitor) VisitExtensionRangeNode(node *ExtensionRangeNode) error

func (*SimpleVisitor) VisitFieldNode

func (b *SimpleVisitor) VisitFieldNode(node *FieldNode) error

func (*SimpleVisitor) VisitFieldReferenceNode

func (b *SimpleVisitor) VisitFieldReferenceNode(node *FieldReferenceNode) error

func (*SimpleVisitor) VisitFileNode

func (b *SimpleVisitor) VisitFileNode(node *FileNode) error

func (*SimpleVisitor) VisitFloatLiteralNode

func (b *SimpleVisitor) VisitFloatLiteralNode(node *FloatLiteralNode) error

func (*SimpleVisitor) VisitGroupNode

func (b *SimpleVisitor) VisitGroupNode(node *GroupNode) error

func (*SimpleVisitor) VisitIdentNode

func (b *SimpleVisitor) VisitIdentNode(node *IdentNode) error

func (*SimpleVisitor) VisitImportNode

func (b *SimpleVisitor) VisitImportNode(node *ImportNode) error

func (*SimpleVisitor) VisitKeywordNode

func (b *SimpleVisitor) VisitKeywordNode(node *KeywordNode) error

func (*SimpleVisitor) VisitMapFieldNode

func (b *SimpleVisitor) VisitMapFieldNode(node *MapFieldNode) error

func (*SimpleVisitor) VisitMapTypeNode

func (b *SimpleVisitor) VisitMapTypeNode(node *MapTypeNode) error

func (*SimpleVisitor) VisitMessageFieldNode

func (b *SimpleVisitor) VisitMessageFieldNode(node *MessageFieldNode) error

func (*SimpleVisitor) VisitMessageLiteralNode

func (b *SimpleVisitor) VisitMessageLiteralNode(node *MessageLiteralNode) error

func (*SimpleVisitor) VisitMessageNode

func (b *SimpleVisitor) VisitMessageNode(node *MessageNode) error

func (*SimpleVisitor) VisitNegativeIntLiteralNode

func (b *SimpleVisitor) VisitNegativeIntLiteralNode(node *NegativeIntLiteralNode) error

func (*SimpleVisitor) VisitOneOfNode

func (b *SimpleVisitor) VisitOneOfNode(node *OneOfNode) error

func (*SimpleVisitor) VisitOptionNameNode

func (b *SimpleVisitor) VisitOptionNameNode(node *OptionNameNode) error

func (*SimpleVisitor) VisitOptionNode

func (b *SimpleVisitor) VisitOptionNode(node *OptionNode) error

func (*SimpleVisitor) VisitPackageNode

func (b *SimpleVisitor) VisitPackageNode(node *PackageNode) error

func (*SimpleVisitor) VisitPositiveUintLiteralNode

func (b *SimpleVisitor) VisitPositiveUintLiteralNode(node *PositiveUintLiteralNode) error

func (*SimpleVisitor) VisitRPCNode

func (b *SimpleVisitor) VisitRPCNode(node *RPCNode) error

func (*SimpleVisitor) VisitRPCTypeNode

func (b *SimpleVisitor) VisitRPCTypeNode(node *RPCTypeNode) error

func (*SimpleVisitor) VisitRangeNode

func (b *SimpleVisitor) VisitRangeNode(node *RangeNode) error

func (*SimpleVisitor) VisitReservedNode

func (b *SimpleVisitor) VisitReservedNode(node *ReservedNode) error

func (*SimpleVisitor) VisitRuneNode

func (b *SimpleVisitor) VisitRuneNode(node *RuneNode) error

func (*SimpleVisitor) VisitServiceNode

func (b *SimpleVisitor) VisitServiceNode(node *ServiceNode) error

func (*SimpleVisitor) VisitSignedFloatLiteralNode

func (b *SimpleVisitor) VisitSignedFloatLiteralNode(node *SignedFloatLiteralNode) error

func (*SimpleVisitor) VisitSpecialFloatLiteralNode

func (b *SimpleVisitor) VisitSpecialFloatLiteralNode(node *SpecialFloatLiteralNode) error

func (*SimpleVisitor) VisitStringLiteralNode

func (b *SimpleVisitor) VisitStringLiteralNode(node *StringLiteralNode) error

func (*SimpleVisitor) VisitSyntaxNode

func (b *SimpleVisitor) VisitSyntaxNode(node *SyntaxNode) error

func (*SimpleVisitor) VisitUintLiteralNode

func (b *SimpleVisitor) VisitUintLiteralNode(node *UintLiteralNode) error

type SourcePos

type SourcePos struct {
	Filename string
	// The line and column numbers for this position. These are
	// one-based, so the first line and column is 1 (not zero). If
	// either is zero, then the line and column are unknown and
	// only the file name is known.
	Line, Col int
	// The offset, in bytes, from the beginning of the file. This
	// is zero-based: the first character in the file is offset zero.
	Offset int
}

SourcePos identifies a location in a proto source file.

func UnknownPos

func UnknownPos(filename string) SourcePos

UnknownPos is a placeholder position when only the source file name is known.

func (SourcePos) String

func (pos SourcePos) String() string

type SpecialFloatLiteralNode

type SpecialFloatLiteralNode struct {
	*KeywordNode
	Val float64
}

SpecialFloatLiteralNode represents a special floating point numeric literal for "inf" and "nan" values.

func NewSpecialFloatLiteralNode

func NewSpecialFloatLiteralNode(name *KeywordNode) *SpecialFloatLiteralNode

NewSpecialFloatLiteralNode returns a new *SpecialFloatLiteralNode for the given keyword, which must be "inf" or "nan".

func (*SpecialFloatLiteralNode) AsFloat

func (n *SpecialFloatLiteralNode) AsFloat() float64

func (*SpecialFloatLiteralNode) Value

func (n *SpecialFloatLiteralNode) Value() interface{}

type StringLiteralNode

type StringLiteralNode struct {

	// Val is the actual string value that the literal indicates.
	Val string
	// contains filtered or unexported fields
}

StringLiteralNode represents a simple string literal. Example:

"proto2"

func NewStringLiteralNode

func NewStringLiteralNode(val string, tok Token) *StringLiteralNode

NewStringLiteralNode creates a new *StringLiteralNode with the given val.

func (*StringLiteralNode) AsString

func (n *StringLiteralNode) AsString() string

func (StringLiteralNode) End

func (n StringLiteralNode) End() Token

func (StringLiteralNode) Start

func (n StringLiteralNode) Start() Token

func (StringLiteralNode) Token

func (n StringLiteralNode) Token() Token

func (*StringLiteralNode) Value

func (n *StringLiteralNode) Value() interface{}

type StringValueNode

type StringValueNode interface {
	ValueNode
	AsString() string
}

StringValueNode is an AST node that represents a string literal. Such a node can be a single literal (*StringLiteralNode) or a concatenation of multiple literals (*CompoundStringLiteralNode).

type SyntaxNode

type SyntaxNode struct {
	Keyword   *KeywordNode
	Equals    *RuneNode
	Syntax    StringValueNode
	Semicolon *RuneNode
	// contains filtered or unexported fields
}

SyntaxNode represents a syntax declaration, which if present must be the first non-comment content. Example:

syntax = "proto2";

Files that don't have a syntax node are assumed to use proto2 syntax.

func NewSyntaxNode

func NewSyntaxNode(keyword *KeywordNode, equals *RuneNode, syntax StringValueNode, semicolon *RuneNode) *SyntaxNode

NewSyntaxNode creates a new *SyntaxNode. All four arguments must be non-nil:

  • keyword: The token corresponding to the "syntax" keyword.
  • equals: The token corresponding to the "=" rune.
  • syntax: The actual syntax value, e.g. "proto2" or "proto3".
  • semicolon: The token corresponding to the ";" rune that ends the declaration.

func (*SyntaxNode) Children

func (n *SyntaxNode) Children() []Node

func (*SyntaxNode) End

func (n *SyntaxNode) End() Token

func (*SyntaxNode) Start

func (n *SyntaxNode) Start() Token

type SyntheticMapField

type SyntheticMapField struct {
	Ident IdentValueNode
	Tag   *UintLiteralNode
}

SyntheticMapField is not an actual node in the AST but a synthetic node that implements FieldDeclNode. These are used to represent the implicit field declarations of the "key" and "value" fields in a map entry.

func NewSyntheticMapField

func NewSyntheticMapField(ident IdentValueNode, tagNum uint64) *SyntheticMapField

NewSyntheticMapField creates a new *SyntheticMapField for the given identifier (either a key or value type in a map declaration) and tag number (1 for key, 2 for value).

func (*SyntheticMapField) End

func (n *SyntheticMapField) End() Token

func (*SyntheticMapField) FieldExtendee

func (n *SyntheticMapField) FieldExtendee() Node

func (*SyntheticMapField) FieldLabel

func (n *SyntheticMapField) FieldLabel() Node

func (*SyntheticMapField) FieldName

func (n *SyntheticMapField) FieldName() Node

func (*SyntheticMapField) FieldTag

func (n *SyntheticMapField) FieldTag() Node

func (*SyntheticMapField) FieldType

func (n *SyntheticMapField) FieldType() Node

func (*SyntheticMapField) GetGroupKeyword

func (n *SyntheticMapField) GetGroupKeyword() Node

func (*SyntheticMapField) GetOptions

func (n *SyntheticMapField) GetOptions() *CompactOptionsNode

func (*SyntheticMapField) LeadingComments

func (n *SyntheticMapField) LeadingComments() []Comment

func (*SyntheticMapField) Start

func (n *SyntheticMapField) Start() Token

func (*SyntheticMapField) TrailingComments

func (n *SyntheticMapField) TrailingComments() []Comment

type SyntheticOneOf

type SyntheticOneOf struct {
	Ident IdentValueNode
}

SyntheticOneOf is not an actual node in the AST but a synthetic node that represents the oneof implied by a proto3 optional field.

func NewSyntheticOneOf

func NewSyntheticOneOf(ident IdentValueNode) *SyntheticOneOf

NewSyntheticOneOf creates a new *SyntheticOneOf for the given identifier (the name of the proto3 optional field).

func (*SyntheticOneOf) End

func (n *SyntheticOneOf) End() Token

func (*SyntheticOneOf) LeadingComments

func (n *SyntheticOneOf) LeadingComments() []Comment

func (*SyntheticOneOf) Start

func (n *SyntheticOneOf) Start() Token

func (*SyntheticOneOf) TrailingComments

func (n *SyntheticOneOf) TrailingComments() []Comment

type TerminalNode

type TerminalNode interface {
	Node
	Token() Token
}

TerminalNode represents a leaf in the AST. These represent the tokens/lexemes in the protobuf language. Comments and whitespace are accumulated by the lexer and associated with the following lexed token.

type Token

type Token int

Token represents a single lexed token.

type UintLiteralNode

type UintLiteralNode struct {

	// Val is the numeric value indicated by the literal
	Val uint64
	// contains filtered or unexported fields
}

UintLiteralNode represents a simple integer literal with no sign character.

func NewUintLiteralNode

func NewUintLiteralNode(val uint64, tok Token) *UintLiteralNode

NewUintLiteralNode creates a new *UintLiteralNode with the given val.

func (*UintLiteralNode) AsFloat

func (n *UintLiteralNode) AsFloat() float64

func (*UintLiteralNode) AsInt64

func (n *UintLiteralNode) AsInt64() (int64, bool)

func (*UintLiteralNode) AsUint64

func (n *UintLiteralNode) AsUint64() (uint64, bool)

func (UintLiteralNode) End

func (n UintLiteralNode) End() Token

func (UintLiteralNode) Start

func (n UintLiteralNode) Start() Token

func (UintLiteralNode) Token

func (n UintLiteralNode) Token() Token

func (*UintLiteralNode) Value

func (n *UintLiteralNode) Value() interface{}

type ValueNode

type ValueNode interface {
	Node
	// Value returns a Go representation of the value. For scalars, this
	// will be a string, int64, uint64, float64, or bool. This could also
	// be an Identifier (e.g. IdentValueNodes). It can also be a composite
	// literal:
	//   * For array literals, the type returned will be []ValueNode
	//   * For message literals, the type returned will be []*MessageFieldNode
	Value() interface{}
}

ValueNode is an AST node that represents a literal value.

It also includes references (e.g. IdentifierValueNode), which can be used as values in some contexts, such as describing the default value for a field, which can refer to an enum value.

This also allows NoSourceNode to be used in place of a real value node for some usages.

type Visitor

type Visitor interface {
	// VisitFileNode is invoked when visiting a *FileNode in the AST.
	VisitFileNode(*FileNode) error
	// VisitSyntaxNode is invoked when visiting a *SyntaxNode in the AST.
	VisitSyntaxNode(*SyntaxNode) error
	// VisitPackageNode is invoked when visiting a *PackageNode in the AST.
	VisitPackageNode(*PackageNode) error
	// VisitImportNode is invoked when visiting an *ImportNode in the AST.
	VisitImportNode(*ImportNode) error
	// VisitOptionNode is invoked when visiting an *OptionNode in the AST.
	VisitOptionNode(*OptionNode) error
	// VisitOptionNameNode is invoked when visiting an *OptionNameNode in the AST.
	VisitOptionNameNode(*OptionNameNode) error
	// VisitFieldReferenceNode is invoked when visiting a *FieldReferenceNode in the AST.
	VisitFieldReferenceNode(*FieldReferenceNode) error
	// VisitCompactOptionsNode is invoked when visiting a *CompactOptionsNode in the AST.
	VisitCompactOptionsNode(*CompactOptionsNode) error
	// VisitMessageNode is invoked when visiting a *MessageNode in the AST.
	VisitMessageNode(*MessageNode) error
	// VisitExtendNode is invoked when visiting an *ExtendNode in the AST.
	VisitExtendNode(*ExtendNode) error
	// VisitExtensionRangeNode is invoked when visiting an *ExtensionRangeNode in the AST.
	VisitExtensionRangeNode(*ExtensionRangeNode) error
	// VisitReservedNode is invoked when visiting a *ReservedNode in the AST.
	VisitReservedNode(*ReservedNode) error
	// VisitRangeNode is invoked when visiting a *RangeNode in the AST.
	VisitRangeNode(*RangeNode) error
	// VisitFieldNode is invoked when visiting a *FieldNode in the AST.
	VisitFieldNode(*FieldNode) error
	// VisitGroupNode is invoked when visiting a *GroupNode in the AST.
	VisitGroupNode(*GroupNode) error
	// VisitMapFieldNode is invoked when visiting a *MapFieldNode in the AST.
	VisitMapFieldNode(*MapFieldNode) error
	// VisitMapTypeNode is invoked when visiting a *MapTypeNode in the AST.
	VisitMapTypeNode(*MapTypeNode) error
	// VisitOneOfNode is invoked when visiting a *OneOfNode in the AST.
	VisitOneOfNode(*OneOfNode) error
	// VisitEnumNode is invoked when visiting an *EnumNode in the AST.
	VisitEnumNode(*EnumNode) error
	// VisitEnumValueNode is invoked when visiting an *EnumValueNode in the AST.
	VisitEnumValueNode(*EnumValueNode) error
	// VisitServiceNode is invoked when visiting a *ServiceNode in the AST.
	VisitServiceNode(*ServiceNode) error
	// VisitRPCNode is invoked when visiting an *RPCNode in the AST.
	VisitRPCNode(*RPCNode) error
	// VisitRPCTypeNode is invoked when visiting an *RPCTypeNode in the AST.
	VisitRPCTypeNode(*RPCTypeNode) error
	// VisitIdentNode is invoked when visiting an *IdentNode in the AST.
	VisitIdentNode(*IdentNode) error
	// VisitCompoundIdentNode is invoked when visiting a *CompoundIdentNode in the AST.
	VisitCompoundIdentNode(*CompoundIdentNode) error
	// VisitStringLiteralNode is invoked when visiting a *StringLiteralNode in the AST.
	VisitStringLiteralNode(*StringLiteralNode) error
	// VisitCompoundStringLiteralNode is invoked when visiting a *CompoundStringLiteralNode in the AST.
	VisitCompoundStringLiteralNode(*CompoundStringLiteralNode) error
	// VisitUintLiteralNode is invoked when visiting a *UintLiteralNode in the AST.
	VisitUintLiteralNode(*UintLiteralNode) error
	// VisitPositiveUintLiteralNode is invoked when visiting a *PositiveUintLiteralNode in the AST.
	VisitPositiveUintLiteralNode(*PositiveUintLiteralNode) error
	// VisitNegativeIntLiteralNode is invoked when visiting a *NegativeIntLiteralNode in the AST.
	VisitNegativeIntLiteralNode(*NegativeIntLiteralNode) error
	// VisitFloatLiteralNode is invoked when visiting a *FloatLiteralNode in the AST.
	VisitFloatLiteralNode(*FloatLiteralNode) error
	// VisitSpecialFloatLiteralNode is invoked when visiting a *SpecialFloatLiteralNode in the AST.
	VisitSpecialFloatLiteralNode(*SpecialFloatLiteralNode) error
	// VisitSignedFloatLiteralNode is invoked when visiting a *SignedFloatLiteralNode in the AST.
	VisitSignedFloatLiteralNode(*SignedFloatLiteralNode) error
	// VisitBoolLiteralNode is invoked when visiting a *BoolLiteralNode in the AST.
	VisitBoolLiteralNode(*BoolLiteralNode) error
	// VisitArrayLiteralNode is invoked when visiting an *ArrayLiteralNode in the AST.
	VisitArrayLiteralNode(*ArrayLiteralNode) error
	// VisitMessageLiteralNode is invoked when visiting a *MessageLiteralNode in the AST.
	VisitMessageLiteralNode(*MessageLiteralNode) error
	// VisitMessageFieldNode is invoked when visiting a *MessageFieldNode in the AST.
	VisitMessageFieldNode(*MessageFieldNode) error
	// VisitKeywordNode is invoked when visiting a *KeywordNode in the AST.
	VisitKeywordNode(*KeywordNode) error
	// VisitRuneNode is invoked when visiting a *RuneNode in the AST.
	VisitRuneNode(*RuneNode) error
	// VisitEmptyDeclNode is invoked when visiting a *EmptyDeclNode in the AST.
	VisitEmptyDeclNode(*EmptyDeclNode) error
}

Visitor provides a technique for walking the AST that allows for dynamic dispatch, where a particular function is invoked based on the runtime type of the argument.

It consists of a number of functions, each of which matches a concrete Node type. It also includes functions for sub-interfaces of Node and the Node interface itself, to be used as broader "catch all" functions.

To use a visitor, provide a function for the node types of interest and pass visitor.Visit as the function to a Walk operation. When a node is traversed, the corresponding function field of the visitor is invoked, if not nil. If the function for a node's concrete type is nil/absent but the function for an interface it implements is present, that interface visit function will be used instead. If no matching function is present, the traversal will continue. If a matching function is present, it will be invoked and its response determines how the traversal proceeds.

Every visit function returns (bool, *Visitor). If the bool returned is false, the visited node's descendants are skipped. Otherwise, traversal will continue into the node's children. If the returned visitor is nil, the current visitor will continue to be used. But if a non-nil visitor is returned, it will be used to visit the node's children.

type WalkOption

type WalkOption func(*walkOptions)

WalkOption represents an option used with the Walk function. These allow optional before and after hooks to be invoked as each node in the tree is visited.

func WithAfter

func WithAfter(fn func(Node) error) WalkOption

WithAfter returns a WalkOption that will cause the given function to be invoked after a node (as well as any descendants) is visited during a walk operation. If this hook returns an error, the node is not visited and the walk operation is aborted.

If the walk is aborted due to some other visitor or before hook returning an error, the after hook is still called for all nodes that have been visited. However, the walk operation fails with the first error it encountered, so any error returned from an after hook is effectively ignored.

func WithBefore

func WithBefore(fn func(Node) error) WalkOption

WithBefore returns a WalkOption that will cause the given function to be invoked before a node is visited during a walk operation. If this hook returns an error, the node is not visited and the walk operation is aborted.

Jump to

Keyboard shortcuts

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