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