9e694c7a3010363b67f418b2c76d3d60c8177b70
[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     Identifier,
32     Register,
33     IntVal,
34     
35     EndOfStatement,
36     Colon,
37     Plus,
38     Minus,
39     Slash,    // '/'
40     LParen, RParen,
41     Star, Comma, Dollar
42   };
43 }
44
45 /// AsmLexer - Lexer class for assembly files.
46 class AsmLexer {
47   SourceMgr &SrcMgr;
48   
49   const char *CurPtr;
50   const MemoryBuffer *CurBuf;
51   
52   // Information about the current token.
53   const char *TokStart;
54   asmtok::TokKind CurKind;
55   std::string CurStrVal;  // This is valid for Identifier.
56   int64_t CurIntVal;
57   
58   /// CurBuffer - This is the current buffer index we're lexing from as managed
59   /// by the SourceMgr object.
60   int CurBuffer;
61   
62 public:
63   AsmLexer(SourceMgr &SrcMgr);
64   ~AsmLexer() {}
65   
66   asmtok::TokKind Lex() {
67     return CurKind = LexToken();
68   }
69   
70   asmtok::TokKind getKind() const { return CurKind; }
71   
72   const std::string &getCurStrVal() const {
73     assert((CurKind == asmtok::Identifier || CurKind == asmtok::Register) &&
74            "This token doesn't have a string value");
75     return CurStrVal;
76   }
77   int64_t getCurIntVal() const {
78     assert(CurKind == asmtok::IntVal && "This token isn't an integer");
79     return CurIntVal;
80   }
81   
82   SMLoc getLoc() const;
83   
84   void PrintError(const char *Loc, const std::string &Msg) const;
85   void PrintError(SMLoc Loc, const std::string &Msg) const;
86   
87 private:
88   int getNextChar();
89   asmtok::TokKind ReturnError(const char *Loc, const std::string &Msg);
90
91   /// LexToken - Read the next token and return its code.
92   asmtok::TokKind LexToken();
93   asmtok::TokKind LexIdentifier();
94   asmtok::TokKind LexPercent();
95   asmtok::TokKind LexSlash();
96   asmtok::TokKind LexHash();
97   asmtok::TokKind LexDigit();
98 };
99   
100 } // end namespace llvm
101
102 #endif