Parse a C++ default value literal to a Python value.
(val_str, cpp_type)
| 275 | |
| 276 | |
| 277 | def _parse_default(val_str, cpp_type): |
| 278 | """Parse a C++ default value literal to a Python value.""" |
| 279 | val_str = val_str.strip() |
| 280 | # Remove suffixes |
| 281 | if val_str.endswith("ULL") or val_str.endswith("ull"): |
| 282 | val_str = val_str[:-3] |
| 283 | elif val_str.endswith("f") and cpp_type == "float": |
| 284 | val_str = val_str[:-1] |
| 285 | # Bool literal |
| 286 | if val_str in ("true", "false"): |
| 287 | return val_str == "true" |
| 288 | # String literal |
| 289 | if val_str.startswith('"') and val_str.endswith('"'): |
| 290 | return val_str[1:-1] |
| 291 | # std::vector<...>{...} brace-init literal — recognise the empty form |
| 292 | # and any comma-separated literal list of scalars. |
| 293 | if cpp_type.startswith("std::vector<") and "{" in val_str and val_str.endswith("}"): |
| 294 | inside = val_str[val_str.index("{") + 1:-1].strip() |
| 295 | if not inside: |
| 296 | return [] |
| 297 | return [_parse_default(item, "") for item in inside.split(",")] |
| 298 | # Numeric |
| 299 | try: |
| 300 | if "." in val_str: |
| 301 | return float(val_str) |
| 302 | return int(val_str) |
| 303 | except ValueError: |
| 304 | return val_str |