PTHLexer.cpp revision 2f1c0243f7fc5d12446efcb006c071854c345923
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  assert (0 && "Not implemented.");
84}
85
86unsigned PTHLexer::isNextPPTokenLParen() {
87  assert (0 && "Not implemented.");
88  return 0;
89}
90
91