LLVM API Documentation
00001 //===- TGLexer.h - Lexer for TableGen Files ---------------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This class represents the Lexer for tablegen files. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef TGLEXER_H 00015 #define TGLEXER_H 00016 00017 #include "llvm/Support/DataTypes.h" 00018 #include <string> 00019 #include <vector> 00020 #include <cassert> 00021 00022 namespace llvm { 00023 class MemoryBuffer; 00024 class SourceMgr; 00025 class SMLoc; 00026 class Twine; 00027 00028 namespace tgtok { 00029 enum TokKind { 00030 // Markers 00031 Eof, Error, 00032 00033 // Tokens with no info. 00034 minus, plus, // - + 00035 l_square, r_square, // [ ] 00036 l_brace, r_brace, // { } 00037 l_paren, r_paren, // ( ) 00038 less, greater, // < > 00039 colon, semi, // : ; 00040 comma, period, // , . 00041 equal, question, // = ? 00042 paste, // # 00043 00044 // Keywords. 00045 Bit, Bits, Class, Code, Dag, Def, Foreach, Defm, Field, In, Int, Let, List, 00046 MultiClass, String, 00047 00048 // !keywords. 00049 XConcat, XSRA, XSRL, XSHL, XStrConcat, XCast, XSubst, 00050 XForEach, XHead, XTail, XEmpty, XIf, XEq, 00051 00052 // Integer value. 00053 IntVal, 00054 00055 // String valued tokens. 00056 Id, StrVal, VarName, CodeFragment 00057 }; 00058 } 00059 00060 /// TGLexer - TableGen Lexer class. 00061 class TGLexer { 00062 SourceMgr &SrcMgr; 00063 00064 const char *CurPtr; 00065 const MemoryBuffer *CurBuf; 00066 00067 // Information about the current token. 00068 const char *TokStart; 00069 tgtok::TokKind CurCode; 00070 std::string CurStrVal; // This is valid for ID, STRVAL, VARNAME, CODEFRAGMENT 00071 int64_t CurIntVal; // This is valid for INTVAL. 00072 00073 /// CurBuffer - This is the current buffer index we're lexing from as managed 00074 /// by the SourceMgr object. 00075 int CurBuffer; 00076 /// Dependencies - This is the list of all included files. 00077 std::vector<std::string> Dependencies; 00078 00079 public: 00080 TGLexer(SourceMgr &SrcMgr); 00081 ~TGLexer() {} 00082 00083 tgtok::TokKind Lex() { 00084 return CurCode = LexToken(); 00085 } 00086 00087 const std::vector<std::string> &getDependencies() const { 00088 return Dependencies; 00089 } 00090 00091 tgtok::TokKind getCode() const { return CurCode; } 00092 00093 const std::string &getCurStrVal() const { 00094 assert((CurCode == tgtok::Id || CurCode == tgtok::StrVal || 00095 CurCode == tgtok::VarName || CurCode == tgtok::CodeFragment) && 00096 "This token doesn't have a string value"); 00097 return CurStrVal; 00098 } 00099 int64_t getCurIntVal() const { 00100 assert(CurCode == tgtok::IntVal && "This token isn't an integer"); 00101 return CurIntVal; 00102 } 00103 00104 SMLoc getLoc() const; 00105 00106 private: 00107 /// LexToken - Read the next token and return its code. 00108 tgtok::TokKind LexToken(); 00109 00110 tgtok::TokKind ReturnError(const char *Loc, const Twine &Msg); 00111 00112 int getNextChar(); 00113 int peekNextChar(int Index); 00114 void SkipBCPLComment(); 00115 bool SkipCComment(); 00116 tgtok::TokKind LexIdentifier(); 00117 bool LexInclude(); 00118 tgtok::TokKind LexString(); 00119 tgtok::TokKind LexVarName(); 00120 tgtok::TokKind LexNumber(); 00121 tgtok::TokKind LexBracket(); 00122 tgtok::TokKind LexExclaim(); 00123 }; 00124 00125 } // end namespace llvm 00126 00127 #endif