1 //===--- CommentBriefParser.cpp - Dumb comment parser ---------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "clang/AST/CommentBriefParser.h"
11 #include "llvm/ADT/StringSwitch.h"
12 
13 namespace clang {
14 namespace comments {
15 
16 namespace {
17 /// Convert all whitespace into spaces, remove leading and trailing spaces,
18 /// compress multiple spaces into one.
19 void cleanupBrief(std::string &S) {
20   bool PrevWasSpace = true;
21   std::string::iterator O = S.begin();
22   for (std::string::iterator I = S.begin(), E = S.end();
23        I != E; ++I) {
24     const char C = *I;
25     if (C == ' ' || C == '\n' || C == '\r' ||
26         C == '\t' || C == '\v' || C == '\f') {
27       if (!PrevWasSpace) {
28         *O++ = ' ';
29         PrevWasSpace = true;
30       }
31       continue;
32     } else {
33       *O++ = C;
34       PrevWasSpace = false;
35     }
36   }
37   if (O != S.begin() && *(O - 1) == ' ')
38     --O;
39 
40   S.resize(O - S.begin());
41 }
42 
43 bool isBlockCommand(StringRef Name) {
44   return llvm::StringSwitch<bool>(Name)
45       .Cases("brief", "short", true)
46       .Cases("result", "return", "returns", true)
47       .Cases("author", "authors", true)
48       .Case("pre", true)
49       .Case("post", true)
50       .Cases("param", "arg", true)
51       .Default(false);
52 }
53 } // unnamed namespace
54 
55 std::string BriefParser::Parse() {
56   std::string FirstParagraphOrBrief;
57   std::string ReturnsParagraph;
58   bool InFirstParagraph = true;
59   bool InBrief = false;
60   bool InReturns = false;
61 
62   while (Tok.isNot(tok::eof)) {
63     if (Tok.is(tok::text)) {
64       if (InFirstParagraph || InBrief)
65         FirstParagraphOrBrief += Tok.getText();
66       else if (InReturns)
67         ReturnsParagraph += Tok.getText();
68       ConsumeToken();
69       continue;
70     }
71 
72     if (Tok.is(tok::command)) {
73       StringRef Name = Tok.getCommandName();
74       if (Name == "brief" || Name == "short") {
75         FirstParagraphOrBrief.clear();
76         InBrief = true;
77         ConsumeToken();
78         continue;
79       }
80       if (Name == "result" || Name == "return" || Name == "returns") {
81         InReturns = true;
82         ReturnsParagraph += "Returns ";
83       }
84       // Block commands implicitly start a new paragraph.
85       if (isBlockCommand(Name)) {
86         // We found an implicit paragraph end.
87         InFirstParagraph = false;
88         if (InBrief)
89           break;
90       }
91     }
92 
93     if (Tok.is(tok::newline)) {
94       if (InFirstParagraph || InBrief)
95         FirstParagraphOrBrief += ' ';
96       else if (InReturns)
97         ReturnsParagraph += ' ';
98       ConsumeToken();
99 
100       if (Tok.is(tok::newline)) {
101         ConsumeToken();
102         // We found a paragraph end.
103         InFirstParagraph = false;
104         InReturns = false;
105         if (InBrief)
106           break;
107       }
108       continue;
109     }
110 
111     // We didn't handle this token, so just drop it.
112     ConsumeToken();
113   }
114 
115   cleanupBrief(FirstParagraphOrBrief);
116   if (!FirstParagraphOrBrief.empty())
117     return FirstParagraphOrBrief;
118 
119   cleanupBrief(ReturnsParagraph);
120   return ReturnsParagraph;
121 }
122 
123 BriefParser::BriefParser(Lexer &L) : L(L) {
124   // Get lookahead token.
125   ConsumeToken();
126 }
127 
128 } // end namespace comments
129 } // end namespace clang
130 
131 
132