================
@@ -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; }
+  void print(llvm::raw_ostream &OS) const override;
+  static bool classof(const InlineNode *N) {
+    return N->getKind() == NodeKind::NK_Emphasis;
+  }
+
+private:
+  InlineList Children;
----------------
ilovepi wrote:

Do these actually need to grow? if not, I'd expect them to stay `ArrayRef`.  
Its fine if there's a reason to do it this way, like it simplifies the later 
parsing implementation, but its not clear that this functionality is required.

https://github.com/llvm/llvm-project/pull/205609
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to