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" |
| 11 | |
| 12 | namespace clang { |
| 13 | namespace comments { |
| 14 | |
Dmitri Gribenko | d558b52 | 2012-06-28 01:38:21 +0000 | [diff] [blame] | 15 | namespace { |
| 16 | /// Convert all whitespace into spaces, remove leading and trailing spaces, |
| 17 | /// compress multiple spaces into one. |
| 18 | void 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 Gribenko | 2d44d77 | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 43 | std::string BriefParser::Parse() { |
Dmitri Gribenko | c0b8324 | 2012-06-27 01:17:34 +0000 | [diff] [blame] | 44 | std::string Paragraph; |
Dmitri Gribenko | 2d44d77 | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 45 | bool InFirstParagraph = true; |
| 46 | bool InBrief = false; |
Dmitri Gribenko | 2d44d77 | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 47 | |
| 48 | while (Tok.isNot(tok::eof)) { |
| 49 | if (Tok.is(tok::text)) { |
Dmitri Gribenko | c0b8324 | 2012-06-27 01:17:34 +0000 | [diff] [blame] | 50 | if (InFirstParagraph || InBrief) |
| 51 | Paragraph += Tok.getText(); |
Dmitri Gribenko | 2d44d77 | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 52 | ConsumeToken(); |
| 53 | continue; |
| 54 | } |
| 55 | |
Dmitri Gribenko | f199b9c | 2012-06-28 00:01:41 +0000 | [diff] [blame] | 56 | 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 Gribenko | 2d44d77 | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | if (Tok.is(tok::newline)) { |
Dmitri Gribenko | c0b8324 | 2012-06-27 01:17:34 +0000 | [diff] [blame] | 77 | if (InFirstParagraph || InBrief) |
Dmitri Gribenko | d558b52 | 2012-06-28 01:38:21 +0000 | [diff] [blame] | 78 | Paragraph += ' '; |
Dmitri Gribenko | 2d44d77 | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 79 | 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 Gribenko | f199b9c | 2012-06-28 00:01:41 +0000 | [diff] [blame] | 87 | break; |
Dmitri Gribenko | 2d44d77 | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | continue; |
| 91 | } |
| 92 | |
| 93 | // We didn't handle this token, so just drop it. |
| 94 | ConsumeToken(); |
| 95 | } |
| 96 | |
Dmitri Gribenko | d558b52 | 2012-06-28 01:38:21 +0000 | [diff] [blame] | 97 | cleanupBrief(Paragraph); |
Dmitri Gribenko | c0b8324 | 2012-06-27 01:17:34 +0000 | [diff] [blame] | 98 | return Paragraph; |
Dmitri Gribenko | 2d44d77 | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | BriefParser::BriefParser(Lexer &L) : L(L) |
| 102 | { |
| 103 | // Get lookahead token. |
| 104 | ConsumeToken(); |
| 105 | } |
| 106 | |
| 107 | } // end namespace comments |
| 108 | } // end namespace clang |
| 109 | |
| 110 | |