TGLexer.h 3.6 KB

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