diff options
Diffstat (limited to 'tools/snippets_translate/converter.py')
-rw-r--r-- | tools/snippets_translate/converter.py | 157 |
1 files changed, 113 insertions, 44 deletions
diff --git a/tools/snippets_translate/converter.py b/tools/snippets_translate/converter.py index 5a2a217d1..d45bf277f 100644 --- a/tools/snippets_translate/converter.py +++ b/tools/snippets_translate/converter.py @@ -10,11 +10,50 @@ from handlers import (handle_array_declarations, handle_casts, handle_class, handle_inc_dec, handle_include, handle_keywords, handle_methods_return_type, handle_negate, handle_type_var_declaration, handle_useless_qt_classes, - handle_void_functions) + handle_new, + handle_void_functions, handle_qt_connects) from parse_utils import dstrip, get_indent, remove_ref +VOID_METHOD_PATTERN = re.compile(r"^ *void *[\w\_]+(::)?[\w\d\_]+\(") +QT_QUALIFIER_PATTERN = re.compile(r"Q[\w]+::") +TERNARY_OPERATOR_PATTERN = re.compile(r"^.* \? .+ : .+$") +COUT_PATTERN = re.compile("^ *(std::)?cout") +FOR_PATTERN = re.compile(r"^ *for *\(") +FOREACH_PATTERN = re.compile(r"^ *foreach *\(") +ELSE_PATTERN = re.compile(r"^ *}? *else *{?") +ELSE_REPLACEMENT_PATTERN = re.compile(r"}? *else *{?") +CLASS_PATTERN = re.compile(r"^ *class ") +STRUCT_PATTERN = re.compile(r"^ *struct ") +DELETE_PATTERN = re.compile(r"^ *delete ") +VAR1_PATTERN = re.compile(r"^[a-zA-Z0-9]+(<.*?>)? [\w\*\&]+(\(.*?\))? ?(?!.*=|:).*$") +VAR2_PATTERN = re.compile(r"^[a-zA-Z0-9]+(<.*?>)? [\w]+::[\w\*\&]+\(.*\)$") +VAR3_PATTERN = re.compile(r"^[a-zA-Z0-9]+(<.*?>)? [\w\*]+ *= *[\w\.\"\']*(\(.*?\))?") +VAR4_PATTERN = re.compile(r"\w+ = [A-Z]{1}\w+") +CONSTRUCTOR_PATTERN = re.compile(r"^ *\w+::\w+\(.*?\)") +ARRAY_VAR_PATTERN = re.compile(r"^[a-zA-Z0-9]+(<.*?>)? [\w\*]+\[?\]? * =? *\{") +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:"} + + +FUNCTION_QUALIFIERS = ["virtual ", " override", "inline ", " noexcept"] + + +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 @@ -24,22 +63,28 @@ def snippet_translate(x): ## General Rules # Remove ';' at the end of the lines - if x.endswith(";"): + has_semicolon = x.endswith(";") + if has_semicolon: x = x[:-1] # Remove lines with only '{' or '}' - if x.strip() == "{" or x.strip() == "}": + xs = x.strip() + if xs == "{" or xs == "}": return "" # Skip lines with the snippet related identifier '//!' - if x.strip().startswith("//!"): + if xs.startswith("//!"): return x # handle lines with only comments using '//' - if x.lstrip().startswith("//"): + if xs.startswith("//"): x = x.replace("//", "#", 1) return x + qt_connects = handle_qt_connects(x) + if qt_connects: + return qt_connects + # Handle "->" if "->" in x: x = x.replace("->", ".") @@ -61,7 +106,7 @@ def snippet_translate(x): # This contains an extra whitespace because of some variables # that include the string 'new' if "new " in x: - x = x.replace("new ", "") + x = handle_new(x) # Handle 'const' # Some variables/functions have the word 'const' so we explicitly @@ -103,12 +148,31 @@ 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).replace("::", ".") + 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 re.search(r"^ *void *[\w\_]+(::)?[\w\d\_]+\(", x): + if VOID_METHOD_PATTERN.search(x): x = handle_void_functions(x) # 'Q*::' -> 'Q*.' - if re.search(r"Q[\w]+::", x): + if QT_QUALIFIER_PATTERN.search(x): x = x.replace("::", ".") # handle 'nullptr' @@ -116,78 +180,76 @@ def snippet_translate(x): x = x.replace("nullptr", "None") ## Special Cases Rules - + xs = x.strip() # Special case for 'main' - if x.strip().startswith("int main("): + if xs.startswith("int main("): return f'{get_indent(x)}if __name__ == "__main__":' - if x.strip().startswith("QApplication app(argc, argv)"): + if xs.startswith("QApplication app(argc, argv)"): return f"{get_indent(x)}app = QApplication([])" # Special case for 'return app.exec()' - if x.strip().startswith("return app.exec"): + if xs.startswith("return app.exec"): return x.replace("return app.exec()", "sys.exit(app.exec())") # Handle includes -> import - if x.strip().startswith("#include"): + if xs.startswith("#include"): x = handle_include(x) return dstrip(x) - if x.strip().startswith("emit "): + if xs.startswith("emit "): x = handle_emit(x) return dstrip(x) # *_cast if "_cast<" in x: x = handle_casts(x) + xs = x.strip() # Handle Qt classes that needs to be removed x = handle_useless_qt_classes(x) # Handling ternary operator - if re.search(r"^.* \? .+ : .+$", x.strip()): + if TERNARY_OPERATOR_PATTERN.search(xs): x = x.replace(" ? ", " if ") x = x.replace(" : ", " else ") + xs = x.strip() # Handle 'while', 'if', and 'else if' # line might end in ')' or ") {" - if x.strip().startswith(("while", "if", "else if", "} else if")): + if xs.startswith(("while", "if", "else if", "} else if")): x = handle_conditions(x) return dstrip(x) - elif re.search("^ *}? *else *{?", x): - x = re.sub(r"}? *else *{?", "else:", x) + elif ELSE_PATTERN.search(x): + x = ELSE_REPLACEMENT_PATTERN.sub("else:", x) return dstrip(x) # 'cout' and 'endl' - if re.search("^ *(std::)?cout", x) or ("endl" in x) or x.lstrip().startswith("qDebug()"): + if COUT_PATTERN.search(x) or ("endl" in x) or xs.startswith("qDebug()"): x = handle_cout_endl(x) return dstrip(x) # 'for' loops - if re.search(r"^ *for *\(", x.strip()): + if FOR_PATTERN.search(xs): return dstrip(handle_for(x)) # 'foreach' loops - if re.search(r"^ *foreach *\(", x.strip()): + if FOREACH_PATTERN.search(xs): return dstrip(handle_foreach(x)) # 'class' and 'structs' - if re.search(r"^ *class ", x) or re.search(r"^ *struct ", x): + if CLASS_PATTERN.search(x) or STRUCT_PATTERN.search(x): if "struct " in x: x = x.replace("struct ", "class ") return handle_class(x) # 'delete' - if re.search(r"^ *delete ", x): + if DELETE_PATTERN.search(x): return x.replace("delete", "del") - # 'public:' - if re.search(r"^public:$", x.strip()): - return x.replace("public:", "# public") - - # 'private:' - if re.search(r"^private:$", x.strip()): - return x.replace("private:", "# private") + # 'public:', etc + if xs in QUALIFIERS: + return f"# {x}".replace(":", "") # For expressions like: `Type var` # which does not contain a `= something` on the right side @@ -202,9 +264,10 @@ def snippet_translate(x): # At the end we skip methods with the form: # QStringView Message::body() # to threat them as methods. - if (re.search(r"^[a-zA-Z0-9]+(<.*?>)? [\w\*\&]+(\(.*?\))? ?(?!.*=|:).*$", x.strip()) - and x.strip().split()[0] not in ("def", "return", "and", "or") - and not re.search(r"^[a-zA-Z0-9]+(<.*?>)? [\w]+::[\w\*\&]+\(.*\)$", x.strip()) + if (has_semicolon and VAR1_PATTERN.search(xs) + and not ([f for f in FUNCTION_QUALIFIERS if f in x]) + and xs.split()[0] not in ("def", "return", "and", "or") + and not VAR2_PATTERN.search(xs) and ("{" not in x and "}" not in x)): # FIXME: this 'if' is a hack for a function declaration with this form: @@ -221,7 +284,7 @@ def snippet_translate(x): # QSome thing = b(...) # float v = 0.1 # QSome *thing = ... - if (re.search(r"^[a-zA-Z0-9]+(<.*?>)? [\w\*]+ *= *[\w\.\"\']*(\(.*?\))?", x.strip()) + if (VAR3_PATTERN.search(xs) and ("{" not in x and "}" not in x)): left, right = x.split("=", 1) var_name = " ".join(left.strip().split()[1:]) @@ -232,23 +295,26 @@ def snippet_translate(x): # layout = QVBoxLayout # so we need to add '()' at the end if it's just a word # with only alpha numeric content - if re.search(r"\w+ = [A-Z]{1}\w+", x.strip()) and not x.strip().endswith(")"): - x = f"{x.rstrip()}()" + if VAR4_PATTERN.search(xs) and not xs.endswith(")"): + v = x.rstrip() + if (not v.endswith(" True") and not v.endswith(" False") + and not v.endswith(" None")): + x = f"{v}()" return dstrip(x) # For constructors, that we now the shape is: # ClassName::ClassName(...) - if re.search(r"^ *\w+::\w+\(.*?\)", x.strip()): + if CONSTRUCTOR_PATTERN.search(xs): x = handle_constructors(x) return dstrip(x) # For base object constructor: # : QWidget(parent) if ( - x.strip().startswith(": ") + xs.startswith(": ") and ("<<" not in x) and ("::" not in x) - and not x.strip().endswith(";") + and not xs.endswith(";") ): return handle_constructor_default_values(x) @@ -257,27 +323,30 @@ def snippet_translate(x): # type var_name[] = {... # type var_name {... # if re.search(r"^[a-zA-Z0-9]+(<.*?>)? [\w\*]+\[\] * = *\{", x.strip()): - if re.search(r"^[a-zA-Z0-9]+(<.*?>)? [\w\*]+\[?\]? * =? *\{", x.strip()): + if ARRAY_VAR_PATTERN.search(xs): x = handle_array_declarations(x) + xs = x.strip() # Methods with return type # int Class::method(...) # QStringView Message::body() - if re.search(r"^[a-zA-Z0-9]+(<.*?>)? [\w]+::[\w\*\&]+\(.*\)$", x.strip()): + if RETURN_TYPE_PATTERN.search(xs): # We just need to capture the 'method name' and 'arguments' x = handle_methods_return_type(x) + xs = x.strip() # Handling functions # By this section of the function, we cover all the other cases # So we can safely assume it's not a variable declaration - if re.search(r"^[a-zA-Z0-9]+(<.*?>)? [\w\*\&]+\(.*\)$", x.strip()): + if FUNCTION_PATTERN.search(xs): x = handle_functions(x) + xs = x.strip() # if it is a C++ iterator declaration, then ignore it due to dynamic typing in Python # eg: std::vector<int> it; # the case of iterator being used inside a for loop is already handed in handle_for(..) # TODO: handle iterator initialization statement like it = container.begin(); - if re.search(r"(std::)?[\w]+<[\w]+>::(const_)?iterator", x): + if ITERATOR_PATTERN.search(x): x = "" return x @@ -285,7 +354,7 @@ def snippet_translate(x): # 'Namespace*::' -> 'Namespace*.' # TODO: In the case where a C++ class function is defined outside the class, this would be wrong # but we do not have such a code snippet yet - if re.search(r"[\w]+::", x): + if SCOPE_PATTERN.search(x): x = x.replace("::", ".") # General return for no special cases |