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