Implement parsing methods

This commit is contained in:
2024-09-15 13:50:10 +02:00
parent 2a7be40f09
commit 6ee7a4b2dc
3 changed files with 75 additions and 18 deletions

View File

@@ -161,6 +161,13 @@ func ParseClass(file string) (*Class, error) {
}
res.Fields = append(res.Fields, field)
})
} else {
method, err := parseMethod(s)
if err != nil {
Error.Printf("Error parsing method: %v", err)
return
}
res.Methods = append(res.Methods, method)
}
})
@@ -311,6 +318,58 @@ func parseField(s *goquery.Selection) (Field, error) {
})
res.Comment = strings.ReplaceAll(res.Comment, "\n--", ". ")
return res, nil
}
func parseMethod(s *goquery.Selection) (Method, error) {
res := Method{}
id, ok := s.Attr("id")
if !ok {
return res, fmt.Errorf("no id found")
}
res.Name = id
returns := s.Find("span.keyword")
if returns.Length() != 0 {
returnsText := CleanUp(returns.Text())
returnsText = strings.ReplaceAll(returnsText, "function", "")
returnsText = CleanUp(returnsText)
res.Returns = append(res.Returns, Return{
Type: MapType(returnsText),
Comment: "",
})
}
types := s.Find("span.type")
parameters := s.Find("span.parameter")
types.Each(func(i int, s *goquery.Selection) {
res.Params = append(res.Params, Param{
Name: CleanUp(parameters.Eq(i).Text()),
Type: MapType(CleanUp(types.Eq(i).Text())),
Comment: "",
})
})
// <div id="add" class="codecontainer">
// <div id="add" class="function">
// <p>
// <span class="keyword">function var</span> add(<span class="type">CargoBay</span> <span
// class="parameter">other</span>) <br />
// </p>
// </div>
// <div id="add" class="">
// <p><span class="docheader">Returns</span></p>
// <div class="indented">
// <p>
// nothing
// </p>
// </div>
// </p>
// </div>
// </div>
spew.Dump(res)
return res, nil
}

View File

@@ -16,8 +16,7 @@
{{- end}}
{{.Name}} = function(self{{if gt (len .Params) 0}}, {{range $index, $param := .Params}}{{if $index}}, {{end}}{{$param.Name}}{{end}}{{end}}) end,
{{- if ne (plus1 $index) $n}}
{{- end}}
{{end}}
{{- end}}
}

29
main.go
View File

@@ -6,6 +6,7 @@ import (
"io"
"log"
"os"
"strings"
"sync"
"github.com/davecgh/go-spew/spew"
@@ -61,20 +62,18 @@ func main() {
}
func MapType(t string) string {
switch t {
case "var":
return "any"
case "int":
return "number"
case "float":
return "number"
case "double":
return "number"
case "bool":
return "boolean"
case "table_t":
return "table"
default:
t = strings.ReplaceAll(t, "var", "any")
t = strings.ReplaceAll(t, "int", "number")
t = strings.ReplaceAll(t, "unsigned", "")
t = strings.ReplaceAll(t, "float", "number")
t = strings.ReplaceAll(t, "double", "number")
t = strings.ReplaceAll(t, "bool", "boolean")
t = strings.ReplaceAll(t, "table_t", "table")
t = strings.ReplaceAll(t, "...", "[]")
return t
}
}
func MapName(s string) string {
s = strings.ReplaceAll(s, "in", "input")
return s
}