1*f4a2713aSLionel Sambuc //===- TGLexer.h - Lexer for TableGen Files ---------------------*- C++ -*-===// 2*f4a2713aSLionel Sambuc // 3*f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure 4*f4a2713aSLionel Sambuc // 5*f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source 6*f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details. 7*f4a2713aSLionel Sambuc // 8*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 9*f4a2713aSLionel Sambuc // 10*f4a2713aSLionel Sambuc // This class represents the Lexer for tablegen files. 11*f4a2713aSLionel Sambuc // 12*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 13*f4a2713aSLionel Sambuc 14*f4a2713aSLionel Sambuc #ifndef TGLEXER_H 15*f4a2713aSLionel Sambuc #define TGLEXER_H 16*f4a2713aSLionel Sambuc 17*f4a2713aSLionel Sambuc #include "llvm/Support/DataTypes.h" 18*f4a2713aSLionel Sambuc #include "llvm/Support/SMLoc.h" 19*f4a2713aSLionel Sambuc #include <cassert> 20*f4a2713aSLionel Sambuc #include <map> 21*f4a2713aSLionel Sambuc #include <string> 22*f4a2713aSLionel Sambuc 23*f4a2713aSLionel Sambuc namespace llvm { 24*f4a2713aSLionel Sambuc class MemoryBuffer; 25*f4a2713aSLionel Sambuc class SourceMgr; 26*f4a2713aSLionel Sambuc class SMLoc; 27*f4a2713aSLionel Sambuc class Twine; 28*f4a2713aSLionel Sambuc 29*f4a2713aSLionel Sambuc namespace tgtok { 30*f4a2713aSLionel Sambuc enum TokKind { 31*f4a2713aSLionel Sambuc // Markers 32*f4a2713aSLionel Sambuc Eof, Error, 33*f4a2713aSLionel Sambuc 34*f4a2713aSLionel Sambuc // Tokens with no info. 35*f4a2713aSLionel Sambuc minus, plus, // - + 36*f4a2713aSLionel Sambuc l_square, r_square, // [ ] 37*f4a2713aSLionel Sambuc l_brace, r_brace, // { } 38*f4a2713aSLionel Sambuc l_paren, r_paren, // ( ) 39*f4a2713aSLionel Sambuc less, greater, // < > 40*f4a2713aSLionel Sambuc colon, semi, // : ; 41*f4a2713aSLionel Sambuc comma, period, // , . 42*f4a2713aSLionel Sambuc equal, question, // = ? 43*f4a2713aSLionel Sambuc paste, // # 44*f4a2713aSLionel Sambuc 45*f4a2713aSLionel Sambuc // Keywords. 46*f4a2713aSLionel Sambuc Bit, Bits, Class, Code, Dag, Def, Foreach, Defm, Field, In, Int, Let, List, 47*f4a2713aSLionel Sambuc MultiClass, String, 48*f4a2713aSLionel Sambuc 49*f4a2713aSLionel Sambuc // !keywords. 50*f4a2713aSLionel Sambuc XConcat, XADD, XSRA, XSRL, XSHL, XStrConcat, XCast, XSubst, 51*f4a2713aSLionel Sambuc XForEach, XHead, XTail, XEmpty, XIf, XEq, 52*f4a2713aSLionel Sambuc 53*f4a2713aSLionel Sambuc // Integer value. 54*f4a2713aSLionel Sambuc IntVal, 55*f4a2713aSLionel Sambuc 56*f4a2713aSLionel Sambuc // String valued tokens. 57*f4a2713aSLionel Sambuc Id, StrVal, VarName, CodeFragment 58*f4a2713aSLionel Sambuc }; 59*f4a2713aSLionel Sambuc } 60*f4a2713aSLionel Sambuc 61*f4a2713aSLionel Sambuc /// TGLexer - TableGen Lexer class. 62*f4a2713aSLionel Sambuc class TGLexer { 63*f4a2713aSLionel Sambuc SourceMgr &SrcMgr; 64*f4a2713aSLionel Sambuc 65*f4a2713aSLionel Sambuc const char *CurPtr; 66*f4a2713aSLionel Sambuc const MemoryBuffer *CurBuf; 67*f4a2713aSLionel Sambuc 68*f4a2713aSLionel Sambuc // Information about the current token. 69*f4a2713aSLionel Sambuc const char *TokStart; 70*f4a2713aSLionel Sambuc tgtok::TokKind CurCode; 71*f4a2713aSLionel Sambuc std::string CurStrVal; // This is valid for ID, STRVAL, VARNAME, CODEFRAGMENT 72*f4a2713aSLionel Sambuc int64_t CurIntVal; // This is valid for INTVAL. 73*f4a2713aSLionel Sambuc 74*f4a2713aSLionel Sambuc /// CurBuffer - This is the current buffer index we're lexing from as managed 75*f4a2713aSLionel Sambuc /// by the SourceMgr object. 76*f4a2713aSLionel Sambuc int CurBuffer; 77*f4a2713aSLionel Sambuc 78*f4a2713aSLionel Sambuc public: 79*f4a2713aSLionel Sambuc typedef std::map<std::string, SMLoc> DependenciesMapTy; 80*f4a2713aSLionel Sambuc private: 81*f4a2713aSLionel Sambuc /// Dependencies - This is the list of all included files. 82*f4a2713aSLionel Sambuc DependenciesMapTy Dependencies; 83*f4a2713aSLionel Sambuc 84*f4a2713aSLionel Sambuc public: 85*f4a2713aSLionel Sambuc TGLexer(SourceMgr &SrcMgr); 86*f4a2713aSLionel Sambuc ~TGLexer() {} 87*f4a2713aSLionel Sambuc 88*f4a2713aSLionel Sambuc tgtok::TokKind Lex() { 89*f4a2713aSLionel Sambuc return CurCode = LexToken(); 90*f4a2713aSLionel Sambuc } 91*f4a2713aSLionel Sambuc 92*f4a2713aSLionel Sambuc const DependenciesMapTy &getDependencies() const { 93*f4a2713aSLionel Sambuc return Dependencies; 94*f4a2713aSLionel Sambuc } 95*f4a2713aSLionel Sambuc 96*f4a2713aSLionel Sambuc tgtok::TokKind getCode() const { return CurCode; } 97*f4a2713aSLionel Sambuc 98*f4a2713aSLionel Sambuc const std::string &getCurStrVal() const { 99*f4a2713aSLionel Sambuc assert((CurCode == tgtok::Id || CurCode == tgtok::StrVal || 100*f4a2713aSLionel Sambuc CurCode == tgtok::VarName || CurCode == tgtok::CodeFragment) && 101*f4a2713aSLionel Sambuc "This token doesn't have a string value"); 102*f4a2713aSLionel Sambuc return CurStrVal; 103*f4a2713aSLionel Sambuc } 104*f4a2713aSLionel Sambuc int64_t getCurIntVal() const { 105*f4a2713aSLionel Sambuc assert(CurCode == tgtok::IntVal && "This token isn't an integer"); 106*f4a2713aSLionel Sambuc return CurIntVal; 107*f4a2713aSLionel Sambuc } 108*f4a2713aSLionel Sambuc 109*f4a2713aSLionel Sambuc SMLoc getLoc() const; 110*f4a2713aSLionel Sambuc 111*f4a2713aSLionel Sambuc private: 112*f4a2713aSLionel Sambuc /// LexToken - Read the next token and return its code. 113*f4a2713aSLionel Sambuc tgtok::TokKind LexToken(); 114*f4a2713aSLionel Sambuc 115*f4a2713aSLionel Sambuc tgtok::TokKind ReturnError(const char *Loc, const Twine &Msg); 116*f4a2713aSLionel Sambuc 117*f4a2713aSLionel Sambuc int getNextChar(); 118*f4a2713aSLionel Sambuc int peekNextChar(int Index); 119*f4a2713aSLionel Sambuc void SkipBCPLComment(); 120*f4a2713aSLionel Sambuc bool SkipCComment(); 121*f4a2713aSLionel Sambuc tgtok::TokKind LexIdentifier(); 122*f4a2713aSLionel Sambuc bool LexInclude(); 123*f4a2713aSLionel Sambuc tgtok::TokKind LexString(); 124*f4a2713aSLionel Sambuc tgtok::TokKind LexVarName(); 125*f4a2713aSLionel Sambuc tgtok::TokKind LexNumber(); 126*f4a2713aSLionel Sambuc tgtok::TokKind LexBracket(); 127*f4a2713aSLionel Sambuc tgtok::TokKind LexExclaim(); 128*f4a2713aSLionel Sambuc }; 129*f4a2713aSLionel Sambuc 130*f4a2713aSLionel Sambuc } // end namespace llvm 131*f4a2713aSLionel Sambuc 132*f4a2713aSLionel Sambuc #endif 133