def GetParser(cls): if DEBUG: print("init StringLiteralParser") # match for opening " token = yield if (not isinstance(token, CharacterToken)): raise MismatchingParserResult() if (token.Value != "\""): raise MismatchingParserResult() # match for string: value value = "" wasEscapeSign = False while True: token = yield if isinstance(token, CharacterToken): if (token.Value == "\""): if (wasEscapeSign is True): wasEscapeSign = False value += "\"" continue else: break elif (token.Value == "\\"): if (wasEscapeSign is True): wasEscapeSign = False value += "\\" continue else: wasEscapeSign = True continue value += token.Value # construct result result = cls(value) if DEBUG: print("StringLiteralParser: matched {0}".format(result)) raise MatchingParserResult(result)
def GetParser(cls): if DEBUG: print("init IdentifierParser") name = "" while True: token = yield if isinstance(token, StringToken): name += token.Value elif isinstance(token, NumberToken): if (name != ""): name += token.Value else: raise MismatchingParserResult( "IdentifierParser: Expected identifier name. Got a number." ) elif (isinstance(token, CharacterToken) and (token.Value == "_")): name += token.Value elif (name == ""): raise MismatchingParserResult( "IdentifierParser: Expected identifier name.") else: break # construct result result = cls(name) if DEBUG: print("IdentifierParser: matched {0}".format(result)) raise GreedyMatchingParserResult(result)
def GetParser(cls): if DEBUG: print("init NotExpressionParser") child = None # match for "!" token = yield # if (not isinstance(token, StringToken)): raise MismatchingParserResult() # if (token.Value != "not"): raise MismatchingParserResult() if (not isinstance(token, CharacterToken)): raise MismatchingParserResult() if (token.Value != "!"): raise MismatchingParserResult() # match for optional whitespace token = yield if isinstance(token, SpaceToken): token = yield # match for sub expression # ========================================================================== parser = cls.__PARSER_EXPRESSIONS__.GetParser() parser.send(None) try: while True: parser.send(token) token = yield except MatchingParserResult as ex: child = ex.value # construct result result = cls(child) if DEBUG: print("NotExpressionParser: matched {0}".format(result)) raise MatchingParserResult(result)
def GetParser(cls): # match for optional whitespacex token = yield if isinstance(token, SpaceToken): token = yield # match for DELETE keyword if (not isinstance(token, StringToken)): raise MismatchingParserResult( "DeleteParser: Expected DELETE keyword.") if (token.Value.lower() != "delete"): raise MismatchingParserResult( "DeleteParser: Expected DELETE keyword.") # match for whitespace token = yield if (not isinstance(token, SpaceToken)): raise MismatchingParserResult( "DeleteParser: Expected whitespace before filename.") # match for string: file; use a StringLiteralParser to parse the pattern parser = StringLiteral.GetParser() parser.send(None) file = None try: while True: token = yield parser.send(token) except MatchingParserResult as ex: file = ex.value.Value # match for optional whitespace token = yield if isinstance(token, SpaceToken): token = yield # match for delimiter sign: \n commentText = "" if (not isinstance(token, CharacterToken)): raise MismatchingParserResult( "DeleteParser: Expected end of line or comment") if (token.Value == "\n"): pass elif (token.Value == "#"): # match for any until line end while True: token = yield if (isinstance(token, CharacterToken) and (token.Value == "\n")): break commentText += token.Value else: raise MismatchingParserResult( "DeleteParser: Expected end of line or comment") # construct result result = cls(file, commentText) raise MatchingParserResult(result)
def GetParser(cls): # match for optional whitespace token = yield if isinstance(token, SpaceToken): token = yield # match for keyword: APPENDLINE if (not isinstance(token, StringToken)): raise MismatchingParserResult( "AppendLineParser: Expected APPENDLINE keyword.") if (token.Value.lower() != "appendline"): raise MismatchingParserResult( "AppendLineParser: Expected APPENDLINE keyword.") # match for whitespace token = yield if (not isinstance(token, SpaceToken)): raise MismatchingParserResult( "AppendLineParser: Expected whitespace before append pattern.") # match for string: appendPattern; use a StringLiteralParser to parse the pattern parser = StringLiteral.GetParser() parser.send(None) appendPattern = None try: while True: token = yield parser.send(token) except MatchingParserResult as ex: appendPattern = ex.value.Value # match for optional whitespace token = yield if isinstance(token, SpaceToken): token = yield # match for delimiter sign: \n commentText = "" if (not isinstance(token, CharacterToken)): raise MismatchingParserResult( "AppendLineParser: Expected end of line or comment") if (token.Value == "\n"): pass elif (token.Value == "#"): # match for any until line end while True: token = yield if (isinstance(token, CharacterToken) and (token.Value == "\n")): break commentText += token.Value else: raise MismatchingParserResult( "AppendLineParser: Expected end of line or comment") # construct result result = cls(appendPattern, commentText) raise MatchingParserResult(result)
def GetParser(cls): # match for optional whitespace token = yield if isinstance(token, SpaceToken): token = yield # match for delimiter sign: \n if (not isinstance(token, CharacterToken)): raise MismatchingParserResult() if (token.Value.lower() != "\n"): raise MismatchingParserResult() # construct result result = cls() raise MatchingParserResult(result)
def GetParser(cls): if DEBUG: print("init ListElementParser") # match for EXISTS keyword token = yield if (not isinstance(token, CharacterToken)): raise MismatchingParserResult() if (token.Value != ","): raise MismatchingParserResult() # match for optional whitespace token = yield if isinstance(token, SpaceToken): token = yield parser = cls.__PARSER_LIST_ELEMENT_EXPRESSIONS__.GetParser() parser.send(None) while True: parser.send(token) token = yield
def GetParser(cls): # match for optional whitespace token = yield if isinstance(token, SpaceToken): token = yield # match for sign: # if (not isinstance(token, CharacterToken)): raise MismatchingParserResult() if (token.Value.lower() != "#"): raise MismatchingParserResult() # match for any until line end commentText = "" while True: token = yield if isinstance(token, CharacterToken): if (token.Value == "\n"): break commentText += token.Value # construct result result = cls(commentText) raise MatchingParserResult(result)
def GetParser(cls): if DEBUG: print("init IntegerLiteralParser") # match for opening " token = yield if (not isinstance(token, NumberToken)): raise MismatchingParserResult() value = int(token.Value) # construct result result = cls(value) if DEBUG: print("IntegerLiteralParser: matched {0}".format(result)) raise MatchingParserResult(result)
def GetParser(cls): result = cls() parser = cls.GetRepeatParser(result.AddStatement, DocumentStatements.GetParser) parser.send(None) token = yield if (not isinstance(token, StartOfDocumentToken)): raise MismatchingParserResult("Expected a StartOfDocumentToken, got {0!s}.".format(token)) try: while True: token = yield parser.send(token) except MatchingParserResult: raise MatchingParserResult(result)
def GetParser(cls): # match for optional whitespacex token = yield if isinstance(token, SpaceToken): token = yield # match for COPY keyword if (not isinstance(token, StringToken)): raise MismatchingParserResult("CopyParser: Expected COPY keyword.") if (token.Value.lower() != "copy"): raise MismatchingParserResult("CopyParser: Expected COPY keyword.") # match for whitespace token = yield if (not isinstance(token, SpaceToken)): raise MismatchingParserResult( "CopyParser: Expected whitespace before source filename.") # match for string: sourceFile; use a StringLiteralParser to parse the pattern parser = StringLiteral.GetParser() parser.send(None) sourceFile = None try: while True: token = yield parser.send(token) except MatchingParserResult as ex: sourceFile = ex.value.Value # match for whitespace token = yield if (not isinstance(token, SpaceToken)): raise MismatchingParserResult( "CopyParser: Expected whitespace before TO keyword.") # match for TO keyword token = yield if (not isinstance(token, StringToken)): raise MismatchingParserResult("CopyParser: Expected TO keyword.") if (token.Value.lower() != "to"): raise MismatchingParserResult("CopyParser: Expected TO keyword.") # match for whitespace token = yield if (not isinstance(token, SpaceToken)): raise MismatchingParserResult( "CopyParser: Expected whitespace before destination directory." ) # match for string: destinationDirectory; use a StringLiteralParser to parse the pattern parser = StringLiteral.GetParser() parser.send(None) destinationDirectory = None try: while True: token = yield parser.send(token) except MatchingParserResult as ex: destinationDirectory = ex.value.Value # match for optional whitespace token = yield if isinstance(token, SpaceToken): token = yield # match for delimiter sign: \n commentText = "" if (not isinstance(token, CharacterToken)): raise MismatchingParserResult( "CopyParser: Expected end of line or comment") if (token.Value == "\n"): pass elif (token.Value == "#"): # match for any until line end while True: token = yield if (isinstance(token, CharacterToken) and (token.Value == "\n")): break commentText += token.Value else: raise MismatchingParserResult( "CopyParser: Expected end of line or comment") # construct result result = cls(sourceFile, destinationDirectory, commentText) raise MatchingParserResult(result)
def GetParser(cls): # match for optional whitespace token = yield if isinstance(token, SpaceToken): token = yield # match for keyword: __PARSER_BLOCK_NAME__ if (not isinstance(token, StringToken)): raise MismatchingParserResult(cls.__PARSER_NAME__ + ": Expected " + cls.__PARSER_BLOCK_NAME__ + " keyword.") if (token.Value.lower() != cls.__PARSER_BLOCK_NAME__): raise MismatchingParserResult(cls.__PARSER_NAME__ + ": Expected " + cls.__PARSER_BLOCK_NAME__ + " keyword.") # match for optional whitespace token = yield if isinstance(token, SpaceToken): token = yield # match for delimiter sign: \n commentText = "" if (not isinstance(token, CharacterToken)): raise MismatchingParserResult(cls.__PARSER_NAME__ + ": Expected end of line or comment") if (token.Value == "\n"): pass elif (token.Value == "#"): # match for any until line end while True: token = yield if (isinstance(token, CharacterToken) and (token.Value == "\n")): break commentText += token.Value else: raise MismatchingParserResult(cls.__PARSER_NAME__ + ": Expected end of line or comment") # match for inner statements # ========================================================================== # construct result result = cls(commentText) parser = cls.GetRepeatParser(result.AddStatement, cls.__PARSER_STATEMENTS__.GetParser) parser.send(None) try: while True: token = yield parser.send(token) except MatchingParserResult: pass # match for END __PARSER_BLOCK_NAME__ # ========================================================================== # match for optional whitespace if isinstance(token, SpaceToken): token = yield # match for keyword: END if (not isinstance(token, StringToken)): raise MismatchingParserResult(cls.__PARSER_NAME__ + ": Expected END keyword.") if (token.Value.lower() != "end"): raise MismatchingParserResult(cls.__PARSER_NAME__ + ": Expected END keyword.") # match for whitespace token = yield if (not isinstance(token, SpaceToken)): raise MismatchingParserResult(cls.__PARSER_NAME__ + ": Expected whitespace before " + cls.__PARSER_BLOCK_NAME__ + " keyword.") # match for keyword: __PARSER_BLOCK_NAME__ token = yield if (not isinstance(token, StringToken)): raise MismatchingParserResult(cls.__PARSER_NAME__ + ": Expected " + cls.__PARSER_BLOCK_NAME__ + " keyword.") if (token.Value.lower() != cls.__PARSER_BLOCK_NAME__): raise MismatchingParserResult(cls.__PARSER_NAME__ + ": Expected " + cls.__PARSER_BLOCK_NAME__ + " keyword.") # match for optional whitespace token = yield if isinstance(token, SpaceToken): token = yield # match for delimiter sign: \n # commentText = "" if (not isinstance(token, CharacterToken)): raise MismatchingParserResult(cls.__PARSER_NAME__ + ": Expected end of line or comment") if (token.Value == "\n"): pass elif (token.Value == "#"): # match for any until line end while True: token = yield if (isinstance(token, CharacterToken) and (token.Value == "\n")): break # commentText += token.Value else: raise MismatchingParserResult(cls.__PARSER_NAME__ + ": Expected end of line or comment") raise MatchingParserResult(result)
def GetParser(cls): # match for IN ... FILE clause # ========================================================================== # match for optional whitespace token = yield if isinstance(token, SpaceToken): token = yield # match for keyword: FILE if (not isinstance(token, StringToken)): raise MismatchingParserResult("FileParser: Expected FILE keyword.") if (token.Value.lower() != "file"): raise MismatchingParserResult("FileParser: Expected FILE keyword.") # match for whitespace token = yield if (not isinstance(token, SpaceToken)): raise MismatchingParserResult( "FileParser: Expected whitespace before filename.") # match for string: replaceFilename; use a StringLiteralParser to parse the pattern parser = StringLiteral.GetParser() parser.send(None) replaceFilename = None try: while True: token = yield parser.send(token) except MatchingParserResult as ex: replaceFilename = ex.value.Value # match for optional whitespace token = yield if isinstance(token, SpaceToken): token = yield # match for delimiter sign: \n commentText = "" if (not isinstance(token, CharacterToken)): raise MismatchingParserResult( "FileParser: Expected end of line or comment") if (token.Value == "\n"): pass elif (token.Value == "#"): # match for any until line end while True: token = yield if (isinstance(token, CharacterToken) and (token.Value == "\n")): break commentText += token.Value else: raise MismatchingParserResult( "FileParser: Expected end of line or comment") # match for inner statements # ========================================================================== # construct result result = cls(replaceFilename, commentText) parser = cls.GetRepeatParser(result.AddStatement, InFileStatements.GetParser) parser.send(None) try: while True: token = yield parser.send(token) except EmptyChoiseParserResult: print("ERROR in *.rules file -> fix me") except MatchingParserResult: pass # match for END FILE clause # ========================================================================== # match for optional whitespace if isinstance(token, SpaceToken): token = yield # match for keyword: END if (not isinstance(token, StringToken)): raise MismatchingParserResult("FileParser: Expected END keyword.") if (token.Value.lower() != "end"): raise MismatchingParserResult("FileParser: Expected END keyword.") # match for whitespace token = yield if (not isinstance(token, SpaceToken)): raise MismatchingParserResult( "FileParser: Expected whitespace before FILE keyword.") # match for keyword: FILE token = yield if (not isinstance(token, StringToken)): raise MismatchingParserResult("FileParser: Expected FILE keyword.") if (token.Value.lower() != "file"): raise MismatchingParserResult("FileParser: Expected FILE keyword.") # match for optional whitespace token = yield if isinstance(token, SpaceToken): token = yield # match for delimiter sign: \n if (not isinstance(token, CharacterToken)): raise MismatchingParserResult( "FileParser: Expected end of line or comment") if (token.Value == "\n"): pass elif (token.Value == "#"): # match for any until line end while True: token = yield if (isinstance(token, CharacterToken) and (token.Value == "\n")): break else: raise MismatchingParserResult( "FileParser: Expected end of line or comment") result.CommentText = commentText raise MatchingParserResult(result)
def GetParser(cls): multiLine = False dotAll = False caseInsensitive = False # match for optional whitespace token = yield if isinstance(token, SpaceToken): token = yield # match for keyword: REPLACE if (not isinstance(token, StringToken)): raise MismatchingParserResult( "ReplaceParser: Expected REPLACE keyword.") if (token.Value.lower() != "replace"): raise MismatchingParserResult( "ReplaceParser: Expected REPLACE keyword.") # match for whitespace token = yield if (not isinstance(token, SpaceToken)): raise MismatchingParserResult( "ReplaceParser: Expected whitespace before search pattern.") # match for string: searchPattern; use a StringLiteralParser to parse the pattern parser = StringLiteral.GetParser() parser.send(None) searchPattern = None try: while True: token = yield parser.send(token) except MatchingParserResult as ex: searchPattern = ex.value.Value # match for whitespace token = yield if (not isinstance(token, SpaceToken)): raise MismatchingParserResult( "ReplaceParser: Expected whitespace before WITH keyword.") # match for WITH keyword token = yield if (not isinstance(token, StringToken)): raise MismatchingParserResult( "ReplaceParser: Expected WITH keyword.") if (token.Value.lower() != "with"): raise MismatchingParserResult( "ReplaceParser: Expected WITH keyword.") # match for whitespace token = yield if (not isinstance(token, SpaceToken)): raise MismatchingParserResult( "ReplaceParser: Expected whitespace before replace pattern.") # match for string: replacePattern; use a StringLiteralParser to parse the pattern parser = StringLiteral.GetParser() parser.send(None) replacePattern = None try: while True: token = yield parser.send(token) except MatchingParserResult as ex: replacePattern = ex.value.Value # match for optional whitespace token = yield if isinstance(token, SpaceToken): token = yield # match for line end, comment or OPTIONS keyword if isinstance(token, StringToken): if (token.Value.lower() == "options"): # match for whitespace token = yield if (not isinstance(token, SpaceToken)): raise MismatchingParserResult( "ReplaceParser: Expected whitespace before MULTILINE, DOTALL or CASEINSENSITIVE keyword." ) token = yield # first keyword for _ in range(3): # match for MULTILINE, DOTALL or CASEINSENSITIVE keyword if (not isinstance(token, StringToken)): raise MismatchingParserResult( "ReplaceParser: Expected MULTILINE, DOTALL or CASEINSENSITIVE keyword." ) if (token.Value.lower() == "multiline"): multiLine = True elif (token.Value.lower() == "dotall"): dotAll = True elif (token.Value.lower() == "caseinsensitive"): caseInsensitive = True else: raise MismatchingParserResult( "ReplaceParser: Expected MULTILINE, DOTALL or CASEINSENSITIVE keyword." ) # match for optional whitespace token = yield if isinstance(token, SpaceToken): token = yield if (not isinstance(token, CharacterToken)): raise MismatchingParserResult( "ReplaceParser: Expected more options, end of line or comment." ) if (token.Value == ","): # match for optional whitespace, before going into the next iteration token = yield if isinstance(token, SpaceToken): token = yield continue # with 'token' set to next keyword else: break # match for delimiter sign: \n or # commentText = "" if (not isinstance(token, CharacterToken)): raise MismatchingParserResult( "ReplaceParser: Expected end of line or comment") if (token.Value == "\n"): pass elif (token.Value == "#"): # match for any until line end while True: token = yield if (isinstance(token, CharacterToken) and (token.Value == "\n")): break commentText += token.Value else: raise MismatchingParserResult( "ReplaceParser: Expected end of line or comment") # construct result result = cls(searchPattern, replacePattern, caseInsensitive, multiLine, dotAll, commentText) raise MatchingParserResult(result)
def GetParser(cls): if DEBUG: print("init " + cls.__PARSER_NAME__) leftChild = None rightChild = None # match for opening ( token = yield if (not isinstance(token, CharacterToken)): raise MismatchingParserResult() if (token.Value != "("): raise MismatchingParserResult() # match for optional whitespace token = yield if isinstance(token, SpaceToken): token = yield # match for sub expression # ========================================================================== parser = cls.__PARSER_LHS_EXPRESSIONS__.GetParser() parser.send(None) try: while True: parser.send(token) token = yield except GreedyMatchingParserResult as ex: leftChild = ex.value except MatchingParserResult as ex: leftChild = ex.value token = yield # match for optional whitespace if isinstance(token, SpaceToken): token = yield # match for operator keyword or sign(s) if isinstance(cls.__PARSER_OPERATOR__, str): if (not isinstance(token, StringToken)): raise MismatchingParserResult() if (token.Value != cls.__PARSER_OPERATOR__): raise MismatchingParserResult() token = yield if (not isinstance(token, SpaceToken)): raise MismatchingParserResult() token = yield elif isinstance(cls.__PARSER_OPERATOR__, tuple): for sign in cls.__PARSER_OPERATOR__: if (not isinstance(token, CharacterToken)): raise MismatchingParserResult() if (token.Value != sign): raise MismatchingParserResult() token = yield # match for optional whitespace if isinstance(token, SpaceToken): token = yield elif isinstance(cls.__PARSER_OPERATOR__, list): for kw in cls.__PARSER_OPERATOR__[:-1]: if (not isinstance(token, StringToken)): raise MismatchingParserResult() if (token.Value != kw): raise MismatchingParserResult() token = yield if (not isinstance(token, SpaceToken)): raise MismatchingParserResult() token = yield kw = cls.__PARSER_OPERATOR__[-1] if (not isinstance(token, StringToken)): raise MismatchingParserResult() if (token.Value != kw): raise MismatchingParserResult() token = yield if (not isinstance(token, SpaceToken)): raise MismatchingParserResult() token = yield # match for sub expression # ========================================================================== parser = cls.__PARSER_RHS_EXPRESSIONS__.GetParser() parser.send(None) try: while True: parser.send(token) token = yield except GreedyMatchingParserResult as ex: rightChild = ex.value except MatchingParserResult as ex: rightChild = ex.value token = yield # match for optional whitespace if isinstance(token, SpaceToken): token = yield # match for closing ) if (not isinstance(token, CharacterToken)): raise MismatchingParserResult() if (token.Value != ")"): raise MismatchingParserResult() # construct result result = cls(leftChild, rightChild) if DEBUG: print(cls.__PARSER_NAME__ + ": matched {0}".format(result)) raise MatchingParserResult(result)