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 "llvm/Support/SMLoc.h" 19 #include <cassert> 20 #include <map> 21 #include <string> 22 23 namespace llvm { 24 class MemoryBuffer; 25 class SourceMgr; 26 class SMLoc; 27 class Twine; 28 29 namespace tgtok { 30 enum TokKind { 31 // Markers 32 Eof, Error, 33 34 // Tokens with no info. 35 minus, plus, // - + 36 l_square, r_square, // [ ] 37 l_brace, r_brace, // { } 38 l_paren, r_paren, // ( ) 39 less, greater, // < > 40 colon, semi, // : ; 41 comma, period, // , . 42 equal, question, // = ? 43 paste, // # 44 45 // Keywords. 46 Bit, Bits, Class, Code, Dag, Def, Foreach, Defm, Field, In, Int, Let, List, 47 MultiClass, String, 48 49 // !keywords. 50 XConcat, XADD, XSRA, XSRL, XSHL, XStrConcat, XCast, XSubst, 51 XForEach, XHead, XTail, XEmpty, XIf, XEq, 52 53 // Integer value. 54 IntVal, 55 56 // String valued tokens. 57 Id, StrVal, VarName, CodeFragment 58 }; 59 } 60 61 /// TGLexer - TableGen Lexer class. 62 class TGLexer { 63 SourceMgr &SrcMgr; 64 65 const char *CurPtr; 66 const MemoryBuffer *CurBuf; 67 68 // Information about the current token. 69 const char *TokStart; 70 tgtok::TokKind CurCode; 71 std::string CurStrVal; // This is valid for ID, STRVAL, VARNAME, CODEFRAGMENT 72 int64_t CurIntVal; // This is valid for INTVAL. 73 74 /// CurBuffer - This is the current buffer index we're lexing from as managed 75 /// by the SourceMgr object. 76 int CurBuffer; 77 78 public: 79 typedef std::map<std::string, SMLoc> DependenciesMapTy; 80 private: 81 /// Dependencies - This is the list of all included files. 82 DependenciesMapTy Dependencies; 83 84 public: 85 TGLexer(SourceMgr &SrcMgr); 86 ~TGLexer() {} 87 88 tgtok::TokKind Lex() { 89 return CurCode = LexToken(); 90 } 91 92 const DependenciesMapTy &getDependencies() const { 93 return Dependencies; 94 } 95 96 tgtok::TokKind getCode() const { return CurCode; } 97 98 const std::string &getCurStrVal() const { 99 assert((CurCode == tgtok::Id || CurCode == tgtok::StrVal || 100 CurCode == tgtok::VarName || CurCode == tgtok::CodeFragment) && 101 "This token doesn't have a string value"); 102 return CurStrVal; 103 } 104 int64_t getCurIntVal() const { 105 assert(CurCode == tgtok::IntVal && "This token isn't an integer"); 106 return CurIntVal; 107 } 108 109 SMLoc getLoc() const; 110 111 private: 112 /// LexToken - Read the next token and return its code. 113 tgtok::TokKind LexToken(); 114 115 tgtok::TokKind ReturnError(const char *Loc, const Twine &Msg); 116 117 int getNextChar(); 118 int peekNextChar(int Index); 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 133