tools

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2018 License: GPL-3.0 Imports: 2 Imported by: 0

README

Babelfish Tools

Build Status codecov

Language analysis tools on top of Babelfish

Build

make dependencies
go build

If you want to build a docker image containing Babelfish Tools, run:

make build

Usage

Babelfish Tools provides a set of tools built on top of Babelfish, to see which tools are supported, run:

bblfsh-tools --help

To make use of any of these tools you need to have the Babelfish server up and running. The easiest way to do so is through docker:

docker run --privileged -p 9432:9432 --name bblfsh bblfsh/server

Look at server site for more information.

Once you have a server running, you can use the dummy tool, which should let you know if the connection with the server succeeded:

bblfsh-tools dummy path/to/source/code

If the server is in a different location, use the address parameter:

bblfsh-tools dummy --address location:port path/to/source/code

Once the connection with the server is working fine, you can use any other available tool in a similar way.

Available tools

Apart from the dummy tool, the following tools are currently provided:

  • cyclomatic: Parses a code file and prints its cyclomatic complexity
  • npath: Parses a code file and prints the npath complexity of its functions
  • tokenizer: Parses a code file and extracts and prints its tokens

How to add a new tool to Babelfish Tools

Adding a new tool to Babelfish Tools involves two steps: implementing the Tool interface and adding it as a command to the CLI interface.

Implementing the Tooler interface

The Tooler interface has a single method Exec(*uast.Node) error, that is, a tool must implement a method called Exec that receives a pointer to an UAST node and returns an optional error.

It's also convenient to create a new type for the new tool, to be used in the CLI interface command. In the simplest case, an empty struct will do: type Dummy struct{}

Adding the new tool as a command to the CLI interface

Create a new file for the tool command in the cmd/bblfsh-tools directory.

Create a new type there for your tool. It should at least include Common struct. If your tool will support additional parameters, add them there. In the simplest case, it'd just be:

type Dummy struct {
	Common
}

Implement the Commander interface. There's a helper common method execute that will provides a good default, in most cases calling this method should be enough:

func (c *Dummy) Execute(args []string) error {
	return c.execute(args, tools.Dummy{})
}

Note that tools.Dummy{} is the instance of the type that implements the Tooler interface that we described in the previous section.

At this point, only adding the command to the parser is left. This is done at cmd/bblfsh-tools/main.go:

parser.AddCommand("dummy", "", "Run dummy tool", &Dummy{})

And that's it, rebuild and your new tool should be ready to use.

License

GPLv3, see LICENSE

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Tokens

func Tokens(n *uast.Node) []string

Tokens returns a slice of tokens contained in the node.

Types

type CyclomaticComplexity

type CyclomaticComplexity struct{}

func (CyclomaticComplexity) Exec

func (cc CyclomaticComplexity) Exec(n *uast.Node) error

type Dummy

type Dummy struct{}

func (Dummy) Exec

func (d Dummy) Exec(*uast.Node) error

type NPath

type NPath struct{}

func (NPath) Exec

func (np NPath) Exec(n *uast.Node) error

type NPathData

type NPathData struct {
	Name       string
	Complexity int
}

func NPathComplexity

func NPathComplexity(n *uast.Node) []*NPathData

Npath computes the NPath of functions in a *uast.Node.

PMD is considered the reference implementation to assert correctness. See: https://pmd.github.io/pmd-5.7.0/pmd-java/xref/net/sourceforge/pmd/lang/java/rule/codesize/NPathComplexityRule.html

func (*NPathData) String

func (nd *NPathData) String() string

type Tokenizer

type Tokenizer struct{}

func (Tokenizer) Exec

func (t Tokenizer) Exec(node *uast.Node) error

type Tooler

type Tooler interface {
	// Exec will be called with a UAST root node. The error will be passed
	// to the command handler
	Exec(*uast.Node) error
}

Tooler is an interface which can be implemented by any supported tool. When implemented, the Exec method will be called with a UAST root node.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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