def accept(self, text): """ If symbol is at the beginning of the text, is removed from the text and reminder is returned. Otherwise accept_exception is raised. :param text: Text to be parsed. :type text: string :returns: Text without begining. :rtype: string :rises: accept_exception if symbol is not at the begining of the text. """ acceptable = True epsCnt = 0 search_len = len(self.kchar) - self.last if len(text) < search_len: raise pattern_exceptions.symbol_string_to_short() for i in range(0, search_len): if isinstance(self.kchar[i], frozenset): kacceptable = False for char in self.kchar[i]: if char == "": kacceptable = True epsCnt += 1 if text[i] == char[0]: kacceptable = True if kacceptable == True: acceptable = True else: raise pattern_exceptions.symbol_accept_exception() else: if self.kchar[i] == "": acceptable = True epsCnt += 1 if text[i] == self.kchar[i][0]: acceptable = True else: raise pattern_exceptions.symbol_accept_exception() if self.eof == True and len(text) > search_len: acceptable = False if acceptable == True: if epsCnt == len(self.kchar): return text else: return text[search_len:] else: raise pattern_exceptions.symbol_accept_exception()
def accept(self, text): """ If symbol is at the beginning of the text, it's removed from the text and reminder is returned. Otherwise accept_exception is raised. :param text: Text to be parsed. :type text: string :returns: Text without begining. :rtype: string :rises: accept_exception if symbol is not at the begining of the text. """ found = False output = str() if len(text) == 0: raise pattern_exceptions.symbol_string_to_short() if "" in self.charClass: found = True output = text if text[0] in self.charClass: found = True output = text[1:] if found == True: return output else: raise pattern_exceptions.symbol_accept_exception()
def accept(self, text): """ If symbol is at the beginning of the text, is removed from the text and reminder is returned. Otherwise accept_exception is raised. :param text: Text to be parsed. :type text: string :returns: Text without begining. :rtype: string :rises: accept_exception if symbol is not at the begining of the text. """ if len(self.string) == 0: return text max_len = len(self.string) * self.stride - self.last if len(text) < max_len: raise pattern_exceptions.symbol_string_to_short() for i in range(0, len(self.string)): for j in range(0, self.stride): matched = False position = i * self.stride + j if position < max_len: for char in self.string[i][j]: if text[position] == self.string[i][j]: matched = True if matched == False: raise pattern_exceptions.symbol_accept_exception() return text[max_len:]
def accept(self, text): """ If symbol is at the beginning of the text, is removed from the text and reminder is returned. Otherwise accept_exception is raised. :param text: Text to be parsed. :type text: string :returns: Text without begining. :rtype: string :rises: accept_exception if symbol is not at the begining of the text. """ if self.string == "": return text if len(text) < len(self.string): raise pattern_exceptions.symbol_string_to_short() for i in range(0, len(self.string)): matched = False for char in self.string[i]: if text[i] == self.string[i]: matched = True if matched == False: raise pattern_exceptions.symbol_accept_exception() return text[len(self.string):]
def accept(self, text): """ If symbol is at the beginning of the text, is removed from the text and reminder is returned. Otherwise accept_exception is raised. :param text: Text to be parsed. :type text: string :returns: Text without begining. :rtype: string :rises: accept_exception if symbol is not at the begining of the text. """ if len(text) == 0: return text else: raise pattern_exceptions.symbol_accept_exception()
def accept(self, text): """ If symbol is at the beginning of the text, is removed from the text and reminder is returned. Otherwise accept_exception is raised. Behavior of this method is controled by attributes greedy and limit. The limit attribute has bigger priority than the greedy one. Limit sets exact number of accepted characters. If -1 is set, the greedy attribute will be used. If greedy is True, the accept() method will consume as much characters from input string as posible. If greedy is False, the accept() mathod will consume only minimal number of characters from input string with respect to the m parameter. :param text: Text to be parsed. :type text: string :returns: Text without begining. :rtype: string :rises: accept_exception if symbol is not at the begining of the text. """ if self.limit > -1: if self.limit < self.m or self.limit > self.n: raise pattern_exceptions.symbol_accept_exception() if len(text) < self.limit: raise pattern_exceptions.symbol_accept_exception() if self.limit == 0: return text for i in range(0, self.limit): if isinstance(self.symbol, set) or isinstance(self.symbol, frozenset): accepted = False for char in self.symbol: if char[0] == text[i]: accepted = True if accepted == False: raise pattern_exceptions.symbol_accept_exception() else: if self.symbol[0] != text[i]: raise pattern_exceptions.symbol_accept_exception() return text[self.limit:] else: index = 0 if len(text) < self.m: raise pattern_exceptions.symbol_accept_exception() for i in range(0, len(text)+1): if index == self.m and self.greedy == False: break if index == self.n: break if i == len(text): return "" if isinstance(self.symbol, set) or isinstance(self.symbol, frozenset): accepted = False for char in self.symbol: if char[0] == text[i]: accepted = True if accepted == False: if index < self.m: raise pattern_exceptions.symbol_accept_exception() else: return text[index:] else: if self.symbol[0] != text[i]: if index < self.m: raise pattern_exceptions.symbol_accept_exception() else: return text[index:] index = i return text[index:]
def accept(self, text): """ If symbol is at the beginning of the text, is removed from the text and reminder is returned. Otherwise accept_exception is raised. Behavior of this method is controled by attributes greedy and limit. The limit attribute has bigger priority than the greedy one. Limit sets exact number of accepted characters. If -1 is set, the greedy attribute will be used. If greedy is True, the accept() method will consume as much characters from input string as posible. If greedy is False, the accept() mathod will consume only minimal number of characters from input string with respect to the m parameter. :param text: Text to be parsed. :type text: string :returns: Text without begining. :rtype: string :rises: accept_exception if symbol is not at the begining of the text. """ if self.limit > -1: if self.limit < self.m or self.limit > self.n: raise pattern_exceptions.symbol_accept_exception() if len(text) < self.limit: raise pattern_exceptions.symbol_accept_exception() if self.limit == 0: return text for i in range(0, self.limit): if isinstance(self.symbol, set) or isinstance( self.symbol, frozenset): accepted = False for char in self.symbol: if char[0] == text[i]: accepted = True if accepted == False: raise pattern_exceptions.symbol_accept_exception() else: if self.symbol[0] != text[i]: raise pattern_exceptions.symbol_accept_exception() return text[self.limit:] else: index = 0 if len(text) < self.m: raise pattern_exceptions.symbol_accept_exception() for i in range(0, len(text) + 1): if index == self.m and self.greedy == False: break if index == self.n: break if i == len(text): return "" if isinstance(self.symbol, set) or isinstance( self.symbol, frozenset): accepted = False for char in self.symbol: if char[0] == text[i]: accepted = True if accepted == False: if index < self.m: raise pattern_exceptions.symbol_accept_exception() else: return text[index:] else: if self.symbol[0] != text[i]: if index < self.m: raise pattern_exceptions.symbol_accept_exception() else: return text[index:] index = i return text[index:]