package parser
import "cuelang.org/go/cue/parser"
Package parser implements a parser for CUE source files. Input may be provided in a variety of forms (see the various Parse* functions); the output is an abstract syntax tree (AST) representing the CUE source. The parser is invoked through one of the Parse* functions.
The parser accepts a larger language than is syntactically permitted by the CUE spec, for simplicity, and for improved robustness in the presence of syntax errors.
Index ¶
- Constants
- func ParseExpr(filename string, src interface{}, mode ...Option) (ast.Expr, error)
- func ParseFile(filename string, src interface{}, mode ...Option) (f *ast.File, err error)
- type Config
- func NewConfig(opts ...Option) Config
- func (cfg Config) Apply(opts ...Option) Config
- func (cfg Config) IsValid() bool
- type DeprecationError
- type Mode
- type Option
Examples ¶
Constants ¶
const ( // Deprecated: see [Version]. Latest = 0 // Deprecated: see [Version]. FullBackwardCompatibility = 0 )
Functions ¶
func ParseExpr ¶
ParseExpr is a convenience function for parsing an expression. The arguments have the same meaning as for Parse, but the source must be a valid CUE (type or value) expression. Specifically, fset must not be nil.
func ParseFile ¶
ParseFile parses the source code of a single CUE source file and returns the corresponding File node. The source code may be provided via the filename of the source file, or via the src parameter.
If src != nil, ParseFile parses the source from src and the filename is only used when recording position information. The type of the argument for the src parameter must be string, []byte, or io.Reader. If src == nil, ParseFile parses the file specified by filename.
The mode parameter controls the amount of source text parsed and other optional parser functionality. Position information is recorded in the file set fset, which must not be nil.
If the source couldn't be read, the returned AST is nil and the error
indicates the specific failure. If the source was read but syntax
errors were found, the result is a partial AST (with Bad* nodes
representing the fragments of erroneous source code). Multiple errors
are returned via a ErrorList which is sorted by file position.
Code:play
Output:Example¶
package main
import (
"fmt"
"cuelang.org/go/cue/parser"
)
func main() {
// Parse some CUE source but stop after processing the imports.
f, err := parser.ParseFile("example.cue", `
import "math"
foo: 1
bar: "baz"
`, parser.ImportsOnly)
if err != nil {
fmt.Println(err)
return
}
// Print the imports from the file's AST.
for spec := range f.ImportSpecs() {
fmt.Println(spec.Path.Value)
}
}
"math"
Types ¶
type Config ¶
type Config struct {
// Mode holds a bitmask of boolean parser options.
Mode Mode
// Version holds the language version to use when
// parsing the CUE syntax.
Version string
// contains filtered or unexported fields
}
Config represents the end result of applying a set of options. The zero value is not OK to use: use NewConfig to construct a Config value before using it.
Config itself implements Option by overwriting the entire configuration.
Config is comparable.
func NewConfig ¶
NewConfig returns the configuration containing all default values with the given options applied.
func (Config) Apply ¶
Apply applies all the given options to cfg and returns the resulting configuration.
func (Config) IsValid ¶
IsValid reports whether cfg is valid; that is, it has been created with NewConfig.
type DeprecationError ¶
type DeprecationError struct {
// Deprecated: version integers have been replaced by language versions as semver strings.
Version int
}
DeprecationError is a sentinel error to indicate that an error is related to an unsupported old CUE syntax.
func (*DeprecationError) Error ¶
func (e *DeprecationError) Error() string
type Mode ¶
type Mode uint
A Mode value is a set of flags (or 0). It controls the amount of source code parsed and other optional parser functionality.
Mode implements Option by or-ing all its bits with [Config.Mode].
const ( // PackageClauseOnly causes parsing to stop after the package clause. PackageClauseOnly Mode = 1 << iota // ImportsOnly causes parsing to stop parsing after the import declarations. ImportsOnly // ParseComments causes comments to be parsed. ParseComments // ParseFuncs causes function declarations to be parsed. // // This is an experimental function and the API is likely to // change or dissapear. ParseFuncs // Trace causes parsing to print a trace of parsed productions. Trace // DeclarationErrors causes parsing to report declaration errors. DeclarationErrors // AllErrors causes all errors to be reported (not just the first 10 on different lines). AllErrors // AllowPartial allows the parser to be used on a prefix buffer. AllowPartial )
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option specifies a parse option.
func FileOffset ¶
FileOffset specifies the File position info to use.
Deprecated: this has no effect.
func FromVersion ¶
FromVersion specifies until which legacy version the parser should provide backwards compatibility.
Deprecated: use Version instead.
func Version ¶
Version specifies the language version to use when parsing the CUE. The argument must be a valid semantic version, as checked by semver.IsValid.
The version will be recorded in the ast.File returned from ParseFile.
Source Files ¶
doc.go interface.go parser.go
- Version
- v0.15.1 (latest)
- Published
- Nov 21, 2025
- Platform
- linux/amd64
- Imports
- 15 packages
- Last checked
- 4 months ago –
Tools for package owners.