add string literals.
[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,
43     Minus,
44     Slash,    // '/'
45     LParen, RParen,
46     Star, Comma, Dollar
47   };
48 }
49
50 /// AsmLexer - Lexer class for assembly files.
51 class AsmLexer {
52   SourceMgr &SrcMgr;
53   
54   const char *CurPtr;
55   const MemoryBuffer *CurBuf;
56   
57   // Information about the current token.
58   const char *TokStart;
59   asmtok::TokKind CurKind;
60   std::string CurStrVal;  // This is valid for Identifier.
61   int64_t CurIntVal;
62   
63   /// CurBuffer - This is the current buffer index we're lexing from as managed
64   /// by the SourceMgr object.
65   int CurBuffer;
66   
67 public:
68   AsmLexer(SourceMgr &SrcMgr);
69   ~AsmLexer() {}
70   
71   asmtok::TokKind Lex() {
72     return CurKind = LexToken();
73   }
74   
75   asmtok::TokKind getKind() const { return CurKind; }
76   
77   const std::string &getCurStrVal() const {
78     assert((CurKind == asmtok::Identifier || CurKind == asmtok::Register ||
79             CurKind == asmtok::String) &&
80            "This token doesn't have a string value");
81     return CurStrVal;
82   }
83   int64_t getCurIntVal() const {
84     assert(CurKind == asmtok::IntVal && "This token isn't an integer");
85     return CurIntVal;
86   }
87   
88   SMLoc getLoc() const;
89   
90   void PrintError(const char *Loc, const std::string &Msg) const;
91   void PrintError(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