ast

package
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2026 License: Apache-2.0 Imports: 11 Imported by: 202

Documentation

Overview

Package ast declares the types used to represent syntax trees for CUE packages.

Index

Constants

This section is empty.

Variables

View Source
var ErrIsExpression = errors.New("not a concrete label")

ErrIsExpression reports whether a label is an expression. This error is never returned directly. Use errors.Is.

Functions

func AddComment added in v0.0.10

func AddComment(n Node, cg *CommentGroup)

AddComment adds the given comment to the node if it supports it. If a node does not support comments, such as for CommentGroup or Comment, this call has no effect.

func Embed added in v0.2.2

func Embed(x Expr) *embedding

Embed can be used in conjunction with NewStruct to embed values.

func IsValidIdent added in v0.0.14

func IsValidIdent(ident string) bool

IsValidIdent reports whether str is a valid identifier. Note that the underscore "_" string is considered valid, for top.

func LabelName

func LabelName(l Label) (name string, isIdent bool, err error)

LabelName reports the name of a label, whether it is an identifier (it binds a value to a scope), and whether it is valid. Keywords that are allowed in label positions are interpreted accordingly.

Examples:

Label   Result
foo     "foo"  true   nil
true    "true" true   nil
"foo"   "foo"  false  nil
"x-y"   "x-y"  false  nil
"foo    ""     false  invalid string
"\(x)"  ""     false  errors.Is(err, ErrIsExpression)
X=foo   "foo"  true   nil

func Name added in v0.3.0

func Name(n Node) string

Name describes the type of n.

func SetComments added in v0.0.10

func SetComments(n Node, cgs []*CommentGroup)

SetComments replaces all comments of n with the given set of comments. If a node does not support comments, such as for CommentGroup or Comment, this call has no effect.

func SetPos added in v0.0.10

func SetPos(n Node, p token.Pos)

SetPos sets a node to the given position, if possible.

func SetRelPos added in v0.0.10

func SetRelPos(n Node, p token.RelPos)

SetRelPos sets the relative position of a node without modifying its file position. Setting it to token.NoRelPos allows a node to adopt default formatting.

func SplitPackageVersion added in v0.13.0

func SplitPackageVersion(path string) (prefix, version string, ok bool)

SplitPackageVersion returns a prefix and version suffix such that prefix+"@"+version == path.

SplitPackageVersion returns (path, "", false) when there is no `@` character splitting the path or if the version is empty.

It does not check that the version is valid in any way other than checking that it is not empty.

For example:

SplitPackageVersion("foo.com/bar@v0.1") returns ("foo.com/bar", "v0.1", true). SplitPackageVersion("foo.com/bar@badvers") returns ("foo.com/bar", "badvers", true). SplitPackageVersion("foo.com/bar") returns ("foo.com/bar", "", false). SplitPackageVersion("foo.com/bar@") returns ("foo.com/bar@", "", false).

func StringLabelNeedsQuoting added in v0.15.0

func StringLabelNeedsQuoting(name string) bool

StringLabelNeedsQuoting reports whether the given string must be quoted via literal.Label.Quote to represent itself as a string label, such as a regular field.

Note that a negative result does not mean you can simply use NewIdent(name) to create a valid label without affecting any references. In the general case, you should use Ident.Node to ensure each identifier references exactly what they mean to, or quote any string label which doesn't need to be referenced.

The main use case of this API is for simple scenarios, such as a JSON decoder where the input is all data without any references.

func Walk

func Walk(node Node, before func(Node) bool, after func(Node))

Walk traverses an AST in depth-first order: It starts by calling f(node); node must not be nil. If before returns true, Walk invokes f recursively for each of the non-nil children of node, followed by a call of after. Both functions may be nil. If before is nil, it is assumed to always return true.

Types

type Alias

type Alias struct {
	Ident *Ident    // field name, always an Ident
	Equal token.Pos // position of "="
	Expr  Expr      // An Ident or SelectorExpr
	// contains filtered or unexported fields
}

An Alias binds another field to the alias name in the current struct.

func (*Alias) AddComment deprecated

func (c *Alias) AddComment(cg *CommentGroup)

Deprecated: use AddComment.

func (*Alias) Comments deprecated

func (c *Alias) Comments() []*CommentGroup

Deprecated: use Comments.

func (*Alias) End

func (a *Alias) End() token.Pos

func (*Alias) Pos

func (a *Alias) Pos() token.Pos

func (*Alias) SetComments deprecated added in v0.0.10

func (c *Alias) SetComments(cgs []*CommentGroup)

Deprecated: use SetComments.

type Attribute

type Attribute struct {
	At   token.Pos
	Text string // must be a valid attribute format.
	// contains filtered or unexported fields
}

An Attribute provides meta data about a field.

func (*Attribute) AddComment deprecated

func (c *Attribute) AddComment(cg *CommentGroup)

Deprecated: use AddComment.

func (*Attribute) Comments deprecated

func (c *Attribute) Comments() []*CommentGroup

Deprecated: use Comments.

func (*Attribute) End

func (a *Attribute) End() token.Pos

func (*Attribute) Name added in v0.17.0

func (a *Attribute) Name() string

func (*Attribute) Pos

func (a *Attribute) Pos() token.Pos

func (*Attribute) SetComments deprecated added in v0.0.10

func (c *Attribute) SetComments(cgs []*CommentGroup)

Deprecated: use SetComments.

func (*Attribute) Split added in v0.1.0

func (a *Attribute) Split() (name, body string)

type BadDecl

type BadDecl struct {
	From, To token.Pos // position range of bad declaration
	// contains filtered or unexported fields
}

A BadDecl node is a placeholder for declarations containing syntax errors for which no correct declaration nodes can be created.

func (*BadDecl) AddComment deprecated

func (c *BadDecl) AddComment(cg *CommentGroup)

Deprecated: use AddComment.

func (*BadDecl) Comments deprecated

func (c *BadDecl) Comments() []*CommentGroup

Deprecated: use Comments.

func (*BadDecl) End

func (d *BadDecl) End() token.Pos

func (*BadDecl) Pos

func (d *BadDecl) Pos() token.Pos

func (*BadDecl) SetComments deprecated added in v0.0.10

func (c *BadDecl) SetComments(cgs []*CommentGroup)

Deprecated: use SetComments.

type BadExpr

type BadExpr struct {
	From, To token.Pos // position range of bad expression
	// contains filtered or unexported fields
}

A BadExpr node is a placeholder for expressions containing syntax errors for which no correct expression nodes can be created. This is different from an ErrorExpr which represents an explicitly marked error in the source.

func (*BadExpr) AddComment deprecated

func (c *BadExpr) AddComment(cg *CommentGroup)

Deprecated: use AddComment.

func (*BadExpr) Comments deprecated

func (c *BadExpr) Comments() []*CommentGroup

Deprecated: use Comments.

func (*BadExpr) End

func (x *BadExpr) End() token.Pos

func (*BadExpr) Pos

func (x *BadExpr) Pos() token.Pos

func (*BadExpr) SetComments deprecated added in v0.0.10

func (c *BadExpr) SetComments(cgs []*CommentGroup)

Deprecated: use SetComments.

type BasicLit

type BasicLit struct {
	ValuePos token.Pos   // literal position
	Kind     token.Token // INT, FLOAT, STRING, NULL, TRUE, FALSE
	Value    string      // literal string; e.g. 42, 0x7f, 3.14, 1_234_567, 1e-9, 2.4i, 'a', '\x7f', "foo", or '\m\n\o'
	// contains filtered or unexported fields
}

A BasicLit node represents a literal of basic type.

func NewBool added in v0.1.0

func NewBool(b bool) *BasicLit

NewBool creates a new BasicLit with a bool value without position. Useful for ASTs generated by code other than the CUE parser.

func NewLit added in v0.1.0

func NewLit(tok token.Token, s string) *BasicLit

NewLit creates a new BasicLit with from a token type and string without position. Useful for ASTs generated by code other than the CUE parser.

func NewNull added in v0.2.1

func NewNull() *BasicLit

NewNull creates a new BasicLit configured to be a null value. Useful for ASTs generated by code other than the CUE parser.

func NewString added in v0.0.10

func NewString(str string) *BasicLit

NewString creates a new BasicLit with a string value without position. It quotes the given string. Useful for ASTs generated by code other than the CUE parser.

func (*BasicLit) AddComment deprecated

func (c *BasicLit) AddComment(cg *CommentGroup)

Deprecated: use AddComment.

func (*BasicLit) Comments deprecated

func (c *BasicLit) Comments() []*CommentGroup

Deprecated: use Comments.

func (*BasicLit) End

func (x *BasicLit) End() token.Pos

func (*BasicLit) Pos

func (x *BasicLit) Pos() token.Pos

func (*BasicLit) SetComments deprecated added in v0.0.10

func (c *BasicLit) SetComments(cgs []*CommentGroup)

Deprecated: use SetComments.

type BinaryExpr

type BinaryExpr struct {
	X     Expr        // left operand
	OpPos token.Pos   // position of Op
	Op    token.Token // operator
	Y     Expr        // right operand
	// contains filtered or unexported fields
}

A BinaryExpr node represents a binary expression.

func (*BinaryExpr) AddComment deprecated

func (c *BinaryExpr) AddComment(cg *CommentGroup)

Deprecated: use AddComment.

func (*BinaryExpr) Comments deprecated

func (c *BinaryExpr) Comments() []*CommentGroup

Deprecated: use Comments.

func (*BinaryExpr) End

func (x *BinaryExpr) End() token.Pos

func (*BinaryExpr) Pos

func (x *BinaryExpr) Pos() token.Pos

func (*BinaryExpr) SetComments deprecated added in v0.0.10

func (c *BinaryExpr) SetComments(cgs []*CommentGroup)

Deprecated: use SetComments.

type BottomLit

type BottomLit struct {
	Bottom token.Pos
	// contains filtered or unexported fields
}

A BottomLit indicates an error.

func (*BottomLit) AddComment deprecated

func (c *BottomLit) AddComment(cg *CommentGroup)

Deprecated: use AddComment.

func (*BottomLit) Comments deprecated

func (c *BottomLit) Comments() []*CommentGroup

Deprecated: use Comments.

func (*BottomLit) End

func (x *BottomLit) End() token.Pos

func (*BottomLit) Pos

func (x *BottomLit) Pos() token.Pos

func (*BottomLit) SetComments deprecated added in v0.0.10

func (c *BottomLit) SetComments(cgs []*CommentGroup)

Deprecated: use SetComments.

type CallExpr

type CallExpr struct {
	Fun    Expr      // function expression
	Lparen token.Pos // position of "("
	Args   []Expr    // function arguments; or nil
	Rparen token.Pos // position of ")"
	// contains filtered or unexported fields
}

A CallExpr node represents an expression followed by an argument list.

func NewCall added in v0.0.10

func NewCall(fun Expr, args ...Expr) *CallExpr

NewCall creates a new CallExpr. Useful for ASTs generated by code other than the CUE parser.

func (*CallExpr) AddComment deprecated

func (c *CallExpr) AddComment(cg *CommentGroup)

Deprecated: use AddComment.

func (*CallExpr) Comments deprecated

func (c *CallExpr) Comments() []*CommentGroup

Deprecated: use Comments.

func (*CallExpr) End

func (x *CallExpr) End() token.Pos

func (*CallExpr) Pos

func (x *CallExpr) Pos() token.Pos

func (*CallExpr) SetComments deprecated added in v0.0.10

func (c *CallExpr) SetComments(cgs []*CommentGroup)

Deprecated: use SetComments.

type Clause

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

Clause nodes are part of comprehensions.

type Comment

type Comment struct {
	Slash token.Pos // position of "/" starting the comment
	Text  string    // comment text excluding '\n'
}

A Comment node represents a single //-style comment.

func (*Comment) AddComment

func (c *Comment) AddComment(*CommentGroup)

func (*Comment) Comments

func (c *Comment) Comments() []*CommentGroup

func (*Comment) End

func (c *Comment) End() token.Pos

func (*Comment) Pos

func (c *Comment) Pos() token.Pos

type CommentGroup

type CommentGroup struct {
	// TODO: remove and use the token position of the first comment.
	Doc  bool
	Line bool // true if it is on the same line as the node's end pos.

	// Position indicates where a comment should be attached if a node has
	// multiple tokens. 0 means before the first token, 1 means before the
	// second, etc. For instance, for a field, the positions are:
	//    <0> Label <1> ":" <2> Expr <3> "," <4>
	Position int8
	List     []*Comment // len(List) > 0
	// contains filtered or unexported fields
}

A CommentGroup represents a sequence of comments with no other tokens and no empty lines between.

func Comments added in v0.0.10

func Comments(n Node) []*CommentGroup

Comments returns all comments associated with a given node.

func (*CommentGroup) AddComment

func (g *CommentGroup) AddComment(*CommentGroup)

func (*CommentGroup) Comments

func (g *CommentGroup) Comments() []*CommentGroup

func (*CommentGroup) End

func (g *CommentGroup) End() token.Pos

func (*CommentGroup) Pos

func (g *CommentGroup) Pos() token.Pos

func (*CommentGroup) Text

func (g *CommentGroup) Text() string

Text returns the text of the comment. Comment markers ("//"), the first space of a line comment, and leading and trailing empty lines are removed. Multiple empty lines are reduced to one, and trailing space on lines is trimmed. Unless the result is empty, it is newline-terminated.

type Comprehension added in v0.0.10

type Comprehension struct {
	Clauses  []Clause        // There must be at least one clause.
	Value    Expr            // Must be a struct TODO: change to Struct
	Fallback *FallbackClause // Optional else/fallback clause
	// contains filtered or unexported fields
}

A Comprehension node represents a comprehension declaration.

func (*Comprehension) AddComment deprecated added in v0.0.10

func (c *Comprehension) AddComment(cg *CommentGroup)

Deprecated: use AddComment.

func (*Comprehension) Comments deprecated added in v0.0.10

func (c *Comprehension) Comments() []*CommentGroup

Deprecated: use Comments.

func (*Comprehension) End added in v0.0.10

func (x *Comprehension) End() token.Pos

func (*Comprehension) Pos added in v0.0.10

func (x *Comprehension) Pos() token.Pos

func (*Comprehension) SetComments deprecated added in v0.0.10

func (c *Comprehension) SetComments(cgs []*CommentGroup)

Deprecated: use SetComments.

type Decl

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

A Decl node is implemented by all declarations.

type Ellipsis added in v0.0.6

type Ellipsis struct {
	Ellipsis token.Pos // open list if set
	Type     Expr      // type for the remaining elements
	// contains filtered or unexported fields
}

func (*Ellipsis) AddComment deprecated added in v0.0.6

func (c *Ellipsis) AddComment(cg *CommentGroup)

Deprecated: use AddComment.

func (*Ellipsis) Comments deprecated added in v0.0.6

func (c *Ellipsis) Comments() []*CommentGroup

Deprecated: use Comments.

func (*Ellipsis) End added in v0.0.6

func (x *Ellipsis) End() token.Pos

func (*Ellipsis) Pos added in v0.0.6

func (x *Ellipsis) Pos() token.Pos

func (*Ellipsis) SetComments deprecated added in v0.0.10

func (c *Ellipsis) SetComments(cgs []*CommentGroup)

Deprecated: use SetComments.

type EmbedDecl added in v0.0.5

type EmbedDecl struct {
	Expr Expr
	// contains filtered or unexported fields
}

An EmbedDecl node represents a single expression used as a declaration. The expressions in this declaration is what will be emitted as configuration output.

An EmbedDecl may only appear at the top level.

func (*EmbedDecl) AddComment deprecated added in v0.0.5

func (c *EmbedDecl) AddComment(cg *CommentGroup)

Deprecated: use AddComment.

func (*EmbedDecl) Comments deprecated added in v0.0.5

func (c *EmbedDecl) Comments() []*CommentGroup

Deprecated: use Comments.

func (*EmbedDecl) End added in v0.0.5

func (d *EmbedDecl) End() token.Pos

func (*EmbedDecl) Pos added in v0.0.5

func (d *EmbedDecl) Pos() token.Pos

func (*EmbedDecl) SetComments deprecated added in v0.0.10

func (c *EmbedDecl) SetComments(cgs []*CommentGroup)

Deprecated: use SetComments.

type Expr

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

An Expr is implemented by all expression nodes.

func NewBinExpr added in v0.1.0

func NewBinExpr(op token.Token, operands ...Expr) Expr

NewBinExpr creates for list of expressions of length 2 or greater a chained binary expression of the form (((x1 op x2) op x3) ...). For lists of length 1 it returns the expression itself. It panics for empty lists. Useful for ASTs generated by code other than the CUE parser.

func NewSel added in v0.0.10

func NewSel(x Expr, sel ...string) Expr

NewSel creates a sequence of selectors. Useful for ASTs generated by code other than the CUE parser.

type FallbackClause added in v0.16.0

type FallbackClause struct {
	// TODO: note that the support for "else" is likely temporary, as
	// we will move that functionality to an "if" and "try" element with an
	// optional "else" body.
	Fallback token.Pos // Position of "else" or "fallback" keyword
	Body     *StructLit
	// contains filtered or unexported fields
}

A FallbackClause node represents an else or fallback clause in a comprehension. Used with `else` after if/try clauses, and `fallback` after for clauses.

func (*FallbackClause) AddComment deprecated added in v0.16.0

func (c *FallbackClause) AddComment(cg *CommentGroup)

Deprecated: use AddComment.

func (*FallbackClause) Comments deprecated added in v0.16.0

func (c *FallbackClause) Comments() []*CommentGroup

Deprecated: use Comments.

func (*FallbackClause) End added in v0.16.0

func (x *FallbackClause) End() token.Pos

func (*FallbackClause) Pos added in v0.16.0

func (x *FallbackClause) Pos() token.Pos

func (*FallbackClause) SetComments deprecated added in v0.16.0

func (c *FallbackClause) SetComments(cgs []*CommentGroup)

Deprecated: use SetComments.

type Field

type Field struct {
	Label      Label         // must have at least one element.
	Alias      *PostfixAlias // optional postfix alias (nil if no alias)
	Constraint token.Token   // token.ILLEGAL, token.OPTION, or token.NOT

	// No TokenPos: Value must be an StructLit with one field.
	TokenPos token.Pos

	Value Expr // the value associated with this field.

	Attrs []*Attribute
	// contains filtered or unexported fields
}

A Field represents a field declaration in a struct.

func (*Field) AddComment deprecated

func (c *Field) AddComment(cg *CommentGroup)

Deprecated: use AddComment.

func (*Field) Comments deprecated

func (c *Field) Comments() []*CommentGroup

Deprecated: use Comments.

func (*Field) End

func (d *Field) End() token.Pos

func (*Field) Pos

func (d *Field) Pos() token.Pos

func (*Field) SetComments deprecated added in v0.0.10

func (c *Field) SetComments(cgs []*CommentGroup)

Deprecated: use SetComments.

type File

type File struct {
	Filename string
	Decls    []Decl // top-level declarations; or nil

	Unresolved []*Ident // unresolved identifiers in this file

	// TODO remove this field: it's here as a temporary
	// entity so that tests can determine which version
	// the file was parsed with. A better approach is probably to
	// include the language version in the `token.File` so
	// it's available in every Position.
	LanguageVersion string // The language version as configured by [parser.ParseFile].
	// contains filtered or unexported fields
}

A File node represents a CUE source file.

func (*File) AddComment deprecated

func (c *File) AddComment(cg *CommentGroup)

Deprecated: use AddComment.

func (*File) Comments deprecated

func (c *File) Comments() []*CommentGroup

Deprecated: use Comments.

func (*File) End

func (f *File) End() token.Pos

func (*File) ImportDecls added in v0.15.0

func (f *File) ImportDecls() iter.Seq[*ImportDecl]

ImportDecls iterates through the import declarations in the file.

func (*File) ImportSpecs added in v0.15.0

func (f *File) ImportSpecs() iter.Seq[*ImportSpec]

ImportSpecs iterates through all the import specs from all the import decls in the file.

func (*File) PackageName added in v0.1.0

func (f *File) PackageName() string

PackageName returns the package name associated with this file or "" if no package is associated.

func (*File) Pos

func (f *File) Pos() token.Pos

func (*File) Preamble added in v0.3.0

func (f *File) Preamble() []Decl

Preamble returns the declarations of the preamble at the top of the file, including any package clause or import declaration found in it.

func (*File) SetComments deprecated added in v0.0.10

func (c *File) SetComments(cgs []*CommentGroup)

Deprecated: use SetComments.

func (*File) VisitImports deprecated added in v0.3.0

func (f *File) VisitImports(fn func(d *ImportDecl))

VisitImports iterates through the import declarations in the file.

Deprecated: use File.ImportDecls.

type ForClause

type ForClause struct {
	For token.Pos
	Key *Ident // allow pattern matching?
	// TODO: change to Comma
	Colon  token.Pos
	Value  *Ident // allow pattern matching?
	In     token.Pos
	Source Expr
	// contains filtered or unexported fields
}

A ForClause node represents a for clause in a comprehension.

func (*ForClause) AddComment deprecated

func (c *ForClause) AddComment(cg *CommentGroup)

Deprecated: use AddComment.

func (*ForClause) Comments deprecated

func (c *ForClause) Comments() []*CommentGroup

Deprecated: use Comments.

func (*ForClause) End

func (x *ForClause) End() token.Pos

func (*ForClause) Pos

func (x *ForClause) Pos() token.Pos

func (*ForClause) SetComments deprecated added in v0.0.10

func (c *ForClause) SetComments(cgs []*CommentGroup)

Deprecated: use SetComments.

type Func added in v0.6.0

type Func struct {
	Func token.Pos // position of "func"
	Args []Expr    // list of elements; or nil
	Ret  Expr      // return type, must not be nil
	// contains filtered or unexported fields
}

A Func node represents a function type.

This is an experimental type and the contents will change without notice.

func (*Func) AddComment deprecated added in v0.6.0

func (c *Func) AddComment(cg *CommentGroup)

Deprecated: use AddComment.

func (*Func) Comments deprecated added in v0.6.0

func (c *Func) Comments() []*CommentGroup

Deprecated: use Comments.

func (*Func) End added in v0.6.0

func (x *Func) End() token.Pos

func (*Func) Pos added in v0.6.0

func (x *Func) Pos() token.Pos

func (*Func) SetComments deprecated added in v0.6.0

func (c *Func) SetComments(cgs []*CommentGroup)

Deprecated: use SetComments.

type Ident

type Ident struct {
	NamePos token.Pos // identifier position

	// This LHS path element may be an identifier. Possible forms:
	//  foo:    a normal identifier
	//  "foo":  JSON compatible
	Name string

	Scope Node // scope in which node was found or nil if referring directly
	Node  Node // node referenced by this identifier, if any; see [cuelang.org/go/cue/ast/astutil.Resolve]
	// contains filtered or unexported fields
}

An Ident node represents a left-hand side identifier, including the underscore "_" identifier to represent top.

func NewIdent

func NewIdent(name string) *Ident

NewIdent creates a new Ident without position. Useful for ASTs generated by code other than the CUE parser.

func NewPredeclared added in v0.16.0

func NewPredeclared(name string) *Ident

NewPredeclared creates an Ident for a predeclared name such as "self", "int", or "matchN". name must not have the "__" prefix.

When cuelang.org/go/cue/ast/astutil.Sanitize encounters an identifier created by NewPredeclared and the name is shadowed in scope, it renames the identifier to avoid the shadow. It currently does so by writing the "__"-prefixed form (e.g. "__self"), but this may change in the future.

Use Ident.IsPredeclared to check if an identifier refers to a predeclared name.

func (*Ident) AddComment deprecated

func (c *Ident) AddComment(cg *CommentGroup)

Deprecated: use AddComment.

func (*Ident) Comments deprecated

func (c *Ident) Comments() []*CommentGroup

Deprecated: use Comments.

func (*Ident) End

func (x *Ident) End() token.Pos

func (*Ident) IsPredeclared added in v0.16.0

func (id *Ident) IsPredeclared() bool

IsPredeclared reports whether id was created by NewPredeclared, i.e., whether it refers to a predeclared name.

func (*Ident) Pos

func (x *Ident) Pos() token.Pos

func (*Ident) SetComments deprecated added in v0.0.10

func (c *Ident) SetComments(cgs []*CommentGroup)

Deprecated: use SetComments.

func (*Ident) String

func (id *Ident) String() string

type IfClause

type IfClause struct {
	If        token.Pos
	Condition Expr
	// contains filtered or unexported fields
}

A IfClause node represents an if guard clause in a comprehension.

func (*IfClause) AddComment deprecated

func (c *IfClause) AddComment(cg *CommentGroup)

Deprecated: use AddComment.

func (*IfClause) Comments deprecated

func (c *IfClause) Comments() []*CommentGroup

Deprecated: use Comments.

func (*IfClause) End

func (x *IfClause) End() token.Pos

func (*IfClause) Pos

func (x *IfClause) Pos() token.Pos

func (*IfClause) SetComments deprecated added in v0.0.10

func (c *IfClause) SetComments(cgs []*CommentGroup)

Deprecated: use SetComments.

type ImportDecl

type ImportDecl struct {
	Import token.Pos
	Lparen token.Pos // position of '(', if any
	Specs  []*ImportSpec
	Rparen token.Pos // position of ')', if any
	// contains filtered or unexported fields
}

A ImportDecl node represents a series of import declarations. A valid Lparen position (Lparen.Line > 0) indicates a parenthesized declaration.

func (*ImportDecl) AddComment deprecated

func (c *ImportDecl) AddComment(cg *CommentGroup)

Deprecated: use AddComment.

func (*ImportDecl) Comments deprecated

func (c *ImportDecl) Comments() []*CommentGroup

Deprecated: use Comments.

func (*ImportDecl) End

func (d *ImportDecl) End() token.Pos

func (*ImportDecl) Pos

func (d *ImportDecl) Pos() token.Pos

func (*ImportDecl) SetComments deprecated added in v0.0.10

func (c *ImportDecl) SetComments(cgs []*CommentGroup)

Deprecated: use SetComments.

type ImportPath added in v0.13.0

type ImportPath struct {
	// Path holds the base package/directory path, similar
	// to that returned by [Version.BasePath].
	Path string

	// Version holds the version of the import
	// or empty if not present. Note: in general this
	// will contain a major version only, but there's no
	// guarantee of that.
	Version string

	// Qualifier holds the package qualifier within the path.
	// This will be derived from the last component of Path
	// if it wasn't explicitly present in the import path.
	// This is not guaranteed to be a valid CUE identifier.
	Qualifier string

	// ExplicitQualifier holds whether the qualifier will
	// always be added regardless of whether it matches
	// the final path element.
	ExplicitQualifier bool
}

ImportPath holds the various components of an import path.

func ParseImportPath added in v0.13.0

func ParseImportPath(p string) ImportPath

ParseImportPath returns the various components of an import path. It does not check the result for validity.

func (ImportPath) Canonical added in v0.13.0

func (parts ImportPath) Canonical() ImportPath

Canonical returns the canonical form of the import path. Specifically, it will only include the package qualifier if it's different from the last component of parts.Path.

It also ensures that the Qualifier field is set when appropriate.

func (ImportPath) String added in v0.13.0

func (parts ImportPath) String() string

func (ImportPath) Unqualified added in v0.13.0

func (parts ImportPath) Unqualified() ImportPath

Unqualified returns the import path without any package qualifier.

type ImportSpec

type ImportSpec struct {
	Name   *Ident    // local package name (including "."); or nil
	Path   *BasicLit // import path
	EndPos token.Pos // end of spec (overrides Path.Pos if nonzero)
	// contains filtered or unexported fields
}

An ImportSpec node represents a single package import.

func NewImport added in v0.0.10

func NewImport(name *Ident, importPath string) *ImportSpec

func (*ImportSpec) AddComment deprecated

func (c *ImportSpec) AddComment(cg *CommentGroup)

Deprecated: use AddComment.

func (*ImportSpec) Comments deprecated

func (c *ImportSpec) Comments() []*CommentGroup

Deprecated: use Comments.

func (*ImportSpec) End

func (s *ImportSpec) End() token.Pos

func (*ImportSpec) Pos

func (s *ImportSpec) Pos() token.Pos

func (*ImportSpec) SetComments deprecated added in v0.0.10

func (c *ImportSpec) SetComments(cgs []*CommentGroup)

Deprecated: use SetComments.

type IndexExpr

type IndexExpr struct {
	X      Expr      // expression
	Lbrack token.Pos // position of "["
	Index  Expr      // index expression
	Rbrack token.Pos // position of "]"
	// contains filtered or unexported fields
}

An IndexExpr node represents an expression followed by an index.

func (*IndexExpr) AddComment deprecated

func (c *IndexExpr) AddComment(cg *CommentGroup)

Deprecated: use AddComment.

func (*IndexExpr) Comments deprecated

func (c *IndexExpr) Comments() []*CommentGroup

Deprecated: use Comments.

func (*IndexExpr) End

func (x *IndexExpr) End() token.Pos

func (*IndexExpr) Pos

func (x *IndexExpr) Pos() token.Pos

func (*IndexExpr) SetComments deprecated added in v0.0.10

func (c *IndexExpr) SetComments(cgs []*CommentGroup)

Deprecated: use SetComments.

type Interpolation

type Interpolation struct {
	Elts []Expr // interleaving of strings and expressions.
	// contains filtered or unexported fields
}

A Interpolation node represents a string or bytes interpolation.

func (*Interpolation) AddComment deprecated

func (c *Interpolation) AddComment(cg *CommentGroup)

Deprecated: use AddComment.

func (*Interpolation) Comments deprecated

func (c *Interpolation) Comments() []*CommentGroup

Deprecated: use Comments.

func (*Interpolation) End

func (x *Interpolation) End() token.Pos

func (*Interpolation) Pos

func (x *Interpolation) Pos() token.Pos

func (*Interpolation) Quotes added in v0.17.0

func (x *Interpolation) Quotes() (first, last *BasicLit)

Quotes returns the opening and closing string fragments of x. A well-formed Interpolation has an odd number of elements whose first and last are [*BasicLit]s; when there is only one element, it is returned as both first and last. Quotes panics if x is empty or if either boundary element is not a *BasicLit.

func (*Interpolation) SetComments deprecated added in v0.0.10

func (c *Interpolation) SetComments(cgs []*CommentGroup)

Deprecated: use SetComments.

type Label

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

A Label is any production that can be used as an LHS label.

func NewStringLabel added in v0.15.0

func NewStringLabel(name string) Label

NewStringLabel creates a new string label with the given string, quoting it as a string literal only if necessary, as outlined in StringLabelNeedsQuoting.

To create labels for definition or hidden fields, use NewIdent.

type LetClause added in v0.2.0

type LetClause struct {
	Let   token.Pos
	Ident *Ident
	Equal token.Pos
	Expr  Expr
	// contains filtered or unexported fields
}

A LetClause node represents a let clause in a comprehension.

func (*LetClause) AddComment deprecated added in v0.2.0

func (c *LetClause) AddComment(cg *CommentGroup)

Deprecated: use AddComment.

func (*LetClause) Comments deprecated added in v0.2.0

func (c *LetClause) Comments() []*CommentGroup

Deprecated: use Comments.

func (*LetClause) End added in v0.2.0

func (x *LetClause) End() token.Pos

func (*LetClause) Pos added in v0.2.0

func (x *LetClause) Pos() token.Pos

func (*LetClause) SetComments deprecated added in v0.2.0

func (c *LetClause) SetComments(cgs []*CommentGroup)

Deprecated: use SetComments.

type ListLit

type ListLit struct {
	Lbrack token.Pos // position of "["

	// TODO: change to embedding or similar.
	Elts   []Expr    // list of composite elements; or nil
	Rbrack token.Pos // position of "]"
	// contains filtered or unexported fields
}

A ListLit node represents a literal list.

func NewList added in v0.0.14

func NewList(exprs ...Expr) *ListLit

NewList creates a list of Expressions. Useful for ASTs generated by code other than the CUE parser.

func (*ListLit) AddComment deprecated

func (c *ListLit) AddComment(cg *CommentGroup)

Deprecated: use AddComment.

func (*ListLit) Comments deprecated

func (c *ListLit) Comments() []*CommentGroup

Deprecated: use Comments.

func (*ListLit) End

func (x *ListLit) End() token.Pos

func (*ListLit) Pos

func (x *ListLit) Pos() token.Pos

func (*ListLit) SetComments deprecated added in v0.0.10

func (c *ListLit) SetComments(cgs []*CommentGroup)

Deprecated: use SetComments.

type Node

type Node interface {
	Pos() token.Pos // position of first character belonging to the node
	End() token.Pos // position of first character immediately after the node
	// contains filtered or unexported methods
}

A Node represents any node in the abstract syntax tree.

type Package added in v0.0.6

type Package struct {
	PackagePos token.Pos // position of "package" pseudo-keyword
	Name       *Ident    // package name
	// contains filtered or unexported fields
}

A Package represents a package clause.

func (*Package) AddComment deprecated added in v0.0.6

func (c *Package) AddComment(cg *CommentGroup)

Deprecated: use AddComment.

func (*Package) Comments deprecated added in v0.0.6

func (c *Package) Comments() []*CommentGroup

Deprecated: use Comments.

func (*Package) End added in v0.0.6

func (p *Package) End() token.Pos

func (*Package) Pos added in v0.0.6

func (p *Package) Pos() token.Pos

func (*Package) SetComments deprecated added in v0.0.10

func (c *Package) SetComments(cgs []*CommentGroup)

Deprecated: use SetComments.

type ParenExpr

type ParenExpr struct {
	Lparen token.Pos // position of "("
	X      Expr      // parenthesized expression
	Rparen token.Pos // position of ")"
	// contains filtered or unexported fields
}

A ParenExpr node represents a parenthesized expression.

func (*ParenExpr) AddComment deprecated

func (c *ParenExpr) AddComment(cg *CommentGroup)

Deprecated: use AddComment.

func (*ParenExpr) Comments deprecated

func (c *ParenExpr) Comments() []*CommentGroup

Deprecated: use Comments.

func (*ParenExpr) End

func (x *ParenExpr) End() token.Pos

func (*ParenExpr) Pos

func (x *ParenExpr) Pos() token.Pos

func (*ParenExpr) SetComments deprecated added in v0.0.10

func (c *ParenExpr) SetComments(cgs []*CommentGroup)

Deprecated: use SetComments.

type PostfixAlias added in v0.15.0

type PostfixAlias struct {
	Tilde token.Pos // position of "~"

	// Dual form: ~(K,V)
	Lparen token.Pos // position of "(" (invalid if simple form)
	Label  *Ident    // K: label name capture (nil if simple form)
	Comma  token.Pos // position of "," (invalid if simple form)
	Rparen token.Pos // position of ")" (invalid if simple form)

	// Both forms: the field reference (always non-nil)
	Field *Ident // X or V: captures the field reference
	// contains filtered or unexported fields
}

A PostfixAlias represents the new postfix alias syntax using ~. It appears in field declarations after the label.

Simple form: label~X where X captures the field reference Dual form: label~(K,V) where K captures the label name string and V captures the field reference

func (*PostfixAlias) AddComment deprecated added in v0.15.0

func (c *PostfixAlias) AddComment(cg *CommentGroup)

Deprecated: use AddComment.

func (*PostfixAlias) Comments deprecated added in v0.15.0

func (c *PostfixAlias) Comments() []*CommentGroup

Deprecated: use Comments.

func (*PostfixAlias) End added in v0.15.0

func (a *PostfixAlias) End() token.Pos

func (*PostfixAlias) Pos added in v0.15.0

func (a *PostfixAlias) Pos() token.Pos

func (*PostfixAlias) SetComments deprecated added in v0.15.0

func (c *PostfixAlias) SetComments(cgs []*CommentGroup)

Deprecated: use SetComments.

type PostfixExpr added in v0.15.0

type PostfixExpr struct {
	X     Expr        // expression
	Op    token.Token // postfix operator // ... or ?
	OpPos token.Pos   // position of operator
	// contains filtered or unexported fields
}

A PostfixExpr node represents an expression followed by a postfix operator.

func (*PostfixExpr) AddComment deprecated added in v0.15.0

func (c *PostfixExpr) AddComment(cg *CommentGroup)

Deprecated: use AddComment.

func (*PostfixExpr) Comments deprecated added in v0.15.0

func (c *PostfixExpr) Comments() []*CommentGroup

Deprecated: use Comments.

func (*PostfixExpr) End added in v0.15.0

func (x *PostfixExpr) End() token.Pos

func (*PostfixExpr) Pos added in v0.15.0

func (x *PostfixExpr) Pos() token.Pos

func (*PostfixExpr) SetComments deprecated added in v0.15.0

func (c *PostfixExpr) SetComments(cgs []*CommentGroup)

Deprecated: use SetComments.

type SelectorExpr

type SelectorExpr struct {
	X      Expr      // expression
	Period token.Pos // position of .
	Sel    Label     // field selector
	// contains filtered or unexported fields
}

A SelectorExpr node represents an expression followed by a selector.

func (*SelectorExpr) AddComment deprecated

func (c *SelectorExpr) AddComment(cg *CommentGroup)

Deprecated: use AddComment.

func (*SelectorExpr) Comments deprecated

func (c *SelectorExpr) Comments() []*CommentGroup

Deprecated: use Comments.

func (*SelectorExpr) End

func (x *SelectorExpr) End() token.Pos

func (*SelectorExpr) Pos

func (x *SelectorExpr) Pos() token.Pos

func (*SelectorExpr) SetComments deprecated added in v0.0.10

func (c *SelectorExpr) SetComments(cgs []*CommentGroup)

Deprecated: use SetComments.

type SliceExpr

type SliceExpr struct {
	X      Expr      // expression
	Lbrack token.Pos // position of "["
	Low    Expr      // begin of slice range; or nil
	High   Expr      // end of slice range; or nil
	Rbrack token.Pos // position of "]"
	// contains filtered or unexported fields
}

An SliceExpr node represents an expression followed by slice indices.

func (*SliceExpr) AddComment deprecated

func (c *SliceExpr) AddComment(cg *CommentGroup)

Deprecated: use AddComment.

func (*SliceExpr) Comments deprecated

func (c *SliceExpr) Comments() []*CommentGroup

Deprecated: use Comments.

func (*SliceExpr) End

func (x *SliceExpr) End() token.Pos

func (*SliceExpr) Pos

func (x *SliceExpr) Pos() token.Pos

func (*SliceExpr) SetComments deprecated added in v0.0.10

func (c *SliceExpr) SetComments(cgs []*CommentGroup)

Deprecated: use SetComments.

type Spec added in v0.0.6

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

type StructLit

type StructLit struct {
	Lbrace token.Pos // position of "{"
	Elts   []Decl    // list of elements; or nil
	Rbrace token.Pos // position of "}"
	// contains filtered or unexported fields
}

A StructLit node represents a literal struct.

func NewStruct added in v0.1.0

func NewStruct(fields ...interface{}) *StructLit

NewStruct creates a struct from the given fields.

A field is either a *Field, an *Ellipsis, *LetClause, a *CommentGroup, or a Label, optionally followed by a token.OPTION or token.NOT to indicate the field is optional or required, followed by an expression for the field value.

It will panic if a values not matching these patterns are given. Useful for ASTs generated by code other than the CUE parser.

func (*StructLit) AddComment deprecated

func (c *StructLit) AddComment(cg *CommentGroup)

Deprecated: use AddComment.

func (*StructLit) Comments deprecated

func (c *StructLit) Comments() []*CommentGroup

Deprecated: use Comments.

func (*StructLit) End

func (x *StructLit) End() token.Pos

func (*StructLit) Pos

func (x *StructLit) Pos() token.Pos

func (*StructLit) SetComments deprecated added in v0.0.10

func (c *StructLit) SetComments(cgs []*CommentGroup)

Deprecated: use SetComments.

type TryClause added in v0.16.0

type TryClause struct {
	Try   token.Pos
	Ident *Ident    // identifier for assignment form (nil for struct form)
	Equal token.Pos // position of "=" (invalid for struct form)
	Expr  Expr      // expression for assignment form (nil for struct form)
	// contains filtered or unexported fields
}

A TryClause node represents a try clause in a comprehension. It can have two forms:

  • try { struct } - Ident/Expr are nil; body is in Comprehension.Value
  • try x = expr - Ident/Expr are set

func (*TryClause) AddComment deprecated added in v0.16.0

func (c *TryClause) AddComment(cg *CommentGroup)

Deprecated: use AddComment.

func (*TryClause) Comments deprecated added in v0.16.0

func (c *TryClause) Comments() []*CommentGroup

Deprecated: use Comments.

func (*TryClause) End added in v0.16.0

func (x *TryClause) End() token.Pos

func (*TryClause) Pos added in v0.16.0

func (x *TryClause) Pos() token.Pos

func (*TryClause) SetComments deprecated added in v0.16.0

func (c *TryClause) SetComments(cgs []*CommentGroup)

Deprecated: use SetComments.

type UnaryExpr

type UnaryExpr struct {
	OpPos token.Pos   // position of Op
	Op    token.Token // operator
	X     Expr        // operand
	// contains filtered or unexported fields
}

A UnaryExpr node represents a unary expression.

func (*UnaryExpr) AddComment deprecated

func (c *UnaryExpr) AddComment(cg *CommentGroup)

Deprecated: use AddComment.

func (*UnaryExpr) Comments deprecated

func (c *UnaryExpr) Comments() []*CommentGroup

Deprecated: use Comments.

func (*UnaryExpr) End

func (x *UnaryExpr) End() token.Pos

func (*UnaryExpr) Pos

func (x *UnaryExpr) Pos() token.Pos

func (*UnaryExpr) SetComments deprecated added in v0.0.10

func (c *UnaryExpr) SetComments(cgs []*CommentGroup)

Deprecated: use SetComments.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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