clang-tools 23.0.0git
Markdown.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// Defines the Markdown AST node hierarchy for the clang-doc Markdown parser.
11///
12/// Block nodes represent structural constructs (paragraphs, headings, lists,
13/// fenced code blocks, etc). Inline nodes represent span-level content (text,
14/// emphasis, inline code) that appears inside block nodes.
15///
16//===----------------------------------------------------------------------===//
17
18#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_MARKDOWN_MARKDOWN_H
19#define LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_MARKDOWN_MARKDOWN_H
20
21#include "llvm/ADT/StringRef.h"
22#include "llvm/ADT/simple_ilist.h"
23#include "llvm/Support/Casting.h"
24#include "llvm/Support/Compiler.h"
25#include "llvm/Support/raw_ostream.h"
26
27namespace clang::doc::markdown {
28
46
47/// Base class for all inline nodes. Inline nodes represent span-level content
48/// such as text, emphasis, and inline code.
49class InlineNode : public llvm::ilist_node<InlineNode> {
50public:
51 explicit InlineNode(NodeKind K) : Kind(K) {}
52 virtual ~InlineNode() = default;
53 NodeKind getKind() const { return Kind; }
54
55 /// Recursively prints the node and its children to OS.
56 virtual void print(llvm::raw_ostream &OS) const = 0;
57
58#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
59 /// Prints to llvm::errs(). Only available in assert builds.
60 LLVM_DUMP_METHOD void dump() const;
61#endif
62
63private:
64 NodeKind Kind;
65};
66
67using InlineList = llvm::simple_ilist<InlineNode>;
68
69/// A plain text run.
70class TextNode : public InlineNode {
71public:
72 explicit TextNode(llvm::StringRef T)
73 : InlineNode(NodeKind::NK_Text), Text(T) {}
74 llvm::StringRef getText() const { return Text; }
75 void print(llvm::raw_ostream &OS) const override;
76 static bool classof(const InlineNode *N) {
77 return N->getKind() == NodeKind::NK_Text;
78 }
79
80private:
81 llvm::StringRef Text;
82};
83
84/// A backtick-delimited inline code span.
85class InlineCodeNode : public InlineNode {
86public:
87 explicit InlineCodeNode(llvm::StringRef C)
88 : InlineNode(NodeKind::NK_InlineCode), Code(C) {}
89 llvm::StringRef getCode() const { return Code; }
90 void print(llvm::raw_ostream &OS) const override;
91 static bool classof(const InlineNode *N) {
92 return N->getKind() == NodeKind::NK_InlineCode;
93 }
94
95private:
96 llvm::StringRef Code;
97};
98
99/// An emphasis span (* or _).
100class EmphasisNode : public InlineNode {
101public:
103 void addChild(InlineNode &N) { Children.push_back(N); }
104 void removeChild(InlineNode &N) { Children.remove(N); }
105 InlineList &children() { return Children; }
106 const InlineList &children() const { return Children; }
107 void print(llvm::raw_ostream &OS) const override;
108 static bool classof(const InlineNode *N) {
109 return N->getKind() == NodeKind::NK_Emphasis;
110 }
111
112private:
113 InlineList Children;
114};
115
116/// A strong emphasis span (** or __).
117class StrongNode : public InlineNode {
118public:
120 void addChild(InlineNode &N) { Children.push_back(N); }
121 void removeChild(InlineNode &N) { Children.remove(N); }
122 InlineList &children() { return Children; }
123 const InlineList &children() const { return Children; }
124 void print(llvm::raw_ostream &OS) const override;
125 static bool classof(const InlineNode *N) {
126 return N->getKind() == NodeKind::NK_Strong;
127 }
128
129private:
130 InlineList Children;
131};
132
133/// Base class for all block nodes. Block nodes represent structural constructs
134/// such as paragraphs, headings, and lists.
135class BlockNode : public llvm::ilist_node<BlockNode> {
136public:
137 explicit BlockNode(NodeKind K) : Kind(K) {}
138 virtual ~BlockNode() = default;
139 NodeKind getKind() const { return Kind; }
140
141 /// Recursively prints the node and its children to OS.
142 virtual void print(llvm::raw_ostream &OS) const = 0;
143
144#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
145 /// Prints to llvm::errs(). Only available in assert builds.
146 LLVM_DUMP_METHOD void dump() const;
147#endif
148
149private:
150 NodeKind Kind;
151};
152
153using BlockList = llvm::simple_ilist<BlockNode>;
154
155/// A paragraph of inline content.
156class ParagraphNode : public BlockNode {
157public:
159 void addChild(InlineNode &N) { Children.push_back(N); }
160 void removeChild(InlineNode &N) { Children.remove(N); }
161 InlineList &children() { return Children; }
162 const InlineList &children() const { return Children; }
163 void print(llvm::raw_ostream &OS) const override;
164 static bool classof(const BlockNode *N) {
165 return N->getKind() == NodeKind::NK_Paragraph;
166 }
167
168private:
169 InlineList Children;
170};
171
172/// An ATX heading (# through ######).
173class HeadingNode : public BlockNode {
174public:
175 explicit HeadingNode(unsigned L)
176 : BlockNode(NodeKind::NK_Heading), Level(L) {}
177 unsigned getLevel() const { return Level; }
178 void addChild(InlineNode &N) { Children.push_back(N); }
179 void removeChild(InlineNode &N) { Children.remove(N); }
180 InlineList &children() { return Children; }
181 const InlineList &children() const { return Children; }
182 void print(llvm::raw_ostream &OS) const override;
183 static bool classof(const BlockNode *N) {
184 return N->getKind() == NodeKind::NK_Heading;
185 }
186
187private:
188 unsigned Level;
189 InlineList Children;
190};
191
192/// A fenced code block (``` or ~~~). Lang holds the info string.
193class FencedCodeNode : public BlockNode {
194public:
195 FencedCodeNode(llvm::StringRef L, llvm::StringRef C)
196 : BlockNode(NodeKind::NK_FencedCode), Lang(L), Code(C) {}
197 llvm::StringRef getLang() const { return Lang; }
198 llvm::StringRef getCode() const { return Code; }
199 void print(llvm::raw_ostream &OS) const override;
200 static bool classof(const BlockNode *N) {
201 return N->getKind() == NodeKind::NK_FencedCode;
202 }
203
204private:
205 llvm::StringRef Lang;
206 llvm::StringRef Code;
207};
208
209/// A single item in an unordered or ordered list.
210/// ListItemNode is not a BlockNode -- it only lives inside list nodes.
211class ListItemNode : public llvm::ilist_node<ListItemNode> {
212public:
213 ListItemNode() = default;
214 void addChild(InlineNode &N) { Children.push_back(N); }
215 void removeChild(InlineNode &N) { Children.remove(N); }
216 InlineList &children() { return Children; }
217 const InlineList &children() const { return Children; }
218 void print(llvm::raw_ostream &OS) const;
219
220#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
221 /// Prints to llvm::errs(). Only available in assert builds.
222 /// ListItemNode provides its own dump() since it does not inherit BlockNode.
223 LLVM_DUMP_METHOD void dump() const;
224#endif
225
226private:
227 InlineList Children;
228};
229
230using ItemList = llvm::simple_ilist<ListItemNode>;
231
232/// An unordered list (-, *, or + markers).
234public:
236 void addItem(ListItemNode &N) { Items.push_back(N); }
237 void removeItem(ListItemNode &N) { Items.remove(N); }
238 ItemList &items() { return Items; }
239 const ItemList &items() const { return Items; }
240 void print(llvm::raw_ostream &OS) const override;
241 static bool classof(const BlockNode *N) {
242 return N->getKind() == NodeKind::NK_UnorderedList;
243 }
244
245private:
246 ItemList Items;
247};
248
249/// An ordered list (1. 2. 3. markers). Start holds the first item number.
251public:
252 explicit OrderedListNode(unsigned S = 1)
253 : BlockNode(NodeKind::NK_OrderedList), Start(S) {}
254 unsigned getStart() const { return Start; }
255 void addItem(ListItemNode &N) { Items.push_back(N); }
256 void removeItem(ListItemNode &N) { Items.remove(N); }
257 ItemList &items() { return Items; }
258 const ItemList &items() const { return Items; }
259 void print(llvm::raw_ostream &OS) const override;
260 static bool classof(const BlockNode *N) {
261 return N->getKind() == NodeKind::NK_OrderedList;
262 }
263
264private:
265 unsigned Start;
266 ItemList Items;
267};
268
269/// A block quote (> marker).
270class BlockQuoteNode : public BlockNode {
271public:
273 void addChild(BlockNode &N) { Children.push_back(N); }
274 void removeChild(BlockNode &N) { Children.remove(N); }
275 BlockList &children() { return Children; }
276 const BlockList &children() const { return Children; }
277 void print(llvm::raw_ostream &OS) const override;
278 static bool classof(const BlockNode *N) {
279 return N->getKind() == NodeKind::NK_BlockQuote;
280 }
281
282private:
283 BlockList Children;
284};
285
286/// A thematic break (---, ***, or ___).
288public:
290 void print(llvm::raw_ostream &OS) const override;
291 static bool classof(const BlockNode *N) {
292 return N->getKind() == NodeKind::NK_ThematicBreak;
293 }
294};
295
296/// The root document node. Contains all top-level block nodes.
297class DocumentNode : public BlockNode {
298public:
300 void addChild(BlockNode &N) { Children.push_back(N); }
301 void removeChild(BlockNode &N) { Children.remove(N); }
302 BlockList &children() { return Children; }
303 const BlockList &children() const { return Children; }
304 void print(llvm::raw_ostream &OS) const override;
305 static bool classof(const BlockNode *N) {
306 return N->getKind() == NodeKind::NK_Document;
307 }
308
309private:
310 BlockList Children;
311};
312
313} // namespace clang::doc::markdown
314
315#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_MARKDOWN_MARKDOWN_H
LLVM_DUMP_METHOD void dump() const
Prints to llvm::errs(). Only available in assert builds.
Definition Markdown.cpp:42
virtual void print(llvm::raw_ostream &OS) const =0
Recursively prints the node and its children to OS.
void print(llvm::raw_ostream &OS) const override
Recursively prints the node and its children to OS.
Definition Markdown.cpp:83
static bool classof(const BlockNode *N)
Definition Markdown.h:278
const BlockList & children() const
Definition Markdown.h:276
void print(llvm::raw_ostream &OS) const override
Recursively prints the node and its children to OS.
Definition Markdown.cpp:93
const BlockList & children() const
Definition Markdown.h:303
void removeChild(BlockNode &N)
Definition Markdown.h:301
static bool classof(const BlockNode *N)
Definition Markdown.h:305
void removeChild(InlineNode &N)
Definition Markdown.h:104
void print(llvm::raw_ostream &OS) const override
Recursively prints the node and its children to OS.
Definition Markdown.cpp:29
static bool classof(const InlineNode *N)
Definition Markdown.h:108
void addChild(InlineNode &N)
Definition Markdown.h:103
const InlineList & children() const
Definition Markdown.h:106
llvm::StringRef getCode() const
Definition Markdown.h:198
void print(llvm::raw_ostream &OS) const override
Recursively prints the node and its children to OS.
Definition Markdown.cpp:57
static bool classof(const BlockNode *N)
Definition Markdown.h:200
FencedCodeNode(llvm::StringRef L, llvm::StringRef C)
Definition Markdown.h:195
llvm::StringRef getLang() const
Definition Markdown.h:197
static bool classof(const BlockNode *N)
Definition Markdown.h:183
void addChild(InlineNode &N)
Definition Markdown.h:178
void removeChild(InlineNode &N)
Definition Markdown.h:179
const InlineList & children() const
Definition Markdown.h:181
void print(llvm::raw_ostream &OS) const override
Recursively prints the node and its children to OS.
Definition Markdown.cpp:51
InlineCodeNode(llvm::StringRef C)
Definition Markdown.h:87
llvm::StringRef getCode() const
Definition Markdown.h:89
static bool classof(const InlineNode *N)
Definition Markdown.h:91
void print(llvm::raw_ostream &OS) const override
Recursively prints the node and its children to OS.
Definition Markdown.cpp:25
Base class for all inline nodes.
Definition Markdown.h:49
LLVM_DUMP_METHOD void dump() const
Prints to llvm::errs(). Only available in assert builds.
Definition Markdown.cpp:18
virtual void print(llvm::raw_ostream &OS) const =0
Recursively prints the node and its children to OS.
A single item in an unordered or ordered list.
Definition Markdown.h:211
const InlineList & children() const
Definition Markdown.h:217
void addChild(InlineNode &N)
Definition Markdown.h:214
void removeChild(InlineNode &N)
Definition Markdown.h:215
void print(llvm::raw_ostream &OS) const
Definition Markdown.cpp:61
LLVM_DUMP_METHOD void dump() const
Prints to llvm::errs().
Definition Markdown.cpp:68
void addItem(ListItemNode &N)
Definition Markdown.h:255
void removeItem(ListItemNode &N)
Definition Markdown.h:256
void print(llvm::raw_ostream &OS) const override
Recursively prints the node and its children to OS.
Definition Markdown.cpp:77
const ItemList & items() const
Definition Markdown.h:258
static bool classof(const BlockNode *N)
Definition Markdown.h:260
void print(llvm::raw_ostream &OS) const override
Recursively prints the node and its children to OS.
Definition Markdown.cpp:45
static bool classof(const BlockNode *N)
Definition Markdown.h:164
void removeChild(InlineNode &N)
Definition Markdown.h:160
const InlineList & children() const
Definition Markdown.h:162
void removeChild(InlineNode &N)
Definition Markdown.h:121
void addChild(InlineNode &N)
Definition Markdown.h:120
const InlineList & children() const
Definition Markdown.h:123
static bool classof(const InlineNode *N)
Definition Markdown.h:125
void print(llvm::raw_ostream &OS) const override
Recursively prints the node and its children to OS.
Definition Markdown.cpp:35
void print(llvm::raw_ostream &OS) const override
Recursively prints the node and its children to OS.
Definition Markdown.cpp:21
static bool classof(const InlineNode *N)
Definition Markdown.h:76
llvm::StringRef getText() const
Definition Markdown.h:74
TextNode(llvm::StringRef T)
Definition Markdown.h:72
void print(llvm::raw_ostream &OS) const override
Recursively prints the node and its children to OS.
Definition Markdown.cpp:89
static bool classof(const BlockNode *N)
Definition Markdown.h:291
const ItemList & items() const
Definition Markdown.h:239
static bool classof(const BlockNode *N)
Definition Markdown.h:241
void print(llvm::raw_ostream &OS) const override
Recursively prints the node and its children to OS.
Definition Markdown.cpp:71
llvm::simple_ilist< ListItemNode > ItemList
Definition Markdown.h:230
llvm::simple_ilist< InlineNode > InlineList
Definition Markdown.h:67
llvm::simple_ilist< BlockNode > BlockList
Definition Markdown.h:153