cuelang.org/go@v0.10.1/cue/parser/interface.go (about)

     1  // Copyright 2018 The CUE Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // This file contains the exported entry points for invoking the
    16  
    17  package parser
    18  
    19  import (
    20  	"cuelang.org/go/cue/ast"
    21  	"cuelang.org/go/cue/ast/astutil"
    22  	"cuelang.org/go/cue/errors"
    23  	"cuelang.org/go/cue/token"
    24  	"cuelang.org/go/internal"
    25  	"cuelang.org/go/internal/source"
    26  )
    27  
    28  // Option specifies a parse option.
    29  type Option func(p *parser)
    30  
    31  var (
    32  	// PackageClauseOnly causes parsing to stop after the package clause.
    33  	PackageClauseOnly Option = packageClauseOnly
    34  	packageClauseOnly        = func(p *parser) {
    35  		p.mode |= packageClauseOnlyMode
    36  	}
    37  
    38  	// ImportsOnly causes parsing to stop parsing after the import declarations.
    39  	ImportsOnly Option = importsOnly
    40  	importsOnly        = func(p *parser) {
    41  		p.mode |= importsOnlyMode
    42  	}
    43  
    44  	// ParseComments causes comments to be parsed.
    45  	ParseComments Option = parseComments
    46  	parseComments        = func(p *parser) {
    47  		p.mode |= parseCommentsMode
    48  	}
    49  
    50  	// ParseFuncs causes function declarations to be parsed.
    51  	//
    52  	// This is an experimental function and the API is likely to
    53  	// change or dissapear.
    54  	ParseFuncs Option = parseFuncs
    55  	parseFuncs        = func(p *parser) {
    56  		p.mode |= parseFuncsMode
    57  	}
    58  
    59  	// Trace causes parsing to print a trace of parsed productions.
    60  	Trace    Option = traceOpt
    61  	traceOpt        = func(p *parser) {
    62  		p.mode |= traceMode
    63  	}
    64  
    65  	// DeclarationErrors causes parsing to report declaration errors.
    66  	DeclarationErrors Option = declarationErrors
    67  	declarationErrors        = func(p *parser) {
    68  		p.mode |= declarationErrorsMode
    69  	}
    70  
    71  	// AllErrors causes all errors to be reported (not just the first 10 on different lines).
    72  	AllErrors Option = allErrors
    73  	allErrors        = func(p *parser) {
    74  		p.mode |= allErrorsMode
    75  	}
    76  
    77  	// AllowPartial allows the parser to be used on a prefix buffer.
    78  	AllowPartial Option = allowPartial
    79  	allowPartial        = func(p *parser) {
    80  		p.mode |= partialMode
    81  	}
    82  )
    83  
    84  // FromVersion specifies until which legacy version the parser should provide
    85  // backwards compatibility.
    86  func FromVersion(version int) Option {
    87  	if version >= 0 {
    88  		version++
    89  	}
    90  	// Versions:
    91  	// <0:  major version 0 (counting -1000 + x, where x = 100*m+p in 0.m.p
    92  	// >=0: x+1 in 1.x.y
    93  	return func(p *parser) { p.version = version }
    94  }
    95  
    96  // DeprecationError is a sentinel error to indicate that an error is
    97  // related to an unsupported old CUE syntax.
    98  type DeprecationError struct {
    99  	Version int
   100  }
   101  
   102  func (e *DeprecationError) Error() string {
   103  	return "try running `cue fix` (possibly with an earlier version, like v0.2.2) to upgrade"
   104  }
   105  
   106  const (
   107  	// Latest specifies the latest version of the parser, effectively setting
   108  	// the strictest implementation.
   109  	Latest = latest
   110  
   111  	latest = -1000 + (100 * internal.MinorCurrent) + 0
   112  
   113  	// FullBackwardCompatibility enables all deprecated features that are
   114  	// currently still supported by the parser.
   115  	FullBackwardCompatibility = fullCompatibility
   116  
   117  	fullCompatibility = -1000
   118  )
   119  
   120  // FileOffset specifies the File position info to use.
   121  //
   122  // Deprecated: this has no effect.
   123  func FileOffset(pos int) Option {
   124  	return func(p *parser) {}
   125  }
   126  
   127  // A mode value is a set of flags (or 0).
   128  // They control the amount of source code parsed and other optional
   129  // parser functionality.
   130  type mode uint
   131  
   132  const (
   133  	packageClauseOnlyMode mode = 1 << iota // stop parsing after package clause
   134  	importsOnlyMode                        // stop parsing after import declarations
   135  	parseCommentsMode                      // parse comments and add them to AST
   136  	parseFuncsMode                         // parse function declarations (experimental)
   137  	partialMode
   138  	traceMode             // print a trace of parsed productions
   139  	declarationErrorsMode // report declaration errors
   140  	allErrorsMode         // report all errors (not just the first 10 on different lines)
   141  )
   142  
   143  // ParseFile parses the source code of a single CUE source file and returns
   144  // the corresponding File node. The source code may be provided via
   145  // the filename of the source file, or via the src parameter.
   146  //
   147  // If src != nil, ParseFile parses the source from src and the filename is
   148  // only used when recording position information. The type of the argument
   149  // for the src parameter must be string, []byte, or io.Reader.
   150  // If src == nil, ParseFile parses the file specified by filename.
   151  //
   152  // The mode parameter controls the amount of source text parsed and other
   153  // optional parser functionality. Position information is recorded in the
   154  // file set fset, which must not be nil.
   155  //
   156  // If the source couldn't be read, the returned AST is nil and the error
   157  // indicates the specific failure. If the source was read but syntax
   158  // errors were found, the result is a partial AST (with Bad* nodes
   159  // representing the fragments of erroneous source code). Multiple errors
   160  // are returned via a ErrorList which is sorted by file position.
   161  func ParseFile(filename string, src interface{}, mode ...Option) (f *ast.File, err error) {
   162  
   163  	// get source
   164  	text, err := source.ReadAll(filename, src)
   165  	if err != nil {
   166  		return nil, err
   167  	}
   168  
   169  	var pp parser
   170  	defer func() {
   171  		if pp.panicking {
   172  			_ = recover()
   173  		}
   174  
   175  		// set result values
   176  		if f == nil {
   177  			// source is not a valid CUE source file - satisfy
   178  			// ParseFile API and return a valid (but) empty *File
   179  			f = &ast.File{
   180  				// Scope: NewScope(nil),
   181  			}
   182  		}
   183  
   184  		err = errors.Sanitize(pp.errors)
   185  	}()
   186  
   187  	// parse source
   188  	pp.init(filename, text, mode)
   189  	f = pp.parseFile()
   190  	if f == nil {
   191  		return nil, pp.errors
   192  	}
   193  	f.Filename = filename
   194  	astutil.Resolve(f, pp.errf)
   195  
   196  	return f, pp.errors
   197  }
   198  
   199  // ParseExpr is a convenience function for parsing an expression.
   200  // The arguments have the same meaning as for Parse, but the source must
   201  // be a valid CUE (type or value) expression. Specifically, fset must not
   202  // be nil.
   203  func ParseExpr(filename string, src interface{}, mode ...Option) (ast.Expr, error) {
   204  	// get source
   205  	text, err := source.ReadAll(filename, src)
   206  	if err != nil {
   207  		return nil, err
   208  	}
   209  
   210  	var p parser
   211  	defer func() {
   212  		if p.panicking {
   213  			_ = recover()
   214  		}
   215  		err = errors.Sanitize(p.errors)
   216  	}()
   217  
   218  	// parse expr
   219  	p.init(filename, text, mode)
   220  	// Set up pkg-level scopes to avoid nil-pointer errors.
   221  	// This is not needed for a correct expression x as the
   222  	// parser will be ok with a nil topScope, but be cautious
   223  	// in case of an erroneous x.
   224  	e := p.parseRHS()
   225  
   226  	// If a comma was inserted, consume it;
   227  	// report an error if there's more tokens.
   228  	if p.tok == token.COMMA && p.lit == "\n" {
   229  		p.next()
   230  	}
   231  	if p.mode&partialMode == 0 {
   232  		p.expect(token.EOF)
   233  	}
   234  
   235  	if p.errors != nil {
   236  		return nil, p.errors
   237  	}
   238  	astutil.ResolveExpr(e, p.errf)
   239  
   240  	return e, p.errors
   241  }
   242  
   243  // parseExprString is a convenience function for obtaining the AST of an
   244  // expression x. The position information recorded in the AST is undefined. The
   245  // filename used in error messages is the empty string.
   246  func parseExprString(x string) (ast.Expr, error) {
   247  	return ParseExpr("", []byte(x))
   248  }