parserutil

package
Version: v0.0.0-...-93f0716 Latest Latest
Warning

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

Go to latest
Published: May 2, 2020 License: MIT Imports: 4 Imported by: 3

Documentation

Overview

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

Example
package main

import (
	"go/ast"
	"os"

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

func main() {
	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

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseDecl

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

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.

Types

This section is empty.

Source Files

Jump to

Keyboard shortcuts

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