aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2023-03-28 15:23:40 +0200
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2023-03-28 15:59:39 +0200
commitf9ed8662794d14b995b53c73ee18693a114e9773 (patch)
tree93b861d21e901678332d3df5e14c5712d8651f7d
parenta587de6ca1658286eba55ee37a9d64f6ad530d30 (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>
-rw-r--r--tools/snippets_translate/converter.py27
-rw-r--r--tools/snippets_translate/tests/test_converter.py34
2 files changed, 61 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)
diff --git a/tools/snippets_translate/tests/test_converter.py b/tools/snippets_translate/tests/test_converter.py
index 15c42cebc..3620313e4 100644
--- a/tools/snippets_translate/tests/test_converter.py
+++ b/tools/snippets_translate/tests/test_converter.py
@@ -4,6 +4,11 @@
from converter import snippet_translate as st
+def multi_st(lines):
+ result = [st(l) for l in lines.split("\n")]
+ return "\n".join(result)
+
+
def test_comments():
assert st("// This is a comment") == "# This is a comment"
assert st("// double slash // inside") == "# double slash // inside"
@@ -438,6 +443,35 @@ def test_lambdas():
pass
+def test_switch_case():
+ source = """switch (v) {
+case 1:
+ f1();
+ break;
+case 2:
+ f2();
+ break;
+default:
+ f3();
+ break;
+}
+"""
+ expected = """
+if v == 1:
+ f1()
+ break
+elif v == 2:
+ f2()
+ break
+else:
+ f3()
+ break
+
+"""
+
+ assert multi_st(source) == expected
+
+
def test_std_function():
# std::function<QImage(const QImage &)> scale = [](const QImage &img) {
pass