1 | /**************************************************************************** |
2 | * Copyright (C) 2013-2016 Woboq GmbH |
3 | * Olivier Goffart <contact at woboq.com> |
4 | * https://woboq.com/ |
5 | * |
6 | * This program is free software: you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License as published by |
8 | * the Free Software Foundation, either version 3 of the License, or |
9 | * (at your option) any later version. |
10 | * |
11 | * This program is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU General Public License |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
18 | */ |
19 | |
20 | /* Export to the Qt Binary Json format */ |
21 | |
22 | #pragma once |
23 | |
24 | #include <map> |
25 | #include <string> |
26 | #include <vector> |
27 | |
28 | namespace llvm { |
29 | namespace yaml { |
30 | |
31 | class Node; |
32 | } |
33 | |
34 | class raw_ostream; |
35 | } |
36 | |
37 | namespace QBJS { |
38 | enum Type { |
39 | Null = 0x0, |
40 | Bool = 0x1, |
41 | Double = 0x2, |
42 | String = 0x3, |
43 | Array = 0x4, |
44 | Object = 0x5, |
45 | Undefined = 0x80 |
46 | }; |
47 | |
48 | struct Value { |
49 | Value() = default; |
50 | Value(std::string S) : T(String) , Str(std::move(S)) {} |
51 | Value(double D) : T(Double) , D(D) {} |
52 | Value(bool B) : T(Bool) , D(B ? 1. : -1.) {} |
53 | Type T = Undefined; |
54 | std::map<std::string,Value> Props; // for Object |
55 | std::vector<Value> Elems; // For Array |
56 | std::string Str; |
57 | double D = 0.; |
58 | mutable int S = -1; |
59 | |
60 | int Size() const { if(S<0)S=ComputeSize(); return S; } |
61 | int ComputeSize() const; |
62 | }; |
63 | |
64 | |
65 | struct Stream { |
66 | Stream(llvm::raw_ostream &OS) : OS(OS){} |
67 | Stream &operator << (const Value &); |
68 | |
69 | private: |
70 | int Col = 0; |
71 | llvm::raw_ostream &OS; |
72 | Stream &operator << (const std::string &Str); |
73 | Stream &operator << (uint32_t); |
74 | Stream &operator << (uint16_t); |
75 | Stream &operator << (unsigned char); |
76 | }; |
77 | |
78 | bool Parse(llvm::yaml::Node *Node, Value &Root); |
79 | } |
80 | |