diff --git a/cmd/codegen/gengo_callbacks.go b/cmd/codegen/gengo_callbacks.go new file mode 100644 index 00000000..fd3b5162 --- /dev/null +++ b/cmd/codegen/gengo_callbacks.go @@ -0,0 +1,276 @@ +package main + +import ( + "errors" + "fmt" + "regexp" + "strings" + + "github.com/kpango/glg" +) + +var callbackNotGeneratedError = errors.New("callback was not generated") + +// - includes logging +// - returns CallbackNotGeneratedError +func (g *typedefsGenerator) writeCallback(typedefName CIdentifier, def string) error { + // see https://github.com/AllenDang/cimgui-go/issues/224#issuecomment-2452156237 + // 1: preprocessing - parse typedefs.data[k] to get return type and arguments + + // now let me use a bit of regex. + // We have 2 possibilities: + // - returnType(*)(args1 arg1Name, args2 arg2Name, args3 arg3Name); + // - returnType (args1 arg1Name, args2 arg2Name, args3 arg3Name); + // NOTE: the second is uesed mainly in immarkdown + // NOTE: in the 1st, spaces does not matter so we'll trim them + expr1, err := regexp.Compile("([a-zA-Z0-9_]+\\*?)\\(\\*.*\\)\\((.*)\\);") + if err != nil { + panic(fmt.Sprintf("Cannot compile regex expr1!: %v", err)) + } + + expr2, err := regexp.Compile("([a-zA-Z0-9_]+)\\s+([a-zA-Z0-9_]+)\\((.*)\\);") + if err != nil { + panic(fmt.Sprintf("Cannot compile regex expr2!: %v", err)) + } + + // we need the following from them: + var returnTypeC CIdentifier + argsC := make([]ArgDef, 0) + + if ok := expr1.MatchString(def); ok { + glg.Debugf("callback typedef \"%s\" is in form 1", typedefName) + // now split by "(" + // it should be something like this: + // ["returnType", "*)(args1 arg1Name, args2 arg2Name, args3 arg3Name); - // - returnType (args1 arg1Name, args2 arg2Name, args3 arg3Name); - // NOTE: the second is uesed mainly in immarkdown - // NOTE: in the 1st, spaces does not matter so we'll trim them - expr1, err := regexp.Compile("([a-zA-Z0-9_]+\\*?)\\(\\*.*\\)\\((.*)\\);") - if err != nil { - panic(fmt.Sprintf("Cannot compile regex expr1!: %v", err)) - } - - expr2, err := regexp.Compile("([a-zA-Z0-9_]+)\\s+([a-zA-Z0-9_]+)\\((.*)\\);") - if err != nil { - panic(fmt.Sprintf("Cannot compile regex expr2!: %v", err)) - } - - // we need the following from them: - var returnTypeC CIdentifier - argsC := make([]ArgDef, 0) - - if ok := expr1.MatchString(typedefs.data[k]); ok { - glg.Debugf("callback typedef \"%s\" is in form 1", k) - // now split by "(" - // it should be something like this: - // ["returnType", "*