Dmitri Gribenko | 2d44d77 | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 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" |
Dmitri Gribenko | aa58081 | 2012-08-09 00:03:17 +0000 | [diff] [blame] | 11 | #include "clang/AST/CommentCommandTraits.h" |
Dmitri Gribenko | 55e1808 | 2012-06-29 18:19:20 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/StringSwitch.h" |
Dmitri Gribenko | 2d44d77 | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 13 | |
| 14 | namespace clang { |
| 15 | namespace comments { |
| 16 | |
Dmitri Gribenko | d558b52 | 2012-06-28 01:38:21 +0000 | [diff] [blame] | 17 | namespace { |
Dmitri Gribenko | 0ac4ec7 | 2012-08-21 21:15:34 +0000 | [diff] [blame] | 18 | inline bool isWhitespace(char C) { |
| 19 | return C == ' ' || C == '\n' || C == '\r' || |
| 20 | C == '\t' || C == '\f' || C == '\v'; |
| 21 | } |
| 22 | |
Dmitri Gribenko | d558b52 | 2012-06-28 01:38:21 +0000 | [diff] [blame] | 23 | /// Convert all whitespace into spaces, remove leading and trailing spaces, |
| 24 | /// compress multiple spaces into one. |
| 25 | void cleanupBrief(std::string &S) { |
| 26 | bool PrevWasSpace = true; |
| 27 | std::string::iterator O = S.begin(); |
| 28 | for (std::string::iterator I = S.begin(), E = S.end(); |
| 29 | I != E; ++I) { |
| 30 | const char C = *I; |
Dmitri Gribenko | 0ac4ec7 | 2012-08-21 21:15:34 +0000 | [diff] [blame] | 31 | if (isWhitespace(C)) { |
Dmitri Gribenko | d558b52 | 2012-06-28 01:38:21 +0000 | [diff] [blame] | 32 | if (!PrevWasSpace) { |
| 33 | *O++ = ' '; |
| 34 | PrevWasSpace = true; |
| 35 | } |
| 36 | continue; |
| 37 | } else { |
| 38 | *O++ = C; |
| 39 | PrevWasSpace = false; |
| 40 | } |
| 41 | } |
| 42 | if (O != S.begin() && *(O - 1) == ' ') |
| 43 | --O; |
| 44 | |
| 45 | S.resize(O - S.begin()); |
| 46 | } |
Dmitri Gribenko | 0ac4ec7 | 2012-08-21 21:15:34 +0000 | [diff] [blame] | 47 | |
| 48 | bool isWhitespace(StringRef Text) { |
| 49 | for (StringRef::const_iterator I = Text.begin(), E = Text.end(); |
| 50 | I != E; ++I) { |
| 51 | if (!isWhitespace(*I)) |
| 52 | return false; |
| 53 | } |
| 54 | return true; |
| 55 | } |
Dmitri Gribenko | d558b52 | 2012-06-28 01:38:21 +0000 | [diff] [blame] | 56 | } // unnamed namespace |
| 57 | |
Dmitri Gribenko | aa58081 | 2012-08-09 00:03:17 +0000 | [diff] [blame] | 58 | BriefParser::BriefParser(Lexer &L, const CommandTraits &Traits) : |
| 59 | L(L), Traits(Traits) { |
| 60 | // Get lookahead token. |
| 61 | ConsumeToken(); |
| 62 | } |
| 63 | |
Dmitri Gribenko | 2d44d77 | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 64 | std::string BriefParser::Parse() { |
Dmitri Gribenko | 72021ff | 2012-07-20 17:01:34 +0000 | [diff] [blame] | 65 | std::string FirstParagraphOrBrief; |
| 66 | std::string ReturnsParagraph; |
Dmitri Gribenko | 2d44d77 | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 67 | bool InFirstParagraph = true; |
| 68 | bool InBrief = false; |
Dmitri Gribenko | 72021ff | 2012-07-20 17:01:34 +0000 | [diff] [blame] | 69 | bool InReturns = false; |
Dmitri Gribenko | 2d44d77 | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 70 | |
| 71 | while (Tok.isNot(tok::eof)) { |
| 72 | if (Tok.is(tok::text)) { |
Dmitri Gribenko | c0b8324 | 2012-06-27 01:17:34 +0000 | [diff] [blame] | 73 | if (InFirstParagraph || InBrief) |
Dmitri Gribenko | 72021ff | 2012-07-20 17:01:34 +0000 | [diff] [blame] | 74 | FirstParagraphOrBrief += Tok.getText(); |
| 75 | else if (InReturns) |
| 76 | ReturnsParagraph += Tok.getText(); |
Dmitri Gribenko | 2d44d77 | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 77 | ConsumeToken(); |
| 78 | continue; |
| 79 | } |
| 80 | |
Dmitri Gribenko | f199b9c | 2012-06-28 00:01:41 +0000 | [diff] [blame] | 81 | if (Tok.is(tok::command)) { |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 82 | const CommandInfo *Info = Traits.getCommandInfo(Tok.getCommandID()); |
| 83 | if (Info->IsBriefCommand) { |
Dmitri Gribenko | 72021ff | 2012-07-20 17:01:34 +0000 | [diff] [blame] | 84 | FirstParagraphOrBrief.clear(); |
Dmitri Gribenko | f199b9c | 2012-06-28 00:01:41 +0000 | [diff] [blame] | 85 | InBrief = true; |
| 86 | ConsumeToken(); |
| 87 | continue; |
| 88 | } |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 89 | if (Info->IsReturnsCommand) { |
Dmitri Gribenko | 72021ff | 2012-07-20 17:01:34 +0000 | [diff] [blame] | 90 | InReturns = true; |
Dmitri Gribenko | 0ac4ec7 | 2012-08-21 21:15:34 +0000 | [diff] [blame] | 91 | InBrief = false; |
| 92 | InFirstParagraph = false; |
Dmitri Gribenko | 72021ff | 2012-07-20 17:01:34 +0000 | [diff] [blame] | 93 | ReturnsParagraph += "Returns "; |
Dmitri Gribenko | 0ac4ec7 | 2012-08-21 21:15:34 +0000 | [diff] [blame] | 94 | ConsumeToken(); |
| 95 | continue; |
Dmitri Gribenko | 72021ff | 2012-07-20 17:01:34 +0000 | [diff] [blame] | 96 | } |
Dmitri Gribenko | 55e1808 | 2012-06-29 18:19:20 +0000 | [diff] [blame] | 97 | // Block commands implicitly start a new paragraph. |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 98 | if (Info->IsBlockCommand) { |
Dmitri Gribenko | f199b9c | 2012-06-28 00:01:41 +0000 | [diff] [blame] | 99 | // We found an implicit paragraph end. |
| 100 | InFirstParagraph = false; |
Dmitri Gribenko | 57aceb2 | 2012-07-03 18:10:20 +0000 | [diff] [blame] | 101 | if (InBrief) |
Dmitri Gribenko | f199b9c | 2012-06-28 00:01:41 +0000 | [diff] [blame] | 102 | break; |
Dmitri Gribenko | f199b9c | 2012-06-28 00:01:41 +0000 | [diff] [blame] | 103 | } |
Dmitri Gribenko | 2d44d77 | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | if (Tok.is(tok::newline)) { |
Dmitri Gribenko | c0b8324 | 2012-06-27 01:17:34 +0000 | [diff] [blame] | 107 | if (InFirstParagraph || InBrief) |
Dmitri Gribenko | 72021ff | 2012-07-20 17:01:34 +0000 | [diff] [blame] | 108 | FirstParagraphOrBrief += ' '; |
| 109 | else if (InReturns) |
| 110 | ReturnsParagraph += ' '; |
Dmitri Gribenko | 2d44d77 | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 111 | ConsumeToken(); |
| 112 | |
Dmitri Gribenko | 0ac4ec7 | 2012-08-21 21:15:34 +0000 | [diff] [blame] | 113 | // If the next token is a whitespace only text, ignore it. Thus we allow |
| 114 | // two paragraphs to be separated by line that has only whitespace in it. |
| 115 | // |
| 116 | // We don't need to add a space to the parsed text because we just added |
| 117 | // a space for the newline. |
| 118 | if (Tok.is(tok::text)) { |
| 119 | if (isWhitespace(Tok.getText())) |
| 120 | ConsumeToken(); |
| 121 | } |
| 122 | |
Dmitri Gribenko | 2d44d77 | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 123 | if (Tok.is(tok::newline)) { |
| 124 | ConsumeToken(); |
Dmitri Gribenko | 0ac4ec7 | 2012-08-21 21:15:34 +0000 | [diff] [blame] | 125 | // We found a paragraph end. This ends the brief description if |
| 126 | // \\brief command or its equivalent was explicitly used. |
| 127 | // Stop scanning text because an explicit \\brief paragraph is the |
| 128 | // preffered one. |
Dmitri Gribenko | 57aceb2 | 2012-07-03 18:10:20 +0000 | [diff] [blame] | 129 | if (InBrief) |
Dmitri Gribenko | f199b9c | 2012-06-28 00:01:41 +0000 | [diff] [blame] | 130 | break; |
Dmitri Gribenko | 0ac4ec7 | 2012-08-21 21:15:34 +0000 | [diff] [blame] | 131 | // End first paragraph if we found some non-whitespace text. |
| 132 | if (InFirstParagraph && !isWhitespace(FirstParagraphOrBrief)) |
| 133 | InFirstParagraph = false; |
| 134 | // End the \\returns paragraph because we found the paragraph end. |
| 135 | InReturns = false; |
Dmitri Gribenko | 2d44d77 | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 136 | } |
| 137 | continue; |
| 138 | } |
| 139 | |
| 140 | // We didn't handle this token, so just drop it. |
| 141 | ConsumeToken(); |
| 142 | } |
| 143 | |
Dmitri Gribenko | 72021ff | 2012-07-20 17:01:34 +0000 | [diff] [blame] | 144 | cleanupBrief(FirstParagraphOrBrief); |
| 145 | if (!FirstParagraphOrBrief.empty()) |
| 146 | return FirstParagraphOrBrief; |
| 147 | |
| 148 | cleanupBrief(ReturnsParagraph); |
| 149 | return ReturnsParagraph; |
Dmitri Gribenko | 2d44d77 | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Dmitri Gribenko | 2d44d77 | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 152 | } // end namespace comments |
| 153 | } // end namespace clang |
| 154 | |
| 155 | |