diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2023-03-28 15:23:40 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2023-03-28 15:59:39 +0200 |
commit | f9ed8662794d14b995b53c73ee18693a114e9773 (patch) | |
tree | 93b861d21e901678332d3df5e14c5712d8651f7d /tools/snippets_translate/converter.py | |
parent | a587de6ca1658286eba55ee37a9d64f6ad530d30 (diff) |
snippets_translate: Implement rudimentary switch() handling
Pick-to: 6.5
Task-number: PYSIDE-1106
Change-Id: If19ac5a962aed846e4a127c652a9bae277999807
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
Diffstat (limited to 'tools/snippets_translate/converter.py')
-rw-r--r-- | tools/snippets_translate/converter.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/tools/snippets_translate/converter.py b/tools/snippets_translate/converter.py index 757d39abb..372c923a7 100644 --- a/tools/snippets_translate/converter.py +++ b/tools/snippets_translate/converter.py @@ -36,13 +36,21 @@ RETURN_TYPE_PATTERN = re.compile(r"^[a-zA-Z0-9]+(<.*?>)? [\w]+::[\w\*\&]+\(.*\)$ FUNCTION_PATTERN = re.compile(r"^[a-zA-Z0-9]+(<.*?>)? [\w\*\&]+\(.*\)$") ITERATOR_PATTERN = re.compile(r"(std::)?[\w]+<[\w]+>::(const_)?iterator") SCOPE_PATTERN = re.compile(r"[\w]+::") +SWITCH_PATTERN = re.compile(r"^\s*switch\s*\(([a-zA-Z0-9_\.]+)\)\s*{.*$") +CASE_PATTERN = re.compile(r"^(\s*)case\s+([a-zA-Z0-9_:\.]+):.*$") +DEFAULT_PATTERN = re.compile(r"^(\s*)default:.*$") QUALIFIERS = {"public:", "protected:", "private:", "public slots:", "protected slots:", "private slots:", "signals:"} +switch_var = None +switch_branch = 0 + + def snippet_translate(x): + global switch_var, switch_branch ## Cases which are not C++ ## TODO: Maybe expand this with lines that doesn't need to be translated @@ -136,6 +144,25 @@ def snippet_translate(x): if "throw" in x: x = handle_keywords(x, "throw", "raise") + switch_match = SWITCH_PATTERN.match(x) + if switch_match: + switch_var = switch_match.group(1) + switch_branch = 0 + return "" + + switch_match = CASE_PATTERN.match(x) + if switch_match: + indent = switch_match.group(1) + value = switch_match.group(2) + cond = "if" if switch_branch == 0 else "elif" + switch_branch += 1 + return f"{indent}{cond} {switch_var} == {value}:" + + switch_match = DEFAULT_PATTERN.match(x) + if switch_match: + indent = switch_match.group(1) + return f"{indent}else:" + # handle 'void Class::method(...)' and 'void method(...)' if VOID_METHOD_PATTERN.search(x): x = handle_void_functions(x) |