blob: 687ece219425b9fcaed6f3991c0acc3cf5b180e6 [file] [log] [blame]
Dmitri Gribenko2d44d772012-06-26 20:39:18 +00001//===--- 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"
Dmitri Gribenko55e18082012-06-29 18:19:20 +000011#include "llvm/ADT/StringSwitch.h"
Dmitri Gribenko2d44d772012-06-26 20:39:18 +000012
13namespace clang {
14namespace comments {
15
Dmitri Gribenkod558b522012-06-28 01:38:21 +000016namespace {
17/// Convert all whitespace into spaces, remove leading and trailing spaces,
18/// compress multiple spaces into one.
19void 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}
Dmitri Gribenko55e18082012-06-29 18:19:20 +000042
43bool isBlockCommand(StringRef Name) {
44 return llvm::StringSwitch<bool>(Name)
45 .Case("brief", true)
Dmitri Gribenko659a7122012-07-17 18:35:14 +000046 .Case("short", true)
Dmitri Gribenko55e18082012-06-29 18:19:20 +000047 .Case("result", true)
48 .Case("return", true)
49 .Case("returns", true)
50 .Case("author", true)
51 .Case("authors", true)
52 .Case("pre", true)
53 .Case("post", true)
54 .Case("param", true)
55 .Case("arg", true)
56 .Default(false);
57}
Dmitri Gribenkod558b522012-06-28 01:38:21 +000058} // unnamed namespace
59
Dmitri Gribenko2d44d772012-06-26 20:39:18 +000060std::string BriefParser::Parse() {
Dmitri Gribenkoc0b83242012-06-27 01:17:34 +000061 std::string Paragraph;
Dmitri Gribenko2d44d772012-06-26 20:39:18 +000062 bool InFirstParagraph = true;
63 bool InBrief = false;
Dmitri Gribenko2d44d772012-06-26 20:39:18 +000064
65 while (Tok.isNot(tok::eof)) {
66 if (Tok.is(tok::text)) {
Dmitri Gribenkoc0b83242012-06-27 01:17:34 +000067 if (InFirstParagraph || InBrief)
68 Paragraph += Tok.getText();
Dmitri Gribenko2d44d772012-06-26 20:39:18 +000069 ConsumeToken();
70 continue;
71 }
72
Dmitri Gribenkof199b9c2012-06-28 00:01:41 +000073 if (Tok.is(tok::command)) {
74 StringRef Name = Tok.getCommandName();
Dmitri Gribenko659a7122012-07-17 18:35:14 +000075 if (Name == "brief" || Name == "short") {
Dmitri Gribenkof199b9c2012-06-28 00:01:41 +000076 Paragraph.clear();
77 InBrief = true;
78 ConsumeToken();
79 continue;
80 }
Dmitri Gribenko55e18082012-06-29 18:19:20 +000081 // Block commands implicitly start a new paragraph.
82 if (isBlockCommand(Name)) {
Dmitri Gribenkof199b9c2012-06-28 00:01:41 +000083 // We found an implicit paragraph end.
84 InFirstParagraph = false;
Dmitri Gribenko57aceb22012-07-03 18:10:20 +000085 if (InBrief)
Dmitri Gribenkof199b9c2012-06-28 00:01:41 +000086 break;
Dmitri Gribenkof199b9c2012-06-28 00:01:41 +000087 }
Dmitri Gribenko2d44d772012-06-26 20:39:18 +000088 }
89
90 if (Tok.is(tok::newline)) {
Dmitri Gribenkoc0b83242012-06-27 01:17:34 +000091 if (InFirstParagraph || InBrief)
Dmitri Gribenkod558b522012-06-28 01:38:21 +000092 Paragraph += ' ';
Dmitri Gribenko2d44d772012-06-26 20:39:18 +000093 ConsumeToken();
94
95 if (Tok.is(tok::newline)) {
96 ConsumeToken();
97 // We found a paragraph end.
98 InFirstParagraph = false;
Dmitri Gribenko57aceb22012-07-03 18:10:20 +000099 if (InBrief)
Dmitri Gribenkof199b9c2012-06-28 00:01:41 +0000100 break;
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000101 }
102 continue;
103 }
104
105 // We didn't handle this token, so just drop it.
106 ConsumeToken();
107 }
108
Dmitri Gribenkod558b522012-06-28 01:38:21 +0000109 cleanupBrief(Paragraph);
Dmitri Gribenkoc0b83242012-06-27 01:17:34 +0000110 return Paragraph;
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000111}
112
113BriefParser::BriefParser(Lexer &L) : L(L)
114{
115 // Get lookahead token.
116 ConsumeToken();
117}
118
119} // end namespace comments
120} // end namespace clang
121
122