parser

package
v0.2.8 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package parser implements a parser for Next 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 Next source.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseExpr

func ParseExpr(x string) (ast.Expr, error)

ParseExpr is a convenience function for obtaining the AST of an expression x. The position information recorded in the AST is undefined. The filename used in error messages is the empty string.

If syntax errors were found, the result is a partial AST (with ast.Bad* nodes representing the fragments of erroneous source code). Multiple errors are returned via a scanner.ErrorList which is sorted by source position.

func ParseExprFrom

func ParseExprFrom(fset *token.FileSet, filename string, src any, mode Mode) (expr ast.Expr, err error)

ParseExprFrom is a convenience function for parsing an expression. The arguments have the same meaning as for ParseFile, but the source must be a valid Next (type or value) expression. Specifically, fset 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 ast.Bad* nodes representing the fragments of erroneous source code). Multiple errors are returned via a scanner.ErrorList which is sorted by source position.

func ParseFile

func ParseFile(fset *token.FileSet, filename string, src any, mode Mode) (f *ast.File, err error)

ParseFile parses a single Next source file and returns the corresponding ast.File node. It takes the following parameters:

  • fset: A token.FileSet for position information (must not be nil)
  • filename: The filename of the source (used for recording positions)
  • src: The source code (string, []byte, io.Reader, or nil to read from filename)
  • mode: Parser mode flags

If parsing fails, it returns a nil ast.File and an error. For syntax errors, it returns a partial AST and a scanner.ErrorList sorted by source position.

Types

type Mode

type Mode uint

Mode is a set of flags controlling parser behavior.

const (
	ParseComments Mode = 1 << iota // Parse comments and add them to AST
	Trace                          // Print a trace of parsed productions
	AllErrors                      // Report all errors (not just the first 10 on different lines)
)

Jump to

Keyboard shortcuts

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