blob: 20180994177ed93580d870b67f75d05552430992 [file] [log] [blame]
Dmitri Gribenko2d44d772012-06-26 20:39:18 +00001#include "clang/AST/CommentLexer.h"
Dmitri Gribenkoaa580812012-08-09 00:03:17 +00002#include "clang/AST/CommentCommandTraits.h"
Dmitri Gribenkobf881442013-02-09 15:16:58 +00003#include "clang/Basic/CharInfo.h"
Dmitri Gribenkoc934dfe2013-01-19 22:06:05 +00004#include "llvm/ADT/StringExtras.h"
Dmitri Gribenko2d44d772012-06-26 20:39:18 +00005#include "llvm/ADT/StringSwitch.h"
Dmitri Gribenkocb5620c2013-01-30 12:06:08 +00006#include "llvm/Support/ConvertUTF.h"
Dmitri Gribenko2d44d772012-06-26 20:39:18 +00007#include "llvm/Support/ErrorHandling.h"
8
9namespace clang {
10namespace comments {
11
12void Token::dump(const Lexer &L, const SourceManager &SM) const {
13 llvm::errs() << "comments::Token Kind=" << Kind << " ";
14 Loc.dump(SM);
15 llvm::errs() << " " << Length << " \"" << L.getSpelling(*this, SM) << "\"\n";
16}
17
Dmitri Gribenko0ff4f8b2013-02-10 11:54:22 +000018static inline bool isHTMLNamedCharacterReferenceCharacter(char C) {
Dmitri Gribenkobf881442013-02-09 15:16:58 +000019 return isLetter(C);
Dmitri Gribenko477a9f52012-07-27 20:37:06 +000020}
21
Dmitri Gribenko0ff4f8b2013-02-10 11:54:22 +000022static inline bool isHTMLDecimalCharacterReferenceCharacter(char C) {
Dmitri Gribenkobf881442013-02-09 15:16:58 +000023 return isDigit(C);
Dmitri Gribenko477a9f52012-07-27 20:37:06 +000024}
25
Dmitri Gribenko0ff4f8b2013-02-10 11:54:22 +000026static inline bool isHTMLHexCharacterReferenceCharacter(char C) {
Dmitri Gribenkobf881442013-02-09 15:16:58 +000027 return isHexDigit(C);
Dmitri Gribenko477a9f52012-07-27 20:37:06 +000028}
Dmitri Gribenko834a5bd2012-08-22 22:56:08 +000029
Dmitri Gribenko0ff4f8b2013-02-10 11:54:22 +000030static inline StringRef convertCodePointToUTF8(
31 llvm::BumpPtrAllocator &Allocator,
32 unsigned CodePoint) {
Fariborz Jahanian658a1152013-01-29 23:42:26 +000033 char *Resolved = Allocator.Allocate<char>(UNI_MAX_UTF8_BYTES_PER_CODE_POINT);
34 char *ResolvedPtr = Resolved;
Dmitri Gribenkocb5620c2013-01-30 12:06:08 +000035 if (llvm::ConvertCodePointToUTF8(CodePoint, ResolvedPtr))
Fariborz Jahanian658a1152013-01-29 23:42:26 +000036 return StringRef(Resolved, ResolvedPtr - Resolved);
37 else
38 return StringRef();
39}
Dmitri Gribenko5bd1e5b2013-01-30 14:29:28 +000040
Dmitri Gribenko0ff4f8b2013-02-10 11:54:22 +000041namespace {
42
Dmitri Gribenko5bd1e5b2013-01-30 14:29:28 +000043#include "clang/AST/CommentHTMLTags.inc"
44#include "clang/AST/CommentHTMLNamedCharacterReferences.inc"
45
46} // unnamed namespace
Fariborz Jahanian658a1152013-01-29 23:42:26 +000047
Dmitri Gribenko477a9f52012-07-27 20:37:06 +000048StringRef Lexer::resolveHTMLNamedCharacterReference(StringRef Name) const {
Dmitri Gribenko5bd1e5b2013-01-30 14:29:28 +000049 // Fast path, first check a few most widely used named character references.
Dmitri Gribenko477a9f52012-07-27 20:37:06 +000050 return llvm::StringSwitch<StringRef>(Name)
51 .Case("amp", "&")
52 .Case("lt", "<")
53 .Case("gt", ">")
54 .Case("quot", "\"")
55 .Case("apos", "\'")
Dmitri Gribenko5bd1e5b2013-01-30 14:29:28 +000056 // Slow path.
57 .Default(translateHTMLNamedCharacterReferenceToUTF8(Name));
Fariborz Jahanian658a1152013-01-29 23:42:26 +000058}
Dmitri Gribenko477a9f52012-07-27 20:37:06 +000059
60StringRef Lexer::resolveHTMLDecimalCharacterReference(StringRef Name) const {
61 unsigned CodePoint = 0;
62 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
63 assert(isHTMLDecimalCharacterReferenceCharacter(Name[i]));
64 CodePoint *= 10;
65 CodePoint += Name[i] - '0';
66 }
Dmitri Gribenko5bd1e5b2013-01-30 14:29:28 +000067 return convertCodePointToUTF8(Allocator, CodePoint);
68}
Dmitri Gribenko477a9f52012-07-27 20:37:06 +000069
Dmitri Gribenko5bd1e5b2013-01-30 14:29:28 +000070StringRef Lexer::resolveHTMLHexCharacterReference(StringRef Name) const {
71 unsigned CodePoint = 0;
72 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
73 CodePoint *= 16;
74 const char C = Name[i];
75 assert(isHTMLHexCharacterReferenceCharacter(C));
76 CodePoint += llvm::hexDigitValue(C);
77 }
78 return convertCodePointToUTF8(Allocator, CodePoint);
Dmitri Gribenko477a9f52012-07-27 20:37:06 +000079}
80
Dmitri Gribenko2d44d772012-06-26 20:39:18 +000081void Lexer::skipLineStartingDecorations() {
82 // This function should be called only for C comments
83 assert(CommentState == LCS_InsideCComment);
84
85 if (BufferPtr == CommentEnd)
86 return;
87
88 switch (*BufferPtr) {
89 case ' ':
90 case '\t':
91 case '\f':
92 case '\v': {
93 const char *NewBufferPtr = BufferPtr;
94 NewBufferPtr++;
95 if (NewBufferPtr == CommentEnd)
96 return;
97
98 char C = *NewBufferPtr;
Dmitri Gribenkobf881442013-02-09 15:16:58 +000099 while (isHorizontalWhitespace(C)) {
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000100 NewBufferPtr++;
101 if (NewBufferPtr == CommentEnd)
102 return;
103 C = *NewBufferPtr;
104 }
105 if (C == '*')
106 BufferPtr = NewBufferPtr + 1;
107 break;
108 }
109 case '*':
110 BufferPtr++;
111 break;
112 }
113}
114
115namespace {
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000116/// Returns pointer to the first newline character in the string.
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000117const char *findNewline(const char *BufferPtr, const char *BufferEnd) {
118 for ( ; BufferPtr != BufferEnd; ++BufferPtr) {
Dmitri Gribenkobf881442013-02-09 15:16:58 +0000119 if (isVerticalWhitespace(*BufferPtr))
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000120 return BufferPtr;
121 }
122 return BufferEnd;
123}
124
125const char *skipNewline(const char *BufferPtr, const char *BufferEnd) {
126 if (BufferPtr == BufferEnd)
127 return BufferPtr;
128
129 if (*BufferPtr == '\n')
130 BufferPtr++;
131 else {
132 assert(*BufferPtr == '\r');
133 BufferPtr++;
134 if (BufferPtr != BufferEnd && *BufferPtr == '\n')
135 BufferPtr++;
136 }
137 return BufferPtr;
138}
139
Dmitri Gribenko477a9f52012-07-27 20:37:06 +0000140const char *skipNamedCharacterReference(const char *BufferPtr,
141 const char *BufferEnd) {
142 for ( ; BufferPtr != BufferEnd; ++BufferPtr) {
143 if (!isHTMLNamedCharacterReferenceCharacter(*BufferPtr))
144 return BufferPtr;
145 }
146 return BufferEnd;
147}
148
149const char *skipDecimalCharacterReference(const char *BufferPtr,
150 const char *BufferEnd) {
151 for ( ; BufferPtr != BufferEnd; ++BufferPtr) {
152 if (!isHTMLDecimalCharacterReferenceCharacter(*BufferPtr))
153 return BufferPtr;
154 }
155 return BufferEnd;
156}
157
158const char *skipHexCharacterReference(const char *BufferPtr,
159 const char *BufferEnd) {
160 for ( ; BufferPtr != BufferEnd; ++BufferPtr) {
161 if (!isHTMLHexCharacterReferenceCharacter(*BufferPtr))
162 return BufferPtr;
163 }
164 return BufferEnd;
165}
166
Dmitri Gribenkoa99ec102012-07-09 21:32:40 +0000167bool isHTMLIdentifierStartingCharacter(char C) {
Dmitri Gribenkobf881442013-02-09 15:16:58 +0000168 return isLetter(C);
Dmitri Gribenkoa99ec102012-07-09 21:32:40 +0000169}
170
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000171bool isHTMLIdentifierCharacter(char C) {
Dmitri Gribenkobf881442013-02-09 15:16:58 +0000172 return isAlphanumeric(C);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000173}
174
175const char *skipHTMLIdentifier(const char *BufferPtr, const char *BufferEnd) {
176 for ( ; BufferPtr != BufferEnd; ++BufferPtr) {
177 if (!isHTMLIdentifierCharacter(*BufferPtr))
178 return BufferPtr;
179 }
180 return BufferEnd;
181}
182
183/// Skip HTML string quoted in single or double quotes. Escaping quotes inside
184/// string allowed.
185///
186/// Returns pointer to closing quote.
187const char *skipHTMLQuotedString(const char *BufferPtr, const char *BufferEnd)
188{
189 const char Quote = *BufferPtr;
190 assert(Quote == '\"' || Quote == '\'');
191
192 BufferPtr++;
193 for ( ; BufferPtr != BufferEnd; ++BufferPtr) {
194 const char C = *BufferPtr;
195 if (C == Quote && BufferPtr[-1] != '\\')
196 return BufferPtr;
197 }
198 return BufferEnd;
199}
200
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000201const char *skipWhitespace(const char *BufferPtr, const char *BufferEnd) {
202 for ( ; BufferPtr != BufferEnd; ++BufferPtr) {
203 if (!isWhitespace(*BufferPtr))
204 return BufferPtr;
205 }
206 return BufferEnd;
207}
208
Dmitri Gribenko64da4e52012-07-18 23:01:58 +0000209bool isWhitespace(const char *BufferPtr, const char *BufferEnd) {
210 return skipWhitespace(BufferPtr, BufferEnd) == BufferEnd;
211}
212
Dmitri Gribenko8c05da32012-09-14 16:35:35 +0000213bool isCommandNameStartCharacter(char C) {
Dmitri Gribenkobf881442013-02-09 15:16:58 +0000214 return isLetter(C);
Dmitri Gribenko8c05da32012-09-14 16:35:35 +0000215}
216
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000217bool isCommandNameCharacter(char C) {
Dmitri Gribenkobf881442013-02-09 15:16:58 +0000218 return isAlphanumeric(C);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000219}
220
221const char *skipCommandName(const char *BufferPtr, const char *BufferEnd) {
222 for ( ; BufferPtr != BufferEnd; ++BufferPtr) {
223 if (!isCommandNameCharacter(*BufferPtr))
224 return BufferPtr;
225 }
226 return BufferEnd;
227}
228
229/// Return the one past end pointer for BCPL comments.
230/// Handles newlines escaped with backslash or trigraph for backslahs.
231const char *findBCPLCommentEnd(const char *BufferPtr, const char *BufferEnd) {
232 const char *CurPtr = BufferPtr;
233 while (CurPtr != BufferEnd) {
Dmitri Gribenkobf881442013-02-09 15:16:58 +0000234 while (!isVerticalWhitespace(*CurPtr)) {
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000235 CurPtr++;
236 if (CurPtr == BufferEnd)
237 return BufferEnd;
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000238 }
239 // We found a newline, check if it is escaped.
240 const char *EscapePtr = CurPtr - 1;
241 while(isHorizontalWhitespace(*EscapePtr))
242 EscapePtr--;
243
244 if (*EscapePtr == '\\' ||
245 (EscapePtr - 2 >= BufferPtr && EscapePtr[0] == '/' &&
246 EscapePtr[-1] == '?' && EscapePtr[-2] == '?')) {
247 // We found an escaped newline.
248 CurPtr = skipNewline(CurPtr, BufferEnd);
249 } else
250 return CurPtr; // Not an escaped newline.
251 }
252 return BufferEnd;
253}
254
255/// Return the one past end pointer for C comments.
256/// Very dumb, does not handle escaped newlines or trigraphs.
257const char *findCCommentEnd(const char *BufferPtr, const char *BufferEnd) {
258 for ( ; BufferPtr != BufferEnd; ++BufferPtr) {
259 if (*BufferPtr == '*') {
260 assert(BufferPtr + 1 != BufferEnd);
261 if (*(BufferPtr + 1) == '/')
262 return BufferPtr;
263 }
264 }
265 llvm_unreachable("buffer end hit before '*/' was seen");
266}
267} // unnamed namespace
268
269void Lexer::lexCommentText(Token &T) {
270 assert(CommentState == LCS_InsideBCPLComment ||
271 CommentState == LCS_InsideCComment);
272
273 switch (State) {
274 case LS_Normal:
275 break;
276 case LS_VerbatimBlockFirstLine:
277 lexVerbatimBlockFirstLine(T);
278 return;
279 case LS_VerbatimBlockBody:
280 lexVerbatimBlockBody(T);
281 return;
Dmitri Gribenko962668d2012-06-27 16:53:58 +0000282 case LS_VerbatimLineText:
283 lexVerbatimLineText(T);
284 return;
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000285 case LS_HTMLStartTag:
286 lexHTMLStartTag(T);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000287 return;
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000288 case LS_HTMLEndTag:
289 lexHTMLEndTag(T);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000290 return;
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000291 }
292
293 assert(State == LS_Normal);
294
295 const char *TokenPtr = BufferPtr;
296 assert(TokenPtr < CommentEnd);
297 while (TokenPtr != CommentEnd) {
298 switch(*TokenPtr) {
299 case '\\':
300 case '@': {
Fariborz Jahanian8536fa12013-03-02 02:39:57 +0000301 bool AtCommand = (*TokenPtr == '@');
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000302 TokenPtr++;
303 if (TokenPtr == CommentEnd) {
Dmitri Gribenko477a9f52012-07-27 20:37:06 +0000304 formTextToken(T, TokenPtr);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000305 return;
306 }
307 char C = *TokenPtr;
308 switch (C) {
309 default:
310 break;
311
312 case '\\': case '@': case '&': case '$':
313 case '#': case '<': case '>': case '%':
314 case '\"': case '.': case ':':
315 // This is one of \\ \@ \& \$ etc escape sequences.
316 TokenPtr++;
317 if (C == ':' && TokenPtr != CommentEnd && *TokenPtr == ':') {
318 // This is the \:: escape sequence.
319 TokenPtr++;
320 }
Dmitri Gribenkof5e0aea2012-06-27 16:30:35 +0000321 StringRef UnescapedText(BufferPtr + 1, TokenPtr - (BufferPtr + 1));
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000322 formTokenWithChars(T, TokenPtr, tok::text);
Dmitri Gribenkof5e0aea2012-06-27 16:30:35 +0000323 T.setText(UnescapedText);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000324 return;
325 }
326
327 // Don't make zero-length commands.
Dmitri Gribenko8c05da32012-09-14 16:35:35 +0000328 if (!isCommandNameStartCharacter(*TokenPtr)) {
Dmitri Gribenko477a9f52012-07-27 20:37:06 +0000329 formTextToken(T, TokenPtr);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000330 return;
331 }
332
333 TokenPtr = skipCommandName(TokenPtr, CommentEnd);
334 unsigned Length = TokenPtr - (BufferPtr + 1);
335
336 // Hardcoded support for lexing LaTeX formula commands
337 // \f$ \f[ \f] \f{ \f} as a single command.
338 if (Length == 1 && TokenPtr[-1] == 'f' && TokenPtr != CommentEnd) {
339 C = *TokenPtr;
340 if (C == '$' || C == '[' || C == ']' || C == '{' || C == '}') {
341 TokenPtr++;
342 Length++;
343 }
344 }
345
346 const StringRef CommandName(BufferPtr + 1, Length);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000347
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000348 const CommandInfo *Info = Traits.getCommandInfoOrNULL(CommandName);
349 if (!Info) {
350 formTokenWithChars(T, TokenPtr, tok::unknown_command);
351 T.setUnknownCommandName(CommandName);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000352 return;
353 }
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000354 if (Info->IsVerbatimBlockCommand) {
355 setupAndLexVerbatimBlock(T, TokenPtr, *BufferPtr, Info);
356 return;
357 }
358 if (Info->IsVerbatimLineCommand) {
359 setupAndLexVerbatimLine(T, TokenPtr, Info);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000360 return;
361 }
Fariborz Jahanian8536fa12013-03-02 02:39:57 +0000362 formTokenWithChars(T, TokenPtr,
363 (AtCommand ? tok::at_command
364 : tok::backslash_command));
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000365 T.setCommandID(Info->getID());
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000366 return;
367 }
368
Dmitri Gribenko477a9f52012-07-27 20:37:06 +0000369 case '&':
370 lexHTMLCharacterReference(T);
371 return;
372
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000373 case '<': {
374 TokenPtr++;
375 if (TokenPtr == CommentEnd) {
Dmitri Gribenko477a9f52012-07-27 20:37:06 +0000376 formTextToken(T, TokenPtr);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000377 return;
378 }
379 const char C = *TokenPtr;
Dmitri Gribenkoa99ec102012-07-09 21:32:40 +0000380 if (isHTMLIdentifierStartingCharacter(C))
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000381 setupAndLexHTMLStartTag(T);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000382 else if (C == '/')
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000383 setupAndLexHTMLEndTag(T);
Dmitri Gribenko477a9f52012-07-27 20:37:06 +0000384 else
385 formTextToken(T, TokenPtr);
386
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000387 return;
388 }
389
390 case '\n':
391 case '\r':
392 TokenPtr = skipNewline(TokenPtr, CommentEnd);
393 formTokenWithChars(T, TokenPtr, tok::newline);
394
395 if (CommentState == LCS_InsideCComment)
396 skipLineStartingDecorations();
397 return;
398
399 default: {
Dmitri Gribenkoaa7dbaf2012-12-30 19:45:46 +0000400 size_t End = StringRef(TokenPtr, CommentEnd - TokenPtr).
401 find_first_of("\n\r\\@&<");
402 if (End != StringRef::npos)
403 TokenPtr += End;
404 else
405 TokenPtr = CommentEnd;
Dmitri Gribenko477a9f52012-07-27 20:37:06 +0000406 formTextToken(T, TokenPtr);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000407 return;
408 }
409 }
410 }
411}
412
413void Lexer::setupAndLexVerbatimBlock(Token &T,
414 const char *TextBegin,
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000415 char Marker, const CommandInfo *Info) {
416 assert(Info->IsVerbatimBlockCommand);
417
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000418 VerbatimBlockEndCommandName.clear();
419 VerbatimBlockEndCommandName.append(Marker == '\\' ? "\\" : "@");
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000420 VerbatimBlockEndCommandName.append(Info->EndCommandName);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000421
422 formTokenWithChars(T, TextBegin, tok::verbatim_block_begin);
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000423 T.setVerbatimBlockID(Info->getID());
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000424
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000425 // If there is a newline following the verbatim opening command, skip the
426 // newline so that we don't create an tok::verbatim_block_line with empty
427 // text content.
Dmitri Gribenkobf881442013-02-09 15:16:58 +0000428 if (BufferPtr != CommentEnd &&
429 isVerticalWhitespace(*BufferPtr)) {
430 BufferPtr = skipNewline(BufferPtr, CommentEnd);
431 State = LS_VerbatimBlockBody;
432 return;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000433 }
434
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000435 State = LS_VerbatimBlockFirstLine;
436}
437
438void Lexer::lexVerbatimBlockFirstLine(Token &T) {
Dmitri Gribenko64da4e52012-07-18 23:01:58 +0000439again:
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000440 assert(BufferPtr < CommentEnd);
441
442 // FIXME: It would be better to scan the text once, finding either the block
443 // end command or newline.
444 //
445 // Extract current line.
446 const char *Newline = findNewline(BufferPtr, CommentEnd);
447 StringRef Line(BufferPtr, Newline - BufferPtr);
448
449 // Look for end command in current line.
450 size_t Pos = Line.find(VerbatimBlockEndCommandName);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000451 const char *TextEnd;
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000452 const char *NextLine;
453 if (Pos == StringRef::npos) {
454 // Current line is completely verbatim.
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000455 TextEnd = Newline;
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000456 NextLine = skipNewline(Newline, CommentEnd);
457 } else if (Pos == 0) {
458 // Current line contains just an end command.
459 const char *End = BufferPtr + VerbatimBlockEndCommandName.size();
Dmitri Gribenkof5e0aea2012-06-27 16:30:35 +0000460 StringRef Name(BufferPtr + 1, End - (BufferPtr + 1));
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000461 formTokenWithChars(T, End, tok::verbatim_block_end);
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000462 T.setVerbatimBlockID(Traits.getCommandInfo(Name)->getID());
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000463 State = LS_Normal;
464 return;
465 } else {
466 // There is some text, followed by end command. Extract text first.
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000467 TextEnd = BufferPtr + Pos;
468 NextLine = TextEnd;
Dmitri Gribenko64da4e52012-07-18 23:01:58 +0000469 // If there is only whitespace before end command, skip whitespace.
470 if (isWhitespace(BufferPtr, TextEnd)) {
471 BufferPtr = TextEnd;
472 goto again;
473 }
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000474 }
475
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000476 StringRef Text(BufferPtr, TextEnd - BufferPtr);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000477 formTokenWithChars(T, NextLine, tok::verbatim_block_line);
Dmitri Gribenkof5e0aea2012-06-27 16:30:35 +0000478 T.setVerbatimBlockText(Text);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000479
480 State = LS_VerbatimBlockBody;
481}
482
483void Lexer::lexVerbatimBlockBody(Token &T) {
484 assert(State == LS_VerbatimBlockBody);
485
486 if (CommentState == LCS_InsideCComment)
487 skipLineStartingDecorations();
488
489 lexVerbatimBlockFirstLine(T);
490}
491
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000492void Lexer::setupAndLexVerbatimLine(Token &T, const char *TextBegin,
493 const CommandInfo *Info) {
494 assert(Info->IsVerbatimLineCommand);
Dmitri Gribenko962668d2012-06-27 16:53:58 +0000495 formTokenWithChars(T, TextBegin, tok::verbatim_line_name);
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000496 T.setVerbatimLineID(Info->getID());
Dmitri Gribenko962668d2012-06-27 16:53:58 +0000497
498 State = LS_VerbatimLineText;
499}
500
501void Lexer::lexVerbatimLineText(Token &T) {
502 assert(State == LS_VerbatimLineText);
503
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000504 // Extract current line.
505 const char *Newline = findNewline(BufferPtr, CommentEnd);
Dmitri Gribenko962668d2012-06-27 16:53:58 +0000506 const StringRef Text(BufferPtr, Newline - BufferPtr);
507 formTokenWithChars(T, Newline, tok::verbatim_line_text);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000508 T.setVerbatimLineText(Text);
Dmitri Gribenko962668d2012-06-27 16:53:58 +0000509
510 State = LS_Normal;
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000511}
512
Dmitri Gribenko477a9f52012-07-27 20:37:06 +0000513void Lexer::lexHTMLCharacterReference(Token &T) {
514 const char *TokenPtr = BufferPtr;
515 assert(*TokenPtr == '&');
516 TokenPtr++;
517 if (TokenPtr == CommentEnd) {
518 formTextToken(T, TokenPtr);
519 return;
520 }
521 const char *NamePtr;
522 bool isNamed = false;
523 bool isDecimal = false;
524 char C = *TokenPtr;
525 if (isHTMLNamedCharacterReferenceCharacter(C)) {
526 NamePtr = TokenPtr;
527 TokenPtr = skipNamedCharacterReference(TokenPtr, CommentEnd);
528 isNamed = true;
529 } else if (C == '#') {
530 TokenPtr++;
531 if (TokenPtr == CommentEnd) {
532 formTextToken(T, TokenPtr);
533 return;
534 }
535 C = *TokenPtr;
536 if (isHTMLDecimalCharacterReferenceCharacter(C)) {
537 NamePtr = TokenPtr;
538 TokenPtr = skipDecimalCharacterReference(TokenPtr, CommentEnd);
539 isDecimal = true;
540 } else if (C == 'x' || C == 'X') {
541 TokenPtr++;
542 NamePtr = TokenPtr;
543 TokenPtr = skipHexCharacterReference(TokenPtr, CommentEnd);
544 } else {
545 formTextToken(T, TokenPtr);
546 return;
547 }
548 } else {
549 formTextToken(T, TokenPtr);
550 return;
551 }
552 if (NamePtr == TokenPtr || TokenPtr == CommentEnd ||
553 *TokenPtr != ';') {
554 formTextToken(T, TokenPtr);
555 return;
556 }
557 StringRef Name(NamePtr, TokenPtr - NamePtr);
558 TokenPtr++; // Skip semicolon.
559 StringRef Resolved;
Dmitri Gribenko5bd1e5b2013-01-30 14:29:28 +0000560 if (isNamed)
Dmitri Gribenko477a9f52012-07-27 20:37:06 +0000561 Resolved = resolveHTMLNamedCharacterReference(Name);
562 else if (isDecimal)
563 Resolved = resolveHTMLDecimalCharacterReference(Name);
564 else
565 Resolved = resolveHTMLHexCharacterReference(Name);
566
567 if (Resolved.empty()) {
568 formTextToken(T, TokenPtr);
569 return;
570 }
571 formTokenWithChars(T, TokenPtr, tok::text);
572 T.setText(Resolved);
573 return;
574}
575
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000576void Lexer::setupAndLexHTMLStartTag(Token &T) {
Dmitri Gribenkoa99ec102012-07-09 21:32:40 +0000577 assert(BufferPtr[0] == '<' &&
578 isHTMLIdentifierStartingCharacter(BufferPtr[1]));
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000579 const char *TagNameEnd = skipHTMLIdentifier(BufferPtr + 2, CommentEnd);
Dmitri Gribenkof5e0aea2012-06-27 16:30:35 +0000580 StringRef Name(BufferPtr + 1, TagNameEnd - (BufferPtr + 1));
Dmitri Gribenko834a5bd2012-08-22 22:56:08 +0000581 if (!isHTMLTagName(Name)) {
582 formTextToken(T, TagNameEnd);
583 return;
584 }
585
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000586 formTokenWithChars(T, TagNameEnd, tok::html_start_tag);
587 T.setHTMLTagStartName(Name);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000588
589 BufferPtr = skipWhitespace(BufferPtr, CommentEnd);
590
Dmitri Gribenkoa99ec102012-07-09 21:32:40 +0000591 const char C = *BufferPtr;
592 if (BufferPtr != CommentEnd &&
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000593 (C == '>' || C == '/' || isHTMLIdentifierStartingCharacter(C)))
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000594 State = LS_HTMLStartTag;
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000595}
596
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000597void Lexer::lexHTMLStartTag(Token &T) {
598 assert(State == LS_HTMLStartTag);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000599
600 const char *TokenPtr = BufferPtr;
601 char C = *TokenPtr;
602 if (isHTMLIdentifierCharacter(C)) {
603 TokenPtr = skipHTMLIdentifier(TokenPtr, CommentEnd);
Dmitri Gribenkof5e0aea2012-06-27 16:30:35 +0000604 StringRef Ident(BufferPtr, TokenPtr - BufferPtr);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000605 formTokenWithChars(T, TokenPtr, tok::html_ident);
Dmitri Gribenkof5e0aea2012-06-27 16:30:35 +0000606 T.setHTMLIdent(Ident);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000607 } else {
608 switch (C) {
609 case '=':
610 TokenPtr++;
611 formTokenWithChars(T, TokenPtr, tok::html_equals);
612 break;
613 case '\"':
614 case '\'': {
615 const char *OpenQuote = TokenPtr;
616 TokenPtr = skipHTMLQuotedString(TokenPtr, CommentEnd);
617 const char *ClosingQuote = TokenPtr;
618 if (TokenPtr != CommentEnd) // Skip closing quote.
619 TokenPtr++;
620 formTokenWithChars(T, TokenPtr, tok::html_quoted_string);
621 T.setHTMLQuotedString(StringRef(OpenQuote + 1,
622 ClosingQuote - (OpenQuote + 1)));
623 break;
624 }
625 case '>':
626 TokenPtr++;
627 formTokenWithChars(T, TokenPtr, tok::html_greater);
Dmitri Gribenkoa99ec102012-07-09 21:32:40 +0000628 State = LS_Normal;
629 return;
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000630 case '/':
631 TokenPtr++;
632 if (TokenPtr != CommentEnd && *TokenPtr == '>') {
633 TokenPtr++;
634 formTokenWithChars(T, TokenPtr, tok::html_slash_greater);
Dmitri Gribenko477a9f52012-07-27 20:37:06 +0000635 } else
636 formTextToken(T, TokenPtr);
637
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000638 State = LS_Normal;
639 return;
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000640 }
641 }
642
643 // Now look ahead and return to normal state if we don't see any HTML tokens
644 // ahead.
645 BufferPtr = skipWhitespace(BufferPtr, CommentEnd);
646 if (BufferPtr == CommentEnd) {
647 State = LS_Normal;
648 return;
649 }
650
651 C = *BufferPtr;
Dmitri Gribenkoa99ec102012-07-09 21:32:40 +0000652 if (!isHTMLIdentifierStartingCharacter(C) &&
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000653 C != '=' && C != '\"' && C != '\'' && C != '>') {
654 State = LS_Normal;
655 return;
656 }
657}
658
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000659void Lexer::setupAndLexHTMLEndTag(Token &T) {
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000660 assert(BufferPtr[0] == '<' && BufferPtr[1] == '/');
661
662 const char *TagNameBegin = skipWhitespace(BufferPtr + 2, CommentEnd);
663 const char *TagNameEnd = skipHTMLIdentifier(TagNameBegin, CommentEnd);
Dmitri Gribenko834a5bd2012-08-22 22:56:08 +0000664 StringRef Name(TagNameBegin, TagNameEnd - TagNameBegin);
665 if (!isHTMLTagName(Name)) {
666 formTextToken(T, TagNameEnd);
667 return;
668 }
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000669
670 const char *End = skipWhitespace(TagNameEnd, CommentEnd);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000671
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000672 formTokenWithChars(T, End, tok::html_end_tag);
Dmitri Gribenko834a5bd2012-08-22 22:56:08 +0000673 T.setHTMLTagEndName(Name);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000674
675 if (BufferPtr != CommentEnd && *BufferPtr == '>')
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000676 State = LS_HTMLEndTag;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000677}
678
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000679void Lexer::lexHTMLEndTag(Token &T) {
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000680 assert(BufferPtr != CommentEnd && *BufferPtr == '>');
681
682 formTokenWithChars(T, BufferPtr + 1, tok::html_greater);
683 State = LS_Normal;
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000684}
685
Dmitri Gribenkoaa580812012-08-09 00:03:17 +0000686Lexer::Lexer(llvm::BumpPtrAllocator &Allocator, const CommandTraits &Traits,
Dmitri Gribenkoaf503a62012-08-31 10:35:30 +0000687 SourceLocation FileLoc,
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000688 const char *BufferStart, const char *BufferEnd):
Dmitri Gribenkoaa580812012-08-09 00:03:17 +0000689 Allocator(Allocator), Traits(Traits),
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000690 BufferStart(BufferStart), BufferEnd(BufferEnd),
Dmitri Gribenkoaf503a62012-08-31 10:35:30 +0000691 FileLoc(FileLoc), BufferPtr(BufferStart),
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000692 CommentState(LCS_BeforeComment), State(LS_Normal) {
693}
694
695void Lexer::lex(Token &T) {
696again:
697 switch (CommentState) {
698 case LCS_BeforeComment:
699 if (BufferPtr == BufferEnd) {
700 formTokenWithChars(T, BufferPtr, tok::eof);
701 return;
702 }
703
704 assert(*BufferPtr == '/');
705 BufferPtr++; // Skip first slash.
706 switch(*BufferPtr) {
707 case '/': { // BCPL comment.
708 BufferPtr++; // Skip second slash.
709
710 if (BufferPtr != BufferEnd) {
711 // Skip Doxygen magic marker, if it is present.
712 // It might be missing because of a typo //< or /*<, or because we
713 // merged this non-Doxygen comment into a bunch of Doxygen comments
714 // around it: /** ... */ /* ... */ /** ... */
715 const char C = *BufferPtr;
716 if (C == '/' || C == '!')
717 BufferPtr++;
718 }
719
720 // Skip less-than symbol that marks trailing comments.
721 // Skip it even if the comment is not a Doxygen one, because //< and /*<
722 // are frequent typos.
723 if (BufferPtr != BufferEnd && *BufferPtr == '<')
724 BufferPtr++;
725
726 CommentState = LCS_InsideBCPLComment;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000727 if (State != LS_VerbatimBlockBody && State != LS_VerbatimBlockFirstLine)
728 State = LS_Normal;
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000729 CommentEnd = findBCPLCommentEnd(BufferPtr, BufferEnd);
730 goto again;
731 }
732 case '*': { // C comment.
733 BufferPtr++; // Skip star.
734
735 // Skip Doxygen magic marker.
736 const char C = *BufferPtr;
737 if ((C == '*' && *(BufferPtr + 1) != '/') || C == '!')
738 BufferPtr++;
739
740 // Skip less-than symbol that marks trailing comments.
741 if (BufferPtr != BufferEnd && *BufferPtr == '<')
742 BufferPtr++;
743
744 CommentState = LCS_InsideCComment;
745 State = LS_Normal;
746 CommentEnd = findCCommentEnd(BufferPtr, BufferEnd);
747 goto again;
748 }
749 default:
750 llvm_unreachable("second character of comment should be '/' or '*'");
751 }
752
753 case LCS_BetweenComments: {
754 // Consecutive comments are extracted only if there is only whitespace
755 // between them. So we can search for the start of the next comment.
756 const char *EndWhitespace = BufferPtr;
757 while(EndWhitespace != BufferEnd && *EndWhitespace != '/')
758 EndWhitespace++;
759
760 // Turn any whitespace between comments (and there is only whitespace
Dmitri Gribenkoa99ec102012-07-09 21:32:40 +0000761 // between them -- guaranteed by comment extraction) into a newline. We
762 // have two newlines between C comments in total (first one was synthesized
763 // after a comment).
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000764 formTokenWithChars(T, EndWhitespace, tok::newline);
765
766 CommentState = LCS_BeforeComment;
767 break;
768 }
769
770 case LCS_InsideBCPLComment:
771 case LCS_InsideCComment:
772 if (BufferPtr != CommentEnd) {
773 lexCommentText(T);
774 break;
775 } else {
776 // Skip C comment closing sequence.
777 if (CommentState == LCS_InsideCComment) {
778 assert(BufferPtr[0] == '*' && BufferPtr[1] == '/');
779 BufferPtr += 2;
780 assert(BufferPtr <= BufferEnd);
781
782 // Synthenize newline just after the C comment, regardless if there is
783 // actually a newline.
784 formTokenWithChars(T, BufferPtr, tok::newline);
785
786 CommentState = LCS_BetweenComments;
787 break;
788 } else {
789 // Don't synthesized a newline after BCPL comment.
790 CommentState = LCS_BetweenComments;
791 goto again;
792 }
793 }
794 }
795}
796
797StringRef Lexer::getSpelling(const Token &Tok,
798 const SourceManager &SourceMgr,
799 bool *Invalid) const {
800 SourceLocation Loc = Tok.getLocation();
801 std::pair<FileID, unsigned> LocInfo = SourceMgr.getDecomposedLoc(Loc);
802
803 bool InvalidTemp = false;
804 StringRef File = SourceMgr.getBufferData(LocInfo.first, &InvalidTemp);
805 if (InvalidTemp) {
806 *Invalid = true;
807 return StringRef();
808 }
809
810 const char *Begin = File.data() + LocInfo.second;
811 return StringRef(Begin, Tok.getLength());
812}
813
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000814} // end namespace comments
815} // end namespace clang
816