blob: a6bf33edd4118052b443691eeafa3bf091577cec [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)
Dmitri Gribenko76e7edd2012-07-17 21:21:55 +000045 .Cases("brief", "short", true)
46 .Cases("result", "return", "returns", true)
47 .Cases("author", "authors", true)
Dmitri Gribenko55e18082012-06-29 18:19:20 +000048 .Case("pre", true)
49 .Case("post", true)
Dmitri Gribenko76e7edd2012-07-17 21:21:55 +000050 .Cases("param", "arg", true)
Dmitri Gribenko55e18082012-06-29 18:19:20 +000051 .Default(false);
52}
Dmitri Gribenkod558b522012-06-28 01:38:21 +000053} // unnamed namespace
54
Dmitri Gribenko2d44d772012-06-26 20:39:18 +000055std::string BriefParser::Parse() {
Dmitri Gribenkoc0b83242012-06-27 01:17:34 +000056 std::string Paragraph;
Dmitri Gribenko2d44d772012-06-26 20:39:18 +000057 bool InFirstParagraph = true;
58 bool InBrief = false;
Dmitri Gribenko2d44d772012-06-26 20:39:18 +000059
60 while (Tok.isNot(tok::eof)) {
61 if (Tok.is(tok::text)) {
Dmitri Gribenkoc0b83242012-06-27 01:17:34 +000062 if (InFirstParagraph || InBrief)
63 Paragraph += Tok.getText();
Dmitri Gribenko2d44d772012-06-26 20:39:18 +000064 ConsumeToken();
65 continue;
66 }
67
Dmitri Gribenkof199b9c2012-06-28 00:01:41 +000068 if (Tok.is(tok::command)) {
69 StringRef Name = Tok.getCommandName();
Dmitri Gribenko659a7122012-07-17 18:35:14 +000070 if (Name == "brief" || Name == "short") {
Dmitri Gribenkof199b9c2012-06-28 00:01:41 +000071 Paragraph.clear();
72 InBrief = true;
73 ConsumeToken();
74 continue;
75 }
Dmitri Gribenko55e18082012-06-29 18:19:20 +000076 // Block commands implicitly start a new paragraph.
77 if (isBlockCommand(Name)) {
Dmitri Gribenkof199b9c2012-06-28 00:01:41 +000078 // We found an implicit paragraph end.
79 InFirstParagraph = false;
Dmitri Gribenko57aceb22012-07-03 18:10:20 +000080 if (InBrief)
Dmitri Gribenkof199b9c2012-06-28 00:01:41 +000081 break;
Dmitri Gribenkof199b9c2012-06-28 00:01:41 +000082 }
Dmitri Gribenko2d44d772012-06-26 20:39:18 +000083 }
84
85 if (Tok.is(tok::newline)) {
Dmitri Gribenkoc0b83242012-06-27 01:17:34 +000086 if (InFirstParagraph || InBrief)
Dmitri Gribenkod558b522012-06-28 01:38:21 +000087 Paragraph += ' ';
Dmitri Gribenko2d44d772012-06-26 20:39:18 +000088 ConsumeToken();
89
90 if (Tok.is(tok::newline)) {
91 ConsumeToken();
92 // We found a paragraph end.
93 InFirstParagraph = false;
Dmitri Gribenko57aceb22012-07-03 18:10:20 +000094 if (InBrief)
Dmitri Gribenkof199b9c2012-06-28 00:01:41 +000095 break;
Dmitri Gribenko2d44d772012-06-26 20:39:18 +000096 }
97 continue;
98 }
99
100 // We didn't handle this token, so just drop it.
101 ConsumeToken();
102 }
103
Dmitri Gribenkod558b522012-06-28 01:38:21 +0000104 cleanupBrief(Paragraph);
Dmitri Gribenkoc0b83242012-06-27 01:17:34 +0000105 return Paragraph;
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000106}
107
108BriefParser::BriefParser(Lexer &L) : L(L)
109{
110 // Get lookahead token.
111 ConsumeToken();
112}
113
114} // end namespace comments
115} // end namespace clang
116
117