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