Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

177 lines
4.8 KiB

  1. //===-- llvm/MC/MCAsmLexer.h - Abstract Asm Lexer Interface -----*- 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. #ifndef LLVM_MC_MCPARSER_MCASMLEXER_H
  10. #define LLVM_MC_MCPARSER_MCASMLEXER_H
  11. #include "llvm/ADT/StringRef.h"
  12. #include "llvm/Support/Compiler.h"
  13. #include "llvm/Support/DataTypes.h"
  14. #include "llvm/Support/SMLoc.h"
  15. namespace llvm {
  16. /// AsmToken - Target independent representation for an assembler token.
  17. class AsmToken {
  18. public:
  19. enum TokenKind {
  20. // Markers
  21. Eof, Error,
  22. // String values.
  23. Identifier,
  24. String,
  25. // Integer values.
  26. Integer,
  27. // Real values.
  28. Real,
  29. // No-value.
  30. EndOfStatement,
  31. Colon,
  32. Space,
  33. Plus, Minus, Tilde,
  34. Slash, // '/'
  35. BackSlash, // '\'
  36. LParen, RParen, LBrac, RBrac, LCurly, RCurly,
  37. Star, Dot, Comma, Dollar, Equal, EqualEqual,
  38. Pipe, PipePipe, Caret,
  39. Amp, AmpAmp, Exclaim, ExclaimEqual, Percent, Hash,
  40. Less, LessEqual, LessLess, LessGreater,
  41. Greater, GreaterEqual, GreaterGreater, At
  42. };
  43. private:
  44. TokenKind Kind;
  45. /// A reference to the entire token contents; this is always a pointer into
  46. /// a memory buffer owned by the source manager.
  47. StringRef Str;
  48. int64_t IntVal;
  49. public:
  50. AsmToken() {}
  51. AsmToken(TokenKind _Kind, StringRef _Str, int64_t _IntVal = 0)
  52. : Kind(_Kind), Str(_Str), IntVal(_IntVal) {}
  53. TokenKind getKind() const { return Kind; }
  54. bool is(TokenKind K) const { return Kind == K; }
  55. bool isNot(TokenKind K) const { return Kind != K; }
  56. SMLoc getLoc() const;
  57. SMLoc getEndLoc() const;
  58. /// getStringContents - Get the contents of a string token (without quotes).
  59. StringRef getStringContents() const {
  60. assert(Kind == String && "This token isn't a string!");
  61. return Str.slice(1, Str.size() - 1);
  62. }
  63. /// getIdentifier - Get the identifier string for the current token, which
  64. /// should be an identifier or a string. This gets the portion of the string
  65. /// which should be used as the identifier, e.g., it does not include the
  66. /// quotes on strings.
  67. StringRef getIdentifier() const {
  68. if (Kind == Identifier)
  69. return getString();
  70. return getStringContents();
  71. }
  72. /// getString - Get the string for the current token, this includes all
  73. /// characters (for example, the quotes on strings) in the token.
  74. ///
  75. /// The returned StringRef points into the source manager's memory buffer, and
  76. /// is safe to store across calls to Lex().
  77. StringRef getString() const { return Str; }
  78. // FIXME: Don't compute this in advance, it makes every token larger, and is
  79. // also not generally what we want (it is nicer for recovery etc. to lex 123br
  80. // as a single token, then diagnose as an invalid number).
  81. int64_t getIntVal() const {
  82. assert(Kind == Integer && "This token isn't an integer!");
  83. return IntVal;
  84. }
  85. };
  86. /// MCAsmLexer - Generic assembler lexer interface, for use by target specific
  87. /// assembly lexers.
  88. class MCAsmLexer {
  89. /// The current token, stored in the base class for faster access.
  90. AsmToken CurTok;
  91. /// The location and description of the current error
  92. SMLoc ErrLoc;
  93. std::string Err;
  94. MCAsmLexer(const MCAsmLexer &) LLVM_DELETED_FUNCTION;
  95. void operator=(const MCAsmLexer &) LLVM_DELETED_FUNCTION;
  96. protected: // Can only create subclasses.
  97. const char *TokStart;
  98. bool SkipSpace;
  99. MCAsmLexer();
  100. virtual AsmToken LexToken() = 0;
  101. void SetError(const SMLoc &errLoc, const std::string &err) {
  102. ErrLoc = errLoc;
  103. Err = err;
  104. }
  105. public:
  106. virtual ~MCAsmLexer();
  107. /// Lex - Consume the next token from the input stream and return it.
  108. ///
  109. /// The lexer will continuosly return the end-of-file token once the end of
  110. /// the main input file has been reached.
  111. const AsmToken &Lex() {
  112. return CurTok = LexToken();
  113. }
  114. virtual StringRef LexUntilEndOfStatement() = 0;
  115. /// getLoc - Get the current source location.
  116. SMLoc getLoc() const;
  117. /// getTok - Get the current (last) lexed token.
  118. const AsmToken &getTok() {
  119. return CurTok;
  120. }
  121. /// getErrLoc - Get the current error location
  122. const SMLoc &getErrLoc() {
  123. return ErrLoc;
  124. }
  125. /// getErr - Get the current error string
  126. const std::string &getErr() {
  127. return Err;
  128. }
  129. /// getKind - Get the kind of current token.
  130. AsmToken::TokenKind getKind() const { return CurTok.getKind(); }
  131. /// is - Check if the current token has kind \p K.
  132. bool is(AsmToken::TokenKind K) const { return CurTok.is(K); }
  133. /// isNot - Check if the current token has kind \p K.
  134. bool isNot(AsmToken::TokenKind K) const { return CurTok.isNot(K); }
  135. /// setSkipSpace - Set whether spaces should be ignored by the lexer
  136. void setSkipSpace(bool val) { SkipSpace = val; }
  137. };
  138. } // End llvm namespace
  139. #endif