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.
48 LParen, RParen, LBrac, RBrac, LCurly, RCurly,
49 Star, Dot, Comma, Dollar, Equal, EqualEqual,
51 Pipe, PipePipe, Caret,
52 Amp, AmpAmp, Exclaim, ExclaimEqual, Percent, Hash,
53 Less, LessEqual, LessLess, LessGreater,
54 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; }
76 /// getStringContents - Get the contents of a string token (without quotes).
77 StringRef getStringContents() const {
78 assert(Kind == String && "This token isn't a string!");
79 return Str.slice(1, Str.size() - 1);
82 /// getIdentifier - Get the identifier string for the current token, which
83 /// should be an identifier or a string. This gets the portion of the string
84 /// which should be used as the identifier, e.g., it does not include the
85 /// quotes on strings.
86 StringRef getIdentifier() const {
87 if (Kind == Identifier)
89 return getStringContents();
92 /// getString - Get the string for the current token, this includes all
93 /// characters (for example, the quotes on strings) in the token.
95 /// The returned StringRef points into the source manager's memory buffer, and
96 /// is safe to store across calls to Lex().
97 StringRef getString() const { return Str; }
99 // FIXME: Don't compute this in advance, it makes every token larger, and is
100 // also not generally what we want (it is nicer for recovery etc. to lex 123br
101 // as a single token, then diagnose as an invalid number).
102 int64_t getIntVal() const {
103 assert(Kind == Integer && "This token isn't an integer!");
107 /// getRegVal - Get the register number for the current token, which should
109 unsigned getRegVal() const {
110 assert(Kind == Register && "This token isn't a register!");
111 return static_cast<unsigned>(IntVal);
115 /// MCAsmLexer - Generic assembler lexer interface, for use by target specific
118 /// The current token, stored in the base class for faster access.
121 /// The location and description of the current error
125 MCAsmLexer(const MCAsmLexer &); // DO NOT IMPLEMENT
126 void operator=(const MCAsmLexer &); // DO NOT IMPLEMENT
127 protected: // Can only create subclasses.
128 const char *TokStart;
132 virtual AsmToken LexToken() = 0;
134 void SetError(const SMLoc &errLoc, const std::string &err) {
140 virtual ~MCAsmLexer();
142 /// Lex - Consume the next token from the input stream and return it.
144 /// The lexer will continuosly return the end-of-file token once the end of
145 /// the main input file has been reached.
146 const AsmToken &Lex() {
147 return CurTok = LexToken();
150 virtual StringRef LexUntilEndOfStatement() = 0;
152 /// getLoc - Get the current source location.
153 SMLoc getLoc() const;
155 /// getTok - Get the current (last) lexed token.
156 const AsmToken &getTok() {
160 /// getErrLoc - Get the current error location
161 const SMLoc &getErrLoc() {
165 /// getErr - Get the current error string
166 const std::string &getErr() {
170 /// getKind - Get the kind of current token.
171 AsmToken::TokenKind getKind() const { return CurTok.getKind(); }
173 /// is - Check if the current token has kind \arg K.
174 bool is(AsmToken::TokenKind K) const { return CurTok.is(K); }
176 /// isNot - Check if the current token has kind \arg K.
177 bool isNot(AsmToken::TokenKind K) const { return CurTok.isNot(K); }
180 } // End llvm namespace