GCC Middle and Back End API Reference
json-parsing.h
Go to the documentation of this file.
1/* JSON parsing
2 Copyright (C) 2017-2026 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 3, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
18along 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
26namespace 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
35{
36public:
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 {
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/* Implementation of json::location_map that records ranges to a std::map. */
59
61{
62public:
63 void
65 const range &r) final override
66 {
67 m_map_jv_to_range[jv] = r;
68 }
69
72 {
73 auto iter = m_map_jv_to_range.find (&jv);
74 gcc_assert (iter != m_map_jv_to_range.end ());
75 return iter->second;
76 }
77
78private:
79 std::map<const json::value *, range> m_map_jv_to_range;
80};
81
82/* Class for recording an error within a JSON file. */
83
84class error
85{
86public:
88 : m_range (r), m_msg (msg)
89 {
90 }
92 {
93 free (m_msg);
94 }
95
96 const location_map::range &get_range () const { return m_range; }
97 const char *get_msg () const { return m_msg; }
98
99private:
101 char *m_msg;
102};
103
104/* Class for the result of an operation: either a value or an error
105 (or both null for the case of "successful nullptr").
106 The types must be default-constructible. */
107
108template <typename ValueType, typename ErrorType>
109struct result
110{
111 result (ValueType val) : m_val (std::move (val)), m_err () {}
112 result (ErrorType err) : m_val (), m_err (std::move (err)) {}
113
114 ValueType m_val;
115 ErrorType m_err;
116};
117
118/* Typedef for the result of parsing JSON: ownership of either a
119 json::value * or of a json::error *. */
121 std::unique_ptr<error>> parser_result_t;
122
123/* Functions for parsing JSON buffers. */
124
125extern parser_result_t
126parse_utf8_string (size_t length,
127 const char *utf8_buf,
128 bool allow_comments,
129 location_map *out_loc_map);
130extern parser_result_t
131parse_utf8_string (const char *utf8,
132 bool allow_comments,
133 location_map *out_loc_map);
134
135} // namespace json
136
137#endif /* GCC_JSON_PARSING_H */
location_map::range m_range
Definition json-parsing.h:100
error(const location_map::range &r, char *msg)
Definition json-parsing.h:87
const char * get_msg() const
Definition json-parsing.h:97
char * m_msg
Definition json-parsing.h:101
const location_map::range & get_range() const
Definition json-parsing.h:96
~error()
Definition json-parsing.h:91
Definition json-parsing.h:35
virtual ~location_map()
Definition json-parsing.h:53
virtual void on_finished_parsing()
Definition json-parsing.h:55
virtual void record_range_for_value(json::value *jv, const range &r)=0
Definition json-parsing.h:61
void record_range_for_value(json::value *jv, const range &r) final override
Definition json-parsing.h:64
std::map< const json::value *, range > m_map_jv_to_range
Definition json-parsing.h:79
const json::location_map::range & get_range_for_value(const json::value &jv) const
Definition json-parsing.h:71
Definition json.h:161
free(str)
Definition cfghooks.h:27
parser_result_t parse_utf8_string(size_t length, const char *utf8_buf, bool allow_comments, location_map *out_loc_map)
Definition json-parsing.cc:1381
result< std::unique_ptr< value >, std::unique_ptr< error > > parser_result_t
Definition json-parsing.h:121
poly_int< N, C > r
Definition poly-int.h:774
static void const char * msg
Definition read-md.cc:204
Definition json-parsing.h:39
size_t m_unichar_idx
Definition json-parsing.h:40
int m_line
Definition json-parsing.h:41
int m_column
Definition json-parsing.h:42
Definition json-parsing.h:48
point m_start
Definition json-parsing.h:49
point m_end
Definition json-parsing.h:50
Definition json-parsing.h:110
std::unique_ptr< value > m_val
Definition json-parsing.h:114
result(ErrorType err)
Definition json-parsing.h:112
result(ValueType val)
Definition json-parsing.h:111
std::unique_ptr< error > m_err
Definition json-parsing.h:115
Definition ira-emit.cc:158
#define gcc_assert(EXPR)
Definition system.h:817