def checkSyntax(self): test = self.input.lstrip() test = test.rstrip() stringOpen = False blockOpen = False expOpen = False blockOpenCounter = 0 blockCloseCounter = 0 expOpenCounter = 0 expCloseCounter = 0 expIndex = 0 guardIndex = 0 equals = False not_equals = False for x in range(0, len(test)): if (test[x] == '"'): if (stringOpen == False): stringOpen = True else: stringOpen = False if (test[x] == '{'): if(blockOpenCounter == 0): blockOpen = True blockOpenCounter += 1 if (test[x] == '}'): blockCloseCounter += 1 if (blockOpenCounter == blockCloseCounter): blockOpen = False if (test[x] == '('): if(expOpenCounter == 0): expOpen = True expOpenCounter += 1 if (test[x] == ')'): expCloseCounter += 1 if(expCloseCounter == expOpenCounter): expOpen = False if (stringOpen == False and blockOpen == False and expOpen == False): if (expIndex == 0 and ((test[x] == '=') or (test[x] == '#'))): if(test[x] == '='): equals = True if(test[x] == '#'): not_equals = True e1 = Expression(test[:x-1],self.getParent()) e1.setSyntaxOnly(self.syntax_only) expIndex = x+2 elif (test[x] == ','): if (expIndex == 0): print ("Missing expression in Guard-Command: " + test + "\nPlease check correct Syntax: expression ('='|'#') expression [',' guard]") return -1 e2 = Expression(test[expIndex:x]) e2.setSyntaxOnly(self.syntax_only) guardIndex = x+1 break if(expIndex == 0): print ("Missing '=' or '#' found in Guard-Command: " + test + "\nPlease check correct Syntax: expression ('='|'#') expression [',' guard]") return -1 if(guardIndex == 0): e2 = Expression(test[expIndex:],self.getParent()) e2.setSyntaxOnly(self.syntax_only) g = Guard("",self.getParent()) g.setSyntaxOnly(self.syntax_only) else: g = Guard(test[guardIndex:],self.getParent()) g.setSyntaxOnly(self.syntax_only) if not self.syntax_only: return (self.checkSemantic(e1,e2,g,equals,not_equals)) else: if(g.getInput() != ""): if (e1.checkSyntax() and e2.checkSyntax() and g.checkSyntax()): return 1 else: return -1 else: if (e1.checkSyntax() and e2.checkSyntax()): return 1 else: return -1
def checkSyntax(self): test = self.input.strip() #print test oBI = -1 #openBracketIndex oPNI = -1 #openPointerOrNameIndex oPI = -1 #openPrintIndexx openBrackets = 0 qOpen = 0 #quotesOpen (yes/no) concluded = 1 outerblock = 0 # is there an easier way than this huge loop? for x in range(0, len(test)): if test[x] == '"': #we do not expect to find " within a string literal (no escape character detection) if qOpen == 1: qOpen = 0 else: qOpen = 1 if (oPI == -1) and (oPNI == -1) and (oBI == -1): if test[x] == '[': oBI = x openBrackets = self.manageBrackets(openBrackets, qOpen, 1) elif test[x] == '^': oPI = x concluded = 0 elif (test[x] == '*') or (test[x] == '"') or test[x].isalpha(): oPNI = x concluded = 0 elif (test[x] == '{') or (test[x] == '('): oPNI = x openBrackets = self.manageBrackets(openBrackets, qOpen, 1) concluded = 0 elif not (oBI == -1): if test[x] == '[' or test[x] == '{' or test[x] == '(': openBrackets = self.manageBrackets(openBrackets, qOpen, 1) elif test[x] == ']' or test[x] == '}' or test[x] == ')': if openBrackets == 1 and test[x] == ']': if not self.checkGuardPart( test[oBI:x + 1]): # on true handling below return False else: oBI = -1 concluded = 1 if not self.syntax_only: self.syntax_only = self.getParent().getSyntaxOnly() openBrackets = self.manageBrackets(openBrackets, qOpen, -1) elif not (oPI == -1): if test[x] == '{' or test[x] == '[' or test[x] == '(': openBrackets = self.manageBrackets(openBrackets, qOpen, 1) elif test[x] == '}' or test[x] == ']' or test[x] == ')': openBrackets = self.manageBrackets(openBrackets, qOpen, -1) elif (openBrackets == 0) and (test[x] == ';'): e = Expression(test[oPI + 1:x], self.getParent()) e.setSyntaxOnly(self.syntax_only) if not e.checkSyntax(): return False else: if (not self.syntax_only): eList = e.getPropertyList() self.getParent().setPropertyList( eList) #TODO choose one? self.property_list = eList self.getParent().setSyntaxOnly(True) oPI = -1 concluded = 1 if not self.syntax_only: self.syntax_only = self.getParent().getSyntaxOnly() elif not (oPNI == -1): if test[x] == '{' or test[x] == '[' or test[x] == '(': openBrackets = self.manageBrackets(openBrackets, qOpen, 1) elif test[x] == '}' or test[x] == ']' or test[x] == ')': openBrackets = self.manageBrackets(openBrackets, qOpen, -1) elif (openBrackets == 0) and (test[x] == ';'): namelength = 0 if (test[oPNI] == '*' or test[oPNI].isalpha()): if (not self.syntax_only): outerblock = self.countStartOccurrences( test[oPNI:], '*') if '=' in test[oPNI:]: namelength = test[oPNI:].index('=') + 1 expressionString = test[oPNI + namelength:x].strip() e = Expression(expressionString, self.getParent()) if (self.isBlock(expressionString)): e.setSyntaxOnly(True) else: e.setSyntaxOnly(self.syntax_only) if not e.checkSyntax(): return False else: if (not self.syntax_only): curr = self.getParent() name = test[oPNI + outerblock:oPNI + namelength - 1].strip() curr = self.escapeNestings(curr, outerblock) if curr == self.getParent(): if (namelength != 0 and self.isBlock(expressionString) ): #must have found an assignment curr.getPropertyList().addProperty( name, expressionString) else: curr.getPropertyList().addProperty( name, e.getPropertyList()) #print name + " (1)" #e.getPropertyList().printList() else: if (namelength != 0 and self.isBlock(expressionString)): curr.getPropertyList().changeProperty( name, expressionString) else: curr.getPropertyList().changeProperty( name, e.getPropertyList()) #print name + " (2)" #e.getPropertyList().printList() outerblock = 0 oPNI = -1 concluded = 1 if not self.syntax_only: self.syntax_only = self.getParent().getSyntaxOnly() if openBrackets > 0: print "There are " + str( openBrackets ) + " unclosed brackets in:\n" + test + "\nIt is not a valid list of commands." return False elif concluded == 0: print "There is an unfinished command in:\n" + test + "\nA ';' is missing." return False return True
def checkSyntax(self): test = self.input.lstrip() test = test.rstrip() stringOpen = False blockOpen = False expOpen = False blockOpenCounter = 0 blockCloseCounter = 0 expOpenCounter = 0 expCloseCounter = 0 expIndex = 0 guardIndex = 0 equals = False not_equals = False for x in range(0, len(test)): if (test[x] == '"'): if (stringOpen == False): stringOpen = True else: stringOpen = False if (test[x] == '{'): if (blockOpenCounter == 0): blockOpen = True blockOpenCounter += 1 if (test[x] == '}'): blockCloseCounter += 1 if (blockOpenCounter == blockCloseCounter): blockOpen = False if (test[x] == '('): if (expOpenCounter == 0): expOpen = True expOpenCounter += 1 if (test[x] == ')'): expCloseCounter += 1 if (expCloseCounter == expOpenCounter): expOpen = False if (stringOpen == False and blockOpen == False and expOpen == False): if (expIndex == 0 and ((test[x] == '=') or (test[x] == '#'))): if (test[x] == '='): equals = True if (test[x] == '#'): not_equals = True e1 = Expression(test[:x - 1], self.getParent()) e1.setSyntaxOnly(self.syntax_only) expIndex = x + 2 elif (test[x] == ','): if (expIndex == 0): print( "Missing expression in Guard-Command: " + test + "\nPlease check correct Syntax: expression ('='|'#') expression [',' guard]" ) return -1 e2 = Expression(test[expIndex:x]) e2.setSyntaxOnly(self.syntax_only) guardIndex = x + 1 break if (expIndex == 0): print( "Missing '=' or '#' found in Guard-Command: " + test + "\nPlease check correct Syntax: expression ('='|'#') expression [',' guard]" ) return -1 if (guardIndex == 0): e2 = Expression(test[expIndex:], self.getParent()) e2.setSyntaxOnly(self.syntax_only) g = Guard("", self.getParent()) g.setSyntaxOnly(self.syntax_only) else: g = Guard(test[guardIndex:], self.getParent()) g.setSyntaxOnly(self.syntax_only) if not self.syntax_only: return (self.checkSemantic(e1, e2, g, equals, not_equals)) else: if (g.getInput() != ""): if (e1.checkSyntax() and e2.checkSyntax() and g.checkSyntax()): return 1 else: return -1 else: if (e1.checkSyntax() and e2.checkSyntax()): return 1 else: return -1