blob: 1194520bf3606a45d1e9d4eb2ecfef316bbe7149 [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 '@': {
Dmitri Gribenko808383d2013-03-04 23:06:15 +0000301 // Commands that start with a backslash and commands that start with
302 // 'at' have equivalent semantics. But we keep information about the
303 // exact syntax in AST for comments.
304 tok::TokenKind CommandKind =
305 (*TokenPtr == '@') ? tok::at_command : tok::backslash_command;
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000306 TokenPtr++;
307 if (TokenPtr == CommentEnd) {
Dmitri Gribenko477a9f52012-07-27 20:37:06 +0000308 formTextToken(T, TokenPtr);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000309 return;
310 }
311 char C = *TokenPtr;
312 switch (C) {
313 default:
314 break;
315
316 case '\\': case '@': case '&': case '$':
317 case '#': case '<': case '>': case '%':
318 case '\"': case '.': case ':':
319 // This is one of \\ \@ \& \$ etc escape sequences.
320 TokenPtr++;
321 if (C == ':' && TokenPtr != CommentEnd && *TokenPtr == ':') {
322 // This is the \:: escape sequence.
323 TokenPtr++;
324 }
Dmitri Gribenkof5e0aea2012-06-27 16:30:35 +0000325 StringRef UnescapedText(BufferPtr + 1, TokenPtr - (BufferPtr + 1));
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000326 formTokenWithChars(T, TokenPtr, tok::text);
Dmitri Gribenkof5e0aea2012-06-27 16:30:35 +0000327 T.setText(UnescapedText);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000328 return;
329 }
330
331 // Don't make zero-length commands.
Dmitri Gribenko8c05da32012-09-14 16:35:35 +0000332 if (!isCommandNameStartCharacter(*TokenPtr)) {
Dmitri Gribenko477a9f52012-07-27 20:37:06 +0000333 formTextToken(T, TokenPtr);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000334 return;
335 }
336
337 TokenPtr = skipCommandName(TokenPtr, CommentEnd);
338 unsigned Length = TokenPtr - (BufferPtr + 1);
339
340 // Hardcoded support for lexing LaTeX formula commands
341 // \f$ \f[ \f] \f{ \f} as a single command.
342 if (Length == 1 && TokenPtr[-1] == 'f' && TokenPtr != CommentEnd) {
343 C = *TokenPtr;
344 if (C == '$' || C == '[' || C == ']' || C == '{' || C == '}') {
345 TokenPtr++;
346 Length++;
347 }
348 }
349
350 const StringRef CommandName(BufferPtr + 1, Length);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000351
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000352 const CommandInfo *Info = Traits.getCommandInfoOrNULL(CommandName);
353 if (!Info) {
354 formTokenWithChars(T, TokenPtr, tok::unknown_command);
355 T.setUnknownCommandName(CommandName);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000356 return;
357 }
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000358 if (Info->IsVerbatimBlockCommand) {
359 setupAndLexVerbatimBlock(T, TokenPtr, *BufferPtr, Info);
360 return;
361 }
362 if (Info->IsVerbatimLineCommand) {
363 setupAndLexVerbatimLine(T, TokenPtr, Info);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000364 return;
365 }
Dmitri Gribenko808383d2013-03-04 23:06:15 +0000366 formTokenWithChars(T, TokenPtr, CommandKind);
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000367 T.setCommandID(Info->getID());
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000368 return;
369 }
370
Dmitri Gribenko477a9f52012-07-27 20:37:06 +0000371 case '&':
372 lexHTMLCharacterReference(T);
373 return;
374
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000375 case '<': {
376 TokenPtr++;
377 if (TokenPtr == CommentEnd) {
Dmitri Gribenko477a9f52012-07-27 20:37:06 +0000378 formTextToken(T, TokenPtr);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000379 return;
380 }
381 const char C = *TokenPtr;
Dmitri Gribenkoa99ec102012-07-09 21:32:40 +0000382 if (isHTMLIdentifierStartingCharacter(C))
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000383 setupAndLexHTMLStartTag(T);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000384 else if (C == '/')
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000385 setupAndLexHTMLEndTag(T);
Dmitri Gribenko477a9f52012-07-27 20:37:06 +0000386 else
387 formTextToken(T, TokenPtr);
388
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000389 return;
390 }
391
392 case '\n':
393 case '\r':
394 TokenPtr = skipNewline(TokenPtr, CommentEnd);
395 formTokenWithChars(T, TokenPtr, tok::newline);
396
397 if (CommentState == LCS_InsideCComment)
398 skipLineStartingDecorations();
399 return;
400
401 default: {
Dmitri Gribenkoaa7dbaf2012-12-30 19:45:46 +0000402 size_t End = StringRef(TokenPtr, CommentEnd - TokenPtr).
403 find_first_of("\n\r\\@&<");
404 if (End != StringRef::npos)
405 TokenPtr += End;
406 else
407 TokenPtr = CommentEnd;
Dmitri Gribenko477a9f52012-07-27 20:37:06 +0000408 formTextToken(T, TokenPtr);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000409 return;
410 }
411 }
412 }
413}
414
415void Lexer::setupAndLexVerbatimBlock(Token &T,
416 const char *TextBegin,
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000417 char Marker, const CommandInfo *Info) {
418 assert(Info->IsVerbatimBlockCommand);
419
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000420 VerbatimBlockEndCommandName.clear();
421 VerbatimBlockEndCommandName.append(Marker == '\\' ? "\\" : "@");
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000422 VerbatimBlockEndCommandName.append(Info->EndCommandName);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000423
424 formTokenWithChars(T, TextBegin, tok::verbatim_block_begin);
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000425 T.setVerbatimBlockID(Info->getID());
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000426
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000427 // If there is a newline following the verbatim opening command, skip the
428 // newline so that we don't create an tok::verbatim_block_line with empty
429 // text content.
Dmitri Gribenkobf881442013-02-09 15:16:58 +0000430 if (BufferPtr != CommentEnd &&
431 isVerticalWhitespace(*BufferPtr)) {
432 BufferPtr = skipNewline(BufferPtr, CommentEnd);
433 State = LS_VerbatimBlockBody;
434 return;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000435 }
436
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000437 State = LS_VerbatimBlockFirstLine;
438}
439
440void Lexer::lexVerbatimBlockFirstLine(Token &T) {
Dmitri Gribenko64da4e52012-07-18 23:01:58 +0000441again:
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000442 assert(BufferPtr < CommentEnd);
443
444 // FIXME: It would be better to scan the text once, finding either the block
445 // end command or newline.
446 //
447 // Extract current line.
448 const char *Newline = findNewline(BufferPtr, CommentEnd);
449 StringRef Line(BufferPtr, Newline - BufferPtr);
450
451 // Look for end command in current line.
452 size_t Pos = Line.find(VerbatimBlockEndCommandName);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000453 const char *TextEnd;
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000454 const char *NextLine;
455 if (Pos == StringRef::npos) {
456 // Current line is completely verbatim.
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000457 TextEnd = Newline;
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000458 NextLine = skipNewline(Newline, CommentEnd);
459 } else if (Pos == 0) {
460 // Current line contains just an end command.
461 const char *End = BufferPtr + VerbatimBlockEndCommandName.size();
Dmitri Gribenkof5e0aea2012-06-27 16:30:35 +0000462 StringRef Name(BufferPtr + 1, End - (BufferPtr + 1));
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000463 formTokenWithChars(T, End, tok::verbatim_block_end);
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000464 T.setVerbatimBlockID(Traits.getCommandInfo(Name)->getID());
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000465 State = LS_Normal;
466 return;
467 } else {
468 // There is some text, followed by end command. Extract text first.
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000469 TextEnd = BufferPtr + Pos;
470 NextLine = TextEnd;
Dmitri Gribenko64da4e52012-07-18 23:01:58 +0000471 // If there is only whitespace before end command, skip whitespace.
472 if (isWhitespace(BufferPtr, TextEnd)) {
473 BufferPtr = TextEnd;
474 goto again;
475 }
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000476 }
477
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000478 StringRef Text(BufferPtr, TextEnd - BufferPtr);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000479 formTokenWithChars(T, NextLine, tok::verbatim_block_line);
Dmitri Gribenkof5e0aea2012-06-27 16:30:35 +0000480 T.setVerbatimBlockText(Text);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000481
482 State = LS_VerbatimBlockBody;
483}
484
485void Lexer::lexVerbatimBlockBody(Token &T) {
486 assert(State == LS_VerbatimBlockBody);
487
488 if (CommentState == LCS_InsideCComment)
489 skipLineStartingDecorations();
490
491 lexVerbatimBlockFirstLine(T);
492}
493
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000494void Lexer::setupAndLexVerbatimLine(Token &T, const char *TextBegin,
495 const CommandInfo *Info) {
496 assert(Info->IsVerbatimLineCommand);
Dmitri Gribenko962668d2012-06-27 16:53:58 +0000497 formTokenWithChars(T, TextBegin, tok::verbatim_line_name);
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000498 T.setVerbatimLineID(Info->getID());
Dmitri Gribenko962668d2012-06-27 16:53:58 +0000499
500 State = LS_VerbatimLineText;
501}
502
503void Lexer::lexVerbatimLineText(Token &T) {
504 assert(State == LS_VerbatimLineText);
505
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000506 // Extract current line.
507 const char *Newline = findNewline(BufferPtr, CommentEnd);
Dmitri Gribenko962668d2012-06-27 16:53:58 +0000508 const StringRef Text(BufferPtr, Newline - BufferPtr);
509 formTokenWithChars(T, Newline, tok::verbatim_line_text);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000510 T.setVerbatimLineText(Text);
Dmitri Gribenko962668d2012-06-27 16:53:58 +0000511
512 State = LS_Normal;
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000513}
514
Dmitri Gribenko477a9f52012-07-27 20:37:06 +0000515void Lexer::lexHTMLCharacterReference(Token &T) {
516 const char *TokenPtr = BufferPtr;
517 assert(*TokenPtr == '&');
518 TokenPtr++;
519 if (TokenPtr == CommentEnd) {
520 formTextToken(T, TokenPtr);
521 return;
522 }
523 const char *NamePtr;
524 bool isNamed = false;
525 bool isDecimal = false;
526 char C = *TokenPtr;
527 if (isHTMLNamedCharacterReferenceCharacter(C)) {
528 NamePtr = TokenPtr;
529 TokenPtr = skipNamedCharacterReference(TokenPtr, CommentEnd);
530 isNamed = true;
531 } else if (C == '#') {
532 TokenPtr++;
533 if (TokenPtr == CommentEnd) {
534 formTextToken(T, TokenPtr);
535 return;
536 }
537 C = *TokenPtr;
538 if (isHTMLDecimalCharacterReferenceCharacter(C)) {
539 NamePtr = TokenPtr;
540 TokenPtr = skipDecimalCharacterReference(TokenPtr, CommentEnd);
541 isDecimal = true;
542 } else if (C == 'x' || C == 'X') {
543 TokenPtr++;
544 NamePtr = TokenPtr;
545 TokenPtr = skipHexCharacterReference(TokenPtr, CommentEnd);
546 } else {
547 formTextToken(T, TokenPtr);
548 return;
549 }
550 } else {
551 formTextToken(T, TokenPtr);
552 return;
553 }
554 if (NamePtr == TokenPtr || TokenPtr == CommentEnd ||
555 *TokenPtr != ';') {
556 formTextToken(T, TokenPtr);
557 return;
558 }
559 StringRef Name(NamePtr, TokenPtr - NamePtr);
560 TokenPtr++; // Skip semicolon.
561 StringRef Resolved;
Dmitri Gribenko5bd1e5b2013-01-30 14:29:28 +0000562 if (isNamed)
Dmitri Gribenko477a9f52012-07-27 20:37:06 +0000563 Resolved = resolveHTMLNamedCharacterReference(Name);
564 else if (isDecimal)
565 Resolved = resolveHTMLDecimalCharacterReference(Name);
566 else
567 Resolved = resolveHTMLHexCharacterReference(Name);
568
569 if (Resolved.empty()) {
570 formTextToken(T, TokenPtr);
571 return;
572 }
573 formTokenWithChars(T, TokenPtr, tok::text);
574 T.setText(Resolved);
575 return;
576}
577
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000578void Lexer::setupAndLexHTMLStartTag(Token &T) {
Dmitri Gribenkoa99ec102012-07-09 21:32:40 +0000579 assert(BufferPtr[0] == '<' &&
580 isHTMLIdentifierStartingCharacter(BufferPtr[1]));
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000581 const char *TagNameEnd = skipHTMLIdentifier(BufferPtr + 2, CommentEnd);
Dmitri Gribenkof5e0aea2012-06-27 16:30:35 +0000582 StringRef Name(BufferPtr + 1, TagNameEnd - (BufferPtr + 1));
Dmitri Gribenko834a5bd2012-08-22 22:56:08 +0000583 if (!isHTMLTagName(Name)) {
584 formTextToken(T, TagNameEnd);
585 return;
586 }
587
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000588 formTokenWithChars(T, TagNameEnd, tok::html_start_tag);
589 T.setHTMLTagStartName(Name);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000590
591 BufferPtr = skipWhitespace(BufferPtr, CommentEnd);
592
Dmitri Gribenkoa99ec102012-07-09 21:32:40 +0000593 const char C = *BufferPtr;
594 if (BufferPtr != CommentEnd &&
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000595 (C == '>' || C == '/' || isHTMLIdentifierStartingCharacter(C)))
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000596 State = LS_HTMLStartTag;
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000597}
598
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000599void Lexer::lexHTMLStartTag(Token &T) {
600 assert(State == LS_HTMLStartTag);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000601
602 const char *TokenPtr = BufferPtr;
603 char C = *TokenPtr;
604 if (isHTMLIdentifierCharacter(C)) {
605 TokenPtr = skipHTMLIdentifier(TokenPtr, CommentEnd);
Dmitri Gribenkof5e0aea2012-06-27 16:30:35 +0000606 StringRef Ident(BufferPtr, TokenPtr - BufferPtr);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000607 formTokenWithChars(T, TokenPtr, tok::html_ident);
Dmitri Gribenkof5e0aea2012-06-27 16:30:35 +0000608 T.setHTMLIdent(Ident);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000609 } else {
610 switch (C) {
611 case '=':
612 TokenPtr++;
613 formTokenWithChars(T, TokenPtr, tok::html_equals);
614 break;
615 case '\"':
616 case '\'': {
617 const char *OpenQuote = TokenPtr;
618 TokenPtr = skipHTMLQuotedString(TokenPtr, CommentEnd);
619 const char *ClosingQuote = TokenPtr;
620 if (TokenPtr != CommentEnd) // Skip closing quote.
621 TokenPtr++;
622 formTokenWithChars(T, TokenPtr, tok::html_quoted_string);
623 T.setHTMLQuotedString(StringRef(OpenQuote + 1,
624 ClosingQuote - (OpenQuote + 1)));
625 break;
626 }
627 case '>':
628 TokenPtr++;
629 formTokenWithChars(T, TokenPtr, tok::html_greater);
Dmitri Gribenkoa99ec102012-07-09 21:32:40 +0000630 State = LS_Normal;
631 return;
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000632 case '/':
633 TokenPtr++;
634 if (TokenPtr != CommentEnd && *TokenPtr == '>') {
635 TokenPtr++;
636 formTokenWithChars(T, TokenPtr, tok::html_slash_greater);
Dmitri Gribenko477a9f52012-07-27 20:37:06 +0000637 } else
638 formTextToken(T, TokenPtr);
639
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +0000640 State = LS_Normal;
641 return;
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000642 }
643 }
644
645 // Now look ahead and return to normal state if we don't see any HTML tokens
646 // ahead.
647 BufferPtr = skipWhitespace(BufferPtr, CommentEnd);
648 if (BufferPtr == CommentEnd) {
649 State = LS_Normal;
650 return;
651 }
652
653 C = *BufferPtr;
Dmitri Gribenkoa99ec102012-07-09 21:32:40 +0000654 if (!isHTMLIdentifierStartingCharacter(C) &&
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000655 C != '=' && C != '\"' && C != '\'' && C != '>') {
656 State = LS_Normal;
657 return;
658 }
659}
660
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000661void Lexer::setupAndLexHTMLEndTag(Token &T) {
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000662 assert(BufferPtr[0] == '<' && BufferPtr[1] == '/');
663
664 const char *TagNameBegin = skipWhitespace(BufferPtr + 2, CommentEnd);
665 const char *TagNameEnd = skipHTMLIdentifier(TagNameBegin, CommentEnd);
Dmitri Gribenko834a5bd2012-08-22 22:56:08 +0000666 StringRef Name(TagNameBegin, TagNameEnd - TagNameBegin);
667 if (!isHTMLTagName(Name)) {
668 formTextToken(T, TagNameEnd);
669 return;
670 }
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000671
672 const char *End = skipWhitespace(TagNameEnd, CommentEnd);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000673
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000674 formTokenWithChars(T, End, tok::html_end_tag);
Dmitri Gribenko834a5bd2012-08-22 22:56:08 +0000675 T.setHTMLTagEndName(Name);
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000676
677 if (BufferPtr != CommentEnd && *BufferPtr == '>')
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000678 State = LS_HTMLEndTag;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000679}
680
Dmitri Gribenko3f38bf22012-07-13 00:44:24 +0000681void Lexer::lexHTMLEndTag(Token &T) {
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000682 assert(BufferPtr != CommentEnd && *BufferPtr == '>');
683
684 formTokenWithChars(T, BufferPtr + 1, tok::html_greater);
685 State = LS_Normal;
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000686}
687
Dmitri Gribenkoaa580812012-08-09 00:03:17 +0000688Lexer::Lexer(llvm::BumpPtrAllocator &Allocator, const CommandTraits &Traits,
Dmitri Gribenkoaf503a62012-08-31 10:35:30 +0000689 SourceLocation FileLoc,
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000690 const char *BufferStart, const char *BufferEnd):
Dmitri Gribenkoaa580812012-08-09 00:03:17 +0000691 Allocator(Allocator), Traits(Traits),
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000692 BufferStart(BufferStart), BufferEnd(BufferEnd),
Dmitri Gribenkoaf503a62012-08-31 10:35:30 +0000693 FileLoc(FileLoc), BufferPtr(BufferStart),
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000694 CommentState(LCS_BeforeComment), State(LS_Normal) {
695}
696
697void Lexer::lex(Token &T) {
698again:
699 switch (CommentState) {
700 case LCS_BeforeComment:
701 if (BufferPtr == BufferEnd) {
702 formTokenWithChars(T, BufferPtr, tok::eof);
703 return;
704 }
705
706 assert(*BufferPtr == '/');
707 BufferPtr++; // Skip first slash.
708 switch(*BufferPtr) {
709 case '/': { // BCPL comment.
710 BufferPtr++; // Skip second slash.
711
712 if (BufferPtr != BufferEnd) {
713 // Skip Doxygen magic marker, if it is present.
714 // It might be missing because of a typo //< or /*<, or because we
715 // merged this non-Doxygen comment into a bunch of Doxygen comments
716 // around it: /** ... */ /* ... */ /** ... */
717 const char C = *BufferPtr;
718 if (C == '/' || C == '!')
719 BufferPtr++;
720 }
721
722 // Skip less-than symbol that marks trailing comments.
723 // Skip it even if the comment is not a Doxygen one, because //< and /*<
724 // are frequent typos.
725 if (BufferPtr != BufferEnd && *BufferPtr == '<')
726 BufferPtr++;
727
728 CommentState = LCS_InsideBCPLComment;
Dmitri Gribenko8d3ba232012-07-06 00:28:32 +0000729 if (State != LS_VerbatimBlockBody && State != LS_VerbatimBlockFirstLine)
730 State = LS_Normal;
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000731 CommentEnd = findBCPLCommentEnd(BufferPtr, BufferEnd);
732 goto again;
733 }
734 case '*': { // C comment.
735 BufferPtr++; // Skip star.
736
737 // Skip Doxygen magic marker.
738 const char C = *BufferPtr;
739 if ((C == '*' && *(BufferPtr + 1) != '/') || C == '!')
740 BufferPtr++;
741
742 // Skip less-than symbol that marks trailing comments.
743 if (BufferPtr != BufferEnd && *BufferPtr == '<')
744 BufferPtr++;
745
746 CommentState = LCS_InsideCComment;
747 State = LS_Normal;
748 CommentEnd = findCCommentEnd(BufferPtr, BufferEnd);
749 goto again;
750 }
751 default:
752 llvm_unreachable("second character of comment should be '/' or '*'");
753 }
754
755 case LCS_BetweenComments: {
756 // Consecutive comments are extracted only if there is only whitespace
757 // between them. So we can search for the start of the next comment.
758 const char *EndWhitespace = BufferPtr;
759 while(EndWhitespace != BufferEnd && *EndWhitespace != '/')
760 EndWhitespace++;
761
762 // Turn any whitespace between comments (and there is only whitespace
Dmitri Gribenkoa99ec102012-07-09 21:32:40 +0000763 // between them -- guaranteed by comment extraction) into a newline. We
764 // have two newlines between C comments in total (first one was synthesized
765 // after a comment).
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000766 formTokenWithChars(T, EndWhitespace, tok::newline);
767
768 CommentState = LCS_BeforeComment;
769 break;
770 }
771
772 case LCS_InsideBCPLComment:
773 case LCS_InsideCComment:
774 if (BufferPtr != CommentEnd) {
775 lexCommentText(T);
776 break;
777 } else {
778 // Skip C comment closing sequence.
779 if (CommentState == LCS_InsideCComment) {
780 assert(BufferPtr[0] == '*' && BufferPtr[1] == '/');
781 BufferPtr += 2;
782 assert(BufferPtr <= BufferEnd);
783
784 // Synthenize newline just after the C comment, regardless if there is
785 // actually a newline.
786 formTokenWithChars(T, BufferPtr, tok::newline);
787
788 CommentState = LCS_BetweenComments;
789 break;
790 } else {
791 // Don't synthesized a newline after BCPL comment.
792 CommentState = LCS_BetweenComments;
793 goto again;
794 }
795 }
796 }
797}
798
799StringRef Lexer::getSpelling(const Token &Tok,
800 const SourceManager &SourceMgr,
801 bool *Invalid) const {
802 SourceLocation Loc = Tok.getLocation();
803 std::pair<FileID, unsigned> LocInfo = SourceMgr.getDecomposedLoc(Loc);
804
805 bool InvalidTemp = false;
806 StringRef File = SourceMgr.getBufferData(LocInfo.first, &InvalidTemp);
807 if (InvalidTemp) {
808 *Invalid = true;
809 return StringRef();
810 }
811
812 const char *Begin = File.data() + LocInfo.second;
813 return StringRef(Begin, Tok.getLength());
814}
815
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000816} // end namespace comments
817} // end namespace clang
818