/* * This file is a part of PikoTools * and is distributed under the (new) BSD licence. * Author: Tomasz Sowa */ /* * Copyright (c) 2012-2021, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * * Neither the name Tomasz Sowa nor the names of contributors to this * project may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef headerfile_picotools_space_jsonspaceparser #define headerfile_picotools_space_jsonspaceparser #include #include "space.h" #include "convert/baseparser.h" namespace pt { class SpaceParser : public BaseParser { public: /* ctor -- setting default values (SetDefault() method) */ SpaceParser(); /* status of parsing */ enum Status { ok, cant_open_file, syntax_error }; /* the last status of parsing, set by parse() methods */ Status status; /* main methods used to parse a JSON file file_name is the path to a file */ Status parse_json_file(const char * file_name, Space & out_space, bool clear_space = true); Status parse_json_file(const std::string & file_name, Space & out_space, bool clear_space = true); Status parse_json_file(const wchar_t * file_name, Space & out_space, bool clear_space = true); Status parse_json_file(const std::wstring & file_name, Space & out_space, bool clear_space = true); /* main methods used to parse a Space file file_name is the path to a file */ Status parse_space_file(const char * file_name, Space & out_space, bool clear_space = true); Status parse_space_file(const std::string & file_name, Space & out_space, bool clear_space = true); Status parse_space_file(const wchar_t * file_name, Space & out_space, bool clear_space = true); Status parse_space_file(const std::wstring & file_name, Space & out_space, bool clear_space = true); /* main methods used to parse str - input string (either 8bit ascii or UTF-8 -- see UTF8() method) */ Status parse_json(const char * str, Space & out_space, bool clear_space = true); Status parse_json(const std::string & str, Space & out_space, bool clear_space = true); /* main methods used to parse here input string is always in unicode (wide characters) */ Status parse_json(const wchar_t * str, Space & out_space, bool clear_space = true); Status parse_json(const std::wstring & str, Space & out_space, bool clear_space = true); Status parse_space(const char * str, Space & out_space, bool clear_space = true); Status parse_space(const std::string & str, Space & out_space, bool clear_space = true); Status parse_space(const wchar_t * str, Space & out_space, bool clear_space = true); Status parse_space(const std::wstring & str, Space & out_space, bool clear_space = true); /* * add two args parse method * Status parse(const char * str, Space & output_space); * */ /* * if true then the input file or string (char* or std::string) is treated as UTF-8 * default true * * the internal storage for strings is std::wstring so if you call set_utf8(false) then * the characters of input string will be simple static_cast<> from char to wchar_t * */ void use_utf8(bool utf); /* * * returns a number of a last parsed line * can be used to obtain the line in which there was a syntax error * */ int get_last_parsed_line(); private: /* current space set by SetSpace(); */ Space * root_space; /* last read token */ std::wstring token; /* separator between a variable and a value, default: '=' */ int separator; /* space starting character, default: '{' */ int space_start; /* space ending character, default: '}' */ int space_end; /* table starting character, default: '[' */ int table_start; /* table ending character, default: ']' */ int table_end; /* option delimiter, default: ',' */ int option_delimiter; /* true if the lastc was escaped (with a backslash) we have to know if the last sequence was \" or just " */ bool char_was_escaped; /* * if parsing_space is false then it means we are parsing JSON format * */ bool parsing_space; void parse_root_space(bool clear_root_space); void parse(Space * space, bool is_object_value, bool is_table_value); void parse_space(Space * space); void parse_table(Space * space); void parse_key_value_pairs(Space * space); void parse_values_list(Space * space); void read_key(); void parse_text_value(Space * space); void parse_integer_value(Space * space); void parse_floating_point_value(Space * space); bool is_alfa_numeric_char(int c); void read_token_until_delimiter(std::wstring & token, int delimiter1, int delimiter2); void read_alfa_numeric_token(std::wstring & token); void read_string_value(std::wstring & token, bool is_object_value, bool is_table_value); bool is_integer_token(); bool is_floating_point_token(); void read_space_field_token(std::wstring & token); void read_token_quoted(std::wstring & token); void read_multiline_token_quoted(std::wstring & token); int read_char(); bool is_white(int c); void skip_line(); void skip_white(); void trim_last_white(std::wstring & s); bool is_hex_digit(wchar_t c); int hex_to_int(wchar_t c); bool read_unicode_four_digit_format(bool has_first_byte, int first_byte); void read_unicode_json_format(bool has_first_byte, int first_byte); void read_unicode_floating_format(); void read_unicode_code_point(); }; } // namespace #endif