Giter Club home page Giter Club logo

goastch's Introduction

goastch (GO AST matCH)

Introduction

Inspired by ast matcher. There are four different basic categories of matchers:

  • Node Matchers: Matchers that match a specific type of AST node.
  • Attributive Matchers: Matchers that match attributes on AST nodes.
  • Logical Matchers: Matchers that allow logic between matchers.
  • Traversal Matchers: Matchers that allow traversal between AST nodes.

Demo of ga toy

./docs/imgs/ga.gif

Using as a lib

Find ‘ShortVarDecl’ from a file, and binding these to ‘bindID’.

package main

import (
	"fmt"
	"go/parser"
	"go/printer"
	"go/token"
	"log"
	"os"

	"github.com/helloyi/goastch"
	. "github.com/helloyi/goastch/goastcher"
)

func main() {
	g := HasDescendant(ShortVarDecl(Anything()).Bind("bindID"))

	src := `package foo
	func bar() {
    a := []int{}
    var b []int
    c := []string{}
	}`

	fset := token.NewFileSet()
	file, _ := parser.ParseFile(fset, "example", src, 0)
	bindings, err := goastch.Find(file, nil, g)
	if err != nil {
		log.Fatalln(err)
	}
	for key, list := range bindings {
		for _, node := range list {
			fmt.Printf("%s: ", key)
			_ = printer.Fprint(os.Stdout, fset, node)
			fmt.Println()
		}
	}
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.