go: github.com/shurcooL/go/parserutil Index | Examples | Files

package parserutil

import "github.com/shurcooL/go/parserutil"

Package parserutil offers convenience functions for parsing Go code to AST.

Code:

stmt, err := parserutil.ParseStmt("var x int")
if err != nil {
    panic(err)
}

ast.Fprint(os.Stdout, nil, stmt, nil)

Output:

     0  *ast.DeclStmt {
     1  .  Decl: *ast.GenDecl {
     2  .  .  Doc: nil
     3  .  .  TokPos: 31
     4  .  .  Tok: var
     5  .  .  Lparen: 0
     6  .  .  Specs: []ast.Spec (len = 1) {
     7  .  .  .  0: *ast.ValueSpec {
     8  .  .  .  .  Doc: nil
     9  .  .  .  .  Names: []*ast.Ident (len = 1) {
    10  .  .  .  .  .  0: *ast.Ident {
    11  .  .  .  .  .  .  NamePos: 35
    12  .  .  .  .  .  .  Name: "x"
    13  .  .  .  .  .  .  Obj: *ast.Object {
    14  .  .  .  .  .  .  .  Kind: var
    15  .  .  .  .  .  .  .  Name: "x"
    16  .  .  .  .  .  .  .  Decl: *(obj @ 7)
    17  .  .  .  .  .  .  .  Data: 0
    18  .  .  .  .  .  .  .  Type: nil
    19  .  .  .  .  .  .  }
    20  .  .  .  .  .  }
    21  .  .  .  .  }
    22  .  .  .  .  Type: *ast.Ident {
    23  .  .  .  .  .  NamePos: 37
    24  .  .  .  .  .  Name: "int"
    25  .  .  .  .  .  Obj: nil
    26  .  .  .  .  }
    27  .  .  .  .  Values: nil
    28  .  .  .  .  Comment: nil
    29  .  .  .  }
    30  .  .  }
    31  .  .  Rparen: 0
    32  .  }
    33  }

Index

Examples

Package Files

parserutil.go

func ParseDecl Uses

func ParseDecl(x string) (ast.Decl, error)

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

func ParseStmt Uses

func ParseStmt(x string) (ast.Stmt, error)

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

Package parserutil imports 4 packages (graph) and is imported by 2 packages. Updated 2017-08-04. Refresh now. Tools for package owners.