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/System/DataTypes.h"
15 #include "llvm/Support/SMLoc.h"
22 /// AsmToken - Target independent representation for an assembler token.
36 // Register values (stored in IntVal). Only used by TargetAsmLexer.
44 LParen, RParen, LBrac, RBrac, LCurly, RCurly,
45 Star, Dot, Comma, Dollar, Equal, EqualEqual,
47 Pipe, PipePipe, Caret,
48 Amp, AmpAmp, Exclaim, ExclaimEqual, Percent, Hash,
49 Less, LessEqual, LessLess, LessGreater,
50 Greater, GreaterEqual, GreaterGreater
55 /// A reference to the entire token contents; this is always a pointer into
56 /// a memory buffer owned by the source manager.
63 AsmToken(TokenKind _Kind, StringRef _Str, int64_t _IntVal = 0)
64 : Kind(_Kind), Str(_Str), IntVal(_IntVal) {}
66 TokenKind getKind() const { return Kind; }
67 bool is(TokenKind K) const { return Kind == K; }
68 bool isNot(TokenKind K) const { return Kind != K; }
72 /// getStringContents - Get the contents of a string token (without quotes).
73 StringRef getStringContents() const {
74 assert(Kind == String && "This token isn't a string!");
75 return Str.slice(1, Str.size() - 1);
78 /// getIdentifier - Get the identifier string for the current token, which
79 /// should be an identifier or a string. This gets the portion of the string
80 /// which should be used as the identifier, e.g., it does not include the
81 /// quotes on strings.
82 StringRef getIdentifier() const {
83 if (Kind == Identifier)
85 return getStringContents();
88 /// getString - Get the string for the current token, this includes all
89 /// characters (for example, the quotes on strings) in the token.
91 /// The returned StringRef points into the source manager's memory buffer, and
92 /// is safe to store across calls to Lex().
93 StringRef getString() const { return Str; }
95 // FIXME: Don't compute this in advance, it makes every token larger, and is
96 // also not generally what we want (it is nicer for recovery etc. to lex 123br
97 // as a single token, then diagnose as an invalid number).
98 int64_t getIntVal() const {
99 assert(Kind == Integer && "This token isn't an integer!");
103 /// getRegVal - Get the register number for the current token, which should
105 unsigned getRegVal() const {
106 assert(Kind == Register && "This token isn't a register!");
107 return static_cast<unsigned>(IntVal);
111 /// MCAsmLexer - Generic assembler lexer interface, for use by target specific
114 /// The current token, stored in the base class for faster access.
117 /// The location and description of the current error
121 MCAsmLexer(const MCAsmLexer &); // DO NOT IMPLEMENT
122 void operator=(const MCAsmLexer &); // DO NOT IMPLEMENT
123 protected: // Can only create subclasses.
126 virtual AsmToken LexToken() = 0;
128 void SetError(const SMLoc &errLoc, const std::string &err) {
134 virtual ~MCAsmLexer();
136 /// Lex - Consume the next token from the input stream and return it.
138 /// The lexer will continuosly return the end-of-file token once the end of
139 /// the main input file has been reached.
140 const AsmToken &Lex() {
141 return CurTok = LexToken();
144 /// getTok - Get the current (last) lexed token.
145 const AsmToken &getTok() {
149 /// getErrLoc - Get the current error location
150 const SMLoc &getErrLoc() {
154 /// getErr - Get the current error string
155 const std::string &getErr() {
159 /// getKind - Get the kind of current token.
160 AsmToken::TokenKind getKind() const { return CurTok.getKind(); }
162 /// is - Check if the current token has kind \arg K.
163 bool is(AsmToken::TokenKind K) const { return CurTok.is(K); }
165 /// isNot - Check if the current token has kind \arg K.
166 bool isNot(AsmToken::TokenKind K) const { return CurTok.isNot(K); }
169 } // End llvm namespace