PTHLexer.h revision cd223444d1680290efe11da657faafc9a1ac14ba
1//===--- PTHLexer.h - Lexer based on Pre-tokenized input --------*- C++ -*-===//
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 defines the PTHLexer interface.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_PTHLEXER_H
15#define LLVM_CLANG_PTHLEXER_H
16
17#include "clang/Lex/PreprocessorLexer.h"
18#include <vector>
19
20namespace clang {
21
22class PTHManager;
23
24class PTHLexer : public PreprocessorLexer {
25  /// TokBuf - Buffer from PTH file containing raw token data.
26  const char* TokBuf;
27
28  /// CurPtr - Pointer into current offset of the token buffer where
29  ///  the next token will be read.
30  const char* CurPtr;
31
32  /// LastHashTokPtr - Pointer into TokBuf of the last processed '#'
33  ///  token that appears at the start of a line.
34  const char* LastHashTokPtr;
35
36  PTHLexer(const PTHLexer&);  // DO NOT IMPLEMENT
37  void operator=(const PTHLexer&); // DO NOT IMPLEMENT
38
39  /// ReadToken - Used by PTHLexer to read tokens TokBuf.
40  void ReadToken(Token& T);
41
42  /// PTHMgr - The PTHManager object that created this PTHLexer.
43  PTHManager& PTHMgr;
44
45  Token LastFetched;
46  Token EofToken;
47  bool NeedsFetching;
48
49public:
50
51  /// Create a PTHLexer for the specified token stream.
52  PTHLexer(Preprocessor& pp, SourceLocation fileloc, const char* D,
53           PTHManager& PM);
54
55  ~PTHLexer() {}
56
57  /// Lex - Return the next token.
58  void Lex(Token &Tok);
59
60  void setEOF(Token &Tok);
61
62  /// DiscardToEndOfLine - Read the rest of the current preprocessor line as an
63  /// uninterpreted string.  This switches the lexer out of directive mode.
64  void DiscardToEndOfLine();
65
66  /// isNextPPTokenLParen - Return 1 if the next unexpanded token will return a
67  /// tok::l_paren token, 0 if it is something else and 2 if there are no more
68  /// tokens controlled by this lexer.
69  unsigned isNextPPTokenLParen() {
70    return AtLastToken() ? 2 : GetToken().is(tok::l_paren);
71  }
72
73  /// IndirectLex - An indirect call to 'Lex' that can be invoked via
74  ///  the PreprocessorLexer interface.
75  void IndirectLex(Token &Result) { Lex(Result); }
76
77  /// getSourceLocation - Return a source location for the token in
78  /// the current file.
79  SourceLocation getSourceLocation() { return GetToken().getLocation(); }
80
81private:
82
83  /// SkipToToken - Skip to the token at the specified offset in TokBuf.
84  void SkipToToken(unsigned offset) {
85    const char* NewPtr = TokBuf + offset;
86    assert(NewPtr > CurPtr && "SkipToToken should not go backwards!");
87    NeedsFetching = true;
88    CurPtr = NewPtr;
89  }
90
91  /// AtLastToken - Returns true if the PTHLexer is at the last token.
92  bool AtLastToken() {
93    Token T = GetToken();
94    return T.is(tok::eof) ? EofToken = T, true : false;
95  }
96
97  /// GetToken - Returns the next token.  This method does not advance the
98  ///  PTHLexer to the next token.
99  Token GetToken();
100
101  /// AdvanceToken - Advances the PTHLexer to the next token.
102  void AdvanceToken() { NeedsFetching = true; }
103
104  bool LexEndOfFile(Token &Result);
105};
106
107}  // end namespace clang
108
109#endif
110