astutil

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2018 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Example (AstWalk)
src := `
var fmt = import("fmt")

func TestFunc(arg1, arg2, arg3) {
	return (arg1 + arg2) * arg3
}

func Main() {
	return TestFunc(1, 2, 3) + BuiltinFuncX(1, 2, 3)
}

fmt.Println(Main())
	`
stmts, err := parser.ParseSrc(src)
if err != nil {
	fmt.Println("ERROR: ", err)
	return
}
var mainFound bool
err = Walk(stmts, func(e interface{}) error {
	switch e := e.(type) {
	case *ast.CallExpr:
		//check if the BuiltinFuncX is getting the right number of args
		if e.Name == `BuiltinFuncX` && len(e.SubExprs) != 3 {
			return errors.New("invalid number of arguments to BuiltinFuncX")
		}
	case *ast.FuncExpr:
		if !mainFound && e.Name == `Main` {
			if len(e.Params) != 0 {
				return errors.New("Too many args to main")
			}
			mainFound = true
		} else if mainFound && e.Name == `Main` {
			return errors.New("Main redefined")
		}
	}
	return nil
})
if err != nil {
	fmt.Println("ERROR: ", err)
	return
}
if !mainFound {
	fmt.Println("ERROR: Main not found")
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Walk

func Walk(stmts []ast.Stmt, f WalkFunc) error

Walk walks the ASTs associated with a statement list generated by parser.ParseSrc each expression and/or statement is passed to the WalkFunc function. If the WalkFunc returns an error the walk is aborted and the error is returned

Types

type WalkFunc

type WalkFunc func(interface{}) error

WalkFunc is used in Walk to walk the AST

Jump to

Keyboard shortcuts

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