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 <string>
6#include <vector>
7
8#include "lib/big_int_util.h"
9#include "lib/castable.h"
10#include "lib/cstring.h"
11#include "lib/ordered_map.h"
12
13namespace P4 {
14
15class JsonData : public ICastable {
16 public:
17 JsonData() {}
18 JsonData(const JsonData &) = default;
19 JsonData(JsonData &&) = default;
20 JsonData &operator=(const JsonData &) & = default;
21 JsonData &operator=(JsonData &&) & = default;
22 virtual ~JsonData() {}
23
24 DECLARE_TYPEINFO(JsonData);
25};
26
27class JsonNumber : public JsonData {
28 public:
29 JsonNumber(big_int v) : val(v) {} // NOLINT(runtime/explicit)
30 operator int() const { return int(val); } // Does not handle overflow
31 big_int val;
32
33 DECLARE_TYPEINFO(JsonNumber, JsonData);
34};
35
36class JsonBoolean : public JsonData {
37 public:
38 JsonBoolean(bool v) : val(v) {} // NOLINT(runtime/explicit)
39 operator bool() const { return val; }
40 bool val;
41
42 DECLARE_TYPEINFO(JsonBoolean, JsonData);
43};
44
45class JsonString : public JsonData, public std::string {
46 public:
47 JsonString() {}
48 JsonString(const std::string &s) : std::string(s) {} // NOLINT(runtime/explicit)
49 JsonString(const char *s) : std::string(s) {} // NOLINT(runtime/explicit)
50 JsonString(const JsonString &) = default;
51 JsonString(JsonString &&) = default;
52 JsonString &operator=(const JsonString &) & = default;
53 JsonString &operator=(JsonString &&) & = default;
54 operator cstring() { return cstring(this->c_str()); }
55
56 DECLARE_TYPEINFO(JsonString, JsonData);
57};
58
59class JsonVector : public JsonData, public std::vector<JsonData *> {
60 public:
61 JsonVector() {}
62 JsonVector(const std::vector<JsonData *> &v) // NOLINT(runtime/explicit)
63 : std::vector<JsonData *>(v) {}
64 JsonVector &operator=(const JsonVector &) & = default;
65 JsonVector &operator=(JsonVector &&) & = default;
66
67 DECLARE_TYPEINFO(JsonVector, JsonData);
68};
69
70class JsonObject : public JsonData, public ordered_map<std::string, JsonData *> {
71 bool _hasSrcInfo = true;
72
73 public:
74 JsonObject() {}
75 JsonObject(const JsonObject &obj) = default;
76 JsonObject &operator=(JsonObject &&) & = default;
77 JsonObject(const ordered_map<std::string, JsonData *> &v) // NOLINT(runtime/explicit)
79 int get_id() const;
80 std::string get_type() const;
81 std::string get_filename() const;
82 std::string get_sourceFragment() const;
83 int get_line() const;
84 int get_column() const;
85 JsonObject get_sourceJson() const;
86 bool hasSrcInfo() { return _hasSrcInfo; }
87 void setSrcInfo(bool value) { _hasSrcInfo = value; }
88
89 DECLARE_TYPEINFO(JsonObject, JsonData);
90};
91
92class JsonNull : public JsonData {
93 DECLARE_TYPEINFO(JsonNull, JsonData);
94};
95
96std::string getIndent(int l);
97
98std::ostream &operator<<(std::ostream &out, JsonData *json);
99std::istream &operator>>(std::istream &in, JsonData *&json);
100
101} // namespace P4
102
103#endif /* IR_JSON_PARSER_H_ */
Definition castable.h:36
Definition json_parser.h:36
Definition json_parser.h:15
Definition json_parser.h:92
Definition json_parser.h:27
Definition json_parser.h:70
Definition json_parser.h:45
Definition json_parser.h:59
Definition cstring.h:85
Definition ordered_map.h:32
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24