MC: Parse .set and assignments.
[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/Support/DataTypes.h"
18 #include <string>
19 #include <cassert>
20
21 namespace llvm {
22 class MemoryBuffer;
23 class SourceMgr;
24 class SMLoc;
25
26 namespace asmtok {
27   enum TokKind {
28     // Markers
29     Eof, Error,
30
31     // String values.
32     Identifier,
33     Register,
34     String,
35     
36     // Integer values.
37     IntVal,
38     
39     // No-value.
40     EndOfStatement,
41     Colon,
42     Plus, Minus, Tilde,
43     Slash,    // '/'
44     LParen, RParen,
45     Star, Comma, Dollar, Equal,
46     
47     Pipe, Caret, Amp, Exclaim,
48     Percent, LessLess, GreaterGreater
49   };
50 }
51
52 /// AsmLexer - Lexer class for assembly files.
53 class AsmLexer {
54   SourceMgr &SrcMgr;
55   
56   const char *CurPtr;
57   const MemoryBuffer *CurBuf;
58   // A llvm::StringSet<>, which provides uniqued and null-terminated strings.
59   void *TheStringSet;
60   
61   // Information about the current token.
62   const char *TokStart;
63   asmtok::TokKind CurKind;
64   const char *CurStrVal;  // This is valid for Identifier.
65   int64_t CurIntVal;
66   
67   /// CurBuffer - This is the current buffer index we're lexing from as managed
68   /// by the SourceMgr object.
69   int CurBuffer;
70   
71   void operator=(const AsmLexer&); // DO NOT IMPLEMENT
72   AsmLexer(const AsmLexer&);       // DO NOT IMPLEMENT
73 public:
74   AsmLexer(SourceMgr &SrcMgr);
75   ~AsmLexer();
76   
77   asmtok::TokKind Lex() {
78     return CurKind = LexToken();
79   }
80   
81   asmtok::TokKind getKind() const { return CurKind; }
82   bool is(asmtok::TokKind K) const { return CurKind == K; }
83   bool isNot(asmtok::TokKind K) const { return CurKind != K; }
84   
85   const char *getCurStrVal() const {
86     assert((CurKind == asmtok::Identifier || CurKind == asmtok::Register ||
87             CurKind == asmtok::String) &&
88            "This token doesn't have a string value");
89     return CurStrVal;
90   }
91   int64_t getCurIntVal() const {
92     assert(CurKind == asmtok::IntVal && "This token isn't an integer");
93     return CurIntVal;
94   }
95   
96   SMLoc getLoc() const;
97   
98   void PrintMessage(SMLoc Loc, const std::string &Msg) const;
99   
100 private:
101   int getNextChar();
102   asmtok::TokKind ReturnError(const char *Loc, const std::string &Msg);
103
104   /// LexToken - Read the next token and return its code.
105   asmtok::TokKind LexToken();
106   asmtok::TokKind LexIdentifier();
107   asmtok::TokKind LexPercent();
108   asmtok::TokKind LexSlash();
109   asmtok::TokKind LexHash();
110   asmtok::TokKind LexDigit();
111   asmtok::TokKind LexQuote();
112 };
113   
114 } // end namespace llvm
115
116 #endif