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 /// getString - Get the string for the current token, this includes all
76 /// characters (for example, the quotes on strings) in the token.
78 /// The returned StringRef points into the source manager's memory buffer, and
79 /// is safe to store across calls to Lex().
80 StringRef getString() const { return Str; }
82 // FIXME: Don't compute this in advance, it makes every token larger, and is
83 // also not generally what we want (it is nicer for recovery etc. to lex 123br
84 // as a single token, then diagnose as an invalid number).
85 int64_t getIntVal() const {
86 assert(Kind == Integer && "This token isn't an integer");
91 /// AsmLexer - Lexer class for assembly files.
92 class AsmLexer : public MCAsmLexer {
96 const MemoryBuffer *CurBuf;
100 /// The current token.
103 /// This is the current buffer index we're lexing from as managed by the
104 /// SourceMgr object.
107 void operator=(const AsmLexer&); // DO NOT IMPLEMENT
108 AsmLexer(const AsmLexer&); // DO NOT IMPLEMENT
110 AsmLexer(SourceMgr &SrcMgr);
113 const AsmToken &Lex() {
114 return CurTok = LexToken();
117 AsmToken::TokenKind getKind() const { return CurTok.getKind(); }
118 bool is(AsmToken::TokenKind K) const { return CurTok.is(K); }
119 bool isNot(AsmToken::TokenKind K) const { return CurTok.isNot(K); }
121 SMLoc getLoc() const;
123 /// getTok - Return a reference to the current (last) lexed token.
124 const AsmToken &getTok() const { return CurTok; }
126 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
127 bool EnterIncludeFile(const std::string &Filename);
129 void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type) const;
133 AsmToken ReturnError(const char *Loc, const std::string &Msg);
135 /// LexToken - Read the next token and return its code.
137 AsmToken LexIdentifier();
138 AsmToken LexPercent();
140 AsmToken LexLineComment();
145 } // end namespace llvm