Refactor class parsing completely

This commit is contained in:
2024-09-12 16:18:55 +02:00
parent 3994b5aa74
commit 867c31b6d3
3 changed files with 45 additions and 10 deletions

View File

@@ -119,9 +119,33 @@ func ParseClass(file string) (*Class, error) {
}
res.ClassName = strings.TrimSpace(class.Text())
res.Constructors, err = getConstructors(doc)
if err != nil {
return nil, fmt.Errorf("error getting constructors: %w", err)
}
res.Fields, err = getFields(doc)
if err != nil {
return nil, fmt.Errorf("error getting fields: %w", err)
}
res.Methods, err = getMethods(doc)
if err != nil {
return nil, fmt.Errorf("error getting methods: %w", err)
}
spew.Dump(res)
return &res, nil
}
// TODO: Implement parsing comments for parameters
// TODO: Implement parsing comments for return values
func getConstructors(doc *goquery.Document) ([]Constructor, error) {
res := []Constructor{}
codeblocks := doc.Find("div.floatright > div.codecontainer")
if codeblocks.Length() == 0 {
return nil, fmt.Errorf("no codeblocks found")
return res, fmt.Errorf("no codeblocks found")
}
// The first code block should be the constructor
@@ -144,8 +168,12 @@ func ParseClass(file string) (*Class, error) {
Comment: "",
})
})
res.Constructors = append(res.Constructors, resConstructor)
return append(res, resConstructor), nil
}
func getFields(doc *goquery.Document) ([]Field, error) {
res := []Field{}
properties := doc.Find("div.floatright > div.codecontainer#Properties")
properties.ChildrenFiltered("div").Each(func(i int, s *goquery.Selection) {
property := Field{}
@@ -156,9 +184,16 @@ func ParseClass(file string) (*Class, error) {
if comment != "" {
property.Comment = strings.TrimSpace(comment)
}
res.Fields = append(res.Fields, property)
res = append(res, property)
})
return res, nil
}
// TODO: Implement parsing return value types and comments
func getMethods(doc *goquery.Document) ([]Method, error) {
res := []Method{}
codeblocks := doc.Find("div.floatright > div.codecontainer")
codeblocks.ChildrenFiltered("div.function").Each(func(i int, s *goquery.Selection) {
method := Method{}
method.Name = strings.TrimSpace(s.AttrOr("id", ""))
@@ -174,10 +209,8 @@ func ParseClass(file string) (*Class, error) {
method.Params = append(method.Params, param)
})
res.Methods = append(res.Methods, method)
res = append(res, method)
})
spew.Dump(res)
return &res, nil
}
return res, nil
}

View File

@@ -1,7 +1,7 @@
---@diagnostic disable: missing-return, lowercase-global
---@class {{.ClassName}}
{{range .Fields -}}
---@field {{.Name}} {{.Type}} {{.Comment}}
---@field {{.Name}} {{.Type}}{{if ne .Comment ""}} {{.Comment}}{{end}}
{{end -}}
{{.ClassName}} = {
{{$n := len .Methods -}}
@@ -9,7 +9,7 @@
{{range $index, $method := .Methods -}}
---@param self {{$.ClassName}}
{{range $param := $method.Params -}}
---@param {{.Name}} {{.Type}} {{.Comment}}
---@param {{.Name}} {{.Type}}{{if ne .Comment ""}} {{.Comment}}{{end}}
{{end -}}
{{range $ret := $method.Returns -}}
---@return {{.Type}} {{if ne .Comment ""}}#{{.Comment}}{{end}}

View File

@@ -42,6 +42,8 @@ func main() {
return
}
// TODO: Implement parsing enums somehow
// TODO: Implement some sort of switching (between a class and an enum)
wg := sync.WaitGroup{}
for _, file := range files {
wg.Add(1)