================ @@ -0,0 +1,182 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include "MarkdownParser.h" +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/SmallString.h" +#include "llvm/ADT/SmallVector.h" + +namespace clang::doc::markdown { + +namespace { + +/// A forward cursor over the lines of the input. Owns no storage; it points +/// into a caller-provided array of lines and tracks the current position. +/// Block parsers consume lines through this cursor instead of juggling a raw +/// index, which keeps the parsing helpers free of manual bookkeeping. +class LineCursor { +public: + explicit LineCursor(llvm::ArrayRef<llvm::StringRef> Lines) : Lines(Lines) {} + + bool atEnd() const { return Pos >= Lines.size(); } + + /// The current line, trimmed of surrounding whitespace. + llvm::StringRef peek() const { return Lines[Pos].trim(); } + + /// The current line as it appears in the source, without trimming. + llvm::StringRef peekRaw() const { return Lines[Pos]; } + + void advance() { ++Pos; } + +private: + llvm::ArrayRef<llvm::StringRef> Lines; + size_t Pos = 0; +}; + +} // namespace + +static bool isListMarker(llvm::StringRef Line) { + return Line.starts_with("- ") || Line.starts_with("* ") || + Line.starts_with("+ "); +} + +static bool isThematicBreak(llvm::StringRef Line) { + if (Line.empty()) + return false; + char Marker = Line.front(); + if (Marker != '-' && Marker != '*' && Marker != '_') + return false; + llvm::SmallString<8> Allowed; + Allowed += Marker; + Allowed += ' '; + if (Line.find_first_not_of(Allowed) != llvm::StringRef::npos) + return false; + return Line.count(Marker) >= 3; +} + +static bool isFence(llvm::StringRef Line) { + return Line.starts_with("```") || Line.starts_with("~~~"); +} + +/// Returns the heading level (1-6) if Line is an ATX heading, or 0 otherwise. +static unsigned getHeadingLevel(llvm::StringRef Line) { + unsigned Level = Line.take_while([](char C) { return C == '#'; }).size(); + if (Level == 0 || Level > 6 || Level >= Line.size() || Line[Level] != ' ') + return 0; + return Level; +} + +/// True if Line begins a new block, meaning paragraph accumulation must stop. +static bool startsNewBlock(llvm::StringRef Line) { + return Line.empty() || isThematicBreak(Line) || isFence(Line) || + isListMarker(Line) || getHeadingLevel(Line) != 0; +} + +static TextNode *makeText(llvm::StringRef Text, ASTContext &Ctx) { + return Ctx.allocate<TextNode>(Ctx.intern(Text)); +} + +static BlockNode *parseThematicBreak(LineCursor &Cursor, ASTContext &Ctx) { + Cursor.advance(); + return Ctx.allocate<ThematicBreakNode>(); +} ---------------- ilovepi wrote:
What if this isn't a thematic break? how is this parsing? https://github.com/llvm/llvm-project/pull/208003 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
