PTHLexer.cpp revision 4d35da2e41941965bbee8ed7e8c30e7c21000d71
1//===--- PTHLexer.cpp - Lex from a token stream ---------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the PTHLexer interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Lex/PTHLexer.h"
15#include "clang/Lex/Preprocessor.h"
16#include "clang/Basic/TokenKinds.h"
17using namespace clang;
18
19PTHLexer::PTHLexer(Preprocessor& pp, SourceLocation fileloc,
20                   const Token *TokArray, unsigned NumToks)
21  : PreprocessorLexer(&pp, fileloc), FileLoc(fileloc),
22    Tokens(TokArray), NumTokens(NumToks), CurToken(0) {
23
24  assert (Tokens[NumTokens-1].is(tok::eof));
25  --NumTokens;
26
27  LexingRawMode = false;
28  ParsingPreprocessorDirective = false;
29  ParsingFilename = false;
30}
31
32void PTHLexer::Lex(Token& Tok) {
33
34  if (CurToken == NumTokens) {
35    // If we hit the end of the file while parsing a preprocessor directive,
36    // end the preprocessor directive first.  The next token returned will
37    // then be the end of file.
38    //   OR
39    // If we are in raw mode, return this event as an EOF token.  Let the caller
40    // that put us in raw mode handle the event.
41    if (ParsingPreprocessorDirective || LexingRawMode) {
42      // Done parsing the "line".
43      ParsingPreprocessorDirective = false;
44      Tok = Tokens[CurToken]; // not an out-of-bound access
45      // FIXME: eom handling?
46    }
47    else
48      PP->HandleEndOfFile(Tok, false);
49
50    return;
51  }
52
53  Tok = Tokens[CurToken];
54
55  if (ParsingPreprocessorDirective && Tok.isAtStartOfLine()) {
56    ParsingPreprocessorDirective = false; // Done parsing the "line".
57    MIOpt.ReadToken();
58    // FIXME:  Need to replicate:
59    // FormTokenWithChars(Tok, CurPtr, tok::eom);
60    Tok.setKind(tok::eom);
61    return;
62  }
63  else // Otherwise, advance to the next token.
64    ++CurToken;
65
66  if (Tok.isAtStartOfLine() && Tok.is(tok::hash) && !LexingRawMode) {
67    PP->HandleDirective(Tok);
68    PP->Lex(Tok);
69    return;
70  }
71
72  MIOpt.ReadToken();
73}
74
75void PTHLexer::setEOF(Token& Tok) {
76  Tok = Tokens[NumTokens]; // NumTokens is already adjusted, so this isn't
77                           // an overflow.
78}
79
80void PTHLexer::DiscardToEndOfLine() {
81  assert(ParsingPreprocessorDirective && ParsingFilename == false &&
82         "Must be in a preprocessing directive!");
83
84  // Already at end-of-file?
85  if (CurToken == NumTokens)
86    return;
87
88  // Find the first token that is not the start of the *current* line.
89  for ( ++CurToken; CurToken != NumTokens ; ++CurToken )
90    if (Tokens[CurToken].isAtStartOfLine())
91      return;
92}
93
94unsigned PTHLexer::isNextPPTokenLParen() {
95  if (CurToken == NumTokens)
96    return 2;
97
98  return Tokens[CurToken].is(tok::l_paren);
99}
100
101