Expose Tokens to target specific assembly parsers.
[oota-llvm.git] / include / llvm / MC / 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
15 namespace llvm {
16 class MCAsmLexer;
17 class MCInst;
18 class SMLoc;
19 class Target;
20
21 /// AsmToken - Target independent representation for an assembler token.
22 struct AsmToken {
23   enum TokenKind {
24     // Markers
25     Eof, Error,
26
27     // String values.
28     Identifier,
29     Register,
30     String,
31     
32     // Integer values.
33     Integer,
34     
35     // No-value.
36     EndOfStatement,
37     Colon,
38     Plus, Minus, Tilde,
39     Slash,    // '/'
40     LParen, RParen,
41     Star, Comma, Dollar, Equal, EqualEqual,
42     
43     Pipe, PipePipe, Caret, 
44     Amp, AmpAmp, Exclaim, ExclaimEqual, Percent, 
45     Less, LessEqual, LessLess, LessGreater,
46     Greater, GreaterEqual, GreaterGreater
47   };
48
49   TokenKind Kind;
50
51   /// A reference to the entire token contents; this is always a pointer into
52   /// a memory buffer owned by the source manager.
53   StringRef Str;
54
55   int64_t IntVal;
56
57 public:
58   AsmToken() {}
59   AsmToken(TokenKind _Kind, const StringRef &_Str, int64_t _IntVal = 0)
60     : Kind(_Kind), Str(_Str), IntVal(_IntVal) {}
61
62   TokenKind getKind() const { return Kind; }
63   bool is(TokenKind K) const { return Kind == K; }
64   bool isNot(TokenKind K) const { return Kind != K; }
65
66   SMLoc getLoc() const;
67
68   /// getString - Get the string for the current token, this includes all
69   /// characters (for example, the quotes on strings) in the token.
70   ///
71   /// The returned StringRef points into the source manager's memory buffer, and
72   /// is safe to store across calls to Lex().
73   StringRef getString() const { return Str; }
74
75   // FIXME: Don't compute this in advance, it makes every token larger, and is
76   // also not generally what we want (it is nicer for recovery etc. to lex 123br
77   // as a single token, then diagnose as an invalid number).
78   int64_t getIntVal() const { 
79     assert(Kind == Integer && "This token isn't an integer");
80     return IntVal; 
81   }
82 };
83
84 /// MCAsmLexer - Generic assembler lexer interface, for use by target specific
85 /// assembly lexers.
86 class MCAsmLexer {
87   /// The current token, stored in the base class for faster access.
88   AsmToken CurTok;
89
90   MCAsmLexer(const MCAsmLexer &);   // DO NOT IMPLEMENT
91   void operator=(const MCAsmLexer &);  // DO NOT IMPLEMENT
92 protected: // Can only create subclasses.
93   MCAsmLexer();
94
95   virtual AsmToken LexToken() = 0;
96
97 public:
98   virtual ~MCAsmLexer();
99
100   /// Lex - Consume the next token from the input stream and return it.
101   ///
102   /// The lexer will continuosly return the end-of-file token once the end of
103   /// the main input file has been reached.
104   const AsmToken &Lex() {
105     return CurTok = LexToken();
106   }
107
108   /// getTok - Get the current (last) lexed token.
109   const AsmToken &getTok() {
110     return CurTok;
111   }
112
113   /// getKind - Get the kind of current token.
114   AsmToken::TokenKind getKind() const { return CurTok.getKind(); }
115
116   /// is - Check if the current token has kind \arg K.
117   bool is(AsmToken::TokenKind K) const { return CurTok.is(K); }
118
119   /// isNot - Check if the current token has kind \arg K.
120   bool isNot(AsmToken::TokenKind K) const { return CurTok.isNot(K); }
121 };
122
123 } // End llvm namespace
124
125 #endif