Hallucinate enum support

This commit is contained in:
2025-07-20 22:21:00 +02:00
parent a0ba2e01e3
commit ddd65f18b4
3 changed files with 225 additions and 3 deletions

82
main.go
View File

@@ -46,6 +46,19 @@ func main() {
// Process each group in parallel
utils.WithWorkers(100, workItems, func(worker int, item WorkItem) {
// First check for enums in each file
for _, file := range item.GroupFiles {
hasEnums, err := processEnums(file, *outdir)
if err != nil {
logger.Error("Error processing enums in file %s: %v", file, err)
}
// If the file only contained enums, skip class parsing
if hasEnums && !strings.Contains(file, "[Client]") && !strings.Contains(file, "[Server]") {
return
}
}
// Then process classes
if len(item.GroupFiles) == 1 {
// Single file, process normally
class, err := ParseClass(item.GroupFiles[0])
@@ -66,6 +79,75 @@ func main() {
})
}
func processEnums(file string, outdir string) (bool, error) {
log := logger.Default.WithPrefix("processEnums")
log.Info("Processing enums from file: %s", file)
filehandle, err := os.Open(file)
if err != nil {
return false, fmt.Errorf("error opening file: %w", err)
}
defer filehandle.Close()
doc, err := goquery.NewDocumentFromReader(filehandle)
if err != nil {
return false, fmt.Errorf("error parsing file: %w", err)
}
// Find all code containers
codeblocks := doc.Find("div.codecontainer")
log.Trace("Found %d code blocks", codeblocks.Length())
foundEnums := false
codeblocks.Each(func(i int, s *goquery.Selection) {
// Check if this is an enum block
enumType := s.Find("span.type").First()
if !strings.HasPrefix(strings.TrimSpace(enumType.Text()), "enum") {
return
}
foundEnums = true
// Get enum name from the ID attribute
enumName := s.AttrOr("id", "")
if enumName == "" {
return
}
enum := &Enum{
Name: enumName,
Values: []string{},
}
// Get enum comment if any
comment := s.Find("p").Text()
if comment != "" {
enum.Comment = strings.TrimSpace(comment)
}
// Get enum values
s.Contents().Each(func(i int, s *goquery.Selection) {
if s.Is("br") {
return
}
text := strings.TrimSpace(s.Text())
if text == "" || strings.HasPrefix(text, "enum") {
return
}
enum.Values = append(enum.Values, text)
})
if len(enum.Values) > 0 {
log.Info("Writing enum %s with %d values", enum.Name, len(enum.Values))
if err := enum.Write(outdir, enumTemplate); err != nil {
log.Error("Error writing enum %s: %v", enum.Name, err)
}
}
})
return foundEnums, nil
}
func MapType(t string) string {
// Handle complex types like table<int, string>
if strings.Contains(t, "<") && strings.Contains(t, ">") {