version: 1.10

package parser

import "go/parser"

Overview

Package parser implements a parser for Go 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 Go source. The parser is invoked through one
of the Parse
functions.

The parser accepts a larger language than is syntactically permitted by the Go
spec, for simplicity, and for improved robustness in the presence of syntax
errors. For instance, in method declarations, the receiver is treated like an
ordinary parameter list and thus may contain multiple entries where the spec
permits exactly one. Consequently, the corresponding field in the AST
(ast.FuncDecl.Recv) field is not restricted to one entry.

Index

Examples

Package files

interface.go parser.go

func ParseDir

  1. func ParseDir(fset *token.FileSet, path string, filter func(os.FileInfo) bool, mode Mode) (pkgs map[string]*ast.Package, first error)

ParseDir calls ParseFile for all files with names ending in “.go” in the
directory specified by path and returns a map of package name -> package AST
with all the packages found.

If filter != nil, only the files with os.FileInfo entries passing through the
filter (and ending in “.go”) are considered. The mode bits are passed to
ParseFile unchanged. Position information is recorded in fset, which must not be
nil.

If the directory couldn’t be read, a nil map and the respective error are
returned. If a parse error occurred, a non-nil but incomplete map and the first
error encountered are returned.

func ParseExpr

  1. 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.

func ParseExprFrom

  1. func ParseExprFrom(fset *token.FileSet, filename string, src interface{}, mode Mode) (ast.Expr, 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 Go (type
or value) expression. Specifically, fset must not be nil.

func ParseFile

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

ParseFile parses the source code of a single Go source file and returns the
corresponding ast.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 ast.Bad* nodes representing the fragments of
erroneous source code). Multiple errors are returned via a scanner.ErrorList
which is sorted by file position.


Example:

  1. fset := token.NewFileSet() // positions are relative to fset
  2. src := `package foo
  3. import (
  4. "fmt"
  5. "time"
  6. )
  7. func bar() {
  8. fmt.Println(time.Now())
  9. }`
  10. // Parse src but stop after processing the imports.
  11. f, err := parser.ParseFile(fset, "", src, parser.ImportsOnly)
  12. if err != nil {
  13. fmt.Println(err)
  14. return
  15. }
  16. // Print the imports from the file's AST.
  17. for _, s := range f.Imports {
  18. fmt.Println(s.Path.Value)
  19. }
  20. // output:
  21. //
  22. // "fmt"
  23. // "time"

type Mode

  1. type Mode uint

A Mode value is a set of flags (or 0). They control the amount of source code
parsed and other optional parser functionality.

  1. const (
  2. PackageClauseOnly Mode = 1 << iota // stop parsing after package clause
  3. ImportsOnly // stop parsing after import declarations
  4. ParseComments // parse comments and add them to AST
  5. Trace // print a trace of parsed productions
  6. DeclarationErrors // report declaration errors
  7. SpuriousErrors // same as AllErrors, for backward-compatibility
  8. AllErrors = SpuriousErrors // report all errors (not just the first 10 on different lines)
  9. )