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/DataTypes.h"
15 #include "llvm/Support/SMLoc.h"
22 /// AsmToken - Target independent representation for an assembler token.
39 // Register values (stored in IntVal). Only used by TargetAsmLexer.
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
58 /// A reference to the entire token contents; this is always a pointer into
59 /// a memory buffer owned by the source manager.
66 AsmToken(TokenKind _Kind, StringRef _Str, int64_t _IntVal = 0)
67 : Kind(_Kind), Str(_Str), IntVal(_IntVal) {}
69 TokenKind getKind() const { return Kind; }
70 bool is(TokenKind K) const { return Kind == K; }
71 bool isNot(TokenKind K) const { return Kind != K; }
75 /// getStringContents - Get the contents of a string token (without quotes).
76 StringRef getStringContents() const {
77 assert(Kind == String && "This token isn't a string!");
78 return Str.slice(1, Str.size() - 1);
81 /// getIdentifier - Get the identifier string for the current token, which
82 /// should be an identifier or a string. This gets the portion of the string
83 /// which should be used as the identifier, e.g., it does not include the
84 /// quotes on strings.
85 StringRef getIdentifier() const {
86 if (Kind == Identifier)
88 return getStringContents();
91 /// getString - Get the string for the current token, this includes all
92 /// characters (for example, the quotes on strings) in the token.
94 /// The returned StringRef points into the source manager's memory buffer, and
95 /// is safe to store across calls to Lex().
96 StringRef getString() const { return Str; }
98 // FIXME: Don't compute this in advance, it makes every token larger, and is
99 // also not generally what we want (it is nicer for recovery etc. to lex 123br
100 // as a single token, then diagnose as an invalid number).
101 int64_t getIntVal() const {
102 assert(Kind == Integer && "This token isn't an integer!");
106 /// getRegVal - Get the register number for the current token, which should
108 unsigned getRegVal() const {
109 assert(Kind == Register && "This token isn't a register!");
110 return static_cast<unsigned>(IntVal);
114 /// MCAsmLexer - Generic assembler lexer interface, for use by target specific
117 /// The current token, stored in the base class for faster access.
120 /// The location and description of the current error
124 MCAsmLexer(const MCAsmLexer &); // DO NOT IMPLEMENT
125 void operator=(const MCAsmLexer &); // DO NOT IMPLEMENT
126 protected: // Can only create subclasses.
127 const char *TokStart;
131 virtual AsmToken LexToken() = 0;
133 void SetError(const SMLoc &errLoc, const std::string &err) {
139 virtual ~MCAsmLexer();
141 /// Lex - Consume the next token from the input stream and return it.
143 /// The lexer will continuosly return the end-of-file token once the end of
144 /// the main input file has been reached.
145 const AsmToken &Lex() {
146 return CurTok = LexToken();
149 virtual StringRef LexUntilEndOfStatement() = 0;
151 /// getLoc - Get the current source location.
152 SMLoc getLoc() const;
154 /// getTok - Get the current (last) lexed token.
155 const AsmToken &getTok() {
159 /// getErrLoc - Get the current error location
160 const SMLoc &getErrLoc() {
164 /// getErr - Get the current error string
165 const std::string &getErr() {
169 /// getKind - Get the kind of current token.
170 AsmToken::TokenKind getKind() const { return CurTok.getKind(); }
172 /// is - Check if the current token has kind \arg K.
173 bool is(AsmToken::TokenKind K) const { return CurTok.is(K); }
175 /// isNot - Check if the current token has kind \arg K.
176 bool isNot(AsmToken::TokenKind K) const { return CurTok.isNot(K); }
179 } // End llvm namespace