Implement full support for parsing primary expressions. We can now parse
[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
46   };
47 }
48
49 /// AsmLexer - Lexer class for assembly files.
50 class AsmLexer {
51   SourceMgr &SrcMgr;
52   
53   const char *CurPtr;
54   const MemoryBuffer *CurBuf;
55   
56   // Information about the current token.
57   const char *TokStart;
58   asmtok::TokKind CurKind;
59   std::string CurStrVal;  // This is valid for Identifier.
60   int64_t CurIntVal;
61   
62   /// CurBuffer - This is the current buffer index we're lexing from as managed
63   /// by the SourceMgr object.
64   int CurBuffer;
65   
66 public:
67   AsmLexer(SourceMgr &SrcMgr);
68   ~AsmLexer() {}
69   
70   asmtok::TokKind Lex() {
71     return CurKind = LexToken();
72   }
73   
74   asmtok::TokKind getKind() const { return CurKind; }
75   bool is(asmtok::TokKind K) const { return CurKind == K; }
76   bool isNot(asmtok::TokKind K) const { return CurKind != K; }
77   
78   const std::string &getCurStrVal() const {
79     assert((CurKind == asmtok::Identifier || CurKind == asmtok::Register ||
80             CurKind == asmtok::String) &&
81            "This token doesn't have a string value");
82     return CurStrVal;
83   }
84   int64_t getCurIntVal() const {
85     assert(CurKind == asmtok::IntVal && "This token isn't an integer");
86     return CurIntVal;
87   }
88   
89   SMLoc getLoc() const;
90   
91   void PrintMessage(SMLoc Loc, const std::string &Msg) const;
92   
93 private:
94   int getNextChar();
95   asmtok::TokKind ReturnError(const char *Loc, const std::string &Msg);
96
97   /// LexToken - Read the next token and return its code.
98   asmtok::TokKind LexToken();
99   asmtok::TokKind LexIdentifier();
100   asmtok::TokKind LexPercent();
101   asmtok::TokKind LexSlash();
102   asmtok::TokKind LexHash();
103   asmtok::TokKind LexDigit();
104   asmtok::TokKind LexQuote();
105 };
106   
107 } // end namespace llvm
108
109 #endif