def parse(self, value): try: value = int(value) if value not in (0, 1): raise ParserError("Expected a 0 or 1.") return value except: raise ParserError("Expected a 0 or 1.")
def parse(self,value): try: value = bool(value) if value not in (True,False): raise ParserError("Expected False or True.") return value except: raise ParserError("Expected False or True.")
def parse(self, value): try: # Allow clients to be compatible with old FIFE versions if value in ["0", "False", "false"]: value = False elif value in ["1", "True", "true"]: value = True if value not in (True, False): raise ParserError(str(self.name) + " expected False or True.") return value except: raise ParserError(str(self.name) + " expected False or True.")
def parse(self,value): a = 255 try: try: r,g,b,a = tuple(map(int,str(value).split(','))) for c in (r,g,b,a): if not 0 <= c < 256: raise ParserError("Expected a color (Failed: 0 <= %d <= 255)" %c) except: r,g,b = tuple(map(int,str(value).split(','))) for c in (r,g,b): if not 0 <= c < 256: raise ParserError("Expected a color (Failed: 0 <= %d <= 255)" %c) except: raise ParserError("Expected a color.") return r,g,b,a
def __init__(self, text: str = ""): self.scanner: Scanner = Scanner(text) stringsAndTokens = self.scanner.get_strings_and_tokens_list() try: self.parser: Parser = Parser(stringsAndTokens) except: raise ParserError()
def set_text(self, text: str) -> None: self.scanner.set_text(text) stringsAndTokens = self.scanner.get_strings_and_tokens_list() try: self.parser: Parser = Parser(stringsAndTokens) except: raise ParserError()
def parse(self, value): try: result = map(unicode, str(value).split(',')) return result except: raise ParserError( str(self.name) + " expected a list with unicode strings.")
def parse(self, value): try: x, y = tuple(map(int, str(value).split(','))) return x, y except: raise ParserError( "Expected a comma separated list of two integers.")
def parse(self, value): try: result = [] # we use ';' to split, because of point and color attributes # example: "Int:5; Color:255,0,255" tmp_result = map(str, str(value).split(';')) for r in tmp_result: type = str(r).split(':')[0].strip() val = str(r).split(':')[1].strip() m = ATTRIBUTE_TYPE.get(type) if m is None: raise ParserError("Unknown attribute type " + type + ".") method = getattr(m(self), 'parse') result.append(method(val)) return result except: raise ParserError(str(self.name) + " expected a mixed list.")
def parse(self, value): try: result = map(float, str(value).split(',')) return result except: raise ParserError(str(self.name) + " expected a list with floats.")
def parse(self, value): try: return float(value) except: raise ParserError(str(self.name) + " expected a float.")
def parse(self, value): try: return int(value) except: raise ParserError(str(self.name) + " expected a single integer.")
def parse(self, value): try: return float(value) except: raise ParserError("Expected a float.")
def error(self, message): print(message, '\n') self.print_help() raise ParserError(message)
def build_tree(tokens): """Create a parse tree from token list. :param tokens: List of tokens. :type tokens: list :return: Parse tree root node. """ node = Node() i = 0 while i < len(tokens): token = tokens[i] # First token on the list or a comma separator if i == 0 or token.type is Separator.COMMA: node.parts.append(SelectNode()) # FROM elif token.type is Command.FROM: pyser_assert(node.source is None, ParserError('Multiple source at %s in %s' % (i, tokens))) next_token = tokens[i+1] pyser_assert(next_token.type is Bracket.LEFT, ParserError.grammar(i, tokens)) bracket_count = 1 end = i+3 while True: if 0 == bracket_count: break pyser_assert(end != len(tokens), ParserError.unbalanced(i+1, tokens)) if tokens[end].type is Bracket.LEFT: bracket_count += 1 elif tokens[end].type is Bracket.RIGHT: bracket_count -= 1 end += 1 end -= 1 logger.debug('Recursion at %s-%s' % (i+1, end)) node.source = build_tree(tokens[i+2:end]) i = end # <RE> elif token.type is RegularExpression.RE: pyser_assert(node.parts[-1].filter_ is None, ParserError.repeated('filter', i, tokens)) node.parts[-1].filter_ = token.token[1:-1] # AS <ID> elif token.type is Command.AS: pyser_assert(node.parts[-1].identifier is None, ParserError.repeated('identifier', i, tokens)) i += 1 next_token = tokens[i] pyser_assert(next_token.type is Identifier.ID, ParserError.grammar(i, tokens)) node.parts[-1].identifier = next_token # ORDER ASC, ORDER DESC, ORDER <RE> elif token.type in [Command.ORDER_ASC, Command.ORDER_DESC, Command.ORDER]: pyser_assert(not types_in([Command.ORDER, Command.ORDER_ASC, Command.ORDER_DESC], node.post_processors), ParserError.repeated('order', i, tokens)) node.post_processors.append(token) if token.type is Command.ORDER: i += 1 next_token = tokens[i] pyser_assert(next_token.type is RegularExpression.RE, ParserError.grammar(i, tokens)) node.post_processors.append(next_token) # LIMIT <INT> elif token.type in [Command.LIMIT]: pyser_assert(not type_in(Command.LIMIT, node.post_processors), ParserError.repeated('limit', i, tokens)) node.post_processors.append(token) i += 1 next_token = tokens[i] pyser_assert(next_token.type is Numeric.INT, ParserError.grammar(i, tokens)) node.post_processors.append(next_token) # GROUP BY <RE> elif token.type is Command.GROUP: #pyser_assert(not type_in(Command.GROUP, node.post_processors), # ParserError.repeated('group', i, tokens)) #node.post_processors.append(token) i += 1 next_token = tokens[i] pyser_assert(next_token.type is RegularExpression.RE, ParserError.grammar(i, tokens)) #node.post_processors.append(next_token) node.group = next_token # SUM, MIN, MAX, COUNT, FUNCTION() else: logger.debug('append %s -> %s' % (i, token)) node.parts[-1].commands.append(token) i += 1 logger.debug('parts: %s' % node.parts) return node
def error(cls, error_code: ErrorCode, token: Token) -> NoReturn: raise ParserError( error_code=error_code, token=token, message=f"{error_code.value} \n\t{token}", )