GCC Middle and Back End API Reference
json-parsing.h
Go to the documentation of this file.
1/* JSON parsing
2 Copyright (C) 2017-2022 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/* Class for recording an error within a JSON file. */
59
60class error
61{
62public:
64 : m_range (r), m_msg (msg)
65 {
66 }
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
75private:
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
84template <typename ValueType, typename ErrorType>
85struct 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 *. */
97 std::unique_ptr<error>> parser_result_t;
98
99/* Functions for parsing JSON buffers. */
100
101extern parser_result_t
102parse_utf8_string (size_t length,
103 const char *utf8_buf,
104 bool allow_comments,
105 location_map *out_loc_map);
106extern parser_result_t
107parse_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 */
Definition json-parsing.h:61
location_map::range m_range
Definition json-parsing.h:76
error(const location_map::range &r, char *msg)
Definition json-parsing.h:63
const char * get_msg() const
Definition json-parsing.h:73
char * m_msg
Definition json-parsing.h:77
const location_map::range & get_range() const
Definition json-parsing.h:72
~error()
Definition json-parsing.h:67
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.h:79
free(str)
Definition json-parsing.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:97
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:86
ValueType m_val
Definition json-parsing.h:90
result(ErrorType err)
Definition json-parsing.h:88
result(ValueType val)
Definition json-parsing.h:87
ErrorType m_err
Definition json-parsing.h:91
Definition ira-emit.cc:158