diff --git a/script_umdp3_checker/umdp3_checker_rules.py b/script_umdp3_checker/umdp3_checker_rules.py index 5aea535b..964469a6 100644 --- a/script_umdp3_checker/umdp3_checker_rules.py +++ b/script_umdp3_checker/umdp3_checker_rules.py @@ -32,6 +32,12 @@ # Declare version VERSION = "13.5.0" +# Precompile regex statements +_vars_declare = re.compile(r"^\s*(INTEGER|REAL|LOGICAL|CHARACTER|TYPE)\s*.*::\s*[A-Z_]+", re.IGNORECASE) +_vars_cleaner = re.compile(r"^\s*(INTEGER|REAL|LOGICAL|CHARACTER|TYPE)\s*.*::\s*(.+)$", re.IGNORECASE) +_consecutive_upper = re.compile(r"([A-Z]{2,})") +_comment_declare = re.compile(r"!.*$") + @dataclass class TestResult: @@ -325,25 +331,29 @@ def lowercase_variable_names(self, lines: List[str]) -> TestResult: count = -1 for count, line in enumerate(lines, 1): clean_line = self.remove_quoted(line) - clean_line = re.sub(r"!.*$", "", clean_line) + clean_line = _comment_declare.sub("", clean_line) # Simple check for UPPERCASE variable declarations - if re.search( - r"^\s*(INTEGER|REAL|LOGICAL|CHARACTER|TYPE)\s*.*::\s*[A-Z_]+", - clean_line, - re.IGNORECASE, - ): - clean_line = re.sub( - r"^\s*(INTEGER|REAL|LOGICAL|CHARACTER|TYPE)\s*.*::\s*", - "", - clean_line, - ) - if match := re.search(r"([A-Z]{2,})", clean_line): - self.add_extra_error(f"UPPERCASE variable name : {match[1]}") - failures += 1 - error_log = self.add_error_log( - error_log, f"UPPERCASE variable name {match[1]}", count - ) + if _vars_declare.search(clean_line): + # Extract variable names part using capturing group + match_vars = _vars_cleaner.search(clean_line) + if match_vars: + var_declarations = match_vars.group(2).strip() + # Split by comma to get individual variable names + var_names = var_declarations.split(",") + + for var_name in var_names: + index = 1 + # Extract just the variable name (before = or () + var_name = re.split(r'[=(]', var_name)[0].strip() + + if _consecutive_upper.search(var_name): + self.add_extra_error(f"UPPERCASE variable name : {var_name}") + failures += 1 + error_log = self.add_error_log( + error_log, f"UPPERCASE variable name {var_name}", count + ) + index += 1 output = f"Checked {count + 1} lines, found {failures} failures." return TestResult(