CommentLexer.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- CommentLexer.h - Lexer for structured comments ---------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines lexer for structured comments and supporting token class.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_CLANG_AST_COMMENTLEXER_H
  18. #define LLVM_CLANG_AST_COMMENTLEXER_H
  19. #include "clang/Basic/Diagnostic.h"
  20. #include "clang/Basic/SourceManager.h"
  21. #include "llvm/ADT/SmallString.h"
  22. #include "llvm/ADT/StringRef.h"
  23. #include "llvm/Support/Allocator.h"
  24. #include "llvm/Support/raw_ostream.h"
  25. namespace clang {
  26. namespace comments {
  27. class Lexer;
  28. class TextTokenRetokenizer;
  29. struct CommandInfo;
  30. class CommandTraits;
  31. namespace tok {
  32. enum TokenKind {
  33. eof,
  34. newline,
  35. text,
  36. unknown_command, // Command that does not have an ID.
  37. backslash_command, // Command with an ID, that used backslash marker.
  38. at_command, // Command with an ID, that used 'at' marker.
  39. verbatim_block_begin,
  40. verbatim_block_line,
  41. verbatim_block_end,
  42. verbatim_line_name,
  43. verbatim_line_text,
  44. html_start_tag, // <tag
  45. html_ident, // attr
  46. html_equals, // =
  47. html_quoted_string, // "blah\"blah" or 'blah\'blah'
  48. html_greater, // >
  49. html_slash_greater, // />
  50. html_end_tag // </tag
  51. };
  52. } // end namespace tok
  53. /// Comment token.
  54. class Token {
  55. friend class Lexer;
  56. friend class TextTokenRetokenizer;
  57. /// The location of the token.
  58. SourceLocation Loc;
  59. /// The actual kind of the token.
  60. tok::TokenKind Kind;
  61. /// Integer value associated with a token.
  62. ///
  63. /// If the token is a known command, contains command ID and TextPtr is
  64. /// unused (command spelling can be found with CommandTraits). Otherwise,
  65. /// contains the length of the string that starts at TextPtr.
  66. unsigned IntVal;
  67. /// Length of the token spelling in comment. Can be 0 for synthenized
  68. /// tokens.
  69. unsigned Length;
  70. /// Contains text value associated with a token.
  71. const char *TextPtr;
  72. public:
  73. SourceLocation getLocation() const LLVM_READONLY { return Loc; }
  74. void setLocation(SourceLocation SL) { Loc = SL; }
  75. SourceLocation getEndLocation() const LLVM_READONLY {
  76. if (Length == 0 || Length == 1)
  77. return Loc;
  78. return Loc.getLocWithOffset(Length - 1);
  79. }
  80. tok::TokenKind getKind() const LLVM_READONLY { return Kind; }
  81. void setKind(tok::TokenKind K) { Kind = K; }
  82. bool is(tok::TokenKind K) const LLVM_READONLY { return Kind == K; }
  83. bool isNot(tok::TokenKind K) const LLVM_READONLY { return Kind != K; }
  84. unsigned getLength() const LLVM_READONLY { return Length; }
  85. void setLength(unsigned L) { Length = L; }
  86. StringRef getText() const LLVM_READONLY {
  87. assert(is(tok::text));
  88. return StringRef(TextPtr, IntVal);
  89. }
  90. void setText(StringRef Text) {
  91. assert(is(tok::text));
  92. TextPtr = Text.data();
  93. IntVal = Text.size();
  94. }
  95. StringRef getUnknownCommandName() const LLVM_READONLY {
  96. assert(is(tok::unknown_command));
  97. return StringRef(TextPtr, IntVal);
  98. }
  99. void setUnknownCommandName(StringRef Name) {
  100. assert(is(tok::unknown_command));
  101. TextPtr = Name.data();
  102. IntVal = Name.size();
  103. }
  104. unsigned getCommandID() const LLVM_READONLY {
  105. assert(is(tok::backslash_command) || is(tok::at_command));
  106. return IntVal;
  107. }
  108. void setCommandID(unsigned ID) {
  109. assert(is(tok::backslash_command) || is(tok::at_command));
  110. IntVal = ID;
  111. }
  112. unsigned getVerbatimBlockID() const LLVM_READONLY {
  113. assert(is(tok::verbatim_block_begin) || is(tok::verbatim_block_end));
  114. return IntVal;
  115. }
  116. void setVerbatimBlockID(unsigned ID) {
  117. assert(is(tok::verbatim_block_begin) || is(tok::verbatim_block_end));
  118. IntVal = ID;
  119. }
  120. StringRef getVerbatimBlockText() const LLVM_READONLY {
  121. assert(is(tok::verbatim_block_line));
  122. return StringRef(TextPtr, IntVal);
  123. }
  124. void setVerbatimBlockText(StringRef Text) {
  125. assert(is(tok::verbatim_block_line));
  126. TextPtr = Text.data();
  127. IntVal = Text.size();
  128. }
  129. unsigned getVerbatimLineID() const LLVM_READONLY {
  130. assert(is(tok::verbatim_line_name));
  131. return IntVal;
  132. }
  133. void setVerbatimLineID(unsigned ID) {
  134. assert(is(tok::verbatim_line_name));
  135. IntVal = ID;
  136. }
  137. StringRef getVerbatimLineText() const LLVM_READONLY {
  138. assert(is(tok::verbatim_line_text));
  139. return StringRef(TextPtr, IntVal);
  140. }
  141. void setVerbatimLineText(StringRef Text) {
  142. assert(is(tok::verbatim_line_text));
  143. TextPtr = Text.data();
  144. IntVal = Text.size();
  145. }
  146. StringRef getHTMLTagStartName() const LLVM_READONLY {
  147. assert(is(tok::html_start_tag));
  148. return StringRef(TextPtr, IntVal);
  149. }
  150. void setHTMLTagStartName(StringRef Name) {
  151. assert(is(tok::html_start_tag));
  152. TextPtr = Name.data();
  153. IntVal = Name.size();
  154. }
  155. StringRef getHTMLIdent() const LLVM_READONLY {
  156. assert(is(tok::html_ident));
  157. return StringRef(TextPtr, IntVal);
  158. }
  159. void setHTMLIdent(StringRef Name) {
  160. assert(is(tok::html_ident));
  161. TextPtr = Name.data();
  162. IntVal = Name.size();
  163. }
  164. StringRef getHTMLQuotedString() const LLVM_READONLY {
  165. assert(is(tok::html_quoted_string));
  166. return StringRef(TextPtr, IntVal);
  167. }
  168. void setHTMLQuotedString(StringRef Str) {
  169. assert(is(tok::html_quoted_string));
  170. TextPtr = Str.data();
  171. IntVal = Str.size();
  172. }
  173. StringRef getHTMLTagEndName() const LLVM_READONLY {
  174. assert(is(tok::html_end_tag));
  175. return StringRef(TextPtr, IntVal);
  176. }
  177. void setHTMLTagEndName(StringRef Name) {
  178. assert(is(tok::html_end_tag));
  179. TextPtr = Name.data();
  180. IntVal = Name.size();
  181. }
  182. void dump(const Lexer &L, const SourceManager &SM) const;
  183. };
  184. /// Comment lexer.
  185. class Lexer {
  186. private:
  187. Lexer(const Lexer &) = delete;
  188. void operator=(const Lexer &) = delete;
  189. /// Allocator for strings that are semantic values of tokens and have to be
  190. /// computed (for example, resolved decimal character references).
  191. llvm::BumpPtrAllocator &Allocator;
  192. DiagnosticsEngine &Diags;
  193. const CommandTraits &Traits;
  194. const char *const BufferStart;
  195. const char *const BufferEnd;
  196. const char *BufferPtr;
  197. /// One past end pointer for the current comment. For BCPL comments points
  198. /// to newline or BufferEnd, for C comments points to star in '*/'.
  199. const char *CommentEnd;
  200. SourceLocation FileLoc;
  201. /// If true, the commands, html tags, etc will be parsed and reported as
  202. /// separate tokens inside the comment body. If false, the comment text will
  203. /// be parsed into text and newline tokens.
  204. bool ParseCommands;
  205. enum LexerCommentState : uint8_t {
  206. LCS_BeforeComment,
  207. LCS_InsideBCPLComment,
  208. LCS_InsideCComment,
  209. LCS_BetweenComments
  210. };
  211. /// Low-level lexer state, track if we are inside or outside of comment.
  212. LexerCommentState CommentState;
  213. enum LexerState : uint8_t {
  214. /// Lexing normal comment text
  215. LS_Normal,
  216. /// Finished lexing verbatim block beginning command, will lex first body
  217. /// line.
  218. LS_VerbatimBlockFirstLine,
  219. /// Lexing verbatim block body line-by-line, skipping line-starting
  220. /// decorations.
  221. LS_VerbatimBlockBody,
  222. /// Finished lexing verbatim line beginning command, will lex text (one
  223. /// line).
  224. LS_VerbatimLineText,
  225. /// Finished lexing \verbatim <TAG \endverbatim part, lexing tag attributes.
  226. LS_HTMLStartTag,
  227. /// Finished lexing \verbatim </TAG \endverbatim part, lexing '>'.
  228. LS_HTMLEndTag
  229. };
  230. /// Current lexing mode.
  231. LexerState State;
  232. /// If State is LS_VerbatimBlock, contains the name of verbatim end
  233. /// command, including command marker.
  234. SmallString<16> VerbatimBlockEndCommandName;
  235. /// Given a character reference name (e.g., "lt"), return the character that
  236. /// it stands for (e.g., "<").
  237. StringRef resolveHTMLNamedCharacterReference(StringRef Name) const;
  238. /// Given a Unicode codepoint as base-10 integer, return the character.
  239. StringRef resolveHTMLDecimalCharacterReference(StringRef Name) const;
  240. /// Given a Unicode codepoint as base-16 integer, return the character.
  241. StringRef resolveHTMLHexCharacterReference(StringRef Name) const;
  242. void formTokenWithChars(Token &Result, const char *TokEnd,
  243. tok::TokenKind Kind);
  244. void formTextToken(Token &Result, const char *TokEnd) {
  245. StringRef Text(BufferPtr, TokEnd - BufferPtr);
  246. formTokenWithChars(Result, TokEnd, tok::text);
  247. Result.setText(Text);
  248. }
  249. SourceLocation getSourceLocation(const char *Loc) const {
  250. assert(Loc >= BufferStart && Loc <= BufferEnd &&
  251. "Location out of range for this buffer!");
  252. const unsigned CharNo = Loc - BufferStart;
  253. return FileLoc.getLocWithOffset(CharNo);
  254. }
  255. DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) {
  256. return Diags.Report(Loc, DiagID);
  257. }
  258. /// Eat string matching regexp \code \s*\* \endcode.
  259. void skipLineStartingDecorations();
  260. /// Skip over pure text.
  261. const char *skipTextToken();
  262. /// Lex comment text, including commands if ParseCommands is set to true.
  263. void lexCommentText(Token &T);
  264. void setupAndLexVerbatimBlock(Token &T, const char *TextBegin, char Marker,
  265. const CommandInfo *Info);
  266. void lexVerbatimBlockFirstLine(Token &T);
  267. void lexVerbatimBlockBody(Token &T);
  268. void setupAndLexVerbatimLine(Token &T, const char *TextBegin,
  269. const CommandInfo *Info);
  270. void lexVerbatimLineText(Token &T);
  271. void lexHTMLCharacterReference(Token &T);
  272. void setupAndLexHTMLStartTag(Token &T);
  273. void lexHTMLStartTag(Token &T);
  274. void setupAndLexHTMLEndTag(Token &T);
  275. void lexHTMLEndTag(Token &T);
  276. public:
  277. Lexer(llvm::BumpPtrAllocator &Allocator, DiagnosticsEngine &Diags,
  278. const CommandTraits &Traits, SourceLocation FileLoc,
  279. const char *BufferStart, const char *BufferEnd,
  280. bool ParseCommands = true);
  281. void lex(Token &T);
  282. StringRef getSpelling(const Token &Tok, const SourceManager &SourceMgr) const;
  283. };
  284. } // end namespace comments
  285. } // end namespace clang
  286. #endif
  287. #ifdef __GNUC__
  288. #pragma GCC diagnostic pop
  289. #endif