Skip to content

Commit

Permalink
feat: doc::Heading
Browse files Browse the repository at this point in the history
  • Loading branch information
vinniefalco authored and sdkrystian committed Jun 15, 2023
1 parent ae14717 commit 9094337
Show file tree
Hide file tree
Showing 19 changed files with 301 additions and 91 deletions.
95 changes: 91 additions & 4 deletions include/mrdox/Metadata/Javadoc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <mrdox/Support/Error.hpp>
#include <memory>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>

Expand All @@ -38,14 +39,15 @@ using List = std::vector<std::unique_ptr<T>>;
enum class Kind
{
text = 1, // needed by bitstream
styled,
paragraph,
brief,
admonition,
brief,
code,
heading,
paragraph,
param,
returns,
styled,
tparam,
returns
};

/** A text style.
Expand Down Expand Up @@ -209,6 +211,31 @@ struct Block : Node
}
};

/** A manually specified section heading.
*/
struct Heading : Block
{
static constexpr Kind static_kind = Kind::heading;

String string;

Heading(
String string_ = String())
: Block(Kind::heading)
, string(std::move(string_))
{
}

bool operator==(const Heading&)
const noexcept = default;

bool equals(const Node& other) const noexcept override
{
return kind == other.kind &&
*this == static_cast<const Heading&>(other);
}
};

/** A sequence of text nodes.
*/
struct Paragraph : Block
Expand Down Expand Up @@ -383,8 +410,68 @@ struct Returns : Paragraph
}
};

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

template<class F, class... Args>
constexpr
auto
visit(
Node const& node,
F&& f, Args&&... args)
{
switch(node.kind)
{
case Kind::admonition:
return f(static_cast<Admonition const&>(node),
std::forward<Args>(args)...);
case Kind::brief:
return f(static_cast<Brief const&>(node),
std::forward<Args>(args)...);
case Kind::code:
return f(static_cast<Code const&>(node),
std::forward<Args>(args)...);
case Kind::heading:
return f(static_cast<Heading const&>(node),
std::forward<Args>(args)...);
case Kind::paragraph:
return f(static_cast<Paragraph const&>(node),
std::forward<Args>(args)...);
case Kind::param:
return f(static_cast<Param const&>(node),
std::forward<Args>(args)...);
case Kind::returns:
return f(static_cast<Returns const&>(node),
std::forward<Args>(args)...);
case Kind::styled:
return f(static_cast<StyledText const&>(node),
std::forward<Args>(args)...);
case Kind::text:
return f(static_cast<Text const&>(node),
std::forward<Args>(args)...);
case Kind::tparam:
return f(static_cast<TParam const&>(node),
std::forward<Args>(args)...);
default:
MRDOX_UNREACHABLE();
}
}

template<class F, class T, class... Args>
requires std::derived_from<T, Node>
void traverse(
std::vector<std::unique_ptr<T>> const& list,
F&& f, Args&&... args)
{
for(auto const& node : list)
visit(*node,
std::forward<F>(f),
std::forward<Args>(args)...);
}

} // doc

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

/** A processed Doxygen-style comment attached to a declaration.
*/
struct MRDOX_VISIBLE
Expand Down
11 changes: 11 additions & 0 deletions source/-XML/XMLWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,9 @@ writeNode(
case doc::Kind::styled:
writeStyledText(static_cast<doc::StyledText const&>(node));
break;
case doc::Kind::heading:
writeHeading(static_cast<doc::Heading const&>(node));
break;
case doc::Kind::paragraph:
writeParagraph(static_cast<doc::Paragraph const&>(node));
break;
Expand Down Expand Up @@ -663,6 +666,14 @@ writeStyledText(
tags_.write(toString(node.style), node.string);
}

void
XMLWriter::
writeHeading(
doc::Heading const& heading)
{
tags_.write("head", heading.string);
}

void
XMLWriter::
writeParagraph(
Expand Down
1 change: 1 addition & 0 deletions source/-XML/XMLWriter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class XMLWriter
void writeBrief(doc::Paragraph const& node);
void writeText(doc::Text const& node);
void writeStyledText(doc::StyledText const& node);
void writeHeading(doc::Heading const& node);
void writeParagraph(doc::Paragraph const& node, llvm::StringRef tag = "");
void writeAdmonition(doc::Admonition const& node);
void writeCode(doc::Code const& node);
Expand Down
6 changes: 6 additions & 0 deletions source/AST/AnyBlock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ class JavadocNodesBlock
case doc::Kind::styled:
nodes.emplace_back(std::make_unique<doc::StyledText>());
return Error::success();
case doc::Kind::heading:
nodes.emplace_back(std::make_unique<doc::Heading>());
return Error::success();
case doc::Kind::paragraph:
nodes.emplace_back(std::make_unique<doc::Paragraph>());
return Error::success();
Expand Down Expand Up @@ -154,6 +157,9 @@ class JavadocNodesBlock
switch(auto node = nodes.back().get();
node->kind)
{
case doc::Kind::heading:
static_cast<doc::Heading*>(node)->string = Blob.str();
return Error::success();
case doc::Kind::text:
case doc::Kind::styled:
static_cast<doc::Text*>(
Expand Down
6 changes: 6 additions & 0 deletions source/AST/BitcodeWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,12 @@ emitBlock(
emitRecord(J.string, JAVADOC_NODE_STRING);
break;
}
case doc::Kind::heading:
{
auto const& J = static_cast<doc::Heading const&>(I);
emitRecord(J.string, JAVADOC_NODE_STRING);
break;
}
case doc::Kind::paragraph:
{
auto const& J = static_cast<doc::Paragraph const&>(I);
Expand Down
Loading

0 comments on commit 9094337

Please sign in to comment.