@@ -174,131 +174,174 @@ int TGLexer::peekNextChar(int Index) const {
174174}
175175
176176tgtok::TokKind TGLexer::LexToken (bool FileOrLineStart) {
177- restart:
178- TokStart = CurPtr;
179- // This always consumes at least one character.
180- int CurChar = getNextChar ();
177+ while ( true ) {
178+ TokStart = CurPtr;
179+ // This always consumes at least one character.
180+ int CurChar = getNextChar ();
181181
182- switch (CurChar) {
183- default :
184- // Handle letters: [a-zA-Z_]
185- if (isValidIDChar (CurChar, /* First=*/ true ))
186- return LexIdentifier ();
187-
188- // Unknown character, emit an error.
189- return ReturnError (TokStart, " unexpected character" );
190- case EOF:
191- // Lex next token, if we just left an include file.
192- if (processEOF ()) {
193- // Leaving an include file means that the next symbol is located at the
194- // end of the 'include "..."' construct.
195- FileOrLineStart = false ;
196- goto restart;
197- }
182+ switch (CurChar) {
183+ default :
184+ // Handle letters: [a-zA-Z_]
185+ if (isValidIDChar (CurChar, /* First=*/ true ))
186+ return LexIdentifier ();
198187
199- // Return EOF denoting the end of lexing.
200- return tgtok::Eof;
201-
202- case ' :' : return tgtok::colon;
203- case ' ;' : return tgtok::semi;
204- case ' ,' : return tgtok::comma;
205- case ' <' : return tgtok::less;
206- case ' >' : return tgtok::greater;
207- case ' ]' : return tgtok::r_square;
208- case ' {' : return tgtok::l_brace;
209- case ' }' : return tgtok::r_brace;
210- case ' (' : return tgtok::l_paren;
211- case ' )' : return tgtok::r_paren;
212- case ' =' : return tgtok::equal;
213- case ' ?' : return tgtok::question;
214- case ' #' :
215- if (FileOrLineStart) {
216- tgtok::TokKind Kind = prepIsDirective ();
217- if (Kind != tgtok::Error)
218- return lexPreprocessor (Kind);
219- }
188+ // Unknown character, emit an error.
189+ return ReturnError (TokStart, " unexpected character" );
190+ case EOF:
191+ // Lex next token, if we just left an include file.
192+ if (processEOF ()) {
193+ // Leaving an include file means that the next symbol is located at the
194+ // end of the 'include "..."' construct.
195+ FileOrLineStart = false ;
196+ break ;
197+ }
220198
221- return tgtok::paste;
199+ // Return EOF denoting the end of lexing.
200+ return tgtok::Eof;
201+
202+ case ' :' :
203+ return tgtok::colon;
204+ case ' ;' :
205+ return tgtok::semi;
206+ case ' ,' :
207+ return tgtok::comma;
208+ case ' <' :
209+ return tgtok::less;
210+ case ' >' :
211+ return tgtok::greater;
212+ case ' ]' :
213+ return tgtok::r_square;
214+ case ' {' :
215+ return tgtok::l_brace;
216+ case ' }' :
217+ return tgtok::r_brace;
218+ case ' (' :
219+ return tgtok::l_paren;
220+ case ' )' :
221+ return tgtok::r_paren;
222+ case ' =' :
223+ return tgtok::equal;
224+ case ' ?' :
225+ return tgtok::question;
226+ case ' #' :
227+ if (FileOrLineStart) {
228+ tgtok::TokKind Kind = prepIsDirective ();
229+ if (Kind != tgtok::Error)
230+ return lexPreprocessor (Kind);
231+ }
232+
233+ return tgtok::paste;
222234
223- // The period is a separate case so we can recognize the "..."
224- // range punctuator.
225- case ' .' :
226- if (peekNextChar (0 ) == ' .' ) {
227- ++CurPtr; // Eat second dot.
235+ // The period is a separate case so we can recognize the "..."
236+ // range punctuator.
237+ case ' .' :
228238 if (peekNextChar (0 ) == ' .' ) {
229- ++CurPtr; // Eat third dot.
230- return tgtok::dotdotdot;
239+ ++CurPtr; // Eat second dot.
240+ if (peekNextChar (0 ) == ' .' ) {
241+ ++CurPtr; // Eat third dot.
242+ return tgtok::dotdotdot;
243+ }
244+ return ReturnError (TokStart, " invalid '..' punctuation" );
231245 }
232- return ReturnError (TokStart, " invalid '..' punctuation" );
233- }
234- return tgtok::dot;
246+ return tgtok::dot;
235247
236- case ' \r ' :
237- llvm_unreachable (" getNextChar() must never return '\r '" );
248+ case ' \r ' :
249+ llvm_unreachable (" getNextChar() must never return '\r '" );
238250
239- case ' ' :
240- case ' \t ' :
241- // Ignore whitespace.
242- goto restart;
243- case ' \n ' :
244- // Ignore whitespace, and identify the new line.
245- FileOrLineStart = true ;
246- goto restart;
247- case ' /' :
248- // If this is the start of a // comment, skip until the end of the line or
249- // the end of the buffer.
250- if (*CurPtr == ' /' )
251- SkipBCPLComment ();
252- else if (*CurPtr == ' *' ) {
253- if (SkipCComment ())
254- return tgtok::Error;
255- } else // Otherwise, this is an error.
256- return ReturnError (TokStart, " unexpected character" );
257- goto restart;
258- case ' -' : case ' +' :
259- case ' 0' : case ' 1' : case ' 2' : case ' 3' : case ' 4' : case ' 5' : case ' 6' :
260- case ' 7' : case ' 8' : case ' 9' : {
261- int NextChar = 0 ;
262- if (isDigit (CurChar)) {
263- // Allow identifiers to start with a number if it is followed by
264- // an identifier. This can happen with paste operations like
265- // foo#8i.
266- int i = 0 ;
267- do {
268- NextChar = peekNextChar (i++);
269- } while (isDigit (NextChar));
270-
271- if (NextChar == ' x' || NextChar == ' b' ) {
272- // If this is [0-9]b[01] or [0-9]x[0-9A-fa-f] this is most
273- // likely a number.
274- int NextNextChar = peekNextChar (i);
275- switch (NextNextChar) {
276- default :
277- break ;
278- case ' 0' : case ' 1' :
279- if (NextChar == ' b' )
280- return LexNumber ();
281- [[fallthrough]];
282- case ' 2' : case ' 3' : case ' 4' : case ' 5' :
283- case ' 6' : case ' 7' : case ' 8' : case ' 9' :
284- case ' a' : case ' b' : case ' c' : case ' d' : case ' e' : case ' f' :
285- case ' A' : case ' B' : case ' C' : case ' D' : case ' E' : case ' F' :
286- if (NextChar == ' x' )
287- return LexNumber ();
288- break ;
251+ case ' ' :
252+ case ' \t ' :
253+ // Ignore whitespace.
254+ break ;
255+ case ' \n ' :
256+ // Ignore whitespace, and identify the new line.
257+ FileOrLineStart = true ;
258+ break ;
259+ case ' /' :
260+ // If this is the start of a // comment, skip until the end of the line or
261+ // the end of the buffer.
262+ if (*CurPtr == ' /' )
263+ SkipBCPLComment ();
264+ else if (*CurPtr == ' *' ) {
265+ if (SkipCComment ())
266+ return tgtok::Error;
267+ } else // Otherwise, this is an error.
268+ return ReturnError (TokStart, " unexpected character" );
269+ break ;
270+ case ' -' :
271+ case ' +' :
272+ case ' 0' :
273+ case ' 1' :
274+ case ' 2' :
275+ case ' 3' :
276+ case ' 4' :
277+ case ' 5' :
278+ case ' 6' :
279+ case ' 7' :
280+ case ' 8' :
281+ case ' 9' : {
282+ int NextChar = 0 ;
283+ if (isDigit (CurChar)) {
284+ // Allow identifiers to start with a number if it is followed by
285+ // an identifier. This can happen with paste operations like
286+ // foo#8i.
287+ int i = 0 ;
288+ do {
289+ NextChar = peekNextChar (i++);
290+ } while (isDigit (NextChar));
291+
292+ if (NextChar == ' x' || NextChar == ' b' ) {
293+ // If this is [0-9]b[01] or [0-9]x[0-9A-fa-f] this is most
294+ // likely a number.
295+ int NextNextChar = peekNextChar (i);
296+ switch (NextNextChar) {
297+ default :
298+ break ;
299+ case ' 0' :
300+ case ' 1' :
301+ if (NextChar == ' b' )
302+ return LexNumber ();
303+ [[fallthrough]];
304+ case ' 2' :
305+ case ' 3' :
306+ case ' 4' :
307+ case ' 5' :
308+ case ' 6' :
309+ case ' 7' :
310+ case ' 8' :
311+ case ' 9' :
312+ case ' a' :
313+ case ' b' :
314+ case ' c' :
315+ case ' d' :
316+ case ' e' :
317+ case ' f' :
318+ case ' A' :
319+ case ' B' :
320+ case ' C' :
321+ case ' D' :
322+ case ' E' :
323+ case ' F' :
324+ if (NextChar == ' x' )
325+ return LexNumber ();
326+ break ;
327+ }
289328 }
290329 }
291- }
292330
293- if (isValidIDChar (NextChar, /* First=*/ true ))
294- return LexIdentifier ();
331+ if (isValidIDChar (NextChar, /* First=*/ true ))
332+ return LexIdentifier ();
295333
296- return LexNumber ();
297- }
298- case ' "' : return LexString ();
299- case ' $' : return LexVarName ();
300- case ' [' : return LexBracket ();
301- case ' !' : return LexExclaim ();
334+ return LexNumber ();
335+ }
336+ case ' "' :
337+ return LexString ();
338+ case ' $' :
339+ return LexVarName ();
340+ case ' [' :
341+ return LexBracket ();
342+ case ' !' :
343+ return LexExclaim ();
344+ }
302345 }
303346}
304347
0 commit comments