MCAsmLexer.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/MC/MCAsmLexer.h - Abstract Asm Lexer Interface ------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_MC_MCPARSER_MCASMLEXER_H
  14. #define LLVM_MC_MCPARSER_MCASMLEXER_H
  15. #include "llvm/ADT/ArrayRef.h"
  16. #include "llvm/ADT/SmallVector.h"
  17. #include "llvm/MC/MCAsmMacro.h"
  18. #include <cassert>
  19. #include <cstddef>
  20. #include <string>
  21. namespace llvm {
  22. /// A callback class which is notified of each comment in an assembly file as
  23. /// it is lexed.
  24. class AsmCommentConsumer {
  25. public:
  26. virtual ~AsmCommentConsumer() = default;
  27. /// Callback function for when a comment is lexed. Loc is the start of the
  28. /// comment text (excluding the comment-start marker). CommentText is the text
  29. /// of the comment, excluding the comment start and end markers, and the
  30. /// newline for single-line comments.
  31. virtual void HandleComment(SMLoc Loc, StringRef CommentText) = 0;
  32. };
  33. /// Generic assembler lexer interface, for use by target specific assembly
  34. /// lexers.
  35. class MCAsmLexer {
  36. /// The current token, stored in the base class for faster access.
  37. SmallVector<AsmToken, 1> CurTok;
  38. /// The location and description of the current error
  39. SMLoc ErrLoc;
  40. std::string Err;
  41. protected: // Can only create subclasses.
  42. const char *TokStart = nullptr;
  43. bool SkipSpace = true;
  44. bool AllowAtInIdentifier;
  45. bool AllowHashInIdentifier = false;
  46. bool IsAtStartOfStatement = true;
  47. bool LexMasmHexFloats = false;
  48. bool LexMasmIntegers = false;
  49. bool LexMasmStrings = false;
  50. bool LexMotorolaIntegers = false;
  51. bool UseMasmDefaultRadix = false;
  52. unsigned DefaultRadix = 10;
  53. bool LexHLASMIntegers = false;
  54. bool LexHLASMStrings = false;
  55. AsmCommentConsumer *CommentConsumer = nullptr;
  56. MCAsmLexer();
  57. virtual AsmToken LexToken() = 0;
  58. void SetError(SMLoc errLoc, const std::string &err) {
  59. ErrLoc = errLoc;
  60. Err = err;
  61. }
  62. public:
  63. MCAsmLexer(const MCAsmLexer &) = delete;
  64. MCAsmLexer &operator=(const MCAsmLexer &) = delete;
  65. virtual ~MCAsmLexer();
  66. /// Consume the next token from the input stream and return it.
  67. ///
  68. /// The lexer will continuously return the end-of-file token once the end of
  69. /// the main input file has been reached.
  70. const AsmToken &Lex() {
  71. assert(!CurTok.empty());
  72. // Mark if we parsing out a EndOfStatement.
  73. IsAtStartOfStatement = CurTok.front().getKind() == AsmToken::EndOfStatement;
  74. CurTok.erase(CurTok.begin());
  75. // LexToken may generate multiple tokens via UnLex but will always return
  76. // the first one. Place returned value at head of CurTok vector.
  77. if (CurTok.empty()) {
  78. AsmToken T = LexToken();
  79. CurTok.insert(CurTok.begin(), T);
  80. }
  81. return CurTok.front();
  82. }
  83. void UnLex(AsmToken const &Token) {
  84. IsAtStartOfStatement = false;
  85. CurTok.insert(CurTok.begin(), Token);
  86. }
  87. bool isAtStartOfStatement() { return IsAtStartOfStatement; }
  88. virtual StringRef LexUntilEndOfStatement() = 0;
  89. /// Get the current source location.
  90. SMLoc getLoc() const;
  91. /// Get the current (last) lexed token.
  92. const AsmToken &getTok() const {
  93. return CurTok[0];
  94. }
  95. /// Look ahead at the next token to be lexed.
  96. const AsmToken peekTok(bool ShouldSkipSpace = true) {
  97. AsmToken Tok;
  98. MutableArrayRef<AsmToken> Buf(Tok);
  99. size_t ReadCount = peekTokens(Buf, ShouldSkipSpace);
  100. assert(ReadCount == 1);
  101. (void)ReadCount;
  102. return Tok;
  103. }
  104. /// Look ahead an arbitrary number of tokens.
  105. virtual size_t peekTokens(MutableArrayRef<AsmToken> Buf,
  106. bool ShouldSkipSpace = true) = 0;
  107. /// Get the current error location
  108. SMLoc getErrLoc() {
  109. return ErrLoc;
  110. }
  111. /// Get the current error string
  112. const std::string &getErr() {
  113. return Err;
  114. }
  115. /// Get the kind of current token.
  116. AsmToken::TokenKind getKind() const { return getTok().getKind(); }
  117. /// Check if the current token has kind \p K.
  118. bool is(AsmToken::TokenKind K) const { return getTok().is(K); }
  119. /// Check if the current token has kind \p K.
  120. bool isNot(AsmToken::TokenKind K) const { return getTok().isNot(K); }
  121. /// Set whether spaces should be ignored by the lexer
  122. void setSkipSpace(bool val) { SkipSpace = val; }
  123. bool getAllowAtInIdentifier() { return AllowAtInIdentifier; }
  124. void setAllowAtInIdentifier(bool v) { AllowAtInIdentifier = v; }
  125. void setAllowHashInIdentifier(bool V) { AllowHashInIdentifier = V; }
  126. void setCommentConsumer(AsmCommentConsumer *CommentConsumer) {
  127. this->CommentConsumer = CommentConsumer;
  128. }
  129. /// Set whether to lex masm-style binary (e.g., 0b1101) and radix-specified
  130. /// literals (e.g., 0ABCh [hex], 576t [decimal], 77o [octal], 1101y [binary]).
  131. void setLexMasmIntegers(bool V) { LexMasmIntegers = V; }
  132. /// Set whether to use masm-style default-radix integer literals. If disabled,
  133. /// assume decimal unless prefixed (e.g., 0x2c [hex], 077 [octal]).
  134. void useMasmDefaultRadix(bool V) { UseMasmDefaultRadix = V; }
  135. unsigned getMasmDefaultRadix() const { return DefaultRadix; }
  136. void setMasmDefaultRadix(unsigned Radix) { DefaultRadix = Radix; }
  137. /// Set whether to lex masm-style hex float literals, such as 3f800000r.
  138. void setLexMasmHexFloats(bool V) { LexMasmHexFloats = V; }
  139. /// Set whether to lex masm-style string literals, such as 'Can''t find file'
  140. /// and "This ""value"" not found".
  141. void setLexMasmStrings(bool V) { LexMasmStrings = V; }
  142. /// Set whether to lex Motorola-style integer literals, such as $deadbeef or
  143. /// %01010110.
  144. void setLexMotorolaIntegers(bool V) { LexMotorolaIntegers = V; }
  145. /// Set whether to lex HLASM-flavour integers. For now this is only [0-9]*
  146. void setLexHLASMIntegers(bool V) { LexHLASMIntegers = V; }
  147. /// Set whether to "lex" HLASM-flavour character and string literals. For now,
  148. /// setting this option to true, will disable lexing for character and string
  149. /// literals.
  150. void setLexHLASMStrings(bool V) { LexHLASMStrings = V; }
  151. };
  152. } // end namespace llvm
  153. #endif // LLVM_MC_MCPARSER_MCASMLEXER_H
  154. #ifdef __GNUC__
  155. #pragma GCC diagnostic pop
  156. #endif