@@ -175,17 +175,25 @@ LLLexer::LLLexer(StringRef StartBuf, SourceMgr &SM, SMDiagnostic &Err,
175175}
176176
177177int LLLexer::getNextChar () {
178- char CurChar = *CurPtr++;
178+ int NextChar = peekNextChar ();
179+ // Keeping CurPtr unchanged at EOF, so that another call to `getNextChar`
180+ // returns EOF again.
181+ if (NextChar != EOF)
182+ ++CurPtr;
183+ return NextChar;
184+ }
185+
186+ int LLLexer::peekNextChar () const {
187+ char CurChar = *CurPtr;
179188 switch (CurChar) {
180189 default : return (unsigned char )CurChar;
181190 case 0 :
182191 // A nul character in the stream is either the end of the current buffer or
183192 // a random nul in the file. Disambiguate that here.
184- if (CurPtr- 1 != CurBuf.end ())
193+ if (CurPtr != CurBuf.end ())
185194 return 0 ; // Just whitespace.
186195
187196 // Otherwise, return end of file.
188- --CurPtr; // Another call to lex will return EOF again.
189197 return EOF;
190198 }
191199}
@@ -251,6 +259,10 @@ lltok::Kind LLLexer::LexToken() {
251259 case ' ,' : return lltok::comma;
252260 case ' *' : return lltok::star;
253261 case ' |' : return lltok::bar;
262+ case ' /' :
263+ if (peekNextChar () == ' *' && SkipCComment ())
264+ return lltok::Error;
265+ continue ;
254266 }
255267 }
256268}
@@ -262,6 +274,38 @@ void LLLexer::SkipLineComment() {
262274 }
263275}
264276
277+ // / SkipCComment - This skips C-style /**/ comments. The only difference from C
278+ // / is that we allow nesting.
279+ bool LLLexer::SkipCComment () {
280+ getNextChar (); // skip the star.
281+ unsigned CommentDepth = 1 ;
282+
283+ while (true ) {
284+ int CurChar = getNextChar ();
285+ switch (CurChar) {
286+ case EOF:
287+ LexError (" unterminated comment" );
288+ return true ;
289+ case ' *' :
290+ // End of the comment?
291+ if (peekNextChar () != ' /' )
292+ break ;
293+
294+ getNextChar (); // End the '/'.
295+ if (--CommentDepth == 0 )
296+ return false ;
297+ break ;
298+ case ' /' :
299+ // Start of a nested comment?
300+ if (peekNextChar () != ' *' )
301+ break ;
302+ getNextChar (); // Eat the '*'.
303+ ++CommentDepth;
304+ break ;
305+ }
306+ }
307+ }
308+
265309// / Lex all tokens that start with an @ character.
266310// / GlobalVar @\"[^\"]*\"
267311// / GlobalVar @[-a-zA-Z$._][-a-zA-Z$._0-9]*
0 commit comments