From b48e6c79064f788f98b525eafb58567aa4b3c83c Mon Sep 17 00:00:00 2001 From: mas2hc Date: Thu, 8 May 2025 16:00:52 +0700 Subject: [PATCH 1/3] Ticket 403 - Additional bracket harms the parameter scope detection --- JsonPreprocessor/CJsonPreprocessor.py | 31 +++++++++++++++++++-------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/JsonPreprocessor/CJsonPreprocessor.py b/JsonPreprocessor/CJsonPreprocessor.py index 151dba8..9647812 100644 --- a/JsonPreprocessor/CJsonPreprocessor.py +++ b/JsonPreprocessor/CJsonPreprocessor.py @@ -1537,9 +1537,11 @@ def __checkNestedParam(self, sInput : str, bKey=False, bCheckKeyName=False) -> b if regex.match(r'^\${[^}]+}+(\[.+\])*\s*$', sInput) and \ (sInput.count("${") != sInput.count("}") or sInput.count("[") != sInput.count("]")): errorMsg = f"Invalid expression found: '{self.__removeTokenStr(sInput.strip())}' - The brackets mismatch!!!" - elif sInput.count("${") != sInput.count("}") or sInput.count("[") != sInput.count("]"): + elif sInput.count("{") != sInput.count("}") or sInput.count("[") != sInput.count("]"): if CNameMangling.STRINGCONVERT.value not in sInput: - errorMsg = f"Invalid expression found: '{self.__removeTokenStr(sInput.strip())}' - The brackets mismatch!!!" + errorMsg = f"Invalid expression found: '{self.__removeTokenStr(sInput.strip())}'" + if sInput.count("${") != sInput.count("}") or sInput.count("[") != sInput.count("]"): + errorMsg = f"{errorMsg} - The brackets mismatch!!!" elif regex.search(r'\[[^\[]+\]', sInput) and bKey: invalidFormat = [] for item in regex.findall(r"\[[^\[]+'[^'\[]+'\s*\]", sInput): @@ -1632,6 +1634,13 @@ def __isAscii(sInput : str) -> bool: elif '${' not in sInput and not regex.match(r'^\s*\[\s*import\s*\]\s*$', sInput.lower()): if not oKeyChecker.keyNameChecker(sInput) and __isAscii(sInput): errorMsg = oKeyChecker.errorMsg + elif regex.search(r'\[[^\'\[]+\'[^\']+\'\s*\]', sInput) or regex.search(r'\[\s*\'[^\']+\'[^\]]+\]', sInput): + errorMsg = f"Invalid key name: {sInput}" + elif regex.search(r'\$+\${', sInput): + correctKey = regex.sub(r'(\$+\${)', '${', sInput) + errorMsg = f"Invalid key name: {sInput} - This key name must be '{correctKey}'" + elif sInput.count('${') != sInput.count('}') or sInput.count('[') != sInput.count(']'): + errorMsg = f"Invalid key name: {sInput} - The brackets mismatch!!!" elif regex.search(r'\${[^}]*}', sInput): if regex.search(r'\[\s*\]', sInput): errorMsg = f"Invalid key name: {sInput}. A pair of square brackets is empty!!!" @@ -1898,7 +1907,7 @@ def __checkKeynameFormat(oJson : dict): __checkKeynameFormat(item) elif isinstance(v, dict): __checkKeynameFormat(v) - + def __handleLastElement(sInput : str) -> str: ''' This function handle a last element of a list or dictionary @@ -1948,6 +1957,9 @@ def __handleLastElement(sInput : str) -> str: if reservedToken in sJsonData: self.__reset() raise Exception(f"The JSONP content contains a reserved token '{reservedToken}'") + indexPattern = r"\[[\s\-\+\d]*\]|\[.*:.*\]" + dictPattern = rf"\[+\s*'.+'\s*\]+|\[+\s*\d+\s*\]+|\[+\s*\${{\s*[^\[]+\s*}}.*\]+|{indexPattern}" + nestedPattern = rf"\${{\s*[^\[}}\$]+(\.*\${{\s*[^\[]+\s*}})*\s*}}({dictPattern})*" sJsonDataUpdated = "" lNestedParams = [] for line in sJsonData.splitlines(): @@ -1958,7 +1970,6 @@ def __handleLastElement(sInput : str) -> str: except Exception as error: self.__reset() raise Exception(f"{error} in line: '{line}'") - if "${" in line: curLine = line tmpList03 = [] @@ -2005,9 +2016,6 @@ def __handleLastElement(sInput : str) -> str: item = item.replace(CNameMangling.NESTEDPARAM.value, tmpList03.pop(0)) curItem = item if "${" in item: - indexPattern = r"\[[\s\-\+\d]*\]|\[.*:.*\]" - dictPattern = rf"\[+\s*'.+'\s*\]+|\[+\s*\d+\s*\]+|\[+\s*\${{\s*[^\[]+\s*}}.*\]+|{indexPattern}" - nestedPattern = rf"\${{\s*[^\[}}\$]+(\.*\${{\s*[^\[]+\s*}})*\s*}}({dictPattern})*" bHandle = False if '"' in item and item.count('"')%2==0: tmpList = regex.findall(r'"[^"]+"', item) @@ -2042,7 +2050,7 @@ def __handleLastElement(sInput : str) -> str: else: subItem = regex.sub(r'(\${.+$)', '"\\1"', subItem.strip()) else: - subItem = __handleLastElement(subItem) + subItem = __handleLastElement(subItem) if j < iSubItems: newSubItem = f'{newSubItem}{subItem}, ' else: @@ -2051,7 +2059,10 @@ def __handleLastElement(sInput : str) -> str: item = newSubItem else: if "${" in item and not bHandle: - item = __handleLastElement(item) + if i==iItems: + item = __handleLastElement(item) + elif not regex.match(r'^[\s{]*"[^"]*"\s*$', item): + item = regex.sub('(\$.+)\s*$', '"\\1" ', item) while CNameMangling.STRINGVALUE.value in item: if "${" in tmpList[0]: sValue = tmpList.pop(0) @@ -2070,6 +2081,8 @@ def __handleLastElement(sInput : str) -> str: sJsonDataUpdated = f"{sJsonDataUpdated}{newLine}\n" else: sJsonDataUpdated = f"{sJsonDataUpdated}{line}\n" + sJsonDataUpdated = regex.sub(r'\[\s+\'', '[\'', sJsonDataUpdated) + sJsonDataUpdated = regex.sub(r'\'\s+\]', '\']', sJsonDataUpdated) lKeyName = regex.findall(r'[,\s{]*("[^:,\n]*")\s*:\s*', sJsonDataUpdated) for key in lKeyName: if regex.match(r'^"\s+[^\s]+.+"$|^".+[^\s]+\s+"$', key): From 9d5ac8c031b0decc71e6ba2a3acc7f0c4e6e8d0b Mon Sep 17 00:00:00 2001 From: mas2hc Date: Fri, 9 May 2025 10:17:14 +0700 Subject: [PATCH 2/3] Ticket 415 - Error messages (51) --- JsonPreprocessor/CJsonPreprocessor.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/JsonPreprocessor/CJsonPreprocessor.py b/JsonPreprocessor/CJsonPreprocessor.py index 9647812..decbc89 100644 --- a/JsonPreprocessor/CJsonPreprocessor.py +++ b/JsonPreprocessor/CJsonPreprocessor.py @@ -1634,8 +1634,17 @@ def __isAscii(sInput : str) -> bool: elif '${' not in sInput and not regex.match(r'^\s*\[\s*import\s*\]\s*$', sInput.lower()): if not oKeyChecker.keyNameChecker(sInput) and __isAscii(sInput): errorMsg = oKeyChecker.errorMsg - elif regex.search(r'\[[^\'\[]+\'[^\']+\'\s*\]', sInput) or regex.search(r'\[\s*\'[^\']+\'[^\]]+\]', sInput): - errorMsg = f"Invalid key name: {sInput}" + elif regex.search(r'\[[^\'\[]+\'[^\']+\'\s*\]|\[\s*\'[^\']+\'[^\]]+\]', sInput) or\ + regex.search(r'\[[^\d\]]+\d+\]|\[\d+[^\d\]]+\]', sInput): + errorMsg = f"Invalid syntax: {sInput}" + elif regex.match(r'^\s*\${.+[\]}]*$', sInput): + tmpInput = sInput + while regex.search(r'\[[^\[\]]+\]', tmpInput): + lCheck = regex.findall(r'\[[^\[\]]+\]', tmpInput) + for item in lCheck: + if regex.match(r'^\[[^\'\$]+.+\]$', item): + errorMsg = f"Invalid syntax: {sInput}" + tmpInput = regex.sub(r'\[[^\[\]]+\]', '', tmpInput) elif regex.search(r'\$+\${', sInput): correctKey = regex.sub(r'(\$+\${)', '${', sInput) errorMsg = f"Invalid key name: {sInput} - This key name must be '{correctKey}'" From 21cd0380bb829959b3b7f02291116766c3e42f79 Mon Sep 17 00:00:00 2001 From: mas2hc Date: Tue, 13 May 2025 14:22:40 +0700 Subject: [PATCH 3/3] Ticket 408 - Blanks around key names --- JsonPreprocessor/CJsonPreprocessor.py | 1 + 1 file changed, 1 insertion(+) diff --git a/JsonPreprocessor/CJsonPreprocessor.py b/JsonPreprocessor/CJsonPreprocessor.py index decbc89..32dc5ef 100644 --- a/JsonPreprocessor/CJsonPreprocessor.py +++ b/JsonPreprocessor/CJsonPreprocessor.py @@ -1980,6 +1980,7 @@ def __handleLastElement(sInput : str) -> str: self.__reset() raise Exception(f"{error} in line: '{line}'") if "${" in line: + line = regex.sub(r'\${\s*([^\s][^}]+[^\s])\s*}', '${\\1}', line) curLine = line tmpList03 = [] while regex.search(r'\${([^}]*)}', line):