Documentation ¶
Overview ¶
Package ast defines types for modeling the AST (Abstract Syntax Tree) for the Protocol Buffers interface definition language.
Nodes ¶
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.
A TerminalNode represents a single lexical element, or Token. A CompositeNode represents a sub-tree of the AST and range of tokens.
Position information is tracked using a *FileInfo. The lexer invokes its various Add* methods to add details as the file is tokenized. Storing the position information in the *FileInfo, instead of in each AST node, allows the AST to have a much more compact representation. To extract detailed position information, you must use the NodeInfo method, available on either the *FileInfo which produced the node's items or the *FileNode root of the tree that contains the node.
Items, Tokens, and Comments ¶
An Item represents a lexical item, excluding whitespace. This can be either a Token or a Comment.
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 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.
A Comment value corresponds to a line ("//") or block ("/*") style comment in the source. These have no bearing on the grammar and are effectively ignored as the parser is determining the shape of the syntax tree.
A Token value corresponds to a component of the grammar, that is used to produce an AST. They correspond to leaves in the AST (i.e. TerminalNode).
The *FileInfo and *FileNode types provide methods for querying and iterating through all the items or tokens in the file. They also include a method for resolving an Item into a Token or Comment.
Factory Functions ¶
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
- Variables
- func AsInt32(n IntValueNode, minVal, maxVal int32) (int32, bool)
- func Visit(n Node, v Visitor) error
- func VisitChildren(n CompositeNode, v Visitor) error
- func Walk(root Node, v Visitor, opts ...WalkOption) error
- type AncestorTracker
- type ArrayLiteralNode
- type Comment
- type Comments
- type CompactOptionsNode
- type CompositeNode
- type CompoundIdentNode
- type CompoundStringLiteralNode
- type EditionNode
- type EmptyDeclNode
- type EnumElement
- type EnumNode
- type EnumValueDeclNode
- type EnumValueNode
- type ExtendElement
- type ExtendNode
- type ExtensionRangeNode
- type FieldDeclNode
- type FieldLabel
- type FieldNode
- func (n *FieldNode) Children() []Node
- func (n *FieldNode) End() Token
- func (n *FieldNode) FieldExtendee() Node
- func (n *FieldNode) FieldLabel() Node
- func (n *FieldNode) FieldName() Node
- func (n *FieldNode) FieldTag() Node
- func (n *FieldNode) FieldType() Node
- func (n *FieldNode) GetGroupKeyword() Node
- func (n *FieldNode) GetOptions() *CompactOptionsNode
- func (n *FieldNode) RangeOptions(fn func(*OptionNode) bool)
- func (n *FieldNode) Start() Token
- type FieldReferenceNode
- type FileDeclNode
- type FileElement
- type FileInfo
- func (f *FileInfo) AddComment(comment, attributedTo Token) Comment
- func (f *FileInfo) AddLine(offset int)
- func (f *FileInfo) AddToken(offset, length int) Token
- func (f *FileInfo) GetItem(i Item) (Token, Comment)
- func (f *FileInfo) ItemInfo(i Item) ItemInfo
- func (f *FileInfo) Items() Sequence[Item]
- func (f *FileInfo) Name() string
- func (f *FileInfo) NodeInfo(n Node) NodeInfo
- func (f *FileInfo) SourcePos(offset int) SourcePos
- func (f *FileInfo) TokenInfo(t Token) NodeInfo
- func (f *FileInfo) Tokens() Sequence[Token]
- type FileNode
- func (n *FileNode) Children() []Node
- func (n *FileNode) End() Token
- func (f *FileNode) GetItem(i Item) (Token, Comment)
- func (f *FileNode) ItemInfo(i Item) ItemInfo
- func (f *FileNode) Items() Sequence[Item]
- func (f *FileNode) Name() string
- func (f *FileNode) NodeInfo(n Node) NodeInfo
- func (f *FileNode) RangeOptions(fn func(*OptionNode) bool)
- func (n *FileNode) Start() Token
- func (f *FileNode) TokenInfo(t Token) NodeInfo
- func (f *FileNode) Tokens() Sequence[Token]
- type FloatLiteralNode
- type FloatValueNode
- type GroupNode
- func (n *GroupNode) AsMessage() *SyntheticGroupMessageNode
- func (n *GroupNode) Children() []Node
- func (n *GroupNode) End() Token
- func (n *GroupNode) FieldExtendee() Node
- func (n *GroupNode) FieldLabel() Node
- func (n *GroupNode) FieldName() Node
- func (n *GroupNode) FieldTag() Node
- func (n *GroupNode) FieldType() Node
- func (n *GroupNode) GetGroupKeyword() Node
- func (n *GroupNode) GetOptions() *CompactOptionsNode
- func (n *GroupNode) RangeOptions(fn func(*OptionNode) bool)
- func (n *GroupNode) Start() Token
- type IdentNode
- type IdentValueNode
- type Identifier
- type ImportNode
- type IntValueNode
- type Item
- type ItemInfo
- type KeywordNode
- type MapFieldNode
- func (n *MapFieldNode) AsMessage() *SyntheticMapEntryNode
- func (n *MapFieldNode) Children() []Node
- func (n *MapFieldNode) End() Token
- func (n *MapFieldNode) FieldExtendee() Node
- func (n *MapFieldNode) FieldLabel() Node
- func (n *MapFieldNode) FieldName() Node
- func (n *MapFieldNode) FieldTag() Node
- func (n *MapFieldNode) FieldType() Node
- func (n *MapFieldNode) GetGroupKeyword() Node
- func (n *MapFieldNode) GetOptions() *CompactOptionsNode
- func (n *MapFieldNode) KeyField() *SyntheticMapField
- func (n *MapFieldNode) RangeOptions(fn func(*OptionNode) bool)
- func (n *MapFieldNode) Start() Token
- func (n *MapFieldNode) ValueField() *SyntheticMapField
- type MapTypeNode
- type MessageBody
- type MessageDeclNode
- type MessageElement
- type MessageFieldNode
- type MessageLiteralNode
- type MessageNode
- type NegativeIntLiteralNode
- func (n *NegativeIntLiteralNode) AsInt64() (int64, bool)
- func (n *NegativeIntLiteralNode) AsUint64() (uint64, bool)
- func (n *NegativeIntLiteralNode) Children() []Node
- func (n *NegativeIntLiteralNode) End() Token
- func (n *NegativeIntLiteralNode) Start() Token
- func (n *NegativeIntLiteralNode) Value() interface{}
- type NoOpVisitor
- func (n NoOpVisitor) VisitArrayLiteralNode(_ *ArrayLiteralNode) error
- func (n NoOpVisitor) VisitCompactOptionsNode(_ *CompactOptionsNode) error
- func (n NoOpVisitor) VisitCompoundIdentNode(_ *CompoundIdentNode) error
- func (n NoOpVisitor) VisitCompoundStringLiteralNode(_ *CompoundStringLiteralNode) error
- func (n NoOpVisitor) VisitEditionNode(_ *EditionNode) error
- func (n NoOpVisitor) VisitEmptyDeclNode(_ *EmptyDeclNode) error
- func (n NoOpVisitor) VisitEnumNode(_ *EnumNode) error
- func (n NoOpVisitor) VisitEnumValueNode(_ *EnumValueNode) error
- func (n NoOpVisitor) VisitExtendNode(_ *ExtendNode) error
- func (n NoOpVisitor) VisitExtensionRangeNode(_ *ExtensionRangeNode) error
- func (n NoOpVisitor) VisitFieldNode(_ *FieldNode) error
- func (n NoOpVisitor) VisitFieldReferenceNode(_ *FieldReferenceNode) error
- func (n NoOpVisitor) VisitFileNode(_ *FileNode) error
- func (n NoOpVisitor) VisitFloatLiteralNode(_ *FloatLiteralNode) error
- func (n NoOpVisitor) VisitGroupNode(_ *GroupNode) error
- func (n NoOpVisitor) VisitIdentNode(_ *IdentNode) error
- func (n NoOpVisitor) VisitImportNode(_ *ImportNode) error
- func (n NoOpVisitor) VisitKeywordNode(_ *KeywordNode) error
- func (n NoOpVisitor) VisitMapFieldNode(_ *MapFieldNode) error
- func (n NoOpVisitor) VisitMapTypeNode(_ *MapTypeNode) error
- func (n NoOpVisitor) VisitMessageFieldNode(_ *MessageFieldNode) error
- func (n NoOpVisitor) VisitMessageLiteralNode(_ *MessageLiteralNode) error
- func (n NoOpVisitor) VisitMessageNode(_ *MessageNode) error
- func (n NoOpVisitor) VisitNegativeIntLiteralNode(_ *NegativeIntLiteralNode) error
- func (n NoOpVisitor) VisitOneofNode(_ *OneofNode) error
- func (n NoOpVisitor) VisitOptionNameNode(_ *OptionNameNode) error
- func (n NoOpVisitor) VisitOptionNode(_ *OptionNode) error
- func (n NoOpVisitor) VisitPackageNode(_ *PackageNode) error
- func (n NoOpVisitor) VisitRPCNode(_ *RPCNode) error
- func (n NoOpVisitor) VisitRPCTypeNode(_ *RPCTypeNode) error
- func (n NoOpVisitor) VisitRangeNode(_ *RangeNode) error
- func (n NoOpVisitor) VisitReservedNode(_ *ReservedNode) error
- func (n NoOpVisitor) VisitRuneNode(_ *RuneNode) error
- func (n NoOpVisitor) VisitServiceNode(_ *ServiceNode) error
- func (n NoOpVisitor) VisitSignedFloatLiteralNode(_ *SignedFloatLiteralNode) error
- func (n NoOpVisitor) VisitSpecialFloatLiteralNode(_ *SpecialFloatLiteralNode) error
- func (n NoOpVisitor) VisitStringLiteralNode(_ *StringLiteralNode) error
- func (n NoOpVisitor) VisitSyntaxNode(_ *SyntaxNode) error
- func (n NoOpVisitor) VisitUintLiteralNode(_ *UintLiteralNode) error
- type NoSourceNode
- func (n *NoSourceNode) End() Token
- func (n *NoSourceNode) FieldExtendee() Node
- func (n *NoSourceNode) FieldLabel() Node
- func (n *NoSourceNode) FieldName() Node
- func (n *NoSourceNode) FieldTag() Node
- func (n *NoSourceNode) FieldType() Node
- func (n *NoSourceNode) GetGroupKeyword() Node
- func (n *NoSourceNode) GetInputType() Node
- func (n *NoSourceNode) GetName() Node
- func (n *NoSourceNode) GetNumber() Node
- func (n *NoSourceNode) GetOptions() *CompactOptionsNode
- func (n *NoSourceNode) GetOutputType() Node
- func (n *NoSourceNode) GetSyntax() Node
- func (n *NoSourceNode) GetValue() ValueNode
- func (n *NoSourceNode) MessageName() Node
- func (n *NoSourceNode) Name() string
- func (n *NoSourceNode) NodeInfo(Node) NodeInfo
- func (n *NoSourceNode) OneofName() Node
- func (n *NoSourceNode) RangeEnd() Node
- func (n *NoSourceNode) RangeOptions(func(*OptionNode) bool)
- func (n *NoSourceNode) RangeStart() Node
- func (n *NoSourceNode) Start() Token
- func (n *NoSourceNode) Value() interface{}
- type Node
- type NodeInfo
- type NodeWithOptions
- type OneofDeclNode
- type OneofElement
- type OneofNode
- type OptionDeclNode
- type OptionNameNode
- type OptionNode
- type PackageNode
- type RPCDeclNode
- type RPCElement
- type RPCNode
- type RPCTypeNode
- type RangeDeclNode
- type RangeNode
- func (n *RangeNode) Children() []Node
- func (n *RangeNode) End() Token
- func (n *RangeNode) EndValue() interface{}
- func (n *RangeNode) EndValueAsInt32(minVal, maxVal int32) (int32, bool)
- func (n *RangeNode) RangeEnd() Node
- func (n *RangeNode) RangeStart() Node
- func (n *RangeNode) Start() Token
- func (n *RangeNode) StartValue() interface{}
- func (n *RangeNode) StartValueAsInt32(minVal, maxVal int32) (int32, bool)
- type ReservedNode
- func NewReservedIdentifiersNode(keyword *KeywordNode, names []*IdentNode, commas []*RuneNode, ...) *ReservedNode
- func NewReservedNamesNode(keyword *KeywordNode, names []StringValueNode, commas []*RuneNode, ...) *ReservedNode
- func NewReservedRangesNode(keyword *KeywordNode, ranges []*RangeNode, commas []*RuneNode, ...) *ReservedNode
- type RuneNode
- type Sequence
- type ServiceElement
- type ServiceNode
- type SignedFloatLiteralNode
- type SimpleVisitor
- func (v *SimpleVisitor) VisitArrayLiteralNode(node *ArrayLiteralNode) error
- func (v *SimpleVisitor) VisitCompactOptionsNode(node *CompactOptionsNode) error
- func (v *SimpleVisitor) VisitCompoundIdentNode(node *CompoundIdentNode) error
- func (v *SimpleVisitor) VisitCompoundStringLiteralNode(node *CompoundStringLiteralNode) error
- func (v *SimpleVisitor) VisitEditionNode(node *EditionNode) error
- func (v *SimpleVisitor) VisitEmptyDeclNode(node *EmptyDeclNode) error
- func (v *SimpleVisitor) VisitEnumNode(node *EnumNode) error
- func (v *SimpleVisitor) VisitEnumValueNode(node *EnumValueNode) error
- func (v *SimpleVisitor) VisitExtendNode(node *ExtendNode) error
- func (v *SimpleVisitor) VisitExtensionRangeNode(node *ExtensionRangeNode) error
- func (v *SimpleVisitor) VisitFieldNode(node *FieldNode) error
- func (v *SimpleVisitor) VisitFieldReferenceNode(node *FieldReferenceNode) error
- func (v *SimpleVisitor) VisitFileNode(node *FileNode) error
- func (v *SimpleVisitor) VisitFloatLiteralNode(node *FloatLiteralNode) error
- func (v *SimpleVisitor) VisitGroupNode(node *GroupNode) error
- func (v *SimpleVisitor) VisitIdentNode(node *IdentNode) error
- func (v *SimpleVisitor) VisitImportNode(node *ImportNode) error
- func (v *SimpleVisitor) VisitKeywordNode(node *KeywordNode) error
- func (v *SimpleVisitor) VisitMapFieldNode(node *MapFieldNode) error
- func (v *SimpleVisitor) VisitMapTypeNode(node *MapTypeNode) error
- func (v *SimpleVisitor) VisitMessageFieldNode(node *MessageFieldNode) error
- func (v *SimpleVisitor) VisitMessageLiteralNode(node *MessageLiteralNode) error
- func (v *SimpleVisitor) VisitMessageNode(node *MessageNode) error
- func (v *SimpleVisitor) VisitNegativeIntLiteralNode(node *NegativeIntLiteralNode) error
- func (v *SimpleVisitor) VisitOneofNode(node *OneofNode) error
- func (v *SimpleVisitor) VisitOptionNameNode(node *OptionNameNode) error
- func (v *SimpleVisitor) VisitOptionNode(node *OptionNode) error
- func (v *SimpleVisitor) VisitPackageNode(node *PackageNode) error
- func (v *SimpleVisitor) VisitRPCNode(node *RPCNode) error
- func (v *SimpleVisitor) VisitRPCTypeNode(node *RPCTypeNode) error
- func (v *SimpleVisitor) VisitRangeNode(node *RangeNode) error
- func (v *SimpleVisitor) VisitReservedNode(node *ReservedNode) error
- func (v *SimpleVisitor) VisitRuneNode(node *RuneNode) error
- func (v *SimpleVisitor) VisitServiceNode(node *ServiceNode) error
- func (v *SimpleVisitor) VisitSignedFloatLiteralNode(node *SignedFloatLiteralNode) error
- func (v *SimpleVisitor) VisitSpecialFloatLiteralNode(node *SpecialFloatLiteralNode) error
- func (v *SimpleVisitor) VisitStringLiteralNode(node *StringLiteralNode) error
- func (v *SimpleVisitor) VisitSyntaxNode(node *SyntaxNode) error
- func (v *SimpleVisitor) VisitUintLiteralNode(node *UintLiteralNode) error
- type SourcePos
- type SourceSpan
- type SpecialFloatLiteralNode
- type StringLiteralNode
- type StringValueNode
- type SyntaxNode
- type SyntheticGroupMessageNode
- type SyntheticMapEntryNode
- type SyntheticMapField
- func (n *SyntheticMapField) End() Token
- func (n *SyntheticMapField) FieldExtendee() Node
- func (n *SyntheticMapField) FieldLabel() Node
- func (n *SyntheticMapField) FieldName() Node
- func (n *SyntheticMapField) FieldTag() Node
- func (n *SyntheticMapField) FieldType() Node
- func (n *SyntheticMapField) GetGroupKeyword() Node
- func (n *SyntheticMapField) GetOptions() *CompactOptionsNode
- func (n *SyntheticMapField) LeadingComments() []Comment
- func (n *SyntheticMapField) RangeOptions(_ func(*OptionNode) bool)
- func (n *SyntheticMapField) Start() Token
- func (n *SyntheticMapField) TrailingComments() []Comment
- type SyntheticOneof
- type TerminalNode
- type Token
- type UintLiteralNode
- func (n *UintLiteralNode) AsFloat() float64
- func (n *UintLiteralNode) AsInt64() (int64, bool)
- func (n *UintLiteralNode) AsUint64() (uint64, bool)
- func (n UintLiteralNode) End() Token
- func (n UintLiteralNode) Start() Token
- func (n UintLiteralNode) Token() Token
- func (n *UintLiteralNode) Value() interface{}
- type ValueNode
- type Visitor
- type WalkOption
Constants ¶
const TokenError = Token(-1)
TokenError indicates an invalid token. It is returned from query functions when no valid token satisfies the request.
Variables ¶
var EmptyComments = Comments{}
EmptyComments is an empty set of comments.
Functions ¶
func AsInt32 ¶
func AsInt32(n IntValueNode, minVal, maxVal 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 ¶
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) Value ¶
func (n *ArrayLiteralNode) 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. A single comment means one line-style comment ("//" to end of line) or one block comment ("/*" through "*/"). If a longer comment uses multiple line comments, each line is considered to be a separate comment. For example:
// This is a single comment, and // this is a separate comment.
func (Comment) IsValid ¶
IsValid returns true if this comment is valid. If this comment is a zero-value struct, it is not valid.
func (Comment) LeadingWhitespace ¶
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 items or AST nodes).
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) GetElements ¶
func (e *CompactOptionsNode) GetElements() []*OptionNode
type CompositeNode ¶
type CompositeNode interface { Node // Children contains 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) 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) Value ¶
func (n *CompoundStringLiteralNode) Value() interface{}
type EditionNode ¶ added in v0.7.0
type EditionNode struct { Keyword *KeywordNode Equals *RuneNode Edition StringValueNode Semicolon *RuneNode // contains filtered or unexported fields }
EditionNode represents an edition declaration, which if present must be the first non-comment content. Example:
edition = "2023";
Files may include either an edition node or a syntax node, but not both. If neither are present, the file is assumed to use proto2 syntax.
func NewEditionNode ¶ added in v0.7.0
func NewEditionNode(keyword *KeywordNode, equals *RuneNode, edition StringValueNode, semicolon *RuneNode) *EditionNode
NewEditionNode creates a new *EditionNode. All four arguments must be non-nil:
- keyword: The token corresponding to the "edition" keyword.
- equals: The token corresponding to the "=" rune.
- edition: The actual edition value, e.g. "2023".
- semicolon: The token corresponding to the ";" rune that ends the declaration.
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.
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) RangeOptions ¶ added in v0.10.0
func (n *EnumNode) RangeOptions(fn func(*OptionNode) bool)
type EnumValueDeclNode ¶
type EnumValueDeclNode interface { NodeWithOptions 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 }
EnumValueNode 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) GetName ¶
func (e *EnumValueNode) GetName() Node
func (*EnumValueNode) GetNumber ¶
func (e *EnumValueNode) GetNumber() Node
func (*EnumValueNode) RangeOptions ¶ added in v0.10.0
func (e *EnumValueNode) RangeOptions(fn func(*OptionNode) bool)
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.
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) RangeOptions ¶ added in v0.10.0
func (e *ExtensionRangeNode) RangeOptions(fn func(*OptionNode) bool)
type FieldDeclNode ¶
type FieldDeclNode interface { NodeWithOptions 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
- *SyntheticMapField
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) FieldExtendee ¶
func (*FieldNode) FieldLabel ¶
func (*FieldNode) GetGroupKeyword ¶
func (*FieldNode) GetOptions ¶
func (n *FieldNode) GetOptions() *CompactOptionsNode
func (*FieldNode) RangeOptions ¶ added in v0.10.0
func (n *FieldNode) RangeOptions(fn func(*OptionNode) bool)
type FieldReferenceNode ¶
type FieldReferenceNode struct { Open *RuneNode // only present for extension names and "any" type references // only present for "any" type references URLPrefix IdentValueNode Slash *RuneNode Name IdentValueNode Close *RuneNode // only present for extension names and "any" type references // contains filtered or unexported fields }
FieldReferenceNode is a reference to a field name. It can indicate a regular field (simple unqualified name), an extension field (possibly-qualified name that is enclosed either in brackets or parentheses), or an "any" type reference (a type URL in the form "server.host/fully.qualified.Name" that is enclosed in brackets).
Extension names are used in options to refer to custom options (which are actually extensions), in which case the name is enclosed in parentheses "(" and ")". They can also be used to refer to extension fields of options.
Extension names are also used in message literals to set extension fields, in which case the name is enclosed in square brackets "[" and "]".
"Any" type references can only be used in message literals, and are not allowed in option names. They are always enclosed in square brackets. An "any" type reference is distinguished from an extension name by the presence of a slash, which must be present in an "any" type reference and must be absent in an extension name.
Examples:
foobar (foo.bar) [foo.bar] [type.googleapis.com/foo.bar]
func NewAnyTypeReferenceNode ¶
func NewAnyTypeReferenceNode(openSym *RuneNode, urlPrefix IdentValueNode, slashSym *RuneNode, name IdentValueNode, closeSym *RuneNode) *FieldReferenceNode
NewAnyTypeReferenceNode creates a new *FieldReferenceNode for an "any" type reference. All args must be non-nil. The openSym and closeSym runes should be "[" and "]". The slashSym run should be "/".
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) IsAnyTypeReference ¶
func (a *FieldReferenceNode) IsAnyTypeReference() bool
IsAnyTypeReference reports if this is an Any type reference.
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) Value ¶
func (a *FieldReferenceNode) Value() string
type FileDeclNode ¶
type FileDeclNode interface { NodeWithOptions Name() string 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 items. A lexer accumulates these details as it scans the file contents. This allows efficient representation of things like source positions.
func NewFileInfo ¶
NewFileInfo creates a new instance for the given file.
func (*FileInfo) AddComment ¶
AddComment adds info about a comment to this file. Comments must first be added as items via f.AddToken(). The given comment argument is the Token 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 ¶
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 ¶
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) GetItem ¶
GetItem returns the token or comment represented by the given item. Only one of the return values will be valid. If the item is a token then the returned comment will be a zero value and thus invalid (i.e. comment.IsValid() returns false). If the item is a comment then the returned token will be TokenError.
If the given i is out of range, this returns (TokenError, Comment{}). If the given i is not out of range but also from a different file than f, then the result is undefined.
func (*FileInfo) ItemInfo ¶
ItemInfo returns details from the original source for the given item.
If the given i is out of range, this returns nil. If the given i is not out of range but also from a different file than f, then the result is undefined.
func (*FileInfo) NodeInfo ¶
NodeInfo returns details from the original source for the given AST node.
If the given n is out of range, this returns an invalid NodeInfo (i.e. nodeInfo.IsValid() returns false). If the given n is not out of range but also from a different file than f, then the result is undefined.
func (*FileInfo) TokenInfo ¶
TokenInfo returns details from the original source for the given token.
If the given t is out of range, this returns an invalid NodeInfo (i.e. nodeInfo.IsValid() returns false). If the given t is not out of range but also from a different file than f, then the result is undefined.
type FileNode ¶
type FileNode struct { // A file has either a Syntax or Edition node, never both. // If both are nil, neither declaration is present and the // file is assumed to use "proto2" syntax. Syntax *SyntaxNode Edition *EditionNode 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 ¶
NewEmptyFileNode returns an empty AST for a file with the given name.
func NewFileNode ¶
func NewFileNode(info *FileInfo, syntax *SyntaxNode, decls []FileElement, eof Token) *FileNode
NewFileNode 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 NewFileNodeWithEdition ¶ added in v0.7.0
func NewFileNodeWithEdition(info *FileInfo, edition *EditionNode, decls []FileElement, eof Token) *FileNode
NewFileNodeWithEdition creates a new *FileNode. The edition parameter is required. If a file has no edition declaration, use NewFileNode instead.
This function panics if the concrete type of any element of decls is not from this package.
func (*FileNode) RangeOptions ¶ added in v0.10.0
func (f *FileNode) RangeOptions(fn func(*OptionNode) bool)
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) Value ¶
func (n *FloatLiteralNode) Value() interface{}
type FloatValueNode ¶
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) AsMessage ¶ added in v0.10.0
func (n *GroupNode) AsMessage() *SyntheticGroupMessageNode
func (*GroupNode) FieldExtendee ¶
func (*GroupNode) FieldLabel ¶
func (*GroupNode) GetGroupKeyword ¶
func (*GroupNode) GetOptions ¶
func (n *GroupNode) GetOptions() *CompactOptionsNode
func (*GroupNode) RangeOptions ¶ added in v0.10.0
func (n *GroupNode) RangeOptions(fn func(*OptionNode) bool)
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 ¶
NewIdentNode creates a new *IdentNode. The given val is the identifier text.
func (*IdentNode) AsIdentifier ¶
func (n *IdentNode) AsIdentifier() Identifier
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.
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.
type IntValueNode ¶
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 Item ¶
type Item int
Item represents an item lexed from source. It represents either a Token or a Comment.
type ItemInfo ¶
type ItemInfo interface { SourceSpan LeadingWhitespace() string RawText() string }
ItemInfo provides details about an item's location in the source file and its contents.
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) AsMessage ¶ added in v0.10.0
func (n *MapFieldNode) AsMessage() *SyntheticMapEntryNode
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) RangeOptions ¶ added in v0.10.0
func (n *MapFieldNode) RangeOptions(fn func(*OptionNode) bool)
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.
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 { NodeWithOptions 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
- *SyntheticGroupMessageNode (the group is a field and inline message type)
- *SyntheticMapEntryNode (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 or list literal (and thus starts with '<', // '{', or '['), then the separator may be omitted and this field 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.
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) 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) MessageName ¶
func (n *MessageNode) MessageName() Node
func (*MessageNode) RangeOptions ¶ added in v0.10.0
func (n *MessageNode) RangeOptions(fn func(*OptionNode) bool)
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) 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) 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) VisitEditionNode ¶ added in v0.7.0
func (n NoOpVisitor) VisitEditionNode(_ *EditionNode) 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 ¶ added in v0.6.0
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) 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 FileInfo
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) OneofName ¶ added in v0.10.0
func (n *NoSourceNode) OneofName() Node
func (*NoSourceNode) RangeEnd ¶
func (n *NoSourceNode) RangeEnd() Node
func (*NoSourceNode) RangeOptions ¶ added in v0.10.0
func (n *NoSourceNode) RangeOptions(func(*OptionNode) bool)
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 ¶
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 or token in the source file's AST. It provides access to information about the node's location in the source file. It also provides access to the original text in the source file (with all the original formatting intact) and also provides access to surrounding comments.
func (NodeInfo) End ¶
End returns the ending position of the element, exclusive. This is the location after the last character of the node or token. If n returns the same position for Start() and End(), the element in source had a length of zero (which should only happen for the special EOF token that designates the end of the file).
func (NodeInfo) IsValid ¶
IsValid returns true if this node info is valid. If n is a zero-value struct, it is not valid.
func (NodeInfo) LeadingComments ¶
LeadingComments returns all comments in the source that exist between the element and the previous element, except for any trailing comment on the previous element.
func (NodeInfo) LeadingWhitespace ¶
LeadingWhitespace returns any whitespace prior to the element. If there were comments in between this element and the previous one, this will return the whitespace between the last such comment in the element. If there were no such comments, this returns the whitespace between the previous element and the current one.
func (NodeInfo) RawText ¶
RawText returns the actual text in the source file that corresponds to the element. If the element is a node in the AST that encompasses multiple items (like an entire declaration), the full text of all items is returned including any interior whitespace and comments.
func (NodeInfo) Start ¶
Start returns the starting position of the element. This is the first character of the node or token.
func (NodeInfo) TrailingComments ¶
TrailingComments returns the trailing comment for the element, if any. An element will have a trailing comment only if it is the last token on a line and is followed by a comment on the same line. Typically, the following comment is a line-style comment (starting with "//").
If the following comment is a block-style comment that spans multiple lines, and the next token is on the same line as the end of the comment, the comment is NOT considered a trailing comment.
Examples:
foo // this is a trailing comment for foo bar /* this is a trailing comment for bar */ baz /* this is a trailing comment for baz */ fizz /* this is NOT a trailing comment for fizz because its on the same line as the following token buzz */ buzz
type NodeWithOptions ¶ added in v0.10.0
type NodeWithOptions interface { Node RangeOptions(func(*OptionNode) bool) }
NodeWithOptions represents a node in the AST that contains option statements.
type OneofDeclNode ¶ added in v0.6.0
type OneofDeclNode interface { NodeWithOptions OneofName() Node }
OneofDeclNode is a node in the AST that defines a oneof. There are multiple types of AST nodes that declare oneofs:
- *OneofNode
- *SyntheticOneof
This also allows NoSourceNode to be used in place of one of the above for some usages.
type OneofElement ¶ added in v0.6.0
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 ¶ added in v0.6.0
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 ¶ added in v0.6.0
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) RangeOptions ¶ added in v0.10.0
func (n *OneofNode) RangeOptions(fn func(*OptionNode) bool)
type OptionDeclNode ¶
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.
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) GetName ¶
func (n *OptionNode) GetName() Node
func (*OptionNode) GetValue ¶
func (n *OptionNode) GetValue() ValueNode
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.
type RPCDeclNode ¶
type RPCDeclNode interface { NodeWithOptions 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) GetInputType ¶
func (*RPCNode) GetOutputType ¶
func (*RPCNode) RangeOptions ¶ added in v0.10.0
func (n *RPCNode) RangeOptions(fn func(*OptionNode) bool)
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.
type RangeDeclNode ¶
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, maxEnd *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) EndValueAsInt32 ¶
func (*RangeNode) RangeStart ¶
func (*RangeNode) StartValue ¶
func (n *RangeNode) StartValue() interface{}
type ReservedNode ¶
type ReservedNode struct { Keyword *KeywordNode // If non-empty, this node represents reserved ranges, and Names and Identifiers // will be empty. Ranges []*RangeNode // If non-empty, this node represents reserved names as string literals, and // Ranges and Identifiers will be empty. String literals are used for reserved // names in proto2 and proto3 syntax. Names []StringValueNode // If non-empty, this node represents reserved names as identifiers, and Ranges // and Names will be empty. Identifiers are used for reserved names in editions. Identifiers []*IdentNode // 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"; reserved foo, bar, baz;
func NewReservedIdentifiersNode ¶ added in v0.7.0
func NewReservedIdentifiersNode(keyword *KeywordNode, names []*IdentNode, commas []*RuneNode, semicolon *RuneNode) *ReservedNode
NewReservedIdentifiersNode 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 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.
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 items, 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 ¶
NewRuneNode creates a new *RuneNode with the given properties.
type Sequence ¶
type Sequence[T any] interface { // First returns the first element in the sequence. The bool return // is false if this sequence contains no elements. For example, an // empty file has no items or tokens. First() (T, bool) // Next returns the next element in the sequence that comes after // the given element. The bool returns is false if there is no next // item (i.e. the given element is the last one). It also returns // false if the given element is invalid. Next(T) (T, bool) // Last returns the last element in the sequence. The bool return // is false if this sequence contains no elements. For example, an // empty file has no items or tokens. Last() (T, bool) // Previous returns the previous element in the sequence that comes // before the given element. The bool returns is false if there is no // previous item (i.e. the given element is the first one). It also // returns false if the given element is invalid. Previous(T) (T, bool) }
Sequence represents a navigable sequence of elements.
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) RangeOptions ¶ added in v0.10.0
func (n *ServiceNode) RangeOptions(fn func(*OptionNode) bool)
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) Value ¶
func (n *SignedFloatLiteralNode) Value() interface{}
type SimpleVisitor ¶
type SimpleVisitor struct { DoVisitFileNode func(*FileNode) error DoVisitSyntaxNode func(*SyntaxNode) error DoVisitEditionNode func(*EditionNode) 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 DoVisitNegativeIntLiteralNode func(*NegativeIntLiteralNode) error DoVisitFloatLiteralNode func(*FloatLiteralNode) error DoVisitSpecialFloatLiteralNode func(*SpecialFloatLiteralNode) error DoVisitSignedFloatLiteralNode func(*SignedFloatLiteralNode) 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 DoVisit*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 and *GroupNode implement both FieldDeclNode and MessageDeclNode. In this case, the DoVisitFieldDeclNode function is treated as more specific than DoVisitMessageDeclNode, so will be preferred if both are present.
func (*SimpleVisitor) VisitArrayLiteralNode ¶
func (v *SimpleVisitor) VisitArrayLiteralNode(node *ArrayLiteralNode) error
func (*SimpleVisitor) VisitCompactOptionsNode ¶
func (v *SimpleVisitor) VisitCompactOptionsNode(node *CompactOptionsNode) error
func (*SimpleVisitor) VisitCompoundIdentNode ¶
func (v *SimpleVisitor) VisitCompoundIdentNode(node *CompoundIdentNode) error
func (*SimpleVisitor) VisitCompoundStringLiteralNode ¶
func (v *SimpleVisitor) VisitCompoundStringLiteralNode(node *CompoundStringLiteralNode) error
func (*SimpleVisitor) VisitEditionNode ¶ added in v0.7.0
func (v *SimpleVisitor) VisitEditionNode(node *EditionNode) error
func (*SimpleVisitor) VisitEmptyDeclNode ¶
func (v *SimpleVisitor) VisitEmptyDeclNode(node *EmptyDeclNode) error
func (*SimpleVisitor) VisitEnumNode ¶
func (v *SimpleVisitor) VisitEnumNode(node *EnumNode) error
func (*SimpleVisitor) VisitEnumValueNode ¶
func (v *SimpleVisitor) VisitEnumValueNode(node *EnumValueNode) error
func (*SimpleVisitor) VisitExtendNode ¶
func (v *SimpleVisitor) VisitExtendNode(node *ExtendNode) error
func (*SimpleVisitor) VisitExtensionRangeNode ¶
func (v *SimpleVisitor) VisitExtensionRangeNode(node *ExtensionRangeNode) error
func (*SimpleVisitor) VisitFieldNode ¶
func (v *SimpleVisitor) VisitFieldNode(node *FieldNode) error
func (*SimpleVisitor) VisitFieldReferenceNode ¶
func (v *SimpleVisitor) VisitFieldReferenceNode(node *FieldReferenceNode) error
func (*SimpleVisitor) VisitFileNode ¶
func (v *SimpleVisitor) VisitFileNode(node *FileNode) error
func (*SimpleVisitor) VisitFloatLiteralNode ¶
func (v *SimpleVisitor) VisitFloatLiteralNode(node *FloatLiteralNode) error
func (*SimpleVisitor) VisitGroupNode ¶
func (v *SimpleVisitor) VisitGroupNode(node *GroupNode) error
func (*SimpleVisitor) VisitIdentNode ¶
func (v *SimpleVisitor) VisitIdentNode(node *IdentNode) error
func (*SimpleVisitor) VisitImportNode ¶
func (v *SimpleVisitor) VisitImportNode(node *ImportNode) error
func (*SimpleVisitor) VisitKeywordNode ¶
func (v *SimpleVisitor) VisitKeywordNode(node *KeywordNode) error
func (*SimpleVisitor) VisitMapFieldNode ¶
func (v *SimpleVisitor) VisitMapFieldNode(node *MapFieldNode) error
func (*SimpleVisitor) VisitMapTypeNode ¶
func (v *SimpleVisitor) VisitMapTypeNode(node *MapTypeNode) error
func (*SimpleVisitor) VisitMessageFieldNode ¶
func (v *SimpleVisitor) VisitMessageFieldNode(node *MessageFieldNode) error
func (*SimpleVisitor) VisitMessageLiteralNode ¶
func (v *SimpleVisitor) VisitMessageLiteralNode(node *MessageLiteralNode) error
func (*SimpleVisitor) VisitMessageNode ¶
func (v *SimpleVisitor) VisitMessageNode(node *MessageNode) error
func (*SimpleVisitor) VisitNegativeIntLiteralNode ¶
func (v *SimpleVisitor) VisitNegativeIntLiteralNode(node *NegativeIntLiteralNode) error
func (*SimpleVisitor) VisitOneofNode ¶ added in v0.6.0
func (v *SimpleVisitor) VisitOneofNode(node *OneofNode) error
func (*SimpleVisitor) VisitOptionNameNode ¶
func (v *SimpleVisitor) VisitOptionNameNode(node *OptionNameNode) error
func (*SimpleVisitor) VisitOptionNode ¶
func (v *SimpleVisitor) VisitOptionNode(node *OptionNode) error
func (*SimpleVisitor) VisitPackageNode ¶
func (v *SimpleVisitor) VisitPackageNode(node *PackageNode) error
func (*SimpleVisitor) VisitRPCNode ¶
func (v *SimpleVisitor) VisitRPCNode(node *RPCNode) error
func (*SimpleVisitor) VisitRPCTypeNode ¶
func (v *SimpleVisitor) VisitRPCTypeNode(node *RPCTypeNode) error
func (*SimpleVisitor) VisitRangeNode ¶
func (v *SimpleVisitor) VisitRangeNode(node *RangeNode) error
func (*SimpleVisitor) VisitReservedNode ¶
func (v *SimpleVisitor) VisitReservedNode(node *ReservedNode) error
func (*SimpleVisitor) VisitRuneNode ¶
func (v *SimpleVisitor) VisitRuneNode(node *RuneNode) error
func (*SimpleVisitor) VisitServiceNode ¶
func (v *SimpleVisitor) VisitServiceNode(node *ServiceNode) error
func (*SimpleVisitor) VisitSignedFloatLiteralNode ¶
func (v *SimpleVisitor) VisitSignedFloatLiteralNode(node *SignedFloatLiteralNode) error
func (*SimpleVisitor) VisitSpecialFloatLiteralNode ¶
func (v *SimpleVisitor) VisitSpecialFloatLiteralNode(node *SpecialFloatLiteralNode) error
func (*SimpleVisitor) VisitStringLiteralNode ¶
func (v *SimpleVisitor) VisitStringLiteralNode(node *StringLiteralNode) error
func (*SimpleVisitor) VisitSyntaxNode ¶
func (v *SimpleVisitor) VisitSyntaxNode(node *SyntaxNode) error
func (*SimpleVisitor) VisitUintLiteralNode ¶
func (v *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 ¶
UnknownPos is a placeholder position when only the source file name is known.
type SourceSpan ¶ added in v0.7.0
SourceSpan represents a range of source positions.
func NewSourceSpan ¶ added in v0.7.0
func NewSourceSpan(start SourcePos, end SourcePos) SourceSpan
NewSourceSpan creates a new span that covers the given range.
func UnknownSpan ¶ added in v0.7.0
func UnknownSpan(filename string) SourceSpan
UnknownSpan is a placeholder span when only the source file name is known.
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. The given keyword should be "inf", "infinity", or "nan" in any case.
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) Value ¶
func (n *StringLiteralNode) Value() interface{}
type StringValueNode ¶
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.
type SyntheticGroupMessageNode ¶ added in v0.10.0
type SyntheticGroupMessageNode GroupNode
SyntheticGroupMessageNode is a view of a GroupNode that implements MessageDeclNode. Since a group field implicitly defines a message type, this node represents that message type while the corresponding GroupNode represents the field.
This type is considered synthetic since it never appears in a file's AST, but is only returned from other accessors (e.g. GroupNode.AsMessage).
func (*SyntheticGroupMessageNode) MessageName ¶ added in v0.10.0
func (n *SyntheticGroupMessageNode) MessageName() Node
func (*SyntheticGroupMessageNode) RangeOptions ¶ added in v0.10.0
func (n *SyntheticGroupMessageNode) RangeOptions(fn func(*OptionNode) bool)
type SyntheticMapEntryNode ¶ added in v0.10.0
type SyntheticMapEntryNode MapFieldNode
SyntheticMapEntryNode is a view of a MapFieldNode that implements MessageDeclNode. Since a map field implicitly defines a message type for the map entry, this node represents that message type.
This type is considered synthetic since it never appears in a file's AST, but is only returned from other accessors (e.g. MapFieldNode.AsMessage).
func (*SyntheticMapEntryNode) MessageName ¶ added in v0.10.0
func (n *SyntheticMapEntryNode) MessageName() Node
func (*SyntheticMapEntryNode) RangeOptions ¶ added in v0.10.0
func (n *SyntheticMapEntryNode) RangeOptions(_ func(*OptionNode) bool)
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.
This type is considered synthetic since it never appears in a file's AST, but is only returned from other accessors and functions (e.g. MapFieldNode.KeyField, MapFieldNode.ValueField, and NewSyntheticMapField).
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) RangeOptions ¶ added in v0.10.0
func (n *SyntheticMapField) RangeOptions(_ func(*OptionNode) bool)
func (*SyntheticMapField) Start ¶
func (n *SyntheticMapField) Start() Token
func (*SyntheticMapField) TrailingComments ¶
func (n *SyntheticMapField) TrailingComments() []Comment
type SyntheticOneof ¶ added in v0.6.0
type SyntheticOneof struct { // The proto3 optional field that implies the presence of this oneof. Field *FieldNode }
SyntheticOneof is not an actual node in the AST but a synthetic node that represents the oneof implied by a proto3 optional field.
This type is considered synthetic since it never appears in a file's AST, but is only returned from other functions (e.g. NewSyntheticOneof).
func NewSyntheticOneof ¶ added in v0.6.0
func NewSyntheticOneof(field *FieldNode) *SyntheticOneof
NewSyntheticOneof creates a new *SyntheticOneof that corresponds to the given proto3 optional field.
func (*SyntheticOneof) End ¶ added in v0.6.0
func (n *SyntheticOneof) End() Token
func (*SyntheticOneof) LeadingComments ¶ added in v0.6.0
func (n *SyntheticOneof) LeadingComments() []Comment
func (*SyntheticOneof) OneofName ¶ added in v0.6.0
func (n *SyntheticOneof) OneofName() Node
func (*SyntheticOneof) RangeOptions ¶ added in v0.10.0
func (n *SyntheticOneof) RangeOptions(_ func(*OptionNode) bool)
func (*SyntheticOneof) Start ¶ added in v0.6.0
func (n *SyntheticOneof) Start() Token
func (*SyntheticOneof) TrailingComments ¶ added in v0.6.0
func (n *SyntheticOneof) TrailingComments() []Comment
type TerminalNode ¶
TerminalNode represents a leaf in the AST. These represent the items/lexemes in the protobuf language. Comments and whitespace are accumulated by the lexer and associated with the following 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) 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 // // If the ValueNode is a NoSourceNode, indicating that there is no actual // source code (and thus not AST information), then this method always // returns nil. 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 // VisitEditionNode is invoked when visiting an *EditionNode in the AST. VisitEditionNode(*EditionNode) 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 // 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 // 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 // contains filtered or unexported methods }
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.
NOTE: As the language evolves, new methods may be added to this interface to correspond to new grammar elements. That is why it cannot be directly implemented outside this package. Visitor implementations must embed NoOpVisitor and then implement the subset of methods of interest. If such an implementation is used with an AST that has newer elements, the visitor will not do anything in response to the new node types.
An alternative to embedding NoOpVisitor is to use an instance of SimpleVisitor.
Visitors can be supplied to a Walk operation or passed to a call to Visit or VisitChildren.
Note that there are some AST node types defined in this package that do not have corresponding visit methods. These are synthetic node types, that have specialized use from the parser, but never appear in an actual AST (which is always rooted at FileNode). These include SyntheticMapField, SyntheticOneof, SyntheticGroupMessageNode, and SyntheticMapEntryNode.
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.