-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dto-compiler): Lay the foundations of a
parser
implementation
- Loading branch information
1 parent
efce74c
commit 5350ba9
Showing
19 changed files
with
921 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* SPDX-License-Identifier: LGPL-3.0-only */ | ||
#pragma once | ||
#include <memory> | ||
#include <type_traits> | ||
|
||
namespace monomux | ||
{ | ||
/// A simple scope guard that fires an optional callback function when it is | ||
/// constructed, and another callback function (usually, a lambda passed to the | ||
/// constructor) when destructed. | ||
/// | ||
/// Examples: | ||
/// | ||
/// \code{.cpp} | ||
/// scope_guard Cleanup{[] { exit(); }}; | ||
/// \endcode | ||
/// | ||
/// \code{.cpp} | ||
/// scope_guard RAII{[] { enter(); }, [] { exit(); }}; | ||
/// \endcode | ||
template <typename EnterFunction, typename ExitFunction> struct scope_guard | ||
{ | ||
// NOLINTNEXTLINE(google-explicit-constructor) | ||
scope_guard(ExitFunction&& Exit) noexcept : Alive(true), Exit(Exit) {} | ||
scope_guard(EnterFunction&& Enter, | ||
ExitFunction&& Exit) noexcept(noexcept(Enter())) | ||
: Alive(false), Exit(Exit) | ||
{ | ||
Enter(); | ||
Alive = true; // NOLINT(cppcoreguidelines-prefer-member-initializer) | ||
} | ||
|
||
~scope_guard() noexcept(noexcept(Exit())) | ||
{ | ||
if (Alive) | ||
Exit(); | ||
Alive = false; | ||
} | ||
|
||
scope_guard() = delete; | ||
scope_guard(const scope_guard&) = delete; | ||
scope_guard(scope_guard&&) = delete; | ||
scope_guard& operator=(const scope_guard&) = delete; | ||
scope_guard& operator=(scope_guard&&) = delete; | ||
|
||
private: | ||
bool Alive; | ||
ExitFunction Exit; | ||
}; | ||
|
||
/// A simple scope guard that restores the value of a "captured" variable when | ||
/// the scope is exited. | ||
/// | ||
/// Example: | ||
/// | ||
/// \code{.cpp} | ||
/// int X = 4; | ||
/// { | ||
/// restore_guard Reset{X}; | ||
/// X = 6; | ||
/// } | ||
/// assert(X == 4); | ||
/// \endcode | ||
template <typename Ty> struct restore_guard | ||
{ | ||
// NOLINTNEXTLINE(google-explicit-constructor) | ||
restore_guard(Ty& Var) noexcept(std::is_copy_constructible_v<Ty>) | ||
: Address(std::addressof(Var)), Value(Var) | ||
{} | ||
|
||
~restore_guard() noexcept(std::is_move_assignable_v<Ty>) | ||
{ | ||
*Address = std::move(Value); | ||
Address = nullptr; | ||
} | ||
|
||
restore_guard() = delete; | ||
restore_guard(const restore_guard&) = delete; | ||
restore_guard(restore_guard&&) = delete; | ||
restore_guard& operator=(const restore_guard&) = delete; | ||
restore_guard& operator=(restore_guard&&) = delete; | ||
|
||
private: | ||
Ty* Address; | ||
Ty Value; | ||
}; | ||
|
||
} // namespace monomux |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* SPDX-License-Identifier: GPL-3.0-only */ | ||
#include <gtest/gtest.h> | ||
|
||
#include "monomux/adt/scope_guard.hpp" | ||
|
||
/* NOLINTBEGIN(cert-err58-cpp,cppcoreguidelines-avoid-goto,cppcoreguidelines-owning-memory) | ||
*/ | ||
|
||
using namespace monomux; | ||
|
||
TEST(ScopeGuard, EntryAndExitCalled) | ||
{ | ||
int Variable = 2; | ||
{ | ||
ASSERT_EQ(Variable, 2); | ||
|
||
scope_guard SG{[&Variable] { Variable = 4; }, | ||
[&Variable] { Variable = 0; }}; | ||
ASSERT_EQ(Variable, 4); | ||
} | ||
ASSERT_EQ(Variable, 0); | ||
} | ||
|
||
TEST(ScopeGuard, RestoreGuard) | ||
{ | ||
int Variable = 2; | ||
{ | ||
ASSERT_EQ(Variable, 2); | ||
restore_guard RG{Variable}; | ||
Variable = 4; | ||
ASSERT_EQ(Variable, 4); | ||
} | ||
ASSERT_EQ(Variable, 2); | ||
} | ||
|
||
/* NOLINTEND(cert-err58-cpp,cppcoreguidelines-avoid-goto,cppcoreguidelines-owning-memory) | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* SPDX-License-Identifier: LGPL-3.0-only */ | ||
#include <sstream> | ||
|
||
#include "decl.hpp" | ||
|
||
namespace monomux::tools::dto_compiler::ast | ||
{ | ||
|
||
namespace | ||
{ | ||
|
||
void print_ident(std::ostringstream& OS, std::size_t Indent) | ||
{ | ||
if (Indent == 0) | ||
OS << '.'; | ||
|
||
while (Indent > 1) | ||
{ | ||
OS << "| "; | ||
--Indent; | ||
} | ||
if (Indent == 1) | ||
{ | ||
OS << "|- "; | ||
} | ||
} | ||
|
||
} // namespace | ||
|
||
void decl_context::dump_children(std::ostringstream& OS, | ||
std::size_t Depth) const | ||
{ | ||
for (const auto& Child : Children) | ||
{ | ||
print_ident(OS, Depth); | ||
Child->dump(OS, Depth + 1); | ||
} | ||
} | ||
|
||
#define MONOMUX_DECL_DUMP(TYPE) \ | ||
void TYPE::dump(std::ostringstream& OS, std::size_t Depth) const | ||
|
||
MONOMUX_DECL_DUMP(decl) {} | ||
|
||
MONOMUX_DECL_DUMP(comment_decl) | ||
{ | ||
static constexpr std::size_t CommentPrintLength = 64; | ||
OS << "CommentDecl " << this << ' '; | ||
OS << (Comment.is_block_comment() ? "block " : "line "); | ||
OS << Comment.get_comment().substr(0, CommentPrintLength); | ||
OS << '\n'; | ||
} | ||
|
||
MONOMUX_DECL_DUMP(named_decl) {} | ||
|
||
MONOMUX_DECL_DUMP(namespace_decl) | ||
{ | ||
OS << "NamespaceDecl " << this << ' ' << get_identifier() << '\n'; | ||
dump_children(OS, Depth); | ||
} | ||
|
||
MONOMUX_DECL_DUMP(value_decl) {} | ||
|
||
MONOMUX_DECL_DUMP(literal_decl) {} | ||
|
||
#undef MONOMUX_DECL_DUMP | ||
|
||
} // namespace monomux::tools::dto_compiler::ast |
Oops, something went wrong.