Unbreak LLVM on the MSVC compiler:
[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 is distributed under the University of Illinois Open Source
6 // 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 "llvm/Support/DataTypes.h"
18 #include <vector>
19 #include <string>
20 #include <iosfwd>
21 #include <cassert>
22
23 namespace llvm {
24 class MemoryBuffer;
25   
26 namespace tgtok {
27   enum TokKind {
28     // Markers
29     Eof, Error,
30     
31     // Tokens with no info.
32     minus, plus,        // - +
33     l_square, r_square, // [ ]
34     l_brace, r_brace,   // { }
35     l_paren, r_paren,   // ( )
36     less, greater,      // < >
37     colon, semi,        // ; :
38     comma, period,      // , .
39     equal, question,    // = ?
40     
41     // Keywords.
42     Bit, Bits, Class, Code, Dag, Def, Defm, Field, In, Int, Let, List,
43     MultiClass, String,
44     
45     // !keywords.
46     XConcat, XSRA, XSRL, XSHL, XStrConcat,
47     
48     // Integer value.
49     IntVal,
50     
51     // String valued tokens.
52     Id, StrVal, VarName, CodeFragment
53   };
54 }
55
56 /// TGLexer - TableGen Lexer class.
57 class TGLexer {
58   const char *CurPtr;
59   unsigned CurLineNo;
60   MemoryBuffer *CurBuf;
61
62   // Information about the current token.
63   const char *TokStart;
64   tgtok::TokKind CurCode;
65   std::string CurStrVal;  // This is valid for ID, STRVAL, VARNAME, CODEFRAGMENT
66   int64_t CurIntVal;      // This is valid for INTVAL.
67   
68   /// IncludeRec / IncludeStack - This captures the current set of include
69   /// directives we are nested within.
70   struct IncludeRec {
71     MemoryBuffer *Buffer;
72     const char *CurPtr;
73     unsigned LineNo;
74     IncludeRec(MemoryBuffer *buffer, const char *curPtr, unsigned lineNo)
75       : Buffer(buffer), CurPtr(curPtr), LineNo(lineNo) {}
76   };
77   std::vector<IncludeRec> IncludeStack;
78   
79   // IncludeDirectories - This is the list of directories we should search for
80   // include files in.
81   std::vector<std::string> IncludeDirectories;
82 public:
83   TGLexer(MemoryBuffer *StartBuf);
84   ~TGLexer();
85   
86   void setIncludeDirs(const std::vector<std::string> &Dirs) {
87     IncludeDirectories = Dirs;
88   }
89   
90   tgtok::TokKind Lex() {
91     return CurCode = LexToken();
92   }
93   
94   tgtok::TokKind getCode() const { return CurCode; }
95
96   const std::string &getCurStrVal() const {
97     assert((CurCode == tgtok::Id || CurCode == tgtok::StrVal || 
98             CurCode == tgtok::VarName || CurCode == tgtok::CodeFragment) &&
99            "This token doesn't have a string value");
100     return CurStrVal;
101   }
102   int64_t getCurIntVal() const {
103     assert(CurCode == tgtok::IntVal && "This token isn't an integer");
104     return CurIntVal;
105   }
106
107   typedef const char* LocTy;
108   LocTy getLoc() const { return TokStart; }
109
110   void PrintError(LocTy Loc, const std::string &Msg) const;
111   
112   void PrintIncludeStack(std::ostream &OS) const;
113   
114 private:
115   /// LexToken - Read the next token and return its code.
116   tgtok::TokKind LexToken();
117   
118   tgtok::TokKind ReturnError(const char *Loc, const std::string &Msg);
119   
120   int getNextChar();
121   void SkipBCPLComment();
122   bool SkipCComment();
123   tgtok::TokKind LexIdentifier();
124   bool LexInclude();
125   tgtok::TokKind LexString();
126   tgtok::TokKind LexVarName();
127   tgtok::TokKind LexNumber();
128   tgtok::TokKind LexBracket();
129   tgtok::TokKind LexExclaim();
130 };
131   
132 } // end namespace llvm
133
134 #endif