Record the start of the current token, for use in error reporting.
[oota-llvm.git] / utils / TableGen / TGLexer.h
1 //===- TGLexer.h - Lexer for TableGen Files ---------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This class represents the Lexer for tablegen files.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef TGLEXER_H
15 #define TGLEXER_H
16
17 #include <vector>
18 #include <string>
19 #include <iosfwd>
20
21 namespace llvm {
22 class MemoryBuffer;
23
24 class TGLexer {
25   const char *CurPtr;
26   unsigned CurLineNo;
27   MemoryBuffer *CurBuf;
28
29   /// IncludeRec / IncludeStack - This captures the current set of include
30   /// directives we are nested within.
31   struct IncludeRec {
32     MemoryBuffer *Buffer;
33     const char *CurPtr;
34     unsigned LineNo;
35     IncludeRec(MemoryBuffer *buffer, const char *curPtr, unsigned lineNo)
36       : Buffer(buffer), CurPtr(curPtr), LineNo(lineNo) {}
37   };
38   std::vector<IncludeRec> IncludeStack;
39   
40   // IncludeDirectories - This is the list of directories we should search for
41   // include files in.
42   std::vector<std::string> IncludeDirectories;
43   const char *TokStart;
44 public:
45   TGLexer(MemoryBuffer *StartBuf);
46   ~TGLexer();
47   
48   void setIncludeDirs(const std::vector<std::string> &Dirs) {
49     IncludeDirectories = Dirs;
50   }
51   
52   int LexToken();
53
54   typedef const char* LocationTy;
55   LocationTy getTokenStart() const { return TokStart; }
56
57   void PrintError(LocationTy Loc, const std::string &Msg) const;
58   
59   std::ostream &err() const;
60   void PrintIncludeStack(std::ostream &OS) const;
61   
62 private:
63   int ReturnError(const char *Loc, const std::string &Msg);
64   
65   int getNextChar();
66   void SkipBCPLComment();
67   bool SkipCComment();
68   int LexIdentifier();
69   bool LexInclude();
70   int LexString();
71   int LexVarName();
72   int LexNumber();
73   int LexBracket();
74   int LexExclaim();
75 };
76   
77 } // end namespace llvm
78
79 #endif