Ted Kremenek | 274b208 | 2008-11-12 21:37:15 +0000 | [diff] [blame] | 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" |
Ted Kremenek | 274b208 | 2008-11-12 21:37:15 +0000 | [diff] [blame] | 16 | #include "clang/Basic/TokenKinds.h" |
Ted Kremenek | 274b208 | 2008-11-12 21:37:15 +0000 | [diff] [blame] | 17 | using namespace clang; |
| 18 | |
| 19 | PTHLexer::PTHLexer(Preprocessor& pp, SourceLocation fileloc, |
| 20 | const Token *TokArray, unsigned NumToks) |
Ted Kremenek | 41938c8 | 2008-11-19 21:57:25 +0000 | [diff] [blame] | 21 | : PreprocessorLexer(&pp, fileloc), FileLoc(fileloc), |
| 22 | Tokens(TokArray), NumTokens(NumToks), CurToken(0) { |
Ted Kremenek | 274b208 | 2008-11-12 21:37:15 +0000 | [diff] [blame] | 23 | |
| 24 | assert (Tokens[NumTokens-1].is(tok::eof)); |
| 25 | --NumTokens; |
| 26 | |
| 27 | LexingRawMode = false; |
| 28 | ParsingPreprocessorDirective = false; |
| 29 | ParsingFilename = false; |
| 30 | } |
| 31 | |
| 32 | void 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 |
Ted Kremenek | d6a2e7d | 2008-11-12 23:13:54 +0000 | [diff] [blame] | 48 | PP->HandleEndOfFile(Tok, false); |
Ted Kremenek | 274b208 | 2008-11-12 21:37:15 +0000 | [diff] [blame] | 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) { |
Ted Kremenek | d6a2e7d | 2008-11-12 23:13:54 +0000 | [diff] [blame] | 67 | PP->HandleDirective(Tok); |
| 68 | PP->Lex(Tok); |
Ted Kremenek | 274b208 | 2008-11-12 21:37:15 +0000 | [diff] [blame] | 69 | return; |
| 70 | } |
| 71 | |
| 72 | MIOpt.ReadToken(); |
| 73 | } |
| 74 | |
| 75 | void PTHLexer::setEOF(Token& Tok) { |
| 76 | Tok = Tokens[NumTokens]; // NumTokens is already adjusted, so this isn't |
| 77 | // an overflow. |
| 78 | } |
Ted Kremenek | 17ff58a | 2008-11-19 22:21:33 +0000 | [diff] [blame] | 79 | |
| 80 | void PTHLexer::DiscardToEndOfLine() { |
| 81 | assert(ParsingPreprocessorDirective && ParsingFilename == false && |
| 82 | "Must be in a preprocessing directive!"); |
Ted Kremenek | 17ff58a | 2008-11-19 22:21:33 +0000 | [diff] [blame] | 83 | assert (0 && "Not implemented."); |
| 84 | } |
Ted Kremenek | 2f1c024 | 2008-11-19 22:42:26 +0000 | [diff] [blame^] | 85 | |
| 86 | unsigned PTHLexer::isNextPPTokenLParen() { |
| 87 | assert (0 && "Not implemented."); |
| 88 | return 0; |
| 89 | } |
| 90 | |