go: cuelang.org/go/cue/parser Index | Examples | Files

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.



Package Files

doc.go interface.go parser.go print.go


const Latest = latest

Latest specifies the latest version of the parser, effectively setting the strictest implementation.

func ParseExpr Uses

func ParseExpr(filename string, src interface{}, mode ...Option) (ast.Expr, error)

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 Uses

func ParseFile(filename string, src interface{}, mode ...Option) (f *ast.File, err error)

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.


// Parse the file containing this very example
// but stop after processing the imports.
f, err := parser.ParseFile("testdata/test.cue", nil)
if err != nil {

// Print the imports from the file's AST.
for _, s := range f.Imports {



type DeprecationError Uses

type DeprecationError struct {
    Version int

DeprecationError is a sentinel error to indicate that an error is related to an unsupported old CUE syntax.

func (*DeprecationError) Error Uses

func (e *DeprecationError) Error() string

type Option Uses

type Option func(p *parser)

Option specifies a parse option.

var (
    // PackageClauseOnly causes parsing to stop after the package clause.
    PackageClauseOnly Option = packageClauseOnly

    // ImportsOnly causes parsing to stop parsing after the import declarations.
    ImportsOnly Option = importsOnly

    // ParseComments causes comments to be parsed.
    ParseComments Option = parseComments

    // Trace causes parsing to print a trace of parsed productions.
    Trace Option = traceOpt

    // DeclarationErrors causes parsing to report declaration errors.
    DeclarationErrors Option = declarationErrors

    // AllErrors causes all errors to be reported (not just the first 10 on different lines).
    AllErrors Option = allErrors

    // AllowPartial allows the parser to be used on a prefix buffer.
    AllowPartial Option = allowPartial

func FileOffset Uses

func FileOffset(pos int) Option

FileOffset specifies the File position info to use.

func FromVersion Uses

func FromVersion(version int) Option

FromVersion specifies until which legacy version the parser should provide backwards compatibility.

Package parser imports 12 packages (graph) and is imported by 16 packages. Updated 2020-07-06. Refresh now. Tools for package owners.