parse

package
v0.0.0-...-6e5b399 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2025 License: BSD-3-Clause Imports: 5 Imported by: 0

Documentation

Overview

Package parse converts GN syntax tokens into an AST.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RenderDump

func RenderDump(w io.Writer, dump NodeDump) error

RenderDump renders a NodeDump as text, matching GN's output when running `gn format --dump_tree=text`.

Types

type AccessorNode

type AccessorNode struct {
	Base      syntax.Token
	Subscript ParseNode
	Member    *IdentifierNode
}

AccessorNode represents accessing an array or scope element.

func (*AccessorNode) Dump

func (n *AccessorNode) Dump() NodeDump

Dump returns a JSON-serializable of this node.

func (*AccessorNode) LocationRange

func (n *AccessorNode) LocationRange() syntax.LocationRange

LocationRange returns the location range for this node.

type BinaryOpNode

type BinaryOpNode struct {
	Op    syntax.Token
	Left  ParseNode
	Right ParseNode
}

func (*BinaryOpNode) Dump

func (n *BinaryOpNode) Dump() NodeDump

Dump returns a JSON-serializable of this node.

func (*BinaryOpNode) LocationRange

func (n *BinaryOpNode) LocationRange() syntax.LocationRange

type BlockCommentNode

type BlockCommentNode struct {
	Comment syntax.Token
}

BlockCommentNode represents standalone comments (that is, those not specifically attached to another syntax element. The most common of these is a standard header block. This node contains only the last line of such a comment block as the anchor, and other lines of the block comment are hung off of it as Before comments, similar to other syntax elements.

func (*BlockCommentNode) Dump

func (n *BlockCommentNode) Dump() NodeDump

Dump returns a JSON-serializable of this node.

func (*BlockCommentNode) LocationRange

func (n *BlockCommentNode) LocationRange() syntax.LocationRange

LocationRange returns the location range for this node.

type BlockNode

type BlockNode struct {
	// ResultMode sets execution option for handling scope and results.
	ResultMode BlockNodeResultMode
	// BeginToken corresponds to "{" token of this block.
	BeginToken syntax.Token
	// End is the end token of this block.
	End EndNode
	// Statements is the list of statements in this block.
	Statements []ParseNode
}

BlockNode represents a block in the AST.

func (*BlockNode) Dump

func (n *BlockNode) Dump() NodeDump

Dump returns a JSON-serializable of this node.

func (*BlockNode) LocationRange

func (n *BlockNode) LocationRange() syntax.LocationRange

LocationRange returns the location range for this node.

type BlockNodeResultMode

type BlockNodeResultMode int

BlockNodeResultMode sets execution option for the scopes and results.

const (
	// ReturnsScope is option to create new scope for the execution of a block
	// and returning it as a Value.
	ReturnsScope BlockNodeResultMode = iota
	// DiscardResults is option to execute block in the context of the calling
	// scope (variables set will go into the invoking scope) and return an
	// empty Value.
	DiscardsResult
)

type ConditionNode

type ConditionNode struct {
	// IfToken represents the if token this node starts at.
	IfToken syntax.Token
	// Condition represents the conditional for this node.
	Condition ParseNode
	// IfTrue is the block to be executed if the conditional evaluates to true.
	IfTrue *BlockNode
	// IfFalse should be a *BlockNode or *ConditionNode to represent either the "else" block or "else if" condition, respectively.
	IfFalse ParseNode
}

ConditionNode represents a conditional in the AST.

func (*ConditionNode) Dump

func (n *ConditionNode) Dump() NodeDump

Dump returns a JSON-serializable of this node.

func (*ConditionNode) LocationRange

func (n *ConditionNode) LocationRange() syntax.LocationRange

type EndNode

type EndNode struct {
	Value syntax.Token
}

EndNode is used as the end object for lists and blocks (rather than just the end ']', '}', or ')' syntax.Token). This is so that during formatting traversal there is a node that appears at the end of the block to which comments can be attached.

func (*EndNode) Dump

func (n *EndNode) Dump() NodeDump

Dump returns a JSON-serializable of this node.

type FunctionCallNode

type FunctionCallNode struct {
	Function syntax.Token
	Args     *ListNode
	Block    *BlockNode
}

FunctionCallNode represents a function call in the AST.

func (*FunctionCallNode) Dump

func (n *FunctionCallNode) Dump() NodeDump

Dump returns a JSON-serializable of this node.

func (*FunctionCallNode) LocationRange

func (n *FunctionCallNode) LocationRange() syntax.LocationRange

LocationRange returns the location range for this node.

type IdentifierNode

type IdentifierNode struct {
	Value syntax.Token
}

IdentifierNode represents an identifier in the AST.

func (*IdentifierNode) Dump

func (n *IdentifierNode) Dump() NodeDump

Dump returns a JSON-serializable of this node.

func (*IdentifierNode) LocationRange

func (n *IdentifierNode) LocationRange() syntax.LocationRange

LocationRange returns the location range for this node.

type ListNode

type ListNode struct {
	BeginToken syntax.Token
	End        EndNode
	Contents   []ParseNode
}

ListNode represents a list in the AST.

func (*ListNode) Dump

func (n *ListNode) Dump() NodeDump

Dump returns a JSON-serializable of this node.

func (*ListNode) LocationRange

func (n *ListNode) LocationRange() syntax.LocationRange

LocationRange returns the location range for this node.

type LiteralNode

type LiteralNode struct {
	// Token is the token for this literal.
	Token syntax.Token
}

LiteralNode represents a literal in the AST.

func (*LiteralNode) Dump

func (n *LiteralNode) Dump() NodeDump

Dump returns a JSON-serializable of this node.

func (*LiteralNode) LocationRange

func (n *LiteralNode) LocationRange() syntax.LocationRange

LocationRange returns the location range for this node.

type NodeDump

type NodeDump struct {
	Type         string     `json:"type"`
	Value        string     `json:"value,omitempty"`
	Children     []NodeDump `json:"child,omitempty"`
	BeginToken   string     `json:"begin_token,omitempty"`
	End          *NodeDump  `json:"end,omitempty"`
	AccessorKind string     `json:"accessor_kind,omitempty"`
}

NodeDump is a JSON-serializable representation of a ParseNode.

type ParseNode

type ParseNode interface {
	// LocationRange is the file range this node represents.
	LocationRange() syntax.LocationRange
	// Dump returns a JSON-serializable representation of this node.
	Dump() NodeDump
}

ParseNode is a node in the AST.

func Parse

func Parse(tokens []syntax.Token) (ParseNode, error)

Parse converts a series of tokens into an AST.

type UnaryOpNode

type UnaryOpNode struct {
	// Op is the operator token.
	Op syntax.Token
	// Operand represents the operand of the operation.
	Operand ParseNode
}

UnaryOpNode represents a unary operation in the AST.

func (*UnaryOpNode) Dump

func (n *UnaryOpNode) Dump() NodeDump

Dump returns a JSON-serializable of this node.

func (*UnaryOpNode) LocationRange

func (n *UnaryOpNode) LocationRange() syntax.LocationRange

LocationRange returns the location range for this node.

Jump to

Keyboard shortcuts

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