astparser

package module
v0.0.0-...-bb1856c Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2022 License: Apache-2.0 Imports: 8 Imported by: 1

README

astparser

go reference license

Easily parse go code, and you can get imports, structs, interfaces, functions, variables, constants.

Installation

Using this package requires a working Go environment. See the install instructions for Go.

This package requires a modern version of Go supporting modules: see the go blog guide on using Go Modules.

Using package
go get github.com/kevinma2010/astparser
...
import (
 "github.com/kevinma2010/astparser"
)
...

Usage/Examples

var code = `package main

import "fmt"

// Greeting greeting interface
type Greeting interface {
	// SayHello say hello
	SayHello()
}

// Dog dog type struct
type Dog struct {
	Name string
}

// SayHello dog say hello
func (dog *Dog) SayHello() {
	fmt.Println("Bow-wow! I am", dog.Name)
}

// FirstName first name
var FirstName = "Kevin"

// LastName last name
const LastName = "Ma"

func main() {
	dog := &Dog{Name: "Buddy"}
	dog.SayHello()
	fmt.Println("The author is", FirstName, LastName)
}`

parser, err := Parse(code)
if err != nil {
  log.Fatalf("parse code failure, reason: %s", err)
}

log.Printf("%+v\n", parser.Values[0])
log.Printf("%+v\n", parser.Structs[0])
log.Printf("%+v\n", parser.Interfaces[0])
log.Printf("%+v\n", parser.Imports)

Documentation

For a more detailed overview of lane, please refer to Documentation

License

Apache-2.0 license

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Function

type Function struct {
	Name, Doc string
	Anonymous bool
	// InputParams input params
	InputParams []string
	// ReturnParams return values
	ReturnParams []string
}

Function go func

type Interface

type Interface struct {
	Name, Doc string
	Functions []*Function
}

Interface go interface

type Parser

type Parser struct {
	// Imports import list
	Imports []string
	// Structs struct list
	Structs []*Struct
	// Values variable list and constant list
	Values []*Value
	// Interfaces interface list
	Interfaces []*Interface
	// contains filtered or unexported fields
}

Parser ast parser

func Parse

func Parse(code string) (*Parser, error)

Parse parses the code and return a parser

func ParseBytes

func ParseBytes(code []byte) (psr *Parser, err error)

ParseBytes parses the code and return a parser

func (*Parser) GetInterface

func (parser *Parser) GetInterface(name string) *Interface

func (*Parser) GetStruct

func (parser *Parser) GetStruct(name string) *Struct

func (*Parser) GetValue

func (parser *Parser) GetValue(name string) *Value

type Struct

type Struct struct {
	Name, Doc string
	// Fields struct field list
	Fields []*StructField
}

Struct go struct

func (*Struct) RangeFieldTags

func (st *Struct) RangeFieldTags(fn func(tags *structtag.Tags, field *StructField, anonymous bool) bool)

type StructField

type StructField struct {
	Name, Doc string
	Anonymous bool
	Type      string
	// Tag fully tag content
	Tag string
	// Tags parsed tag list
	Tags *structtag.Tags
}

StructField go struct field

type Value

type Value struct {
	Name, Doc string
	// Kind constant or variable
	Kind string
	Val  string
}

Value go value, contains constants, variables

Jump to

Keyboard shortcuts

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