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 std::streamoff start = -1, finish = -1; // location in istream read from
19
20 protected:
21 JsonData() {}
22 JsonData(const JsonData &) = default;
23 JsonData(JsonData &&) = default;
24 JsonData &operator=(const JsonData &) = default;
25 JsonData &operator=(JsonData &&) = default;
26
27 public:
28 virtual ~JsonData() = default;
29 static bool strict; // enforce strict syntax checking of json on input, default false.
30
31 friend std::istream &operator>>(std::istream &in, std::unique_ptr<JsonData> &json);
32 struct error : public std::runtime_error {
33 const JsonData *data = nullptr; // object/item with error if parsed
34 std::streamoff loc = -1; // location of the error if no object
35 error(const std::string_view what, std::streamoff l)
36 : runtime_error(std::string(what)), loc(l) {}
37 error(const std::string_view what, const JsonData *d)
38 : runtime_error(std::string(what)), data(d) {}
39 };
40
41 class LocationInfo {
42 std::string name;
43 std::istream &in;
44 std::streamoff scanned = 0;
45 std::map<std::streamoff, int> line; // map offset (to start of line) to linenumber
46 public:
47 LocationInfo(std::string n, std::istream &i) : name(n), in(i) {
48 line[0] = 1; // line 1 at the start of the file
49 }
50 std::pair<int, int> loc(std::streamoff l);
51 std::string desc(std::streamoff l);
52 std::string desc(const JsonData &d);
53 std::string desc(const error &e);
54 };
55
56 DECLARE_TYPEINFO(JsonData);
57};
58
59class JsonNumber : public JsonData {
60 public:
61 explicit JsonNumber(big_int v) : val(v) {}
62 operator int() const { return int(val); } // Does not handle overflow
63 big_int val;
64
65 DECLARE_TYPEINFO(JsonNumber, JsonData);
66};
67
68class JsonBoolean : public JsonData {
69 public:
70 JsonBoolean(bool v) : val(v) {} // NOLINT(runtime/explicit)
71 operator bool() const { return val; }
72 bool val;
73
74 DECLARE_TYPEINFO(JsonBoolean, JsonData);
75};
76
77class JsonString : public JsonData, public std::string {
78 public:
79 JsonString() {}
80 explicit JsonString(std::string_view s) : std::string(s) {}
81 JsonString(const JsonString &) = default;
82 JsonString(JsonString &&) = default;
83 JsonString &operator=(const JsonString &) & = default;
84 JsonString &operator=(JsonString &&) & = default;
85 operator cstring() { return cstring(this->c_str()); }
86
87 DECLARE_TYPEINFO(JsonString, JsonData);
88};
89
90class JsonVector : public JsonData, public std::vector<std::unique_ptr<JsonData>> {
91 public:
92 JsonVector() {}
93 explicit JsonVector(std::vector<std::unique_ptr<JsonData>> &&v)
94 : std::vector<std::unique_ptr<JsonData>>(std::move(v)) {}
95 JsonVector &operator=(const JsonVector &) = delete;
96 JsonVector &operator=(JsonVector &&) = default;
97
98 DECLARE_TYPEINFO(JsonVector, JsonData);
99};
100
101class JsonObject : public JsonData, public string_map<std::unique_ptr<JsonData>> {
102 public:
103 JsonObject() {}
104 JsonObject(const JsonObject &obj) = delete;
105 JsonObject &operator=(JsonObject &&) = default;
106 explicit JsonObject(string_map<std::unique_ptr<JsonData>> &&v)
107 : string_map<std::unique_ptr<JsonData>>(std::move(v)) {}
108
109 DECLARE_TYPEINFO(JsonObject, JsonData);
110};
111
112class JsonNull : public JsonData {
113 DECLARE_TYPEINFO(JsonNull, JsonData);
114};
115
116std::string getIndent(int l);
117
118std::ostream &operator<<(std::ostream &out, const JsonData *json);
119inline std::ostream &operator<<(std::ostream &out, const JsonData &json) { return out << &json; }
120std::istream &operator>>(std::istream &in, std::unique_ptr<JsonData> &json);
121
122} // namespace P4
123
124#endif /* IR_JSON_PARSER_H_ */
Definition castable.h:36
Definition json_parser.h:17
Definition json_parser.h:112
Definition cstring.h:85
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
Definition bson.cpp:69
Definition json_parser.h:32