================ @@ -0,0 +1,304 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, 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 +// +//===----------------------------------------------------------------------===// +/// +/// \file +/// Defines the Markdown AST node hierarchy for the clang-doc Markdown parser. +/// +/// Block nodes represent structural constructs (paragraphs, headings, lists, +/// fenced code blocks, etc). Inline nodes represent span-level content (text, +/// emphasis, inline code) that appears inside block nodes. +/// +/// All nodes are arena-allocated via ASTContext, which manages their lifetime. +/// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_SUPPORT_MARKDOWN_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_SUPPORT_MARKDOWN_H + +#include "llvm/ADT/StringRef.h" +#include "llvm/ADT/simple_ilist.h" +#include "llvm/Support/Casting.h" +#include "llvm/Support/Compiler.h" +#include "llvm/Support/raw_ostream.h" + +namespace clang::doc::markdown { + +enum class NodeKind { + // Inline nodes + NK_Text, + NK_InlineCode, + NK_Emphasis, + NK_Strong, + // Block nodes + NK_Paragraph, + NK_Heading, + NK_FencedCode, + NK_Table, // TODO: add TableNode + NK_UnorderedList, + NK_OrderedList, + NK_BlockQuote, + NK_ThematicBreak, + NK_Document, +}; + +/// Base class for all inline nodes. Inline nodes represent span-level content +/// such as text, emphasis, and inline code. +class InlineNode : public llvm::ilist_node<InlineNode> { +public: + explicit InlineNode(NodeKind K) : Kind(K) {} + virtual ~InlineNode() = default; + NodeKind getKind() const { return Kind; } + /// Recursively prints the node and its children to OS. + virtual void print(llvm::raw_ostream &OS) const = 0; + /// Prints to llvm::errs(). Only available in assert builds. + LLVM_DUMP_METHOD void dump() const; + +private: + NodeKind Kind; +}; + +using InlineList = llvm::simple_ilist<InlineNode>; + +/// A plain text run. +class TextNode : public InlineNode { +public: + explicit TextNode(llvm::StringRef T) + : InlineNode(NodeKind::NK_Text), Text(T) {} + llvm::StringRef getText() const { return Text; } + void print(llvm::raw_ostream &OS) const override; + static bool classof(const InlineNode *N) { + return N->getKind() == NodeKind::NK_Text; + } + +private: + llvm::StringRef Text; +}; + +/// A backtick-delimited inline code span. +class InlineCodeNode : public InlineNode { +public: + explicit InlineCodeNode(llvm::StringRef C) + : InlineNode(NodeKind::NK_InlineCode), Code(C) {} + llvm::StringRef getCode() const { return Code; } + void print(llvm::raw_ostream &OS) const override; + static bool classof(const InlineNode *N) { + return N->getKind() == NodeKind::NK_InlineCode; + } + +private: + llvm::StringRef Code; +}; + +/// An emphasis span (* or _). +class EmphasisNode : public InlineNode { +public: + EmphasisNode() : InlineNode(NodeKind::NK_Emphasis) {} + void addChild(InlineNode &N) { Children.push_back(N); } + void removeChild(InlineNode &N) { Children.remove(N); } + InlineList &children() { return Children; } + const InlineList &children() const { return Children; } ---------------- ilovepi wrote:
Typically this would return the iterator range, and there would be a `getChildrenList()` API to facilitate using the actual list that would be protected. LLVM IR's User class is a good example: https://github.com/llvm/llvm-project/blob/f298ab915f28ec8979de184b429b8a8c04491dff/llvm/include/llvm/IR/User.h#L267 I'm not 100% sure we want to expose the underlying list like this. Do you think making the class iteratable over the list explicitly would be a better option? this would allow you to operate a lot like LLVM's BasicBlock class, which also makes me wonder if these should be simple ilists or the list with parent type, like BasicBlock, Function, Instruction, etc. https://github.com/llvm/llvm-project/blob/f298ab915f28ec8979de184b429b8a8c04491dff/llvm/include/llvm/IR/BasicBlock.h#L61 This doesn't need to be solved right now, but its something that we should settle sooner rather than later, since it would cause a lot of churn in the follow code. https://github.com/llvm/llvm-project/pull/205609 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
