初版提交
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ErlangFunction 表示 Erlang 函数
|
||||
type ErlangFunction struct {
|
||||
Name string // 函数名
|
||||
Arity int // 参数个数
|
||||
Parameters []string // 参数列表
|
||||
Comments []string // 函数注释
|
||||
StartLine int // 函数起始行号
|
||||
EndLine int // 函数结束行号
|
||||
Export bool // 是否被导出
|
||||
KeyMap map[string]map[string][]string // 注解
|
||||
}
|
||||
|
||||
func (function *ErlangFunction) ParseComments() ErlangFunction {
|
||||
var keyMap = make(map[string]map[string][]string)
|
||||
//var parameters = make(map[string]string)
|
||||
|
||||
for _, comment := range function.Comments {
|
||||
// 匹配注解格式: @keyword(param1=value1, param2=value2)
|
||||
re := regexp.MustCompile(`@(\w+)(?:\s*\((.*)\))?`)
|
||||
matches := re.FindStringSubmatch(comment)
|
||||
|
||||
if len(matches) < 2 {
|
||||
continue
|
||||
}
|
||||
|
||||
keyword := matches[1]
|
||||
parameters, is := keyMap[keyword]
|
||||
if !is {
|
||||
parameters = make(map[string][]string)
|
||||
}
|
||||
|
||||
// 解析参数
|
||||
if len(matches) > 2 && matches[2] != "" {
|
||||
paramsStr := matches[2]
|
||||
paramRe := regexp.MustCompile(`(\w+)\s*=\s*('[^']*'|"[^"]*"|[^,]+)`)
|
||||
paramMatches := paramRe.FindAllStringSubmatch(paramsStr, -1)
|
||||
|
||||
for _, match := range paramMatches {
|
||||
if len(match) >= 3 {
|
||||
value := strings.TrimSpace(match[2])
|
||||
// 移除引号
|
||||
value = strings.Trim(value, `'"`)
|
||||
parameters[strings.TrimSpace(match[1])] = append(parameters[strings.TrimSpace(match[1])], value)
|
||||
}
|
||||
}
|
||||
}
|
||||
keyMap[keyword] = parameters
|
||||
}
|
||||
function.KeyMap = keyMap
|
||||
return *function
|
||||
}
|
||||
Reference in New Issue
Block a user