Drop some AsmLexer methods in favor of their AsmToken equivalents.
[oota-llvm.git] / tools / llvm-mc / AsmLexer.h
1 //===- AsmLexer.h - Lexer for Assembly Files --------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This class declares the lexer for assembly files.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef ASMLEXER_H
15 #define ASMLEXER_H
16
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/MC/MCAsmLexer.h"
19 #include "llvm/Support/DataTypes.h"
20 #include <string>
21 #include <cassert>
22
23 namespace llvm {
24 class MemoryBuffer;
25 class SourceMgr;
26 class SMLoc;
27
28 /// AsmToken - Target independent representation for an assembler token.
29 struct AsmToken {
30   enum TokenKind {
31     // Markers
32     Eof, Error,
33
34     // String values.
35     Identifier,
36     Register,
37     String,
38     
39     // Integer values.
40     Integer,
41     
42     // No-value.
43     EndOfStatement,
44     Colon,
45     Plus, Minus, Tilde,
46     Slash,    // '/'
47     LParen, RParen,
48     Star, Comma, Dollar, Equal, EqualEqual,
49     
50     Pipe, PipePipe, Caret, 
51     Amp, AmpAmp, Exclaim, ExclaimEqual, Percent, 
52     Less, LessEqual, LessLess, LessGreater,
53     Greater, GreaterEqual, GreaterGreater
54   };
55
56   TokenKind Kind;
57
58   /// A reference to the entire token contents; this is always a pointer into
59   /// a memory buffer owned by the source manager.
60   StringRef Str;
61
62   int64_t IntVal;
63
64 public:
65   AsmToken() {}
66   AsmToken(TokenKind _Kind, const StringRef &_Str, int64_t _IntVal = 0)
67     : Kind(_Kind), Str(_Str), IntVal(_IntVal) {}
68
69   TokenKind getKind() const { return Kind; }
70   bool is(TokenKind K) const { return Kind == K; }
71   bool isNot(TokenKind K) const { return Kind != K; }
72
73   SMLoc getLoc() const;
74
75   /// getString - Get the string for the current token, this includes all
76   /// characters (for example, the quotes on strings) in the token.
77   ///
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; }
81
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");
87     return IntVal; 
88   }
89 };
90
91 /// AsmLexer - Lexer class for assembly files.
92 class AsmLexer : public MCAsmLexer {
93   SourceMgr &SrcMgr;
94   
95   const char *CurPtr;
96   const MemoryBuffer *CurBuf;
97   
98   const char *TokStart;
99
100   /// The current token.
101   AsmToken CurTok;
102   
103   /// This is the current buffer index we're lexing from as managed by the
104   /// SourceMgr object.
105   int CurBuffer;
106   
107   void operator=(const AsmLexer&); // DO NOT IMPLEMENT
108   AsmLexer(const AsmLexer&);       // DO NOT IMPLEMENT
109 public:
110   AsmLexer(SourceMgr &SrcMgr);
111   ~AsmLexer();
112   
113   AsmToken::TokenKind Lex() {
114     return CurTok = LexToken(), getKind();
115   }
116   
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); }
120
121   SMLoc getLoc() const;
122   
123   /// getTok - Return a reference to the current (last) lexed token.
124   const AsmToken &getTok() const { return CurTok; }
125   
126   /// EnterIncludeFile - Enter the specified file. This returns true on failure.
127   bool EnterIncludeFile(const std::string &Filename);
128   
129   void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type) const;
130   
131 private:
132   int getNextChar();
133   AsmToken ReturnError(const char *Loc, const std::string &Msg);
134
135   /// LexToken - Read the next token and return its code.
136   AsmToken LexToken();
137   AsmToken LexIdentifier();
138   AsmToken LexPercent();
139   AsmToken LexSlash();
140   AsmToken LexLineComment();
141   AsmToken LexDigit();
142   AsmToken LexQuote();
143 };
144   
145 } // end namespace llvm
146
147 #endif