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