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
}