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 // 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)) ret := make([]CppParameter, 0, len(params))
for _, p := range params { for _, p := range params {
@ -456,6 +456,31 @@ func parseTypeString(typeString string) (CppParameter, []CppParameter, error) {
return returnType, ret, nil 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 { func parseSingleTypeString(p string) CppParameter {
tokens := strings.Split(strings.TrimSpace(p), " ") tokens := strings.Split(strings.TrimSpace(p), " ")

View File

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