make "locations" a class instead of a typedef.
[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 class TGLoc;
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,
49     
50     // Integer value.
51     IntVal,
52     
53     // String valued tokens.
54     Id, StrVal, VarName, CodeFragment
55   };
56 }
57
58 /// TGLexer - TableGen Lexer class.
59 class TGLexer {
60   TGSourceMgr &SrcMgr;
61   
62   const char *CurPtr;
63   const MemoryBuffer *CurBuf;
64
65   // Information about the current token.
66   const char *TokStart;
67   tgtok::TokKind CurCode;
68   std::string CurStrVal;  // This is valid for ID, STRVAL, VARNAME, CODEFRAGMENT
69   int64_t CurIntVal;      // This is valid for INTVAL.
70
71   /// CurBuffer - This is the current buffer index we're lexing from as managed
72   /// by the SourceMgr object.
73   int CurBuffer;
74   
75   // IncludeDirectories - This is the list of directories we should search for
76   // include files in.
77   std::vector<std::string> IncludeDirectories;
78 public:
79   TGLexer(TGSourceMgr &SrcMgr);
80   ~TGLexer() {}
81   
82   void setIncludeDirs(const std::vector<std::string> &Dirs) {
83     IncludeDirectories = Dirs;
84   }
85   
86   tgtok::TokKind Lex() {
87     return CurCode = LexToken();
88   }
89   
90   tgtok::TokKind getCode() const { return CurCode; }
91
92   const std::string &getCurStrVal() const {
93     assert((CurCode == tgtok::Id || CurCode == tgtok::StrVal || 
94             CurCode == tgtok::VarName || CurCode == tgtok::CodeFragment) &&
95            "This token doesn't have a string value");
96     return CurStrVal;
97   }
98   int64_t getCurIntVal() const {
99     assert(CurCode == tgtok::IntVal && "This token isn't an integer");
100     return CurIntVal;
101   }
102
103   TGLoc getLoc() const;
104
105   void PrintError(const char *Loc, const std::string &Msg) const;
106   void PrintError(TGLoc Loc, const std::string &Msg) const;
107   
108 private:
109   /// LexToken - Read the next token and return its code.
110   tgtok::TokKind LexToken();
111   
112   tgtok::TokKind ReturnError(const char *Loc, const std::string &Msg);
113   
114   int getNextChar();
115   void SkipBCPLComment();
116   bool SkipCComment();
117   tgtok::TokKind LexIdentifier();
118   bool LexInclude();
119   tgtok::TokKind LexString();
120   tgtok::TokKind LexVarName();
121   tgtok::TokKind LexNumber();
122   tgtok::TokKind LexBracket();
123   tgtok::TokKind LexExclaim();
124 };
125   
126 } // end namespace llvm
127
128 #endif