Added llvm-mc support for parsing the .abort directive.
[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, EqualEqual,
46     
47     Pipe, PipePipe, Caret, 
48     Amp, AmpAmp, Exclaim, ExclaimEqual, Percent, 
49     Less, LessEqual, LessLess, LessGreater,
50     Greater, GreaterEqual, GreaterGreater
51   };
52 }
53
54 /// AsmLexer - Lexer class for assembly files.
55 class AsmLexer {
56   SourceMgr &SrcMgr;
57   
58   const char *CurPtr;
59   const MemoryBuffer *CurBuf;
60   // A llvm::StringSet<>, which provides uniqued and null-terminated strings.
61   void *TheStringSet;
62   
63   // Information about the current token.
64   const char *TokStart;
65   asmtok::TokKind CurKind;
66   const char *CurStrVal;  // This is valid for Identifier.
67   int64_t CurIntVal;
68   
69   /// CurBuffer - This is the current buffer index we're lexing from as managed
70   /// by the SourceMgr object.
71   int CurBuffer;
72   
73   void operator=(const AsmLexer&); // DO NOT IMPLEMENT
74   AsmLexer(const AsmLexer&);       // DO NOT IMPLEMENT
75 public:
76   AsmLexer(SourceMgr &SrcMgr);
77   ~AsmLexer();
78   
79   asmtok::TokKind Lex() {
80     return CurKind = LexToken();
81   }
82   
83   asmtok::TokKind getKind() const { return CurKind; }
84   bool is(asmtok::TokKind K) const { return CurKind == K; }
85   bool isNot(asmtok::TokKind K) const { return CurKind != K; }
86   
87   const char *getCurStrVal() const {
88     assert((CurKind == asmtok::Identifier || CurKind == asmtok::Register ||
89             CurKind == asmtok::String) &&
90            "This token doesn't have a string value");
91     return CurStrVal;
92   }
93   int64_t getCurIntVal() const {
94     assert(CurKind == asmtok::IntVal && "This token isn't an integer");
95     return CurIntVal;
96   }
97   
98   SMLoc getLoc() const;
99   
100   void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type) const;
101   
102 private:
103   int getNextChar();
104   asmtok::TokKind ReturnError(const char *Loc, const std::string &Msg);
105
106   /// LexToken - Read the next token and return its code.
107   asmtok::TokKind LexToken();
108   asmtok::TokKind LexIdentifier();
109   asmtok::TokKind LexPercent();
110   asmtok::TokKind LexSlash();
111   asmtok::TokKind LexLineComment();
112   asmtok::TokKind LexDigit();
113   asmtok::TokKind LexQuote();
114 };
115   
116 } // end namespace llvm
117
118 #endif