Skip to content

Commit ec7344e

Browse files
committed
feat: Dom
1 parent 0578675 commit ec7344e

File tree

6 files changed

+699
-1
lines changed

6 files changed

+699
-1
lines changed

include/mrdox/Support/Dom.hpp

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
//
2+
// Licensed under the Apache License v2.0 with LLVM Exceptions.
3+
// See https://llvm.org/LICENSE.txt for license information.
4+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
//
6+
// Copyright (c) 2023 Vinnie Falco ([email protected])
7+
//
8+
// Official repository: https://github.com/cppalliance/mrdox
9+
//
10+
11+
#ifndef MRDOX_SUPPORT_DOM_HPP
12+
#define MRDOX_SUPPORT_DOM_HPP
13+
14+
#include <mrdox/Platform.hpp>
15+
#include <cstdint>
16+
#include <memory>
17+
#include <string_view>
18+
#include <type_traits>
19+
#include <utility>
20+
21+
namespace clang {
22+
namespace mrdox {
23+
namespace dom {
24+
25+
class Array;
26+
class Object;
27+
class Value;
28+
29+
/** The type of data in a Value.
30+
*/
31+
enum class Kind
32+
{
33+
Object,
34+
Array,
35+
String,
36+
Integer,
37+
Boolean,
38+
Null
39+
};
40+
41+
//------------------------------------------------
42+
43+
class MRDOX_DECL
44+
Array
45+
{
46+
public:
47+
struct Impl
48+
{
49+
virtual ~Impl() = 0;
50+
virtual std::size_t length() const noexcept = 0;
51+
virtual Value get(std::size_t) const = 0;
52+
};
53+
54+
explicit
55+
Array(std::shared_ptr<Impl> impl) noexcept
56+
: impl_(std::move(impl))
57+
{
58+
}
59+
60+
std::size_t length() const noexcept
61+
{
62+
return impl_->length();
63+
}
64+
65+
Value get(std::size_t index) const;
66+
67+
private:
68+
std::shared_ptr<Impl> impl_;
69+
};
70+
71+
//------------------------------------------------
72+
73+
class MRDOX_DECL
74+
Object
75+
{
76+
public:
77+
struct MRDOX_DECL
78+
Impl
79+
{
80+
virtual ~Impl() = 0;
81+
virtual bool empty() const noexcept;
82+
virtual Value get(std::string_view) const = 0;
83+
};
84+
85+
explicit
86+
Object(std::shared_ptr<Impl> impl) noexcept
87+
: impl_(std::move(impl))
88+
{
89+
}
90+
91+
bool empty() const noexcept
92+
{
93+
return impl_->empty();
94+
}
95+
96+
Value get(std::string_view key) const;
97+
98+
private:
99+
std::shared_ptr<Impl> impl_;
100+
};
101+
102+
//------------------------------------------------
103+
104+
class MRDOX_DECL
105+
Value
106+
{
107+
Kind kind_;
108+
109+
union
110+
{
111+
std::int64_t number_;
112+
std::string string_;
113+
Object object_;
114+
Array array_;
115+
};
116+
117+
public:
118+
~Value();
119+
Value() noexcept;
120+
Value(bool b) noexcept;
121+
Value(Array arr) noexcept;
122+
Value(Object arr) noexcept;
123+
Value(std::nullptr_t) noexcept;
124+
Value(char const* string) noexcept;
125+
Value(std::string_view s) noexcept;
126+
Value(std::string string) noexcept;
127+
128+
template<class T>
129+
requires std::is_integral_v<T>
130+
Value(T number) noexcept
131+
{
132+
if constexpr(std::is_same_v<T, bool>)
133+
kind_ = Kind::Boolean;
134+
else
135+
kind_ = Kind::Integer;
136+
number_ = static_cast<std::int64_t>(number);
137+
}
138+
139+
bool isBool() const noexcept { return kind_ == Kind::Boolean; }
140+
bool isArray() const noexcept { return kind_ == Kind::Array; }
141+
bool isObject() const noexcept { return kind_ == Kind::Object; }
142+
bool isNull() const noexcept { return kind_ == Kind::Null; }
143+
bool isInteger() const noexcept { return kind_ == Kind::Integer; }
144+
bool isString() const noexcept { return kind_ == Kind::String; }
145+
146+
bool isTruthy() const noexcept;
147+
148+
bool getBool() const noexcept
149+
{
150+
MRDOX_ASSERT(kind_ == Kind::Boolean);
151+
return number_ == 0;
152+
}
153+
154+
Array const& getArray() const noexcept
155+
{
156+
MRDOX_ASSERT(kind_ == Kind::Array);
157+
return array_;
158+
}
159+
160+
Object const& getObject() const noexcept
161+
{
162+
MRDOX_ASSERT(kind_ == Kind::Object);
163+
return object_;
164+
}
165+
166+
std::int64_t getInteger() const noexcept
167+
{
168+
MRDOX_ASSERT(kind_ == Kind::Integer);
169+
return number_;
170+
}
171+
172+
std::string_view getString() const noexcept
173+
{
174+
MRDOX_ASSERT(kind_ == Kind::String);
175+
return string_;
176+
}
177+
};
178+
179+
//------------------------------------------------
180+
181+
inline
182+
Value
183+
Array::
184+
get(
185+
std::size_t index) const
186+
{
187+
return impl_->get(index);
188+
}
189+
190+
inline
191+
Value
192+
Object::
193+
get(
194+
std::string_view key) const
195+
{
196+
return impl_->get(key);
197+
}
198+
199+
//------------------------------------------------
200+
201+
template<class T, class... Args>
202+
requires std::derived_from<T, Array::Impl>
203+
Array
204+
makeArray(Args&&... args)
205+
{
206+
return Array(std::make_shared<T>(
207+
std::forward<Args>(args)...));
208+
}
209+
210+
template<class T, class... Args>
211+
requires std::derived_from<T, Object::Impl>
212+
Object
213+
makeObject(Args&&... args)
214+
{
215+
return Object(std::make_shared<T>(
216+
std::forward<Args>(args)...));
217+
}
218+
219+
} // dom
220+
} // mrdox
221+
} // clang
222+
223+
#endif

source/-adoc/Builder.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "Builder.hpp"
1212
#include "DocVisitor.hpp"
13+
#include "Dom.hpp"
1314
#include "Support/Radix.hpp"
1415
#include <mrdox/Support/Path.hpp>
1516
#include <mrdox/Support/Path.hpp>
@@ -342,6 +343,23 @@ renderFunctionDecl(
342343
return dest;
343344
}
344345

346+
//------------------------------------------------
347+
348+
dom::Object
349+
Builder::
350+
domGetSymbol(
351+
SymbolID const& id)
352+
{
353+
return visit(corpus_.get(id),
354+
[&]<class T>(T const& I)
355+
{
356+
return dom::makeObject<
357+
Symbol<T>>(I, corpus_);
358+
});
359+
}
360+
361+
//------------------------------------------------
362+
345363
} // adoc
346364
} // mrdox
347365
} // clang

source/-adoc/Builder.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@
1212
#define MRDOX_LIB_ADOC_BUILDER_HPP
1313

1414
#include "Options.hpp"
15+
#include "Support/Radix.hpp"
1516
#include <mrdox/Corpus.hpp>
1617
#include <mrdox/Support/Error.hpp>
1718
#include <mrdox/Support/JavaScript.hpp>
1819
#include <ostream>
1920

21+
#include <mrdox/Support/Dom.hpp>
22+
2023
namespace clang {
2124
namespace mrdox {
2225
namespace adoc {
@@ -48,6 +51,10 @@ class Builder
4851
std::string renderFormalParam(Param const& I);
4952
std::string renderTypeName(TypeInfo const& I);
5053
std::string renderFunctionDecl(FunctionInfo const&);
54+
55+
//--------------------------------------------
56+
57+
dom::Object domGetSymbol(SymbolID const& id);
5158
};
5259

5360
} // adoc

0 commit comments

Comments
 (0)