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 { |
| 18 | /// Convert all whitespace into spaces, remove leading and trailing spaces, |
| 19 | /// compress multiple spaces into one. |
| 20 | void cleanupBrief(std::string &S) { |
| 21 | bool PrevWasSpace = true; |
| 22 | std::string::iterator O = S.begin(); |
| 23 | for (std::string::iterator I = S.begin(), E = S.end(); |
| 24 | I != E; ++I) { |
| 25 | const char C = *I; |
| 26 | if (C == ' ' || C == '\n' || C == '\r' || |
| 27 | C == '\t' || C == '\v' || C == '\f') { |
| 28 | if (!PrevWasSpace) { |
| 29 | *O++ = ' '; |
| 30 | PrevWasSpace = true; |
| 31 | } |
| 32 | continue; |
| 33 | } else { |
| 34 | *O++ = C; |
| 35 | PrevWasSpace = false; |
| 36 | } |
| 37 | } |
| 38 | if (O != S.begin() && *(O - 1) == ' ') |
| 39 | --O; |
| 40 | |
| 41 | S.resize(O - S.begin()); |
| 42 | } |
| 43 | } // unnamed namespace |
| 44 | |
Dmitri Gribenko | aa58081 | 2012-08-09 00:03:17 +0000 | [diff] [blame^] | 45 | BriefParser::BriefParser(Lexer &L, const CommandTraits &Traits) : |
| 46 | L(L), Traits(Traits) { |
| 47 | // Get lookahead token. |
| 48 | ConsumeToken(); |
| 49 | } |
| 50 | |
Dmitri Gribenko | 2d44d77 | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 51 | std::string BriefParser::Parse() { |
Dmitri Gribenko | 72021ff | 2012-07-20 17:01:34 +0000 | [diff] [blame] | 52 | std::string FirstParagraphOrBrief; |
| 53 | std::string ReturnsParagraph; |
Dmitri Gribenko | 2d44d77 | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 54 | bool InFirstParagraph = true; |
| 55 | bool InBrief = false; |
Dmitri Gribenko | 72021ff | 2012-07-20 17:01:34 +0000 | [diff] [blame] | 56 | bool InReturns = false; |
Dmitri Gribenko | 2d44d77 | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 57 | |
| 58 | while (Tok.isNot(tok::eof)) { |
| 59 | if (Tok.is(tok::text)) { |
Dmitri Gribenko | c0b8324 | 2012-06-27 01:17:34 +0000 | [diff] [blame] | 60 | if (InFirstParagraph || InBrief) |
Dmitri Gribenko | 72021ff | 2012-07-20 17:01:34 +0000 | [diff] [blame] | 61 | FirstParagraphOrBrief += Tok.getText(); |
| 62 | else if (InReturns) |
| 63 | ReturnsParagraph += Tok.getText(); |
Dmitri Gribenko | 2d44d77 | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 64 | ConsumeToken(); |
| 65 | continue; |
| 66 | } |
| 67 | |
Dmitri Gribenko | f199b9c | 2012-06-28 00:01:41 +0000 | [diff] [blame] | 68 | if (Tok.is(tok::command)) { |
| 69 | StringRef Name = Tok.getCommandName(); |
Dmitri Gribenko | aa58081 | 2012-08-09 00:03:17 +0000 | [diff] [blame^] | 70 | if (Traits.isBriefCommand(Name)) { |
Dmitri Gribenko | 72021ff | 2012-07-20 17:01:34 +0000 | [diff] [blame] | 71 | FirstParagraphOrBrief.clear(); |
Dmitri Gribenko | f199b9c | 2012-06-28 00:01:41 +0000 | [diff] [blame] | 72 | InBrief = true; |
| 73 | ConsumeToken(); |
| 74 | continue; |
| 75 | } |
Dmitri Gribenko | aa58081 | 2012-08-09 00:03:17 +0000 | [diff] [blame^] | 76 | if (Traits.isReturnsCommand(Name)) { |
Dmitri Gribenko | 72021ff | 2012-07-20 17:01:34 +0000 | [diff] [blame] | 77 | InReturns = true; |
| 78 | ReturnsParagraph += "Returns "; |
| 79 | } |
Dmitri Gribenko | 55e1808 | 2012-06-29 18:19:20 +0000 | [diff] [blame] | 80 | // Block commands implicitly start a new paragraph. |
Dmitri Gribenko | aa58081 | 2012-08-09 00:03:17 +0000 | [diff] [blame^] | 81 | if (Traits.isBlockCommand(Name)) { |
Dmitri Gribenko | f199b9c | 2012-06-28 00:01:41 +0000 | [diff] [blame] | 82 | // We found an implicit paragraph end. |
| 83 | InFirstParagraph = false; |
Dmitri Gribenko | 57aceb2 | 2012-07-03 18:10:20 +0000 | [diff] [blame] | 84 | if (InBrief) |
Dmitri Gribenko | f199b9c | 2012-06-28 00:01:41 +0000 | [diff] [blame] | 85 | break; |
Dmitri Gribenko | f199b9c | 2012-06-28 00:01:41 +0000 | [diff] [blame] | 86 | } |
Dmitri Gribenko | 2d44d77 | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | if (Tok.is(tok::newline)) { |
Dmitri Gribenko | c0b8324 | 2012-06-27 01:17:34 +0000 | [diff] [blame] | 90 | if (InFirstParagraph || InBrief) |
Dmitri Gribenko | 72021ff | 2012-07-20 17:01:34 +0000 | [diff] [blame] | 91 | FirstParagraphOrBrief += ' '; |
| 92 | else if (InReturns) |
| 93 | ReturnsParagraph += ' '; |
Dmitri Gribenko | 2d44d77 | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 94 | ConsumeToken(); |
| 95 | |
| 96 | if (Tok.is(tok::newline)) { |
| 97 | ConsumeToken(); |
| 98 | // We found a paragraph end. |
| 99 | InFirstParagraph = false; |
Dmitri Gribenko | 72021ff | 2012-07-20 17:01:34 +0000 | [diff] [blame] | 100 | InReturns = 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 | 2d44d77 | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 103 | } |
| 104 | continue; |
| 105 | } |
| 106 | |
| 107 | // We didn't handle this token, so just drop it. |
| 108 | ConsumeToken(); |
| 109 | } |
| 110 | |
Dmitri Gribenko | 72021ff | 2012-07-20 17:01:34 +0000 | [diff] [blame] | 111 | cleanupBrief(FirstParagraphOrBrief); |
| 112 | if (!FirstParagraphOrBrief.empty()) |
| 113 | return FirstParagraphOrBrief; |
| 114 | |
| 115 | cleanupBrief(ReturnsParagraph); |
| 116 | return ReturnsParagraph; |
Dmitri Gribenko | 2d44d77 | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Dmitri Gribenko | 2d44d77 | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 119 | } // end namespace comments |
| 120 | } // end namespace clang |
| 121 | |
| 122 | |