blob: b0ecb270d9d8edeaeea6370fd076ee464f54c0dc [file] [log] [blame]
Ted Kremenek274b2082008-11-12 21:37:15 +00001//===--- 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/Lex/Token.h"
17#include "clang/Basic/TokenKinds.h"
18
19using namespace clang;
20
21PTHLexer::PTHLexer(Preprocessor& pp, SourceLocation fileloc,
22 const Token *TokArray, unsigned NumToks)
Ted Kremenekd6a2e7d2008-11-12 23:13:54 +000023 : PreprocessorLexer(&pp), FileLoc(fileloc), Tokens(TokArray),
24 NumTokens(NumToks), CurToken(0) {
Ted Kremenek274b2082008-11-12 21:37:15 +000025
26 assert (Tokens[NumTokens-1].is(tok::eof));
27 --NumTokens;
28
29 LexingRawMode = false;
30 ParsingPreprocessorDirective = false;
31 ParsingFilename = false;
32}
33
34void PTHLexer::Lex(Token& Tok) {
35
36 if (CurToken == NumTokens) {
37 // If we hit the end of the file while parsing a preprocessor directive,
38 // end the preprocessor directive first. The next token returned will
39 // then be the end of file.
40 // OR
41 // If we are in raw mode, return this event as an EOF token. Let the caller
42 // that put us in raw mode handle the event.
43 if (ParsingPreprocessorDirective || LexingRawMode) {
44 // Done parsing the "line".
45 ParsingPreprocessorDirective = false;
46 Tok = Tokens[CurToken]; // not an out-of-bound access
47 // FIXME: eom handling?
48 }
49 else
Ted Kremenekd6a2e7d2008-11-12 23:13:54 +000050 PP->HandleEndOfFile(Tok, false);
Ted Kremenek274b2082008-11-12 21:37:15 +000051
52 return;
53 }
54
55 Tok = Tokens[CurToken];
56
57 if (ParsingPreprocessorDirective && Tok.isAtStartOfLine()) {
58 ParsingPreprocessorDirective = false; // Done parsing the "line".
59 MIOpt.ReadToken();
60 // FIXME: Need to replicate:
61 // FormTokenWithChars(Tok, CurPtr, tok::eom);
62 Tok.setKind(tok::eom);
63 return;
64 }
65 else // Otherwise, advance to the next token.
66 ++CurToken;
67
68 if (Tok.isAtStartOfLine() && Tok.is(tok::hash) && !LexingRawMode) {
Ted Kremenekd6a2e7d2008-11-12 23:13:54 +000069 PP->HandleDirective(Tok);
70 PP->Lex(Tok);
Ted Kremenek274b2082008-11-12 21:37:15 +000071 return;
72 }
73
74 MIOpt.ReadToken();
75}
76
77void PTHLexer::setEOF(Token& Tok) {
78 Tok = Tokens[NumTokens]; // NumTokens is already adjusted, so this isn't
79 // an overflow.
80}