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