blob: e1b392068c00864f71b87ded45ef4f7b12e0f4ef [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)
23 : PP(pp), FileLoc(fileloc), Tokens(TokArray), NumTokens(NumToks), CurToken(0){
24
25 assert (Tokens[NumTokens-1].is(tok::eof));
26 --NumTokens;
27
28 LexingRawMode = false;
29 ParsingPreprocessorDirective = false;
30 ParsingFilename = false;
31}
32
33void PTHLexer::Lex(Token& Tok) {
34
35 if (CurToken == NumTokens) {
36 // If we hit the end of the file while parsing a preprocessor directive,
37 // end the preprocessor directive first. The next token returned will
38 // then be the end of file.
39 // OR
40 // If we are in raw mode, return this event as an EOF token. Let the caller
41 // that put us in raw mode handle the event.
42 if (ParsingPreprocessorDirective || LexingRawMode) {
43 // Done parsing the "line".
44 ParsingPreprocessorDirective = false;
45 Tok = Tokens[CurToken]; // not an out-of-bound access
46 // FIXME: eom handling?
47 }
48 else
49 PP.HandleEndOfFile(Tok, false);
50
51 return;
52 }
53
54 Tok = Tokens[CurToken];
55
56 if (ParsingPreprocessorDirective && Tok.isAtStartOfLine()) {
57 ParsingPreprocessorDirective = false; // Done parsing the "line".
58 MIOpt.ReadToken();
59 // FIXME: Need to replicate:
60 // FormTokenWithChars(Tok, CurPtr, tok::eom);
61 Tok.setKind(tok::eom);
62 return;
63 }
64 else // Otherwise, advance to the next token.
65 ++CurToken;
66
67 if (Tok.isAtStartOfLine() && Tok.is(tok::hash) && !LexingRawMode) {
68 PP.HandleDirective(Tok);
69 PP.Lex(Tok);
70 return;
71 }
72
73 MIOpt.ReadToken();
74}
75
76void PTHLexer::setEOF(Token& Tok) {
77 Tok = Tokens[NumTokens]; // NumTokens is already adjusted, so this isn't
78 // an overflow.
79}