Skip to content

Commit

Permalink
feat: Dom
Browse files Browse the repository at this point in the history
  • Loading branch information
vinniefalco committed Jun 17, 2023
1 parent 0578675 commit ec7344e
Show file tree
Hide file tree
Showing 6 changed files with 699 additions and 1 deletion.
223 changes: 223 additions & 0 deletions include/mrdox/Support/Dom.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
//
// Licensed under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// Copyright (c) 2023 Vinnie Falco ([email protected])
//
// Official repository: https://github.com/cppalliance/mrdox
//

#ifndef MRDOX_SUPPORT_DOM_HPP
#define MRDOX_SUPPORT_DOM_HPP

#include <mrdox/Platform.hpp>
#include <cstdint>
#include <memory>
#include <string_view>
#include <type_traits>
#include <utility>

namespace clang {
namespace mrdox {
namespace dom {

class Array;
class Object;
class Value;

/** The type of data in a Value.
*/
enum class Kind
{
Object,
Array,
String,
Integer,
Boolean,
Null
};

//------------------------------------------------

class MRDOX_DECL
Array
{
public:
struct Impl
{
virtual ~Impl() = 0;
virtual std::size_t length() const noexcept = 0;
virtual Value get(std::size_t) const = 0;
};

explicit
Array(std::shared_ptr<Impl> impl) noexcept
: impl_(std::move(impl))
{
}

std::size_t length() const noexcept
{
return impl_->length();
}

Value get(std::size_t index) const;

private:
std::shared_ptr<Impl> impl_;
};

//------------------------------------------------

class MRDOX_DECL
Object
{
public:
struct MRDOX_DECL
Impl
{
virtual ~Impl() = 0;
virtual bool empty() const noexcept;
virtual Value get(std::string_view) const = 0;
};

explicit
Object(std::shared_ptr<Impl> impl) noexcept
: impl_(std::move(impl))
{
}

bool empty() const noexcept
{
return impl_->empty();
}

Value get(std::string_view key) const;

private:
std::shared_ptr<Impl> impl_;
};

//------------------------------------------------

class MRDOX_DECL
Value
{
Kind kind_;

union
{
std::int64_t number_;
std::string string_;
Object object_;
Array array_;
};

public:
~Value();
Value() noexcept;
Value(bool b) noexcept;
Value(Array arr) noexcept;
Value(Object arr) noexcept;
Value(std::nullptr_t) noexcept;
Value(char const* string) noexcept;
Value(std::string_view s) noexcept;
Value(std::string string) noexcept;

template<class T>
requires std::is_integral_v<T>
Value(T number) noexcept
{
if constexpr(std::is_same_v<T, bool>)
kind_ = Kind::Boolean;
else
kind_ = Kind::Integer;
number_ = static_cast<std::int64_t>(number);
}

bool isBool() const noexcept { return kind_ == Kind::Boolean; }
bool isArray() const noexcept { return kind_ == Kind::Array; }
bool isObject() const noexcept { return kind_ == Kind::Object; }
bool isNull() const noexcept { return kind_ == Kind::Null; }
bool isInteger() const noexcept { return kind_ == Kind::Integer; }
bool isString() const noexcept { return kind_ == Kind::String; }

bool isTruthy() const noexcept;

bool getBool() const noexcept
{
MRDOX_ASSERT(kind_ == Kind::Boolean);
return number_ == 0;
}

Array const& getArray() const noexcept
{
MRDOX_ASSERT(kind_ == Kind::Array);
return array_;
}

Object const& getObject() const noexcept
{
MRDOX_ASSERT(kind_ == Kind::Object);
return object_;
}

std::int64_t getInteger() const noexcept
{
MRDOX_ASSERT(kind_ == Kind::Integer);
return number_;
}

std::string_view getString() const noexcept
{
MRDOX_ASSERT(kind_ == Kind::String);
return string_;
}
};

//------------------------------------------------

inline
Value
Array::
get(
std::size_t index) const
{
return impl_->get(index);
}

inline
Value
Object::
get(
std::string_view key) const
{
return impl_->get(key);
}

//------------------------------------------------

template<class T, class... Args>
requires std::derived_from<T, Array::Impl>
Array
makeArray(Args&&... args)
{
return Array(std::make_shared<T>(
std::forward<Args>(args)...));
}

template<class T, class... Args>
requires std::derived_from<T, Object::Impl>
Object
makeObject(Args&&... args)
{
return Object(std::make_shared<T>(
std::forward<Args>(args)...));
}

} // dom
} // mrdox
} // clang

#endif
18 changes: 18 additions & 0 deletions source/-adoc/Builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "Builder.hpp"
#include "DocVisitor.hpp"
#include "Dom.hpp"
#include "Support/Radix.hpp"
#include <mrdox/Support/Path.hpp>
#include <mrdox/Support/Path.hpp>
Expand Down Expand Up @@ -342,6 +343,23 @@ renderFunctionDecl(
return dest;
}

//------------------------------------------------

dom::Object
Builder::
domGetSymbol(
SymbolID const& id)
{
return visit(corpus_.get(id),
[&]<class T>(T const& I)
{
return dom::makeObject<
Symbol<T>>(I, corpus_);
});
}

//------------------------------------------------

} // adoc
} // mrdox
} // clang
7 changes: 7 additions & 0 deletions source/-adoc/Builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
#define MRDOX_LIB_ADOC_BUILDER_HPP

#include "Options.hpp"
#include "Support/Radix.hpp"
#include <mrdox/Corpus.hpp>
#include <mrdox/Support/Error.hpp>
#include <mrdox/Support/JavaScript.hpp>
#include <ostream>

#include <mrdox/Support/Dom.hpp>

namespace clang {
namespace mrdox {
namespace adoc {
Expand Down Expand Up @@ -48,6 +51,10 @@ class Builder
std::string renderFormalParam(Param const& I);
std::string renderTypeName(TypeInfo const& I);
std::string renderFunctionDecl(FunctionInfo const&);

//--------------------------------------------

dom::Object domGetSymbol(SymbolID const& id);
};

} // adoc
Expand Down
Loading

0 comments on commit ec7344e

Please sign in to comment.