2012-07-12 17:01:15 +02:00
|
|
|
/*
|
|
|
|
* This file is a part of PikoTools
|
|
|
|
* and is distributed under the (new) BSD licence.
|
|
|
|
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2021-03-16 18:40:14 +01:00
|
|
|
* Copyright (c) 2012-2021, Tomasz Sowa
|
2012-07-12 17:01:15 +02:00
|
|
|
* 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 <fstream>
|
|
|
|
#include "space.h"
|
2021-07-17 14:38:22 +02:00
|
|
|
#include "convert/baseparser.h"
|
2012-07-12 17:01:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2021-05-20 16:11:12 +02:00
|
|
|
namespace pt
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
|
|
|
|
2021-03-16 18:40:14 +01:00
|
|
|
|
2012-07-12 17:01:15 +02:00
|
|
|
|
|
|
|
|
2021-07-17 14:38:22 +02:00
|
|
|
class SpaceParser : public BaseParser
|
2012-07-12 17:01:15 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
ctor -- setting default values (SetDefault() method)
|
|
|
|
*/
|
2021-03-17 18:33:41 +01:00
|
|
|
SpaceParser();
|
2012-07-12 17:01:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
status of parsing
|
|
|
|
*/
|
2021-05-21 01:33:01 +02:00
|
|
|
enum Status { ok, cant_open_file, syntax_error };
|
2012-07-12 17:01:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
2021-05-21 04:42:55 +02:00
|
|
|
the last status of parsing, set by parse() methods
|
2012-07-12 17:01:15 +02:00
|
|
|
*/
|
|
|
|
Status status;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2021-03-17 18:24:50 +01:00
|
|
|
main methods used to parse a JSON file
|
2012-07-12 17:01:15 +02:00
|
|
|
file_name is the path to a file
|
|
|
|
*/
|
2021-05-21 04:42:55 +02:00
|
|
|
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);
|
2021-03-17 18:24:50 +01:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
main methods used to parse a Space file
|
|
|
|
file_name is the path to a file
|
|
|
|
*/
|
2021-05-21 04:42:55 +02:00
|
|
|
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);
|
2012-07-12 17:01:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
main methods used to parse
|
|
|
|
str - input string (either 8bit ascii or UTF-8 -- see UTF8() method)
|
|
|
|
*/
|
2021-05-21 04:42:55 +02:00
|
|
|
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);
|
2012-07-12 17:01:15 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
main methods used to parse
|
|
|
|
here input string is always in unicode (wide characters)
|
|
|
|
*/
|
2021-05-21 04:42:55 +02:00
|
|
|
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);
|
2021-03-17 18:24:50 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
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);
|
2021-03-17 18:24:50 +01:00
|
|
|
|
|
|
|
|
2021-03-16 18:40:14 +01:00
|
|
|
|
|
|
|
/*
|
2021-05-21 04:42:55 +02:00
|
|
|
* add two args parse method
|
|
|
|
* Status parse(const char * str, Space & output_space);
|
2021-03-16 18:40:14 +01:00
|
|
|
*
|
|
|
|
*/
|
2012-07-12 17:01:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2021-05-21 04:28:31 +02:00
|
|
|
* 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);
|
2012-07-12 17:01:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
2021-03-16 18:40:14 +01:00
|
|
|
*
|
|
|
|
* 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();
|
2012-07-12 17:01:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2021-03-16 18:40:14 +01:00
|
|
|
* if parsing_space is false then it means we are parsing JSON format
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
bool parsing_space;
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
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);
|
2021-03-16 18:40:14 +01:00
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
void parse_key_value_pairs(Space * space);
|
|
|
|
void parse_values_list(Space * space);
|
2012-07-12 17:01:15 +02:00
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
void read_key();
|
2021-03-16 18:40:14 +01:00
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
void parse_text_value(Space * space);
|
|
|
|
void parse_integer_value(Space * space);
|
|
|
|
void parse_floating_point_value(Space * space);
|
2021-03-16 18:40:14 +01:00
|
|
|
|
|
|
|
bool is_alfa_numeric_char(int c);
|
2021-03-17 18:24:50 +01:00
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
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);
|
2021-03-16 18:40:14 +01:00
|
|
|
|
|
|
|
bool is_integer_token();
|
|
|
|
bool is_floating_point_token();
|
2012-07-12 17:01:15 +02:00
|
|
|
|
2021-05-21 04:42:55 +02:00
|
|
|
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);
|
2021-06-14 13:48:32 +02:00
|
|
|
|
|
|
|
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();
|
2021-05-21 04:42:55 +02:00
|
|
|
void read_unicode_code_point();
|
2012-07-12 17:01:15 +02:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2021-03-16 18:40:14 +01:00
|
|
|
|
2021-03-15 19:25:04 +01:00
|
|
|
|
2012-07-12 17:01:15 +02:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|