|
@@ -2,6 +2,7 @@
|
|
|
# "usage: txtcompile.py <input_dir> <output_dir> <output_filename>"
|
|
|
|
|
|
import os
|
|
|
+from posixpath import expanduser
|
|
|
import sys
|
|
|
import re
|
|
|
import io
|
|
@@ -38,10 +39,11 @@ def class2qsp(text):
|
|
|
|
|
|
|
|
|
|
|
|
- while match is not None:
|
|
|
+ if match is not None:
|
|
|
class_name = match.groups()[0]
|
|
|
- fields_set = []
|
|
|
- fields_get = []
|
|
|
+
|
|
|
+ text = text.replace("#THIS","#"+class_name)
|
|
|
+ text = text.replace("#$THIS","#$"+class_name)
|
|
|
|
|
|
field_missing_error = 'ERROR("Missing field '+class_name+'.<<$ARGS[1]>>")'
|
|
|
|
|
@@ -51,36 +53,113 @@ def class2qsp(text):
|
|
|
|
|
|
field_match = re.search(regex_inner, field_text, re.IGNORECASE)
|
|
|
|
|
|
- while field_match is not None:
|
|
|
- print(field_match.groups())
|
|
|
+ field_count_get = 0
|
|
|
+ field_count_set = 0
|
|
|
+
|
|
|
+ text_get = '\nif $ARGS[0] = "get":\n'
|
|
|
+ text_set = '\nif $ARGS[0] = "set":\n'
|
|
|
|
|
|
- fields_get += [field_match.groups()]
|
|
|
- fields_set += [field_match.groups()]
|
|
|
+ while field_match is not None:
|
|
|
+ #('age', ' GET(\n LOOKUP(#this.height?\n 0-40: 1,\n 41-100:2\n )\n )')
|
|
|
+ field_name = field_match.groups()[0]
|
|
|
+ field_name_prefix = ''
|
|
|
+ if field_name[0] == '$':
|
|
|
+ field_name_prefix = '$'
|
|
|
+ field_name = field_name[1:]
|
|
|
+
|
|
|
+ field_arguments = field_match.groups()[1]
|
|
|
+
|
|
|
+ #GET Arguments
|
|
|
+ get_text = ''
|
|
|
+ get_done = False
|
|
|
+ currentPos = field_arguments.find('GET(')
|
|
|
+ if currentPos >= 0:
|
|
|
+ currentPos += len('GET(') - 1
|
|
|
+
|
|
|
+ getArguments = getNestedText(field_arguments[currentPos:],'(',')')
|
|
|
+
|
|
|
+ #print (getArguments)
|
|
|
+
|
|
|
+ currentPosLookup = getArguments.find('LOOKUP(')
|
|
|
+ if currentPosLookup >= 0:
|
|
|
+ lookupText = ''
|
|
|
+ currentPosLookup += len('LOOKUP(') - 1
|
|
|
+ lookupArguments = getNestedText(getArguments[currentPosLookup:],'(',')')
|
|
|
+
|
|
|
+ #print(lookupArguments)
|
|
|
+
|
|
|
+ lookupArguments = lookupArguments.split('?')
|
|
|
+ lookupVar = lookupArguments[0] # #this.height
|
|
|
+ lookupValuePairs = lookupArguments[1].split(',') # ['\n 0-40: 1', '\n 41-100:2\n ']
|
|
|
+
|
|
|
+ #print(lookupValuePairs)
|
|
|
+ lookupValuePairCounter = 0
|
|
|
+ for lookupValuePair in lookupValuePairs:
|
|
|
+ if lookupValuePairCounter > 0: lookupText += '\t\telse'
|
|
|
+ lookupCondition = ""
|
|
|
+ lookupValuePairDetails = lookupValuePair.split(':')
|
|
|
+ lookupValuePairRange = lookupValuePairDetails[0].strip()
|
|
|
+ if lookupValuePairRange[0] == "[":
|
|
|
+ seperatorPos = lookupValuePairRange.find('/')
|
|
|
+ if seperatorPos >= 0:
|
|
|
+ lowerValue = lookupValuePairRange[1:seperatorPos]
|
|
|
+ higherValue = lookupValuePairRange[seperatorPos+1:len(lookupValuePairRange)-1]
|
|
|
+ #print(lowerValue+"-"+higherValue)
|
|
|
+ lookupCondition = lookupVar + '>='+lowerValue+' and '+lookupVar+'<='+higherValue
|
|
|
+
|
|
|
+ #print(lookupCondition)
|
|
|
+
|
|
|
+ lookupValuePairValue = lookupValuePairDetails[1].strip()
|
|
|
+
|
|
|
+ lookupText += 'if '+lookupCondition+':\n'+field_name_prefix+'\t\t\tRESULT='+lookupValuePairValue+'\n'
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ lookupValuePairCounter += 1
|
|
|
+ lookupText += '\t\tend\n'
|
|
|
+ get_text = lookupText
|
|
|
+ get_done == True
|
|
|
+
|
|
|
+ if get_done == False and len(getArguments) > 0:
|
|
|
+ get_text = '\t\t'+field_name_prefix+'RESULT = '+getArguments+'\n'
|
|
|
+ get_done = True
|
|
|
+
|
|
|
+
|
|
|
+ if get_text == '' and field_arguments.find('SET(') < 0:
|
|
|
+ get_text = '\t\t'+field_name_prefix+'RESULT = '+field_name_prefix+class_name+'_'+field_name+'\n'
|
|
|
+
|
|
|
+ if get_text != '':
|
|
|
+ if field_count_get > 0:
|
|
|
+ text_get += '\telse'
|
|
|
+ text_get+='if $ARGS[1]="'+field_name_prefix+field_name+'":\n'+get_text
|
|
|
+ field_count_get += 1
|
|
|
+
|
|
|
+ #SET Arguments
|
|
|
+ set_text = ''
|
|
|
+ if field_arguments.find('GET(') < 0:
|
|
|
+ set_text = '\t\t'+field_name_prefix+class_name+'_'+field_name+'='+field_name_prefix+'ARGS[2]\n'
|
|
|
+
|
|
|
+ if set_text != '':
|
|
|
+ if field_count_set > 0:
|
|
|
+ text_set += '\telse'
|
|
|
+ text_set+='if $ARGS[1]="'+field_name_prefix+field_name+'":\n'+set_text
|
|
|
+ field_count_set += 1
|
|
|
+
|
|
|
|
|
|
field_text = field_text[field_match.end():]
|
|
|
field_match = re.search(regex_inner, field_text, re.IGNORECASE)
|
|
|
|
|
|
- text_get = '\nif $ARGS[0] = "get":\n'
|
|
|
- text_set = '\nif $ARGS[0] = "set":\n'
|
|
|
+
|
|
|
|
|
|
- for i in range(len(fields_get)):
|
|
|
- if i >0:
|
|
|
- text_get += '\telse'
|
|
|
- text_set += '\telse'
|
|
|
- text_get += 'if $ARGS[1] = "'+fields_get[i][0]+'":\n'
|
|
|
- text_set += 'if $ARGS[1] = "'+fields_get[i][0]+'":\n'
|
|
|
- if fields_get[i][0][0] == '$':
|
|
|
- text_get += '\t\t$RESULT = $'+class_name+'_'+fields_get[i][0][1:]+'\n'
|
|
|
- text_set += '\t\t$'+class_name+'_'+fields_get[i][0][1:]+'=$ARGS[2]\n'
|
|
|
- else:
|
|
|
- text_get += '\t\tRESULT = '+class_name+'_'+fields_get[i][0]+'\n'
|
|
|
- text_set += '\t\t'+class_name+'_'+fields_get[i][0]+'=ARGS[2]\n'
|
|
|
|
|
|
- if len(fields_get) > 0:
|
|
|
+ if field_count_get > 0:
|
|
|
text_get += '\telse:\n\t\t!'+field_missing_error+'\n\tend\n'
|
|
|
- text_set += '\telse:\n\t\t!'+field_missing_error+'\n\tend\n'
|
|
|
else:
|
|
|
text_get += '\n\t!'+field_missing_error+'\n'
|
|
|
+
|
|
|
+ if field_count_set > 0:
|
|
|
+ text_set += '\telse:\n\t\t!'+field_missing_error+'\n\tend\n'
|
|
|
+ else:
|
|
|
text_set += '\n\t!'+field_missing_error+'\n'
|
|
|
|
|
|
text_get += 'end\n'
|
|
@@ -93,11 +172,32 @@ def class2qsp(text):
|
|
|
|
|
|
|
|
|
text = text[:max(match.start(),0)] +text_get+text_set+ text[(nextClose+1):]
|
|
|
- match = re.search(regex, text, re.IGNORECASE)
|
|
|
+ #match = re.search(regex, text, re.IGNORECASE)
|
|
|
|
|
|
return text
|
|
|
|
|
|
|
|
|
+def getNestedText(text,left,right):
|
|
|
+ #Paste text including braces, e.g. "(asdasdasdasd)""
|
|
|
+ currentPos = 1
|
|
|
+ startPos = currentPos
|
|
|
+ currentOpen = 1
|
|
|
+ while(currentOpen > 0):
|
|
|
+ nextOpen = text.find(left,currentPos)
|
|
|
+ nextClose = text.find(right,currentPos)
|
|
|
+
|
|
|
+ if nextClose == -1:
|
|
|
+ print('Error')
|
|
|
+ sys.exit('Syntax error')
|
|
|
+
|
|
|
+ if nextOpen != -1 and nextOpen < nextClose:
|
|
|
+ currentPos = nextOpen+1
|
|
|
+ currentOpen += 1
|
|
|
+ else:
|
|
|
+ currentPos = nextClose+1
|
|
|
+ currentOpen -= 1
|
|
|
+
|
|
|
+ return text[startPos:currentPos-1]
|
|
|
|
|
|
def getProcessingArguments(text):
|
|
|
regex = r"\s*!!\s*PROCESS\s*:(.*)"
|