ast

package
v0.4.4-rc2 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2024 License: MPL-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package ast provides low level parsing facilities for HCL configuration. It includes extra features like block merging, specific to Terramate needs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AsHCLAttributes

func AsHCLAttributes(syntaxAttrs hclsyntax.Attributes) hhcl.Attributes

AsHCLAttributes converts hclsyntax.Attributes to hcl.Attributes.

func CloneExpr

func CloneExpr(expr hclsyntax.Expression) hclsyntax.Expression

CloneExpr clones the given expression.

func ParseExpression

func ParseExpression(str string, filename string) (hcl.Expression, error)

ParseExpression parses the expression str.

func SortRawAttributes

func SortRawAttributes(attrs hhcl.Attributes) []*hhcl.Attribute

SortRawAttributes sorts the raw attributes.

func TokensForExpression

func TokensForExpression(expr hcl.Expression) hclwrite.Tokens

TokensForExpression generates valid tokens for the given expression.

func TokensForValue

func TokensForValue(value cty.Value) hclwrite.Tokens

TokensForValue returns the tokens for the provided value.

Types

type Attribute

type Attribute struct {
	Range info.Range
	*hhcl.Attribute
}

Attribute represents a parsed attribute.

func NewAttribute

func NewAttribute(rootdir string, val *hhcl.Attribute) Attribute

NewAttribute creates a new attribute given a parsed attribute and the rootdir of the project.

type AttributeSlice

type AttributeSlice []Attribute

AttributeSlice is an sortable Attribute slice.

func (AttributeSlice) Len

func (a AttributeSlice) Len() int

Len returns the size of the attributes slice.

func (AttributeSlice) Less

func (a AttributeSlice) Less(i, j int) bool

Less returns true if i < j, false otherwise.

func (AttributeSlice) Swap

func (a AttributeSlice) Swap(i, j int)

Swap swaps i and j.

type Attributes

type Attributes map[string]Attribute

Attributes represents multiple parsed attributes.

func NewAttributes

func NewAttributes(rootdir string, rawAttrs hhcl.Attributes) Attributes

NewAttributes creates a map of Attributes from the raw hcl.Attributes.

func (Attributes) SortedList

func (a Attributes) SortedList() AttributeSlice

SortedList returns a sorted list of attributes from the map.

type Block

type Block struct {
	Range      info.Range
	Attributes Attributes
	Blocks     []*Block

	*hclsyntax.Block
}

Block is a wrapper to the hclsyntax.Block but with the file origin. The hclsyntax.Block.Attributes are converted to hcl.Attributes.

func NewBlock

func NewBlock(rootdir string, block *hclsyntax.Block) *Block

NewBlock creates a new block wrapper.

func (*Block) LabelRanges

func (b *Block) LabelRanges() hcl.Range

LabelRanges computes a range between the first and last label in the case the block has labels or a range for the empty space between the block name and the open brace.

type BlockType

type BlockType string

BlockType represents a block type.

type Blocks

type Blocks []*Block

Blocks is a list of block.

func NewBlocks

func NewBlocks(rootdir string, rawblocks hclsyntax.Blocks) Blocks

NewBlocks creates a Block slice from the raw hclsyntax.Block.

type CloneExpression

type CloneExpression struct {
	hclsyntax.Expression
}

CloneExpression is an expression wrapper that

func (*CloneExpression) Value

func (clone *CloneExpression) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics)

Value evaluates the wrapped expression.

type LabelBlockType

type LabelBlockType struct {
	Type      BlockType                       // Type of the block
	Labels    [project.MaxGlobalLabels]string // Labels are the block labels.
	NumLabels int                             // NumLabels is the number of defined labels.
}

LabelBlockType represents a labelled block type.

func NewEmptyLabelBlockType

func NewEmptyLabelBlockType(typ string) LabelBlockType

NewEmptyLabelBlockType returns a new LabelBlockType with empty labels.

func NewLabelBlockType

func NewLabelBlockType(typ string, labels []string) (LabelBlockType, error)

NewLabelBlockType returns a new LabelBlockType.

type MergedBlock

type MergedBlock struct {
	// Type is the block type (or name).
	Type BlockType

	// Labels is the comma-separated list of labels
	Labels []string

	// Attributes are the block's attributes.
	Attributes Attributes

	// RawOrigins is the list of original blocks that contributed to this block.
	RawOrigins Blocks

	// Blocks maps block types to merged blocks.
	Blocks map[LabelBlockType]*MergedBlock

	// RawBlocks keeps a map of block type to original blocks.
	RawBlocks map[string]Blocks
}

MergedBlock represents a block that spans multiple files.

func NewMergedBlock

func NewMergedBlock(typ string, labels []string) *MergedBlock

NewMergedBlock creates a new MergedBlock of type typ.

func (*MergedBlock) MergeBlock

func (mb *MergedBlock) MergeBlock(other *Block, isLabelled bool) error

MergeBlock recursively merges the other block into this one.

func (*MergedBlock) ValidateSubBlocks

func (mb *MergedBlock) ValidateSubBlocks(allowed ...string) error

ValidateSubBlocks checks if the block only has the allowed block types.

type MergedBlocks

type MergedBlocks map[string]*MergedBlock

MergedBlocks maps the block name to the MergedBlock.

func (MergedBlocks) AsBlocks

func (mergedBlocks MergedBlocks) AsBlocks() Blocks

AsBlocks returns a Block list from a MergedBlocks.

type MergedLabelBlocks

type MergedLabelBlocks map[LabelBlockType]*MergedBlock

MergedLabelBlocks maps the block labels/types to the MergedBlock.

func (MergedLabelBlocks) AsBlocks

func (mergedBlocks MergedLabelBlocks) AsBlocks() Blocks

AsBlocks returns a Block list from a MergedBlocks.

func (MergedLabelBlocks) AsList

func (mergedBlocks MergedLabelBlocks) AsList() []*MergedBlock

AsList returns a list of merged blocks sorted by its label strings.

Jump to

Keyboard shortcuts

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