1 //===--- CommentBriefParser.cpp - Dumb comment parser ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "clang/AST/CommentBriefParser.h"
11 #include "clang/AST/CommentCommandTraits.h"
12 #include "llvm/ADT/StringSwitch.h"
18 /// Convert all whitespace into spaces, remove leading and trailing spaces,
19 /// compress multiple spaces into one.
20 void cleanupBrief(std::string &S) {
21 bool PrevWasSpace = true;
22 std::string::iterator O = S.begin();
23 for (std::string::iterator I = S.begin(), E = S.end();
26 if (C == ' ' || C == '\n' || C == '\r' ||
27 C == '\t' || C == '\v' || C == '\f') {
38 if (O != S.begin() && *(O - 1) == ' ')
41 S.resize(O - S.begin());
43 } // unnamed namespace
45 BriefParser::BriefParser(Lexer &L, const CommandTraits &Traits) :
46 L(L), Traits(Traits) {
47 // Get lookahead token.
51 std::string BriefParser::Parse() {
52 std::string FirstParagraphOrBrief;
53 std::string ReturnsParagraph;
54 bool InFirstParagraph = true;
56 bool InReturns = false;
58 while (Tok.isNot(tok::eof)) {
59 if (Tok.is(tok::text)) {
60 if (InFirstParagraph || InBrief)
61 FirstParagraphOrBrief += Tok.getText();
63 ReturnsParagraph += Tok.getText();
68 if (Tok.is(tok::command)) {
69 StringRef Name = Tok.getCommandName();
70 if (Traits.isBriefCommand(Name)) {
71 FirstParagraphOrBrief.clear();
76 if (Traits.isReturnsCommand(Name)) {
78 ReturnsParagraph += "Returns ";
80 // Block commands implicitly start a new paragraph.
81 if (Traits.isBlockCommand(Name)) {
82 // We found an implicit paragraph end.
83 InFirstParagraph = false;
89 if (Tok.is(tok::newline)) {
90 if (InFirstParagraph || InBrief)
91 FirstParagraphOrBrief += ' ';
93 ReturnsParagraph += ' ';
96 if (Tok.is(tok::newline)) {
98 // We found a paragraph end.
99 InFirstParagraph = false;
107 // We didn't handle this token, so just drop it.
111 cleanupBrief(FirstParagraphOrBrief);
112 if (!FirstParagraphOrBrief.empty())
113 return FirstParagraphOrBrief;
115 cleanupBrief(ReturnsParagraph);
116 return ReturnsParagraph;
119 } // end namespace comments
120 } // end namespace clang