| 1 |
/* JSON parsing |
| 2 |
Copyright (C) 2017-2025 Free Software Foundation, Inc. |
| 3 |
Contributed by David Malcolm <[email protected]>. |
| 4 |
|
| 5 |
This file is part of GCC. |
| 6 |
|
| 7 |
GCC is free software; you can redistribute it and/or modify it under |
| 8 |
the terms of the GNU General Public License as published by the Free |
| 9 |
Software Foundation; either version 3, or (at your option) any later |
| 10 |
version. |
| 11 |
|
| 12 |
GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 13 |
WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 15 |
for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License |
| 18 |
along with GCC; see the file COPYING3. If not see |
| 19 |
<http://www.gnu.org/licenses/>. */ |
| 20 |
|
| 21 |
#ifndef GCC_JSON_PARSING_H |
| 22 |
#define GCC_JSON_PARSING_H |
| 23 |
|
| 24 |
#include "json.h" |
| 25 |
|
| 26 |
namespace json |
| 27 |
{ |
| 28 |
|
| 29 |
/* Declarations for parsing JSON to a json::value * tree. */ |
| 30 |
|
| 31 |
/* Abstract base class for recording what the locations of JSON values |
| 32 |
were as they parsed. */ |
| 33 |
|
| 34 |
class location_map |
| 35 |
{ |
| 36 |
public: |
| 37 |
/* A point within the JSON input file. */ |
| 38 |
struct point |
| 39 |
{ |
| 40 |
size_t m_unichar_idx; /* zero-based. */ |
| 41 |
int m_line; /* one-based. */ |
| 42 |
int m_column; /* zero-based unichar count. */ |
| 43 |
}; |
| 44 |
|
| 45 |
/* A range of points within the JSON input file. |
| 46 |
Both endpoints are part of the range. */ |
| 47 |
struct range |
| 48 |
{ |
| 49 |
point m_start; |
| 50 |
point m_end; |
| 51 |
}; |
| 52 |
|
| 53 |
virtual ~location_map () {} |
| 54 |
virtual void record_range_for_value (json::value *jv, const range &r) = 0; |
| 55 |
virtual void on_finished_parsing () {} |
| 56 |
}; |
| 57 |
|
| 58 |
/* Class for recording an error within a JSON file. */ |
| 59 |
|
| 60 |
class error |
| 61 |
{ |
| 62 |
public: |
| 63 |
error (const location_map::range &r, char *msg) |
| 64 |
: m_range (r), m_msg (msg) |
| 65 |
{ |
| 66 |
} |
| 67 |
~error () |
| 68 |
{ |
| 69 |
free (m_msg); |
| 70 |
} |
| 71 |
|
| 72 |
const location_map::range &get_range () const { return m_range; } |
| 73 |
const char *get_msg () const { return m_msg; } |
| 74 |
|
| 75 |
private: |
| 76 |
location_map::range m_range; |
| 77 |
char *m_msg; |
| 78 |
}; |
| 79 |
|
| 80 |
/* Class for the result of an operation: either a value or an error |
| 81 |
(or both null for the case of "successful nullptr"). |
| 82 |
The types must be default-constructible. */ |
| 83 |
|
| 84 |
template <typename ValueType, typename ErrorType> |
| 85 |
struct result |
| 86 |
{ |
| 87 |
result (ValueType val) : m_val (std::move (val)), m_err () {} |
| 88 |
result (ErrorType err) : m_val (), m_err (std::move (err)) {} |
| 89 |
|
| 90 |
ValueType m_val; |
| 91 |
ErrorType m_err; |
| 92 |
}; |
| 93 |
|
| 94 |
/* Typedef for the result of parsing JSON: ownership of either a |
| 95 |
json::value * or of a json::error *. */ |
| 96 |
typedef result<std::unique_ptr<value>, |
| 97 |
std::unique_ptr<error>> parser_result_t; |
| 98 |
|
| 99 |
/* Functions for parsing JSON buffers. */ |
| 100 |
|
| 101 |
extern parser_result_t |
| 102 |
parse_utf8_string (size_t length, |
| 103 |
const char *utf8_buf, |
| 104 |
bool allow_comments, |
| 105 |
location_map *out_loc_map); |
| 106 |
extern parser_result_t |
| 107 |
parse_utf8_string (const char *utf8, |
| 108 |
bool allow_comments, |
| 109 |
location_map *out_loc_map); |
| 110 |
|
| 111 |
} // namespace json |
| 112 |
|
| 113 |
#endif /* GCC_JSON_PARSING_H */ |