1 //===- AsmLexer.h - Lexer for Assembly Files --------------------*- 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 // This class declares the lexer for assembly files.
12 //===----------------------------------------------------------------------===//
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/MC/MCAsmLexer.h"
19 #include "llvm/Support/DataTypes.h"
28 /// AsmToken - Target independent representation for an assembler token.
48 Star, Comma, Dollar, Equal, EqualEqual,
50 Pipe, PipePipe, Caret,
51 Amp, AmpAmp, Exclaim, ExclaimEqual, Percent,
52 Less, LessEqual, LessLess, LessGreater,
53 Greater, GreaterEqual, GreaterGreater
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, const 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 StringRef getString() const { return Str; }
77 // FIXME: Don't compute this in advance, it makes every token larger, and is
78 // also not generally what we want (it is nicer for recovery etc. to lex 123br
79 // as a single token, then diagnose as an invalid number).
80 int64_t getIntVal() const {
81 assert(Kind == Integer && "This token isn't an integer");
86 /// AsmLexer - Lexer class for assembly files.
87 class AsmLexer : public MCAsmLexer {
91 const MemoryBuffer *CurBuf;
95 /// The current token.
98 /// This is the current buffer index we're lexing from as managed by the
102 void operator=(const AsmLexer&); // DO NOT IMPLEMENT
103 AsmLexer(const AsmLexer&); // DO NOT IMPLEMENT
105 AsmLexer(SourceMgr &SrcMgr);
108 AsmToken::TokenKind Lex() {
109 return CurTok = LexToken(), getKind();
112 AsmToken::TokenKind getKind() const { return CurTok.getKind(); }
113 bool is(AsmToken::TokenKind K) const { return CurTok.is(K); }
114 bool isNot(AsmToken::TokenKind K) const { return CurTok.isNot(K); }
116 /// getCurStrVal - Get the string for the current token, this includes all
117 /// characters (for example, the quotes on strings) in the token.
119 /// The returned StringRef points into the source manager's memory buffer, and
120 /// is safe to store across calls to Lex().
121 StringRef getCurStrVal() const {
122 return CurTok.getString();
124 int64_t getCurIntVal() const {
125 return CurTok.getIntVal();
128 SMLoc getLoc() const;
130 const AsmToken &getTok() const;
132 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
133 bool EnterIncludeFile(const std::string &Filename);
135 void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type) const;
139 AsmToken ReturnError(const char *Loc, const std::string &Msg);
141 /// LexToken - Read the next token and return its code.
143 AsmToken LexIdentifier();
144 AsmToken LexPercent();
146 AsmToken LexLineComment();
151 } // end namespace llvm