parser

package
v0.7.5 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2024 License: MIT Imports: 15 Imported by: 0

README

這是一個專門解析golang結構的包

如何使用

scan := "./"
for dir, fileParsers := range parser.NewGoParserForDir(scan) {
	for _, fileParser := range fileParsers {
		for _, goType := range fileParser.Types {
			for _, attr := range goType.Attrs {
				if attr.HasTag("inject") {
					// 是否擁有某個tag
				}
			}

			// 檢查struct註解
			if goType.Doc.HasAnnotation("@Bean") {
				
			}
		}
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DirIsExist

func DirIsExist(f string) bool

func GenImportAlias

func GenImportAlias(path, packageName string, m map[string]string) map[string]string

GenImportAlias 生成 import => alias

func GetArrWord

func GetArrWord(l []*word) [][]*word

组装成数组

func GetBrackets

func GetBrackets(l []*word, start, end string) (int, int)

括号引用起来的块, 词性必须是分隔符 返回是开始偏移和结束偏移

func GetBracketsAtString added in v0.2.5

func GetBracketsAtString(l []*word, start, end string) (int, int)

括号引用起来的块, 不限制分隔符

func GetBracketsOrLn

func GetBracketsOrLn(l []*word, start, end string) (int, int, bool)

括号引用起来的块, 或者第一行没有块就换行

func GetFistStr

func GetFistStr(l []*word) (string, int)

获取第一个字符(不包括空格, 换行符, 制表)

func GetFistWord

func GetFistWord(l []*word) (string, int)

获取第一个单词

func GetFistWordBehindStr

func GetFistWordBehindStr(l []*word, behind string) (string, int)

获取某字符串后第一个单词

func GetImportStrForMap

func GetImportStrForMap(m map[string]string) string

GetImportStrForMap 生成import

func GetLastIsIdentifier

func GetLastIsIdentifier(l []*word, start string) (bool, int)

最后一个有意义的符号是否是start(后面跟踪回车、空格、注释不影响, 前面也不能有start)

func GetStrAtEnd added in v0.2.5

func GetStrAtEnd(l []*word) (string, int)

获取换行前所有词

func GetWord

func GetWord(l []*word, n int) (string, int)

获取第n个单词

func GetWords

func GetWords(source string) []*word

对原始字符串分词

func HasPrefix

func HasPrefix(s, prefix string) bool

判断是否以某字符串作为开始 HasPrefix

func HasSuffix

func HasSuffix(s, suffix string) bool

判断是否以某字符串作为结尾 HasSuffix

func InArrString

func InArrString(str string, arr []string) bool

func IsIdentifier

func IsIdentifier(r int32) bool

标识符

func IsSpaceAndEspecially added in v0.0.6

func IsSpaceAndEspecially(r rune) bool

func NewAst added in v0.4.7

func NewAst(path string, skips ...map[string]bool) map[string][]GoFileParser

func NewGoParserForDir

func NewGoParserForDir(path string) map[string][]GoFileParser

func NewProtocParserForDir

func NewProtocParserForDir(path string) map[string][]ProtocFileParser

func NextLine

func NextLine(l []*word) int

获取下一行开始

func SortMap

func SortMap(m map[string]string) []string

SortMap 排序map

func StringToHump

func StringToHump(s string) string

蛇形转驼峰

func StringToSnake

func StringToSnake(s string) string

驼峰转蛇形

Types

type Attr

type Attr struct {
	Doc      string
	Name     string
	Ty       string
	Num      int
	Repeated bool
	Message  *Message
}

type DirInfo

type DirInfo struct {
	Name string
	Path string
}

func GetChildrenDir

func GetChildrenDir(path string, skips ...map[string]bool) []DirInfo

GetChildrenDir 获取目录和所有子目录

func (DirInfo) GetFiles

func (di DirInfo) GetFiles(ext string) []FileInfo

GetFiles 获取目录下所有文件

type Enum

type Enum struct {
	Doc  string
	Name string
	Opt  []Attr
}

type FileInfo

type FileInfo struct {
	fs.FileInfo
	Path string
}

func GetDirFiles

func GetDirFiles(path string, ext string) []FileInfo

GetDirFiles 读取目录中的所有文件包括子目录的文件

type GoDoc

type GoDoc string

func (GoDoc) GetAlias

func (d GoDoc) GetAlias() string

func (GoDoc) HasAnnotation

func (d GoDoc) HasAnnotation(check string) bool

HasAnnotation 是否存在某个注解

type GoFileParser

type GoFileParser struct {
	PackageName string
	PackageDoc  string
	Imports     map[string]string
	Types       map[string]GoType
	Funds       map[string]GoFunc
}

GoFileParser 非完整token实现

func GetFileParser

func GetFileParser(path string) (GoFileParser, error)

type GoFunc

type GoFunc struct {
	Name string
	Stu  string
}

type GoType

type GoType struct {
	Doc          GoDoc
	Name         string
	Attrs        map[string]GoTypeAttr
	AttrsSort    []string
	GoFileParser *GoFileParser
}

type GoTypeAttr

type GoTypeAttr struct {
	Name         string
	TypeName     string
	TypeAlias    string
	TypeImport   string
	InPackage    bool // 是否本包的引用
	Tag          map[string]TagDoc
	GoFileParser *GoFileParser
}

func (GoTypeAttr) HasTag

func (receiver GoTypeAttr) HasTag(name string) bool

func (GoTypeAttr) IsPointer

func (receiver GoTypeAttr) IsPointer() bool

IsPointer 普通指针

type GoWords

type GoWords struct {
	// contains filtered or unexported fields
}

type Message

type Message struct {
	Doc  string
	Name string
	Attr []Attr
	Opt  map[string]Option
}

type Option

type Option struct {
	Doc string
	Key string
	Val string
	Map map[string]string
	// contains filtered or unexported fields
}

type ProtocFileParser

type ProtocFileParser struct {
	Doc         string
	Syntax      string
	PackageName string
	Imports     []string
	Option      map[string]Option
	Services    map[string]Service
	Messages    map[string]Message
	Enums       map[string]Enum
}

ProtocFileParser 解释proto文件结构

func GetProtoFileParser

func GetProtoFileParser(path string) (ProtocFileParser, error)

type Service

type Service struct {
	Protoc *ProtocFileParser
	Doc    string
	Name   string
	Opt    map[string]Option
	Rpc    map[string]ServiceRpc
}

type ServiceRpc

type ServiceRpc struct {
	Doc    string
	Name   string
	Param  string
	Return string
	Opt    map[string][]Option
}

type TagDoc

type TagDoc string

func (TagDoc) Count

func (t TagDoc) Count() int

func (TagDoc) Get

func (t TagDoc) Get(num int) string

Jump to

Keyboard shortcuts

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