...

Package ast

import "github.com/protocolbuffers/txtpbfmt/ast"
Overview
Index

Overview ▾

Package ast provides data structure representing textproto syntax tree.

func ByFieldName

func ByFieldName(_, ni, nj *Node, isWholeSlice bool) bool

ByFieldName is a NodeLess function that orders nodes by their field name.

func ByFieldValue

func ByFieldValue(_, ni, nj *Node, isWholeSlice bool) bool

ByFieldValue is a NodeLess function that orders adjacent scalar nodes with the same name by their scalar value.

func SortNodes

func SortNodes(parent *Node, ns []*Node, less NodeLess)

SortNodes sorts nodes by the given less function.

type Node

Node represents a field with a value in a proto message, or a comment unattached to a field.

type Node struct {
    // Start describes the start position of the node.
    // For nodes that span entire lines, this is the first character
    // of the first line attributed to the node; possible a whitespace if the node is indented.
    // For nodes that are members of one-line message literals,
    // this is the first non-whitespace character encountered.
    Start Position
    // Lines of comments appearing before the field.
    // Each non-empty line starts with a # and does not contain the trailing newline.
    PreComments []string
    // Name of proto field (eg 'presubmit'). Will be an empty string for comment-only
    // nodes and unqualified messages, e.g.
    //     { name: "first_msg" }
    //     { name: "second_msg" }
    Name string
    // Values, for nodes that don't have children.
    Values []*Value
    // Children for nodes that have children.
    Children []*Node
    // Whether or not this node was deleted by edits.
    Deleted bool
    // Should the colon after the field name be omitted?
    // (e.g. "presubmit: {" vs "presubmit {")
    SkipColon bool
    // Whether or not all children are in the same line.
    // (eg "base { id: "id" }")
    ChildrenSameLine bool
    // Comment in the same line as the "}".
    ClosingBraceComment string
    // End holds the position suitable for inserting new items.
    // For multi-line nodes, this is the first character on the line with the closing brace.
    // For single-line nodes, this is the first character after the last item (usually a space).
    // For non-message nodes, this is Position zero value.
    End Position
    // Keep values in list (e.g "list: [1, 2]").
    ValuesAsList bool
    // Keep children in list (e.g "list: [ { value: 1 }, { value: 2 } ]").
    ChildrenAsList bool
    // Lines of comments appearing after last value inside list.
    // Each non-empty line starts with a # and does not contain the trailing newline.
    // e.g
    // field: [
    //   value
    //   # Comment
    // ]
    PostValuesComments []string
    // Whether the braces used for the children of this node are curly braces or angle brackets.
    IsAngleBracket bool
}

func GetFromPath

func GetFromPath(nodes []*Node, path []string) []*Node

GetFromPath returns all nodes with a given string path in the parse tree. See ast_test.go for examples.

func StringNode

func StringNode(name, unquoted string) *Node

StringNode is a helper for constructing simple string nodes.

func (*Node) Fix

func (n *Node) Fix()

Fix fixes inconsistencies that may arise after manipulation.

For example if a node is ChildrenSameLine but has non-inline children, or children with comments ChildrenSameLine will be set to false.

func (*Node) IsCommentOnly

func (n *Node) IsCommentOnly() bool

IsCommentOnly returns true if this is a comment-only node.

type NodeLess

NodeLess is a sorting function that compares two *Nodes, possibly using the parent Node for context, returning whether a is less than b.

type NodeLess func(parent, a, b *Node, isWholeSlice bool) bool

func ByFieldSubfield

func ByFieldSubfield(field, subfield string) NodeLess

ByFieldSubfield returns a NodeLess function that orders adjacent message nodes with the given field name by the given subfield name value. If no field name is provided, it compares the subfields of any adjacent nodes with matching names.

func ChainNodeLess

func ChainNodeLess(first, second NodeLess) NodeLess

ChainNodeLess combines two NodeLess functions that returns the first comparison if values are not equal, else returns the second.

type Position

Position describes a position of a token in the input. Both byte-based and line/column-based positions are maintained because different downstream consumers need different formats and we don't want to keep the entire input in memory to be able to convert between the two. Fields Byte, Line and Column should be interpreted as ByteRange.start_byte, TextRange.start_line, and TextRange.start_column of devtools.api.Range proto.

type Position struct {
    Byte   uint32
    Line   int32
    Column int32
}

type Value

Value represents a field value in a proto message.

type Value struct {
    // Lines of comments appearing before the value (for multi-line strings).
    // Each non-empty line starts with a # and does not contain the trailing newline.
    PreComments []string
    // Node value (eg 'ERROR').
    Value string
    // Comment in the same line as the value.
    InlineComment string
}

func (*Value) String

func (v *Value) String() string