P4C
The P4 Compiler
Loading...
Searching...
No Matches
json_parser.h
1#ifndef IR_JSON_PARSER_H_
2#define IR_JSON_PARSER_H_
3
4#include <iosfwd>
5#include <memory>
6#include <string>
7#include <string_view>
8#include <vector>
9
10#include "lib/big_int_util.h"
11#include "lib/castable.h"
12#include "lib/cstring.h"
13#include "lib/string_map.h"
14
15namespace P4 {
16
17class JsonData : public ICastable {
18 protected:
19 JsonData() {}
20 JsonData(const JsonData &) = default;
21 JsonData(JsonData &&) = default;
22 JsonData &operator=(const JsonData &) = default;
23 JsonData &operator=(JsonData &&) = default;
24
25 public:
26 virtual ~JsonData() = default;
27
28 DECLARE_TYPEINFO(JsonData);
29};
30
31class JsonNumber : public JsonData {
32 public:
33 explicit JsonNumber(big_int v) : val(v) {}
34 operator int() const { return int(val); } // Does not handle overflow
35 big_int val;
36
37 DECLARE_TYPEINFO(JsonNumber, JsonData);
38};
39
40class JsonBoolean : public JsonData {
41 public:
42 JsonBoolean(bool v) : val(v) {} // NOLINT(runtime/explicit)
43 operator bool() const { return val; }
44 bool val;
45
46 DECLARE_TYPEINFO(JsonBoolean, JsonData);
47};
48
49class JsonString : public JsonData, public std::string {
50 public:
51 JsonString() {}
52 explicit JsonString(std::string_view s) : std::string(s) {}
53 JsonString(const JsonString &) = default;
54 JsonString(JsonString &&) = default;
55 JsonString &operator=(const JsonString &) & = default;
56 JsonString &operator=(JsonString &&) & = default;
57 operator cstring() { return cstring(this->c_str()); }
58
59 DECLARE_TYPEINFO(JsonString, JsonData);
60};
61
62class JsonVector : public JsonData, public std::vector<std::unique_ptr<JsonData>> {
63 public:
64 JsonVector() {}
65 explicit JsonVector(std::vector<std::unique_ptr<JsonData>> &&v)
66 : std::vector<std::unique_ptr<JsonData>>(std::move(v)) {}
67 JsonVector &operator=(const JsonVector &) = delete;
68 JsonVector &operator=(JsonVector &&) = default;
69
70 DECLARE_TYPEINFO(JsonVector, JsonData);
71};
72
73class JsonObject : public JsonData, public string_map<std::unique_ptr<JsonData>> {
74 public:
75 JsonObject() {}
76 JsonObject(const JsonObject &obj) = delete;
77 JsonObject &operator=(JsonObject &&) = default;
78 explicit JsonObject(string_map<std::unique_ptr<JsonData>> &&v)
79 : string_map<std::unique_ptr<JsonData>>(std::move(v)) {}
80
81 DECLARE_TYPEINFO(JsonObject, JsonData);
82};
83
84class JsonNull : public JsonData {
85 DECLARE_TYPEINFO(JsonNull, JsonData);
86};
87
88std::string getIndent(int l);
89
90std::ostream &operator<<(std::ostream &out, const JsonData *json);
91inline std::ostream &operator<<(std::ostream &out, const JsonData &json) { return out << &json; }
92std::istream &operator>>(std::istream &in, std::unique_ptr<JsonData> &json);
93
94} // namespace P4
95
96#endif /* IR_JSON_PARSER_H_ */
Definition castable.h:36
Definition json_parser.h:17
Definition json_parser.h:84
Definition cstring.h:85
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24