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