Skip to content

Commit 6806796

Browse files
committed
feat: more doc node types
1 parent 68ccbfe commit 6806796

File tree

7 files changed

+458
-276
lines changed

7 files changed

+458
-276
lines changed

include/mrdox/Metadata/Javadoc.hpp

+68-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ enum class Kind
4343
brief,
4444
code,
4545
heading,
46+
link,
47+
list_item,
4648
paragraph,
4749
param,
4850
returns,
@@ -147,7 +149,7 @@ struct Text : Node
147149
}
148150
};
149151

150-
/** A piece of style text.
152+
/** A piece of styled text.
151153
*/
152154
struct Styled : Text
153155
{
@@ -172,6 +174,42 @@ struct Styled : Text
172174

173175
};
174176

177+
/** A hyperlink.
178+
179+
Note that the text of the hyperlink can be
180+
block content.
181+
*/
182+
struct Link : Node
183+
{
184+
String href;
185+
186+
static constexpr Kind static_kind = Kind::link;
187+
188+
explicit
189+
Link(
190+
String href_ = String()) noexcept
191+
: Node(Kind::link)
192+
, href(std::move(href_))
193+
{
194+
}
195+
196+
bool operator==(const Link&) const noexcept = default;
197+
bool equals(const Node& other) const noexcept override
198+
{
199+
return kind == other.kind &&
200+
*this == static_cast<const Link&>(other);
201+
}
202+
203+
protected:
204+
Link(
205+
String href_,
206+
Kind kind_)
207+
: Node(kind_)
208+
, href(std::move(href_))
209+
{
210+
}
211+
};
212+
175213
//------------------------------------------------
176214
//
177215
// Block nodes
@@ -334,6 +372,27 @@ struct Code : Paragraph
334372
}
335373
};
336374

375+
/** An item in a list
376+
*/
377+
struct ListItem : Paragraph
378+
{
379+
static constexpr Kind static_kind = Kind::list_item;
380+
381+
ListItem()
382+
: Paragraph(Kind::list_item)
383+
{
384+
}
385+
386+
bool operator==(const ListItem&)
387+
const noexcept = default;
388+
389+
bool equals(const Node& other) const noexcept override
390+
{
391+
return kind == other.kind &&
392+
*this == static_cast<const ListItem&>(other);
393+
}
394+
};
395+
337396
/** Documentation for a function parameter
338397
*/
339398
struct Param : Paragraph
@@ -426,6 +485,8 @@ visit(
426485
return f.template operator()<Code>(std::forward<Args>(args)...);
427486
case Kind::heading:
428487
return f.template operator()<Heading>(std::forward<Args>(args)...);
488+
case Kind::list_item:
489+
return f.template operator()<ListItem>(std::forward<Args>(args)...);
429490
case Kind::paragraph:
430491
return f.template operator()<Paragraph>(std::forward<Args>(args)...);
431492
case Kind::param:
@@ -467,6 +528,12 @@ visit(
467528
case Kind::paragraph:
468529
return f(static_cast<Paragraph const&>(node),
469530
std::forward<Args>(args)...);
531+
case Kind::link:
532+
return f(static_cast<Link const&>(node),
533+
std::forward<Args>(args)...);
534+
case Kind::list_item:
535+
return f(static_cast<ListItem const&>(node),
536+
std::forward<Args>(args)...);
470537
case Kind::param:
471538
return f(static_cast<Param const&>(node),
472539
std::forward<Args>(args)...);

source/-XML/XMLWriter.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,12 @@ writeNode(
613613
case doc::Kind::paragraph:
614614
writeParagraph(static_cast<doc::Paragraph const&>(node));
615615
break;
616+
case doc::Kind::link:
617+
writeLink(static_cast<doc::Link const&>(node));
618+
break;
619+
case doc::Kind::list_item:
620+
writeListItem(static_cast<doc::ListItem const&>(node));
621+
break;
616622
case doc::Kind::brief:
617623
writeBrief(static_cast<doc::Brief const&>(node));
618624
break;
@@ -637,6 +643,26 @@ writeNode(
637643
}
638644
}
639645

646+
void
647+
XMLWriter::
648+
writeLink(
649+
doc::Link const& node)
650+
{
651+
tags_.write("link", {}, {
652+
{ "href", node.href }
653+
});
654+
}
655+
656+
void
657+
XMLWriter::
658+
writeListItem(
659+
doc::ListItem const& node)
660+
{
661+
tags_.open("item");
662+
writeNodes(node.children);
663+
tags_.close("item");
664+
}
665+
640666
void
641667
XMLWriter::
642668
writeBrief(

source/-XML/XMLWriter.hpp

+8-5
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,18 @@ class XMLWriter
8484
template<class T>
8585
void writeNodes(doc::List<T> const& list);
8686
void writeNode(doc::Node const& node);
87+
88+
void writeAdmonition(doc::Admonition const& node);
8789
void writeBrief(doc::Paragraph const& node);
88-
void writeText(doc::Text const& node);
89-
void writeStyledText(doc::Styled const& node);
90+
void writeCode(doc::Code const& node);
9091
void writeHeading(doc::Heading const& node);
92+
void writeLink(doc::Link const& node);
93+
void writeListItem(doc::ListItem const& node);
9194
void writeParagraph(doc::Paragraph const& node, llvm::StringRef tag = "");
92-
void writeAdmonition(doc::Admonition const& node);
93-
void writeCode(doc::Code const& node);
94-
void writeReturns(doc::Returns const& node);
9595
void writeJParam(doc::Param const& node);
96+
void writeReturns(doc::Returns const& node);
97+
void writeStyledText(doc::Styled const& node);
98+
void writeText(doc::Text const& node);
9699
void writeTParam(doc::TParam const& node);
97100
};
98101

source/AST/AnyBlock.hpp

+50-30
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,47 @@ class JavadocNodesBlock
9898
{
9999
switch (ID)
100100
{
101+
case JAVADOC_NODE_ADMONISH:
102+
{
103+
doc::Admonish admonish =
104+
doc::Admonish::none;
105+
if(auto err = decodeRecord(R, admonish, Blob))
106+
return err;
107+
auto node = nodes.back().get();
108+
if(node->kind != doc::Kind::admonition)
109+
return Error("admonish on wrong kind");
110+
static_cast<doc::Admonition*>(
111+
node)->style = admonish;
112+
return Error::success();
113+
}
114+
115+
case JAVADOC_PARAM_DIRECTION:
116+
{
117+
doc::ParamDirection direction =
118+
doc::ParamDirection::none;
119+
if(auto err = decodeRecord(R, direction, Blob))
120+
return err;
121+
auto node = nodes.back().get();
122+
if(node->kind != doc::Kind::param)
123+
return Error("direction on wrong kind");
124+
auto param = static_cast<doc::Param*>(node);
125+
param->direction = direction;
126+
return Error::success();
127+
}
128+
129+
case JAVADOC_NODE_HREF:
130+
{
131+
switch(auto node = nodes.back().get();
132+
node->kind)
133+
{
134+
case doc::Kind::link:
135+
static_cast<doc::Link*>(node)->href = Blob.str();
136+
return Error::success();
137+
default:
138+
return Error("href on wrong kind");
139+
}
140+
}
141+
101142
case JAVADOC_NODE_KIND:
102143
{
103144
doc::Kind kind{};
@@ -117,19 +158,7 @@ class JavadocNodesBlock
117158
}
118159
});
119160
}
120-
case JAVADOC_PARAM_DIRECTION:
121-
{
122-
doc::ParamDirection direction =
123-
doc::ParamDirection::none;
124-
if(auto err = decodeRecord(R, direction, Blob))
125-
return err;
126-
auto node = nodes.back().get();
127-
if(node->kind != doc::Kind::param)
128-
return Error("direction on wrong kind");
129-
auto param = static_cast<doc::Param*>(node);
130-
param->direction = direction;
131-
return Error::success();
132-
}
161+
133162
case JAVADOC_NODE_STRING:
134163
{
135164
switch(auto node = nodes.back().get();
@@ -155,6 +184,7 @@ class JavadocNodesBlock
155184
return Error("string on wrong kind");
156185
}
157186
}
187+
158188
case JAVADOC_NODE_STYLE:
159189
{
160190
doc::Style style =
@@ -169,19 +199,7 @@ class JavadocNodesBlock
169199
return Error::success();
170200

171201
}
172-
case JAVADOC_NODE_ADMONISH:
173-
{
174-
doc::Admonish admonish =
175-
doc::Admonish::none;
176-
if(auto err = decodeRecord(R, admonish, Blob))
177-
return err;
178-
auto node = nodes.back().get();
179-
if(node->kind != doc::Kind::admonition)
180-
return Error("admonish on wrong kind");
181-
static_cast<doc::Admonition*>(
182-
node)->style = admonish;
183-
return Error::success();
184-
}
202+
185203
default:
186204
return AnyBlock::parseRecord(R, ID, Blob);
187205
}
@@ -193,10 +211,6 @@ class JavadocNodesBlock
193211
{
194212
switch(ID)
195213
{
196-
case BI_JAVADOC_NODE_BLOCK_ID:
197-
{
198-
return br_.readBlock(*this, ID);
199-
}
200214
case BI_JAVADOC_LIST_BLOCK_ID:
201215
{
202216
auto node = nodes.back().get();
@@ -212,6 +226,12 @@ class JavadocNodesBlock
212226
std::move(B.nodes));
213227
return Error::success();
214228
}
229+
230+
case BI_JAVADOC_NODE_BLOCK_ID:
231+
{
232+
return br_.readBlock(*this, ID);
233+
}
234+
215235
default:
216236
return AnyBlock::readSubBlock(ID);
217237
}

source/AST/BitcodeIDs.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ enum RecordID
102102
FUNCTION_PARAM_NAME,
103103
FUNCTION_PARAM_DEFAULT,
104104
JAVADOC_NODE_ADMONISH,
105+
JAVADOC_NODE_HREF,
105106
JAVADOC_NODE_KIND,
106107
JAVADOC_NODE_STRING,
107108
JAVADOC_NODE_STYLE,

source/AST/BitcodeWriter.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,11 @@ RecordIDNameMap = []()
251251
{INFO_PART_ID, {"InfoID", &SymbolIDAbbrev}},
252252
{INFO_PART_NAME, {"InfoName", &StringAbbrev}},
253253
{INFO_PART_PARENTS, {"InfoParents", &SymbolIDsAbbrev}},
254+
{JAVADOC_NODE_ADMONISH, {"JavadocNodeAdmonish", &Integer32Abbrev}},
255+
{JAVADOC_NODE_HREF, {"JavadocNodeHref", &StringAbbrev}},
254256
{JAVADOC_NODE_KIND, {"JavadocNodeKind", &Integer32Abbrev}},
255257
{JAVADOC_NODE_STRING, {"JavadocNodeString", &StringAbbrev}},
256258
{JAVADOC_NODE_STYLE, {"JavadocNodeStyle", &Integer32Abbrev}},
257-
{JAVADOC_NODE_ADMONISH, {"JavadocNodeAdmonish", &Integer32Abbrev}},
258259
{JAVADOC_PARAM_DIRECTION, {"JavadocParamDirection", &Integer32Abbrev}},
259260
{NAMESPACE_MEMBERS, {"NamespaceMembers", &SymbolIDsAbbrev}},
260261
{NAMESPACE_SPECIALIZATIONS, {"NamespaceSpecializations", &SymbolIDsAbbrev}},
@@ -329,7 +330,7 @@ RecordsByBlock{
329330
{}},
330331
// doc::Node
331332
{BI_JAVADOC_NODE_BLOCK_ID,
332-
{JAVADOC_NODE_KIND, JAVADOC_NODE_STRING, JAVADOC_NODE_STYLE,
333+
{JAVADOC_NODE_KIND, JAVADOC_NODE_HREF, JAVADOC_NODE_STRING, JAVADOC_NODE_STYLE,
333334
JAVADOC_NODE_ADMONISH, JAVADOC_PARAM_DIRECTION}},
334335
// NamespaceInfo
335336
{BI_NAMESPACE_BLOCK_ID,
@@ -860,6 +861,9 @@ emitBlock(
860861
{
861862
auto const& J = static_cast<T const&>(I);
862863

864+
if constexpr(std::derived_from<T, doc::Link>)
865+
emitRecord(J.href, JAVADOC_NODE_HREF);
866+
863867
if constexpr(
864868
std::derived_from<T, doc::Heading> ||
865869
std::derived_from<T, doc::Text>)

0 commit comments

Comments
 (0)