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