llvm-mc: Sink token enum into AsmToken.
[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   StringRef getString() const { return Str; }
76
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");
82     return IntVal; 
83   }
84 };
85
86 /// AsmLexer - Lexer class for assembly files.
87 class AsmLexer : public MCAsmLexer {
88   SourceMgr &SrcMgr;
89   
90   const char *CurPtr;
91   const MemoryBuffer *CurBuf;
92   
93   const char *TokStart;
94
95   /// The current token.
96   AsmToken CurTok;
97   
98   /// This is the current buffer index we're lexing from as managed by the
99   /// SourceMgr object.
100   int CurBuffer;
101   
102   void operator=(const AsmLexer&); // DO NOT IMPLEMENT
103   AsmLexer(const AsmLexer&);       // DO NOT IMPLEMENT
104 public:
105   AsmLexer(SourceMgr &SrcMgr);
106   ~AsmLexer();
107   
108   AsmToken::TokenKind Lex() {
109     return CurTok = LexToken(), getKind();
110   }
111   
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); }
115
116   /// getCurStrVal - Get the string for the current token, this includes all
117   /// characters (for example, the quotes on strings) in the token.
118   ///
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();
123   }
124   int64_t getCurIntVal() const {
125     return CurTok.getIntVal();
126   }
127   
128   SMLoc getLoc() const;
129
130   const AsmToken &getTok() const;
131   
132   /// EnterIncludeFile - Enter the specified file. This returns true on failure.
133   bool EnterIncludeFile(const std::string &Filename);
134   
135   void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type) const;
136   
137 private:
138   int getNextChar();
139   AsmToken ReturnError(const char *Loc, const std::string &Msg);
140
141   /// LexToken - Read the next token and return its code.
142   AsmToken LexToken();
143   AsmToken LexIdentifier();
144   AsmToken LexPercent();
145   AsmToken LexSlash();
146   AsmToken LexLineComment();
147   AsmToken LexDigit();
148   AsmToken LexQuote();
149 };
150   
151 } // end namespace llvm
152
153 #endif