e60fa201e5e52507f07d8def12fc76bd92878c1b
[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 namespace asmtok {
29   enum TokKind {
30     // Markers
31     Eof, Error,
32
33     // String values.
34     Identifier,
35     Register,
36     String,
37     
38     // Integer values.
39     IntVal,
40     
41     // No-value.
42     EndOfStatement,
43     Colon,
44     Plus, Minus, Tilde,
45     Slash,    // '/'
46     LParen, RParen,
47     Star, Comma, Dollar, Equal, EqualEqual,
48     
49     Pipe, PipePipe, Caret, 
50     Amp, AmpAmp, Exclaim, ExclaimEqual, Percent, 
51     Less, LessEqual, LessLess, LessGreater,
52     Greater, GreaterEqual, GreaterGreater
53   };
54 }
55
56 /// AsmToken - Target independent representation for an assembler token.
57 struct AsmToken {
58   asmtok::TokKind Kind;
59
60   /// A reference to the entire token contents; this is always a pointer into
61   /// a memory buffer owned by the source manager.
62   StringRef Str;
63
64   int64_t IntVal;
65
66 public:
67   AsmToken() {}
68   AsmToken(asmtok::TokKind _Kind, const StringRef &_Str, int64_t _IntVal = 0)
69     : Kind(_Kind), Str(_Str), IntVal(_IntVal) {}
70
71   asmtok::TokKind getKind() const { return Kind; }
72   bool is(asmtok::TokKind K) const { return Kind == K; }
73   bool isNot(asmtok::TokKind K) const { return Kind != K; }
74
75   SMLoc getLoc() const;
76
77   StringRef getString() const { return Str; }
78
79   int64_t getIntVal() const { 
80     assert(Kind == asmtok::IntVal && "This token isn't an integer");
81     return IntVal; 
82   }
83 };
84
85 /// AsmLexer - Lexer class for assembly files.
86 class AsmLexer : public MCAsmLexer {
87   SourceMgr &SrcMgr;
88   
89   const char *CurPtr;
90   const MemoryBuffer *CurBuf;
91   
92   const char *TokStart;
93
94   /// The current token.
95   AsmToken CurTok;
96   
97   /// This is the current buffer index we're lexing from as managed by the
98   /// SourceMgr object.
99   int CurBuffer;
100   
101   void operator=(const AsmLexer&); // DO NOT IMPLEMENT
102   AsmLexer(const AsmLexer&);       // DO NOT IMPLEMENT
103 public:
104   AsmLexer(SourceMgr &SrcMgr);
105   ~AsmLexer();
106   
107   asmtok::TokKind Lex() {
108     return CurTok = LexToken(), getKind();
109   }
110   
111   asmtok::TokKind getKind() const { return CurTok.getKind(); }
112   bool is(asmtok::TokKind K) const { return CurTok.is(K); }
113   bool isNot(asmtok::TokKind K) const { return CurTok.isNot(K); }
114
115   /// getCurStrVal - Get the string for the current token, this includes all
116   /// characters (for example, the quotes on strings) in the token.
117   ///
118   /// The returned StringRef points into the source manager's memory buffer, and
119   /// is safe to store across calls to Lex().
120   StringRef getCurStrVal() const {
121     return CurTok.getString();
122   }
123   int64_t getCurIntVal() const {
124     return CurTok.getIntVal();
125   }
126   
127   SMLoc getLoc() const;
128   
129   /// EnterIncludeFile - Enter the specified file. This returns true on failure.
130   bool EnterIncludeFile(const std::string &Filename);
131   
132   void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type) const;
133   
134 private:
135   int getNextChar();
136   AsmToken ReturnError(const char *Loc, const std::string &Msg);
137
138   /// LexToken - Read the next token and return its code.
139   AsmToken LexToken();
140   AsmToken LexIdentifier();
141   AsmToken LexPercent();
142   AsmToken LexSlash();
143   AsmToken LexLineComment();
144   AsmToken LexDigit();
145   AsmToken LexQuote();
146 };
147   
148 } // end namespace llvm
149
150 #endif