Hallucinate enum support
This commit is contained in:
137
class.go
137
class.go
@@ -16,6 +16,10 @@ import (
|
||||
var templatestr string
|
||||
var classTemplate *template.Template
|
||||
|
||||
//go:embed enum.tmpl
|
||||
var enumTemplateStr string
|
||||
var enumTemplate *template.Template
|
||||
|
||||
var fns = template.FuncMap{
|
||||
"plus1": func(x int) int {
|
||||
return x + 1
|
||||
@@ -39,14 +43,19 @@ var fns = template.FuncMap{
|
||||
|
||||
func init() {
|
||||
log := logger.Default.WithPrefix("init")
|
||||
log.Info("Initializing template")
|
||||
log.Info("Initializing templates")
|
||||
var err error
|
||||
classTemplate, err = template.New("class").Funcs(fns).Parse(templatestr)
|
||||
if err != nil {
|
||||
logger.Error("Error parsing template: %v", err)
|
||||
logger.Error("Error parsing class template: %v", err)
|
||||
return
|
||||
}
|
||||
log.Info("Template initialized successfully")
|
||||
enumTemplate, err = template.New("enum").Funcs(fns).Parse(enumTemplateStr)
|
||||
if err != nil {
|
||||
logger.Error("Error parsing enum template: %v", err)
|
||||
return
|
||||
}
|
||||
log.Info("Templates initialized successfully")
|
||||
}
|
||||
|
||||
type (
|
||||
@@ -81,6 +90,11 @@ type (
|
||||
Params []Param
|
||||
Comment string
|
||||
}
|
||||
Enum struct {
|
||||
Name string
|
||||
Values []string
|
||||
Comment string
|
||||
}
|
||||
)
|
||||
|
||||
func (c *Class) GetOutFile(root string) (*os.File, error) {
|
||||
@@ -115,6 +129,38 @@ func (c *Class) GetOutFile(root string) (*os.File, error) {
|
||||
return f, nil
|
||||
}
|
||||
|
||||
func (e *Enum) GetOutFile(root string) (*os.File, error) {
|
||||
log := logger.Default.WithPrefix("GetOutFile")
|
||||
log.Debug("Getting output file for enum: %s", e.Name)
|
||||
|
||||
if e.Name == "" {
|
||||
log.Error("Name is empty")
|
||||
return nil, fmt.Errorf("Name is empty")
|
||||
}
|
||||
|
||||
filename := fmt.Sprintf("%s.lua", e.Name)
|
||||
log.Trace("Original filename: %s", filename)
|
||||
|
||||
filename = strings.ReplaceAll(filename, " ", "")
|
||||
filename = strings.ReplaceAll(filename, "-", "")
|
||||
filename = strings.ReplaceAll(filename, ",", "")
|
||||
filename = strings.ReplaceAll(filename, ":", "")
|
||||
log.Trace("Cleaned filename: %s", filename)
|
||||
|
||||
filePath := filepath.Join(root, filename)
|
||||
filePath = filepath.Clean(filePath)
|
||||
log.Debug("Full file path: %s", filePath)
|
||||
|
||||
f, err := os.Create(filePath)
|
||||
if err != nil {
|
||||
log.Error("Error creating file: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
log.Info("Successfully created output file: %s", filePath)
|
||||
return f, nil
|
||||
}
|
||||
|
||||
func (c *Class) Write(root string, tmpl *template.Template) error {
|
||||
log := logger.Default.WithPrefix("Write")
|
||||
log.Info("Writing class %s to output", c.ClassName)
|
||||
@@ -136,6 +182,91 @@ func (c *Class) Write(root string, tmpl *template.Template) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *Enum) Write(root string, tmpl *template.Template) error {
|
||||
log := logger.Default.WithPrefix("Write")
|
||||
log.Info("Writing enum %s to output", e.Name)
|
||||
|
||||
outfile, err := e.GetOutFile(root)
|
||||
if err != nil {
|
||||
log.Error("Error creating output file %v: %v", e.Name, err)
|
||||
return fmt.Errorf("error creating output file %v: %v", e.Name, err)
|
||||
}
|
||||
|
||||
log.Debug("Executing template for enum: %s", e.Name)
|
||||
err = tmpl.Execute(outfile, e)
|
||||
if err != nil {
|
||||
log.Error("Error writing output file %v: %v", e.Name, err)
|
||||
return fmt.Errorf("error writing output file %v: %v", e.Name, err)
|
||||
}
|
||||
|
||||
log.Info("Successfully wrote enum %s to output", e.Name)
|
||||
return nil
|
||||
}
|
||||
|
||||
func ParseEnum(file string) (*Enum, error) {
|
||||
log := logger.Default.WithPrefix(file)
|
||||
log.Info("Starting to parse enum file")
|
||||
|
||||
res := Enum{
|
||||
Values: []string{},
|
||||
}
|
||||
|
||||
log.Debug("Opening file: %s", file)
|
||||
filehandle, err := os.Open(file)
|
||||
if err != nil {
|
||||
log.Error("Error opening file: %v", err)
|
||||
return nil, fmt.Errorf("error opening file: %w", err)
|
||||
}
|
||||
|
||||
log.Debug("Creating goquery document from file")
|
||||
doc, err := goquery.NewDocumentFromReader(filehandle)
|
||||
if err != nil {
|
||||
log.Error("Error parsing file: %v", err)
|
||||
return nil, fmt.Errorf("error parsing file: %w", err)
|
||||
}
|
||||
|
||||
// Find all enum containers
|
||||
codeblocks := doc.Find("div.floatright > div.codecontainer")
|
||||
log.Trace("Found %d code blocks", codeblocks.Length())
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// Get enum name from the ID attribute
|
||||
enumName := s.AttrOr("id", "")
|
||||
if enumName == "" {
|
||||
return
|
||||
}
|
||||
|
||||
res.Name = enumName
|
||||
|
||||
// Get enum comment if any
|
||||
comment := s.Find("p").Text()
|
||||
if comment != "" {
|
||||
res.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
|
||||
}
|
||||
res.Values = append(res.Values, text)
|
||||
})
|
||||
})
|
||||
|
||||
log.Info("Successfully parsed enum %s with %d values", res.Name, len(res.Values))
|
||||
return &res, nil
|
||||
}
|
||||
|
||||
func ParseClass(file string) (*Class, error) {
|
||||
log := logger.Default.WithPrefix(file)
|
||||
log.Info("Starting to parse file")
|
||||
|
||||
Reference in New Issue
Block a user