implement a trivial binary expression parser, we can now parse all of 176.gcc.llc.s
[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     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   
59   // Information about the current token.
60   const char *TokStart;
61   asmtok::TokKind CurKind;
62   std::string CurStrVal;  // This is valid for Identifier.
63   int64_t CurIntVal;
64   
65   /// CurBuffer - This is the current buffer index we're lexing from as managed
66   /// by the SourceMgr object.
67   int CurBuffer;
68   
69 public:
70   AsmLexer(SourceMgr &SrcMgr);
71   ~AsmLexer() {}
72   
73   asmtok::TokKind Lex() {
74     return CurKind = LexToken();
75   }
76   
77   asmtok::TokKind getKind() const { return CurKind; }
78   bool is(asmtok::TokKind K) const { return CurKind == K; }
79   bool isNot(asmtok::TokKind K) const { return CurKind != K; }
80   
81   const std::string &getCurStrVal() const {
82     assert((CurKind == asmtok::Identifier || CurKind == asmtok::Register ||
83             CurKind == asmtok::String) &&
84            "This token doesn't have a string value");
85     return CurStrVal;
86   }
87   int64_t getCurIntVal() const {
88     assert(CurKind == asmtok::IntVal && "This token isn't an integer");
89     return CurIntVal;
90   }
91   
92   SMLoc getLoc() const;
93   
94   void PrintMessage(SMLoc Loc, const std::string &Msg) const;
95   
96 private:
97   int getNextChar();
98   asmtok::TokKind ReturnError(const char *Loc, const std::string &Msg);
99
100   /// LexToken - Read the next token and return its code.
101   asmtok::TokKind LexToken();
102   asmtok::TokKind LexIdentifier();
103   asmtok::TokKind LexPercent();
104   asmtok::TokKind LexSlash();
105   asmtok::TokKind LexHash();
106   asmtok::TokKind LexDigit();
107   asmtok::TokKind LexQuote();
108 };
109   
110 } // end namespace llvm
111
112 #endif