LLVM API Documentation

TGLexer.h
Go to the documentation of this file.
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 LLVM_LIB_TABLEGEN_TGLEXER_H
00015 #define LLVM_LIB_TABLEGEN_TGLEXER_H
00016 
00017 #include "llvm/ADT/StringRef.h"
00018 #include "llvm/Support/DataTypes.h"
00019 #include "llvm/Support/SMLoc.h"
00020 #include <cassert>
00021 #include <map>
00022 #include <string>
00023 
00024 namespace llvm {
00025 class SourceMgr;
00026 class SMLoc;
00027 class Twine;
00028 
00029 namespace tgtok {
00030   enum TokKind {
00031     // Markers
00032     Eof, Error,
00033     
00034     // Tokens with no info.
00035     minus, plus,        // - +
00036     l_square, r_square, // [ ]
00037     l_brace, r_brace,   // { }
00038     l_paren, r_paren,   // ( )
00039     less, greater,      // < >
00040     colon, semi,        // : ;
00041     comma, period,      // , .
00042     equal, question,    // = ?
00043     paste,              // #
00044 
00045     // Keywords.
00046     Bit, Bits, Class, Code, Dag, Def, Foreach, Defm, Field, In, Int, Let, List,
00047     MultiClass, String,
00048     
00049     // !keywords.
00050     XConcat, XADD, XAND, XSRA, XSRL, XSHL, XListConcat, XStrConcat, XCast,
00051     XSubst, XForEach, XHead, XTail, XEmpty, XIf, XEq,
00052 
00053     // Integer value.
00054     IntVal,
00055 
00056     // Binary constant.  Note that these are sized according to the number of
00057     // bits given.
00058     BinaryIntVal,
00059     
00060     // String valued tokens.
00061     Id, StrVal, VarName, CodeFragment
00062   };
00063 }
00064 
00065 /// TGLexer - TableGen Lexer class.
00066 class TGLexer {
00067   SourceMgr &SrcMgr;
00068   
00069   const char *CurPtr;
00070   StringRef CurBuf;
00071 
00072   // Information about the current token.
00073   const char *TokStart;
00074   tgtok::TokKind CurCode;
00075   std::string CurStrVal;  // This is valid for ID, STRVAL, VARNAME, CODEFRAGMENT
00076   int64_t CurIntVal;      // This is valid for INTVAL.
00077 
00078   /// CurBuffer - This is the current buffer index we're lexing from as managed
00079   /// by the SourceMgr object.
00080   unsigned CurBuffer;
00081 
00082 public:
00083   typedef std::map<std::string, SMLoc> DependenciesMapTy;
00084 private:
00085   /// Dependencies - This is the list of all included files.
00086   DependenciesMapTy Dependencies;
00087 
00088 public:
00089   TGLexer(SourceMgr &SrcMgr);
00090   ~TGLexer() {}
00091   
00092   tgtok::TokKind Lex() {
00093     return CurCode = LexToken();
00094   }
00095 
00096   const DependenciesMapTy &getDependencies() const {
00097     return Dependencies;
00098   }
00099   
00100   tgtok::TokKind getCode() const { return CurCode; }
00101 
00102   const std::string &getCurStrVal() const {
00103     assert((CurCode == tgtok::Id || CurCode == tgtok::StrVal || 
00104             CurCode == tgtok::VarName || CurCode == tgtok::CodeFragment) &&
00105            "This token doesn't have a string value");
00106     return CurStrVal;
00107   }
00108   int64_t getCurIntVal() const {
00109     assert(CurCode == tgtok::IntVal && "This token isn't an integer");
00110     return CurIntVal;
00111   }
00112   std::pair<int64_t, unsigned> getCurBinaryIntVal() const {
00113     assert(CurCode == tgtok::BinaryIntVal &&
00114            "This token isn't a binary integer");
00115     return std::make_pair(CurIntVal, (CurPtr - TokStart)-2);
00116   }
00117 
00118   SMLoc getLoc() const;
00119   
00120 private:
00121   /// LexToken - Read the next token and return its code.
00122   tgtok::TokKind LexToken();
00123   
00124   tgtok::TokKind ReturnError(const char *Loc, const Twine &Msg);
00125   
00126   int getNextChar();
00127   int peekNextChar(int Index);
00128   void SkipBCPLComment();
00129   bool SkipCComment();
00130   tgtok::TokKind LexIdentifier();
00131   bool LexInclude();
00132   tgtok::TokKind LexString();
00133   tgtok::TokKind LexVarName();
00134   tgtok::TokKind LexNumber();
00135   tgtok::TokKind LexBracket();
00136   tgtok::TokKind LexExclaim();
00137 };
00138   
00139 } // end namespace llvm
00140 
00141 #endif