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