create a new MCParser library and move some stuff into it.
[oota-llvm.git] / include / llvm / MC / MCParser / MCAsmLexer.h
1 //===-- llvm/MC/MCAsmLexer.h - Abstract Asm Lexer Interface -----*- 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 #ifndef LLVM_MC_MCASMLEXER_H
11 #define LLVM_MC_MCASMLEXER_H
12
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/System/DataTypes.h"
15 #include "llvm/Support/SMLoc.h"
16
17 namespace llvm {
18 class MCAsmLexer;
19 class MCInst;
20 class Target;
21
22 /// AsmToken - Target independent representation for an assembler token.
23 class AsmToken {
24 public:
25   enum TokenKind {
26     // Markers
27     Eof, Error,
28
29     // String values.
30     Identifier,
31     String,
32     
33     // Integer values.
34     Integer,
35     
36     // No-value.
37     EndOfStatement,
38     Colon,
39     Plus, Minus, Tilde,
40     Slash,    // '/'
41     LParen, RParen, LBrac, RBrac, LCurly, RCurly,
42     Star, Comma, Dollar, Equal, EqualEqual,
43     
44     Pipe, PipePipe, Caret, 
45     Amp, AmpAmp, Exclaim, ExclaimEqual, Percent, Hash,
46     Less, LessEqual, LessLess, LessGreater,
47     Greater, GreaterEqual, GreaterGreater
48   };
49
50   TokenKind Kind;
51
52   /// A reference to the entire token contents; this is always a pointer into
53   /// a memory buffer owned by the source manager.
54   StringRef Str;
55
56   int64_t IntVal;
57
58 public:
59   AsmToken() {}
60   AsmToken(TokenKind _Kind, StringRef _Str, int64_t _IntVal = 0)
61     : Kind(_Kind), Str(_Str), IntVal(_IntVal) {}
62
63   TokenKind getKind() const { return Kind; }
64   bool is(TokenKind K) const { return Kind == K; }
65   bool isNot(TokenKind K) const { return Kind != K; }
66
67   SMLoc getLoc() const;
68
69   /// getStringContents - Get the contents of a string token (without quotes).
70   StringRef getStringContents() const { 
71     assert(Kind == String && "This token isn't a string!");
72     return Str.slice(1, Str.size() - 1);
73   }
74
75   /// getIdentifier - Get the identifier string for the current token, which
76   /// should be an identifier or a string. This gets the portion of the string
77   /// which should be used as the identifier, e.g., it does not include the
78   /// quotes on strings.
79   StringRef getIdentifier() const {
80     if (Kind == Identifier)
81       return getString();
82     return getStringContents();
83   }
84
85   /// getString - Get the string for the current token, this includes all
86   /// characters (for example, the quotes on strings) in the token.
87   ///
88   /// The returned StringRef points into the source manager's memory buffer, and
89   /// is safe to store across calls to Lex().
90   StringRef getString() const { return Str; }
91
92   // FIXME: Don't compute this in advance, it makes every token larger, and is
93   // also not generally what we want (it is nicer for recovery etc. to lex 123br
94   // as a single token, then diagnose as an invalid number).
95   int64_t getIntVal() const { 
96     assert(Kind == Integer && "This token isn't an integer!");
97     return IntVal; 
98   }
99 };
100
101 /// MCAsmLexer - Generic assembler lexer interface, for use by target specific
102 /// assembly lexers.
103 class MCAsmLexer {
104   /// The current token, stored in the base class for faster access.
105   AsmToken CurTok;
106   
107   /// The location and description of the current error
108   SMLoc ErrLoc;
109   std::string Err;
110
111   MCAsmLexer(const MCAsmLexer &);   // DO NOT IMPLEMENT
112   void operator=(const MCAsmLexer &);  // DO NOT IMPLEMENT
113 protected: // Can only create subclasses.
114   MCAsmLexer();
115
116   virtual AsmToken LexToken() = 0;
117   
118   void SetError(const SMLoc &errLoc, const std::string &err) {
119     ErrLoc = errLoc;
120     Err = err;
121   }
122   
123 public:
124   virtual ~MCAsmLexer();
125
126   /// Lex - Consume the next token from the input stream and return it.
127   ///
128   /// The lexer will continuosly return the end-of-file token once the end of
129   /// the main input file has been reached.
130   const AsmToken &Lex() {
131     return CurTok = LexToken();
132   }
133
134   /// getTok - Get the current (last) lexed token.
135   const AsmToken &getTok() {
136     return CurTok;
137   }
138   
139   /// getErrLoc - Get the current error location
140   const SMLoc &getErrLoc() {
141     return ErrLoc;
142   }
143            
144   /// getErr - Get the current error string
145   const std::string &getErr() {
146     return Err;
147   }
148
149   /// getKind - Get the kind of current token.
150   AsmToken::TokenKind getKind() const { return CurTok.getKind(); }
151
152   /// is - Check if the current token has kind \arg K.
153   bool is(AsmToken::TokenKind K) const { return CurTok.is(K); }
154
155   /// isNot - Check if the current token has kind \arg K.
156   bool isNot(AsmToken::TokenKind K) const { return CurTok.isNot(K); }
157 };
158
159 } // End llvm namespace
160
161 #endif