blob: da865d2ee365f5367f8aeccc364097c0960704f7 [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 Jahanianc98e9132013-03-01 22:51:30 +0000301 T.HDCommand = (*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 }
362 formTokenWithChars(T, TokenPtr, tok::command);
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000363 T.setCommandID(Info->getID());
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000364 return;
365 }
366
Dmitri Gribenko477a9f52012-07-27 20:37:06 +0000367 case '&':
368 lexHTMLCharacterReference(T);
369 return;
370
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000371 case '<': {
372 TokenPtr++;
373 if (TokenPtr == CommentEnd) {
Dmitri Gribenko477a9f52012-07-27 20:37:06 +0000374 formTextToken(T, TokenPtr);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000375 return;
376 }
377 const char C = *TokenPtr;
Dmitri Gribenkoa99ec102012-07-09 21:32:40 +0000378 if (isHTMLIdentifierStartingCharacter(C))
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000379 setupAndLexHTMLStartTag(T);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000380 else if (C == '/')
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000381 setupAndLexHTMLEndTag(T);
Dmitri Gribenko477a9f52012-07-27 20:37:06 +0000382 else
383 formTextToken(T, TokenPtr);
384
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000385 return;
386 }
387
388 case '\n':
389 case '\r':
390 TokenPtr = skipNewline(TokenPtr, CommentEnd);
391 formTokenWithChars(T, TokenPtr, tok::newline);
392
393 if (CommentState == LCS_InsideCComment)
394 skipLineStartingDecorations();
395 return;
396
397 default: {
Dmitri Gribenkoaa7dbaf2012-12-30 19:45:46 +0000398 size_t End = StringRef(TokenPtr, CommentEnd - TokenPtr).
399 find_first_of("\n\r\\@&<");
400 if (End != StringRef::npos)
401 TokenPtr += End;
402 else
403 TokenPtr = CommentEnd;
Dmitri Gribenko477a9f52012-07-27 20:37:06 +0000404 formTextToken(T, TokenPtr);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000405 return;
406 }
407 }
408 }
409}
410
411void Lexer::setupAndLexVerbatimBlock(Token &T,
412 const char *TextBegin,
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000413 char Marker, const CommandInfo *Info) {
414 assert(Info->IsVerbatimBlockCommand);
415
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000416 VerbatimBlockEndCommandName.clear();
417 VerbatimBlockEndCommandName.append(Marker == '\\' ? "\\" : "@");
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000418 VerbatimBlockEndCommandName.append(Info->EndCommandName);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000419
420 formTokenWithChars(T, TextBegin, tok::verbatim_block_begin);
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000421 T.setVerbatimBlockID(Info->getID());
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000422
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000423 // If there is a newline following the verbatim opening command, skip the
424 // newline so that we don't create an tok::verbatim_block_line with empty
425 // text content.
Dmitri Gribenkobf881442013-02-09 15:16:58 +0000426 if (BufferPtr != CommentEnd &&
427 isVerticalWhitespace(*BufferPtr)) {
428 BufferPtr = skipNewline(BufferPtr, CommentEnd);
429 State = LS_VerbatimBlockBody;
430 return;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000431 }
432
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000433 State = LS_VerbatimBlockFirstLine;
434}
435
436void Lexer::lexVerbatimBlockFirstLine(Token &T) {
Dmitri Gribenko64da4e52012-07-18 23:01:58 +0000437again:
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000438 assert(BufferPtr < CommentEnd);
439
440 // FIXME: It would be better to scan the text once, finding either the block
441 // end command or newline.
442 //
443 // Extract current line.
444 const char *Newline = findNewline(BufferPtr, CommentEnd);
445 StringRef Line(BufferPtr, Newline - BufferPtr);
446
447 // Look for end command in current line.
448 size_t Pos = Line.find(VerbatimBlockEndCommandName);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000449 const char *TextEnd;
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000450 const char *NextLine;
451 if (Pos == StringRef::npos) {
452 // Current line is completely verbatim.
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000453 TextEnd = Newline;
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000454 NextLine = skipNewline(Newline, CommentEnd);
455 } else if (Pos == 0) {
456 // Current line contains just an end command.
457 const char *End = BufferPtr + VerbatimBlockEndCommandName.size();
Dmitri Gribenkof5e0aea2012-06-27 16:30:35 +0000458 StringRef Name(BufferPtr + 1, End - (BufferPtr + 1));
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000459 formTokenWithChars(T, End, tok::verbatim_block_end);
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000460 T.setVerbatimBlockID(Traits.getCommandInfo(Name)->getID());
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000461 State = LS_Normal;
462 return;
463 } else {
464 // There is some text, followed by end command. Extract text first.
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000465 TextEnd = BufferPtr + Pos;
466 NextLine = TextEnd;
Dmitri Gribenko64da4e52012-07-18 23:01:58 +0000467 // If there is only whitespace before end command, skip whitespace.
468 if (isWhitespace(BufferPtr, TextEnd)) {
469 BufferPtr = TextEnd;
470 goto again;
471 }
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000472 }
473
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000474 StringRef Text(BufferPtr, TextEnd - BufferPtr);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000475 formTokenWithChars(T, NextLine, tok::verbatim_block_line);
Dmitri Gribenkof5e0aea2012-06-27 16:30:35 +0000476 T.setVerbatimBlockText(Text);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000477
478 State = LS_VerbatimBlockBody;
479}
480
481void Lexer::lexVerbatimBlockBody(Token &T) {
482 assert(State == LS_VerbatimBlockBody);
483
484 if (CommentState == LCS_InsideCComment)
485 skipLineStartingDecorations();
486
487 lexVerbatimBlockFirstLine(T);
488}
489
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000490void Lexer::setupAndLexVerbatimLine(Token &T, const char *TextBegin,
491 const CommandInfo *Info) {
492 assert(Info->IsVerbatimLineCommand);
Dmitri Gribenko962668d2012-06-27 16:53:58 +0000493 formTokenWithChars(T, TextBegin, tok::verbatim_line_name);
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000494 T.setVerbatimLineID(Info->getID());
Dmitri Gribenko962668d2012-06-27 16:53:58 +0000495
496 State = LS_VerbatimLineText;
497}
498
499void Lexer::lexVerbatimLineText(Token &T) {
500 assert(State == LS_VerbatimLineText);
501
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000502 // Extract current line.
503 const char *Newline = findNewline(BufferPtr, CommentEnd);
Dmitri Gribenko962668d2012-06-27 16:53:58 +0000504 const StringRef Text(BufferPtr, Newline - BufferPtr);
505 formTokenWithChars(T, Newline, tok::verbatim_line_text);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000506 T.setVerbatimLineText(Text);
Dmitri Gribenko962668d2012-06-27 16:53:58 +0000507
508 State = LS_Normal;
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000509}
510
Dmitri Gribenko477a9f52012-07-27 20:37:06 +0000511void Lexer::lexHTMLCharacterReference(Token &T) {
512 const char *TokenPtr = BufferPtr;
513 assert(*TokenPtr == '&');
514 TokenPtr++;
515 if (TokenPtr == CommentEnd) {
516 formTextToken(T, TokenPtr);
517 return;
518 }
519 const char *NamePtr;
520 bool isNamed = false;
521 bool isDecimal = false;
522 char C = *TokenPtr;
523 if (isHTMLNamedCharacterReferenceCharacter(C)) {
524 NamePtr = TokenPtr;
525 TokenPtr = skipNamedCharacterReference(TokenPtr, CommentEnd);
526 isNamed = true;
527 } else if (C == '#') {
528 TokenPtr++;
529 if (TokenPtr == CommentEnd) {
530 formTextToken(T, TokenPtr);
531 return;
532 }
533 C = *TokenPtr;
534 if (isHTMLDecimalCharacterReferenceCharacter(C)) {
535 NamePtr = TokenPtr;
536 TokenPtr = skipDecimalCharacterReference(TokenPtr, CommentEnd);
537 isDecimal = true;
538 } else if (C == 'x' || C == 'X') {
539 TokenPtr++;
540 NamePtr = TokenPtr;
541 TokenPtr = skipHexCharacterReference(TokenPtr, CommentEnd);
542 } else {
543 formTextToken(T, TokenPtr);
544 return;
545 }
546 } else {
547 formTextToken(T, TokenPtr);
548 return;
549 }
550 if (NamePtr == TokenPtr || TokenPtr == CommentEnd ||
551 *TokenPtr != ';') {
552 formTextToken(T, TokenPtr);
553 return;
554 }
555 StringRef Name(NamePtr, TokenPtr - NamePtr);
556 TokenPtr++; // Skip semicolon.
557 StringRef Resolved;
Dmitri Gribenko5bd1e5b2013-01-30 14:29:28 +0000558 if (isNamed)
Dmitri Gribenko477a9f52012-07-27 20:37:06 +0000559 Resolved = resolveHTMLNamedCharacterReference(Name);
560 else if (isDecimal)
561 Resolved = resolveHTMLDecimalCharacterReference(Name);
562 else
563 Resolved = resolveHTMLHexCharacterReference(Name);
564
565 if (Resolved.empty()) {
566 formTextToken(T, TokenPtr);
567 return;
568 }
569 formTokenWithChars(T, TokenPtr, tok::text);
570 T.setText(Resolved);
571 return;
572}
573
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000574void Lexer::setupAndLexHTMLStartTag(Token &T) {
Dmitri Gribenkoa99ec102012-07-09 21:32:40 +0000575 assert(BufferPtr[0] == '<' &&
576 isHTMLIdentifierStartingCharacter(BufferPtr[1]));
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000577 const char *TagNameEnd = skipHTMLIdentifier(BufferPtr + 2, CommentEnd);
Dmitri Gribenkof5e0aea2012-06-27 16:30:35 +0000578 StringRef Name(BufferPtr + 1, TagNameEnd - (BufferPtr + 1));
Dmitri Gribenko834a5bd2012-08-22 22:56:08 +0000579 if (!isHTMLTagName(Name)) {
580 formTextToken(T, TagNameEnd);
581 return;
582 }
583
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000584 formTokenWithChars(T, TagNameEnd, tok::html_start_tag);
585 T.setHTMLTagStartName(Name);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000586
587 BufferPtr = skipWhitespace(BufferPtr, CommentEnd);
588
Dmitri Gribenkoa99ec102012-07-09 21:32:40 +0000589 const char C = *BufferPtr;
590 if (BufferPtr != CommentEnd &&
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000591 (C == '>' || C == '/' || isHTMLIdentifierStartingCharacter(C)))
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000592 State = LS_HTMLStartTag;
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000593}
594
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000595void Lexer::lexHTMLStartTag(Token &T) {
596 assert(State == LS_HTMLStartTag);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000597
598 const char *TokenPtr = BufferPtr;
599 char C = *TokenPtr;
600 if (isHTMLIdentifierCharacter(C)) {
601 TokenPtr = skipHTMLIdentifier(TokenPtr, CommentEnd);
Dmitri Gribenkof5e0aea2012-06-27 16:30:35 +0000602 StringRef Ident(BufferPtr, TokenPtr - BufferPtr);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000603 formTokenWithChars(T, TokenPtr, tok::html_ident);
Dmitri Gribenkof5e0aea2012-06-27 16:30:35 +0000604 T.setHTMLIdent(Ident);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000605 } else {
606 switch (C) {
607 case '=':
608 TokenPtr++;
609 formTokenWithChars(T, TokenPtr, tok::html_equals);
610 break;
611 case '\"':
612 case '\'': {
613 const char *OpenQuote = TokenPtr;
614 TokenPtr = skipHTMLQuotedString(TokenPtr, CommentEnd);
615 const char *ClosingQuote = TokenPtr;
616 if (TokenPtr != CommentEnd) // Skip closing quote.
617 TokenPtr++;
618 formTokenWithChars(T, TokenPtr, tok::html_quoted_string);
619 T.setHTMLQuotedString(StringRef(OpenQuote + 1,
620 ClosingQuote - (OpenQuote + 1)));
621 break;
622 }
623 case '>':
624 TokenPtr++;
625 formTokenWithChars(T, TokenPtr, tok::html_greater);
Dmitri Gribenkoa99ec102012-07-09 21:32:40 +0000626 State = LS_Normal;
627 return;
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000628 case '/':
629 TokenPtr++;
630 if (TokenPtr != CommentEnd && *TokenPtr == '>') {
631 TokenPtr++;
632 formTokenWithChars(T, TokenPtr, tok::html_slash_greater);
Dmitri Gribenko477a9f52012-07-27 20:37:06 +0000633 } else
634 formTextToken(T, TokenPtr);
635
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000636 State = LS_Normal;
637 return;
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000638 }
639 }
640
641 // Now look ahead and return to normal state if we don't see any HTML tokens
642 // ahead.
643 BufferPtr = skipWhitespace(BufferPtr, CommentEnd);
644 if (BufferPtr == CommentEnd) {
645 State = LS_Normal;
646 return;
647 }
648
649 C = *BufferPtr;
Dmitri Gribenkoa99ec102012-07-09 21:32:40 +0000650 if (!isHTMLIdentifierStartingCharacter(C) &&
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000651 C != '=' && C != '\"' && C != '\'' && C != '>') {
652 State = LS_Normal;
653 return;
654 }
655}
656
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000657void Lexer::setupAndLexHTMLEndTag(Token &T) {
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000658 assert(BufferPtr[0] == '<' && BufferPtr[1] == '/');
659
660 const char *TagNameBegin = skipWhitespace(BufferPtr + 2, CommentEnd);
661 const char *TagNameEnd = skipHTMLIdentifier(TagNameBegin, CommentEnd);
Dmitri Gribenko834a5bd2012-08-22 22:56:08 +0000662 StringRef Name(TagNameBegin, TagNameEnd - TagNameBegin);
663 if (!isHTMLTagName(Name)) {
664 formTextToken(T, TagNameEnd);
665 return;
666 }
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000667
668 const char *End = skipWhitespace(TagNameEnd, CommentEnd);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000669
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000670 formTokenWithChars(T, End, tok::html_end_tag);
Dmitri Gribenko834a5bd2012-08-22 22:56:08 +0000671 T.setHTMLTagEndName(Name);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000672
673 if (BufferPtr != CommentEnd && *BufferPtr == '>')
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000674 State = LS_HTMLEndTag;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000675}
676
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000677void Lexer::lexHTMLEndTag(Token &T) {
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000678 assert(BufferPtr != CommentEnd && *BufferPtr == '>');
679
680 formTokenWithChars(T, BufferPtr + 1, tok::html_greater);
681 State = LS_Normal;
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000682}
683
Dmitri Gribenkoaa580812012-08-09 00:03:17 +0000684Lexer::Lexer(llvm::BumpPtrAllocator &Allocator, const CommandTraits &Traits,
Dmitri Gribenkoaf503a62012-08-31 10:35:30 +0000685 SourceLocation FileLoc,
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000686 const char *BufferStart, const char *BufferEnd):
Dmitri Gribenkoaa580812012-08-09 00:03:17 +0000687 Allocator(Allocator), Traits(Traits),
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000688 BufferStart(BufferStart), BufferEnd(BufferEnd),
Dmitri Gribenkoaf503a62012-08-31 10:35:30 +0000689 FileLoc(FileLoc), BufferPtr(BufferStart),
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000690 CommentState(LCS_BeforeComment), State(LS_Normal) {
691}
692
693void Lexer::lex(Token &T) {
694again:
695 switch (CommentState) {
696 case LCS_BeforeComment:
697 if (BufferPtr == BufferEnd) {
698 formTokenWithChars(T, BufferPtr, tok::eof);
699 return;
700 }
701
702 assert(*BufferPtr == '/');
703 BufferPtr++; // Skip first slash.
704 switch(*BufferPtr) {
705 case '/': { // BCPL comment.
706 BufferPtr++; // Skip second slash.
707
708 if (BufferPtr != BufferEnd) {
709 // Skip Doxygen magic marker, if it is present.
710 // It might be missing because of a typo //< or /*<, or because we
711 // merged this non-Doxygen comment into a bunch of Doxygen comments
712 // around it: /** ... */ /* ... */ /** ... */
713 const char C = *BufferPtr;
714 if (C == '/' || C == '!')
715 BufferPtr++;
716 }
717
718 // Skip less-than symbol that marks trailing comments.
719 // Skip it even if the comment is not a Doxygen one, because //< and /*<
720 // are frequent typos.
721 if (BufferPtr != BufferEnd && *BufferPtr == '<')
722 BufferPtr++;
723
724 CommentState = LCS_InsideBCPLComment;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000725 if (State != LS_VerbatimBlockBody && State != LS_VerbatimBlockFirstLine)
726 State = LS_Normal;
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000727 CommentEnd = findBCPLCommentEnd(BufferPtr, BufferEnd);
728 goto again;
729 }
730 case '*': { // C comment.
731 BufferPtr++; // Skip star.
732
733 // Skip Doxygen magic marker.
734 const char C = *BufferPtr;
735 if ((C == '*' && *(BufferPtr + 1) != '/') || C == '!')
736 BufferPtr++;
737
738 // Skip less-than symbol that marks trailing comments.
739 if (BufferPtr != BufferEnd && *BufferPtr == '<')
740 BufferPtr++;
741
742 CommentState = LCS_InsideCComment;
743 State = LS_Normal;
744 CommentEnd = findCCommentEnd(BufferPtr, BufferEnd);
745 goto again;
746 }
747 default:
748 llvm_unreachable("second character of comment should be '/' or '*'");
749 }
750
751 case LCS_BetweenComments: {
752 // Consecutive comments are extracted only if there is only whitespace
753 // between them. So we can search for the start of the next comment.
754 const char *EndWhitespace = BufferPtr;
755 while(EndWhitespace != BufferEnd && *EndWhitespace != '/')
756 EndWhitespace++;
757
758 // Turn any whitespace between comments (and there is only whitespace
Dmitri Gribenkoa99ec102012-07-09 21:32:40 +0000759 // between them -- guaranteed by comment extraction) into a newline. We
760 // have two newlines between C comments in total (first one was synthesized
761 // after a comment).
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000762 formTokenWithChars(T, EndWhitespace, tok::newline);
763
764 CommentState = LCS_BeforeComment;
765 break;
766 }
767
768 case LCS_InsideBCPLComment:
769 case LCS_InsideCComment:
770 if (BufferPtr != CommentEnd) {
771 lexCommentText(T);
772 break;
773 } else {
774 // Skip C comment closing sequence.
775 if (CommentState == LCS_InsideCComment) {
776 assert(BufferPtr[0] == '*' && BufferPtr[1] == '/');
777 BufferPtr += 2;
778 assert(BufferPtr <= BufferEnd);
779
780 // Synthenize newline just after the C comment, regardless if there is
781 // actually a newline.
782 formTokenWithChars(T, BufferPtr, tok::newline);
783
784 CommentState = LCS_BetweenComments;
785 break;
786 } else {
787 // Don't synthesized a newline after BCPL comment.
788 CommentState = LCS_BetweenComments;
789 goto again;
790 }
791 }
792 }
793}
794
795StringRef Lexer::getSpelling(const Token &Tok,
796 const SourceManager &SourceMgr,
797 bool *Invalid) const {
798 SourceLocation Loc = Tok.getLocation();
799 std::pair<FileID, unsigned> LocInfo = SourceMgr.getDecomposedLoc(Loc);
800
801 bool InvalidTemp = false;
802 StringRef File = SourceMgr.getBufferData(LocInfo.first, &InvalidTemp);
803 if (InvalidTemp) {
804 *Invalid = true;
805 return StringRef();
806 }
807
808 const char *Begin = File.data() + LocInfo.second;
809 return StringRef(Begin, Tok.getLength());
810}
811
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000812} // end namespace comments
813} // end namespace clang
814