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"
22 /// AsmToken - Target independent representation for an assembler token.
40 LParen, RParen, LBrac, RBrac, LCurly, RCurly,
41 Star, Comma, Dollar, Equal, EqualEqual,
43 Pipe, PipePipe, Caret,
44 Amp, AmpAmp, Exclaim, ExclaimEqual, Percent, Hash,
45 Less, LessEqual, LessLess, LessGreater,
46 Greater, GreaterEqual, GreaterGreater
51 /// A reference to the entire token contents; this is always a pointer into
52 /// a memory buffer owned by the source manager.
59 AsmToken(TokenKind _Kind, const StringRef &_Str, int64_t _IntVal = 0)
60 : Kind(_Kind), Str(_Str), IntVal(_IntVal) {}
62 TokenKind getKind() const { return Kind; }
63 bool is(TokenKind K) const { return Kind == K; }
64 bool isNot(TokenKind K) const { return Kind != K; }
68 /// getStringContents - Get the contents of a string token (without quotes).
69 StringRef getStringContents() const {
70 assert(Kind == String && "This token isn't a string!");
71 return Str.slice(1, Str.size() - 1);
74 /// getIdentifier - Get the identifier string for the current token, which
75 /// should be an identifier or a string. This gets the portion of the string
76 /// which should be used as the identifier, e.g., it does not include the
77 /// quotes on strings.
78 StringRef getIdentifier() const {
79 if (Kind == Identifier)
81 return getStringContents();
84 /// getString - Get the string for the current token, this includes all
85 /// characters (for example, the quotes on strings) in the token.
87 /// The returned StringRef points into the source manager's memory buffer, and
88 /// is safe to store across calls to Lex().
89 StringRef getString() const { return Str; }
91 // FIXME: Don't compute this in advance, it makes every token larger, and is
92 // also not generally what we want (it is nicer for recovery etc. to lex 123br
93 // as a single token, then diagnose as an invalid number).
94 int64_t getIntVal() const {
95 assert(Kind == Integer && "This token isn't an integer!");
100 /// MCAsmLexer - Generic assembler lexer interface, for use by target specific
103 /// The current token, stored in the base class for faster access.
106 MCAsmLexer(const MCAsmLexer &); // DO NOT IMPLEMENT
107 void operator=(const MCAsmLexer &); // DO NOT IMPLEMENT
108 protected: // Can only create subclasses.
111 virtual AsmToken LexToken() = 0;
114 virtual ~MCAsmLexer();
116 /// Lex - Consume the next token from the input stream and return it.
118 /// The lexer will continuosly return the end-of-file token once the end of
119 /// the main input file has been reached.
120 const AsmToken &Lex() {
121 return CurTok = LexToken();
124 /// getTok - Get the current (last) lexed token.
125 const AsmToken &getTok() {
129 /// getKind - Get the kind of current token.
130 AsmToken::TokenKind getKind() const { return CurTok.getKind(); }
132 /// is - Check if the current token has kind \arg K.
133 bool is(AsmToken::TokenKind K) const { return CurTok.is(K); }
135 /// isNot - Check if the current token has kind \arg K.
136 bool isNot(AsmToken::TokenKind K) const { return CurTok.isNot(K); }
139 } // End llvm namespace