Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions JsonPreprocessor/CJsonPreprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -1632,6 +1634,22 @@ 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*\]|\[\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}'"
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!!!"
Expand Down Expand Up @@ -1898,7 +1916,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
Expand Down Expand Up @@ -1948,6 +1966,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():
Expand All @@ -1958,8 +1979,8 @@ def __handleLastElement(sInput : str) -> str:
except Exception as error:
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):
Expand Down Expand Up @@ -2005,9 +2026,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)
Expand Down Expand Up @@ -2042,7 +2060,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:
Expand All @@ -2051,7 +2069,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)
Expand All @@ -2070,6 +2091,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):
Expand Down
Loading