genbindings: use state machine tokenization for template parameters

This commit is contained in:
mappu 2024-08-19 19:11:19 +12:00
parent 0e1630a8aa
commit 926cfc7c84
2 changed files with 56 additions and 10 deletions

View File

@ -437,7 +437,7 @@ func parseTypeString(typeString string) (CppParameter, []CppParameter, error) {
}
// Parameters are separated by commas and nesting can not be possible
params := strings.Split(inner, `,`)
params := tokenizeMultipleParameters(inner) // strings.Split(inner, `,`)
ret := make([]CppParameter, 0, len(params))
for _, p := range params {
@ -456,6 +456,31 @@ func parseTypeString(typeString string) (CppParameter, []CppParameter, error) {
return returnType, ret, nil
}
func tokenizeMultipleParameters(p string) []string {
// Tokenize into top-level strings
templateDepth := 0
tokens := []string{}
wip := ""
p = strings.TrimSpace(p)
for _, c := range p {
if c == '<' {
wip += string(c)
templateDepth++
} else if c == '>' {
wip += string(c)
templateDepth--
} else if c == ',' && templateDepth == 0 {
tokens = append(tokens, wip)
wip = ""
} else {
wip += string(c)
}
}
tokens = append(tokens, wip)
return tokens
}
func parseSingleTypeString(p string) CppParameter {
tokens := strings.Split(strings.TrimSpace(p), " ")

View File

@ -10,6 +10,7 @@ func TestParseTypeString(t *testing.T) {
input string
expectReturn CppParameter
expectParams []CppParameter
expectErr bool
}
cases := []testCase{
@ -20,20 +21,40 @@ func TestParseTypeString(t *testing.T) {
CppParameter{ParameterType: "bool"},
},
},
testCase{
input: "bool (QList<QPair<Foo, Bar>>, QString)",
/*
expectReturn: CppParameter{ParameterType: "bool"},
expectParams: []CppParameter{
CppParameter{ParameterType: "QList<QPair<Foo, Bar>>"},
CppParameter{ParameterType: "QString"},
},
*/
expectErr: true,
},
}
for _, tc := range cases {
r, p, err := parseTypeString(tc.input)
if err != nil {
t.Errorf("Test %q got error %v", tc.input, err)
continue
}
if !reflect.DeepEqual(r, tc.expectReturn) {
t.Errorf("Test %q got return=%#v, expected=%#v", tc.input, r, tc.expectReturn)
}
if !reflect.DeepEqual(p, tc.expectParams) {
t.Errorf("Test %q got return=%#v, expected=%#v", tc.input, r, tc.expectReturn)
if tc.expectErr {
if err == nil {
t.Errorf("Test %q got error=nil but it was expected to fail", tc.input)
continue
}
} else {
if err != nil {
t.Errorf("Test %q got error %v", tc.input, err)
continue
}
if !reflect.DeepEqual(r, tc.expectReturn) {
t.Errorf("Test %q\n-got return=%#v\n-expected =%#v", tc.input, r, tc.expectReturn)
}
if !reflect.DeepEqual(p, tc.expectParams) {
t.Errorf("Test %q\n-got params=%#v\n-expected =%#v", tc.input, p, tc.expectParams)
}
}
}
}