aboutsummaryrefslogtreecommitdiff
path: root/lib/AST/CommentBriefParser.cpp
blob: 528fd2606fe7e9a7f1c417b503b9a202e437ebde (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//===--- CommentBriefParser.cpp - Dumb comment parser ---------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "clang/AST/CommentBriefParser.h"

namespace clang {
namespace comments {

std::string BriefParser::Parse() {
  std::string FirstParagraph;
  std::string Brief;
  bool InFirstParagraph = true;
  bool InBrief = false;
  bool BriefDone = false;

  while (Tok.isNot(tok::eof)) {
    if (Tok.is(tok::text)) {
      if (InFirstParagraph)
        FirstParagraph += Tok.getText();
      if (InBrief)
        Brief += Tok.getText();
      ConsumeToken();
      continue;
    }

    if (!BriefDone && Tok.is(tok::command) && Tok.getCommandName() == "brief") {
      InBrief = true;
      ConsumeToken();
      continue;
    }

    if (Tok.is(tok::newline)) {
      if (InFirstParagraph)
        FirstParagraph += '\n';
      if (InBrief)
        Brief += '\n';
      ConsumeToken();

      if (Tok.is(tok::newline)) {
        ConsumeToken();
        // We found a paragraph end.
        InFirstParagraph = false;
        if (InBrief) {
          InBrief = false;
          BriefDone = true;
        }
      }
      continue;
    }

    // We didn't handle this token, so just drop it.
    ConsumeToken();
  }

  if (Brief.size() > 0)
    return Brief;

  return FirstParagraph;
}

BriefParser::BriefParser(Lexer &L) : L(L)
{
  // Get lookahead token.
  ConsumeToken();
}

} // end namespace comments
} // end namespace clang