def __error(self, error_message, position=None): """Report an error at the character just consumed.""" if position is None: position = max(0, self.__scanner.position - 1) raise JsonParseException( error_message, position, self.__scanner.line_number_for_offset( position - self.__scanner.position))
def parse_value(self): """Parses a Json value from the input.""" start_pos = self.__scanner.position try: c = self.__peek_next_non_whitespace() if c == "{": return self.__parse_object() elif c == "[": return self.__parse_array() elif c == '"': if self.__check_repeated_chars('"', offset=1, count=2): return self.__parse_triple_quoted_string() else: return self.__parse_string_with_concatenation() elif c == "t": self.__match("true", "unknown identifier") return True elif c == "f": self.__match("false", "unknown identifier") return False elif c == "n": self.__match("null", "unknown identifier") return None # end of the text or empty input. elif c is None: if start_pos == 0: return self.__error("Empty input") else: return self.__error("Unexpected end-of-text") # this must be after None check because we can not compare with None in python3 elif c == "-" or "0" <= c <= "9": return self.__parse_number() elif c == "}": return self.__error("'}' can only be used to end an object") elif c == "`": return self.__error( "Json_lib parser does not support length prefixed strings any more." ) else: return self.__error("Unexpected character '%s'" % c) except IndexError: raise JsonParseException( "Parser unexpectantly reached end of input probably due to " "an terminated string, object, or array", start_pos, self.__scanner.line_number_for_offset(start_pos), )
def parse_value(self): """Parses a Json value from the input.""" start_pos = self.__scanner.position try: c = self.__peek_next_non_whitespace() if c == '{': return self.__parse_object() elif c == '[': return self.__parse_array() elif c == '"': if self.__check_repeated_chars('"', offset=1, count=2): return self.__parse_triple_quoted_string() else: return self.__parse_string_with_concatenation() elif c == 't': self.__match("true", "unknown identifier") return True elif c == 'f': self.__match("false", "unknown identifier") return False elif c == 'n': self.__match("null", "unknown identifier") return None elif c == '-' or '0' <= c <= '9': return self.__parse_number() elif c == '}': return self.__error("'}' can only be used to end an object") elif c == '`': return self.__parse_length_prefixed_string() else: if c is None: if start_pos == 0: return self.__error("Empty input") else: return self.__error("Unexpected end-of-text") else: return self.__error("Unexpected character '%s'" % c) except IndexError: raise JsonParseException( "Parser unexpectantly reached end of input probably due to " "an terminated string, object, or array", start_pos, self.__scanner.line_number_for_offset(start_pos))