Compare commits

...

3 Commits

Author SHA1 Message Date
6836b35004 Cleanup 2024-09-15 13:51:34 +02:00
6ee7a4b2dc Implement parsing methods 2024-09-15 13:50:10 +02:00
2a7be40f09 Implement parsing read only from properties 2024-09-15 13:13:37 +02:00
3 changed files with 86 additions and 23 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)
}
})
@@ -172,7 +179,7 @@ func getClassName(dataContainer *goquery.Selection) (string, error) {
if class.Length() == 0 {
return "", fmt.Errorf("no class found")
}
res := strings.TrimSpace(class.Text())
res := CleanUp(class.Text())
return res, nil
}
@@ -209,8 +216,8 @@ func getConstructor(dataContainer *goquery.Selection) (Constructor, error) {
types.Each(func(i int, s *goquery.Selection) {
resConstructor.Params = append(resConstructor.Params, Param{
Name: strings.TrimSpace(params.Eq(i).Text()),
Type: MapType(strings.TrimSpace(types.Eq(i).Text())),
Name: CleanUp(params.Eq(i).Text()),
Type: MapType(CleanUp(types.Eq(i).Text())),
Comment: "",
})
})
@@ -233,7 +240,7 @@ func getConstructor(dataContainer *goquery.Selection) (Constructor, error) {
// So we just ignore it
return
}
text := strings.TrimSpace(s.Text())
text := CleanUp(s.Text())
if text == "" {
return
}
@@ -252,7 +259,7 @@ func getConstructor(dataContainer *goquery.Selection) (Constructor, error) {
return
case 1:
param := s.ChildrenFiltered("span.parameter").Text()
paramTrimmed := strings.TrimSpace(param)
paramTrimmed := CleanUp(param)
for i := range resConstructor.Params {
cparam := &resConstructor.Params[i]
if paramTrimmed == cparam.Name {
@@ -289,6 +296,12 @@ func parseField(s *goquery.Selection) (Field, error) {
}
res.Type = MapType(CleanUp(typeElement.Text()))
readonlyElement := s.Find("td[align='right']")
if readonlyElement.Length() != 0 {
// return res, fmt.Errorf("no readonly found")
res.Comment = CleanUp(readonlyElement.Text())
}
comments := s.ChildrenFiltered("div")
if comments.Length() == 0 {
return res, fmt.Errorf("no comments found")
@@ -305,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: MapName(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}}
}

31
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:
return t
}
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
}