1 //===-- llvm/MC/MCAsmLexer.h - Abstract Asm Lexer Interface -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_MC_MCASMLEXER_H
11 #define LLVM_MC_MCASMLEXER_H
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/Support/Compiler.h"
15 #include "llvm/Support/DataTypes.h"
16 #include "llvm/Support/SMLoc.h"
20 /// AsmToken - Target independent representation for an assembler token.
37 // Register values (stored in IntVal). Only used by MCTargetAsmLexer.
47 LParen, RParen, LBrac, RBrac, LCurly, RCurly,
48 Star, Dot, Comma, Dollar, Equal, EqualEqual,
50 Pipe, PipePipe, Caret,
51 Amp, AmpAmp, Exclaim, ExclaimEqual, Percent, Hash,
52 Less, LessEqual, LessLess, LessGreater,
53 Greater, GreaterEqual, GreaterGreater, At
59 /// A reference to the entire token contents; this is always a pointer into
60 /// a memory buffer owned by the source manager.
67 AsmToken(TokenKind _Kind, StringRef _Str, int64_t _IntVal = 0)
68 : Kind(_Kind), Str(_Str), IntVal(_IntVal) {}
70 TokenKind getKind() const { return Kind; }
71 bool is(TokenKind K) const { return Kind == K; }
72 bool isNot(TokenKind K) const { return Kind != K; }
75 SMLoc getEndLoc() const;
77 /// getStringContents - Get the contents of a string token (without quotes).
78 StringRef getStringContents() const {
79 assert(Kind == String && "This token isn't a string!");
80 return Str.slice(1, Str.size() - 1);
83 /// getIdentifier - Get the identifier string for the current token, which
84 /// should be an identifier or a string. This gets the portion of the string
85 /// which should be used as the identifier, e.g., it does not include the
86 /// quotes on strings.
87 StringRef getIdentifier() const {
88 if (Kind == Identifier)
90 return getStringContents();
93 /// getString - Get the string for the current token, this includes all
94 /// characters (for example, the quotes on strings) in the token.
96 /// The returned StringRef points into the source manager's memory buffer, and
97 /// is safe to store across calls to Lex().
98 StringRef getString() const { return Str; }
100 // FIXME: Don't compute this in advance, it makes every token larger, and is
101 // also not generally what we want (it is nicer for recovery etc. to lex 123br
102 // as a single token, then diagnose as an invalid number).
103 int64_t getIntVal() const {
104 assert(Kind == Integer && "This token isn't an integer!");
108 /// getRegVal - Get the register number for the current token, which should
110 unsigned getRegVal() const {
111 assert(Kind == Register && "This token isn't a register!");
112 return static_cast<unsigned>(IntVal);
116 /// MCAsmLexer - Generic assembler lexer interface, for use by target specific
119 /// The current token, stored in the base class for faster access.
122 /// The location and description of the current error
126 MCAsmLexer(const MCAsmLexer &) LLVM_DELETED_FUNCTION;
127 void operator=(const MCAsmLexer &) LLVM_DELETED_FUNCTION;
128 protected: // Can only create subclasses.
129 const char *TokStart;
134 virtual AsmToken LexToken() = 0;
136 void SetError(const SMLoc &errLoc, const std::string &err) {
142 virtual ~MCAsmLexer();
144 /// Lex - Consume the next token from the input stream and return it.
146 /// The lexer will continuosly return the end-of-file token once the end of
147 /// the main input file has been reached.
148 const AsmToken &Lex() {
149 return CurTok = LexToken();
152 virtual StringRef LexUntilEndOfStatement() = 0;
154 /// getLoc - Get the current source location.
155 SMLoc getLoc() const;
157 /// getTok - Get the current (last) lexed token.
158 const AsmToken &getTok() {
162 /// getErrLoc - Get the current error location
163 const SMLoc &getErrLoc() {
167 /// getErr - Get the current error string
168 const std::string &getErr() {
172 /// getKind - Get the kind of current token.
173 AsmToken::TokenKind getKind() const { return CurTok.getKind(); }
175 /// is - Check if the current token has kind \p K.
176 bool is(AsmToken::TokenKind K) const { return CurTok.is(K); }
178 /// isNot - Check if the current token has kind \p K.
179 bool isNot(AsmToken::TokenKind K) const { return CurTok.isNot(K); }
181 /// setSkipSpace - Set whether spaces should be ignored by the lexer
182 void setSkipSpace(bool val) { SkipSpace = val; }
185 } // End llvm namespace