astfrom

package
v0.0.0-...-9c152ed Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2017 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package astfrom provides a more forgiving interface for ad-hoc creation of objects in the go/ast package. It's intended for quick debugging and inspection of Go ASTs. See the parent package for a higher level interface for generating complete ASTs at runtime.

Example
package main

import (
	"bytes"
	"fmt"
	"go/format"
	"go/token"

	"github.com/cstockton/astgen/astfrom"
)

func main() {
	run := func(src string) {
		node := astfrom.Source(src)

		fset := token.NewFileSet()
		var buf bytes.Buffer
		if err := format.Node(&buf, fset, node); err != nil {
			fmt.Println(`Error:`, err)
		}
		fmt.Printf("`%v` ->\n%s\n\n", src, buf.String())
	}

	run(`myIdent`)
	run(`1 + 2`)
	run(`func() {}`)
	run(`var foo = "str"`)
	run(`var foo = "str"; i := 0`)

}
Output:

`myIdent` ->
myIdent

`1 + 2` ->
1 + 2

`func() {}` ->
func() {
}

`var foo = "str"` ->
var foo = "str"

`var foo = "str"; i := 0` ->
{
	var foo = "str"
	i := 0
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Source

func Source(src string) ast.Node

Source will return a valid ast.Node from all well formed Go source code. The returned node will never be nil, instead returning a simple *ast.Ident containing the error string if a failure occurs.

Types

This section is empty.

Jump to

Keyboard shortcuts

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