Implement constructor parsing
This commit is contained in:
140
main.go
140
main.go
@@ -8,6 +8,8 @@ import (
|
||||
"os"
|
||||
"text/template"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
|
||||
_ "embed"
|
||||
)
|
||||
|
||||
@@ -45,65 +47,97 @@ func main() {
|
||||
// return
|
||||
// }
|
||||
|
||||
test := Class{
|
||||
ClassName: "Test",
|
||||
Fields: []Field{
|
||||
{Name: "a", Type: "int", Comment: "test"},
|
||||
{Name: "b", Type: "string", Comment: "test"},
|
||||
},
|
||||
Methods: []Method{
|
||||
{
|
||||
Name: "test",
|
||||
Params: []Param{
|
||||
{Name: "a", Type: "int", Comment: "test param 1"},
|
||||
{Name: "b", Type: "string", Comment: "test param 2"},
|
||||
},
|
||||
Returns: []Return{
|
||||
{Type: "int", Comment: "test return int"},
|
||||
{Type: "string", Comment: "test return string"},
|
||||
},
|
||||
Comment: "test method",
|
||||
},
|
||||
{
|
||||
Name: "test2",
|
||||
Params: []Param{
|
||||
{Name: "a", Type: "int", Comment: "test param 1"},
|
||||
{Name: "b", Type: "string", Comment: "test param 2"},
|
||||
},
|
||||
Returns: []Return{
|
||||
{Type: "int", Comment: "test return int"},
|
||||
{Type: "string", Comment: "test return string"},
|
||||
},
|
||||
Comment: "test method",
|
||||
},
|
||||
},
|
||||
Constructors: []Constructor{
|
||||
{
|
||||
Params: []Param{},
|
||||
},
|
||||
{
|
||||
Params: []Param{
|
||||
{Name: "a", Type: "int", Comment: "test param 1"},
|
||||
{Name: "b", Type: "string", Comment: "test param 2"},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
ltemplate, err := template.New("class").Funcs(fns).Parse(templatestr)
|
||||
if err != nil {
|
||||
Error.Printf("Error parsing template: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
outfile, err := test.GetOutFile()
|
||||
if err != nil {
|
||||
Error.Printf("Error creating output file: %v", err)
|
||||
return
|
||||
}
|
||||
ltemplate.Execute(outfile, test)
|
||||
|
||||
for _, file := range files {
|
||||
fmt.Println(file)
|
||||
class, err := ParseFile(file)
|
||||
if err != nil {
|
||||
Error.Printf("Error parsing file: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
outfile, err := class.GetOutFile()
|
||||
if err != nil {
|
||||
Error.Printf("Error creating output file: %v", err)
|
||||
return
|
||||
}
|
||||
ltemplate.Execute(outfile, class)
|
||||
}
|
||||
}
|
||||
|
||||
func ParseFile(filename string) (*Class, error) {
|
||||
log.Printf("Parsing file: '%s'", filename)
|
||||
res := Class{
|
||||
Fields: []Field{},
|
||||
Methods: []Method{},
|
||||
Constructors: []Constructor{},
|
||||
}
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error opening file: %w", err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
doc, err := goquery.NewDocumentFromReader(file)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error parsing file: %w", err)
|
||||
}
|
||||
|
||||
class := doc.Find("div.floatright > h1")
|
||||
if class.Length() == 0 {
|
||||
return nil, fmt.Errorf("no class found")
|
||||
}
|
||||
res.ClassName = class.Text()
|
||||
|
||||
codeblocks := doc.Find("div.floatright > div.codecontainer")
|
||||
if codeblocks.Length() == 0 {
|
||||
return nil, fmt.Errorf("no codeblocks found")
|
||||
}
|
||||
|
||||
// The first code block should be the constructor
|
||||
// So far I have not found any classes with overloaded constructors...
|
||||
// So I can not handle that case yet
|
||||
constructorBlock := codeblocks.Eq(0)
|
||||
constructor := constructorBlock.Find("div.function")
|
||||
paramTypes := constructor.Find("span.type")
|
||||
paramNames := constructor.Find("span.parameter")
|
||||
|
||||
resConstructor := Constructor{}
|
||||
paramTypes.Each(func(i int, s *goquery.Selection) {
|
||||
pname := paramNames.Eq(i).Text()
|
||||
ptype := paramTypes.Eq(i).Text()
|
||||
ptype = MapType(ptype)
|
||||
|
||||
resConstructor.Params = append(resConstructor.Params, Param{
|
||||
Name: pname,
|
||||
Type: ptype,
|
||||
Comment: "",
|
||||
})
|
||||
})
|
||||
res.Constructors = append(res.Constructors, resConstructor)
|
||||
|
||||
log.Printf("%#v", res)
|
||||
|
||||
return &res, nil
|
||||
}
|
||||
|
||||
func MapType(t string) string {
|
||||
switch t {
|
||||
case "var":
|
||||
return "any"
|
||||
case "int":
|
||||
return "number"
|
||||
case "string":
|
||||
return "string"
|
||||
case "boolean":
|
||||
return "boolean"
|
||||
case "void":
|
||||
return "nil"
|
||||
default:
|
||||
return t
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user