blob: 4040a999249bd0de85f6dbea7028997ed3b1bf00 [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"
11
12namespace clang {
13namespace comments {
14
Dmitri Gribenkod558b522012-06-28 01:38:21 +000015namespace {
16/// Convert all whitespace into spaces, remove leading and trailing spaces,
17/// compress multiple spaces into one.
18void cleanupBrief(std::string &S) {
19 bool PrevWasSpace = true;
20 std::string::iterator O = S.begin();
21 for (std::string::iterator I = S.begin(), E = S.end();
22 I != E; ++I) {
23 const char C = *I;
24 if (C == ' ' || C == '\n' || C == '\r' ||
25 C == '\t' || C == '\v' || C == '\f') {
26 if (!PrevWasSpace) {
27 *O++ = ' ';
28 PrevWasSpace = true;
29 }
30 continue;
31 } else {
32 *O++ = C;
33 PrevWasSpace = false;
34 }
35 }
36 if (O != S.begin() && *(O - 1) == ' ')
37 --O;
38
39 S.resize(O - S.begin());
40}
41} // unnamed namespace
42
Dmitri Gribenko2d44d772012-06-26 20:39:18 +000043std::string BriefParser::Parse() {
Dmitri Gribenkoc0b83242012-06-27 01:17:34 +000044 std::string Paragraph;
Dmitri Gribenko2d44d772012-06-26 20:39:18 +000045 bool InFirstParagraph = true;
46 bool InBrief = false;
Dmitri Gribenko2d44d772012-06-26 20:39:18 +000047
48 while (Tok.isNot(tok::eof)) {
49 if (Tok.is(tok::text)) {
Dmitri Gribenkoc0b83242012-06-27 01:17:34 +000050 if (InFirstParagraph || InBrief)
51 Paragraph += Tok.getText();
Dmitri Gribenko2d44d772012-06-26 20:39:18 +000052 ConsumeToken();
53 continue;
54 }
55
Dmitri Gribenkof199b9c2012-06-28 00:01:41 +000056 if (Tok.is(tok::command)) {
57 StringRef Name = Tok.getCommandName();
58 if (Name == "brief") {
59 Paragraph.clear();
60 InBrief = true;
61 ConsumeToken();
62 continue;
63 }
64 // Check if this command implicitly starts a new paragraph.
65 if (Name == "param" || Name == "result" || Name == "return" ||
66 Name == "returns") {
67 // We found an implicit paragraph end.
68 InFirstParagraph = false;
69 if (InBrief) {
70 InBrief = false;
71 break;
72 }
73 }
Dmitri Gribenko2d44d772012-06-26 20:39:18 +000074 }
75
76 if (Tok.is(tok::newline)) {
Dmitri Gribenkoc0b83242012-06-27 01:17:34 +000077 if (InFirstParagraph || InBrief)
Dmitri Gribenkod558b522012-06-28 01:38:21 +000078 Paragraph += ' ';
Dmitri Gribenko2d44d772012-06-26 20:39:18 +000079 ConsumeToken();
80
81 if (Tok.is(tok::newline)) {
82 ConsumeToken();
83 // We found a paragraph end.
84 InFirstParagraph = false;
85 if (InBrief) {
86 InBrief = false;
Dmitri Gribenkof199b9c2012-06-28 00:01:41 +000087 break;
Dmitri Gribenko2d44d772012-06-26 20:39:18 +000088 }
89 }
90 continue;
91 }
92
93 // We didn't handle this token, so just drop it.
94 ConsumeToken();
95 }
96
Dmitri Gribenkod558b522012-06-28 01:38:21 +000097 cleanupBrief(Paragraph);
Dmitri Gribenkoc0b83242012-06-27 01:17:34 +000098 return Paragraph;
Dmitri Gribenko2d44d772012-06-26 20:39:18 +000099}
100
101BriefParser::BriefParser(Lexer &L) : L(L)
102{
103 // Get lookahead token.
104 ConsumeToken();
105}
106
107} // end namespace comments
108} // end namespace clang
109
110